deviceid_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (C) 2014 The Protocol Authors.
  2. //go:generate go run ../../script/protofmt.go deviceid_test.proto
  3. //go:generate protoc -I ../../ -I . --gogofast_out=. deviceid_test.proto
  4. package protocol
  5. import "testing"
  6. var formatted = "P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2"
  7. var formatCases = []string{
  8. "P56IOI-7MZJNU-2IQGDR-EYDM2M-GTMGL3-BXNPQ6-W5BTBB-Z4TJXZ-WICQ",
  9. "P56IOI-7MZJNU2Y-IQGDR-EYDM2M-GTI-MGL3-BXNPQ6-W5BM-TBB-Z4TJXZ-WICQ2",
  10. "P56IOI7 MZJNU2I QGDREYD M2MGTMGL 3BXNPQ6W 5BTB BZ4T JXZWICQ",
  11. "P56IOI7 MZJNU2Y IQGDREY DM2MGTI MGL3BXN PQ6W5BM TBBZ4TJ XZWICQ2",
  12. "P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQ",
  13. "p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicq",
  14. "P56IOI7MZJNU2YIQGDREYDM2MGTIMGL3BXNPQ6W5BMTBBZ4TJXZWICQ2",
  15. "P561017MZJNU2YIQGDREYDM2MGTIMGL3BXNPQ6W5BMT88Z4TJXZWICQ2",
  16. "p56ioi7mzjnu2yiqgdreydm2mgtimgl3bxnpq6w5bmtbbz4tjxzwicq2",
  17. "p561017mzjnu2yiqgdreydm2mgtimgl3bxnpq6w5bmt88z4tjxzwicq2",
  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. if len(sid) != 7 {
  79. t.Errorf("Wrong length for short ID: got %d, want 7", len(sid))
  80. }
  81. want := formatted[:len(sid)]
  82. if sid != want {
  83. t.Errorf("Wrong short ID: got %q, want %q", sid, want)
  84. }
  85. }
  86. func TestDeviceIDFromBytes(t *testing.T) {
  87. id0, _ := DeviceIDFromString(formatted)
  88. id1 := DeviceIDFromBytes(id0[:])
  89. if id1.String() != formatted {
  90. t.Errorf("Wrong device ID, got %q, want %q", id1, formatted)
  91. }
  92. }
  93. func TestNewDeviceIDMarshalling(t *testing.T) {
  94. // The new DeviceID.Unmarshal / DeviceID.MarshalTo serialization should
  95. // be message compatible with how we used to serialize DeviceIDs.
  96. // Create a message with a device ID in old style bytes format
  97. id0, _ := DeviceIDFromString(formatted)
  98. msg0 := TestOldDeviceID{Test: id0[:]}
  99. // Marshal it
  100. bs, err := msg0.Marshal()
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. // Unmarshal using the new DeviceID.Unmarshal
  105. var msg1 TestNewDeviceID
  106. if err := msg1.Unmarshal(bs); err != nil {
  107. t.Fatal(err)
  108. }
  109. // Verify it's the same
  110. if msg1.Test != id0 {
  111. t.Error("Mismatch in old -> new direction")
  112. }
  113. // Marshal using the new DeviceID.MarshalTo
  114. bs, err = msg1.Marshal()
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. // Create an old style message and attempt unmarshal
  119. var msg2 TestOldDeviceID
  120. if err := msg2.Unmarshal(bs); err != nil {
  121. t.Fatal(err)
  122. }
  123. // Verify it's the same
  124. if DeviceIDFromBytes(msg2.Test) != id0 {
  125. t.Error("Mismatch in old -> new direction")
  126. }
  127. }
  128. var resStr string
  129. func BenchmarkLuhnify(b *testing.B) {
  130. str := "ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB"
  131. var err error
  132. for i := 0; i < b.N; i++ {
  133. resStr, err = luhnify(str)
  134. if err != nil {
  135. b.Fatal(err)
  136. }
  137. }
  138. }
  139. func BenchmarkUnluhnify(b *testing.B) {
  140. str, _ := luhnify("ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB")
  141. var err error
  142. for i := 0; i < b.N; i++ {
  143. resStr, err = unluhnify(str)
  144. if err != nil {
  145. b.Fatal(err)
  146. }
  147. }
  148. }
  149. func BenchmarkChunkify(b *testing.B) {
  150. str := "ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB"
  151. for i := 0; i < b.N; i++ {
  152. resStr = chunkify(str)
  153. }
  154. }
  155. func BenchmarkUnchunkify(b *testing.B) {
  156. str := chunkify("ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB")
  157. for i := 0; i < b.N; i++ {
  158. resStr = unchunkify(str)
  159. }
  160. }