feat: add tests
This commit is contained in:
@@ -156,6 +156,7 @@ fn get_signature(sign_algorithm: &SignAlgorithm,
|
|||||||
string_to_sign.push_str(payload);
|
string_to_sign.push_str(payload);
|
||||||
|
|
||||||
let string_to_sign_digest_hex = sign_algorithm.digest(string_to_sign.as_bytes());
|
let string_to_sign_digest_hex = sign_algorithm.digest(string_to_sign.as_bytes());
|
||||||
|
let mut string_to_sign = String::new();
|
||||||
string_to_sign.push_str(sign_algorithm.as_aliyun_name_v4());
|
string_to_sign.push_str(sign_algorithm.as_aliyun_name_v4());
|
||||||
string_to_sign.push('\n');
|
string_to_sign.push('\n');
|
||||||
string_to_sign.push_str(&string_to_sign_digest_hex);
|
string_to_sign.push_str(&string_to_sign_digest_hex);
|
||||||
@@ -219,3 +220,147 @@ fn get_signed_headers(headers: &BTreeMap<String, String>) -> Vec<(String, &Strin
|
|||||||
}
|
}
|
||||||
signed_headers
|
signed_headers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_nonce() {
|
||||||
|
let nonce = get_nonce();
|
||||||
|
assert!(nonce.starts_with("nonce-"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_signing_key_sha256() {
|
||||||
|
let signing_key = get_signing_key(
|
||||||
|
&SignAlgorithm::Sha256,
|
||||||
|
"AccessKeySecret",
|
||||||
|
"20121231",
|
||||||
|
"cn-hangzhou",
|
||||||
|
"test");
|
||||||
|
assert_eq!(
|
||||||
|
"7a297f88537fed9e2e2ad740cd945860ce62df50c425e4a73f6414c0aa1cd04a",
|
||||||
|
hex::encode(&signing_key)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_signing_key_sm3() {
|
||||||
|
let signing_key = get_signing_key(
|
||||||
|
&SignAlgorithm::Sm3,
|
||||||
|
"AccessKeySecret",
|
||||||
|
"20121231",
|
||||||
|
"cn-hangzhou",
|
||||||
|
"test");
|
||||||
|
assert_eq!(
|
||||||
|
"a54b85a3b1e6a04c939cf807c1d6bc131877527122b2d2dfc5df94b86b8d8a83",
|
||||||
|
hex::encode(&signing_key)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_signed_headers_01() {
|
||||||
|
let mut headers = BTreeMap::new();
|
||||||
|
headers.insert("host".to_string(), "example.com".to_string());
|
||||||
|
let signed_headers = get_signed_headers(&headers);
|
||||||
|
let signed_headers = signed_headers.iter().map(|h| &h.0).collect::<Vec<_>>();
|
||||||
|
|
||||||
|
assert_eq!(1, signed_headers.len());
|
||||||
|
assert_eq!("host", signed_headers[0].as_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_signed_headers_02() {
|
||||||
|
let mut headers = BTreeMap::new();
|
||||||
|
headers.insert("host".to_string(), "example.com".to_string());
|
||||||
|
headers.insert("x-acs-date".to_string(), "19700112T010101Z".to_string());
|
||||||
|
let signed_headers = get_signed_headers(&headers);
|
||||||
|
let signed_headers = signed_headers.iter().map(|h| &h.0).collect::<Vec<_>>();
|
||||||
|
|
||||||
|
assert_eq!(2, signed_headers.len());
|
||||||
|
assert_eq!("host", signed_headers[0].as_str());
|
||||||
|
assert_eq!("x-acs-date", signed_headers[1].as_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_signed_headers_03() {
|
||||||
|
let mut headers = BTreeMap::new();
|
||||||
|
headers.insert("host".to_string(), "example.com".to_string());
|
||||||
|
headers.insert("x-acs-date".to_string(), "19700112T010101Z".to_string());
|
||||||
|
headers.insert("content-type".to_string(), "application/json".to_string());
|
||||||
|
let signed_headers = get_signed_headers(&headers);
|
||||||
|
let signed_headers = signed_headers.iter().map(|h| &h.0).collect::<Vec<_>>();
|
||||||
|
|
||||||
|
assert_eq!(3, signed_headers.len());
|
||||||
|
assert_eq!("content-type", signed_headers[0].as_str());
|
||||||
|
assert_eq!("host", signed_headers[1].as_str());
|
||||||
|
assert_eq!("x-acs-date", signed_headers[2].as_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_build_canonicalized_headers_01() {
|
||||||
|
let mut headers = BTreeMap::new();
|
||||||
|
headers.insert("host".to_string(), "example.com".to_string());
|
||||||
|
headers.insert("x-acs-date".to_string(), "19700112T010101Z".to_string());
|
||||||
|
headers.insert("content-type".to_string(), "application/json".to_string());
|
||||||
|
|
||||||
|
let canonicalized_headers = build_canonicalized_headers(&headers);
|
||||||
|
assert_eq!(r#"content-type:application/json
|
||||||
|
host:example.com
|
||||||
|
x-acs-date:19700112T010101Z
|
||||||
|
"#, canonicalized_headers.as_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_build_canonicalized_headers_02() {
|
||||||
|
let mut headers = BTreeMap::new();
|
||||||
|
headers.insert("host".to_string(), "example.com".to_string());
|
||||||
|
headers.insert("x-acs-date".to_string(), "19700112T010101Z".to_string());
|
||||||
|
headers.insert("content-type".to_string(), "application/json!@#$%^&*()_+[]{};':\"".to_string());
|
||||||
|
|
||||||
|
let canonicalized_headers = build_canonicalized_headers(&headers);
|
||||||
|
assert_eq!(r#"content-type:application/json!@#$%^&*()_+[]{};':"
|
||||||
|
host:example.com
|
||||||
|
x-acs-date:19700112T010101Z
|
||||||
|
"#, canonicalized_headers.as_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_build_canonicalized_resource() {
|
||||||
|
let mut query = BTreeMap::new();
|
||||||
|
query.insert("host".to_string(), "example.com".to_string());
|
||||||
|
query.insert("x-acs-date".to_string(), "19700112T010101Z".to_string());
|
||||||
|
query.insert("content-type".to_string(), "application/json!@#$%^&*()_+[]{};':\"".to_string());
|
||||||
|
|
||||||
|
let canonicalized_query = build_canonicalized_resource(&query);
|
||||||
|
assert_eq!("content-type=application%2Fjson%21%40%23%24%25%5E%26%2A%28%29_%2B\
|
||||||
|
%5B%5D%7B%7D%3B%27%3A%22&host=example.com&x-acs-date=19700112T010101Z", canonicalized_query.as_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_region() {
|
||||||
|
assert_eq!("center", get_region("", ""));
|
||||||
|
assert_eq!("cn-hangzhou", get_region("ecs", "ecs.cn-hangzhou.aliyuncs.com"));
|
||||||
|
assert_eq!("center", get_region("ecs", "ecs.aliyuncs.com"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_signature() {
|
||||||
|
let signing_key = get_signing_key(
|
||||||
|
&SignAlgorithm::Sha256,
|
||||||
|
"AccessKeySecret",
|
||||||
|
"20121231",
|
||||||
|
"cn-hangzhou",
|
||||||
|
"test");
|
||||||
|
|
||||||
|
let query = BTreeMap::new();
|
||||||
|
let mut headers = BTreeMap::new();
|
||||||
|
headers.insert("host".to_string(), "example.com".to_string());
|
||||||
|
let sig = get_signature(
|
||||||
|
&SignAlgorithm::Sha256,
|
||||||
|
&signing_key,
|
||||||
|
"/test",
|
||||||
|
"POST",
|
||||||
|
&query,
|
||||||
|
&headers,
|
||||||
|
"PAYLOAD",
|
||||||
|
);
|
||||||
|
assert_eq!("ee5033e71c4674a94c511ac177f2a2f41835d2c8724f8a98632a2193786ce40b", sig.as_str());
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user