deviceid_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright (C) 2014 The Protocol Authors.
  2. package protocol
  3. import "testing"
  4. var (
  5. formatted = "P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2"
  6. formatCases = []string{
  7. "P56IOI-7MZJNU-2IQGDR-EYDM2M-GTMGL3-BXNPQ6-W5BTBB-Z4TJXZ-WICQ",
  8. "P56IOI-7MZJNU2Y-IQGDR-EYDM2M-GTI-MGL3-BXNPQ6-W5BM-TBB-Z4TJXZ-WICQ2",
  9. "P56IOI7 MZJNU2I QGDREYD M2MGTMGL 3BXNPQ6W 5BTB BZ4T JXZWICQ",
  10. "P56IOI7 MZJNU2Y IQGDREY DM2MGTI MGL3BXN PQ6W5BM TBBZ4TJ XZWICQ2",
  11. "P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQ",
  12. "p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicq",
  13. "P56IOI7MZJNU2YIQGDREYDM2MGTIMGL3BXNPQ6W5BMTBBZ4TJXZWICQ2",
  14. "P561017MZJNU2YIQGDREYDM2MGTIMGL3BXNPQ6W5BMT88Z4TJXZWICQ2",
  15. "p56ioi7mzjnu2yiqgdreydm2mgtimgl3bxnpq6w5bmtbbz4tjxzwicq2",
  16. "p561017mzjnu2yiqgdreydm2mgtimgl3bxnpq6w5bmt88z4tjxzwicq2",
  17. }
  18. )
  19. func TestFormatDeviceID(t *testing.T) {
  20. for i, tc := range formatCases {
  21. var id DeviceID
  22. err := id.UnmarshalText([]byte(tc))
  23. if err != nil {
  24. t.Errorf("#%d UnmarshalText(%q); %v", i, tc, err)
  25. } else if f := id.String(); f != formatted {
  26. t.Errorf("#%d FormatDeviceID(%q)\n\t%q !=\n\t%q", i, tc, f, formatted)
  27. }
  28. }
  29. }
  30. var validateCases = []struct {
  31. s string
  32. ok bool
  33. }{
  34. {"", true}, // Empty device ID, all zeroes
  35. {"a", false},
  36. {"P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2", true},
  37. {"P56IOI7-MZJNU2-IQGDREY-DM2MGT-MGL3BXN-PQ6W5B-TBBZ4TJ-XZWICQ", true},
  38. {"P56IOI7 MZJNU2I QGDREYD M2MGTMGL 3BXNPQ6W 5BTB BZ4T JXZWICQ", true},
  39. {"P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQ", true},
  40. {"P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQCCCC", false},
  41. {"p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicq", true},
  42. {"p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicqCCCC", false},
  43. }
  44. func TestValidateDeviceID(t *testing.T) {
  45. for _, tc := range validateCases {
  46. var id DeviceID
  47. err := id.UnmarshalText([]byte(tc.s))
  48. if (err == nil && !tc.ok) || (err != nil && tc.ok) {
  49. t.Errorf("ValidateDeviceID(%q); %v != %v", tc.s, err, tc.ok)
  50. }
  51. }
  52. }
  53. func TestMarshallingDeviceID(t *testing.T) {
  54. n0 := DeviceID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 10, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
  55. n1 := DeviceID{}
  56. n2 := DeviceID{}
  57. bs, _ := n0.MarshalText()
  58. if err := n1.UnmarshalText(bs); err != nil {
  59. t.Fatal(err)
  60. }
  61. bs, _ = n1.MarshalText()
  62. if err := n2.UnmarshalText(bs); err != nil {
  63. t.Fatal(err)
  64. }
  65. if n2.String() != n0.String() {
  66. t.Errorf("String marshalling error; %q != %q", n2.String(), n0.String())
  67. }
  68. if !n2.Equals(n0) {
  69. t.Error("Equals error")
  70. }
  71. if n2.Compare(n0) != 0 {
  72. t.Error("Compare error")
  73. }
  74. }
  75. func TestShortIDString(t *testing.T) {
  76. id, _ := DeviceIDFromString(formatted)
  77. sid := id.Short().String()
  78. // keep consistent with ShortIDStringLength in lib/protocol/deviceid.go
  79. if len(sid) != 7 {
  80. t.Errorf("Wrong length for short ID: got %d, want 7", len(sid))
  81. }
  82. want := formatted[:len(sid)]
  83. if sid != want {
  84. t.Errorf("Wrong short ID: got %q, want %q", sid, want)
  85. }
  86. }
  87. func TestDeviceIDFromBytes(t *testing.T) {
  88. id0, _ := DeviceIDFromString(formatted)
  89. id1, err := DeviceIDFromBytes(id0[:])
  90. if err != nil {
  91. t.Fatal(err)
  92. } else if id1.String() != formatted {
  93. t.Errorf("Wrong device ID, got %q, want %q", id1, formatted)
  94. }
  95. }
  96. func TestNewDeviceIDMarshalling(t *testing.T) {
  97. // The new DeviceID.Unmarshal / DeviceID.MarshalTo serialization should
  98. // be message compatible with how we used to serialize DeviceIDs.
  99. // Create a message with a device ID in old style bytes format
  100. id0, _ := DeviceIDFromString(formatted)
  101. msg0 := TestOldDeviceID{Test: id0[:]}
  102. // Marshal it
  103. bs, err := msg0.Marshal()
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. // Unmarshal using the new DeviceID.Unmarshal
  108. var msg1 TestNewDeviceID
  109. if err := msg1.Unmarshal(bs); err != nil {
  110. t.Fatal(err)
  111. }
  112. // Verify it's the same
  113. if msg1.Test != id0 {
  114. t.Error("Mismatch in old -> new direction")
  115. }
  116. // Marshal using the new DeviceID.MarshalTo
  117. bs, err = msg1.Marshal()
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. // Create an old style message and attempt unmarshal
  122. var msg2 TestOldDeviceID
  123. if err := msg2.Unmarshal(bs); err != nil {
  124. t.Fatal(err)
  125. }
  126. // Verify it's the same
  127. id1, err := DeviceIDFromBytes(msg2.Test)
  128. if err != nil {
  129. t.Fatal(err)
  130. } else if id1 != id0 {
  131. t.Error("Mismatch in old -> new direction")
  132. }
  133. }
  134. var resStr string
  135. func BenchmarkLuhnify(b *testing.B) {
  136. str := "ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB"
  137. var err error
  138. for i := 0; i < b.N; i++ {
  139. resStr, err = luhnify(str)
  140. if err != nil {
  141. b.Fatal(err)
  142. }
  143. }
  144. }
  145. func BenchmarkUnluhnify(b *testing.B) {
  146. str, _ := luhnify("ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB")
  147. var err error
  148. for i := 0; i < b.N; i++ {
  149. resStr, err = unluhnify(str)
  150. if err != nil {
  151. b.Fatal(err)
  152. }
  153. }
  154. }
  155. func BenchmarkChunkify(b *testing.B) {
  156. str := "ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB"
  157. for i := 0; i < b.N; i++ {
  158. resStr = chunkify(str)
  159. }
  160. }
  161. func BenchmarkUnchunkify(b *testing.B) {
  162. str := chunkify("ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB")
  163. for i := 0; i < b.N; i++ {
  164. resStr = unchunkify(str)
  165. }
  166. }