package locator import ( "strings" "testing" "g.activething.com/go/DateKeys/codec" ) func small() *Locator { return &Locator{Addresses: []Address{{URI: "https://ejemplo.org/b"}}, EnvelopeHeader: []byte("h\n"), RestSize: 1} } // Review: a reader rejects an address that breaks the rules of ยง44.1, not the // locator, and uses the others; a writer never writes such an address. func TestUsableAddresses(t *testing.T) { l := small() l.Addresses = append([]Address{{URI: "http://ejemplo.org/a"}}, l.Addresses...) if _, err := l.Marshal(); err == nil { t.Error("a writer wrote an http address") } b, err := l.marshal() if err != nil { t.Fatal(err) } back, err := Unmarshal(b) if err != nil || len(back.Addresses) != 2 { t.Fatalf("a locator with a rejected address: %v", err) } if u := back.Usable(); len(u) != 1 || u[0].URI != "https://ejemplo.org/b" { t.Errorf("usable: %v", u) } } // Review: the plaintext measures the least multiple of 4096 that key 6 can // fill. A CBOR of 8192 bytes where 4096 suffice, valid in every other way, is // not canonical. func TestLeastMultiple(t *testing.T) { l := small() var base codec.Encoder l.encode(&base, -1) b0, err := base.Out() if err != nil { t.Fatal(err) } var e codec.Encoder l.encode(&e, 2*Block-len(b0)-1-3) b, err := e.Out() if err != nil || len(b) != 2*Block { t.Fatalf("%d bytes, %v", len(b), err) } if _, err := Unmarshal(b); err == nil || !strings.Contains(err.Error(), "least multiple") { t.Errorf("two blocks where one suffices: %v", err) } if want, err := l.Marshal(); err != nil || len(want) != Block { t.Errorf("%d bytes, %v", len(want), err) } } // Review: the bases where no length of key 6 completes a multiple are those // where the plaintext would need 0 to 2, 26 or 259 more bytes: key 6 takes at // least 3, and the header of its byte string grows at 24 and 256 bytes. func TestPaddingBoundaries(t *testing.T) { for base := 1; base <= 3*Block; base++ { got := PlaintextLength(base) need := (Block - base%Block) % Block want := base + need switch need { case 0: case 1, 2, 26, 259: want += Block } if got != want { t.Fatalf("base %d: %d bytes, want %d", base, got, want) } } }