-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathinvoke_probes_test.go
More file actions
82 lines (79 loc) · 3.02 KB
/
Copy pathinvoke_probes_test.go
File metadata and controls
82 lines (79 loc) · 3.02 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
package libXray
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestInvokeShareOutboundsResponseShape(t *testing.T) {
const validLink = "vless://12345678-abcd-abcd-abcd-123456789abc@example.com:443?encryption=none&security=tls&sni=example.com"
response := invokeForTest(t, LibXrayMethodConvertShareLinksToXrayJson, ConvertShareLinksToXrayJsonRequest{
Text: validLink + "\nvless://bad@example.com:443",
})
if !response.Success {
t.Fatal(response.Err)
}
var root map[string]json.RawMessage
if err := json.Unmarshal(response.Data, &root); err != nil {
t.Fatal(err)
}
if len(root) != 1 || root["outbounds"] == nil {
t.Fatalf("data = %s, want only outbounds", response.Data)
}
if config := decodeShareConfig(t, response); len(config.OutboundConfigs) != 1 {
t.Fatalf("outbounds = %d, want 1", len(config.OutboundConfigs))
}
for _, text := range []string{"vless://bad@example.com:443", `{"outbounds":[]}`, `{"outbounds":`, "-----BEGIN AGE ENCRYPTED FILE-----\ninvalid"} {
response := invokeForTest(t, LibXrayMethodConvertShareLinksToXrayJson, ConvertShareLinksToXrayJsonRequest{Text: text})
if response.Success || string(response.Data) != "null" || response.Err == "" {
t.Fatalf("response = %+v", response)
}
}
}
func TestInvokePingLocationAndZeroDelayWireFields(t *testing.T) {
raw, err := json.Marshal(PingBatchItemResponse{Success: true, Delay: 0})
if err != nil {
t.Fatal(err)
}
if string(raw) != `{"success":true,"delay":0}` {
t.Fatalf("zero latency response = %s", raw)
}
empty := ""
raw, err = json.Marshal(PingBatchItemResponse{Success: true, Delay: 0, LocationJSON: &empty})
if err != nil {
t.Fatal(err)
}
if string(raw) != `{"success":true,"delay":0,"locationJson":""}` {
t.Fatalf("empty location response = %s", raw)
}
locationBody := `{"ip_address":"203.0.113.1","country":"SG"}`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, locationBody)
}))
defer server.Close()
request := PingBatchRequest{
Configs: []PingBatchItemRequest{{XrayJson: `{"outbounds":[{"protocol":"freedom"}]}`}},
Timeout: 1, URL: server.URL, LocationURL: server.URL,
}
response := invokeForTest(t, LibXrayMethodPingBatch, request)
if !response.Success {
t.Fatalf("error = %s", response.Err)
}
result := decodeDataObject[PingBatchResponse](t, response).Results[0]
if !result.Success || result.LocationJSON == nil || *result.LocationJSON != `{"ip_address":"203.0.113.1","country":"SG"}` {
t.Fatalf("result = %+v", result)
}
locationBody = ""
response = invokeForTest(t, LibXrayMethodPingBatch, request)
result = decodeDataObject[PingBatchResponse](t, response).Results[0]
if !response.Success || result.LocationJSON == nil || *result.LocationJSON != "" {
t.Fatalf("empty location response = %+v", response)
}
request.LocationURL = ""
response = invokeForTest(t, LibXrayMethodPingBatch, request)
if !response.Success || strings.Contains(string(response.Data), "location") {
t.Fatalf("latency-only response = %+v", response)
}
}