Skip to content

Commit

Permalink
Merge pull request #2 from TheDhejavu/feat/add-test
Browse files Browse the repository at this point in the history
feat: add test
  • Loading branch information
TheDhejavu authored Jul 2, 2024
2 parents e9a7cab + 4491b92 commit f9edbeb
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ fn main() {
Err(e) => println!("Failed to decode and create assertion: {:?}", e),
}
}

12 changes: 12 additions & 0 deletions src/assertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,16 @@ impl Assertion {

Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_from_base64_valid() {
let valid_cbor_base64 = "omlzaWduYXR1cmVYRjBEAiAImFuY4+UbGZ5/ZbjAJpjQ3bd8GxaKFpMEo58WMEUGbwIgaqdDJnVS8/3oJCz16O5Zp4Qga5g6zrFF7eoiYEWkdtNxYXV0aGVudGljYXRvckRhdGFYJaRc2WwGuoniZEqtF+kolObjxcczFdDxbrhJR/nT8ehTQAAAAAI=";
let result = Assertion::from_base64(valid_cbor_base64);
assert!(result.is_ok());
}
}
36 changes: 36 additions & 0 deletions src/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,39 @@ impl Attestation {
Ok((public_key_bytes.0.clone(), self.statement.receipt))
}
}


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_from_base64_valid() {
let valid_cbor_base64 = "o2NmbXRvYXBwbGUtYXBwYXR0ZXN0Z2F0dFN0bXSiY3g1Y4JZAzEwggMtMIICs6ADAgECAgYBkGqxbE8wCgYIKoZIzj0EAwIwTzEjMCEGA1UEAwwaQXBwbGUgQXBwIEF0dGVzdGF0aW9uIENBIDExEzARBgNVBAoMCkFwcGxlIEluYy4xEzARBgNVBAgMCkNhbGlmb3JuaWEwHhcNMjQwNjI5MTk0ODUwWhcNMjUwMTI0MDcyNzUwWjCBkTFJMEcGA1U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BcHBsaWNhdGlvbiBJbnRlZ3JhdGlvbiBDQSA1IC0gRzExJjAkBgNVBAsMHUFwcGxlIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MRMwEQYDVQQKDApBcHBsZSBJbmMuMQswCQYDVQQGEwJVUwIQfgISYNjOd6typZ3waCe+/TANBglghkgBZQMEAgEFADAKBggqhkjOPQQDAgRHMEUCIDzodg4szIkkk6IxaqaR/NcsLQO3LtXn9DDBt/yoESUYAiEApRtfQvovTtktiicXHCiBke0Dzlyk14nuYQUnNNumVR0AAAAAAABoYXV0aERhdGFYpKRc2WwGuoniZEqtF+kolObjxcczFdDxbrhJR/nT8ehTQAAAAABhcHBhdHRlc3QAAAAAAAAAACAbd5/2ke303gPFSOj+GWL21XmAYMpoPdR7srxyp43+v6UBAgMmIAEhWCDVrgv9TJ/pAmgUQYA0gtXDRV9vw3TRJv8C1qtpFZ4POCJYIMIBHcByLUsDZSFPJQQxM3nRmKD1ELEfd0RXzKZrhhXn";
let result = Attestation::from_base64(valid_cbor_base64);
assert!(result.is_ok());
}

#[test]
fn test_verify_certificates_empty() {
let empty_certs = Vec::new();
let root_cert_pem = b"-----BEGIN CERTIFICATE-----\n\
MIICITCCAaegAwIBAgIQC/O+DvHN0uD7jG5yH2IXmDAKBggqhkjOPQQDAzBSMSYw\n\
JAYDVQQDDB1BcHBsZSBBcHAgQXR0ZXN0YXRpb24gUm9vdCBDQTETMBEGA1UECgwK\n\
QXBwbGUgSW5jLjETMBEGA1UECAwKQ2FsaWZvcm5pYTAeFw0yMDAzMTgxODMyNTNa\n\
Fw00NTAzMTUwMDAwMDBaMFIxJjAkBgNVBAMMHUFwcGxlIEFwcCBBdHRlc3RhdGlv\n\
biBSb290IENBMRMwEQYDVQQKDApBcHBsZSBJbmMuMRMwEQYDVQQIDApDYWxpZm9y\n\
bmlhMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAERTHhmLW07ATaFQIEVwTtT4dyctdh\n\
NbJhFs/Ii2FdCgAHGbpphY3+d8qjuDngIN3WVhQUBHAoMeQ/cLiP1sOUtgjqK9au\n\
Yen1mMEvRq9Sk3Jm5X8U62H+xTD3FE9TgS41o0IwQDAPBgNVHRMBAf8EBTADAQH/\n\
MB0GA1UdDgQWBBSskRBTM72+aEH/pwyp5frq5eWKoTAOBgNVHQ8BAf8EBAMCAQYw\n\
CgYIKoZIzj0EAwMDaAAwZQIwQgFGnByvsiVbpTKwSga0kP0e8EeDS4+sQmTvb7vn\n\
53O5+FRXgeLhpJ06ysC5PrOyAjEAp5U4xDgEgllF7En3VcE3iexZZtKeYnpqtijV\n\
oyFraWVIyd/dganmrduC1bmTBGwD\n\
-----END CERTIFICATE-----";

let root_cert = openssl::x509::X509::from_pem(root_cert_pem).unwrap();
let result = Attestation::verify_certificates(empty_certs, &root_cert);
assert!(result.is_err());
}
}
80 changes: 80 additions & 0 deletions src/authenticator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,83 @@ impl AAGUID {
}

}


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_auth_data_new_valid() {
let mut bytes = vec![0u8; 37];
// use random flags
bytes[32] = 0b00000001;
// set counter to 1
BigEndian::write_u32(&mut bytes[33..37], 1);

let result = AuthenticatorData::new(bytes);
assert!(result.is_ok());

let auth_data = result.unwrap();
assert_eq!(auth_data.counter, 1);
}

#[test]
fn test_auth_data_new_too_short() {
// here, we create bytes less than required 37 bytes
let bytes = vec![0u8; 36];
let result = AuthenticatorData::new(bytes);
assert!(result.is_err());
}

#[test]
fn test_aaguid_new_valid() {
let appattest_bytes = APP_ATTEST.as_bytes().to_vec();
let result = AAGUID::new(appattest_bytes);
assert!(result.is_ok());
assert_eq!(result.unwrap().0, APP_ATTEST);
}

#[test]
fn test_aaguid_new_invalid() {
let invalid_bytes = vec![0u8; 16]; // use dummy bytes that does not match any known AAGUID
let result = AAGUID::new(invalid_bytes);
assert!(result.is_err());
}

#[test]
fn test_verify_app_id() {
let app_id = "app.apple.connect";
let mut hasher = Sha256::new();
hasher.update(app_id.as_bytes());
let hash = hasher.finalize().to_vec();

let auth_data = AuthenticatorData {
bytes: vec![],
rp_id_hash: hash,
flags: 0,
counter: 0,
aaguid: None,
credential_id: None,
};

assert!(auth_data.verify_app_id("app.apple.connect").is_ok());
assert!(auth_data.verify_app_id("invalid.apple.connect").is_err());
}

#[test]
fn test_verify_key_id() {
let key_id = vec![1, 2, 3, 4];
let auth_data = AuthenticatorData {
bytes: vec![],
rp_id_hash: vec![],
flags: 0,
counter: 0,
aaguid: None,
credential_id: Some(key_id.clone()),
};

assert!(auth_data.verify_key_id(&key_id).is_ok());
assert!(auth_data.verify_key_id(&vec![4, 3, 2, 1]).is_err());
}
}

0 comments on commit f9edbeb

Please sign in to comment.