util_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
  2. // Use of this source code is governed by an MIT-style license that can be
  3. // found in the LICENSE file.
  4. package model
  5. import (
  6. "testing"
  7. "github.com/calmh/syncthing/protocol"
  8. )
  9. var testcases = []struct {
  10. local, remote protocol.ClusterConfigMessage
  11. err string
  12. }{
  13. {
  14. local: protocol.ClusterConfigMessage{},
  15. remote: protocol.ClusterConfigMessage{},
  16. err: "",
  17. },
  18. {
  19. local: protocol.ClusterConfigMessage{ClientName: "a", ClientVersion: "b"},
  20. remote: protocol.ClusterConfigMessage{ClientName: "c", ClientVersion: "d"},
  21. err: "",
  22. },
  23. {
  24. local: protocol.ClusterConfigMessage{
  25. Repositories: []protocol.Repository{
  26. {ID: "foo"},
  27. {ID: "bar"},
  28. },
  29. },
  30. remote: protocol.ClusterConfigMessage{
  31. Repositories: []protocol.Repository{
  32. {ID: "foo"},
  33. {ID: "bar"},
  34. },
  35. },
  36. err: "",
  37. },
  38. {
  39. local: protocol.ClusterConfigMessage{
  40. Repositories: []protocol.Repository{
  41. {
  42. ID: "foo",
  43. Nodes: []protocol.Node{
  44. {ID: "a"},
  45. },
  46. },
  47. {ID: "bar"},
  48. },
  49. },
  50. remote: protocol.ClusterConfigMessage{
  51. Repositories: []protocol.Repository{
  52. {ID: "foo"},
  53. {ID: "bar"},
  54. },
  55. },
  56. err: "",
  57. },
  58. {
  59. local: protocol.ClusterConfigMessage{
  60. Repositories: []protocol.Repository{
  61. {
  62. ID: "foo",
  63. Nodes: []protocol.Node{
  64. {ID: "a"},
  65. },
  66. },
  67. {ID: "bar"},
  68. },
  69. },
  70. remote: protocol.ClusterConfigMessage{
  71. Repositories: []protocol.Repository{
  72. {
  73. ID: "foo",
  74. Nodes: []protocol.Node{
  75. {ID: "a"},
  76. {ID: "b"},
  77. },
  78. },
  79. {ID: "bar"},
  80. },
  81. },
  82. err: "",
  83. },
  84. {
  85. local: protocol.ClusterConfigMessage{
  86. Repositories: []protocol.Repository{
  87. {
  88. ID: "foo",
  89. Nodes: []protocol.Node{
  90. {
  91. ID: "a",
  92. Flags: protocol.FlagShareReadOnly,
  93. },
  94. },
  95. },
  96. {ID: "bar"},
  97. },
  98. },
  99. remote: protocol.ClusterConfigMessage{
  100. Repositories: []protocol.Repository{
  101. {
  102. ID: "foo",
  103. Nodes: []protocol.Node{
  104. {
  105. ID: "a",
  106. Flags: protocol.FlagShareTrusted,
  107. },
  108. },
  109. },
  110. {ID: "bar"},
  111. },
  112. },
  113. err: `remote has different sharing flags for node "a" in repository "foo"`,
  114. },
  115. }
  116. func TestCompareClusterConfig(t *testing.T) {
  117. for i, tc := range testcases {
  118. err := compareClusterConfig(tc.local, tc.remote)
  119. switch {
  120. case tc.err == "" && err != nil:
  121. t.Errorf("#%d: unexpected error: %v", i, err)
  122. case tc.err != "" && err == nil:
  123. t.Errorf("#%d: unexpected nil error", i)
  124. case tc.err != "" && err != nil && tc.err != err.Error():
  125. t.Errorf("#%d: incorrect error: %q != %q", i, err, tc.err)
  126. }
  127. }
  128. }