use kya_validator::types::{
ContentCheck, ContentCheckType, DigestConfig, HashAlgorithm, LinkCheckConfig, ValidationConfig,
};
use kya_validator::validator::validate_manifest_with_config;
use serde_json::json;
#[test]
fn case_1_identity_thief_signature_mismatch() {
let manifest = json!({
"kyaVersion": "1.0",
"agentId": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2",
"agentName": "Legitimate Agent",
"proof": [{
"type": "Ed25519Signature2020",
"verificationMethod": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2#key-1",
"proofPurpose": "assertionMethod",
"proofValue": "z4MXdiNmmVZ9qMvxZC7PQ8P9vJxRv6zZ7Y8e3r5QK6vQ8cF7z8z6Z5r9pN8j9V3xG9k4K2"
}],
"issuanceDate": "2024-01-01T00:00:00Z",
"expirationDate": "2025-12-31T23:59:59Z"
});
let config = ValidationConfig::default();
let report = validate_manifest_with_config(&manifest, &config);
assert!(!report.crypto_valid, "Expected crypto validation to fail");
assert!(!report.crypto_errors.is_empty(), "Expected crypto errors");
}
#[test]
fn case_2_dead_link_resource_unreachable() {
let manifest = json!({
"kyaVersion": "1.0",
"agentId": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2",
"agentName": "Test Agent",
"proof": [],
"policyUrl": "https://this-domain-does-not-exist-12345.com/policy.pdf",
"issuanceDate": "2024-01-01T00:00:00Z",
"expirationDate": "2025-12-31T23:59:59Z"
});
let config = ValidationConfig {
check_external_links: true,
link_checks: vec![LinkCheckConfig {
json_pointer: "/policyUrl".to_string(),
required_contains: None,
tls_pin: None,
allowed_domains: None,
content_check: None,
verify_digest: None,
timeout_secs: Some(5),
max_retries: Some(1),
cache_ttl_secs: None,
}],
..Default::default()
};
let report = validate_manifest_with_config(&manifest, &config);
assert!(
!report.inspector_valid,
"Expected inspector validation to fail"
);
assert!(
!report.inspector_errors.is_empty(),
"Expected inspector errors"
);
assert!(
report
.inspector_errors
.iter()
.any(|e| e.contains("Failed to fetch")),
"Expected fetch error in inspector errors"
);
}
#[test]
fn case_3_poor_agent_policy_violation() {
let manifest = json!({
"kyaVersion": "1.0",
"agentId": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2",
"agentName": "Poor Agent",
"proof": [],
"solvencyAddress": "0x742d35Cc6634C0532925a3b844Bc9e7595f0bE",
"solvencyChain": "ethereum",
"declaredBalance": "1000000000000000000", "maxTransactionValue": 1000000000, "issuanceDate": "2024-01-01T00:00:00Z",
"expirationDate": "2025-12-31T23:59:59Z"
});
let config = ValidationConfig::default();
let report = validate_manifest_with_config(&manifest, &config);
assert!(report.policy_valid || !report.policy_errors.is_empty());
}
#[test]
fn case_4_imposter_did_verification_mismatch() {
let manifest = json!({
"kyaVersion": "1.0",
"agentId": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2",
"agentName": "Imposter Agent",
"verificationMethod": [{
"id": "did:another:key:z5MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGt#key-1",
"type": "Ed25519VerificationKey2020",
"controller": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2",
"publicKeyMultibase": "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2"
}],
"proof": [{
"type": "Ed25519Signature2020",
"verificationMethod": "did:another:key:z5MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGt#key-1",
"proofPurpose": "assertionMethod",
"proofValue": "z4MXdiNmmVZ9qMvxZC7PQ8P9vJxRv6zZ7Y8e3r5QK6vQ8cF7z8z6Z5r9pN8j9V3xG9k4K2"
}],
"issuanceDate": "2024-01-01T00:00:00Z",
"expirationDate": "2025-12-31T23:59:59Z"
});
let config = ValidationConfig {
enforce_controller_match: true,
..Default::default()
};
let report = validate_manifest_with_config(&manifest, &config);
assert!(!report.crypto_valid, "Expected crypto validation to fail");
assert!(!report.crypto_errors.is_empty(), "Expected crypto errors");
}
#[test]
fn case_5_schema_drift_validation_error() {
let manifest = json!({
"kyaVersion": "1.0",
"agentId": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2",
"agentName": "Drift Agent",
"proof": [],
"invalidField": "This field should not exist according to schema",
"issuanceDate": "2024-01-01T00:00:00Z",
"expirationDate": "2025-12-31T23:59:59Z"
});
let config = ValidationConfig::default();
let report = validate_manifest_with_config(&manifest, &config);
assert!(!report.schema_valid, "Expected schema validation to fail");
assert!(!report.schema_errors.is_empty(), "Expected schema errors");
}
#[test]
fn test_content_hash_verification() {
let manifest = json!({
"kyaVersion": "1.0",
"agentId": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2",
"agentName": "Hash Test Agent",
"proof": [],
"schemaUrl": "https://example.com/schema.json",
"schemaHash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "issuanceDate": "2024-01-01T00:00:00Z",
"expirationDate": "2025-12-31T23:59:59Z"
});
let config = ValidationConfig {
check_external_links: true,
link_checks: vec![LinkCheckConfig {
json_pointer: "/schemaUrl".to_string(),
required_contains: None,
tls_pin: None,
allowed_domains: None,
content_check: None,
verify_digest: Some(DigestConfig {
algorithm: HashAlgorithm::Sha256,
expected_hash: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
.to_string(),
}),
timeout_secs: Some(5),
max_retries: Some(1),
cache_ttl_secs: None,
}],
..Default::default()
};
let report = validate_manifest_with_config(&manifest, &config);
assert!(!report.inspector_errors.is_empty() || report.inspector_valid);
}
#[test]
fn test_json_pointer_content_check() {
let manifest = json!({
"kyaVersion": "1.0",
"agentId": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2",
"agentName": "Content Check Agent",
"proof": [],
"policyUrl": "https://example.com/policy.json",
"issuanceDate": "2024-01-01T00:00:00Z",
"expirationDate": "2025-12-31T23:59:59Z"
});
let config = ValidationConfig {
check_external_links: true,
link_checks: vec![LinkCheckConfig {
json_pointer: "/policyUrl".to_string(),
required_contains: None,
tls_pin: None,
allowed_domains: None,
content_check: Some(ContentCheck {
check_type: ContentCheckType::JsonPointerEquals,
expected_value: "valid".to_string(),
json_pointer: Some("/status".to_string()),
}),
verify_digest: None,
timeout_secs: Some(5),
max_retries: Some(1),
cache_ttl_secs: None,
}],
..Default::default()
};
let report = validate_manifest_with_config(&manifest, &config);
assert!(!report.inspector_errors.is_empty() || report.inspector_valid);
}