feat: add tests
This commit is contained in:
@@ -92,13 +92,13 @@ fn get_timestamp() -> (String, String) {
|
||||
("yyyymmdd".into(), "yyyy-mm-dd".into())
|
||||
}
|
||||
|
||||
|
||||
fn get_nonce() -> String {
|
||||
let seq = SEQ.fetch_add(1, Ordering::Relaxed);
|
||||
let now = SystemTime::now();
|
||||
let rand_bytes: [u8; 32] = random();
|
||||
let seed = format!("{}-{:?}-{:?}", seq, now, rand_bytes);
|
||||
"nonce-".to_string() + &SignAlgorithm::Sha256.digest(seed.as_bytes())
|
||||
let secs_hex = hex::encode(now.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs().to_be_bytes());
|
||||
"nonce-".to_string() + &secs_hex + "-" + &SignAlgorithm::Sha256.digest(seed.as_bytes())[0..32]
|
||||
}
|
||||
|
||||
fn get_authorization(sign_algorithm: &SignAlgorithm,
|
||||
@@ -363,4 +363,100 @@ fn test_get_signature() {
|
||||
"PAYLOAD",
|
||||
);
|
||||
assert_eq!("ee5033e71c4674a94c511ac177f2a2f41835d2c8724f8a98632a2193786ce40b", sig.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_authorization_sha256_1() {
|
||||
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 auth = get_authorization(
|
||||
&SignAlgorithm::Sha256,
|
||||
&DerivedAccessKey {
|
||||
access_key_id: "AccessKeyId".to_string(),
|
||||
derived_access_key_secret: signing_key,
|
||||
},
|
||||
"20121231",
|
||||
"cn-hangzhou",
|
||||
"test",
|
||||
"/test",
|
||||
"POST",
|
||||
&query,
|
||||
&headers,
|
||||
"PAYLOAD",
|
||||
);
|
||||
assert_eq!("ACS4-HMAC-SHA256 Credential=AccessKeyId/20121231/cn-hangzhou/test/aliyun_v4_request,\
|
||||
SignedHeaders=host,Signature=ee5033e71c4674a94c511ac177f2a2f41835d2c8724f8a98632a2193786ce40b", auth.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_authorization_sha256_2() {
|
||||
let signing_key = get_signing_key(
|
||||
&SignAlgorithm::Sha256,
|
||||
"AccessKeySecret",
|
||||
"20121231",
|
||||
"cn-hangzhou",
|
||||
"test");
|
||||
|
||||
let mut query = BTreeMap::new();
|
||||
query.insert("a b".to_string(), "C D-=/".to_string());
|
||||
query.insert("x b".to_string(), "X----Y".to_string());
|
||||
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 auth = get_authorization(
|
||||
&SignAlgorithm::Sha256,
|
||||
&DerivedAccessKey {
|
||||
access_key_id: "AccessKeyId".to_string(),
|
||||
derived_access_key_secret: signing_key,
|
||||
},
|
||||
"20121231",
|
||||
"cn-hangzhou",
|
||||
"test",
|
||||
"/test",
|
||||
"POST",
|
||||
&query,
|
||||
&headers,
|
||||
"PAYLOAD",
|
||||
);
|
||||
assert_eq!("ACS4-HMAC-SHA256 Credential=AccessKeyId/20121231/cn-hangzhou/test/aliyun_v4_request,\
|
||||
SignedHeaders=host;x-acs-date,Signature=e227bad4cdadedccb5b9ff2f6f39d89bb4167e61a5100262d116a331f546ffab", auth.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_authorization_sm3() {
|
||||
let signing_key = get_signing_key(
|
||||
&SignAlgorithm::Sm3,
|
||||
"AccessKeySecret",
|
||||
"20121231",
|
||||
"cn-hangzhou",
|
||||
"test");
|
||||
|
||||
let query = BTreeMap::new();
|
||||
let mut headers = BTreeMap::new();
|
||||
headers.insert("host".to_string(), "example.com".to_string());
|
||||
let auth = get_authorization(
|
||||
&SignAlgorithm::Sm3,
|
||||
&DerivedAccessKey {
|
||||
access_key_id: "AccessKeyId".to_string(),
|
||||
derived_access_key_secret: signing_key,
|
||||
},
|
||||
"20121231",
|
||||
"cn-hangzhou",
|
||||
"test",
|
||||
"/test",
|
||||
"POST",
|
||||
&query,
|
||||
&headers,
|
||||
"PAYLOAD",
|
||||
);
|
||||
assert_eq!("ACS4-HMAC-SM3 Credential=AccessKeyId/20121231/cn-hangzhou/test/aliyun_v4_request,\
|
||||
SignedHeaders=host,Signature=07d7fc2773a74656109127d365d2933847735a9bbfa9bbb932780f7eeb69dde8", auth.as_str());
|
||||
}
|
||||
Reference in New Issue
Block a user