pii_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package helper
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "testing"
  6. "fwh-toolbox-server/domain"
  7. "fwh-toolbox-server/dto"
  8. )
  9. // ---------- 掩码函数单元测试 ----------
  10. func TestMaskName(t *testing.T) {
  11. cases := map[string]string{
  12. "": "",
  13. "张三": "张***",
  14. "John": "J***",
  15. "李": "李***",
  16. }
  17. for in, want := range cases {
  18. if got := maskName(in); got != want {
  19. t.Errorf("maskName(%q)=%q, want %q", in, got, want)
  20. }
  21. }
  22. }
  23. func TestMaskEmail(t *testing.T) {
  24. if got := maskEmail("john@corp.com"); got != "j***@corp.com" {
  25. t.Errorf("maskEmail got %q", got)
  26. }
  27. // 非法邮箱退化为完全脱敏
  28. if got := maskEmail("notanemail"); got != maskFull() {
  29. t.Errorf("maskEmail(invalid) got %q", got)
  30. }
  31. if got := maskEmail(""); got != "" {
  32. t.Errorf("maskEmail(empty) got %q", got)
  33. }
  34. }
  35. func TestMaskAccount(t *testing.T) {
  36. if got := maskAccount("abcdefgh"); got != "ab***gh" {
  37. t.Errorf("maskAccount got %q", got)
  38. }
  39. // 过短完全脱敏
  40. if got := maskAccount("abc"); got != maskFull() {
  41. t.Errorf("maskAccount(short) got %q", got)
  42. }
  43. }
  44. func TestMaskFullFixedLength(t *testing.T) {
  45. if got := maskFull(); got != strings.Repeat(MaskChar, fullMaskLen) {
  46. t.Errorf("maskFull got %q", got)
  47. }
  48. }
  49. // ---------- 优化 B:流式 token 脱敏测试 ----------
  50. func TestMaskJSONBytes_FlatFields(t *testing.T) {
  51. in := []byte(`{
  52. "sn": "SN123",
  53. "userName": "Alice",
  54. "contactNumber": "13812345678",
  55. "email": "a@b.com",
  56. "longitude": 121.5,
  57. "latitude": 31.2,
  58. "address": "123 Main St",
  59. "city": "Shanghai",
  60. "province": "SH",
  61. "country": "CN",
  62. "iccid": "8986012345678901234",
  63. "imei": "356938035643809",
  64. "account": "installer01"
  65. }`)
  66. out, err := MaskJSONBytes(in)
  67. if err != nil {
  68. t.Fatalf("MaskJSONBytes err: %v", err)
  69. }
  70. var m map[string]any
  71. if err := json.Unmarshal(out, &m); err != nil {
  72. t.Fatalf("output not valid JSON: %v\n%s", err, out)
  73. }
  74. // 保留字段
  75. if m["sn"] != "SN123" {
  76. t.Errorf("sn should be preserved, got %v", m["sn"])
  77. }
  78. if m["iccid"] != "8986012345678901234" {
  79. t.Errorf("iccid should be preserved, got %v", m["iccid"])
  80. }
  81. if m["city"] != "Shanghai" || m["province"] != "SH" || m["country"] != "CN" {
  82. t.Errorf("admin region should be preserved: city=%v province=%v country=%v", m["city"], m["province"], m["country"])
  83. }
  84. // 完全脱敏字段
  85. full := maskFull()
  86. for _, k := range []string{"contactNumber", "longitude", "latitude", "address", "imei"} {
  87. if m[k] != full {
  88. t.Errorf("%s should be fully masked, got %v", k, m[k])
  89. }
  90. }
  91. // 姓名
  92. if m["userName"] != "A***" {
  93. t.Errorf("userName masked wrong: %v", m["userName"])
  94. }
  95. // 邮箱
  96. if m["email"] != "a***@b.com" {
  97. t.Errorf("email masked wrong: %v", m["email"])
  98. }
  99. // 账号
  100. if m["account"] != "in***01" {
  101. t.Errorf("account masked wrong: %v", m["account"])
  102. }
  103. }
  104. func TestMaskJSONBytes_Nested(t *testing.T) {
  105. in := []byte(`{
  106. "data": [
  107. {"userName": "Bob", "sn": "S1", "nested": {"phone": "111", "keep": "ok"}},
  108. {"userName": "Carol", "sn": "S2"}
  109. ],
  110. "total": 2
  111. }`)
  112. out, err := MaskJSONBytes(in)
  113. if err != nil {
  114. t.Fatalf("err: %v", err)
  115. }
  116. var m map[string]any
  117. if err := json.Unmarshal(out, &m); err != nil {
  118. t.Fatalf("invalid JSON: %v\n%s", err, out)
  119. }
  120. arr := m["data"].([]any)
  121. first := arr[0].(map[string]any)
  122. if first["userName"] != "B***" {
  123. t.Errorf("nested list userName wrong: %v", first["userName"])
  124. }
  125. if first["sn"] != "S1" {
  126. t.Errorf("sn should be preserved in list: %v", first["sn"])
  127. }
  128. nested := first["nested"].(map[string]any)
  129. if nested["phone"] != maskFull() {
  130. t.Errorf("deeply nested phone should be masked: %v", nested["phone"])
  131. }
  132. if nested["keep"] != "ok" {
  133. t.Errorf("non-sensitive nested field should be preserved: %v", nested["keep"])
  134. }
  135. if m["total"].(float64) != 2 {
  136. t.Errorf("total wrong: %v", m["total"])
  137. }
  138. }
  139. func TestMaskJSONBytes_SensitiveSubtreeRedacted(t *testing.T) {
  140. // 敏感 key 对应对象/数组时,整个子树被脱敏
  141. in := []byte(`{"address": {"street": "secret", "no": 42}, "list_phone": ["a","b"]}`)
  142. // 注意 list_phone 不在规则表,仅测 address 子树
  143. out, err := MaskJSONBytes(in)
  144. if err != nil {
  145. t.Fatalf("err: %v", err)
  146. }
  147. var m map[string]any
  148. if err := json.Unmarshal(out, &m); err != nil {
  149. t.Fatalf("invalid JSON: %v\n%s", err, out)
  150. }
  151. addr := m["address"].(map[string]any)
  152. if addr["street"] != maskFull() {
  153. t.Errorf("address.street should be masked: %v", addr["street"])
  154. }
  155. // number 子字段被替换为掩码字符串
  156. if addr["no"] != maskFull() {
  157. t.Errorf("address.no should be masked: %v", addr["no"])
  158. }
  159. }
  160. func TestMaskJSONBytes_InvalidJSONFailsClosed(t *testing.T) {
  161. _, err := MaskJSONBytes([]byte(`{not valid json`))
  162. if err == nil {
  163. t.Fatal("expected error for invalid JSON (fail-closed)")
  164. }
  165. }
  166. func TestMaskJSONBytes_NullPreserved(t *testing.T) {
  167. in := []byte(`{"userName": null, "phone": null}`)
  168. out, err := MaskJSONBytes(in)
  169. if err != nil {
  170. t.Fatalf("err: %v", err)
  171. }
  172. if !strings.Contains(string(out), `"userName":null`) {
  173. t.Errorf("null should be preserved: %s", out)
  174. }
  175. }
  176. // ---------- 优化 A:强类型脱敏测试 ----------
  177. func TestSanitizeSiteInfoDTO_NoUpstreamPollution(t *testing.T) {
  178. name := "Alice"
  179. phone := "13812345678"
  180. addr := "123 Main St"
  181. iccid := "8986012345678901234"
  182. orig := &dto.SiteInfoDTO{
  183. Sn: "SN1",
  184. UserName: &name,
  185. ContactNumber: &phone,
  186. Address: &addr,
  187. Longitude: "121.5",
  188. Latitude: "31.2",
  189. City: "Shanghai",
  190. ICCID: iccid,
  191. IMEI: "356938035643809",
  192. }
  193. sanitized, ok := sanitizeStrong(orig)
  194. if !ok {
  195. t.Fatal("expected strong-type hit for *dto.SiteInfoDTO")
  196. }
  197. s := sanitized.(*dto.SiteInfoDTO)
  198. // 脱敏结果正确
  199. if *s.UserName != "A***" {
  200. t.Errorf("userName: %v", *s.UserName)
  201. }
  202. if *s.ContactNumber != maskFull() {
  203. t.Errorf("contactNumber: %v", *s.ContactNumber)
  204. }
  205. if *s.Address != maskFull() {
  206. t.Errorf("address: %v", *s.Address)
  207. }
  208. if s.Longitude != maskFull() || s.Latitude != maskFull() {
  209. t.Errorf("geo not masked: %v %v", s.Longitude, s.Latitude)
  210. }
  211. if s.IMEI != maskFull() {
  212. t.Errorf("imei: %v", s.IMEI)
  213. }
  214. // 保留字段
  215. if s.ICCID != iccid {
  216. t.Errorf("iccid should be preserved: %v", s.ICCID)
  217. }
  218. if s.City != "Shanghai" {
  219. t.Errorf("city should be preserved: %v", s.City)
  220. }
  221. // 上游未被污染
  222. if *orig.UserName != "Alice" || *orig.ContactNumber != "13812345678" || *orig.Address != "123 Main St" {
  223. t.Errorf("upstream polluted: name=%v phone=%v addr=%v", *orig.UserName, *orig.ContactNumber, *orig.Address)
  224. }
  225. if orig.Longitude != "121.5" || orig.IMEI != "356938035643809" {
  226. t.Errorf("upstream scalar polluted: lng=%v imei=%v", orig.Longitude, orig.IMEI)
  227. }
  228. }
  229. func TestSanitizeResPageSiteInfo(t *testing.T) {
  230. name := "Bob"
  231. orig := &dto.ResPage[dto.SiteInfoDTO]{
  232. Data: []dto.SiteInfoDTO{{Sn: "S1", UserName: &name, IMEI: "111"}},
  233. Total: 1,
  234. }
  235. sanitized, ok := sanitizeStrong(orig)
  236. if !ok {
  237. t.Fatal("expected hit")
  238. }
  239. s := sanitized.(*dto.ResPage[dto.SiteInfoDTO])
  240. if *s.Data[0].UserName != "B***" {
  241. t.Errorf("page item userName: %v", *s.Data[0].UserName)
  242. }
  243. // 上游 slice 未被污染
  244. if *orig.Data[0].UserName != "Bob" {
  245. t.Errorf("upstream page polluted: %v", *orig.Data[0].UserName)
  246. }
  247. }
  248. func TestSanitizeDomainSimCardInfo(t *testing.T) {
  249. orig := &domain.SimCardInfo{GICCID: "GIC19", IMEI: "imei1", IMSI: "imsi1", MSISDN: "msisdn1"}
  250. sanitized, ok := sanitizeStrong(orig)
  251. if !ok {
  252. t.Fatal("expected hit")
  253. }
  254. s := sanitized.(*domain.SimCardInfo)
  255. if s.IMEI != maskFull() || s.IMSI != maskFull() || s.MSISDN != maskFull() {
  256. t.Errorf("sim identifiers not masked: %+v", s)
  257. }
  258. if s.GICCID != "GIC19" {
  259. t.Errorf("giccid should be preserved: %v", s.GICCID)
  260. }
  261. // 上游未污染
  262. if orig.IMEI != "imei1" {
  263. t.Errorf("upstream sim polluted: %v", orig.IMEI)
  264. }
  265. }
  266. // ---------- A/B 一致性测试 ----------
  267. func TestAB_Consistency_SiteInfo(t *testing.T) {
  268. name := "Alice"
  269. phone := "13812345678"
  270. addr := "123 Main St"
  271. site := &dto.SiteInfoDTO{
  272. Sn: "SN1",
  273. UserName: &name,
  274. ContactNumber: &phone,
  275. Address: &addr,
  276. Longitude: "121.5",
  277. City: "Shanghai",
  278. ICCID: "8986012345678901234",
  279. IMEI: "356938035643809",
  280. }
  281. // A 路径
  282. sanitized, _ := sanitizeStrong(site)
  283. aBytes, err := json.Marshal(sanitized)
  284. if err != nil {
  285. t.Fatal(err)
  286. }
  287. // B 路径:直接对原始结构体序列化后走流式脱敏
  288. rawBytes, _ := json.Marshal(site)
  289. bBytes, err := MaskJSONBytes(rawBytes)
  290. if err != nil {
  291. t.Fatalf("B err: %v", err)
  292. }
  293. // 归一化比较(字段顺序可能不同,比较解析后的 map)
  294. var ma, mb map[string]any
  295. if err := json.Unmarshal(aBytes, &ma); err != nil {
  296. t.Fatal(err)
  297. }
  298. if err := json.Unmarshal(bBytes, &mb); err != nil {
  299. t.Fatal(err)
  300. }
  301. for _, k := range []string{"userName", "contactNumber", "address", "longitude", "imei", "iccId", "city", "sn"} {
  302. if ma[k] != mb[k] {
  303. t.Errorf("A/B mismatch on %q: A=%v B=%v", k, ma[k], mb[k])
  304. }
  305. }
  306. }