deviceid_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package protocol
  7. import "testing"
  8. var (
  9. formatted = "P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2"
  10. formatCases = []string{
  11. "P56IOI-7MZJNU-2IQGDR-EYDM2M-GTMGL3-BXNPQ6-W5BTBB-Z4TJXZ-WICQ",
  12. "P56IOI-7MZJNU2Y-IQGDR-EYDM2M-GTI-MGL3-BXNPQ6-W5BM-TBB-Z4TJXZ-WICQ2",
  13. "P56IOI7 MZJNU2I QGDREYD M2MGTMGL 3BXNPQ6W 5BTB BZ4T JXZWICQ",
  14. "P56IOI7 MZJNU2Y IQGDREY DM2MGTI MGL3BXN PQ6W5BM TBBZ4TJ XZWICQ2",
  15. "P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQ",
  16. "p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicq",
  17. "P56IOI7MZJNU2YIQGDREYDM2MGTIMGL3BXNPQ6W5BMTBBZ4TJXZWICQ2",
  18. "P561017MZJNU2YIQGDREYDM2MGTIMGL3BXNPQ6W5BMT88Z4TJXZWICQ2",
  19. "p56ioi7mzjnu2yiqgdreydm2mgtimgl3bxnpq6w5bmtbbz4tjxzwicq2",
  20. "p561017mzjnu2yiqgdreydm2mgtimgl3bxnpq6w5bmt88z4tjxzwicq2",
  21. }
  22. )
  23. func TestFormatDeviceID(t *testing.T) {
  24. for i, tc := range formatCases {
  25. var id DeviceID
  26. err := id.UnmarshalText([]byte(tc))
  27. if err != nil {
  28. t.Errorf("#%d UnmarshalText(%q); %v", i, tc, err)
  29. } else if f := id.String(); f != formatted {
  30. t.Errorf("#%d FormatDeviceID(%q)\n\t%q !=\n\t%q", i, tc, f, formatted)
  31. }
  32. }
  33. }
  34. var validateCases = []struct {
  35. s string
  36. ok bool
  37. }{
  38. {"", true}, // Empty device ID, all zeroes
  39. {"a", false},
  40. {"P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2", true},
  41. {"P56IOI7-MZJNU2-IQGDREY-DM2MGT-MGL3BXN-PQ6W5B-TBBZ4TJ-XZWICQ", true},
  42. {"P56IOI7 MZJNU2I QGDREYD M2MGTMGL 3BXNPQ6W 5BTB BZ4T JXZWICQ", true},
  43. {"P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQ", true},
  44. {"P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQCCCC", false},
  45. {"p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicq", true},
  46. {"p56ioi7mzjnu2iqgdreydm2mgtmgl3bxnpq6w5btbbz4tjxzwicqCCCC", false},
  47. }
  48. func TestValidateDeviceID(t *testing.T) {
  49. for _, tc := range validateCases {
  50. var id DeviceID
  51. err := id.UnmarshalText([]byte(tc.s))
  52. if (err == nil && !tc.ok) || (err != nil && tc.ok) {
  53. t.Errorf("ValidateDeviceID(%q); %v != %v", tc.s, err, tc.ok)
  54. }
  55. }
  56. }
  57. func TestMarshallingDeviceID(t *testing.T) {
  58. 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}
  59. n1 := DeviceID{}
  60. n2 := DeviceID{}
  61. bs, _ := n0.MarshalText()
  62. if err := n1.UnmarshalText(bs); err != nil {
  63. t.Fatal(err)
  64. }
  65. bs, _ = n1.MarshalText()
  66. if err := n2.UnmarshalText(bs); err != nil {
  67. t.Fatal(err)
  68. }
  69. if n2.String() != n0.String() {
  70. t.Errorf("String marshalling error; %q != %q", n2.String(), n0.String())
  71. }
  72. if !n2.Equals(n0) {
  73. t.Error("Equals error")
  74. }
  75. if n2.Compare(n0) != 0 {
  76. t.Error("Compare error")
  77. }
  78. }
  79. func TestShortIDString(t *testing.T) {
  80. id, _ := DeviceIDFromString(formatted)
  81. sid := id.Short().String()
  82. // keep consistent with ShortIDStringLength in lib/protocol/deviceid.go
  83. if len(sid) != 7 {
  84. t.Errorf("Wrong length for short ID: got %d, want 7", len(sid))
  85. }
  86. want := formatted[:len(sid)]
  87. if sid != want {
  88. t.Errorf("Wrong short ID: got %q, want %q", sid, want)
  89. }
  90. }
  91. func TestDeviceIDFromBytes(t *testing.T) {
  92. id0, _ := DeviceIDFromString(formatted)
  93. id1, err := DeviceIDFromBytes(id0[:])
  94. if err != nil {
  95. t.Fatal(err)
  96. } else if id1.String() != formatted {
  97. t.Errorf("Wrong device ID, got %q, want %q", id1, formatted)
  98. }
  99. }
  100. var resStr string
  101. func BenchmarkLuhnify(b *testing.B) {
  102. str := "ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB"
  103. var err error
  104. for i := 0; i < b.N; i++ {
  105. resStr, err = luhnify(str)
  106. if err != nil {
  107. b.Fatal(err)
  108. }
  109. }
  110. }
  111. func BenchmarkUnluhnify(b *testing.B) {
  112. str, _ := luhnify("ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB")
  113. var err error
  114. for i := 0; i < b.N; i++ {
  115. resStr, err = unluhnify(str)
  116. if err != nil {
  117. b.Fatal(err)
  118. }
  119. }
  120. }
  121. func BenchmarkChunkify(b *testing.B) {
  122. str := "ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB"
  123. for i := 0; i < b.N; i++ {
  124. resStr = chunkify(str)
  125. }
  126. }
  127. func BenchmarkUnchunkify(b *testing.B) {
  128. str := chunkify("ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJAB")
  129. for i := 0; i < b.N; i++ {
  130. resStr = unchunkify(str)
  131. }
  132. }