Debian Codegraph @

pkg/storage/continue_test.go

raw
1 /*
2 Copyright 2022 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package storage
18
19 import (
20 "encoding/base64"
21 "encoding/json"
22 "errors"
23 "testing"
24 )
25
26 func encodeContinueOrDie(apiVersion string, resourceVersion int64, nextKey string) string {
27 out, err := json.Marshal(&continueToken{APIVersion: apiVersion, ResourceVersion: resourceVersion, StartKey: nextKey})
28 if err != nil {
29 panic(err)
30 }
31 return base64.RawURLEncoding.EncodeToString(out)
32 }
33
34 func Test_decodeContinue(t *testing.T) {
35 type args struct {
36 continueValue string
37 keyPrefix string
38 }
39 tests := []struct {
40 name string
41 args args
42 wantFromKey string
43 wantRv int64
44 wantErr error
45 }{
46 {
47 name: "valid",
48 args: args{continueValue: encodeContinueOrDie("meta.k8s.io/v1", 1, "key"), keyPrefix: "/test/"},
49 wantRv: 1,
50 wantFromKey: "/test/key",
51 },
52 {
53 name: "root path",
54 args: args{continueValue: encodeContinueOrDie("meta.k8s.io/v1", 1, "/"), keyPrefix: "/test/"},
55 wantRv: 1,
56 wantFromKey: "/test/",
57 },
58 {
59 name: "empty version",
60 args: args{continueValue: encodeContinueOrDie("", 1, "key"), keyPrefix: "/test/"},
61 wantErr: ErrUnrecognizedEncodedVersion,
62 },
63 {
64 name: "invalid version",
65 args: args{continueValue: encodeContinueOrDie("v1", 1, "key"), keyPrefix: "/test/"},
66 wantErr: ErrUnrecognizedEncodedVersion,
67 },
68 {
69 name: "invalid RV",
70 args: args{continueValue: encodeContinueOrDie("meta.k8s.io/v1", 0, "key"), keyPrefix: "/test/"},
71 wantErr: ErrInvalidStartRV,
72 },
73 {
74 name: "no start Key",
75 args: args{continueValue: encodeContinueOrDie("meta.k8s.io/v1", 1, ""), keyPrefix: "/test/"},
76 wantErr: ErrEmptyStartKey,
77 },
78 {
79 name: "path traversal - parent",
80 args: args{continueValue: encodeContinueOrDie("meta.k8s.io/v1", 1, "../key"), keyPrefix: "/test/"},
81 wantErr: ErrGenericInvalidKey,
82 },
83 {
84 name: "path traversal - local",
85 args: args{continueValue: encodeContinueOrDie("meta.k8s.io/v1", 1, "./key"), keyPrefix: "/test/"},
86 wantErr: ErrGenericInvalidKey,
87 },
88 {
89 name: "path traversal - double parent",
90 args: args{continueValue: encodeContinueOrDie("meta.k8s.io/v1", 1, "./../key"), keyPrefix: "/test/"},
91 wantErr: ErrGenericInvalidKey,
92 },
93 {
94 name: "path traversal - after parent",
95 args: args{continueValue: encodeContinueOrDie("meta.k8s.io/v1", 1, "key/../.."), keyPrefix: "/test/"},
96 wantErr: ErrGenericInvalidKey,
97 },
98 }
99 for _, tt := range tests {
100 t.Run(tt.name, func(t *testing.T) {
101 gotFromKey, gotRv, err := DecodeContinue(tt.args.continueValue, tt.args.keyPrefix)
102 if !errors.Is(err, tt.wantErr) {
103 t.Errorf("decodeContinue() error = %v, wantErr %v", err, tt.wantErr)
104 return
105 }
106 if gotFromKey != tt.wantFromKey {
107 t.Errorf("decodeContinue() gotFromKey = %v, want %v", gotFromKey, tt.wantFromKey)
108 }
109 if gotRv != tt.wantRv {
110 t.Errorf("decodeContinue() gotRv = %v, want %v", gotRv, tt.wantRv)
111 }
112 })
113 }
114 }