Skip to content

Commit

Permalink
minor markdown nitpicks
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliotFriend committed Dec 20, 2024
1 parent 073eb23 commit b63f2b5
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ Test snapshots files are verbose. Test snapshots are most useful when changes ap

To give this a go, check out the [Getting Started] contract or any of the [examples], run the tests, and look for the test snapshots on disk.

[Differential Testing]: ./differential-tests
[Differential Testing]: ./differential-tests.mdx
[Getting Started]: ../../smart-contracts/getting-started/README.mdx
[examples]: ../../smart-contracts/example-contracts/README.mdx
12 changes: 6 additions & 6 deletions docs/build/guides/testing/differential-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ All contracts built with the Rust Soroban SDK have a form of differential testin

:::

### How to Write Differential Tests
## How to Write Differential Tests

To experiment with writing a differential test, open a contract that you've deployed, or checkout an example from the [soroban-examples] repository and deploy it.

Assuming the contract has been deployed, and changes are being made to the local copy. We need to check that unchanged behavior in the contract hasn't changed compared to what is deployed.

1. Use the [stellar contract fetch] command to fetch the contract that's already deployed. The contract already deployed will be used as a baseline that the local copy is expected to behave like.

```
```shell
stellar contract fetch --id C... --out-file contract.wasm
```

Expand Down Expand Up @@ -112,8 +112,8 @@ Depending on the test complexity it can be desirable to use an independent `Env`

[Getting Started]: ../../smart-contracts/getting-started
[increment example]: https://github.com/stellar/soroban-examples/blob/main/increment/src/lib.rs
[Differential Testing with Test Snapshots]: ./differential-tests-with-test-snapshots
[stellar contract fetch]: ../../../tools/developer-tools/cli/stellar-cli#stellar-contract-fetch
[integration tests]: integration-tests
[unit tests]: unit-tests
[Differential Testing with Test Snapshots]: ./differential-tests-with-test-snapshots.mdx
[stellar contract fetch]: ../../../tools/developer-tools/cli/stellar-cli.mdx#stellar-contract-fetch
[integration tests]: ./integration-tests.mdx
[unit tests]: ./unit-tests.mdx
[stellar/rs-soroban-sdk#1360]: https://github.com/stellar/rs-soroban-sdk/issues/1360
23 changes: 11 additions & 12 deletions docs/build/guides/testing/fuzzing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ Fuzz tests can also be written as property tests that instead of seeking to iden

The following steps can be used in any Stellar contract workspace. If experimenting, try them in the [increment example]. The contract has an `increment` function that increases a counter value by one on every invocation.

### How to Write Fuzz Tests
## How to Write Fuzz Tests

1. Install `cargo-fuzz`.

```console
$ cargo install --locked cargo-fuzz
```shell
cargo install --locked cargo-fuzz
```

2. Initialize a fuzz project by running the following command inside your contract directory.

```console
$ cargo fuzz init
```shell
cargo fuzz init
```

3. Open the contract's `Cargo.toml` file. Add `lib` as a `crate-type`.
Expand Down Expand Up @@ -87,7 +87,7 @@ The following steps can be used in any Stellar contract workspace. If experiment

7. Execute the fuzz target.

```
```shell
cargo +nightly fuzz run --sanitizer=thread fuzz_target_1
```

Expand Down Expand Up @@ -116,12 +116,11 @@ There is another tool for fuzzing Rust code, `cargo-afl`. See the [Rust Fuzz boo

:::

[Getting Started]: ../../smart-contracts/getting-started
[increment example]: https://github.com/stellar/soroban-examples/blob/main/increment/src/lib.rs
[Differential Testing with Test Snapshots]: ./differential-testing-with-test-snapshots
[stellar contract fetch]: ../../tools/developer-tools/cli/stellar-cli#stellar-contract-fetch
[integration tests]: ./integration-tests
[unit tests]: ./unit-tests
[Differential Testing with Test Snapshots]: ./differential-tests-with-test-snapshots.mdx
[stellar contract fetch]: ../../../tools/developer-tools/cli/stellar-cli.mdx#stellar-contract-fetch
[integration tests]: ./integration-tests.mdx
[unit tests]: ./unit-tests.mdx
[stellar/rs-soroban-sdk#1360]: https://github.com/stellar/rs-soroban-sdk/issues/1360
[fuzzing example]: ../../smart-contracts/example-contracts/fuzzing
[fuzzing example]: ../../smart-contracts/example-contracts/fuzzing.mdx
[Rust Fuzz Book]: https://rust-fuzz.github.io/book
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Testing with mainnet data is one way to further close the gap.

The [Soroban Rust SDK] and [Stellar CLI] come together to make possible testing with mainnet data.

### How to Write Tests with Mainnet Data
## How to Write Tests with Mainnet Data

The following is an example of a test that includes a dependency contract into the test, rather than mock it. The test is written to test the [increment-with-pause contract] and the [pause contract]. The contract has an `increment` function that increases a counter value by one on every invocation. The contract depends on the pause contract to control whether the increment functionality is paused.

Expand Down Expand Up @@ -78,7 +78,7 @@ A snapshot can be created of any contract deployed to mainnet or testnet using t

[increment-with-pause contract]: https://github.com/stellar/soroban-examples/blob/main/increment-with-pause/src/lib.rs
[pause contract]: https://github.com/stellar/soroban-examples/blob/main/pause/src/lib.rs
[integration test]: ./integration-tests
[Making Cross-Contract Calls]: ../conventions/cross-contract
[Soroban Rust SDK]: ../../../tools/sdks/library#soroban-rust-sdk
[Stellar CLI]: ../../../tools/developer-tools/cli
[integration test]: ./integration-tests.mdx
[Making Cross-Contract Calls]: ../conventions/cross-contract.mdx
[Soroban Rust SDK]: ../../../tools/sdks/library.mdx#soroban-rust-sdk
[Stellar CLI]: ../../../tools/developer-tools/cli/README.mdx
10 changes: 5 additions & 5 deletions docs/build/guides/testing/integration-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Integration tests are tests that include the integration between components, and

The [Soroban Rust SDK] makes it just as easy to integration test by providing utilities for testing against real contracts fetched from mainnet, testnet, or the local file system.

### How to Write Integration Tests
## How to Write Integration Tests

The following is an example of a test that includes a dependency contract into the test, rather than mock it. The test is written to test the [increment-with-pause contract] and the [pause contract]. The contract has an `increment` function that increases a counter value by one on every invocation. The contract depends on the pause contract to control whether the increment functionality is paused.

The following tests set up the `increment-with-pause` contract, as well as import and register the real pause contract using it's wasm file.

1. Use the [stellar contract fetch] command to fetch the dependency contract that's already deployed on testnet or mainnet.

```
```shell
stellar contract fetch --id C... > pause.wasm
```

Expand Down Expand Up @@ -67,6 +67,6 @@ Most tests, whether they're unit, mocks, or integration tests, will look very si

[increment-with-pause contract]: https://github.com/stellar/soroban-examples/blob/main/increment-with-pause/src/lib.rs
[pause contract]: https://github.com/stellar/soroban-examples/blob/main/pause/src/lib.rs
[Integration Tests]: ./integration-tests
[Making Cross-Contract Calls]: ../conventions/cross-contract
[Soroban Rust SDK]: ../../../tools/sdks/library#soroban-rust-sdk
[Integration Tests]: ./integration-tests.mdx
[Making Cross-Contract Calls]: ../conventions/cross-contract.mdx
[Soroban Rust SDK]: ../../../tools/sdks/library.mdx#soroban-rust-sdk
8 changes: 4 additions & 4 deletions docs/build/guides/testing/mocking.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The [Soroban Rust SDK] makes it just as easy to test against a real contract as

:::

### How to Write Tests with Mocks
## How to Write Tests with Mocks

The following is an example of a test that uses a mock, written to test the [increment-with-pause contract]. The contract has an `increment` function that increases a counter value by one on every invocation. The contract depends on another contract that controls whether the increment functionality is paused.

Expand Down Expand Up @@ -121,6 +121,6 @@ The [Soroban Rust SDK] handles contract calls defensively so that any unexpected
:::

[increment-with-pause contract]: https://github.com/stellar/soroban-examples/blob/main/increment-with-pause/src/lib.rs
[Integration Tests]: ./integration-tests
[Making Cross-Contract Calls]: ../conventions/cross-contract
[Soroban Rust SDK]: ../../../tools/sdks/library#soroban-rust-sdk
[Integration Tests]: ./integration-tests.mdx
[Making Cross-Contract Calls]: ../conventions/cross-contract.mdx
[Soroban Rust SDK]: ../../../tools/sdks/library.mdx#soroban-rust-sdk
6 changes: 3 additions & 3 deletions docs/build/guides/testing/mutation-testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ Mutation testing is making changes to a program, either manually or automaticall

Mutation testing is similar to measuring [code coverage]. It's goal is to identify code not covered by tests, or possibly that looks like it's covered by tests because the lines of code are executing during a test, but the outcomes themselves such as contract function return value, or events published, are not tested.

### How to do Mutation Testing
## How to do Mutation Testing

The `cargo-mutants` tool can be used to automatically and iteratively modify the Rust code, and rerun the tests after each mutation, to identify code not tested.

1. Install `cargo-mutants`:

```console
```shell
cargo install --locked cargo-mutants
```

2. Run the `cargo mutants` command inside your contract's crate directory.

```console
```shell
$ cargo mutants
Found 4 mutants to test
ok Unmutated baseline in 19.0s build + 0.6s test
Expand Down
2 changes: 1 addition & 1 deletion docs/build/guides/testing/unit-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sidebar_position: 1

Unit tests are small tests that test one piece of functionality within a contract.

### How to Write Unit Tests
## How to Write Unit Tests

The following is an example of a unit test, written to test the [increment contract]. The contract has an `increment` function, that increases a counter value by one on every invocation. The following test invokes that contract's function several times, and checks that the value increases by one.

Expand Down
34 changes: 17 additions & 17 deletions docs/build/smart-contracts/example-contracts/fuzzing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ First go through the [setup] process to get your development environment configu

[setup]: ../../smart-contracts/getting-started/setup.mdx

```sh
```shell
git clone -b v21.6.0 https://github.com/stellar/soroban-examples
```

You will also need the `cargo-fuzz` tool, and to run `cargo-fuzz` you will need a nightly Rust toolchain:

```sh
```shell
cargo install cargo-fuzz
rustup install nightly
```

To run one of the fuzz tests, navigate to the `fuzzing` directory and run the `cargo fuzz` subcommand with the `nightly` toolchain:

```sh
```shell
cd fuzzing
cargo +nightly fuzz run fuzz_target_1
```
Expand All @@ -63,7 +63,7 @@ If you're developing on MacOS you may need to add the `--sanitizer=thread` flag

You should see output that begins like this:

```console
```shell
$ cargo +nightly fuzz run fuzz_target_1
Compiling soroban-fuzzing-contract v0.0.0 (/home/azureuser/data/stellar/soroban-examples/fuzzing)
Compiling soroban-fuzzing-contract-fuzzer v0.0.0 (/home/azureuser/data/stellar/soroban-examples/fuzzing/fuzz)
Expand Down Expand Up @@ -161,7 +161,7 @@ For these examples, the fuzz tests have been created for you, but normally you w
To do that you would navigate to the contract directory, in this case, `soroban-examples/fuzzing`, and execute
```sh
```shell
cargo fuzz init
```
Expand Down Expand Up @@ -247,7 +247,7 @@ First let's look at [`fuzz_target_1.rs`]. This fuzz test does two things: it fir
Again, you can run this fuzzer from the `soroban-examples/fuzzing` directory with the following command:
```sh
```shell
cargo +nightly fuzz run fuzz_target_1
```
Expand Down Expand Up @@ -394,7 +394,7 @@ fn assert_invariants(
If you run `cargo-fuzz` with `fuzz_target_1`, from inside the `soroban-examples/fuzzing` directory, you will see output similar to:
```console
```shell
$ cargo +nightly fuzz run fuzz_target_1
Compiling soroban-fuzzing-contract v0.0.0 (/home/azureuser/data/stellar/soroban-examples/fuzzing)
Compiling soroban-fuzzing-contract-fuzzer v0.0.0 (/home/azureuser/data/stellar/soroban-examples/fuzzing/fuzz)
Expand Down Expand Up @@ -465,7 +465,7 @@ This is a fuzzing failure, indicating a bug in either the fuzzer or the program.
Here is the same output, with less important lines trimmed:
```
```shell
thread '<unnamed>' panicked at 'assertion failed: claimable_balance.amount > 0', fuzz_targets/fuzz_target_1.rs:130:13
...
Failing input:
Expand All @@ -492,15 +492,15 @@ The first line here is printed by our Rust program, and indicates exactly where
The first thing to do when you get a fuzzing failure is copy the command to reproduce the failure, so that you can use it to debug:
```sh
```shell
cargo +nightly fuzz run fuzz_target_1 fuzz/artifacts/fuzz_target_1/crash-04704b1542f61a21a4649e39023ec57ff502f627
```
Notice though that we need to tell `cargo` to use the nightly toolchain with the `+nightly` flag, something that `cargo-fuzz` doesn't print in its version of the command.
Another thing to notice is that by default, `cargo-fuzz` / `libfuzzer` does not print names of functions in its output, as in the stack trace:
```
```shell
==6102== ERROR: libFuzzer: deadly signal
#0 0x561f6ae3a431 (/home/azureuser/data/stellar/soroban-examples/fuzzing/fuzz/target/x86_64-unknown-linux-gnu/release/fuzz_target_1+0x1c80431) (BuildId: 6a95a932984a405ebab8171dddc9f812fdf16846)
...
Expand All @@ -511,13 +511,13 @@ Another thing to notice is that by default, `cargo-fuzz` / `libfuzzer` does not
Depending on how your system is set up, you may or may not have this problem. In order to print stack traces, `libfuzzer` needs the `llvm-symbolizer` program. On Ubuntu-based systems this can be installed with the `llvm-dev` package:
```sh
```shell
sudo apt install llvm-dev
```
After which `libfuzzer` will print demangled function names instead of addresses:
```
```shell
==6323== ERROR: libFuzzer: deadly signal
#0 0x557c9da6a431 in __sanitizer_print_stack_trace /rustc/llvm/src/llvm-project/compiler-rt/lib/asan/asan_stack.cpp:87:3
#1 0x557ca0fb55b0 in fuzzer::PrintStackTrace() /home/azureuser/.cargo/registry/src/index.crates.io-6f17d22bba15001f/libfuzzer-sys-0.4.5/libfuzzer/FuzzerUtil.cpp:210:38
Expand All @@ -541,7 +541,7 @@ To continue, our program has a bug that should be easy to fix by inspecting the
Once the bug is fixed, the fuzzer will run continuously, producing output that looks like
```console
```shell
$ cargo +nightly fuzz run fuzz_target_1
Compiling soroban-fuzzing-contract v0.0.0 (/home/azureuser/data/stellar/soroban-examples/fuzzing)
Compiling soroban-fuzzing-contract-fuzzer v0.0.0 (/home/azureuser/data/stellar/soroban-examples/fuzzing/fuzz)
Expand Down Expand Up @@ -583,7 +583,7 @@ And this output will continue until the fuzzer is killed with `Ctrl-C`.
Next, let's look at a single line of fuzzer output:
```
```shell
#177 NEW cov: 8545 ft: 13821 corp: 43/1419b lim: 48 exec/s: 29 rss: 384Mb L: 32/48 MS: 1 ChangeASCIIInt-
```
Expand All @@ -593,19 +593,19 @@ The most important column here is `cov`. This is a cumulative measure of branche
Finally, lets look at this warning:
```
```shell
INFO: -max_len is not provided; libFuzzer will not generate inputs larger than 4096 bytes.
```
By default, `libfuzzer` only generates input up to 4096 bytes. In a lot of cases, this is probably reasonable, but `cargo-fuzz` can increase the `max_len` by appending the argument after `--`:
```sh
```shell
cargo +nightly fuzz run fuzz_target_1 -- -max_len=20000
```
All the options to libfuzzer can be listed with
```sh
```shell
cargo +nightly fuzz run fuzz_target_1 -- -help=1
```
Expand Down

0 comments on commit b63f2b5

Please sign in to comment.