| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestIncompleteExitSamplesIdentifyRoutesAndRedactPrivateErrors(t *testing.T) { |
| 11 | good := networkObservation{Status: 200, IP: "8.8.8.8"} |
| 12 | message := incompleteExitSamples("", context.DeadlineExceeded, good, nil, networkObservation{}, errors.New("浏览器请求失败 https://user:[email protected]/secret token=private-token /Users/alice/project")) |
| 13 | for _, want := range []string{"宿主", "超时", "浏览器", "出口采样不完整"} { |
| 14 | if !strings.Contains(message, want) { |
| 15 | t.Fatalf("missing diagnostic %q: %s", want, message) |
| 16 | } |
| 17 | } |
| 18 | for _, unwanted := range []string{"虚拟机终端", "private.example", "private-token", "alice", "password"} { |
| 19 | if strings.Contains(message, unwanted) { |
| 20 | t.Fatalf("successful route or private data included: %s", message) |
| 21 | } |
| 22 | } |
| 23 | if message := incompleteExitSamples("8.8.8.8", nil, good, nil, good, nil); message != "" { |
| 24 | t.Fatal("complete samples reported missing", message) |
| 25 | } |
| 26 | for _, tc := range []struct { |
| 27 | observation networkObservation |
| 28 | want string |
| 29 | }{ |
| 30 | {networkObservation{}, "没有收到检测通道的响应"}, |
| 31 | {networkObservation{Status: 503}, "HTTP 503"}, |
| 32 | {networkObservation{Status: 200}, "没有返回出口地址"}, |
| 33 | } { |
| 34 | message := incompleteExitSamples("8.8.8.8", nil, tc.observation, nil, good, nil) |
| 35 | if !strings.Contains(message, "虚拟机终端:") || !strings.Contains(message, tc.want) || strings.Contains(message, "宿主:") || strings.Contains(message, "浏览器:") { |
| 36 | t.Fatalf("wrong route or failure category: %s", message) |
| 37 | } |
| 38 | } |
| 39 | } |