nodeid_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  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 TestFormatNodeID(t *testing.T) {
  20. for i, tc := range formatCases {
  21. var id NodeID
  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 FormatNodeID(%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 TestValidateNodeID(t *testing.T) {
  44. for _, tc := range validateCases {
  45. var id NodeID
  46. err := id.UnmarshalText([]byte(tc.s))
  47. if (err == nil && !tc.ok) || (err != nil && tc.ok) {
  48. t.Errorf("ValidateNodeID(%q); %v != %v", tc.s, err, tc.ok)
  49. }
  50. }
  51. }
  52. func TestMarshallingNodeID(t *testing.T) {
  53. n0 := NodeID{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 := NodeID{}
  55. n2 := NodeID{}
  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. }