Debian Codegraph @

mount/mount_test.go

raw
1 /*
2 Copyright 2018 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 mount
18
19 import (
20 "reflect"
21 "strings"
22 "testing"
23 )
24
25 func TestMakeBindOpts(t *testing.T) {
26 tests := []struct {
27 mountOption []string
28 isBind bool
29 expectedBindOpts []string
30 expectedRemountOpts []string
31 }{
32 {
33 []string{"vers=2", "ro", "_netdev"},
34 false,
35 []string{},
36 []string{},
37 },
38 {
39
40 []string{"bind", "vers=2", "ro", "_netdev"},
41 true,
42 []string{"bind", "_netdev"},
43 []string{"bind", "remount", "vers=2", "ro", "_netdev"},
44 },
45 }
46 for _, test := range tests {
47 bind, bindOpts, bindRemountOpts := MakeBindOpts(test.mountOption)
48 if bind != test.isBind {
49 t.Errorf("Expected bind to be %v but got %v", test.isBind, bind)
50 }
51 if test.isBind {
52 if !reflect.DeepEqual(test.expectedBindOpts, bindOpts) {
53 t.Errorf("Expected bind mount options to be %+v got %+v", test.expectedBindOpts, bindOpts)
54 }
55 if !reflect.DeepEqual(test.expectedRemountOpts, bindRemountOpts) {
56 t.Errorf("Expected remount options to be %+v got %+v", test.expectedRemountOpts, bindRemountOpts)
57 }
58 }
59
60 }
61 }
62
63 func TestMakeBindOptsSensitive(t *testing.T) {
64 tests := []struct {
65 mountOptions []string
66 sensitiveMountOptions []string
67 isBind bool
68 expectedBindOpts []string
69 expectedRemountOpts []string
70 expectedSensitiveRemountOpts []string
71 }{
72 {
73 mountOptions: []string{"vers=2", "ro", "_netdev"},
74 sensitiveMountOptions: []string{"user=foo", "pass=bar"},
75 isBind: false,
76 expectedBindOpts: []string{},
77 expectedRemountOpts: []string{},
78 expectedSensitiveRemountOpts: []string{"user=foo", "pass=bar"},
79 },
80 {
81
82 mountOptions: []string{"vers=2", "ro", "_netdev"},
83 sensitiveMountOptions: []string{"user=foo", "pass=bar", "bind"},
84 isBind: true,
85 expectedBindOpts: []string{"bind", "_netdev"},
86 expectedRemountOpts: []string{"bind", "remount", "vers=2", "ro", "_netdev"},
87 expectedSensitiveRemountOpts: []string{"user=foo", "pass=bar"},
88 },
89 {
90 mountOptions: []string{"vers=2", "remount", "ro", "_netdev"},
91 sensitiveMountOptions: []string{"user=foo", "pass=bar"},
92 isBind: false,
93 expectedBindOpts: []string{},
94 expectedRemountOpts: []string{},
95 expectedSensitiveRemountOpts: []string{"user=foo", "pass=bar"},
96 },
97 {
98 mountOptions: []string{"vers=2", "ro", "_netdev"},
99 sensitiveMountOptions: []string{"user=foo", "pass=bar", "remount"},
100 isBind: false,
101 expectedBindOpts: []string{},
102 expectedRemountOpts: []string{},
103 expectedSensitiveRemountOpts: []string{"user=foo", "pass=bar"},
104 },
105 {
106
107 mountOptions: []string{"vers=2", "bind", "ro", "_netdev"},
108 sensitiveMountOptions: []string{"user=foo", "remount", "pass=bar"},
109 isBind: true,
110 expectedBindOpts: []string{"bind", "_netdev"},
111 expectedRemountOpts: []string{"bind", "remount", "vers=2", "ro", "_netdev"},
112 expectedSensitiveRemountOpts: []string{"user=foo", "pass=bar"},
113 },
114 {
115
116 mountOptions: []string{"vers=2", "bind", "ro", "_netdev"},
117 sensitiveMountOptions: []string{"user=foo", "remount", "pass=bar"},
118 isBind: true,
119 expectedBindOpts: []string{"bind", "_netdev"},
120 expectedRemountOpts: []string{"bind", "remount", "vers=2", "ro", "_netdev"},
121 expectedSensitiveRemountOpts: []string{"user=foo", "pass=bar"},
122 },
123 }
124 for _, test := range tests {
125 bind, bindOpts, bindRemountOpts, bindRemountSensitiveOpts := MakeBindOptsSensitive(test.mountOptions, test.sensitiveMountOptions)
126 if bind != test.isBind {
127 t.Errorf("Expected bind to be %v but got %v", test.isBind, bind)
128 }
129 if test.isBind {
130 if !reflect.DeepEqual(test.expectedBindOpts, bindOpts) {
131 t.Errorf("Expected bind mount options to be %+v got %+v", test.expectedBindOpts, bindOpts)
132 }
133 if !reflect.DeepEqual(test.expectedRemountOpts, bindRemountOpts) {
134 t.Errorf("Expected remount options to be %+v got %+v", test.expectedRemountOpts, bindRemountOpts)
135 }
136 if !reflect.DeepEqual(test.expectedSensitiveRemountOpts, bindRemountSensitiveOpts) {
137 t.Errorf("Expected sensitive remount options to be %+v got %+v", test.expectedSensitiveRemountOpts, bindRemountSensitiveOpts)
138 }
139 }
140
141 }
142 }
143
144 func TestOptionsForLogging(t *testing.T) {
145 // Arrange
146 testcases := []struct {
147 options []string
148 sensitiveOptions []string
149 }{
150 {
151 options: []string{"o1", "o2"},
152 sensitiveOptions: []string{"s1"},
153 },
154 {
155 options: []string{"o1", "o2"},
156 sensitiveOptions: []string{"s1", "s2"},
157 },
158 {
159 sensitiveOptions: []string{"s1", "s2"},
160 },
161 {
162 options: []string{"o1", "o2"},
163 },
164 {},
165 }
166
167 for _, v := range testcases {
168 // Act
169 maskedStr := sanitizedOptionsForLogging(v.options, v.sensitiveOptions)
170
171 // Assert
172 for _, sensitiveOption := range v.sensitiveOptions {
173 if strings.Contains(maskedStr, sensitiveOption) {
174 t.Errorf("Found sensitive log option %q in %q", sensitiveOption, maskedStr)
175 }
176 }
177
178 actualCount := strings.Count(maskedStr, sensitiveOptionsRemoved)
179 expectedCount := len(v.sensitiveOptions)
180 if actualCount != expectedCount {
181 t.Errorf("Found %v instances of %q in %q. Expected %v", actualCount, sensitiveOptionsRemoved, maskedStr, expectedCount)
182 }
183 }
184 }