aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/yandex/tvm/tvm_test.go
blob: b7cb5605d169bcbbd8f5c79a9aafd46facd98eab (plain) (blame)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package tvm_test

import (
	"testing"

	"github.com/stretchr/testify/require"
	"github.com/ydb-platform/ydb/library/go/yandex/tvm"
)

func TestUserTicketCheckScopes(t *testing.T) {
	cases := map[string]struct {
		ticketScopes   []string
		requiredScopes []string
		err            bool
	}{
		"wo_required_scopes": {
			ticketScopes:   []string{"bb:sessionid"},
			requiredScopes: nil,
			err:            false,
		},
		"multiple_scopes_0": {
			ticketScopes:   []string{"bb:sessionid", "test:test"},
			requiredScopes: []string{"bb:sessionid", "test:test"},
			err:            false,
		},
		"multiple_scopes_1": {
			ticketScopes:   []string{"bb:sessionid", "test:test"},
			requiredScopes: []string{"test:test", "bb:sessionid"},
			err:            false,
		},
		"wo_scopes": {
			ticketScopes:   nil,
			requiredScopes: []string{"bb:sessionid"},
			err:            true,
		},
		"invalid_scopes": {
			ticketScopes:   []string{"bb:sessionid"},
			requiredScopes: []string{"test:test"},
			err:            true,
		},
		"not_all_scopes": {
			ticketScopes:   []string{"bb:sessionid", "test:test1"},
			requiredScopes: []string{"bb:sessionid", "test:test"},
			err:            true,
		},
	}

	for name, testCase := range cases {
		t.Run(name, func(t *testing.T) {
			ticket := tvm.CheckedUserTicket{
				Scopes: testCase.ticketScopes,
			}
			err := ticket.CheckScopes(testCase.requiredScopes...)
			if testCase.err {
				require.Error(t, err)
				require.IsType(t, &tvm.TicketError{}, err)
				ticketErr := err.(*tvm.TicketError)
				require.Equal(t, tvm.TicketInvalidScopes, ticketErr.Status)
			} else {
				require.NoError(t, err)
			}
		})
	}
}

func TestUserTicketCheckScopesAny(t *testing.T) {
	cases := map[string]struct {
		ticketScopes   []string
		requiredScopes []string
		err            bool
	}{
		"wo_required_scopes": {
			ticketScopes:   []string{"bb:sessionid"},
			requiredScopes: nil,
			err:            false,
		},
		"multiple_scopes_0": {
			ticketScopes:   []string{"bb:sessionid", "test:test"},
			requiredScopes: []string{"bb:sessionid"},
			err:            false,
		},
		"multiple_scopes_1": {
			ticketScopes:   []string{"bb:sessionid", "test:test"},
			requiredScopes: []string{"test:test"},
			err:            false,
		},
		"multiple_scopes_2": {
			ticketScopes:   []string{"bb:sessionid", "test:test"},
			requiredScopes: []string{"bb:sessionid", "test:test"},
			err:            false,
		},
		"multiple_scopes_3": {
			ticketScopes:   []string{"bb:sessionid", "test:test"},
			requiredScopes: []string{"test:test", "bb:sessionid"},
			err:            false,
		},
		"wo_scopes": {
			ticketScopes:   nil,
			requiredScopes: []string{"bb:sessionid"},
			err:            true,
		},
		"invalid_scopes": {
			ticketScopes:   []string{"bb:sessionid"},
			requiredScopes: []string{"test:test"},
			err:            true,
		},
	}

	for name, testCase := range cases {
		t.Run(name, func(t *testing.T) {
			ticket := tvm.CheckedUserTicket{
				Scopes: testCase.ticketScopes,
			}
			err := ticket.CheckScopes(testCase.requiredScopes...)
			if testCase.err {
				require.Error(t, err)
				require.IsType(t, &tvm.TicketError{}, err)
				ticketErr := err.(*tvm.TicketError)
				require.Equal(t, tvm.TicketInvalidScopes, ticketErr.Status)
			} else {
				require.NoError(t, err)
			}
		})
	}
}

func TestServiceTicketAllowedSrcIDs(t *testing.T) {
	cases := map[string]struct {
		srcID         uint32
		allowedSrcIDs []uint32
		err           bool
	}{
		"empty_allow_list_allows_any_srcID": {srcID: 162, allowedSrcIDs: []uint32{}, err: false},
		"known_src_id_is_allowed":           {srcID: 42, allowedSrcIDs: []uint32{42, 100500}, err: false},
		"unknown_src_id_is_not_allowed":     {srcID: 404, allowedSrcIDs: []uint32{42, 100500}, err: true},
	}

	for name, testCase := range cases {
		t.Run(name, func(t *testing.T) {
			ticket := tvm.CheckedServiceTicket{
				SrcID: tvm.ClientID(testCase.srcID),
			}
			allowedSrcIDsMap := make(map[uint32]struct{}, len(testCase.allowedSrcIDs))
			for _, allowedSrcID := range testCase.allowedSrcIDs {
				allowedSrcIDsMap[allowedSrcID] = struct{}{}
			}
			err := ticket.CheckSrcID(allowedSrcIDsMap)
			if testCase.err {
				require.Error(t, err)
				require.IsType(t, &tvm.TicketError{}, err)
				ticketErr := err.(*tvm.TicketError)
				require.Equal(t, tvm.TicketInvalidSrcID, ticketErr.Status)
			} else {
				require.NoError(t, err)
			}
		})
	}
}

func TestTicketError_Is(t *testing.T) {
	err1 := &tvm.TicketError{
		Status: tvm.TicketInvalidSrcID,
		Msg:    "uh oh",
	}
	err2 := &tvm.TicketError{
		Status: tvm.TicketInvalidSrcID,
		Msg:    "uh oh",
	}
	err3 := &tvm.TicketError{
		Status: tvm.TicketInvalidSrcID,
		Msg:    "other uh oh message",
	}
	err4 := &tvm.TicketError{
		Status: tvm.TicketExpired,
		Msg:    "uh oh",
	}
	err5 := &tvm.TicketError{
		Status: tvm.TicketMalformed,
		Msg:    "i am completely different",
	}
	var nilErr *tvm.TicketError = nil

	// ticketErrors are equal to themselves
	require.True(t, err1.Is(err1))
	require.True(t, err2.Is(err2))
	require.True(t, nilErr.Is(nilErr))

	// equal value ticketErrors are equal
	require.True(t, err1.Is(err2))
	require.True(t, err2.Is(err1))
	// equal status ticketErrors are equal
	require.True(t, err1.Is(err3))
	require.True(t, err1.Is(tvm.ErrTicketInvalidSrcID))
	require.True(t, err2.Is(tvm.ErrTicketInvalidSrcID))
	require.True(t, err3.Is(tvm.ErrTicketInvalidSrcID))
	require.True(t, err4.Is(tvm.ErrTicketExpired))
	require.True(t, err5.Is(tvm.ErrTicketMalformed))

	// different status ticketErrors are not equal
	require.False(t, err1.Is(err4))

	// completely different ticketErrors are not equal
	require.False(t, err1.Is(err5))

	// non-nil ticketErrors are not equal to nil errors
	require.False(t, err1.Is(nil))
	require.False(t, err2.Is(nil))

	// non-nil ticketErrors are not equal to nil ticketErrors
	require.False(t, err1.Is(nilErr))
	require.False(t, err2.Is(nilErr))
}

func TestBbEnvFromString(t *testing.T) {
	type Case struct {
		in  string
		env tvm.BlackboxEnv
		err string
	}
	cases := []Case{
		{in: "prod", env: tvm.BlackboxProd},
		{in: "Prod", env: tvm.BlackboxProd},
		{in: "ProD", env: tvm.BlackboxProd},
		{in: "PROD", env: tvm.BlackboxProd},
		{in: "test", env: tvm.BlackboxTest},
		{in: "prod_yateam", env: tvm.BlackboxProdYateam},
		{in: "ProdYateam", env: tvm.BlackboxProdYateam},
		{in: "test_yateam", env: tvm.BlackboxTestYateam},
		{in: "TestYateam", env: tvm.BlackboxTestYateam},
		{in: "stress", env: tvm.BlackboxStress},
		{in: "", err: "blackbox env is unknown: ''"},
		{in: "kek", err: "blackbox env is unknown: 'kek'"},
	}

	for idx, c := range cases {
		res, err := tvm.BlackboxEnvFromString(c.in)

		if c.err == "" {
			require.NoError(t, err, idx)
			require.Equal(t, c.env, res, idx)
		} else {
			require.EqualError(t, err, c.err, idx)
		}
	}
}