Skip to content

Commit

Permalink
Merge pull request #5 from cybermerqury/bugfix/optional_flags_in_queue
Browse files Browse the repository at this point in the history
Update queue flags to be optional
  • Loading branch information
jwhb authored Jul 10, 2023
2 parents 720d2c7 + 4f42782 commit 8c6fc03
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,9 @@ pub struct Queue {
/// Queue number.
pub num: Expression,

#[serde(skip_serializing_if = "Option::is_none")]
/// Queue flags.
pub flags: HashSet<QueueFlag>,
pub flags: Option<HashSet<QueueFlag>>,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Hash)]
Expand Down
76 changes: 75 additions & 1 deletion tests/json_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use nftables::expr::{Expression, Meta, MetaKey, NamedExpression};
use nftables::stmt::{Match, Operator, Statement};
use nftables::stmt::{Match, Operator, Queue, Statement};
use nftables::{schema::*, types::*};
use serde_json::json;
use std::fs::{self, File};
Expand Down Expand Up @@ -91,3 +91,77 @@ fn test_insert() {
let parsed: Nftables = serde_json::from_value(json).unwrap();
assert_eq!(expected, parsed);
}

#[test]
fn test_parsing_of_queue_without_flags() {
let expected = Nftables {
objects: vec![NfObject::ListObject(NfListObject::Rule(Rule {
family: NfFamily::IP,
table: "test_table".to_string(),
chain: "test_chain".to_string(),
expr: vec![
Statement::Match(Match {
left: Expression::Named(NamedExpression::Payload(nftables::expr::Payload {
protocol: "udp".to_string(),
field: "dport".to_string(),
})),
right: Expression::Number(20000),
op: Operator::EQ,
}),
Statement::Queue(Queue {
num: Expression::Number(0),
flags: None,
}),
],
handle: Some(2),
index: None,
comment: None,
}))],
};

let json = json!({
"nftables": [
{
"rule": {
"family": "ip",
"table": "test_table",
"chain": "test_chain",
"handle": 2,
"expr": [
{
"match": {
"op": "==",
"left": {
"payload": {
"protocol": "udp",
"field": "dport"
}
},
"right": 20000
}
},
{
"queue": {
"num": 0
}
}
]
}
}
]
});

let parsed: Nftables = serde_json::from_value(json).unwrap();
assert_eq!(expected, parsed);
}

#[test]
fn test_queue_json_serialisation() {
let queue = Statement::Queue(Queue {
num: Expression::Number(0),
flags: None,
});

let expected_json = String::from(r#"{"queue":{"num":0}}"#);
assert_eq!(expected_json, serde_json::to_string(&queue).unwrap());
}

0 comments on commit 8c6fc03

Please sign in to comment.