-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanonymize_test.go
106 lines (83 loc) · 2.27 KB
/
anonymize_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package goanonymizer
import (
"crypto/sha256"
"encoding/hex"
"testing"
)
type Example struct {
// Anonymize with asterisk (*)
MaskedField string `anonymize:"asterisk"`
// Anonymize to empty string ("")
EmptyField string `anonymize:"empty"`
// Anonymize with custom handler
CustomField string `anonymize:"mysha256"`
// Don't anonymize
PlainField string
NestedField
ArrayField []string `anonymize:"asterisk"`
}
type NestedField struct {
MaskedField string `anonymize:"asterisk"`
}
var example = Example{
MaskedField: "MaskedField",
EmptyField: "EmptystringField",
CustomField: "CustomField",
PlainField: "PlainField",
NestedField: NestedField{
MaskedField: "NestedMaskedField",
},
ArrayField: []string{"Field 1", "Field 2"},
}
func TestAnonymization(t *testing.T) {
target := example
oMaskedField := target.MaskedField
oEmptyField := target.EmptyField
oCustomField := target.CustomField
oPlainField := target.PlainField
oNestedMaskedField := target.NestedField.MaskedField
err := Anonymize(&target)
if err != nil {
t.Error(err)
}
if target.MaskedField != asteriskReplacer(oMaskedField) {
t.Error("target.CustomField != sha256Replacer(oCustomField)")
}
if target.EmptyField != emptystringReplacer(oEmptyField) {
t.Error("target.EmptyField != emptystringReplacer(oEmptyField)")
}
if target.CustomField != oCustomField {
t.Error("target.CustomField != oCustomField")
}
if target.PlainField != oPlainField {
t.Error("target.PlainField != oPlainField")
}
if target.NestedField.MaskedField != asteriskReplacer(oNestedMaskedField) {
t.Error("target.NestedField.MaskedField != oNestedMaskedField")
}
}
func TestTargetIsNotPointer(t *testing.T) {
target := example
err := Anonymize(target)
if err.Error() != "target is not pointer" {
t.Error(err)
}
}
func TestCustomReplacer(t *testing.T) {
target := example
oCustomField := target.CustomField
sha256Replacer := func(source string) string {
h := sha256.New()
h.Write([]byte(source))
return hex.EncodeToString(h.Sum(nil))
}
AddCustomReplacer("mysha256", sha256Replacer)
err := Anonymize(&target)
if err != nil {
t.Error(err)
}
RemoveCustomReplacer("mysha256")
if target.CustomField != sha256Replacer(oCustomField) {
t.Error("target.CustomField != sha256Replacer(oCustomField)")
}
}