-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. 数据脱敏策略单元测试代码通过
- Loading branch information
Showing
7 changed files
with
226 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 105 additions & 1 deletion
106
drivers/strategy/data-mask-strategy/mask/json-path/json-path_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,111 @@ | ||
package json_path | ||
|
||
import "testing" | ||
import ( | ||
"testing" | ||
|
||
"github.com/eolinker/apinto/drivers/strategy/data-mask-strategy/mask" | ||
) | ||
|
||
func maskFunc(input string) string { | ||
return "***" // Simple mask function that replaces the input with asterisks | ||
} | ||
|
||
func TestJsonPath(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
rule *mask.Rule | ||
input string | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Simple Mask", | ||
rule: &mask.Rule{ | ||
Match: &mask.BasicItem{ | ||
Type: mask.MatchJsonPath, | ||
Value: "$.password", | ||
}, | ||
Mask: &mask.Mask{}, | ||
}, | ||
input: `{"user":"john","password":"secret123"}`, | ||
want: `{"user":"john","password":"***"}`, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "No Match", | ||
rule: &mask.Rule{ | ||
Match: &mask.BasicItem{ | ||
Type: mask.MatchJsonPath, | ||
Value: "$.notpresent", | ||
}, | ||
Mask: &mask.Mask{}, | ||
}, | ||
input: `{"user":"john","password":"secret123"}`, | ||
want: `{"user":"john","password":"secret123"}`, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Invalid JSON", | ||
rule: &mask.Rule{ | ||
Match: &mask.BasicItem{ | ||
Type: mask.MatchJsonPath, | ||
Value: "$.password", | ||
}, | ||
Mask: &mask.Mask{}, | ||
}, | ||
input: `{"user":"john","password": "secret123"`, | ||
want: "", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Invalid JSONPath", | ||
rule: &mask.Rule{ | ||
Match: &mask.BasicItem{ | ||
Type: mask.MatchJsonPath, | ||
Value: "$[password[", | ||
}, | ||
Mask: &mask.Mask{}, | ||
}, | ||
input: `{"user":"john","password":"secret123"}`, | ||
want: "", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Long JSON String", | ||
rule: &mask.Rule{ | ||
Match: &mask.BasicItem{ | ||
Type: mask.MatchJsonPath, | ||
Value: "$.longKey", | ||
}, | ||
Mask: &mask.Mask{}, | ||
}, | ||
input: `{"longKey":"1234567890123456789012345678901234567890"}`, | ||
want: `{"longKey":"***"}`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Log(tt.input) | ||
d, err := newDriver(tt.rule, maskFunc) | ||
if err != nil { | ||
if !tt.wantErr { | ||
t.Errorf("newDriver() error = %v,wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
return | ||
} | ||
|
||
if err == nil { | ||
got, err := d.Exec([]byte(tt.input)) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Exec() error = %v,wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
|
||
if string(got) != tt.want { | ||
t.Errorf("Exec() got = %v,want %v", string(got), tt.want) | ||
} | ||
} | ||
}) | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
drivers/strategy/data-mask-strategy/mask/keyword/keyword_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package keyword | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/eolinker/apinto/drivers/strategy/data-mask-strategy/mask" | ||
) | ||
|
||
func TestKeywordDriver(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg *mask.Rule | ||
maskFunc mask.MaskFunc | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
name: "Basic replacement", | ||
cfg: &mask.Rule{ | ||
Match: &mask.BasicItem{ | ||
Type: mask.MatchKeyword, | ||
Value: "secret", | ||
}, | ||
Mask: &mask.Mask{}, | ||
}, | ||
maskFunc: func(value string) string { | ||
return "****" | ||
}, | ||
input: "This is a secret text.", | ||
expected: "This is a **** text.", | ||
}, | ||
{ | ||
name: "No replacement needed", | ||
cfg: &mask.Rule{ | ||
Match: &mask.BasicItem{ | ||
Type: mask.MatchKeyword, | ||
Value: "hidden", | ||
}, | ||
Mask: &mask.Mask{}, | ||
}, | ||
maskFunc: func(value string) string { | ||
return "****" | ||
}, | ||
input: "This is a visible text.", | ||
expected: "This is a visible text.", | ||
}, | ||
{ | ||
name: "Multiple occurrences", | ||
cfg: &mask.Rule{ | ||
Match: &mask.BasicItem{ | ||
Type: mask.MatchKeyword, | ||
Value: "cat", | ||
}, | ||
Mask: &mask.Mask{}, | ||
}, | ||
maskFunc: func(value string) string { | ||
return "dog" | ||
}, | ||
input: "A cat chasing another cat.", | ||
expected: "A dog chasing another dog.", | ||
}, | ||
{ | ||
name: "Empty input", | ||
cfg: &mask.Rule{ | ||
Match: &mask.BasicItem{ | ||
Type: mask.MatchKeyword, | ||
Value: "anything", | ||
}, | ||
Mask: &mask.Mask{}, | ||
}, | ||
maskFunc: func(value string) string { | ||
return "nothing" | ||
}, | ||
input: "", | ||
expected: "", | ||
}, | ||
{ | ||
name: "Long text replacement", | ||
cfg: &mask.Rule{ | ||
Match: &mask.BasicItem{ | ||
Type: mask.MatchKeyword, | ||
Value: "important", | ||
}, | ||
Mask: &mask.Mask{}, | ||
}, | ||
maskFunc: func(value string) string { | ||
return "[REDACTED]" | ||
}, | ||
input: strings.Repeat("This is important. ", 1000), | ||
expected: strings.Repeat("This is [REDACTED]. ", 1000), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
driver, err := NewKeywordMaskDriver(test.cfg, test.maskFunc) | ||
if err != nil { | ||
t.Fatalf("Failed to create driver: %v", err) | ||
} | ||
|
||
result, _ := driver.Exec([]byte(test.input)) | ||
if string(result) != test.expected { | ||
t.Errorf("Expected '%v', but got '%v'", test.expected, string(result)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters