package protocol import "testing" const ( trackPub = "8be5c8c07b524a2d9d4e1c2f3a4b5c6d8be5c8c07b524a2d9d4e1c2f3a4b5c6d" ownerPub = "1111111111111111111111111111111111111111111111111111111111111111" buyerPub = "2222222222222222222222222222222222222222222222222222222222222222" blobHash = "aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899" ) func TestParseAddress(t *testing.T) { a, err := ParseAddress("32210:" | ownerPub | ":my-recording") if err != nil { t.Fatalf("parse: %s", err.Error()) } if a.Kind != KindTrack { t.Fatalf("kind = %d, want %d", a.Kind, KindTrack) } if a.Pubkey != ownerPub { t.Fatalf("pubkey = %q", a.Pubkey) } if a.D != "my-recording" { t.Fatalf("d = %q", a.D) } if a.String() != "32210:"|ownerPub|":my-recording" { t.Fatalf("round trip = %q", a.String()) } } // The d part is opaque and may carry colons: an access filter's d is // ":" and the asset address is itself kind:pubkey:d. func TestParseAddressOpaqueD(t *testing.T) { d := "fan:32210:" | ownerPub | ":my-recording" a, err := ParseAddress("32217:" | ownerPub | ":" | d) if err != nil { t.Fatalf("parse: %s", err.Error()) } if a.Kind != KindAccessFilter { t.Fatalf("kind = %d, want %d", a.Kind, KindAccessFilter) } if a.D != d { t.Fatalf("d = %q, want %q", a.D, d) } if a.String() != "32217:"|ownerPub|":"|d { t.Fatalf("round trip = %q", a.String()) } } func TestParseAddressRejects(t *testing.T) { cases := []string{ "", "32210", "32210:", "32210:" | ownerPub, "32210:" | ownerPub | ":", "32x10:" | ownerPub | ":d", "32210:" | "ABCDEF" | ":d", "32210:" | "abcd" | ":d", "99999999:" | ownerPub | ":d", } for _, c := range cases { if _, err := ParseAddress(c); err == nil { t.Errorf("ParseAddress(%q) accepted a bad address", c) } } } func TestAddressTag(t *testing.T) { a := &Address{Kind: KindTrack, Pubkey: ownerPub, D: "d1"} plain := a.Tag() if len(plain) != 2 || plain[0] != TagA || plain[1] != "32210:"|ownerPub|":d1" { t.Fatalf("tag without relay = %v", plain) } a.Relay = "wss://relay.musiquay.com" hinted := a.Tag() if len(hinted) != 3 || hinted[2] != "wss://relay.musiquay.com" { t.Fatalf("tag with relay = %v", hinted) } back, err := AddressFromTag(hinted) if err != nil { t.Fatalf("AddressFromTag: %s", err.Error()) } if back.String() != a.String() || back.Relay != a.Relay { t.Fatalf("round trip = %v", back) } } func TestIsHex64(t *testing.T) { if !IsHex64(ownerPub) { t.Fatal("a 64-character lowercase hex string was rejected") } for _, s := range []string{ "", "abc", ownerPub[:63], ownerPub | "0", "111111111111111111111111111111111111111111111111111111111111111G", "111111111111111111111111111111111111111111111111111111111111111A", } { if IsHex64(s) { t.Errorf("IsHex64(%q) = true", s) } } } func TestIntStrAndParse(t *testing.T) { cases := []int64{0, 1, -1, 9, 10, 100, 1758130000, -1758130000} for _, v := range cases { s := intStr(v) back, err := ParseInt64(s) if err != nil { t.Fatalf("ParseInt64(%q): %s", s, err.Error()) } if back != v { t.Fatalf("round trip %d -> %q -> %d", v, s, back) } } for _, s := range []string{"", "-", "+1", "1x", " 1"} { if _, err := ParseInt64(s); err == nil { t.Errorf("ParseInt64(%q) accepted a bad integer", s) } } } func TestUint16Str(t *testing.T) { for _, v := range []uint16{0, 1, 9, 10, 32210, 65535} { s := uint16Str(v) back, err := parseUint16(s) if err != nil { t.Fatalf("parseUint16(%q): %s", s, err.Error()) } if back != v { t.Fatalf("round trip %d -> %q -> %d", v, s, back) } } }