Skip to content

Commit

Permalink
Test with new test vectors (#25)
Browse files Browse the repository at this point in the history
* Test: Test with new test vectors

* Update test vectors

* Update to test vector v0.2.0

* Tests: Update parsing of test vectors

* Update test vectors to 0.3.0

* Test key are hex in test vectors 0.3.0
  • Loading branch information
brycx authored Nov 26, 2021
1 parent 019725d commit 88729c0
Show file tree
Hide file tree
Showing 4 changed files with 382 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ Cargo.lock
/target
**/*.rs.bk
Cargo.lock

.DS_Store
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ orion = "0.16.0"
serde = "^1.0"
serde_derive = "1.0.115"
serde_json = "1.0"
hex = "0.4.2"
getrandom = "0.2.3"
176 changes: 111 additions & 65 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,77 +501,123 @@ mod unit_tests {

use super::*;

#[derive(Serialize, Deserialize, Debug)]
struct JSONTest {
a: String,
b: bool,
}

// Test vectors from: https://github.com/tuupola/branca-js/blob/master/test.js
#[test]
pub fn test_decode_1() {
let ciphertext =
"870S4BYjk7NvyViEjUNsTEmGXbARAX9PamXZg0b3JyeIdGyZkFJhNsOQW6m0K9KnXt3ZUBqDB6hF4";
let key = b"supersecretkeyyoushouldnotcommit";
let ttl = 0;

assert_eq!(decode(ciphertext, key, ttl).unwrap(), b"Hello world!");
}

#[test]
pub fn test_decode_2() {
let ciphertext =
"89i7YCwtsSiYfXvOKlgkCyElnGCOEYG7zLCjUp4MuDIZGbkKJgt79Sts9RdW2Yo4imonXsILmqtNb";
let key = b"supersecretkeyyoushouldnotcommit";
let ttl = 0;

assert_eq!(decode(ciphertext, key, ttl).unwrap(), b"Hello world!");
}

#[test]
pub fn test_decode_3() {
let ciphertext =
"875GH234UdXU6PkYq8g7tIM80XapDQOH72bU48YJ7SK1iHiLkrqT8Mly7P59TebOxCyQeqpMJ0a7a";
let key = b"supersecretkeyyoushouldnotcommit";
let ttl = 0;

assert_eq!(decode(ciphertext, key, ttl).unwrap(), b"Hello world!");
}
mod json_test_vectors {
use super::*;
use std::fs::File;
use std::io::BufReader;

#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Debug)]
struct TestFile {
version: String,
numberOfTests: u32,
testGroups: Vec<TestGroup>,
}

#[test]
pub fn test_decode_4() {
let ciphertext = "1jIBheHWEwYIP59Wpm4QkjkIKuhc12NcYdp9Y60B6av7sZc3vJ5wBwmKJyQzGfJCrvuBgGnf";
let key = b"supersecretkeyyoushouldnotcommit";
let ttl = 0;
#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Debug)]
struct TestGroup {
testType: String,
tests: Vec<TestVector>,
}

assert_eq!(
decode(ciphertext, key, ttl).unwrap(),
b"\x00\x00\x00\x00\x00\x00\x00\x00"
);
}
#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Debug)]
struct TestVector {
id: u32,
comment: String,
key: String,
nonce: Option<String>,
timestamp: u32,
token: String,
msg: String,
isValid: bool,
}

#[test]
pub fn test_decode_5() {
let ciphertext = "1jrx6DUq9HmXvYdmhWMhXzx3klRzhlAjsc3tUFxDPCvZZLm16GYOzsBG4KwF1djjW1yTeZ2B";
let key = b"supersecretkeyyoushouldnotcommit";
let ttl = 0;
fn parse_hex(data: &str) -> Vec<u8> {
match data {
"" => vec![0u8; 0],
"80" => b"\x80".to_vec(),
_ => hex::decode(data).unwrap(),
}
}

assert_eq!(
decode(ciphertext, key, ttl).unwrap(),
b"\x00\x00\x00\x00\x00\x00\x00\x00"
);
#[test]
pub fn run_test_vectors() {
let file = File::open("test_data/test_vectors.json").unwrap();
let reader = BufReader::new(file);
let tests: TestFile = serde_json::from_reader(reader).unwrap();

let mut tests_run = 0;
for test_group in tests.testGroups.iter() {
for test in test_group.tests.iter() {
if test_group.testType == "encoding" {
debug_assert!(test.nonce.is_some());

if test.isValid {
let nonce = Nonce::from_slice(&parse_hex(test.nonce.as_ref().unwrap()))
.unwrap();

let res = encode_with_nonce(
&parse_hex(&test.msg),
&parse_hex(&test.key),
&nonce,
test.timestamp,
)
.unwrap();

assert_eq!(res, test.token);
assert_eq!(
decode(&test.token, &parse_hex(&test.key), 0).unwrap(),
parse_hex(&test.msg)
);

tests_run += 1;
}

if !test.isValid {
let nonce = Nonce::from_slice(&parse_hex(test.nonce.as_ref().unwrap()));

if nonce.is_err() {
tests_run += 1;
continue;
}

let res = encode_with_nonce(
&parse_hex(&test.msg),
&parse_hex(&test.key),
&nonce.unwrap(),
test.timestamp,
);

assert!(res.is_err());
tests_run += 1;
}
}

if test_group.testType == "decoding" {
debug_assert!(test.nonce.is_none());

let res = decode(
&test.token,
&parse_hex(&test.key),
0, // Not a part of test vectors
);

assert_eq!(test.isValid, res.is_ok());
tests_run += 1;
}
}
}

assert_eq!(tests_run, tests.numberOfTests);
}
}

#[test]
pub fn test_decode_6() {
let ciphertext = "1jJDJOEfuc4uBJh5ivaadjo6UaBZJDZ1NsWixVCz2mXw3824JRDQZIgflRqCNKz6yC7a0JKC";
let key = b"supersecretkeyyoushouldnotcommit";
let ttl = 0;

assert_eq!(
decode(ciphertext, key, ttl).unwrap(),
b"\x00\x00\x00\x00\x00\x00\x00\x00"
);
#[derive(Serialize, Deserialize, Debug)]
struct JSONTest {
a: String,
b: bool,
}

#[test]
Expand Down
Loading

0 comments on commit 88729c0

Please sign in to comment.