| 32 | } |
| 33 | |
| 34 | func TestUD(t *testing.T) { |
| 35 | RedactUserData = true |
| 36 | defer func() { RedactUserData = defaultRedactUserData }() |
| 37 | |
| 38 | // Straight-forward string test. |
| 39 | ud := UD("hello world") |
| 40 | assert.Equal(t, UserDataPrefix+"hello world"+UserDataSuffix, ud.Redact()) |
| 41 | |
| 42 | // big.Int fulfils the Stringer interface, so we should get sensible values. |
| 43 | ud = UD(big.NewInt(1234)) |
| 44 | assert.Equal(t, UserDataPrefix+"1234"+UserDataSuffix, ud.Redact()) |
| 45 | |
| 46 | // Even plain structs could be redactable. |
| 47 | ud = UD(struct{}{}) |
| 48 | assert.Equal(t, UserDataPrefix+"{}"+UserDataSuffix, ud.Redact()) |
| 49 | |
| 50 | // String slice test. |
| 51 | ud = UD([]string{"hello", "world", "o/"}) |
| 52 | assert.Equal(t, "[ "+UserDataPrefix+"hello"+UserDataSuffix+" "+UserDataPrefix+"world"+UserDataSuffix+" "+UserDataPrefix+"o/"+UserDataSuffix+" ]", ud.Redact()) |
| 53 | |
| 54 | // Set |
| 55 | ud = UD(SetOf("hello", "world")) |
| 56 | // As a set comes from a map we can't be sure which order it'll end up with so should check both permutations |
| 57 | redactedPerm1 := "{" + UserDataPrefix + "hello" + UserDataSuffix + ", " + UserDataPrefix + "world" + UserDataSuffix + "}" |
| 58 | redactedPerm2 := "{" + UserDataPrefix + "world" + UserDataSuffix + ", " + UserDataPrefix + "hello" + UserDataSuffix + "}" |
| 59 | redactedSet := ud.Redact() |
| 60 | redactedCorrectly := redactedPerm1 == redactedSet || redactedPerm2 == redactedSet |
| 61 | assert.True(t, redactedCorrectly, "Unexpected redact got %v", redactedSet) |
| 62 | } |
| 63 | |
| 64 | func BenchmarkUserDataRedact(b *testing.B) { |
| 65 | defer func() { RedactUserData = defaultRedactUserData }() |