use indoc::indoc;
mod common;
use common::*;
#[test]
fn test_xid_resolution_basic() {
bc_envelope::register_tags();
let xid_doc =
run_cli(&["xid", "new", "--nickname", "Alice", ALICE_PUBKEYS]).unwrap();
#[rustfmt::skip]
run_cli_expect(
&["format", &xid_doc],
indoc! {r#"
XID(93a4d4e7) [
'key': PublicKeys(cab108a0, SigningPublicKey(93a4d4e7, SchnorrPublicKey(26712894)), EncapsulationPublicKey(00b42db3, X25519PublicKey(00b42db3))) [
'allow': 'All'
'nickname': "Alice"
]
]
"#}.trim()
).unwrap();
run_cli_expect(&["xid", "resolution", "count", &xid_doc], "0").unwrap();
let xid_doc = run_cli(&[
"xid",
"resolution",
"add",
"https://resolver.example.com",
&xid_doc,
])
.unwrap();
#[rustfmt::skip]
run_cli_expect(
&["format", &xid_doc],
indoc! {r#"
XID(93a4d4e7) [
'dereferenceVia': URI(https://resolver.example.com)
'key': PublicKeys(cab108a0, SigningPublicKey(93a4d4e7, SchnorrPublicKey(26712894)), EncapsulationPublicKey(00b42db3, X25519PublicKey(00b42db3))) [
'allow': 'All'
'nickname': "Alice"
]
]
"#}.trim()
).unwrap();
run_cli_expect(&["xid", "resolution", "count", &xid_doc], "1").unwrap();
run_cli_expect(
&["xid", "resolution", "all", &xid_doc],
"https://resolver.example.com",
)
.unwrap();
run_cli_expect(
&["xid", "resolution", "at", "0", &xid_doc],
"https://resolver.example.com",
)
.unwrap();
}
#[test]
fn test_xid_resolution_multiple() {
bc_envelope::register_tags();
let xid_doc =
run_cli(&["xid", "new", "--nickname", "Alice", ALICE_PUBKEYS]).unwrap();
let xid_doc = run_cli(&[
"xid",
"resolution",
"add",
"https://resolver.example.com",
&xid_doc,
])
.unwrap();
let xid_doc =
run_cli(&["xid", "resolution", "add", "btcr:01234567", &xid_doc])
.unwrap();
#[rustfmt::skip]
run_cli_expect(
&["format", &xid_doc],
indoc! {r#"
XID(93a4d4e7) [
'dereferenceVia': URI(btcr:01234567)
'dereferenceVia': URI(https://resolver.example.com)
'key': PublicKeys(cab108a0, SigningPublicKey(93a4d4e7, SchnorrPublicKey(26712894)), EncapsulationPublicKey(00b42db3, X25519PublicKey(00b42db3))) [
'allow': 'All'
'nickname': "Alice"
]
]
"#}.trim()
).unwrap();
run_cli_expect(&["xid", "resolution", "count", &xid_doc], "2").unwrap();
let all_methods = run_cli(&["xid", "resolution", "all", &xid_doc]).unwrap();
assert!(all_methods.contains("https://resolver.example.com"));
assert!(all_methods.contains("btcr:01234567"));
}
#[test]
fn test_xid_resolution_remove() {
bc_envelope::register_tags();
let xid_doc =
run_cli(&["xid", "new", "--nickname", "Alice", ALICE_PUBKEYS]).unwrap();
let xid_doc = run_cli(&[
"xid",
"resolution",
"add",
"https://resolver.example.com",
&xid_doc,
])
.unwrap();
let xid_doc =
run_cli(&["xid", "resolution", "add", "btcr:01234567", &xid_doc])
.unwrap();
#[rustfmt::skip]
run_cli_expect(
&["format", &xid_doc],
indoc! {r#"
XID(93a4d4e7) [
'dereferenceVia': URI(btcr:01234567)
'dereferenceVia': URI(https://resolver.example.com)
'key': PublicKeys(cab108a0, SigningPublicKey(93a4d4e7, SchnorrPublicKey(26712894)), EncapsulationPublicKey(00b42db3, X25519PublicKey(00b42db3))) [
'allow': 'All'
'nickname': "Alice"
]
]
"#}.trim()
).unwrap();
run_cli_expect(&["xid", "resolution", "count", &xid_doc], "2").unwrap();
let xid_doc =
run_cli(&["xid", "resolution", "remove", "btcr:01234567", &xid_doc])
.unwrap();
#[rustfmt::skip]
run_cli_expect(
&["format", &xid_doc],
indoc! {r#"
XID(93a4d4e7) [
'dereferenceVia': URI(https://resolver.example.com)
'key': PublicKeys(cab108a0, SigningPublicKey(93a4d4e7, SchnorrPublicKey(26712894)), EncapsulationPublicKey(00b42db3, X25519PublicKey(00b42db3))) [
'allow': 'All'
'nickname': "Alice"
]
]
"#}.trim()
).unwrap();
run_cli_expect(&["xid", "resolution", "count", &xid_doc], "1").unwrap();
run_cli_expect(
&["xid", "resolution", "all", &xid_doc],
"https://resolver.example.com",
)
.unwrap();
}
#[test]
fn test_xid_resolution_remove_not_found() {
bc_envelope::register_tags();
let xid_doc =
run_cli(&["xid", "new", "--nickname", "Alice", ALICE_PUBKEYS]).unwrap();
let xid_doc = run_cli(&[
"xid",
"resolution",
"add",
"https://resolver.example.com",
&xid_doc,
])
.unwrap();
let result = run_cli(&[
"xid",
"resolution",
"remove",
"https://nonexistent.com",
&xid_doc,
]);
assert!(result.is_err());
let err_msg = result.unwrap_err().to_string();
assert!(err_msg.contains("Resolution method not found"));
assert!(err_msg.contains("https://nonexistent.com"));
}
#[test]
fn test_xid_resolution_index_out_of_bounds() {
bc_envelope::register_tags();
let xid_doc =
run_cli(&["xid", "new", "--nickname", "Alice", ALICE_PUBKEYS]).unwrap();
let xid_doc = run_cli(&[
"xid",
"resolution",
"add",
"https://resolver.example.com",
&xid_doc,
])
.unwrap();
let result = run_cli(&["xid", "resolution", "at", "5", &xid_doc]);
assert!(result.is_err());
let err_msg = result.unwrap_err().to_string();
assert!(err_msg.contains("Index out of bounds"));
}
#[test]
fn test_xid_resolution_with_signature() {
bc_envelope::register_tags();
let xid_doc = run_cli(&[
"xid",
"new",
"--nickname",
"Alice",
"--private",
"encrypt",
"--encrypt-password",
"secret",
"--sign",
"inception",
ALICE_PRVKEYS,
])
.unwrap();
#[rustfmt::skip]
run_cli_expect(
&["format", &xid_doc],
indoc! {r#"
{
XID(93a4d4e7) [
'key': PublicKeys(cab108a0, SigningPublicKey(93a4d4e7, SchnorrPublicKey(26712894)), EncapsulationPublicKey(00b42db3, X25519PublicKey(00b42db3))) [
{
'privateKey': ENCRYPTED [
'hasSecret': EncryptedKey(Argon2id)
]
} [
'salt': Salt
]
'allow': 'All'
'nickname': "Alice"
]
]
} [
'signed': Signature
]
"#}.trim()
).unwrap();
let xid_doc = run_cli(&[
"xid",
"resolution",
"add",
"--verify",
"inception",
"--password",
"secret",
"--sign",
"inception",
"--private",
"encrypt",
"--encrypt-password",
"secret",
"https://resolver.example.com",
&xid_doc,
])
.unwrap();
#[rustfmt::skip]
run_cli_expect(
&["format", &xid_doc],
indoc! {r#"
{
XID(93a4d4e7) [
'dereferenceVia': URI(https://resolver.example.com)
'key': PublicKeys(cab108a0, SigningPublicKey(93a4d4e7, SchnorrPublicKey(26712894)), EncapsulationPublicKey(00b42db3, X25519PublicKey(00b42db3))) [
{
'privateKey': ENCRYPTED [
'hasSecret': EncryptedKey(Argon2id)
]
} [
'salt': Salt
]
'allow': 'All'
'nickname': "Alice"
]
]
} [
'signed': Signature
]
"#}.trim()
).unwrap();
let result = run_cli(&["xid", "id", "--verify", "inception", &xid_doc]);
assert!(result.is_ok());
}
#[test]
fn test_xid_resolution_empty_list() {
bc_envelope::register_tags();
let xid_doc =
run_cli(&["xid", "new", "--nickname", "Alice", ALICE_PUBKEYS]).unwrap();
run_cli_expect(&["xid", "resolution", "count", &xid_doc], "0").unwrap();
run_cli_expect(&["xid", "resolution", "all", &xid_doc], "").unwrap();
}
#[test]
fn test_xid_resolution_preserved_after_other_operations() {
bc_envelope::register_tags();
let xid_doc =
run_cli(&["xid", "new", "--nickname", "Alice", ALICE_PUBKEYS]).unwrap();
let xid_doc = run_cli(&[
"xid",
"resolution",
"add",
"https://resolver.example.com",
&xid_doc,
])
.unwrap();
let xid_doc = run_cli(&[
"xid",
"key",
"add",
"--nickname",
"Device2",
BOB_PUBKEYS,
&xid_doc,
])
.unwrap();
#[rustfmt::skip]
run_cli_expect(
&["format", &xid_doc],
indoc! {r#"
XID(93a4d4e7) [
'dereferenceVia': URI(https://resolver.example.com)
'key': PublicKeys(cab108a0, SigningPublicKey(93a4d4e7, SchnorrPublicKey(26712894)), EncapsulationPublicKey(00b42db3, X25519PublicKey(00b42db3))) [
'allow': 'All'
'nickname': "Alice"
]
'key': PublicKeys(e2c18423, SigningPublicKey(f1199a75, SchnorrPublicKey(f0638394)), EncapsulationPublicKey(4af6be52, X25519PublicKey(4af6be52))) [
'allow': 'All'
'nickname': "Device2"
]
]
"#}.trim()
).unwrap();
run_cli_expect(&["xid", "resolution", "count", &xid_doc], "1").unwrap();
run_cli_expect(
&["xid", "resolution", "all", &xid_doc],
"https://resolver.example.com",
)
.unwrap();
}