address_test.mx raw

   1  package protocol
   2  
   3  import "testing"
   4  
   5  const (
   6  	trackPub  = "8be5c8c07b524a2d9d4e1c2f3a4b5c6d8be5c8c07b524a2d9d4e1c2f3a4b5c6d"
   7  	ownerPub  = "1111111111111111111111111111111111111111111111111111111111111111"
   8  	buyerPub  = "2222222222222222222222222222222222222222222222222222222222222222"
   9  	blobHash  = "aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899"
  10  )
  11  
  12  func TestParseAddress(t *testing.T) {
  13  	a, err := ParseAddress("32210:" | ownerPub | ":my-recording")
  14  	if err != nil {
  15  		t.Fatalf("parse: %s", err.Error())
  16  	}
  17  	if a.Kind != KindTrack {
  18  		t.Fatalf("kind = %d, want %d", a.Kind, KindTrack)
  19  	}
  20  	if a.Pubkey != ownerPub {
  21  		t.Fatalf("pubkey = %q", a.Pubkey)
  22  	}
  23  	if a.D != "my-recording" {
  24  		t.Fatalf("d = %q", a.D)
  25  	}
  26  	if a.String() != "32210:"|ownerPub|":my-recording" {
  27  		t.Fatalf("round trip = %q", a.String())
  28  	}
  29  }
  30  
  31  // The d part is opaque and may carry colons: an access filter's d is
  32  // "<tier>:<asset-address>" and the asset address is itself kind:pubkey:d.
  33  func TestParseAddressOpaqueD(t *testing.T) {
  34  	d := "fan:32210:" | ownerPub | ":my-recording"
  35  	a, err := ParseAddress("32217:" | ownerPub | ":" | d)
  36  	if err != nil {
  37  		t.Fatalf("parse: %s", err.Error())
  38  	}
  39  	if a.Kind != KindAccessFilter {
  40  		t.Fatalf("kind = %d, want %d", a.Kind, KindAccessFilter)
  41  	}
  42  	if a.D != d {
  43  		t.Fatalf("d = %q, want %q", a.D, d)
  44  	}
  45  	if a.String() != "32217:"|ownerPub|":"|d {
  46  		t.Fatalf("round trip = %q", a.String())
  47  	}
  48  }
  49  
  50  func TestParseAddressRejects(t *testing.T) {
  51  	cases := []string{
  52  		"",
  53  		"32210",
  54  		"32210:",
  55  		"32210:" | ownerPub,
  56  		"32210:" | ownerPub | ":",
  57  		"32x10:" | ownerPub | ":d",
  58  		"32210:" | "ABCDEF" | ":d",
  59  		"32210:" | "abcd" | ":d",
  60  		"99999999:" | ownerPub | ":d",
  61  	}
  62  	for _, c := range cases {
  63  		if _, err := ParseAddress(c); err == nil {
  64  			t.Errorf("ParseAddress(%q) accepted a bad address", c)
  65  		}
  66  	}
  67  }
  68  
  69  func TestAddressTag(t *testing.T) {
  70  	a := &Address{Kind: KindTrack, Pubkey: ownerPub, D: "d1"}
  71  	plain := a.Tag()
  72  	if len(plain) != 2 || plain[0] != TagA || plain[1] != "32210:"|ownerPub|":d1" {
  73  		t.Fatalf("tag without relay = %v", plain)
  74  	}
  75  	a.Relay = "wss://relay.musiquay.com"
  76  	hinted := a.Tag()
  77  	if len(hinted) != 3 || hinted[2] != "wss://relay.musiquay.com" {
  78  		t.Fatalf("tag with relay = %v", hinted)
  79  	}
  80  	back, err := AddressFromTag(hinted)
  81  	if err != nil {
  82  		t.Fatalf("AddressFromTag: %s", err.Error())
  83  	}
  84  	if back.String() != a.String() || back.Relay != a.Relay {
  85  		t.Fatalf("round trip = %v", back)
  86  	}
  87  }
  88  
  89  func TestIsHex64(t *testing.T) {
  90  	if !IsHex64(ownerPub) {
  91  		t.Fatal("a 64-character lowercase hex string was rejected")
  92  	}
  93  	for _, s := range []string{
  94  		"", "abc", ownerPub[:63], ownerPub | "0",
  95  		"111111111111111111111111111111111111111111111111111111111111111G",
  96  		"111111111111111111111111111111111111111111111111111111111111111A",
  97  	} {
  98  		if IsHex64(s) {
  99  			t.Errorf("IsHex64(%q) = true", s)
 100  		}
 101  	}
 102  }
 103  
 104  func TestIntStrAndParse(t *testing.T) {
 105  	cases := []int64{0, 1, -1, 9, 10, 100, 1758130000, -1758130000}
 106  	for _, v := range cases {
 107  		s := intStr(v)
 108  		back, err := ParseInt64(s)
 109  		if err != nil {
 110  			t.Fatalf("ParseInt64(%q): %s", s, err.Error())
 111  		}
 112  		if back != v {
 113  			t.Fatalf("round trip %d -> %q -> %d", v, s, back)
 114  		}
 115  	}
 116  	for _, s := range []string{"", "-", "+1", "1x", " 1"} {
 117  		if _, err := ParseInt64(s); err == nil {
 118  			t.Errorf("ParseInt64(%q) accepted a bad integer", s)
 119  		}
 120  	}
 121  }
 122  
 123  func TestUint16Str(t *testing.T) {
 124  	for _, v := range []uint16{0, 1, 9, 10, 32210, 65535} {
 125  		s := uint16Str(v)
 126  		back, err := parseUint16(s)
 127  		if err != nil {
 128  			t.Fatalf("parseUint16(%q): %s", s, err.Error())
 129  		}
 130  		if back != v {
 131  			t.Fatalf("round trip %d -> %q -> %d", v, s, back)
 132  		}
 133  	}
 134  }
 135