Skip to content

Commit

Permalink
Add unit tests (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojtek242 authored Feb 25, 2024
1 parent b3e0f96 commit 4528c1b
Show file tree
Hide file tree
Showing 25 changed files with 2,688 additions and 524 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/ci-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ jobs:
run: cp msrv.lock Cargo.lock
- name: cargo build
run: cargo build --all-targets ${{ inputs.profile }} ${{ inputs.features }}
# Since secrets are not passed to workflows that are triggered by a pull request from a fork
# the npcap dll won't be installed so we skip this step on pull requests. It will
# nevertheless be run once merged and the pull request will still verify the build steps.
- name: cargo test (windows pull-request special)
if: (inputs.os == 'windows-latest') && (github.event_name == 'pull_request')
run: cargo test --lib ${{ inputs.profile }} ${{ inputs.features }}
- name: cargo test
# Since secrets are not passed to workflows that are triggered by a pull request from a fork
# the npcap dll won't be installed so we skip this step on pull requests. It will
# nevertheless be run once merged and the pull request will still verify the build steps.
if: (inputs.os != 'windows-latest') || (github.event_name != 'pull_request')
run: cargo test --all-targets ${{ inputs.profile }} ${{ inputs.features }}
- name: cargo clippy
Expand Down
33 changes: 18 additions & 15 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ The current code coverage for the `main` branch is automatically published on ev

### Pre-requisites

To obtain code coverage locally you will need the nightly compiler toolchain, [compatible LLVM coverage tools](https://doc.rust-lang.org/rustc/instrument-coverage.html#installing-llvm-coverage-tools), and [`grcov`](https://github.com/mozilla/grcov).

Make sure you have the nightly toolchain installed with `rustup`:
```
rustup install nightly
```
To obtain code coverage locally you will need [compatible LLVM coverage tools](https://doc.rust-lang.org/rustc/instrument-coverage.html#installing-llvm-coverage-tools) and [`grcov`](https://github.com/mozilla/grcov).

The easiest way to obtain compatible LLVM coverage tools is by adding the `llvm-tools-preview` `rustup` component (for nightly!):
```
rustup +nightly component add llvm-tools-preview
rustup component add llvm-tools-preview
```

`grcov` can be installed through cargo:
Expand All @@ -25,24 +20,32 @@ cargo install grcov

### Obtaining code coverage

First, switch to the nightly toolchain. Note that switching toolchains is necessary, as opposed to just using the `+nightly` option, because `grcov` does not have such an option and will not be able to find (or worse, will find the incorrect version of) `llvm-tools-preview`.
```
rustup default nightly
```

Clean any previously compiled objects and binaries.
```
cargo clean
rm -rf ./target/debug/{coverage,profraw}
cargo clean -p pcap
```

Compile and run the tests. We set `RUSTFLAGS="-C instrument-coverage"` to enable source-based code coverage and `LLVM_PROFILE_FILE="target/debug/coverage/profraw/pcap-%p-%m.profraw"` to make sure each test gets its own profile information.
```
RUSTFLAGS="-C instrument-coverage" LLVM_PROFILE_FILE="target/debug/profraw/pcap-%p-%m.profraw" cargo test --all-features
env RUSTFLAGS="-C instrument-coverage" \
LLVM_PROFILE_FILE="target/debug/profraw/pcap-%p-%m.profraw" \
cargo test --all-features --all-targets
```

And finally, run `grcov` to obtain a report.
```
grcov target/debug/profraw -s src/ --binary-path ./target/debug/ -t html --branch --ignore-not-existing -o ./target/debug/coverage/
grcov target/debug/profraw \
--binary-path ./target/debug/ \
--output-types html \
--source-dir . \
--ignore-not-existing \
--ignore "build.rs" \
--ignore "tests/*" \
--ignore "examples/*" \
--excl-start "GRCOV_EXCL_START|mod tests \{" \
--excl-stop "GRCOV_EXCL_STOP" \
--output-path ./target/debug/coverage/
```

The code coverage report will be available in `target/debug/coverage/index.html` which you can explore in your browser.
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ windows-sys = { version = "0.36.1", features = ["Win32_Foundation", "Win32_Netwo
tun-tap = { version = "0.1.3", optional = true }

[dev-dependencies]
once_cell = "1.14.0"
mockall = "0.11.4"
tempdir = "0.3"

[target.'cfg(target_os = "windows")'.dev-dependencies]
Expand Down
132 changes: 129 additions & 3 deletions msrv.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "aho-corasick"
version = "0.7.18"
Expand Down Expand Up @@ -120,6 +118,24 @@ dependencies = [
"lazy_static",
]

[[package]]
name = "difflib"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"

[[package]]
name = "downcast"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1435fa1053d8b2fbbe9be7e97eca7f33d37b28409959813daefc1446a14247f1"

[[package]]
name = "either"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a"

[[package]]
name = "errno"
version = "0.2.8"
Expand Down Expand Up @@ -160,12 +176,27 @@ dependencies = [
"rustc-serialize",
]

[[package]]
name = "float-cmp"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4"
dependencies = [
"num-traits",
]

[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"

[[package]]
name = "fragile"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c2141d6d6c8512188a7891b4b01590a45f6dac67afb4f255c4124dbb86d4eaa"

[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
Expand Down Expand Up @@ -321,6 +352,15 @@ dependencies = [
"libc",
]

[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]

[[package]]
name = "kernel32-sys"
version = "0.2.2"
Expand Down Expand Up @@ -457,6 +497,33 @@ dependencies = [
"winapi 0.3.9",
]

[[package]]
name = "mockall"
version = "0.11.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c84490118f2ee2d74570d114f3d0493cbf02790df303d2707606c3e14e07c96"
dependencies = [
"cfg-if 1.0.0",
"downcast",
"fragile",
"lazy_static",
"mockall_derive",
"predicates",
"predicates-tree",
]

[[package]]
name = "mockall_derive"
version = "0.11.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ce75669015c4f47b289fd4d4f56e894e4c96003ffdf3ac51313126f94c6cbb"
dependencies = [
"cfg-if 1.0.0",
"proc-macro2",
"quote",
"syn",
]

[[package]]
name = "net2"
version = "0.2.38"
Expand All @@ -468,6 +535,12 @@ dependencies = [
"winapi 0.3.9",
]

[[package]]
name = "normalize-line-endings"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be"

[[package]]
name = "ntapi"
version = "0.3.7"
Expand All @@ -477,6 +550,15 @@ dependencies = [
"winapi 0.3.9",
]

[[package]]
name = "num-traits"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a"
dependencies = [
"autocfg",
]

[[package]]
name = "num_cpus"
version = "1.13.1"
Expand All @@ -487,6 +569,12 @@ dependencies = [
"libc",
]

[[package]]
name = "once_cell"
version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0"

[[package]]
name = "parking_lot"
version = "0.9.0"
Expand Down Expand Up @@ -515,7 +603,7 @@ dependencies = [

[[package]]
name = "pcap"
version = "1.1.0"
version = "1.2.0"
dependencies = [
"bitflags",
"errno",
Expand All @@ -525,6 +613,8 @@ dependencies = [
"gat-std",
"libc",
"libloading",
"mockall",
"once_cell",
"pkg-config",
"regex",
"tempdir",
Expand All @@ -551,6 +641,36 @@ version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"

[[package]]
name = "predicates"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5aab5be6e4732b473071984b3164dbbfb7a3674d30ea5ff44410b6bcd960c3c"
dependencies = [
"difflib",
"float-cmp",
"itertools",
"normalize-line-endings",
"predicates-core",
"regex",
]

[[package]]
name = "predicates-core"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da1c2388b1513e1b605fcec39a95e0a9e8ef088f71443ef37099fa9ae6673fcb"

[[package]]
name = "predicates-tree"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d86de6de25020a36c6d3643a86d9a6a9f552107c0559c60ea03551b5e16c032"
dependencies = [
"predicates-core",
"termtree",
]

[[package]]
name = "proc-macro2"
version = "1.0.37"
Expand Down Expand Up @@ -726,6 +846,12 @@ dependencies = [
"remove_dir_all",
]

[[package]]
name = "termtree"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b"

[[package]]
name = "tokio"
version = "0.1.22"
Expand Down
Loading

0 comments on commit 4528c1b

Please sign in to comment.