| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- // Copyright (c) Tailscale Inc & AUTHORS
- // SPDX-License-Identifier: BSD-3-Clause
- package syspolicy
- import (
- "testing"
- )
- func TestHandlerReadString(t *testing.T) {
- tests := []struct {
- name string
- key string
- handlerKey Key
- handlerValue string
- handlerError error
- preserveHandler bool
- wantValue string
- wantErr error
- strings map[string]string
- expectedCalls int
- }{
- {
- name: "read existing cached values",
- key: "test",
- handlerKey: "do not read",
- strings: map[string]string{"test": "foo"},
- wantValue: "foo",
- expectedCalls: 0,
- },
- {
- name: "read existing values not cached",
- key: "test",
- handlerKey: "test",
- handlerValue: "foo",
- wantValue: "foo",
- expectedCalls: 1,
- },
- {
- name: "error no such key",
- key: "test",
- handlerKey: "test",
- handlerError: ErrNoSuchKey,
- wantErr: ErrNoSuchKey,
- expectedCalls: 1,
- },
- {
- name: "other error",
- key: "test",
- handlerKey: "test",
- handlerError: someOtherError,
- wantErr: someOtherError,
- preserveHandler: true,
- expectedCalls: 2,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- testHandler := &testHandler{
- t: t,
- key: tt.handlerKey,
- s: tt.handlerValue,
- err: tt.handlerError,
- }
- cache := NewCachingHandler(testHandler)
- if tt.strings != nil {
- cache.strings = tt.strings
- }
- got, err := cache.ReadString(tt.key)
- if err != tt.wantErr {
- t.Errorf("err=%v want %v", err, tt.wantErr)
- }
- if got != tt.wantValue {
- t.Errorf("got %v want %v", got, cache.strings[tt.key])
- }
- if !tt.preserveHandler {
- testHandler.key, testHandler.s, testHandler.err = "do not read", "", nil
- }
- got, err = cache.ReadString(tt.key)
- if err != tt.wantErr {
- t.Errorf("repeat err=%v want %v", err, tt.wantErr)
- }
- if got != tt.wantValue {
- t.Errorf("repeat got %v want %v", got, cache.strings[tt.key])
- }
- if testHandler.calls != tt.expectedCalls {
- t.Errorf("calls=%v want %v", testHandler.calls, tt.expectedCalls)
- }
- })
- }
- }
- func TestHandlerReadUint64(t *testing.T) {
- tests := []struct {
- name string
- key string
- handlerKey Key
- handlerValue uint64
- handlerError error
- preserveHandler bool
- wantValue uint64
- wantErr error
- uint64s map[string]uint64
- expectedCalls int
- }{
- {
- name: "read existing cached values",
- key: "test",
- handlerKey: "do not read",
- uint64s: map[string]uint64{"test": 1},
- wantValue: 1,
- expectedCalls: 0,
- },
- {
- name: "read existing values not cached",
- key: "test",
- handlerKey: "test",
- handlerValue: 1,
- wantValue: 1,
- expectedCalls: 1,
- },
- {
- name: "error no such key",
- key: "test",
- handlerKey: "test",
- handlerError: ErrNoSuchKey,
- wantErr: ErrNoSuchKey,
- expectedCalls: 1,
- },
- {
- name: "other error",
- key: "test",
- handlerKey: "test",
- handlerError: someOtherError,
- wantErr: someOtherError,
- preserveHandler: true,
- expectedCalls: 2,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- testHandler := &testHandler{
- t: t,
- key: tt.handlerKey,
- u64: tt.handlerValue,
- err: tt.handlerError,
- }
- cache := NewCachingHandler(testHandler)
- if tt.uint64s != nil {
- cache.uint64s = tt.uint64s
- }
- got, err := cache.ReadUInt64(tt.key)
- if err != tt.wantErr {
- t.Errorf("err=%v want %v", err, tt.wantErr)
- }
- if got != tt.wantValue {
- t.Errorf("got %v want %v", got, cache.strings[tt.key])
- }
- if !tt.preserveHandler {
- testHandler.key, testHandler.s, testHandler.err = "do not read", "", nil
- }
- got, err = cache.ReadUInt64(tt.key)
- if err != tt.wantErr {
- t.Errorf("repeat err=%v want %v", err, tt.wantErr)
- }
- if got != tt.wantValue {
- t.Errorf("repeat got %v want %v", got, cache.strings[tt.key])
- }
- if testHandler.calls != tt.expectedCalls {
- t.Errorf("calls=%v want %v", testHandler.calls, tt.expectedCalls)
- }
- })
- }
- }
- func TestHandlerReadBool(t *testing.T) {
- tests := []struct {
- name string
- key string
- handlerKey Key
- handlerValue bool
- handlerError error
- preserveHandler bool
- wantValue bool
- wantErr error
- bools map[string]bool
- expectedCalls int
- }{
- {
- name: "read existing cached values",
- key: "test",
- handlerKey: "do not read",
- bools: map[string]bool{"test": true},
- wantValue: true,
- expectedCalls: 0,
- },
- {
- name: "read existing values not cached",
- key: "test",
- handlerKey: "test",
- handlerValue: true,
- wantValue: true,
- expectedCalls: 1,
- },
- {
- name: "error no such key",
- key: "test",
- handlerKey: "test",
- handlerError: ErrNoSuchKey,
- wantErr: ErrNoSuchKey,
- expectedCalls: 1,
- },
- {
- name: "other error",
- key: "test",
- handlerKey: "test",
- handlerError: someOtherError,
- wantErr: someOtherError,
- preserveHandler: true,
- expectedCalls: 2,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- testHandler := &testHandler{
- t: t,
- key: tt.handlerKey,
- b: tt.handlerValue,
- err: tt.handlerError,
- }
- cache := NewCachingHandler(testHandler)
- if tt.bools != nil {
- cache.bools = tt.bools
- }
- got, err := cache.ReadBoolean(tt.key)
- if err != tt.wantErr {
- t.Errorf("err=%v want %v", err, tt.wantErr)
- }
- if got != tt.wantValue {
- t.Errorf("got %v want %v", got, cache.strings[tt.key])
- }
- if !tt.preserveHandler {
- testHandler.key, testHandler.s, testHandler.err = "do not read", "", nil
- }
- got, err = cache.ReadBoolean(tt.key)
- if err != tt.wantErr {
- t.Errorf("repeat err=%v want %v", err, tt.wantErr)
- }
- if got != tt.wantValue {
- t.Errorf("repeat got %v want %v", got, cache.strings[tt.key])
- }
- if testHandler.calls != tt.expectedCalls {
- t.Errorf("calls=%v want %v", testHandler.calls, tt.expectedCalls)
- }
- })
- }
- }
|