deviceid_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (C) 2014 The Protocol Authors.
  2. //go:generate go run ../../script/protofmt.go deviceid_test.proto
  3. //go:generate protoc -I ../../../../../ -I ../../../../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. {"", false},
  35. {"P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2", true},
  36. {"P56IOI7-MZJNU2-IQGDREY-DM2MGT-MGL3BXN-PQ6W5B-TBBZ4TJ-XZWICQ", true},
  37. {"P56IOI7 MZJNU2I QGDREYD M2MGTMGL 3BXNPQ6W 5BTB BZ4T JXZWICQ", true},
  38. {"P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQ", true},
  39. {"P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQCCCC", false},
  40. {"p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicq", true},
  41. {"p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicqCCCC", false},
  42. }
  43. func TestValidateDeviceID(t *testing.T) {
  44. for _, tc := range validateCases {
  45. var id DeviceID
  46. err := id.UnmarshalText([]byte(tc.s))
  47. if (err == nil && !tc.ok) || (err != nil && tc.ok) {
  48. t.Errorf("ValidateDeviceID(%q); %v != %v", tc.s, err, tc.ok)
  49. }
  50. }
  51. }
  52. func TestMarshallingDeviceID(t *testing.T) {
  53. 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}
  54. n1 := DeviceID{}
  55. n2 := DeviceID{}
  56. bs, _ := n0.MarshalText()
  57. n1.UnmarshalText(bs)
  58. bs, _ = n1.MarshalText()
  59. n2.UnmarshalText(bs)
  60. if n2.String() != n0.String() {
  61. t.Errorf("String marshalling error; %q != %q", n2.String(), n0.String())
  62. }
  63. if !n2.Equals(n0) {
  64. t.Error("Equals error")
  65. }
  66. if n2.Compare(n0) != 0 {
  67. t.Error("Compare error")
  68. }
  69. }
  70. func TestShortIDString(t *testing.T) {
  71. id, _ := DeviceIDFromString(formatted)
  72. sid := id.Short().String()
  73. if len(sid) != 7 {
  74. t.Errorf("Wrong length for short ID: got %d, want 7", len(sid))
  75. }
  76. want := formatted[:len(sid)]
  77. if sid != want {
  78. t.Errorf("Wrong short ID: got %q, want %q", sid, want)
  79. }
  80. }
  81. func TestDeviceIDFromBytes(t *testing.T) {
  82. id0, _ := DeviceIDFromString(formatted)
  83. id1 := DeviceIDFromBytes(id0[:])
  84. if id1.String() != formatted {
  85. t.Errorf("Wrong device ID, got %q, want %q", id1, formatted)
  86. }
  87. }
  88. func TestNewDeviceIDMarshalling(t *testing.T) {
  89. // The new DeviceID.Unmarshal / DeviceID.MarshalTo serialization should
  90. // be message compatible with how we used to serialize DeviceIDs.
  91. // Create a message with a device ID in old style bytes format
  92. id0, _ := DeviceIDFromString(formatted)
  93. msg0 := TestOldDeviceID{id0[:]}
  94. // Marshal it
  95. bs, err := msg0.Marshal()
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. // Unmarshal using the new DeviceID.Unmarshal
  100. var msg1 TestNewDeviceID
  101. if err := msg1.Unmarshal(bs); err != nil {
  102. t.Fatal(err)
  103. }
  104. // Verify it's the same
  105. if msg1.Test != id0 {
  106. t.Error("Mismatch in old -> new direction")
  107. }
  108. // Marshal using the new DeviceID.MarshalTo
  109. bs, err = msg1.Marshal()
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. // Create an old style message and and attempt unmarshal
  114. var msg2 TestOldDeviceID
  115. if err := msg2.Unmarshal(bs); err != nil {
  116. t.Fatal(err)
  117. }
  118. // Verify it's the same
  119. if DeviceIDFromBytes(msg2.Test) != id0 {
  120. t.Error("Mismatch in old -> new direction")
  121. }
  122. }