deviceid_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (C) 2014 The Protocol Authors.
  2. package protocol
  3. import "testing"
  4. var formatted = "P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2"
  5. var formatCases = []string{
  6. "P56IOI-7MZJNU-2IQGDR-EYDM2M-GTMGL3-BXNPQ6-W5BTBB-Z4TJXZ-WICQ",
  7. "P56IOI-7MZJNU2Y-IQGDR-EYDM2M-GTI-MGL3-BXNPQ6-W5BM-TBB-Z4TJXZ-WICQ2",
  8. "P56IOI7 MZJNU2I QGDREYD M2MGTMGL 3BXNPQ6W 5BTB BZ4T JXZWICQ",
  9. "P56IOI7 MZJNU2Y IQGDREY DM2MGTI MGL3BXN PQ6W5BM TBBZ4TJ XZWICQ2",
  10. "P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQ",
  11. "p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicq",
  12. "P56IOI7MZJNU2YIQGDREYDM2MGTIMGL3BXNPQ6W5BMTBBZ4TJXZWICQ2",
  13. "P561017MZJNU2YIQGDREYDM2MGTIMGL3BXNPQ6W5BMT88Z4TJXZWICQ2",
  14. "p56ioi7mzjnu2yiqgdreydm2mgtimgl3bxnpq6w5bmtbbz4tjxzwicq2",
  15. "p561017mzjnu2yiqgdreydm2mgtimgl3bxnpq6w5bmt88z4tjxzwicq2",
  16. }
  17. func TestFormatDeviceID(t *testing.T) {
  18. for i, tc := range formatCases {
  19. var id DeviceID
  20. err := id.UnmarshalText([]byte(tc))
  21. if err != nil {
  22. t.Errorf("#%d UnmarshalText(%q); %v", i, tc, err)
  23. } else if f := id.String(); f != formatted {
  24. t.Errorf("#%d FormatDeviceID(%q)\n\t%q !=\n\t%q", i, tc, f, formatted)
  25. }
  26. }
  27. }
  28. var validateCases = []struct {
  29. s string
  30. ok bool
  31. }{
  32. {"", false},
  33. {"P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2", true},
  34. {"P56IOI7-MZJNU2-IQGDREY-DM2MGT-MGL3BXN-PQ6W5B-TBBZ4TJ-XZWICQ", true},
  35. {"P56IOI7 MZJNU2I QGDREYD M2MGTMGL 3BXNPQ6W 5BTB BZ4T JXZWICQ", true},
  36. {"P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQ", true},
  37. {"P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQCCCC", false},
  38. {"p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicq", true},
  39. {"p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicqCCCC", false},
  40. }
  41. func TestValidateDeviceID(t *testing.T) {
  42. for _, tc := range validateCases {
  43. var id DeviceID
  44. err := id.UnmarshalText([]byte(tc.s))
  45. if (err == nil && !tc.ok) || (err != nil && tc.ok) {
  46. t.Errorf("ValidateDeviceID(%q); %v != %v", tc.s, err, tc.ok)
  47. }
  48. }
  49. }
  50. func TestMarshallingDeviceID(t *testing.T) {
  51. 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}
  52. n1 := DeviceID{}
  53. n2 := DeviceID{}
  54. bs, _ := n0.MarshalText()
  55. n1.UnmarshalText(bs)
  56. bs, _ = n1.MarshalText()
  57. n2.UnmarshalText(bs)
  58. if n2.String() != n0.String() {
  59. t.Errorf("String marshalling error; %q != %q", n2.String(), n0.String())
  60. }
  61. if !n2.Equals(n0) {
  62. t.Error("Equals error")
  63. }
  64. if n2.Compare(n0) != 0 {
  65. t.Error("Compare error")
  66. }
  67. }