Skip to content

Commit

Permalink
Basic wasm API dissect_packet and CI actions
Browse files Browse the repository at this point in the history
- Added a basic API `dissect_packet` if configured to build with `wasm`
  feature. This is a demonstration of the basic `wasm` feature.
- Fixed issue with compilation of examples when `wasm` feature was
  enabled.
- Added some simple tests when building with `wasm` feature.

Co-authored-by: csking101 <chinmayasahu101@gmail.com>
Signed-off-by: Abhijit Gadgil <gabhijit@iitbombay.org>
  • Loading branch information
gabhijit and csking101 committed Apr 4, 2024
1 parent 952ffcb commit df86e98
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@ jobs:
cargo test --verbose --features python-bindings,logging
python -m pip install scalpel_python_bindings --find-links dist/
python -c 'import scalpel; print(scalpel.Packet.from_bytes_py(bytes.fromhex("000573a007d168a3c4f949f686dd600000000020064020010470e5bfdead49572174e82c48872607f8b0400c0c03000000000000001af9c7001903a088300000000080022000da4700000204058c0103030801010402"), 1).as_json())'
- name: Install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

- name: Run wasm-pack tests in firefox
run: wasm-pack test --firefox --headless --features wasm

- name: Run wasm-pack tests in chrome
run: wasm-pack test --chrome --headless --features wasm

- name: Run wasm-pack tests in node
run: wasm-pack test --node --features wasm
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ clap = { version = "4.0" , features = ["derive"] }
pcap = { version = "1.3"}
criterion = "0.5"

[target.'cfg(target_family = "wasm")'.dev-dependencies]
wasm-bindgen-test = { version = "0.3"}

# Required by `cargo flamegraph`
[profile.bench]
debug = true
Expand Down
7 changes: 6 additions & 1 deletion examples/pcap_example.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use clap::Parser;
use pcap::{Capture, Device, Linktype};

#[derive(Parser, Debug)]
struct Opts {
Expand All @@ -12,7 +11,10 @@ struct Opts {
num_packets: Option<u32>,
}

#[cfg(not(feature = "wasm"))]
fn main() -> Result<(), Box<dyn std::error::Error>> {
use pcap::{Capture, Device, Linktype};

// Command Line Handling
let opts = Opts::parse();

Expand Down Expand Up @@ -54,3 +56,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

Ok(())
}

#[cfg(feature = "wasm")]
fn main() {}
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,22 @@ fn scalpel(py: Python, m: &PyModule) -> PyResult<()> {
packet::register(py, m)?;
Ok(())
}

#[cfg(all(not(feature = "python-bindings"), target_family = "wasm"))]
use wasm_bindgen::prelude::*;

#[cfg(all(not(feature = "python-bindings"), target_family = "wasm"))]
#[wasm_bindgen]
pub fn dissect_packet(packet: String) -> String {
let _ = layers::register_defaults();

let packet = hex::decode(packet);

let packet = packet.unwrap();

let p = Packet::from_bytes(&packet, ENCAP_TYPE_ETH);

let p = p.unwrap();

serde_json::to_string_pretty(&p).unwrap()
}
11 changes: 11 additions & 0 deletions src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ pub(crate) fn register(_py: Python, m: &PyModule) -> PyResult<()> {
#[cfg(test)]
mod tests {

#[cfg(feature = "wasm")]
use wasm_bindgen_test::wasm_bindgen_test;

use super::*;
use hex;

Expand All @@ -211,6 +214,7 @@ mod tests {
use crate::layers::tcp::TCP_BASE_HEADER_LENGTH;
use crate::{ENCAP_TYPE_ETH, ENCAP_TYPE_LINUX_SLL};

#[cfg_attr(feature = "wasm", wasm_bindgen_test)]
#[test]
fn from_bytes_fail_too_short() {
let _ = crate::layers::register_defaults();
Expand All @@ -220,6 +224,7 @@ mod tests {
assert!(p.is_err(), "{:?}", p.ok());
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test)]
#[test]
fn from_bytes_success_eth_hdr_size() {
let _ = crate::layers::register_defaults();
Expand All @@ -229,6 +234,7 @@ mod tests {
assert!(p.is_ok(), "{:?}", p.err());
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test)]
#[test]
fn parse_valid_ipv4_packet() {
let _ = layers::register_defaults();
Expand All @@ -253,6 +259,7 @@ mod tests {
);
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test)]
#[test]
fn parse_valid_ipv6_packet() {
let _ = layers::register_defaults();
Expand All @@ -277,6 +284,7 @@ mod tests {
);
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test)]
#[test]
fn parse_valid_dns_packet() {
use crate::layers;
Expand Down Expand Up @@ -309,6 +317,7 @@ mod tests {
assert!(false, "{}", register_defaults);
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test)]
#[test]
fn parse_failing_packet() {
use crate::layers;
Expand All @@ -319,6 +328,7 @@ mod tests {
assert!(should_not_fail.is_ok(), "{:?}", should_not_fail.err());
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test)]
#[test]
fn parse_failing_packet_2() {
use crate::layers;
Expand All @@ -329,6 +339,7 @@ mod tests {
assert!(should_not_fail.is_ok(), "{:?}", should_not_fail.err());
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test)]
#[test]
fn parse_failing_packet_3() {
use crate::layers;
Expand Down
13 changes: 13 additions & 0 deletions tests/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Test suite for Node JS
#![cfg(target_arch = "wasm32")]

extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;

#[wasm_bindgen_test]
fn simple_dissect_test() {
let bytestream = "003096e6fc3900309605283888470001ddff45c0002800030000ff06a4e80a0102010a2200012af90017983210058dd58ea55010102099cd00000000";
scalpel::dissect_packet(bytestream.to_string());
assert!(true);
}
15 changes: 15 additions & 0 deletions tests/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Test suite for the Web and headless browsers.
#![cfg(target_arch = "wasm32")]

extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn simple_dissect_test() {
let bytestream = "003096e6fc3900309605283888470001ddff45c0002800030000ff06a4e80a0102010a2200012af90017983210058dd58ea55010102099cd00000000";
scalpel::dissect_packet(bytestream.to_string());
assert!(true);
}

0 comments on commit df86e98

Please sign in to comment.