deviceid_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (C) 2014 The Protocol Authors.
  2. //go:generate go run ../../script/protofmt.go deviceid_test.proto
  3. //go:generate protoc -I ../../vendor/ -I ../../vendor/github.com/gogo/protobuf/protobuf -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. n1.UnmarshalText(bs)
  59. bs, _ = n1.MarshalText()
  60. n2.UnmarshalText(bs)
  61. if n2.String() != n0.String() {
  62. t.Errorf("String marshalling error; %q != %q", n2.String(), n0.String())
  63. }
  64. if !n2.Equals(n0) {
  65. t.Error("Equals error")
  66. }
  67. if n2.Compare(n0) != 0 {
  68. t.Error("Compare error")
  69. }
  70. }
  71. func TestShortIDString(t *testing.T) {
  72. id, _ := DeviceIDFromString(formatted)
  73. sid := id.Short().String()
  74. if len(sid) != 7 {
  75. t.Errorf("Wrong length for short ID: got %d, want 7", len(sid))
  76. }
  77. want := formatted[:len(sid)]
  78. if sid != want {
  79. t.Errorf("Wrong short ID: got %q, want %q", sid, want)
  80. }
  81. }
  82. func TestDeviceIDFromBytes(t *testing.T) {
  83. id0, _ := DeviceIDFromString(formatted)
  84. id1 := DeviceIDFromBytes(id0[:])
  85. if id1.String() != formatted {
  86. t.Errorf("Wrong device ID, got %q, want %q", id1, formatted)
  87. }
  88. }
  89. func TestNewDeviceIDMarshalling(t *testing.T) {
  90. // The new DeviceID.Unmarshal / DeviceID.MarshalTo serialization should
  91. // be message compatible with how we used to serialize DeviceIDs.
  92. // Create a message with a device ID in old style bytes format
  93. id0, _ := DeviceIDFromString(formatted)
  94. msg0 := TestOldDeviceID{id0[:]}
  95. // Marshal it
  96. bs, err := msg0.Marshal()
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. // Unmarshal using the new DeviceID.Unmarshal
  101. var msg1 TestNewDeviceID
  102. if err := msg1.Unmarshal(bs); err != nil {
  103. t.Fatal(err)
  104. }
  105. // Verify it's the same
  106. if msg1.Test != id0 {
  107. t.Error("Mismatch in old -> new direction")
  108. }
  109. // Marshal using the new DeviceID.MarshalTo
  110. bs, err = msg1.Marshal()
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. // Create an old style message and attempt unmarshal
  115. var msg2 TestOldDeviceID
  116. if err := msg2.Unmarshal(bs); err != nil {
  117. t.Fatal(err)
  118. }
  119. // Verify it's the same
  120. if DeviceIDFromBytes(msg2.Test) != id0 {
  121. t.Error("Mismatch in old -> new direction")
  122. }
  123. }
  124. var resStr string
  125. func BenchmarkLuhnify(b *testing.B) {
  126. str := "ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB"
  127. var err error
  128. for i := 0; i < b.N; i++ {
  129. resStr, err = luhnify(str)
  130. if err != nil {
  131. b.Fatal(err)
  132. }
  133. }
  134. }
  135. func BenchmarkUnluhnify(b *testing.B) {
  136. str, _ := luhnify("ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB")
  137. var err error
  138. for i := 0; i < b.N; i++ {
  139. resStr, err = unluhnify(str)
  140. if err != nil {
  141. b.Fatal(err)
  142. }
  143. }
  144. }
  145. func BenchmarkChunkify(b *testing.B) {
  146. str := "ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB"
  147. for i := 0; i < b.N; i++ {
  148. resStr = chunkify(str)
  149. }
  150. }
  151. func BenchmarkUnchunkify(b *testing.B) {
  152. str := chunkify("ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB")
  153. for i := 0; i < b.N; i++ {
  154. resStr = unchunkify(str)
  155. }
  156. }