Repository navigation
Expand file tree
/
Copy pathhandler_stealth_test.go
More file actions
184 lines (158 loc) · 5.45 KB
/
Copy pathhandler_stealth_test.go
File metadata and controls
184 lines (158 loc) · 5.45 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"bytes"
"net/http/httptest"
"testing"
)
func TestHandleStealthStatus_NoTab_ReturnsStatic(t *testing.T) {
b := &Bridge{}
b.tabs = make(map[string]*TabEntry)
req := httptest.NewRequest("GET", "/stealth/status", nil)
w := httptest.NewRecorder()
b.handleStealthStatus(w, req)
if w.Code != 200 {
t.Errorf("expected 200, got %d", w.Code)
}
// Should contain level and score
body := w.Body.String()
if !searchString(body, "level") || !searchString(body, "score") {
t.Errorf("expected level and score in response, got: %s", body)
}
}
func TestHandleFingerprintRotate_InvalidJSON(t *testing.T) {
b := &Bridge{}
req := httptest.NewRequest("POST", "/fingerprint/rotate", bytes.NewReader([]byte(`not json`)))
w := httptest.NewRecorder()
b.handleFingerprintRotate(w, req)
if w.Code != 400 {
t.Errorf("expected 400, got %d", w.Code)
}
}
// ── generateFingerprint ────────────────────────────────────
func TestGenerateFingerprint_Windows(t *testing.T) {
fp := generateFingerprint(fingerprintRequest{OS: "windows"})
if fp.Platform != "Win32" {
t.Errorf("expected Win32, got %q", fp.Platform)
}
if fp.UserAgent == "" {
t.Error("expected non-empty user agent")
}
if fp.ScreenWidth == 0 || fp.ScreenHeight == 0 {
t.Error("expected non-zero screen dimensions")
}
if fp.Vendor != "Google Inc." {
t.Errorf("expected Google Inc., got %q", fp.Vendor)
}
}
func TestGenerateFingerprint_Mac(t *testing.T) {
fp := generateFingerprint(fingerprintRequest{OS: "mac"})
if fp.Platform != "MacIntel" {
t.Errorf("expected MacIntel, got %q", fp.Platform)
}
}
func TestGenerateFingerprint_Random(t *testing.T) {
fp := generateFingerprint(fingerprintRequest{OS: "random"})
validPlatforms := map[string]bool{"Win32": true, "MacIntel": true}
if !validPlatforms[fp.Platform] {
t.Errorf("unexpected platform %q", fp.Platform)
}
}
func TestGenerateFingerprint_UnknownOS(t *testing.T) {
// Unknown OS — should return empty fingerprint (no match in map)
fp := generateFingerprint(fingerprintRequest{OS: "freebsd"})
// Will have screen/language/timezone from random selection below
// but platform/UA won't be set from the map
_ = fp // just ensure no panic
}
func TestGenerateFingerprint_WithBrowser(t *testing.T) {
fp := generateFingerprint(fingerprintRequest{OS: "windows", Browser: "chrome"})
if fp.UserAgent == "" {
t.Error("expected non-empty user agent")
}
}
// ── getStealthRecommendations ──────────────────────────────
func TestGetStealthRecommendations_AllEnabled(t *testing.T) {
features := map[string]bool{
"user_agent_override": true,
"languages_spoofed": true,
"webrtc_leak_prevented": true,
"timezone_spoofed": true,
"canvas_noise": true,
"font_spoofing": true,
}
recs := getStealthRecommendations(features)
if len(recs) != 1 || recs[0] != "Stealth mode is well configured" {
t.Errorf("expected well-configured message, got %v", recs)
}
}
func TestGetStealthRecommendations_NoneEnabled(t *testing.T) {
features := map[string]bool{}
recs := getStealthRecommendations(features)
if len(recs) != 6 {
t.Errorf("expected 6 recommendations, got %d: %v", len(recs), recs)
}
}
func TestGetStealthRecommendations_Partial(t *testing.T) {
features := map[string]bool{
"user_agent_override": true,
"timezone_spoofed": true,
}
recs := getStealthRecommendations(features)
// Should have 4 recommendations (the 4 missing features)
if len(recs) != 4 {
t.Errorf("expected 4 recommendations, got %d: %v", len(recs), recs)
}
}
// ── sendStealthResponse ────────────────────────────────────
func TestSendStealthResponse_HighScore(t *testing.T) {
b := &Bridge{}
b.tabs = make(map[string]*TabEntry)
features := map[string]bool{
"a": true, "b": true, "c": true, "d": true, "e": true,
}
w := httptest.NewRecorder()
b.sendStealthResponse(w, features, "TestAgent/1.0")
if w.Code != 200 {
t.Errorf("expected 200, got %d", w.Code)
}
body := w.Body.String()
if !searchString(body, `"level":"high"`) {
t.Errorf("expected high level for 100%% score, got: %s", body)
}
}
func TestSendStealthResponse_LowScore(t *testing.T) {
b := &Bridge{}
b.tabs = make(map[string]*TabEntry)
features := map[string]bool{
"a": true, "b": false, "c": false, "d": false, "e": false,
"f": false, "g": false, "h": false, "i": false, "j": false,
}
w := httptest.NewRecorder()
b.sendStealthResponse(w, features, "")
body := w.Body.String()
if !searchString(body, `"level":"minimal"`) {
t.Errorf("expected minimal level for 10%% score, got: %s", body)
}
}
func TestSendStealthResponse_MediumScore(t *testing.T) {
b := &Bridge{}
b.tabs = make(map[string]*TabEntry)
// 6/10 = 60%
features := map[string]bool{
"a": true, "b": true, "c": true, "d": true, "e": true,
"f": true, "g": false, "h": false, "i": false, "j": false,
}
w := httptest.NewRecorder()
b.sendStealthResponse(w, features, "")
body := w.Body.String()
if !searchString(body, `"level":"medium"`) {
t.Errorf("expected medium level, got: %s", body)
}
}
// ── staticStealthFeatures ──────────────────────────────────
func TestStaticStealthFeatures_HasEntries(t *testing.T) {
features := staticStealthFeatures()
if len(features) == 0 {
t.Error("expected non-empty stealth features map")
}
}