Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cerclique committed Aug 20, 2024
1 parent 515d37c commit 0a99861
Show file tree
Hide file tree
Showing 19 changed files with 452 additions and 84 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ resolver = "2"
[workspace.package]
authors = ["Aurélien VERNIZEAU <[email protected]>"]
edition = "2021"
repository = "https://github.com/cerclique/widigen"
repository = "https://github.com/cerclique/wirdigen"
license = "MIT"

[workspace.dependencies]
Expand All @@ -20,3 +20,4 @@ thiserror = "1.0.61"
dissector_configuration = { path = "lib/dissector_configuration" }
dissector_parser = { path = "lib/dissector_parser" }
dissector_generator = { path = "lib/dissector_generator" }

15 changes: 9 additions & 6 deletions app/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ impl Runtime {
fn check_impl<T: DissectorParsing>(input_path: &str) -> anyhow::Result<()> {
let mut rdr = Self::load_input_file(input_path)?;

match T::check(&mut rdr)? {
true => println!("Check succeeded (file: {input_path}"),
false => println!("Check failed (file: {input_path}"),
};

Ok(())
T::check(&mut rdr)
.map(|res| {
if res.status {
println!("OK")
} else {
println!("{}", res.message.unwrap())
}
})
.or_else(|e| bail!(e))
}

fn generate_impl<T: DissectorParsing>(
Expand Down
3 changes: 3 additions & 0 deletions lib/dissector_configuration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ license.workspace = true

[dependencies]
strum = { version = "0.26.3", features = ["derive"] }

[lib]
doctest = false
20 changes: 19 additions & 1 deletion lib/dissector_configuration/src/base.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use strum::{Display, EnumIter, IntoEnumIterator};

#[derive(Debug, EnumIter, Display)]
#[derive(Debug, EnumIter, Display, Eq, PartialEq)]
pub enum Base {
None,
Dec,
Expand Down Expand Up @@ -47,3 +47,21 @@ impl Base {
Self::iter().map(|x| format!("{}", x)).collect()
}
}

#[cfg(test)]
mod tests {
use crate::base::Base;

#[test]
fn test_get_values_as_string_not_empty() {
let list = Base::get_values_as_string();
assert!(list.len() > 0)
}

#[test]
#[should_panic]
fn test_base_from_invalid_string() {
let str = Some("InvalidString".to_string());
let _ = Base::from(str);
}
}
37 changes: 37 additions & 0 deletions lib/dissector_configuration/src/dissector_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,40 @@ impl DissectorConfiguration {
}
}
}

#[cfg(test)]
mod tests {
use crate::base::Base;
use crate::dissector_configuration::DissectorConfiguration;
use crate::dissector_data::Data;
use crate::endianness::Endianness;
use crate::format::Format;
use crate::network_information::NetworkInformation;
use crate::protocol::Protocol;

#[test]
fn test_configuration_new() {
let conf = DissectorConfiguration::new(
"test_conf",
Endianness::BigEndian,
NetworkInformation::new(Protocol::Tcp, vec![42, 1337]),
vec![Data::new(
"test_data".to_string(),
Format::Char,
Base::DecHex,
)],
);

assert_eq!(conf.name, "test_conf");
assert_eq!(conf.endianness, Endianness::BigEndian);
assert_eq!(
conf.network_information,
NetworkInformation::new(Protocol::Tcp, vec![42, 1337])
);
assert_eq!(conf.payload.len(), 1);
assert_eq!(
conf.payload[0],
Data::new("test_data".to_string(), Format::Char, Base::DecHex,)
)
}
}
29 changes: 24 additions & 5 deletions lib/dissector_configuration/src/dissector_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,38 @@ use crate::base::Base;
use crate::format::Format;

/// Data chunk description inside the payload
#[derive(Debug)]
#[allow(dead_code)]
#[derive(Debug, Eq, PartialEq)]
pub struct Data {
/// Name
name: String,
pub name: String,
/// Format
format: Format,
pub format: Format,
/// Display base
base: Base,
pub base: Base,
}

impl Data {
pub fn new(name: String, format: Format, base: Base) -> Self {
Self { name, format, base }
}
}

#[cfg(test)]
mod tests {
use crate::base::Base;
use crate::dissector_data::Data;
use crate::format::Format;

#[test]
fn test_data_new() {
let name = "test_data".to_string();
let format = Format::Char;
let base = Base::DecHex;

let data = Data::new(name, format, base);

assert_eq!(data.name, "test_data");
assert_eq!(data.format, Format::Char);
assert_eq!(data.base, Base::DecHex);
}
}
20 changes: 19 additions & 1 deletion lib/dissector_configuration/src/endianness.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use strum::{Display, EnumIter, IntoEnumIterator};

#[derive(Debug, EnumIter, Display)]
#[derive(Debug, EnumIter, Display, Eq, PartialEq)]
pub enum Endianness {
LittleEndian,
BigEndian,
Expand All @@ -21,3 +21,21 @@ impl Endianness {
Self::iter().map(|x| format!("{}", x)).collect()
}
}

#[cfg(test)]
mod tests {
use crate::endianness::Endianness;

#[test]
fn test_get_values_as_string_not_empty() {
let list = Endianness::get_values_as_string();
assert!(list.len() > 0)
}

#[test]
#[should_panic]
fn test_base_from_invalid_string() {
let str = "InvalidString".to_string();
let _ = Endianness::from(str);
}
}
20 changes: 19 additions & 1 deletion lib/dissector_configuration/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use strum::{Display, EnumIter, IntoEnumIterator};

#[derive(Debug, EnumIter, Display)]
#[derive(Debug, EnumIter, Display, Eq, PartialEq)]
pub enum Format {
None,
Bool,
Expand Down Expand Up @@ -62,3 +62,21 @@ impl Format {
Self::iter().map(|x| format!("{}", x)).collect()
}
}

#[cfg(test)]
mod tests {
use crate::format::Format;

#[test]
fn test_get_values_as_string_not_empty() {
let list = Format::get_values_as_string();
assert!(list.len() > 0)
}

#[test]
#[should_panic]
fn test_base_from_invalid_string() {
let str = "InvalidString".to_string();
let _ = Format::from(str);
}
}
19 changes: 18 additions & 1 deletion lib/dissector_configuration/src/network_information.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::protocol::Protocol;

/// Network information of the dissector
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub struct NetworkInformation {
/// Communication protocol
pub protocol: Protocol,
Expand All @@ -14,3 +14,20 @@ impl NetworkInformation {
Self { protocol, ports }
}
}

#[cfg(test)]
mod tests {
use crate::network_information::NetworkInformation;
use crate::protocol::Protocol;

#[test]
fn test_network_information_new() {
let protocol = Protocol::Udp;
let ports = vec![42, 1337];

let net_info = NetworkInformation::new(protocol, ports);

assert_eq!(net_info.protocol, Protocol::Udp);
assert_eq!(net_info.ports, vec![42, 1337]);
}
}
20 changes: 19 additions & 1 deletion lib/dissector_configuration/src/protocol.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use strum::{Display, EnumIter, IntoEnumIterator};

#[derive(Debug, EnumIter, Display)]
#[derive(Debug, EnumIter, Display, Eq, PartialEq)]
pub enum Protocol {
Tcp = 0,
Udp = 1,
Expand All @@ -21,3 +21,21 @@ impl Protocol {
Self::iter().map(|x| format!("{}", x)).collect()
}
}

#[cfg(test)]
mod tests {
use crate::protocol::Protocol;

#[test]
fn test_get_values_as_string_not_empty() {
let list = Protocol::get_values_as_string();
assert!(list.len() > 0)
}

#[test]
#[should_panic]
fn test_base_from_invalid_string() {
let str = "InvalidString".to_string();
let _ = Protocol::from(str);
}
}
5 changes: 4 additions & 1 deletion lib/dissector_generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ license.workspace = true
dissector_configuration.workspace = true
thiserror.workspace = true

regex = "1.10.5"
regex = "1.10.5"

[lib]
doctest = false
3 changes: 3 additions & 0 deletions lib/dissector_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ jsonschema = { version = "0.18.0", default-features = false }
serde_json = "1.0.120"
thiserror.workspace = true
serde = "1.0.203"

[lib]
doctest = false
10 changes: 5 additions & 5 deletions lib/dissector_parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ pub(crate) type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Error)]
pub enum Error {
#[error("Dissector description is not valid.")]
InvalidDissector,

#[error("Failed to deserialize JSON schema ({0})")]
#[error("Failed to deserialize JSON schema: {0}")]
SchemaDeserialization(serde_json::Error),

#[error("Failed to deserialize JSON dissector ({0})")]
#[error("Failed to compile JSON schema ({0})")]
SchemaCompilation(String),

#[error("Failed to deserialize JSON dissector: {0}")]
DissectorDeserialization(serde_json::Error),
}
Loading

0 comments on commit 0a99861

Please sign in to comment.