File: cache_test.go

package info (click to toggle)
golang-github-smallstep-crypto 0.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,800 kB
  • sloc: sh: 66; makefile: 50
file content (171 lines) | stat: -rw-r--r-- 3,806 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package tlsutil

import (
	"reflect"
	"sync"
	"testing"
)

func testCache() *sync.Map {
	m := new(sync.Map)
	m.Store("ok.test", &credentialsCacheElement{
		sni: "ok.test",
	})
	m.Store("bad.test", 123)
	m.Store("nil.test", nil)
	return m
}

func Test_newCredentialsCache(t *testing.T) {
	tests := []struct {
		name string
		want *credentialsCache
	}{
		{"ok", &credentialsCache{
			CacheStore: new(sync.Map),
		}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := newCredentialsCache(); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("newCredentialsCache() = %v, want %v", got, tt.want)
			}
		})
	}
}

func Test_credentialsCache_Load(t *testing.T) {
	m := testCache()
	type fields struct {
		CacheStore *sync.Map
	}
	type args struct {
		domain string
	}
	tests := []struct {
		name   string
		fields fields
		args   args
		want   *credentialsCacheElement
		want1  bool
	}{
		{"ok", fields{m}, args{"ok.test"}, &credentialsCacheElement{sni: "ok.test"}, true},
		{"nil", fields{m}, args{"nil.test"}, nil, false},
		{"fail", fields{m}, args{"fail.test"}, nil, false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			c := &credentialsCache{
				CacheStore: tt.fields.CacheStore,
			}
			got, got1 := c.Load(tt.args.domain)
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("credentialsCache.Load() got = %v, want %v", got, tt.want)
			}
			if got1 != tt.want1 {
				t.Errorf("credentialsCache.Load() got1 = %v, want %v", got1, tt.want1)
			}
		})
	}
}

func Test_credentialsCache_Store(t *testing.T) {
	type fields struct {
		CacheStore *sync.Map
	}
	type args struct {
		domain string
		v      *credentialsCacheElement
	}
	tests := []struct {
		name   string
		fields fields
		args   args
	}{
		{"ok", fields{new(sync.Map)}, args{"ok.test", &credentialsCacheElement{sni: "ok.test"}}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			c := &credentialsCache{
				CacheStore: tt.fields.CacheStore,
			}
			c.Store(tt.args.domain, tt.args.v)

			got, got1 := c.Load(tt.args.domain)
			if !reflect.DeepEqual(got, tt.args.v) {
				t.Errorf("credentialsCache.Load() got = %v, want %v", got, tt.args.v)
			}
			if !got1 {
				t.Errorf("credentialsCache.Load() got1 = %v, want %v", got1, true)
			}
		})
	}
}

func Test_credentialsCache_Delete(t *testing.T) {
	m := testCache()
	type fields struct {
		CacheStore *sync.Map
	}
	type args struct {
		domain string
	}
	tests := []struct {
		name   string
		fields fields
		args   args
	}{
		{"ok", fields{m}, args{"ok.test"}},
		{"deleted", fields{m}, args{"ok.test"}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			c := &credentialsCache{
				CacheStore: tt.fields.CacheStore,
			}
			c.Delete(tt.args.domain)

			got, got1 := c.Load(tt.args.domain)
			if !reflect.DeepEqual(got, (*credentialsCacheElement)(nil)) {
				t.Errorf("credentialsCache.Load() got = %v, want %v", got, nil)
			}
			if got1 {
				t.Errorf("credentialsCache.Load() got1 = %v, want %v", got1, false)
			}
		})
	}
}

func Test_credentialsCache_Range(t *testing.T) {
	got := []*credentialsCacheElement{}
	want := []*credentialsCacheElement{
		{sni: "ok.test"},
	}
	type fields struct {
		CacheStore *sync.Map
	}
	type args struct {
		fn func(domain string, v *credentialsCacheElement) bool
	}
	tests := []struct {
		name   string
		fields fields
		args   args
	}{
		{"ok", fields{testCache()}, args{func(domain string, v *credentialsCacheElement) bool {
			got = append(got, v)
			return true
		}}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			c := &credentialsCache{
				CacheStore: tt.fields.CacheStore,
			}
			c.Range(tt.args.fn)
			if !reflect.DeepEqual(got, want) {
				t.Errorf("credentialsCache.Range() got = %v, want %v", got, want)
			}
		})
	}
}