caching_handler_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package syspolicy
  4. import (
  5. "testing"
  6. )
  7. func TestHandlerReadString(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. key string
  11. handlerKey Key
  12. handlerValue string
  13. handlerError error
  14. preserveHandler bool
  15. wantValue string
  16. wantErr error
  17. strings map[string]string
  18. expectedCalls int
  19. }{
  20. {
  21. name: "read existing cached values",
  22. key: "test",
  23. handlerKey: "do not read",
  24. strings: map[string]string{"test": "foo"},
  25. wantValue: "foo",
  26. expectedCalls: 0,
  27. },
  28. {
  29. name: "read existing values not cached",
  30. key: "test",
  31. handlerKey: "test",
  32. handlerValue: "foo",
  33. wantValue: "foo",
  34. expectedCalls: 1,
  35. },
  36. {
  37. name: "error no such key",
  38. key: "test",
  39. handlerKey: "test",
  40. handlerError: ErrNoSuchKey,
  41. wantErr: ErrNoSuchKey,
  42. expectedCalls: 1,
  43. },
  44. {
  45. name: "other error",
  46. key: "test",
  47. handlerKey: "test",
  48. handlerError: someOtherError,
  49. wantErr: someOtherError,
  50. preserveHandler: true,
  51. expectedCalls: 2,
  52. },
  53. }
  54. for _, tt := range tests {
  55. t.Run(tt.name, func(t *testing.T) {
  56. testHandler := &testHandler{
  57. t: t,
  58. key: tt.handlerKey,
  59. s: tt.handlerValue,
  60. err: tt.handlerError,
  61. }
  62. cache := NewCachingHandler(testHandler)
  63. if tt.strings != nil {
  64. cache.strings = tt.strings
  65. }
  66. got, err := cache.ReadString(tt.key)
  67. if err != tt.wantErr {
  68. t.Errorf("err=%v want %v", err, tt.wantErr)
  69. }
  70. if got != tt.wantValue {
  71. t.Errorf("got %v want %v", got, cache.strings[tt.key])
  72. }
  73. if !tt.preserveHandler {
  74. testHandler.key, testHandler.s, testHandler.err = "do not read", "", nil
  75. }
  76. got, err = cache.ReadString(tt.key)
  77. if err != tt.wantErr {
  78. t.Errorf("repeat err=%v want %v", err, tt.wantErr)
  79. }
  80. if got != tt.wantValue {
  81. t.Errorf("repeat got %v want %v", got, cache.strings[tt.key])
  82. }
  83. if testHandler.calls != tt.expectedCalls {
  84. t.Errorf("calls=%v want %v", testHandler.calls, tt.expectedCalls)
  85. }
  86. })
  87. }
  88. }
  89. func TestHandlerReadUint64(t *testing.T) {
  90. tests := []struct {
  91. name string
  92. key string
  93. handlerKey Key
  94. handlerValue uint64
  95. handlerError error
  96. preserveHandler bool
  97. wantValue uint64
  98. wantErr error
  99. uint64s map[string]uint64
  100. expectedCalls int
  101. }{
  102. {
  103. name: "read existing cached values",
  104. key: "test",
  105. handlerKey: "do not read",
  106. uint64s: map[string]uint64{"test": 1},
  107. wantValue: 1,
  108. expectedCalls: 0,
  109. },
  110. {
  111. name: "read existing values not cached",
  112. key: "test",
  113. handlerKey: "test",
  114. handlerValue: 1,
  115. wantValue: 1,
  116. expectedCalls: 1,
  117. },
  118. {
  119. name: "error no such key",
  120. key: "test",
  121. handlerKey: "test",
  122. handlerError: ErrNoSuchKey,
  123. wantErr: ErrNoSuchKey,
  124. expectedCalls: 1,
  125. },
  126. {
  127. name: "other error",
  128. key: "test",
  129. handlerKey: "test",
  130. handlerError: someOtherError,
  131. wantErr: someOtherError,
  132. preserveHandler: true,
  133. expectedCalls: 2,
  134. },
  135. }
  136. for _, tt := range tests {
  137. t.Run(tt.name, func(t *testing.T) {
  138. testHandler := &testHandler{
  139. t: t,
  140. key: tt.handlerKey,
  141. u64: tt.handlerValue,
  142. err: tt.handlerError,
  143. }
  144. cache := NewCachingHandler(testHandler)
  145. if tt.uint64s != nil {
  146. cache.uint64s = tt.uint64s
  147. }
  148. got, err := cache.ReadUInt64(tt.key)
  149. if err != tt.wantErr {
  150. t.Errorf("err=%v want %v", err, tt.wantErr)
  151. }
  152. if got != tt.wantValue {
  153. t.Errorf("got %v want %v", got, cache.strings[tt.key])
  154. }
  155. if !tt.preserveHandler {
  156. testHandler.key, testHandler.s, testHandler.err = "do not read", "", nil
  157. }
  158. got, err = cache.ReadUInt64(tt.key)
  159. if err != tt.wantErr {
  160. t.Errorf("repeat err=%v want %v", err, tt.wantErr)
  161. }
  162. if got != tt.wantValue {
  163. t.Errorf("repeat got %v want %v", got, cache.strings[tt.key])
  164. }
  165. if testHandler.calls != tt.expectedCalls {
  166. t.Errorf("calls=%v want %v", testHandler.calls, tt.expectedCalls)
  167. }
  168. })
  169. }
  170. }
  171. func TestHandlerReadBool(t *testing.T) {
  172. tests := []struct {
  173. name string
  174. key string
  175. handlerKey Key
  176. handlerValue bool
  177. handlerError error
  178. preserveHandler bool
  179. wantValue bool
  180. wantErr error
  181. bools map[string]bool
  182. expectedCalls int
  183. }{
  184. {
  185. name: "read existing cached values",
  186. key: "test",
  187. handlerKey: "do not read",
  188. bools: map[string]bool{"test": true},
  189. wantValue: true,
  190. expectedCalls: 0,
  191. },
  192. {
  193. name: "read existing values not cached",
  194. key: "test",
  195. handlerKey: "test",
  196. handlerValue: true,
  197. wantValue: true,
  198. expectedCalls: 1,
  199. },
  200. {
  201. name: "error no such key",
  202. key: "test",
  203. handlerKey: "test",
  204. handlerError: ErrNoSuchKey,
  205. wantErr: ErrNoSuchKey,
  206. expectedCalls: 1,
  207. },
  208. {
  209. name: "other error",
  210. key: "test",
  211. handlerKey: "test",
  212. handlerError: someOtherError,
  213. wantErr: someOtherError,
  214. preserveHandler: true,
  215. expectedCalls: 2,
  216. },
  217. }
  218. for _, tt := range tests {
  219. t.Run(tt.name, func(t *testing.T) {
  220. testHandler := &testHandler{
  221. t: t,
  222. key: tt.handlerKey,
  223. b: tt.handlerValue,
  224. err: tt.handlerError,
  225. }
  226. cache := NewCachingHandler(testHandler)
  227. if tt.bools != nil {
  228. cache.bools = tt.bools
  229. }
  230. got, err := cache.ReadBoolean(tt.key)
  231. if err != tt.wantErr {
  232. t.Errorf("err=%v want %v", err, tt.wantErr)
  233. }
  234. if got != tt.wantValue {
  235. t.Errorf("got %v want %v", got, cache.strings[tt.key])
  236. }
  237. if !tt.preserveHandler {
  238. testHandler.key, testHandler.s, testHandler.err = "do not read", "", nil
  239. }
  240. got, err = cache.ReadBoolean(tt.key)
  241. if err != tt.wantErr {
  242. t.Errorf("repeat err=%v want %v", err, tt.wantErr)
  243. }
  244. if got != tt.wantValue {
  245. t.Errorf("repeat got %v want %v", got, cache.strings[tt.key])
  246. }
  247. if testHandler.calls != tt.expectedCalls {
  248. t.Errorf("calls=%v want %v", testHandler.calls, tt.expectedCalls)
  249. }
  250. })
  251. }
  252. }