|
5 | 5 | package scrubber
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "encoding/json" |
8 | 9 | "math/rand"
|
9 | 10 | "testing"
|
10 | 11 |
|
@@ -68,6 +69,10 @@ type TrustedStructToTest struct {
|
68 | 69 | StructToTest
|
69 | 70 | }
|
70 | 71 |
|
| 72 | +type TestWrap struct { |
| 73 | + Test *StructToTest |
| 74 | +} |
| 75 | + |
71 | 76 | type UnexportedStructToTest struct {
|
72 | 77 | Exported string
|
73 | 78 | unexportedPtr *string
|
@@ -293,6 +298,171 @@ func TestJSON(t *testing.T) {
|
293 | 298 | }
|
294 | 299 | }
|
295 | 300 |
|
| 301 | +func TestDeepCopyStruct(t *testing.T) { |
| 302 | + type Expectation struct { |
| 303 | + Error string |
| 304 | + Result any |
| 305 | + } |
| 306 | + tests := []struct { |
| 307 | + Name string |
| 308 | + Struct any |
| 309 | + Expectation Expectation |
| 310 | + CmpOpts []cmp.Option |
| 311 | + }{ |
| 312 | + { |
| 313 | + Name: "basic happy path", |
| 314 | + Struct: &struct { |
| 315 | + Username string |
| 316 | + Email string |
| 317 | + Password string |
| 318 | + WorkspaceID string |
| 319 | + LeaveMeAlone string |
| 320 | + }{ Username: "foo", Email: "[email protected]", Password: "foobar", WorkspaceID: "gitpodio-gitpod-uesaddev73c", LeaveMeAlone: "foo"}, |
| 321 | + Expectation: Expectation{ |
| 322 | + Result: &struct { |
| 323 | + Username string |
| 324 | + Email string |
| 325 | + Password string |
| 326 | + WorkspaceID string |
| 327 | + LeaveMeAlone string |
| 328 | + }{Username: "[redacted:md5:acbd18db4cc2f85cedef654fccc4a4d8]", Email: "[redacted]", Password: "[redacted]", WorkspaceID: "[redacted:md5:a35538939333def8477b5c19ac694b35]", LeaveMeAlone: "foo"}, |
| 329 | + }, |
| 330 | + }, |
| 331 | + { |
| 332 | + Name: "stuct without pointer", |
| 333 | + Struct: struct { |
| 334 | + Username string |
| 335 | + Email string |
| 336 | + Password string |
| 337 | + WorkspaceID string |
| 338 | + LeaveMeAlone string |
| 339 | + }{ Username: "foo", Email: "[email protected]", Password: "foobar", WorkspaceID: "gitpodio-gitpod-uesaddev73c", LeaveMeAlone: "foo"}, |
| 340 | + Expectation: Expectation{ |
| 341 | + Result: struct { |
| 342 | + Username string |
| 343 | + Email string |
| 344 | + Password string |
| 345 | + WorkspaceID string |
| 346 | + LeaveMeAlone string |
| 347 | + }{Username: "[redacted:md5:acbd18db4cc2f85cedef654fccc4a4d8]", Email: "[redacted]", Password: "[redacted]", WorkspaceID: "[redacted:md5:a35538939333def8477b5c19ac694b35]", LeaveMeAlone: "foo"}, |
| 348 | + }, |
| 349 | + }, |
| 350 | + { |
| 351 | + Name: "map field", |
| 352 | + Struct: &struct { |
| 353 | + WithMap map[string]interface{} |
| 354 | + }{ |
| 355 | + WithMap: map[string]interface{}{ |
| 356 | + |
| 357 | + }, |
| 358 | + }, |
| 359 | + Expectation: Expectation{ |
| 360 | + Result: &struct{ WithMap map[string]any }{WithMap: map[string]any{"email": string("[redacted]")}}, |
| 361 | + }, |
| 362 | + }, |
| 363 | + { |
| 364 | + Name: "slices", |
| 365 | + Struct: &struct { |
| 366 | + Slice []string |
| 367 | + }{ Slice: [] string{ "foo", "bar", "[email protected]"}}, |
| 368 | + Expectation: Expectation{ |
| 369 | + Result: &struct { |
| 370 | + Slice []string |
| 371 | + }{Slice: []string{"foo", "bar", "[redacted:email]"}}, |
| 372 | + }, |
| 373 | + }, |
| 374 | + { |
| 375 | + Name: "struct tags", |
| 376 | + Struct: &struct { |
| 377 | + Hashed string `scrub:"hash"` |
| 378 | + Redacted string `scrub:"redact"` |
| 379 | + Email string `scrub:"ignore"` |
| 380 | + }{ |
| 381 | + Hashed: "foo", |
| 382 | + Redacted: "foo", |
| 383 | + Email: "foo", |
| 384 | + }, |
| 385 | + Expectation: Expectation{ |
| 386 | + Result: &struct { |
| 387 | + Hashed string `scrub:"hash"` |
| 388 | + Redacted string `scrub:"redact"` |
| 389 | + Email string `scrub:"ignore"` |
| 390 | + }{ |
| 391 | + Hashed: "[redacted:md5:acbd18db4cc2f85cedef654fccc4a4d8]", |
| 392 | + Redacted: "[redacted]", |
| 393 | + Email: "foo", |
| 394 | + }, |
| 395 | + }, |
| 396 | + }, |
| 397 | + { |
| 398 | + Name: "trusted struct", |
| 399 | + Struct: scrubStructToTest(&StructToTest{ |
| 400 | + Username: "foo", |
| 401 | + |
| 402 | + Password: "foobar", |
| 403 | + }), |
| 404 | + Expectation: Expectation{ |
| 405 | + Result: &TrustedStructToTest{ |
| 406 | + StructToTest: StructToTest{ |
| 407 | + Username: "foo", |
| 408 | + Email: "trusted:[redacted:email]", |
| 409 | + Password: "trusted:[redacted]", |
| 410 | + }, |
| 411 | + }, |
| 412 | + }, |
| 413 | + }, |
| 414 | + { |
| 415 | + Name: "trusted interface", |
| 416 | + Struct: scrubStructToTestAsTrustedValue(&StructToTest{ |
| 417 | + Username: "foo", |
| 418 | + |
| 419 | + Password: "foobar", |
| 420 | + }), |
| 421 | + Expectation: Expectation{ |
| 422 | + Result: &TrustedStructToTest{ |
| 423 | + StructToTest: StructToTest{ |
| 424 | + Username: "foo", |
| 425 | + Email: "trusted:[redacted:email]", |
| 426 | + Password: "trusted:[redacted]", |
| 427 | + }, |
| 428 | + }, |
| 429 | + }, |
| 430 | + }, |
| 431 | + { |
| 432 | + Name: "contains unexported pointers", |
| 433 | + Struct: UnexportedStructToTest{ |
| 434 | + Exported: "foo", |
| 435 | + unexportedPtr: nil, |
| 436 | + }, |
| 437 | + Expectation: Expectation{ |
| 438 | + Result: UnexportedStructToTest{ |
| 439 | + Exported: "foo", |
| 440 | + unexportedPtr: nil, |
| 441 | + }, |
| 442 | + }, |
| 443 | + CmpOpts: []cmp.Option{cmpopts.IgnoreUnexported(UnexportedStructToTest{})}, |
| 444 | + }, |
| 445 | + } |
| 446 | + |
| 447 | + for _, test := range tests { |
| 448 | + t.Run(test.Name, func(t *testing.T) { |
| 449 | + var act Expectation |
| 450 | + b, _ := json.Marshal(test.Struct) |
| 451 | + |
| 452 | + act.Result = Default.DeepCopyStruct(test.Struct) |
| 453 | + b2, _ := json.Marshal(test.Struct) |
| 454 | + |
| 455 | + if diff := cmp.Diff(b, b2, test.CmpOpts...); diff != "" { |
| 456 | + t.Errorf("DeepCopyStruct for origin struct modified (-want +got):\n%s", diff) |
| 457 | + } |
| 458 | + |
| 459 | + if diff := cmp.Diff(test.Expectation, act, test.CmpOpts...); diff != "" { |
| 460 | + t.Errorf("DeepCopyStruct() mismatch (-want +got):\n%s", diff) |
| 461 | + } |
| 462 | + }) |
| 463 | + } |
| 464 | +} |
| 465 | + |
296 | 466 | func BenchmarkKeyValue(b *testing.B) {
|
297 | 467 | key := HashedFieldNames[rand.Intn(len(HashedFieldNames))]
|
298 | 468 |
|
|
0 commit comments