Skip to content

Commit

Permalink
Add unit test for save_id_map() (#76)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #76

# What
* Add unit tests for save_id_map funcion on partner side.
* save_id_map function is called after the create_id_map().
* Add create_key function to create fixed keys for testing.
* create_id_map function use partner.private_keys.1 to encrypt.
* self_permutation also needs to be fixed when we test create_id_map().
* Create a temp file and pass the path to save_id_map() and check  the string in the file is correct or not.

# Why
* need to improve code coverage

Differential Revision: D39142927

fbshipit-source-id: ad0028ff05ad42024d4c165bae1d6c5cea49728e
  • Loading branch information
Jian Cao authored and facebook-github-bot committed Sep 4, 2022
1 parent 64777bf commit 84724a1
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions protocol/src/private_id_multi_key/partner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,123 @@ mod tests {
expected.sort();
assert_eq!(res, expected);
}

#[test]
fn check_save_id_map() {
use std::io::Read;
let f = create_data_file().unwrap();

let mut partner = PartnerPrivateIdMultiKey::new();
let p = f.path().to_str().unwrap();
partner.load_data(p, false).unwrap();
partner.permute_hash_to_bytes().unwrap();
partner.self_permutation = Arc::new(RwLock::new(vec![2, 0, 1]));

let v_partner = vec![
ByteBuffer {
buffer: vec![
184, 37, 136, 74, 91, 89, 249, 229, 149, 35, 102, 42, 232, 146, 17, 246, 76,
220, 123, 255, 26, 158, 35, 211, 76, 0, 12, 77, 138, 141, 88, 55,
],
},
ByteBuffer {
buffer: vec![
100, 97, 144, 135, 147, 68, 216, 225, 242, 22, 79, 71, 68, 234, 128, 43, 10,
77, 232, 44, 231, 186, 118, 248, 170, 72, 69, 235, 244, 14, 89, 39,
],
},
ByteBuffer {
buffer: vec![
80, 11, 202, 56, 183, 53, 94, 71, 140, 170, 181, 22, 207, 222, 150, 81, 80,
180, 174, 79, 191, 137, 150, 58, 5, 76, 147, 129, 97, 101, 254, 78,
],
},
];

let s_prime_company = vec![ByteBuffer {
buffer: vec![
150, 101, 24, 58, 27, 195, 133, 170, 204, 58, 112, 209, 217, 143, 84, 106, 228,
249, 130, 71, 190, 173, 65, 47, 162, 8, 216, 116, 205, 239, 8, 17,
],
}];
partner.private_keys.1 = create_key();
partner.create_id_map(v_partner, s_prime_company);

// Create a file inside of `std::env::temp_dir()`.
let mut file1 = NamedTempFile::new().unwrap();
let p = file1.path().to_str().unwrap();
partner.save_id_map(p).unwrap();
let mut actual_result = String::new();
file1.read_to_string(&mut actual_result).unwrap();
let expected_result = "08FCF66A09440EFCB475BBCFA5915648A9A7DD0F2D0B75E965EDBAEC249D7D,NA\n30A397CD5C79AB7D6FBD59BF191326BAC43983497C81E1E2F109B3252EACE5F,email1,phone1\n7E105B924F454CF6E0BB4DC7158003A5647DC64A08FDC58BFCC03BDFF85718,email3\nD69F32E652AED8427DAACF74D57B807714160D7454310BF3515DD5AA5F98F4F,phone2\n";

assert_eq!(actual_result, expected_result);
}

#[test]
fn check_unshuffle_encrypt() {
let f = create_data_file().unwrap();

let mut partner = PartnerPrivateIdMultiKey::new();
let p = f.path().to_str().unwrap();
partner.load_data(p, false).unwrap();
let company = vec![
ByteBuffer {
buffer: vec![
36, 244, 207, 223, 128, 173, 31, 181, 186, 89, 61, 91, 219, 83, 150, 163, 56,
181, 116, 224, 145, 141, 18, 242, 129, 233, 88, 17, 110, 49, 49, 113,
],
},
ByteBuffer {
buffer: vec![
206, 39, 208, 50, 231, 84, 72, 52, 135, 144, 245, 104, 135, 123, 216, 160, 250,
156, 243, 96, 68, 64, 103, 112, 164, 31, 215, 241, 135, 231, 229, 51,
],
},
ByteBuffer {
buffer: vec![
144, 162, 198, 54, 153, 9, 60, 84, 104, 61, 151, 133, 153, 255, 136, 35, 177,
232, 237, 86, 120, 242, 246, 122, 108, 43, 120, 114, 122, 164, 232, 51,
],
},
ByteBuffer {
buffer: vec![
120, 158, 173, 88, 177, 146, 46, 30, 111, 2, 68, 17, 136, 215, 136, 104, 90,
99, 11, 75, 109, 8, 51, 118, 132, 25, 188, 220, 104, 111, 176, 2,
],
},
];

partner.company_permutation = Arc::new(RwLock::new(vec![2, 0, 1]));
partner.private_keys.1 = create_key();

let actual_result = partner.unshuffle_encrypt(company).unwrap();
let expected_result = vec![
ByteBuffer {
buffer: vec![
202, 241, 242, 47, 249, 131, 246, 166, 85, 138, 218, 82, 84, 153, 30, 46, 133,
64, 162, 113, 123, 163, 114, 137, 37, 175, 188, 211, 29, 31, 140, 1,
],
},
ByteBuffer {
buffer: vec![
94, 224, 94, 242, 165, 118, 221, 50, 246, 154, 226, 214, 153, 9, 99, 197, 204,
62, 30, 150, 243, 209, 123, 42, 94, 157, 3, 206, 7, 225, 230, 123,
],
},
ByteBuffer {
buffer: vec![
176, 194, 85, 162, 206, 222, 179, 118, 53, 252, 105, 204, 185, 60, 160, 145,
164, 6, 89, 69, 170, 120, 63, 6, 15, 198, 94, 114, 76, 140, 65, 8,
],
},
ByteBuffer {
buffer: vec![
54, 143, 63, 196, 218, 246, 85, 75, 171, 239, 105, 248, 133, 85, 150, 20, 1,
236, 251, 228, 106, 88, 96, 240, 68, 236, 174, 68, 237, 25, 49, 43,
],
},
];
assert_eq!(actual_result, expected_result);
}
}

0 comments on commit 84724a1

Please sign in to comment.