func TestKeyBytes(t *testing.T) { buf := bytes.Repeat([]byte("borketyBorkBORK!"), 4) k := cas.NewKey(buf) if g, e := k.Bytes(), buf; !bytes.Equal(g, e) { t.Errorf("unexpected key data: %q %x", g, e) } }
func TestKeyNewPrivateNum(t *testing.T) { k := cas.NewKeyPrivateNum(31337) buf := k.Bytes() k2 := cas.NewKey(buf) if g, e := k2, cas.Invalid; g != e { t.Errorf("expected NewKey to give Invalid: %v", g) } k3 := cas.NewKeyPrivate(buf) if g, e := k3, k; g != e { t.Errorf("expected NewKeyPrivate to give original key: %v", g) } priv, ok := k3.Private() if !ok { t.Fatalf("expected Private to work: %v %v", priv, ok) } if g, e := priv, uint64(31337); g != e { t.Errorf("expected Private to match original: %v", g) } if g, e := k.IsSpecial(), true; g != e { t.Errorf("not Special: %v != %v", g, e) } if g, e := k.IsPrivate(), true; g != e { t.Errorf("not Private: %v != %v", g, e) } if g, e := k.IsReserved(), false; g != e { t.Errorf("bad Reserved: %v != %v", g, e) } }
func makeKey(keybuf []byte) cas.Key { key := cas.NewKey(keybuf) if key.IsSpecial() { // Tough luck, we happened to hash one of the reserved byte // sequences. You should play lotto today. copy(keybuf, replaceSpecial) key = cas.NewKey(keybuf) if key.IsSpecial() { // This has to mean replaceSpecial was poorly chosen, and // does not avoid the special prefixes. panic(fmt.Errorf("replaceSpecial is still Special: %x", keybuf)) } } return key }
func TestKeyMarshalBinary(t *testing.T) { buf := bytes.Repeat([]byte("borketyBorkBORK!"), 4) k := cas.NewKey(buf) out, err := k.MarshalBinary() if err != nil { t.Fatal(err) } if g, e := string(out), string(buf); g != e { t.Errorf("unexpected marshaled data: %q != %q", g, e) } }
func TestNotFoundErrorDispay(t *testing.T) { k := make([]byte, cas.KeySize) copy(k, "\x01evil\xFF") e := cas.NotFoundError{ Type: "blob", Level: 4, Key: cas.NewKey(k), } got := e.Error() if got != `Not found: "blob"@4 016576696cff00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000` { t.Errorf("bad error message: %q", got) } }
func TestKeyMarshalTo(t *testing.T) { buf := bytes.Repeat([]byte("borketyBorkBORK!"), 4) k := cas.NewKey(buf) out := make([]byte, 100) n, err := k.MarshalTo(out) if err != nil { t.Fatal(err) } if g, e := n, cas.KeySize; g != e { t.Errorf("unexpected MarshalTo size: %q %x", g, e) } if g, e := string(out[:n]), string(buf); g != e { t.Errorf("unexpected MarshalTo data: %q != %q", g, e) } }
func TestKeySimple(t *testing.T) { buf := bytes.Repeat([]byte("borketyBorkBORK!"), 4) k := cas.NewKey(buf) if g, e := k.String(), hex.EncodeToString(buf); g != e { t.Errorf("bad key: %q != %q", g, e) } if g, e := k.IsSpecial(), false; g != e { t.Errorf("bad Special: %v != %v", g, e) } if g, e := k.IsPrivate(), false; g != e { t.Errorf("bad Private: %v != %v", g, e) } if g, e := k.IsReserved(), false; g != e { t.Errorf("bad Reserved: %v != %v", g, e) } }
func TestKeyInvalid(t *testing.T) { buf := make([]byte, cas.KeySize) buf[len(buf)-1] = 0x42 k := cas.NewKey(buf) if g, e := k, cas.Invalid; g != e { t.Errorf("not Invalid: %q != %q", g, e) } if g, e := k.IsSpecial(), true; g != e { t.Errorf("not Special: %v != %v", g, e) } if g, e := k.IsPrivate(), false; g != e { t.Errorf("bad Private: %v != %v", g, e) } if g, e := k.IsReserved(), true; g != e { t.Errorf("not Reserved: %v != %v", g, e) } }
func TestKeyBadSize(t *testing.T) { buf := []byte("tooshort") defer func() { x := recover() switch i := x.(type) { case nil: t.Error("expected panic") case cas.BadKeySizeError: if g, e := i.Error(), "Key is bad length 8: 746f6f73686f7274"; g != e { t.Errorf("bad error message: %q != %q", g, e) } default: t.Errorf("expected BadKeySize: %v", x) } }() _ = cas.NewKey(buf) }
func TestKeyEmpty(t *testing.T) { buf := make([]byte, cas.KeySize) k := cas.NewKey(buf) if g, e := k, cas.Empty; g != e { t.Errorf("not Empty: %q != %q", g, e) } if g, e := k.String(), strings.Repeat("00", cas.KeySize); g != e { t.Errorf("bad key: %q != %q", g, e) } if g, e := k.IsSpecial(), true; g != e { t.Errorf("not Special: %v != %v", g, e) } if g, e := k.IsPrivate(), false; g != e { t.Errorf("bad Private: %v != %v", g, e) } if g, e := k.IsReserved(), false; g != e { t.Errorf("bad Reserved: %v != %v", g, e) } }