github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/rkt/flag/rktflag_test.go (about) 1 // Copyright 2015 The rkt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package flag 16 17 import ( 18 "strings" 19 "testing" 20 ) 21 22 var options = []string{"zero", "one", "two"} 23 24 func TestOptionList(t *testing.T) { 25 tests := []struct { 26 opts string 27 ex string 28 err bool 29 }{ 30 { 31 opts: "zero,two", 32 ex: "zero,two", 33 err: false, 34 }, 35 { // Duplicate test 36 opts: "one,two,two", 37 ex: "", 38 err: true, 39 }, 40 { // Not permissible test 41 opts: "one,two,three", 42 ex: "", 43 err: true, 44 }, 45 { // Empty string 46 opts: "", 47 ex: "", 48 err: false, 49 }, 50 } 51 52 for i, tt := range tests { 53 // test NewOptionsList 54 if _, err := NewOptionList(options, tt.opts); (err != nil) != tt.err { 55 t.Errorf("test %d: unexpected error in NewOptionList: %v", i, err) 56 } 57 58 // test OptionList.Set() 59 ol, err := NewOptionList(options, strings.Join(options, ",")) 60 if err != nil { 61 t.Errorf("test %d: unexpected error preparing test: %v", i, err) 62 } 63 64 if err := ol.Set(tt.opts); (err != nil) != tt.err { 65 t.Errorf("test %d: could not parse options as expected: %v", i, err) 66 } 67 if tt.ex != "" && tt.ex != ol.String() { 68 t.Errorf("test %d: resulting options not as expected: %s != %s", 69 i, tt.ex, ol.String()) 70 } 71 } 72 } 73 74 var bfMap = map[string]int{ 75 options[0]: 0, 76 options[1]: 1, 77 options[2]: 1 << 1, 78 } 79 80 func TestBitFlags(t *testing.T) { 81 tests := []struct { 82 opts string 83 ex int 84 parseErr bool 85 logicErr bool 86 }{ 87 { 88 opts: "one,two", 89 ex: 3, 90 }, 91 { // Duplicate test 92 opts: "zero,two,two", 93 ex: -1, 94 parseErr: true, 95 }, 96 { // Not included test 97 opts: "zero,two,three", 98 ex: -1, 99 parseErr: true, 100 }, 101 { // Test 10 in 11 102 opts: "one,two", 103 ex: 1, 104 }, 105 { // Test 11 not in 01 106 opts: "one", 107 ex: 3, 108 logicErr: true, 109 }, 110 } 111 112 for i, tt := range tests { 113 // test newBitFlags 114 if _, err := newBitFlags(options, tt.opts, bfMap); (err != nil) != tt.parseErr { 115 t.Errorf("test %d: unexpected error in newBitFlags: %v", i, err) 116 } 117 118 bf, err := newBitFlags(options, strings.Join(options, ","), bfMap) 119 if err != nil { 120 t.Errorf("test %d: unexpected error preparing test: %v", i, err) 121 } 122 123 // test bitFlags.Set() 124 if err := bf.Set(tt.opts); (err != nil) != tt.parseErr { 125 t.Errorf("test %d: Could not parse options as expected: %v", i, err) 126 } 127 if tt.ex >= 0 && bf.hasFlag(tt.ex) == tt.logicErr { 128 t.Errorf("test %d: Result was unexpected: %d != %d", 129 i, tt.ex, bf.flags) 130 } 131 } 132 } 133 134 func TestSecFlags(t *testing.T) { 135 tests := []struct { 136 opts string 137 image bool 138 tls bool 139 onDisk bool 140 http bool 141 err bool 142 }{ 143 { 144 opts: "none", 145 image: false, 146 tls: false, 147 onDisk: false, 148 http: false, 149 }, 150 { 151 opts: "image", 152 image: true, 153 tls: false, 154 onDisk: false, 155 http: false, 156 }, 157 { 158 opts: "tls", 159 image: false, 160 tls: true, 161 onDisk: false, 162 http: false, 163 }, 164 { 165 opts: "onDisk", 166 image: false, 167 tls: false, 168 onDisk: true, 169 http: false, 170 }, 171 { 172 opts: "http", 173 image: false, 174 tls: false, 175 onDisk: false, 176 http: true, 177 }, 178 { 179 opts: "all", 180 image: true, 181 tls: true, 182 onDisk: true, 183 http: true, 184 }, 185 { 186 opts: "image,tls", 187 image: true, 188 tls: true, 189 onDisk: false, 190 http: false, 191 }, 192 { 193 opts: "i-am-sure-we-will-not-get-this-insecure-flag", 194 err: true, 195 }, 196 } 197 198 for i, tt := range tests { 199 sf, err := NewSecFlags(tt.opts) 200 if err != nil && !tt.err { 201 t.Errorf("test %d: unexpected error in NewSecFlags: %v", i, err) 202 } else if err == nil && tt.err { 203 t.Errorf("test %d: unexpected success in NewSecFlags for options %q", i, tt.opts) 204 } 205 if err != nil { 206 continue 207 } 208 209 if got := sf.SkipImageCheck(); tt.image != got { 210 t.Errorf("test %d: expected image skip to be %v, got %v", i, tt.image, got) 211 } 212 213 if got := sf.SkipTLSCheck(); tt.tls != got { 214 t.Errorf("test %d: expected tls skip to be %v, got %v", i, tt.tls, got) 215 } 216 217 if got := sf.SkipOnDiskCheck(); tt.onDisk != got { 218 t.Errorf("test %d: expected on disk skip to be %v, got %v", i, tt.onDisk, got) 219 } 220 221 if got := sf.AllowHTTP(); tt.http != got { 222 t.Errorf("test %d: expected http allowed to be %v, got %v", i, tt.http, got) 223 } 224 225 all := tt.http && tt.onDisk && tt.tls && tt.image 226 if got := sf.SkipAllSecurityChecks(); all != got { 227 t.Errorf("test %d: expected all skip to be %v, got %v", i, all, got) 228 } 229 230 any := tt.http || tt.onDisk || tt.tls || tt.image 231 if got := sf.SkipAnySecurityChecks(); any != got { 232 t.Errorf("test %d: expected all skip to be %v, got %v", i, any, got) 233 } 234 } 235 }