github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/controller/pkg/usertokens/common/common_test.go (about) 1 package common 2 3 import ( 4 "testing" 5 6 . "github.com/smartystreets/assertions" 7 ) 8 9 func TestFlattenClaim(t *testing.T) { 10 type args struct { 11 key string 12 claim interface{} 13 } 14 tests := []struct { 15 name string 16 args args 17 want []string 18 notwant []string 19 }{ 20 { 21 name: "test-slice", 22 args: args{ 23 key: "slicekey", 24 claim: []interface{}{"claim1", "claim2"}, 25 }, 26 want: []string{"slicekey=claim1", "slicekey=claim2"}, 27 }, 28 { 29 name: "test-string", 30 args: args{key: "testkey1", claim: "testclaim1"}, 31 want: []string{"testkey1=testclaim1"}, 32 }, 33 { 34 name: "test-empty-string", 35 args: args{key: "testkey1", claim: ""}, 36 want: []string{"testkey1="}, 37 }, 38 { 39 name: "test-bool-true", 40 args: args{key: "key", claim: true}, 41 want: []string{"key=true"}, 42 }, 43 { 44 name: "test-bool-false", 45 args: args{key: "key", claim: false}, 46 want: []string{"key=false"}, 47 }, 48 { 49 name: "test-bool-not-true", 50 args: args{key: "key", claim: true}, 51 notwant: []string{"key=false"}, 52 }, 53 { 54 name: "test-bool-not-false", 55 args: args{key: "key", claim: false}, 56 notwant: []string{"key=true"}, 57 }, 58 { 59 name: "test-string-slice-succeed", 60 args: args{key: "key", claim: []string{"claim1", "claim2"}}, 61 want: []string{"key=claim1", "key=claim2"}, 62 }, 63 { 64 name: "test-string-slice-fail", 65 args: args{key: "key", claim: []string{"claim1", "claim2"}}, 66 notwant: []string{"key=claim3", "key=claim4"}, 67 }, 68 { 69 name: "test-map-string-string", 70 args: args{key: "testkey", claim: map[string]interface{}{"key1": "value1", "key2": "value2"}}, 71 want: []string{"testkey:key1=value1", "testkey:key2=value2"}, 72 }, 73 { 74 name: "test-map-string-int", 75 args: args{key: "testkey", claim: map[string]interface{}{"key1": 1, "key2": 2}}, 76 want: []string{"testkey:key1=1", "testkey:key2=2"}, 77 }, 78 { 79 name: "test-map-string-bool", 80 args: args{key: "testkey", claim: map[string]interface{}{"key1": true, "key2": false}}, 81 want: []string{"testkey:key1=true", "testkey:key2=false"}, 82 }, 83 { 84 name: "test-map-string-slice", 85 args: args{key: "rootkey", claim: map[string]interface{}{"key1": []string{"val1", "val2"}}}, 86 want: []string{"rootkey:key1=val1", "rootkey:key1=val2"}, 87 }, 88 { 89 name: "test-map-string-map", 90 args: args{ 91 key: "rootkey", 92 claim: map[string]interface{}{ 93 "level1": map[string]interface{}{ 94 "key1": "val1", 95 }, 96 "level2": map[string]interface{}{ 97 "key2": "val2", 98 }, 99 }, 100 }, 101 want: []string{"rootkey:level1:key1=val1", "rootkey:level2:key2=val2"}, 102 }, 103 { 104 name: "test-positive-int", 105 args: args{key: "key", claim: int(1)}, 106 want: []string{"key=1"}, 107 }, 108 { 109 name: "test-positive-int8", 110 args: args{key: "key", claim: int8(1)}, 111 want: []string{"key=1"}, 112 }, 113 { 114 name: "test-positive-int16", 115 args: args{key: "key", claim: int16(1)}, 116 want: []string{"key=1"}, 117 }, 118 { 119 name: "test-positive-int32", 120 args: args{key: "key", claim: int32(1)}, 121 want: []string{"key=1"}, 122 }, 123 { 124 name: "test-positive-int64", 125 args: args{key: "key", claim: int64(1)}, 126 want: []string{"key=1"}, 127 }, 128 { 129 name: "test-negative-int", 130 args: args{key: "key", claim: int(-1)}, 131 want: []string{"key=-1"}, 132 }, 133 { 134 name: "test-uint", 135 args: args{key: "key", claim: uint(1)}, 136 want: []string{"key=1"}, 137 }, 138 { 139 name: "test-uint8", 140 args: args{key: "key", claim: uint8(1)}, 141 want: []string{"key=1"}, 142 }, 143 { 144 name: "test-uint16", 145 args: args{key: "key", claim: uint16(1)}, 146 want: []string{"key=1"}, 147 }, 148 { 149 name: "test-uint32", 150 args: args{key: "key", claim: uint32(1)}, 151 want: []string{"key=1"}, 152 }, 153 { 154 name: "test-uint64", 155 args: args{key: "key", claim: uint64(1)}, 156 want: []string{"key=1"}, 157 }, 158 { 159 name: "test-float32-small", 160 args: args{key: "key", claim: float32(3.14)}, 161 want: []string{"key=3.14"}, 162 }, 163 { 164 name: "test-negative-float32-small", 165 args: args{key: "key", claim: float32(-3.14)}, 166 want: []string{"key=-3.14"}, 167 }, 168 { 169 name: "test-float32-rounding", 170 args: args{key: "key", claim: float32(3.141592654)}, 171 want: []string{"key=3.1415927"}, 172 }, 173 { 174 name: "test-negative-float32-rounding", 175 args: args{key: "key", claim: float32(-3.141592654)}, 176 want: []string{"key=-3.1415927"}, 177 }, 178 { 179 name: "test-float32-exponent", 180 args: args{key: "key", claim: float32(3.141592654e22)}, 181 want: []string{"key=3.1415927E+22"}, 182 }, 183 { 184 name: "test-float32-negative-exponent", 185 args: args{key: "key", claim: float32(3.141592654e-22)}, 186 want: []string{"key=3.1415927E-22"}, 187 }, 188 { 189 name: "test-negative-float32-exponent", 190 args: args{key: "key", claim: float32(-3.141592654e22)}, 191 want: []string{"key=-3.1415927E+22"}, 192 }, 193 { 194 name: "test-negative-float32-negative-exponent", 195 args: args{key: "key", claim: float32(-3.141592654e-22)}, 196 want: []string{"key=-3.1415927E-22"}, 197 }, 198 { 199 name: "test-float64-small", 200 args: args{key: "key", claim: float64(3.14)}, 201 want: []string{"key=3.14"}, 202 }, 203 { 204 name: "test-negative-float64-small", 205 args: args{key: "key", claim: float64(-3.14)}, 206 want: []string{"key=-3.14"}, 207 }, 208 { 209 name: "test-float64-rounding", 210 args: args{key: "key", claim: float64(1.412135623730950488)}, 211 want: []string{"key=1.4121356237309506"}, 212 }, 213 { 214 name: "test-negative-float64-rounding", 215 args: args{key: "key", claim: float64(-1.412135623730950488)}, 216 want: []string{"key=-1.4121356237309506"}, 217 }, 218 { 219 name: "test-float64-exponent", 220 args: args{key: "key", claim: float64(1.41213562373095e22)}, 221 want: []string{"key=1.41213562373095E+22"}, 222 }, 223 { 224 name: "test-float64-negative-exponent", 225 args: args{key: "key", claim: float64(1.41213562373095e-22)}, 226 want: []string{"key=1.41213562373095E-22"}, 227 }, 228 { 229 name: "test-negative-float64-exponent", 230 args: args{key: "key", claim: float64(-1.41213562373095e22)}, 231 want: []string{"key=-1.41213562373095E+22"}, 232 }, 233 { 234 name: "test-negative-float64-negative-exponent", 235 args: args{key: "key", claim: float64(-1.41213562373095e-22)}, 236 want: []string{"key=-1.41213562373095E-22"}, 237 }, 238 { 239 name: "test-map-string-float", 240 args: args{key: "test", claim: map[string]interface{}{"key1": 3.1415, "key2": -1.21e+33}}, 241 want: []string{"test:key1=3.1415", "test:key2=-1.21E+33"}, 242 }, 243 { 244 name: "test-map-string-mixed", 245 args: args{key: "test", claim: map[string]interface{}{"key1": true, "key2": -1.21e+33, "key3": "val3"}}, 246 want: []string{"test:key1=true", "test:key2=-1.21E+33", "test:key3=val3"}, 247 }, 248 } 249 for _, tt := range tests { 250 t.Run(tt.name, func(t *testing.T) { 251 got := FlattenClaim(tt.args.key, tt.args.claim) 252 t.Logf("test: %s, got: %v\n", tt.name, got) 253 for _, want := range tt.want { 254 if ok, errStr := So(got, ShouldContain, want); !ok { 255 t.Errorf("%s", errStr) 256 } 257 } 258 for _, notwant := range tt.notwant { 259 if ok, errStr := So(got, ShouldNotContain, notwant); !ok { 260 t.Errorf("%s", errStr) 261 } 262 } 263 }) 264 } 265 } 266 267 func Test_toInt64(t *testing.T) { 268 type args struct { 269 i interface{} 270 } 271 tests := []struct { 272 name string 273 args args 274 want int64 275 wantPanic bool 276 }{ 277 { 278 name: "test-toInt64-int", 279 args: args{i: int(1)}, 280 want: int64(1), 281 }, 282 { 283 name: "test-toInt64-int8", 284 args: args{i: int8(1)}, 285 want: int64(1), 286 }, 287 { 288 name: "test-toInt64-int16", 289 args: args{i: int16(1)}, 290 want: int64(1), 291 }, 292 { 293 name: "test-toInt64-int32", 294 args: args{i: int32(1)}, 295 want: int64(1), 296 }, 297 { 298 name: "test-toInt64-int64", 299 args: args{i: int64(1)}, 300 want: int64(1), 301 }, 302 { 303 name: "test-toInt64-string", 304 args: args{i: "a string"}, 305 want: int64(1), 306 wantPanic: true, 307 }, 308 } 309 for _, tt := range tests { 310 t.Run(tt.name, func(t *testing.T) { 311 defer func() { 312 r := recover() 313 if (r != nil) != tt.wantPanic { 314 t.Errorf("toInt64() recover = %v, wantPanic = %v", r, tt.wantPanic) 315 } 316 }() 317 if got := toInt64(tt.args.i); got != tt.want { 318 t.Errorf("toInt64() = %v, want %v", got, tt.want) 319 } else { 320 t.Logf("toInt64(): test: %s PASS, want %v, got: %v\n", tt.name, tt.want, got) 321 } 322 }) 323 } 324 } 325 326 func Test_toUint64(t *testing.T) { 327 type args struct { 328 i interface{} 329 } 330 tests := []struct { 331 name string 332 args args 333 want uint64 334 wantPanic bool 335 }{ 336 { 337 name: "test-toUint64-int", 338 args: args{i: uint(1)}, 339 want: uint64(1), 340 }, 341 { 342 name: "test-toUint64-int8", 343 args: args{i: uint8(1)}, 344 want: uint64(1), 345 }, 346 { 347 name: "test-toUint64-int16", 348 args: args{i: uint16(1)}, 349 want: uint64(1), 350 }, 351 { 352 name: "test-toUint64-int32", 353 args: args{i: uint32(1)}, 354 want: uint64(1), 355 }, 356 { 357 name: "test-toUint64-int64", 358 args: args{i: uint64(1)}, 359 want: uint64(1), 360 }, 361 { 362 name: "test-toUint64-string", 363 args: args{i: "a string"}, 364 want: uint64(1), 365 wantPanic: true, 366 }, 367 } 368 for _, tt := range tests { 369 t.Run(tt.name, func(t *testing.T) { 370 defer func() { 371 r := recover() 372 if (r != nil) != tt.wantPanic { 373 t.Errorf("toUint64() recover = %v, wantPanic = %v", r, tt.wantPanic) 374 } 375 }() 376 if got := toUint64(tt.args.i); got != tt.want { 377 t.Errorf("toUint64() = %v, want %v", got, tt.want) 378 } else { 379 t.Logf("toUint64(): test: %s PASS, want %v, got: %v\n", tt.name, tt.want, got) 380 } 381 }) 382 } 383 }