Skip to content

Commit

Permalink
Use raw_seed
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraSuegami committed Dec 19, 2024
1 parent 124304c commit f1d9cdd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
21 changes: 15 additions & 6 deletions src/dns_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ pub async fn get_dkim_public_key(

// #[cfg(debug_assertions)]
// let prefixes = vec!["https://dns.google/resolve"];
// let (seed_raw,): ([u8; 32],) = ic_cdk::call(Principal::management_canister(), "raw_rand", ())
// .await
// .expect("Failed to call the management canister");
// let seed = (seed_raw[0..16].iter().map(|&b| b as u128).sum::<u128>() % 3) as usize;

let seed = ic_cdk::api::time() as usize % prefixes.len();
let (seed_raw,): ([u8; 32],) = ic_cdk::call(Principal::management_canister(), "raw_rand", ())
.await
.expect("Failed to call the management canister");
let seed = (seed_raw[0..16].iter().map(|&b| b as u128).sum::<u128>() % 3) as usize;
// let seed = ic_cdk::api::time() as usize % prefixes.len();
let mut shuffled_prefixes = vec![];
for i in 0..prefixes.len() {
shuffled_prefixes.push(prefixes[(seed + i) % prefixes.len()]);
Expand Down Expand Up @@ -298,6 +297,8 @@ mod test {
"Comment": "Response from 216.239.32.10."
}
"#;
pic.tick();
pic.tick();
mock_http_response(&pic, body);
pic.tick();
mock_http_response(&pic, body);
Expand Down Expand Up @@ -356,6 +357,8 @@ mod test {
"Comment": "Response from 216.239.32.10."
}
"#;
pic.tick();
pic.tick();
mock_http_response(&pic, body);
pic.tick();
let body = r#"
Expand Down Expand Up @@ -441,6 +444,8 @@ mod test {
"Comment": "Response from 216.239.32.10."
}
"#;
pic.tick();
pic.tick();
mock_http_response(&pic, body);
pic.tick();
mock_http_response(&pic, body);
Expand Down Expand Up @@ -501,6 +506,8 @@ mod test {
"Comment": "Response from 216.239.32.10."
}
"#;
pic.tick();
pic.tick();
mock_http_response(&pic, body);
pic.tick();
mock_http_response(&pic, body);
Expand Down Expand Up @@ -562,6 +569,8 @@ mod test {
"Comment": "Response from 216.239.32.10."
}
"#;
pic.tick();
pic.tick();
mock_http_response(&pic, body);
pic.tick();
mock_http_response(&pic, body);
Expand Down
53 changes: 45 additions & 8 deletions src/ic_dns_oracle_backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ mod test {
"Comment": "Response from 216.239.32.10."
}
"#;
pic.tick();
pic.tick();
mock_http_response(&pic, body);
pic.tick();
mock_http_response(&pic, body);
Expand Down Expand Up @@ -862,6 +864,8 @@ mod test {
let body = r#"
{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"20230601._domainkey.zeroknowledge-fm.20230601.gappssmtp.com.","type":16}],"Answer":[],"Comment":"Response from 216.239.38.99."}
"#;
pic.tick();
pic.tick();
mock_http_response(&pic, body);
pic.tick();
mock_http_response(&pic, body);
Expand All @@ -872,6 +876,8 @@ mod test {
let body = r#"
{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"20230601._domainkey.zeroknowledge-fm.20230601.gappssmtp.com.","type":16}],"Answer":[{"name":"20230601._domainkey.zeroknowledge-fm.20230601.gappssmtp.com.","type":16,"TTL":3600,"data":"v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3gWcOhCm99qzN+h7/2+LeP3CLsJkQQ4EP/2mrceXle5pKq8uZmBl1U4d2Vxn4w+pWFANDLmcHolLboESLFqEL5N6ae7u9b236dW4zn9AFkXAGenTzQEeif9VUFtLAZ0Qh2eV7OQgz/vPj5IaNqJ7h9hpM9gO031fe4v+J0DLCE8Rgo7hXbNgJavctc0983DaCDQaznHZ44LZ6TtZv9TBs+QFvsy4+UCTfsuOtHzoEqOOuXsVXZKLP6B882XbEnBpXEF8QzV4J26HiAJFUbO3mAqZL2UeKC0hhzoIZqZXNG0BfuzOF0VLpDa18GYMUiu+LhEJPJO9D8zhzvQIHNrpGwIDAQAB"}],"Comment":"Response from 216.239.38.99."}
"#;
pic.tick();
pic.tick();
mock_http_response(&pic, body);
pic.tick();
mock_http_response(&pic, body);
Expand Down Expand Up @@ -986,6 +992,8 @@ mod test {
"#
.to_string();
body = body.replace("{{BASE64}}", &public_key_der_base64);
pic.tick();
pic.tick();
mock_http_response(&pic, &body);
pic.tick();
mock_http_response(&pic, &body);
Expand Down Expand Up @@ -1077,14 +1085,16 @@ mod test {
"CD": false,
"Question": [
{
"name": "20230601._domainkey.gmail.com.",
"name": "20230601.gmail.com.",
"type": 16
}
],
"Comment": "Response from 216.239.32.10."
}
"#
.to_string();
pic.tick();
pic.tick();
mock_http_response(&pic, &body);
pic.tick();
mock_http_response(&pic, &body);
Expand All @@ -1102,13 +1112,13 @@ mod test {
"CD": false,
"Question": [
{
"name": "20230601._domainkey.gmail.com.",
"name": "20230601._domainkey.gmail-com.20230601.gappssmtp.com.",
"type": 16
}
],
"Answer": [
{
"name": "20230601._domainkey.gmail.com.",
"name": "20230601._domainkey.gmail-com.20230601.gappssmtp.com.",
"type": 16,
"TTL": 3600,
"data": "v=DKIM1; k=rsa; p={{BASE64}}"
Expand All @@ -1119,9 +1129,12 @@ mod test {
"#
.to_string();
body = body.replace("{{BASE64}}", &public_key_der_base64);
pic.tick();
pic.tick();
mock_http_response(&pic, &body);
pic.tick();
mock_http_response(&pic, &body);
pic.tick();

let reply = pic.await_call(call_id).unwrap();
let res = match reply {
Expand Down Expand Up @@ -1199,13 +1212,40 @@ mod test {
"CD": false,
"Question": [
{
"name": "20230601._domainkey.gmail.com.",
"name": "20230601.gmail.com.",
"type": 16
}
],
"Comment": "Response from 216.239.32.10."
}
"#
.to_string();
pic.tick();
pic.tick();
mock_http_response(&pic, &body);
pic.tick();
mock_http_response(&pic, &body);
pic.tick();
mock_http_response(&pic, &body);
pic.tick();

let body = r#"
{
"Status": 0,
"TC": false,
"RD": true,
"RA": true,
"AD": false,
"CD": false,
"Question": [
{
"name": "20230601._domainkey.gmail-com.20230601.gappssmtp.com.",
"type": 16
}
],
"Answer": [
{
"name": "20230601._domainkey.gmail.com.",
"name": "20230601._domainkey.gmail-com.20230601.gappssmtp.com.",
"type": 16,
"TTL": 3600,
"data": "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAntvSKT1hkqhKe0xcaZ0x+QbouDsJuBfby/S82jxsoC/SodmfmVs2D1KAH3mi1AqdMdU12h2VfETeOJkgGYq5ljd996AJ7ud2SyOLQmlhaNHH7Lx+Mdab8/zDN1SdxPARDgcM7AsRECHwQ15R20FaKUABGu4NTbR2fDKnYwiq5jQyBkLWP+LgGOgfUF4T4HZb2PY2bQtEP6QeqOtcW4rrsH24L7XhD+HSZb1hsitrE0VPbhJzxDwI4JF815XMnSVjZgYUXP8CxI1Y0FONlqtQYgsorZ9apoW1KPQe8brSSlRsi9sXB/tu56LmG7tEDNmrZ5XUwQYUUADBOu7t1niwXwIDAQAB"
Expand All @@ -1215,14 +1255,11 @@ mod test {
}
"#
.to_string();
mock_http_response(&pic, &body);
pic.tick();
mock_http_response(&pic, &body);
pic.tick();
mock_http_response(&pic, &body);
pic.tick();
mock_http_response(&pic, &body);
pic.tick();

let reply = pic.await_call(call_id).unwrap();
match reply {
Expand Down

0 comments on commit f1d9cdd

Please sign in to comment.