use proptest::prelude::*;
use stratum_node_core::{
card_names_owner, profile_acl, profile_card, profile_card_path, profile_owner, DidNostr,
NostrSecretKey, PodPath, PROFILE_CARD_RELATIVE,
};
fn key(byte: u8) -> DidNostr {
NostrSecretKey::from_hex(&format!("{byte:02x}").repeat(32))
.expect("fixed key")
.public()
}
fn path(owner: &DidNostr) -> String {
profile_card_path(owner).as_str().to_owned()
}
#[test]
fn the_profile_path_is_fixed() {
let owner = key(7);
let hex = owner.to_hex();
assert_eq!(path(&owner), format!("/pods/{hex}/profile/card"), "path");
assert_eq!(
PodPath::pod_root(&owner)
.join(PROFILE_CARD_RELATIVE)
.expect("join")
.as_str(),
path(&owner),
"relative form"
);
assert_eq!(
profile_owner(path(&owner).as_bytes()),
Some(owner),
"matcher accepts it"
);
}
#[test]
fn the_refusal_table_is_refused_byte_for_byte() {
let hex = key(7).to_hex();
let upper = hex.to_uppercase();
let base = format!("/pods/{hex}");
let refused: Vec<Vec<u8>> = [
format!("{base}/profile/card.acl"),
format!("{base}/profile/card/"),
format!("{base}/profile/Card"),
format!("{base}/profile/"),
format!("{base}/profile"),
format!("{base}/profile/card.meta"),
format!("{base}/profile/card.meta.json"),
format!("{base}/profile//card"),
format!("{base}//profile/card"),
format!("{base}/profile/./card"),
format!("{base}/./profile/card"),
format!("{base}/profile/x/../card"),
format!("{base}/profile/%2e/card"),
format!("{base}/profile/%63ard"),
format!("{base}/profile/card%00"),
format!("/pods/{upper}/profile/card"),
format!("{base}/profile/card?"),
format!("{base}/profile/card?x=1"),
format!("{base}/profile/card#me"),
format!("{base}/profile/card "),
format!(" {base}/profile/card"),
format!("{base}/PROFILE/card"),
format!("{base}.acl"),
format!("{base}/"),
format!("/pods/{hex}0/profile/card"),
format!("/pods/{}/profile/card", &hex[1..]),
format!("/Pods/{hex}/profile/card"),
format!("//pods/{hex}/profile/card"),
format!("http://h/pods/{hex}/profile/card"),
format!("/pods/did:nostr:{hex}/profile/card"),
String::new(),
]
.into_iter()
.map(String::into_bytes)
.chain([
[base.as_bytes(), b"/profile/card\0"].concat(),
[base.as_bytes(), b"/profile/card\r\n"].concat(),
[base.as_bytes(), b"/profile/c\xc3\xa1rd"].concat(),
])
.collect();
for target in refused {
assert_eq!(
profile_owner(&target),
None,
"{:?}",
String::from_utf8_lossy(&target)
);
}
}
#[test]
fn a_key_that_is_not_a_point_is_refused() {
let target = format!("/pods/{}/profile/card", "0".repeat(64));
assert!(DidNostr::from_hex(&"0".repeat(64)).is_err(), "not a key");
assert_eq!(profile_owner(target.as_bytes()), None, "refused");
}
proptest! {
#[test]
fn arbitrary_bytes_are_accepted_only_when_exact(target in proptest::collection::vec(any::<u8>(), 0..96)) {
if let Some(owner) = profile_owner(&target) {
prop_assert_eq!(target, path(&owner).into_bytes());
}
}
#[test]
fn single_edits_of_a_valid_path_are_refused(
byte in 1u8..=0xfe,
at in 0usize..83,
edit in 0u8..3,
insert in any::<u8>(),
) {
let owner = key(byte);
let valid = path(&owner).into_bytes();
let mut edited = valid.clone();
let at = at.min(valid.len() - 1);
match edit {
0 => edited.insert(at, insert),
1 => { edited.remove(at); }
_ => edited[at] = insert,
}
if let Some(found) = profile_owner(&edited) {
prop_assert_eq!(&edited, &path(&found).into_bytes());
prop_assert_eq!(edit, 2);
prop_assert!((6..70).contains(&at));
} else {
prop_assert_ne!(edited, valid);
}
}
#[test]
fn every_key_is_matched(byte in 1u8..=0xfe) {
let owner = key(byte);
prop_assert_eq!(profile_owner(path(&owner).as_bytes()), Some(owner));
}
}
#[test]
fn the_card_is_one_subject_and_two_statements() {
let owner = key(9);
let card = profile_card(&owner);
assert_eq!(
card,
format!(
"@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n\
@prefix owl: <http://www.w3.org/2002/07/owl#> .\n\
\n\
<#me> a foaf:Person ;\n owl:sameAs <did:nostr:{}> .\n",
owner.to_hex()
),
"exact card"
);
assert!(card_names_owner(card.as_bytes(), &owner), "names its owner");
assert!(
!card_names_owner(card.as_bytes(), &key(10)),
"and no one else"
);
}
#[test]
fn the_acl_has_exactly_the_two_measured_rules() {
let owner = key(9);
let hex = owner.to_hex();
let acl = profile_acl(&owner);
assert_eq!(
acl,
format!(
"@prefix acl: <http://www.w3.org/ns/auth/acl#> .\n\
@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n\
\n\
<#owner> a acl:Authorization ;\n \
acl:agent <did:nostr:{hex}> ;\n \
acl:accessTo </pods/{hex}/profile/card> ;\n \
acl:mode acl:Read, acl:Write, acl:Control .\n\
\n\
<#public> a acl:Authorization ;\n \
acl:agentClass foaf:Agent ;\n \
acl:accessTo </pods/{hex}/profile/card> ;\n \
acl:mode acl:Read .\n"
),
"exact acl"
);
assert!(!acl.contains("default"), "no acl:default");
}
fn prefixes() -> &'static str {
"@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n@prefix owl: <http://www.w3.org/2002/07/owl#> .\n"
}
#[test]
fn customised_cards_that_name_the_owner_are_accepted() {
let owner = key(11);
let did = owner.to_string();
let accepted = [
format!("{}<#me> a foaf:Person ; foaf:name \"Ada; owl:sameAs\" ; owl:sameAs <{did}> .\n", prefixes()),
format!("{}<#me> owl:sameAs <{did}> .", prefixes()),
format!("{}<#me> owl:sameAs <https://example.org/ada>, <{did}> .\n", prefixes()),
format!("<#me> <http://www.w3.org/2002/07/owl#sameAs> <{did}> .\n"),
format!("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n<#me> owl:sameAs <{did}> .\n"),
format!("{}# a comment <did:nostr:x> is not an IRI\n<#me> a foaf:Person.\n<#me> owl:sameAs <{did}>.\n", prefixes()),
format!("{}<#other> foaf:name \"x\" .\n<#me> a foaf:Person ;\n owl:sameAs <{did}> ;\n foaf:nick '''multi\nline''' .\n", prefixes()),
];
for card in accepted {
assert!(
card_names_owner(card.as_bytes(), &owner),
"accepted:\n{card}"
);
}
}
#[test]
fn cards_that_do_not_clearly_name_the_owner_are_refused() {
let owner = key(11);
let did = owner.to_string();
let stranger = key(12).to_string();
let refused = [
format!("{}<#me> a foaf:Person . # owl:sameAs <{did}>\n", prefixes()),
format!(
"{}<#me> foaf:name \"<#me> owl:sameAs <{did}> .\" .\n",
prefixes()
),
format!(
"{}<#me> foaf:name '''\n<#me> owl:sameAs <{did}> .\n''' .\n",
prefixes()
),
format!("{}<#other> owl:sameAs <{did}> .\n", prefixes()),
format!("{}<#me> foaf:knows <{did}> .\n", prefixes()),
format!("{}<{did}> owl:sameAs <#me> .\n", prefixes()),
format!("{}<#me> owl:sameAs <{did}>, <{stranger}> .\n", prefixes()),
format!(
"{}<#me> owl:sameAs <{did}> .\n<#x> foaf:knows <{stranger}> .\n",
prefixes()
),
format!("{}<#me> owl:sameAs <{stranger}> .\n", prefixes()),
format!(
"{}<#me> owl:sameAs <DID:NOSTR:{}> .\n",
prefixes(),
owner.to_hex()
),
format!("@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n<#me> owl:sameAs <{did}> .\n"),
format!(
"{}@prefix owl: <http://example.org/> .\n<#me> owl:sameAs <{did}> .\n",
prefixes()
),
format!(
"{}@base <https://elsewhere.example/> .\n<#me> owl:sameAs <{did}> .\n",
prefixes()
),
format!(
"BASE <https://elsewhere.example/>\n{}<#me> owl:sameAs <{did}> .\n",
prefixes()
),
format!(
"{}@prefix d: <did:nostr:> .\n<#me> owl:sameAs d:{} .\n",
prefixes(),
owner.to_hex()
),
format!(
"{}@prefix d: <did:> .\n<#me> owl:sameAs <{did}>, d:nostr:{} .\n",
prefixes(),
key(12).to_hex()
),
format!(
"{}<#me> owl:sameAs <{did}>, <did:web:example.org> .\n",
prefixes()
),
format!("{}<#me> owl:sameAs [ owl:sameAs <{did}> ] .\n", prefixes()),
format!("{}<#me> owl:sameAs ( <{did}> ) .\n", prefixes()),
format!("{}<#me> owl:sameAs <{did}", prefixes()),
format!(
"{}<#me> foaf:name \"open ; owl:sameAs <{did}> .\n",
prefixes()
),
String::new(),
];
for card in refused {
assert!(
!card_names_owner(card.as_bytes(), &owner),
"refused:\n{card}"
);
}
assert!(!card_names_owner(b"\xff\xfe<#me>", &owner), "not utf-8");
}
#[test]
fn cards_that_hide_a_directive_or_break_the_grammar_are_refused() {
let owner = key(11);
let did = owner.to_string();
let p = prefixes();
let naming = format!("<#me> owl:sameAs <{did}> .\n");
let refused = [
format!("{p}# c\r@base <https://other.example/card> .\n{naming}"),
format!("{p}<#me> ; owl:sameAs <{did}> .\n"),
format!("{p}<#me> owl:sameAs <{did}>\n"),
format!("{p}{naming}<#x> a foaf:Person\n"),
format!("{p}{naming}<#x> foaf:name .\n"),
format!("{p}{naming}\"x\" foaf:name \"y\" .\n"),
format!(
"{p}<#s> foaf:name foaf:o\\# . @base <https://other.example/> . <#t> foaf:name\nfoaf:o .\n{naming}"
),
format!("{p}<#s> foaf:name \"a\\\n@base <https://other.example/> . \" .\n{naming}"),
format!("{p}<#me> foaf:name \"a\r\" ; owl:sameAs <{did}> .\n"),
format!("\u{feff}@base <https://other.example/> .\n{p}{naming}"),
format!("{p}<#me>\x0cowl:sameAs <{did}> .\n"),
format!("@BASE <https://other.example/> .\n{p}{naming}"),
format!("{p}@keywords a .\n{naming}"),
format!("{p}<#me> owl:sameAs <{did}>, <\\u0064id:web:example.org> .\n"),
format!("{p}{naming}<#x> ex:p <#y> .\n"),
format!("{p}@prefix d: <{did}> .\n<#me> owl:sameAs <{did}>, d:x .\n"),
format!("{p}PREFIX d: <{did}>\n<#me> owl:sameAs <{did}>, d:x .\n"),
];
let wrongly_accepted: Vec<(usize, &String)> = refused
.iter()
.enumerate()
.filter(|(_, card)| card_names_owner(card.as_bytes(), &owner))
.collect();
assert!(
wrongly_accepted.is_empty(),
"accepted, should be refused: {wrongly_accepted:#?}"
);
}
#[test]
fn valid_turtle_around_the_naming_statement_is_still_accepted() {
let owner = key(11);
let did = owner.to_string();
let p = prefixes();
let xsd = "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n";
let accepted = [
format!("{}\r\n<#me> owl:sameAs <{did}> .\r\n", p.replace('\n', "\r\n")),
format!("{p}# a comment\r<#me> owl:sameAs <{did}> .\n"),
format!("{p}{xsd}<#me> foaf:name \"Ada\"@en-GB ; foaf:age \"36\"^^xsd:int ; owl:sameAs <{did}> .\n"),
format!("{p}<#me> foaf:nick \"x\"^^<http://www.w3.org/2001/XMLSchema#string> ; owl:sameAs <{did}> ; .\n"),
format!("{p}<#me> a foaf:Person ;; owl:sameAs <{did}> .\n"),
format!("{p}<#me> foaf:knows [ foaf:name \"Bob\" ; foaf:knows ( <#a> _:b0 ) ] .\n<#me> owl:sameAs <{did}> .\n"),
format!("{p}[] foaf:name \"anon\" .\n_:b1 foaf:age 42, -1.5e3, true .\n<#me> owl:sameAs <{did}> .\n"),
format!("{p}[ foaf:name \"subject list\" ] .\n( 1 2 ) foaf:name \"collection\" .\n<#me> owl:sameAs <{did}> .\n"),
format!("{p}<#me> foaf:name 'single', \"\"\"long \"quoted\" \\u00e9 text\"\"\" ; owl:sameAs <{did}> .\n"),
];
let wrongly_refused: Vec<(usize, &String)> = accepted
.iter()
.enumerate()
.filter(|(_, card)| !card_names_owner(card.as_bytes(), &owner))
.collect();
assert!(
wrongly_refused.is_empty(),
"refused, should be accepted: {wrongly_refused:#?}"
);
}
proptest! {
#[test]
fn arbitrary_cards_never_panic(card in proptest::collection::vec(any::<u8>(), 0..512)) {
let owner = key(11);
let accepted = card_names_owner(&card, &owner);
let names = card
.windows(owner.to_string().len())
.any(|window| window == owner.to_string().as_bytes());
prop_assert!(!accepted || names);
}
#[test]
fn turtle_like_cards_never_panic(parts in proptest::collection::vec(
prop::sample::select(vec![
"<#me>", "<#x>", " ", "\n", "\r", ".", ";", ",", "[", "]", "(", ")", "a", "owl:sameAs",
"foaf:name", "\"s\"", "'''l'''", "@en", "^^", "_:b", "1.5", "# c", "@prefix", "owl:",
"<http: ]),
0..40,
)) {
let owner = key(11);
let card = parts.concat();
prop_assert!(!card_names_owner(card.as_bytes(), &owner));
}
}