use super::*;
mod store;
const DEVICE: &str = "-----BEGIN PUBLIC KEY-----\n\
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENflj+nLA7lsFuxVfqaryK84n9wP4\n\
3JqG0mf0zqZecVFyPJ6ZX6m9IVK9g0IKk4hdMBbVTWXZvaIRcR4UDpgzCg==\n\
-----END PUBLIC KEY-----\n";
const CA: &str = "-----BEGIN CERTIFICATE-----\n\
MIIBdzCCAR2gAwIBAgIUS6jy88xKuMgm0widLw6ejqRjV2EwCgYIKoZIzj0EAwIw\n\
ETEPMA0GA1UEAwwGeW9nLWNhMB4XDTI2MTExMTE5MTgyOVoXDTI5MDIxMzE5MTgy\n\
OVowETEPMA0GA1UEAwwGeW9nLWNhMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE\n\
a2lFtf+Bdi55ENgocMU/VFmRRnrTvKFmGo1Jc/jkpoLCcUiuY+oHUyxbLu9bjGPN\n\
KTzzSgWlmGrNv8lTlWTamKNTMFEwHQYDVR0OBBYEFDuf4kFm3Ug1PoxXdM4G1sUD\n\
nSeYMB8GA1UdIwQYMBaAFDuf4kFm3Ug1PoxXdM4G1sUDnSeYMA8GA1UdEwEB/wQF\n\
MAMBAf8wCgYIKoZIzj0EAwIDSAAwRQIhAIYwai5c3FwmIffCooBMLoSYb8A+40Id\n\
n1/Fr3EXpRUyAiB/98dHbS9USqSz9WaFPMWG6lVZgiWI3ZGtfzdB30JEmA==\n\
-----END CERTIFICATE-----\n";
#[test]
fn the_confirmation_word_is_pinned_by_a_vector() {
use rustls::pki_types::pem::PemObject;
use rustls::pki_types::{CertificateDer, SubjectPublicKeyInfoDer};
let public = SubjectPublicKeyInfoDer::from_pem_slice(DEVICE.as_bytes()).expect("public key");
let ca = CertificateDer::from_pem_slice(CA.as_bytes()).expect("certificate");
assert_eq!((public.len(), ca.len()), (91, 379));
let digest = ring::digest::digest(&ring::digest::SHA256, &[&public[..], &ca[..]].concat());
assert_eq!(
digest.as_ref().get(..4),
Some(&[0xc2, 0x8b, 0x19, 0x3c][..]),
"the digest's head, which is all the word reads"
);
assert_eq!(word(&public, &ca), "2LFTUR");
assert_ne!(word(&ca, &public), "2LFTUR");
}
#[test]
fn the_alphabet_has_no_symbol_a_reader_confuses() {
assert_eq!(ALPHABET.len(), 32);
for confusable in b"IO01" {
assert!(!ALPHABET.contains(confusable));
}
assert_eq!(symbol(0), 'A');
assert_eq!(symbol(31), '9');
assert_eq!(symbol(32), 'A', "only the low five bits are read");
}
#[test]
fn a_code_is_eight_symbols_from_the_random_source() {
let (one, two) = (code().expect("code"), code().expect("code"));
assert_eq!(one.len(), CODE_LEN);
assert!(one.bytes().all(|b| ALPHABET.contains(&b)), "{one}");
assert_ne!(one, two, "forty bits do not repeat by accident");
}
#[test]
fn a_state_is_its_word_both_ways() {
for state in [State::Open, State::Claimed, State::Proved] {
assert_eq!(State::of(state.word()), Some(state));
}
assert_eq!(State::of("pending"), None);
}
#[test]
fn a_malformed_offers_row_refuses_by_name() {
use serde_json::json;
let read = |rows: serde_json::Value| {
let reply = json!({ "ok": true, "kind": "offers", "rows": rows });
wire::answer_of("offers", reply.as_object().expect("object")).expect("this family's")
};
let unknown = read(json!([{ "name": "phone", "grade": "operator",
"state": "pending", "expires": 1 }]));
assert!(unknown.expect_err("refused").contains("pending"));
assert!(read(json!(["phone"])).expect_err("refused").contains("row"));
assert!(wire::answer_of("enrolled", &serde_json::Map::new()).is_none());
}