nodeid_test.go 2.4 KB

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