deviceid_test.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package protocol
  16. import "testing"
  17. var formatted = "P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2"
  18. var formatCases = []string{
  19. "P56IOI-7MZJNU-2IQGDR-EYDM2M-GTMGL3-BXNPQ6-W5BTBB-Z4TJXZ-WICQ",
  20. "P56IOI-7MZJNU2Y-IQGDR-EYDM2M-GTI-MGL3-BXNPQ6-W5BM-TBB-Z4TJXZ-WICQ2",
  21. "P56IOI7 MZJNU2I QGDREYD M2MGTMGL 3BXNPQ6W 5BTB BZ4T JXZWICQ",
  22. "P56IOI7 MZJNU2Y IQGDREY DM2MGTI MGL3BXN PQ6W5BM TBBZ4TJ XZWICQ2",
  23. "P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQ",
  24. "p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicq",
  25. "P56IOI7MZJNU2YIQGDREYDM2MGTIMGL3BXNPQ6W5BMTBBZ4TJXZWICQ2",
  26. "P561017MZJNU2YIQGDREYDM2MGTIMGL3BXNPQ6W5BMT88Z4TJXZWICQ2",
  27. "p56ioi7mzjnu2yiqgdreydm2mgtimgl3bxnpq6w5bmtbbz4tjxzwicq2",
  28. "p561017mzjnu2yiqgdreydm2mgtimgl3bxnpq6w5bmt88z4tjxzwicq2",
  29. }
  30. func TestFormatDeviceID(t *testing.T) {
  31. for i, tc := range formatCases {
  32. var id DeviceID
  33. err := id.UnmarshalText([]byte(tc))
  34. if err != nil {
  35. t.Errorf("#%d UnmarshalText(%q); %v", i, tc, err)
  36. } else if f := id.String(); f != formatted {
  37. t.Errorf("#%d FormatDeviceID(%q)\n\t%q !=\n\t%q", i, tc, f, formatted)
  38. }
  39. }
  40. }
  41. var validateCases = []struct {
  42. s string
  43. ok bool
  44. }{
  45. {"", false},
  46. {"P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2", true},
  47. {"P56IOI7-MZJNU2-IQGDREY-DM2MGT-MGL3BXN-PQ6W5B-TBBZ4TJ-XZWICQ", true},
  48. {"P56IOI7 MZJNU2I QGDREYD M2MGTMGL 3BXNPQ6W 5BTB BZ4T JXZWICQ", true},
  49. {"P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQ", true},
  50. {"P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQCCCC", false},
  51. {"p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicq", true},
  52. {"p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicqCCCC", false},
  53. }
  54. func TestValidateDeviceID(t *testing.T) {
  55. for _, tc := range validateCases {
  56. var id DeviceID
  57. err := id.UnmarshalText([]byte(tc.s))
  58. if (err == nil && !tc.ok) || (err != nil && tc.ok) {
  59. t.Errorf("ValidateDeviceID(%q); %v != %v", tc.s, err, tc.ok)
  60. }
  61. }
  62. }
  63. func TestMarshallingDeviceID(t *testing.T) {
  64. 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}
  65. n1 := DeviceID{}
  66. n2 := DeviceID{}
  67. bs, _ := n0.MarshalText()
  68. n1.UnmarshalText(bs)
  69. bs, _ = n1.MarshalText()
  70. n2.UnmarshalText(bs)
  71. if n2.String() != n0.String() {
  72. t.Errorf("String marshalling error; %q != %q", n2.String(), n0.String())
  73. }
  74. if !n2.Equals(n0) {
  75. t.Error("Equals error")
  76. }
  77. if n2.Compare(n0) != 0 {
  78. t.Error("Compare error")
  79. }
  80. }