utils_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // Copyright (C) 2016 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 util
  7. import (
  8. "testing"
  9. )
  10. type Defaulter struct {
  11. Value string
  12. }
  13. func (d *Defaulter) ParseDefault(v string) error {
  14. *d = Defaulter{Value: v}
  15. return nil
  16. }
  17. func TestSetDefaults(t *testing.T) {
  18. x := &struct {
  19. A string `default:"string"`
  20. B int `default:"2"`
  21. C float64 `default:"2.2"`
  22. D bool `default:"true"`
  23. E Defaulter `default:"defaulter"`
  24. }{}
  25. if x.A != "" {
  26. t.Error("string failed")
  27. } else if x.B != 0 {
  28. t.Error("int failed")
  29. } else if x.C != 0 {
  30. t.Errorf("float failed")
  31. } else if x.D {
  32. t.Errorf("bool failed")
  33. } else if x.E.Value != "" {
  34. t.Errorf("defaulter failed")
  35. }
  36. SetDefaults(x)
  37. if x.A != "string" {
  38. t.Error("string failed")
  39. } else if x.B != 2 {
  40. t.Error("int failed")
  41. } else if x.C != 2.2 {
  42. t.Errorf("float failed")
  43. } else if !x.D {
  44. t.Errorf("bool failed")
  45. } else if x.E.Value != "defaulter" {
  46. t.Errorf("defaulter failed")
  47. }
  48. }
  49. func TestUniqueStrings(t *testing.T) {
  50. tests := []struct {
  51. input []string
  52. expected []string
  53. }{
  54. {
  55. []string{"a", "b"},
  56. []string{"a", "b"},
  57. },
  58. {
  59. []string{"a", "a"},
  60. []string{"a"},
  61. },
  62. {
  63. []string{"a", "a", "a", "a"},
  64. []string{"a"},
  65. },
  66. {
  67. nil,
  68. nil,
  69. },
  70. {
  71. []string{" a ", " a ", "b ", " b"},
  72. []string{"a", "b"},
  73. },
  74. }
  75. for _, test := range tests {
  76. result := UniqueTrimmedStrings(test.input)
  77. if len(result) != len(test.expected) {
  78. t.Errorf("%s != %s", result, test.expected)
  79. }
  80. for i := range result {
  81. if test.expected[i] != result[i] {
  82. t.Errorf("%s != %s", result, test.expected)
  83. }
  84. }
  85. }
  86. }
  87. func TestFillNillSlices(t *testing.T) {
  88. // Nil
  89. x := &struct {
  90. A []string `default:"a,b"`
  91. }{}
  92. if x.A != nil {
  93. t.Error("not nil")
  94. }
  95. if err := FillNilSlices(x); err != nil {
  96. t.Error(err)
  97. }
  98. if len(x.A) != 2 {
  99. t.Error("length")
  100. }
  101. // Already provided
  102. y := &struct {
  103. A []string `default:"c,d,e"`
  104. }{[]string{"a", "b"}}
  105. if len(y.A) != 2 {
  106. t.Error("length")
  107. }
  108. if err := FillNilSlices(y); err != nil {
  109. t.Error(err)
  110. }
  111. if len(y.A) != 2 {
  112. t.Error("length")
  113. }
  114. // Non-nil but empty
  115. z := &struct {
  116. A []string `default:"c,d,e"`
  117. }{[]string{}}
  118. if len(z.A) != 0 {
  119. t.Error("length")
  120. }
  121. if err := FillNilSlices(z); err != nil {
  122. t.Error(err)
  123. }
  124. if len(z.A) != 0 {
  125. t.Error("length")
  126. }
  127. }
  128. func TestAddress(t *testing.T) {
  129. tests := []struct {
  130. network string
  131. host string
  132. result string
  133. }{
  134. {"tcp", "google.com", "tcp://google.com"},
  135. {"foo", "google", "foo://google"},
  136. {"123", "456", "123://456"},
  137. }
  138. for _, test := range tests {
  139. result := Address(test.network, test.host)
  140. if result != test.result {
  141. t.Errorf("%s != %s", result, test.result)
  142. }
  143. }
  144. }
  145. func TestCopyMatching(t *testing.T) {
  146. type Nested struct {
  147. A int
  148. }
  149. type Test struct {
  150. CopyA int
  151. CopyB []string
  152. CopyC Nested
  153. CopyD *Nested
  154. NoCopy int `restart:"true"`
  155. }
  156. from := Test{
  157. CopyA: 1,
  158. CopyB: []string{"friend", "foe"},
  159. CopyC: Nested{
  160. A: 2,
  161. },
  162. CopyD: &Nested{
  163. A: 3,
  164. },
  165. NoCopy: 4,
  166. }
  167. to := Test{
  168. CopyA: 11,
  169. CopyB: []string{"foot", "toe"},
  170. CopyC: Nested{
  171. A: 22,
  172. },
  173. CopyD: &Nested{
  174. A: 33,
  175. },
  176. NoCopy: 44,
  177. }
  178. // Copy empty fields
  179. CopyMatchingTag(&from, &to, "restart", func(v string) bool {
  180. return v != "true"
  181. })
  182. if to.CopyA != 1 {
  183. t.Error("CopyA")
  184. }
  185. if len(to.CopyB) != 2 || to.CopyB[0] != "friend" || to.CopyB[1] != "foe" {
  186. t.Error("CopyB")
  187. }
  188. if to.CopyC.A != 2 {
  189. t.Error("CopyC")
  190. }
  191. if to.CopyD.A != 3 {
  192. t.Error("CopyC")
  193. }
  194. if to.NoCopy != 44 {
  195. t.Error("NoCopy")
  196. }
  197. }
  198. type mockedAddr struct {
  199. network string
  200. addr string
  201. }
  202. func (a mockedAddr) Network() string {
  203. return a.network
  204. }
  205. func (a mockedAddr) String() string {
  206. return a.addr
  207. }
  208. func TestInspecifiedAddressLess(t *testing.T) {
  209. cases := []struct {
  210. netA string
  211. addrA string
  212. netB string
  213. addrB string
  214. }{
  215. // B is assumed the winner.
  216. {"tcp", "127.0.0.1:1234", "tcp", ":1235"},
  217. {"tcp", "127.0.0.1:1234", "tcp", "0.0.0.0:1235"},
  218. {"tcp4", "0.0.0.0:1234", "tcp", "0.0.0.0:1235"}, // tcp4 on the first one
  219. }
  220. for i, testCase := range cases {
  221. addrs := []mockedAddr{
  222. {testCase.netA, testCase.addrA},
  223. {testCase.netB, testCase.addrB},
  224. }
  225. if AddressUnspecifiedLess(addrs[0], addrs[1]) {
  226. t.Error(i, "unexpected")
  227. }
  228. if !AddressUnspecifiedLess(addrs[1], addrs[0]) {
  229. t.Error(i, "unexpected")
  230. }
  231. if AddressUnspecifiedLess(addrs[0], addrs[0]) || AddressUnspecifiedLess(addrs[1], addrs[1]) {
  232. t.Error(i, "unexpected")
  233. }
  234. }
  235. }
  236. func TestFillNil(t *testing.T) {
  237. type A struct {
  238. Slice []int
  239. Map map[string]string
  240. Chan chan int
  241. }
  242. type B struct {
  243. Slice *[]int
  244. Map *map[string]string
  245. Chan *chan int
  246. }
  247. type C struct {
  248. A A
  249. B *B
  250. D *****[]int
  251. }
  252. c := C{}
  253. FillNil(&c)
  254. if c.A.Slice == nil {
  255. t.Error("c.A.Slice")
  256. }
  257. if c.A.Map == nil {
  258. t.Error("c.A.Slice")
  259. }
  260. if c.A.Chan == nil {
  261. t.Error("c.A.Chan")
  262. }
  263. if c.B == nil {
  264. t.Error("c.B")
  265. }
  266. if c.B.Slice == nil {
  267. t.Error("c.B.Slice")
  268. }
  269. if c.B.Map == nil {
  270. t.Error("c.B.Slice")
  271. }
  272. if c.B.Chan == nil {
  273. t.Error("c.B.Chan")
  274. }
  275. if *c.B.Slice == nil {
  276. t.Error("*c.B.Slice")
  277. }
  278. if *c.B.Map == nil {
  279. t.Error("*c.B.Slice")
  280. }
  281. if *c.B.Chan == nil {
  282. t.Error("*c.B.Chan")
  283. }
  284. if *****c.D == nil {
  285. t.Error("c.D")
  286. }
  287. }
  288. func TestFillNilDoesNotBulldozeSetFields(t *testing.T) {
  289. type A struct {
  290. Slice []int
  291. Map map[string]string
  292. Chan chan int
  293. }
  294. type B struct {
  295. Slice *[]int
  296. Map *map[string]string
  297. Chan *chan int
  298. }
  299. type C struct {
  300. A A
  301. B *B
  302. D **[]int
  303. }
  304. ch := make(chan int, 10)
  305. d := make([]int, 10)
  306. dd := &d
  307. c := C{
  308. A: A{
  309. Slice: []int{1},
  310. Map: map[string]string{
  311. "k": "v",
  312. },
  313. Chan: make(chan int, 10),
  314. },
  315. B: &B{
  316. Slice: &[]int{1},
  317. Map: &map[string]string{
  318. "k": "v",
  319. },
  320. Chan: &ch,
  321. },
  322. D: &dd,
  323. }
  324. FillNil(&c)
  325. if len(c.A.Slice) != 1 {
  326. t.Error("c.A.Slice")
  327. }
  328. if len(c.A.Map) != 1 {
  329. t.Error("c.A.Slice")
  330. }
  331. if cap(c.A.Chan) != 10 {
  332. t.Error("c.A.Chan")
  333. }
  334. if c.B == nil {
  335. t.Error("c.B")
  336. }
  337. if len(*c.B.Slice) != 1 {
  338. t.Error("c.B.Slice")
  339. }
  340. if len(*c.B.Map) != 1 {
  341. t.Error("c.B.Slice")
  342. }
  343. if cap(*c.B.Chan) != 10 {
  344. t.Error("c.B.Chan")
  345. }
  346. if cap(**c.D) != 10 {
  347. t.Error("c.D")
  348. }
  349. }