forked from openmls/openmls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test vector generation and testing - TreeMath and Encryption (openmls…
…#299) This implements test vector generation and testing for treemath and encryption. It follows the basic idea of https://github.com/mlswg/mls-implementations/blob/87e47925116b95c01db0e7b2a03b71b5a44b8a30/test-vectors.md but slightly changes the test vector definition and uses JSON serialised representations. This version should be used to discuss the exact test vectors we want for MLS and will probably change later on. Also see mlswg/mls-implementations#32 for a discussion. Note that generating and testing for the encryption is currently pretty cumbersome and ugly due to OpenMLS' code structure. This should change in future when the code is refactored to respect logic boundaries between the different parts of MLS.
- Loading branch information
1 parent
583e19b
commit c12cf07
Showing
17 changed files
with
60,626 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//! Test utilities | ||
//! | ||
#![allow(dead_code)] | ||
|
||
use serde::{self, de::DeserializeOwned, Serialize}; | ||
use std::{ | ||
fs::File, | ||
io::{BufReader, Write}, | ||
}; | ||
|
||
pub fn write(file_name: &str, obj: impl Serialize) { | ||
let mut file = match File::create(file_name) { | ||
Ok(f) => f, | ||
Err(_) => panic!("Couldn't open file {}.", file_name), | ||
}; | ||
file.write_all( | ||
serde_json::to_string_pretty(&obj) | ||
.expect("Error serializing test vectors") | ||
.as_bytes(), | ||
) | ||
.expect("Error writing test vector file"); | ||
} | ||
|
||
pub fn read<T: DeserializeOwned>(file_name: &str) -> T { | ||
let file = match File::open(file_name) { | ||
Ok(f) => f, | ||
Err(_) => panic!("Couldn't open file {}.", file_name), | ||
}; | ||
let reader = BufReader::new(file); | ||
match serde_json::from_reader(reader) { | ||
Ok(r) => return r, | ||
Err(e) => panic!("Error reading file.\n{:?}", e), | ||
}; | ||
} | ||
|
||
/// Convert `bytes` to a hex string. | ||
pub fn bytes_to_hex(bytes: &[u8]) -> String { | ||
let mut hex = String::new(); | ||
for &b in bytes { | ||
hex += &format!("{:02X}", b); | ||
} | ||
hex | ||
} | ||
|
||
/// Convert a hex string to a byte vector. | ||
pub fn hex_to_bytes(hex: &str) -> Vec<u8> { | ||
assert!(hex.len() % 2 == 0); | ||
let mut bytes = Vec::new(); | ||
for i in 0..(hex.len() / 2) { | ||
bytes.push(u8::from_str_radix(&hex[2 * i..2 * i + 2], 16).unwrap()); | ||
} | ||
bytes | ||
} | ||
|
||
/// Convert a hex string to a byte vector. | ||
/// If the input is `None`, this returns an empty vector. | ||
pub fn hex_to_bytes_option(hex: Option<String>) -> Vec<u8> { | ||
match hex { | ||
Some(s) => hex_to_bytes(&s), | ||
None => vec![], | ||
} | ||
} |
Oops, something went wrong.