github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/profile/id_test.go (about) 1 package profile 2 3 import ( 4 "bytes" 5 "fmt" 6 "testing" 7 8 "github.com/google/go-cmp/cmp" 9 testkeys "github.com/qri-io/qri/auth/key/test" 10 ) 11 12 const base64encoded = "QmRdexT18WuAKVX3vPusqmJTWLeNSeJgjmMbaF5QLGHna1" 13 14 func TestIDJSON(t *testing.T) { 15 idbytes, err := IDB58MustDecode(base64encoded).MarshalJSON() 16 if err != nil { 17 t.Error(err.Error()) 18 return 19 } 20 expect := []byte(`"QmRdexT18WuAKVX3vPusqmJTWLeNSeJgjmMbaF5QLGHna1"`) 21 if !bytes.Equal(idbytes, expect) { 22 t.Errorf("byte mistmatch. expected: %s, got: %s", string(expect), string(idbytes)) 23 } 24 } 25 26 func TestPeerID(t *testing.T) { 27 kd := testkeys.GetKeyData(0) 28 29 idStr := kd.EncodedPeerID 30 if idStr != "QmeL2mdVka1eahKENjehK6tBxkkpk5dNQ1qMcgWi7Hrb4B" { 31 t.Errorf("unexpected value for encoded peerID") 32 } 33 34 mistakenDecode := "9tmzz8FC9hjBrY1J9NFFt4gjAzGZWCGrKwB4pcdwuSHC7Y4Y7oPPAkrV48ryPYu" 35 36 badlyConstructedProfileID := ID(idStr) 37 if badlyConstructedProfileID.Encode() != mistakenDecode { 38 t.Errorf("unexpected value for encoded peerID, got %s", badlyConstructedProfileID) 39 } 40 41 wellformedProfileID0 := IDB58DecodeOrEmpty(idStr) 42 if wellformedProfileID0.Encode() != idStr { 43 t.Errorf("unexpected value for encoded peerID, got %s", wellformedProfileID0) 44 } 45 46 wellformedProfileID1 := IDFromPeerID(kd.PeerID) 47 if wellformedProfileID1.Encode() != idStr { 48 t.Errorf("unexpected value for encoded peerID, got %s", wellformedProfileID1) 49 } 50 51 wellformedProfileID2 := IDRawByteString(string(kd.PeerID)) 52 if wellformedProfileID2.Encode() != idStr { 53 t.Errorf("unexpected value for encoded peerID, got %s", wellformedProfileID2) 54 } 55 } 56 57 func TestStringifyVsEncode(t *testing.T) { 58 var pid ID 59 60 kd := testkeys.GetKeyData(0) 61 pid = IDFromPeerID(kd.PeerID) 62 63 actual := pid.String() 64 expect := `profile.ID{1220ed925aab9128c318acffebcc9a2b66876cebefb2511d2b1e3256f74ce96549a8}` 65 if diff := cmp.Diff(expect, actual); diff != "" { 66 t.Errorf("profileID.String() (-want +got):\n%s", diff) 67 } 68 69 actual = pid.Encode() 70 expect = kd.EncodedPeerID 71 if diff := cmp.Diff(expect, actual); diff != "" { 72 t.Errorf("profileID.String() (-want +got):\n%s", diff) 73 } 74 } 75 76 func TestEmpty(t *testing.T) { 77 var pid ID 78 if !pid.Empty() { 79 t.Fatal("expected: profileID.Empty should be true") 80 } 81 82 kd := testkeys.GetKeyData(0) 83 pid = IDFromPeerID(kd.PeerID) 84 if pid.Empty() { 85 t.Error("expected: profileID.Empty should be false") 86 } 87 } 88 89 func TestMarshalJSON(t *testing.T) { 90 var pid ID 91 92 kd := testkeys.GetKeyData(0) 93 pid = IDFromPeerID(kd.PeerID) 94 95 data, err := pid.MarshalJSON() 96 if err != nil { 97 t.Fatal(err) 98 } 99 actual := string(data) 100 expect := fmt.Sprintf("%q", kd.EncodedPeerID) 101 if diff := cmp.Diff(expect, actual); diff != "" { 102 t.Errorf("profileID.String() (-want +got):\n%s", diff) 103 } 104 } 105 106 func TestMarshalYAML(t *testing.T) { 107 var pid ID 108 109 kd := testkeys.GetKeyData(0) 110 pid = IDFromPeerID(kd.PeerID) 111 112 actual, err := pid.MarshalYAML() 113 if err != nil { 114 t.Fatal(err) 115 } 116 expect := kd.EncodedPeerID 117 if diff := cmp.Diff(expect, actual); diff != "" { 118 t.Errorf("profileID.String() (-want +got):\n%s", diff) 119 } 120 } 121 122 func TestValidate(t *testing.T) { 123 var pid ID 124 125 // profile.ID is empty, Validate will fail 126 err := pid.Validate() 127 if err == nil { 128 t.Fatal("error expected, did not get one") 129 } 130 expectErr := `empty peer ID` 131 if expectErr != err.Error() { 132 t.Errorf("error mismatch, expect: %s, got: %s", expectErr, err) 133 } 134 135 // profile.ID is invalid, Validate will fail 136 // NOTE: Do not call this function this way! Only used here to demonstrate 137 // what *not* to do 138 pid = IDRawByteString("bad") 139 err = pid.Validate() 140 if err == nil { 141 t.Fatal("error expected, did not get one") 142 } 143 expectErr = `profile.ID invalid, encodes to "a3c7"` 144 if expectErr != err.Error() { 145 t.Errorf("error mismatch, expect: %s, got: %s", expectErr, err) 146 } 147 148 // profile.ID is invalid due to double-encoding, Validate will fail 149 // NOTE: Do not call this function this way! Only used here to demonstrate 150 // what *not* to do 151 kd := testkeys.GetKeyData(0) 152 pid = IDRawByteString(kd.EncodedPeerID) 153 err = pid.Validate() 154 if err == nil { 155 t.Fatal("error expected, did not get one") 156 } 157 expectErr = `profile.ID invalid, was double encoded as "9tmzz8FC9hjBrY1J9NFFt4gjAzGZWCGrKwB4pcdwuSHC7Y4Y7oPPAkrV48ryPYu". do not pass a base64 encoded string, instead use IDB58Decode(b64encodedID)` 158 if expectErr != err.Error() { 159 t.Errorf("error mismatch, expect: %s, got: %s", expectErr, err) 160 } 161 162 // profile.ID is correctly decoded, Validate will not return an error 163 pid, err = IDB58Decode(kd.EncodedPeerID) 164 if err != nil { 165 t.Fatalf("Decode got error: %s", err) 166 } 167 err = pid.Validate() 168 if err != nil { 169 t.Errorf("Validate() got error: %s", err) 170 } 171 172 // profile.ID is correctly coerced from peer.ID, Validate will not return an error 173 pid = IDFromPeerID(kd.PeerID) 174 err = pid.Validate() 175 if err != nil { 176 t.Errorf("Validate() got error: %s", err) 177 } 178 }