Skip to content

Commit

Permalink
Add snapshot testing for statement parsing
Browse files Browse the repository at this point in the history
For statements it is hard to write any unit tests, as a statement is a rather complex struct also containing some meta information. It is much easier to test the statement parsing, looking at the generated statement and review it instead.
  • Loading branch information
Kraemii committed Nov 21, 2024
1 parent 19c4ec0 commit b4e4683
Show file tree
Hide file tree
Showing 32 changed files with 1,380 additions and 17 deletions.
62 changes: 62 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 28 additions & 13 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ license = "GPL-2.0-or-later"

[dependencies]
aes = "0.8.2"
aes-gcm = { version = "0.10.1"}
aes-gcm = { version = "0.10.1" }
anyhow = "1.0.75"
async-trait = "0.1.68"
base64 = "0.21.2"
cbc = { version = "0.1.2", features = ["alloc"]}
cbc = { version = "0.1.2", features = ["alloc"] }
ccm = "0.5.0"
chacha20 = "0.9.1"
chrono = { version = "0.4.23", default-features = false, features = ["clock"]}
chrono = { version = "0.4.23", default-features = false, features = ["clock"] }
clap = { version = "4.3.0", features = ["derive", "env"] }
cmac = "0.7.2"
configparser = "3"
Expand Down Expand Up @@ -57,8 +57,10 @@ rustls = "0.23.5"
rustls-pemfile = "2.1.2"
rustls-pemfile-old = { version = "1.0.2", package = "rustls-pemfile" }
sequoia-ipc = "0.30.1"
sequoia-openpgp = { version ="1.16.1", default-features = false, features = ["crypto-openssl"] }
serde = { version = "1.0", features = ["derive"]}
sequoia-openpgp = { version = "1.16.1", default-features = false, features = [
"crypto-openssl",
] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.96"
sha1 = "0.10.5"
sha2 = "0.10.7"
Expand All @@ -78,11 +80,14 @@ rayon = { version = "1.8.0", optional = true }
pcap = { version = "1.0.0", optional = true }
pnet_base = { version = "0.33.0", optional = true }
pnet = { version = "0.33.0", optional = true }
socket2 = {version = "0.5.2", features = ["all"], optional = true}
socket2 = { version = "0.5.2", features = ["all"], optional = true }
pnet_macros = { version = "0.33.0", optional = true }
pnet_macros_support = { version = "0.33.0", optional = true }

libssh-rs = {version = "~0.2", features = ["vendored-openssl", "vendored"], optional = true}
libssh-rs = { version = "~0.2", features = [
"vendored-openssl",
"vendored",
], optional = true }

nasl-function-proc-macro = { path = "crates/nasl-function-proc-macro" }
nasl-c-lib = { path = "crates/nasl-c-lib", optional = true }
Expand All @@ -92,22 +97,32 @@ rc4 = "0.1.0"

[workspace]
resolver = "2"
members = [
"crates/smoketest",
"crates/nasl-function-proc-macro",
]
members = ["crates/smoketest", "crates/nasl-function-proc-macro"]

[dev-dependencies]
tracing-test = "0.2.5"
criterion = "0"
insta = { version = "1.41.1", features = ["ron"] }

[features]
dep-graph-parallel = ["rayon", "crossbeam-channel"]
openvas_serde_support = []
serde_support = []
default = ["dep-graph-parallel", "openvas_serde_support", "enforce-no-trailing-arguments", "serde_support"]
default = [
"dep-graph-parallel",
"openvas_serde_support",
"enforce-no-trailing-arguments",
"serde_support",
]

nasl-builtin-raw-ip = ["pcap", "pnet_base", "pnet", "socket2", "pnet_macros", "pnet_macros_support",]
nasl-builtin-raw-ip = [
"pcap",
"pnet_base",
"pnet",
"socket2",
"pnet_macros",
"pnet_macros_support",
]
nasl-builtin-ssh = ["libssh-rs"]
experimental = ["nasl-builtin-raw-ip", "nasl-builtin-ssh", "nasl-c-lib"]

Expand Down
45 changes: 45 additions & 0 deletions rust/src/nasl/syntax/snapshots/statement_add.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
source: src/nasl/syntax/statement.rs
expression: stmt
snapshot_kind: text
---
Statement(
start: Token(
category: Identifier(Undefined("a")),
line_column: (1, 1),
position: (0, 1),
),
end: Some(Token(
category: Semicolon,
line_column: (1, 6),
position: (5, 6),
)),
kind: Operator(Plus, [
Statement(
start: Token(
category: Identifier(Undefined("a")),
line_column: (1, 1),
position: (0, 1),
),
end: Some(Token(
category: Identifier(Undefined("a")),
line_column: (1, 1),
position: (0, 1),
)),
kind: Variable,
),
Statement(
start: Token(
category: Number(1),
line_column: (1, 5),
position: (4, 5),
),
end: Some(Token(
category: Semicolon,
line_column: (1, 6),
position: (5, 6),
)),
kind: Primitive,
),
]),
)
30 changes: 30 additions & 0 deletions rust/src/nasl/syntax/snapshots/statement_array.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: src/nasl/syntax/statement.rs
expression: stmt
snapshot_kind: text
---
Statement(
start: Token(
category: Identifier(Undefined("a")),
line_column: (1, 1),
position: (0, 1),
),
end: Some(Token(
category: Semicolon,
line_column: (1, 5),
position: (4, 5),
)),
kind: Array(Some(Statement(
start: Token(
category: Number(1),
line_column: (1, 3),
position: (2, 3),
),
end: Some(Token(
category: RightBrace,
line_column: (1, 4),
position: (3, 4),
)),
kind: Primitive,
))),
)
42 changes: 42 additions & 0 deletions rust/src/nasl/syntax/snapshots/statement_assign.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
source: src/nasl/syntax/statement.rs
expression: stmt
snapshot_kind: text
---
Statement(
start: Token(
category: Identifier(Undefined("a")),
line_column: (1, 1),
position: (0, 1),
),
end: Some(Token(
category: Semicolon,
line_column: (1, 6),
position: (5, 6),
)),
kind: Assign(Equal, AssignReturn, Statement(
start: Token(
category: Identifier(Undefined("a")),
line_column: (1, 1),
position: (0, 1),
),
end: Some(Token(
category: Identifier(Undefined("a")),
line_column: (1, 1),
position: (0, 1),
)),
kind: Variable,
), Statement(
start: Token(
category: Number(1),
line_column: (1, 5),
position: (4, 5),
),
end: Some(Token(
category: Semicolon,
line_column: (1, 6),
position: (5, 6),
)),
kind: Primitive,
)),
)
38 changes: 38 additions & 0 deletions rust/src/nasl/syntax/snapshots/statement_assign_return.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
source: src/nasl/syntax/statement.rs
expression: stmt
snapshot_kind: text
---
Statement(
start: Token(
category: MinusMinus,
line_column: (1, 1),
position: (0, 2),
),
end: Some(Token(
category: Semicolon,
line_column: (1, 4),
position: (3, 4),
)),
kind: Assign(MinusMinus, AssignReturn, Statement(
start: Token(
category: Identifier(Undefined("a")),
line_column: (1, 3),
position: (2, 3),
),
end: Some(Token(
category: Identifier(Undefined("a")),
line_column: (1, 3),
position: (2, 3),
)),
kind: Variable,
), Statement(
start: Token(
category: UnknownSymbol,
line_column: (0, 0),
position: (0, 0),
),
end: None,
kind: NoOp,
)),
)
32 changes: 32 additions & 0 deletions rust/src/nasl/syntax/snapshots/statement_block.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: src/nasl/syntax/statement.rs
expression: stmt
snapshot_kind: text
---
Statement(
start: Token(
category: LeftCurlyBracket,
line_column: (1, 1),
position: (0, 1),
),
end: Some(Token(
category: RightCurlyBracket,
line_column: (1, 6),
position: (5, 6),
)),
kind: Block([
Statement(
start: Token(
category: Identifier(Undefined("a")),
line_column: (1, 3),
position: (2, 3),
),
end: Some(Token(
category: Semicolon,
line_column: (1, 4),
position: (3, 4),
)),
kind: Variable,
),
]),
)
Loading

0 comments on commit b4e4683

Please sign in to comment.