Skip to content

Commit

Permalink
Add test for flags serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey authored and nickray committed Sep 13, 2023
1 parent 1e49931 commit fbea1cb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ serde-indexed = "0.1.0"
serial_test = { version = "2" }
entropy = "0.4.0"
once_cell = "1.13.0"
serde_test = "1"
# If this is not enabled, serde_test makes serde_cbor's compilation fail
serde_cbor = { feature = "0.11.2", features = ["std"] }
# Somehow, this is causing a regression.
# rand_core = { version = "0.5", features = ["getrandom"] }

Expand Down
73 changes: 73 additions & 0 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,76 @@ impl Kind {
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde_test::{assert_tokens, Token};

#[test]
fn keyflags_format() {
assert_tokens(
&Flags { bits: 0 },
&[
Token::Map { len: Some(1) },
Token::U64(0),
Token::U16(0),
Token::MapEnd,
],
);
assert_tokens(
&Flags::LOCAL,
&[
Token::Map { len: Some(1) },
Token::U64(0),
Token::U16(0b1),
Token::MapEnd,
],
);
assert_tokens(
&(Flags::LOCAL | Flags::SENSITIVE),
&[
Token::Map { len: Some(1) },
Token::U64(0),
Token::U16(0b11),
Token::MapEnd,
],
);
assert_tokens(
&(Flags::LOCAL | Flags::SENSITIVE | Flags::SERIALIZABLE),
&[
Token::Map { len: Some(1) },
Token::U64(0),
Token::U16(0b10011),
Token::MapEnd,
],
);
assert_tokens(
&Flags::SENSITIVE,
&[
Token::Map { len: Some(1) },
Token::U64(0),
Token::U16(0b10),
Token::MapEnd,
],
);
assert_tokens(
&(Flags::SENSITIVE | Flags::SERIALIZABLE),
&[
Token::Map { len: Some(1) },
Token::U64(0),
Token::U16(0b10010),
Token::MapEnd,
],
);
assert_tokens(
&Flags::SERIALIZABLE,
&[
Token::Map { len: Some(1) },
Token::U64(0),
Token::U16(0b10000),
Token::MapEnd,
],
);
}
}

0 comments on commit fbea1cb

Please sign in to comment.