AndroidLibXrayLite/libv2ray_support_test.go

146 lines
3.1 KiB
Go
Raw Normal View History

2022-06-26 20:18:07 +08:00
package libv2ray
2020-11-26 13:38:51 +08:00
import (
"bufio"
"context"
"fmt"
"net"
"sync"
"testing"
"time"
2020-12-04 11:11:10 +08:00
v2net "github.com/xtls/xray-core/common/net"
2020-11-26 13:38:51 +08:00
)
2025-03-28 05:23:10 +03:30
// fakeSupportSet is a mock implementation of the protectSet interface
2020-11-26 13:38:51 +08:00
type fakeSupportSet struct{}
2025-03-28 05:23:10 +03:30
// Protect is a mock implementation that always returns true
2020-11-26 13:38:51 +08:00
func (f fakeSupportSet) Protect(int) bool {
return true
}
2025-03-28 05:23:10 +03:30
// TestProtectedDialer_PrepareDomain tests the PrepareDomain method of the ProtectedDialer
2020-11-26 13:38:51 +08:00
func TestProtectedDialer_PrepareDomain(t *testing.T) {
type args struct {
domainName string
}
tests := []struct {
name string
args args
}{
2025-03-28 05:23:10 +03:30
{"Test with baidu.com", args{"baidu.com:80"}},
// Add more test cases if needed
2020-11-26 13:38:51 +08:00
}
d := NewProtectedDialer(fakeSupportSet{})
2020-11-26 13:38:51 +08:00
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2025-03-28 05:23:10 +03:30
ch := make(chan struct{})
2020-11-26 13:38:51 +08:00
go d.PrepareDomain(tt.args.domainName, ch, false)
time.Sleep(time.Second)
2025-03-28 05:23:10 +03:30
d.vServer.NextIP()
2020-11-26 13:38:51 +08:00
t.Log(d.vServer.currentIP())
})
}
time.Sleep(time.Second)
}
2025-03-28 05:23:10 +03:30
// TestProtectedDialer_Dial tests the Dial method of the ProtectedDialer
2020-11-26 13:38:51 +08:00
func TestProtectedDialer_Dial(t *testing.T) {
tests := []struct {
name string
wantErr bool
}{
{"baidu.com:80", false},
{"cloudflare.com:80", false},
{"172.16.192.11:80", true},
2025-03-28 05:23:10 +03:30
// Add more test cases if needed
2020-11-26 13:38:51 +08:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ch := make(chan struct{})
d := NewProtectedDialer(fakeSupportSet{})
2020-11-26 13:38:51 +08:00
d.currentServer = tt.name
go d.PrepareDomain(tt.name, ch, false)
var wg sync.WaitGroup
dial := func() {
defer wg.Done()
dest, _ := v2net.ParseDestination("tcp:" + tt.name)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
conn, err := d.Dial(ctx, nil, dest, nil)
if err != nil {
t.Log(err)
return
}
2025-03-28 05:23:10 +03:30
defer conn.Close()
host, _, _ := net.SplitHostPort(tt.name)
fmt.Fprintf(conn, "GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host)
2020-11-26 13:38:51 +08:00
status, err := bufio.NewReader(conn).ReadString('\n')
2025-03-28 05:23:10 +03:30
t.Logf("Status: %s, Error: %v", status, err)
2020-11-26 13:38:51 +08:00
}
for n := 0; n < 3; n++ {
wg.Add(1)
go dial()
}
wg.Wait()
})
}
}
2025-03-28 05:23:10 +03:30
// Test_resolved_NextIP tests the NextIP method of the resolved struct
2020-11-26 13:38:51 +08:00
func Test_resolved_NextIP(t *testing.T) {
type fields struct {
domain string
IPs []net.IP
Port int
}
tests := []struct {
name string
fields fields
}{
2025-03-28 05:23:10 +03:30
{"test1", fields{
domain: "www.baidu.com",
IPs: []net.IP{
net.ParseIP("1.2.3.4"),
net.ParseIP("4.3.2.1"),
net.ParseIP("1234::1"),
net.ParseIP("4321::1"),
},
}},
2020-11-26 13:38:51 +08:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &resolved{
domain: tt.fields.domain,
IPs: tt.fields.IPs,
Port: tt.fields.Port,
}
2025-03-28 05:23:10 +03:30
t.Logf("Initial IPs: %v", r.IPs)
t.Logf("Current IP: %v", r.currentIP())
2020-11-26 13:38:51 +08:00
r.NextIP()
2025-03-28 05:23:10 +03:30
t.Logf("Next IP: %v", r.currentIP())
2020-11-26 13:38:51 +08:00
r.NextIP()
2025-03-28 05:23:10 +03:30
t.Logf("Next IP: %v", r.currentIP())
2020-11-26 13:38:51 +08:00
r.NextIP()
2025-03-28 05:23:10 +03:30
t.Logf("Next IP: %v", r.currentIP())
2020-11-26 13:38:51 +08:00
time.Sleep(3 * time.Second)
r.NextIP()
2025-03-28 05:23:10 +03:30
t.Logf("Next IP: %v", r.currentIP())
2020-11-26 13:38:51 +08:00
time.Sleep(5 * time.Second)
r.NextIP()
2025-03-28 05:23:10 +03:30
t.Logf("Next IP: %v", r.currentIP())
2020-11-26 13:38:51 +08:00
})
}
}