mirror of
https://github.com/bestnite/sub2clash.git
synced 2025-06-17 04:33:18 +08:00
add tests
This commit is contained in:
219
test/parser/anytls_test.go
Normal file
219
test/parser/anytls_test.go
Normal file
@ -0,0 +1,219 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/parser"
|
||||
)
|
||||
|
||||
func TestAnytls_Basic_SimpleLink(t *testing.T) {
|
||||
p := &parser.AnytlsParser{}
|
||||
input := "anytls://password123@127.0.0.1:8080#Anytls%20Proxy"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "anytls",
|
||||
Name: "Anytls Proxy",
|
||||
Anytls: proxy.Anytls{
|
||||
Server: "127.0.0.1",
|
||||
Port: 8080,
|
||||
Password: "password123",
|
||||
SkipCertVerify: false,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestAnytls_Basic_WithSNI(t *testing.T) {
|
||||
p := &parser.AnytlsParser{}
|
||||
input := "anytls://password123@proxy.example.com:443?sni=proxy.example.com#Anytls%20SNI"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "anytls",
|
||||
Name: "Anytls SNI",
|
||||
Anytls: proxy.Anytls{
|
||||
Server: "proxy.example.com",
|
||||
Port: 443,
|
||||
Password: "password123",
|
||||
SNI: "proxy.example.com",
|
||||
SkipCertVerify: false,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestAnytls_Basic_WithInsecure(t *testing.T) {
|
||||
p := &parser.AnytlsParser{}
|
||||
input := "anytls://password123@proxy.example.com:443?insecure=1&sni=proxy.example.com#Anytls%20Insecure"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "anytls",
|
||||
Name: "Anytls Insecure",
|
||||
Anytls: proxy.Anytls{
|
||||
Server: "proxy.example.com",
|
||||
Port: 443,
|
||||
Password: "password123",
|
||||
SNI: "proxy.example.com",
|
||||
SkipCertVerify: true,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestAnytls_Basic_IPv6Address(t *testing.T) {
|
||||
p := &parser.AnytlsParser{}
|
||||
input := "anytls://password123@[2001:db8::1]:8080#Anytls%20IPv6"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "anytls",
|
||||
Name: "Anytls IPv6",
|
||||
Anytls: proxy.Anytls{
|
||||
Server: "2001:db8::1",
|
||||
Port: 8080,
|
||||
Password: "password123",
|
||||
SkipCertVerify: false,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestAnytls_Basic_ComplexPassword(t *testing.T) {
|
||||
p := &parser.AnytlsParser{}
|
||||
input := "anytls://ComplexPassword!%40%23%24@proxy.example.com:8443?sni=example.com&insecure=1#Anytls%20Full"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "anytls",
|
||||
Name: "Anytls Full",
|
||||
Anytls: proxy.Anytls{
|
||||
Server: "proxy.example.com",
|
||||
Port: 8443,
|
||||
Password: "ComplexPassword!@#$",
|
||||
SNI: "example.com",
|
||||
SkipCertVerify: true,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestAnytls_Basic_NoPassword(t *testing.T) {
|
||||
p := &parser.AnytlsParser{}
|
||||
input := "anytls://@127.0.0.1:8080#No%20Password"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "anytls",
|
||||
Name: "No Password",
|
||||
Anytls: proxy.Anytls{
|
||||
Server: "127.0.0.1",
|
||||
Port: 8080,
|
||||
Password: "",
|
||||
SkipCertVerify: false,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestAnytls_Basic_UsernameOnly(t *testing.T) {
|
||||
p := &parser.AnytlsParser{}
|
||||
input := "anytls://username@127.0.0.1:8080#Username%20Only"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "anytls",
|
||||
Name: "Username Only",
|
||||
Anytls: proxy.Anytls{
|
||||
Server: "127.0.0.1",
|
||||
Port: 8080,
|
||||
Password: "username",
|
||||
SkipCertVerify: false,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestAnytls_Error_MissingServer(t *testing.T) {
|
||||
p := &parser.AnytlsParser{}
|
||||
input := "anytls://password123@:8080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnytls_Error_MissingPort(t *testing.T) {
|
||||
p := &parser.AnytlsParser{}
|
||||
input := "anytls://password123@127.0.0.1"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnytls_Error_InvalidPort(t *testing.T) {
|
||||
p := &parser.AnytlsParser{}
|
||||
input := "anytls://password123@127.0.0.1:99999"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnytls_Error_InvalidProtocol(t *testing.T) {
|
||||
p := &parser.AnytlsParser{}
|
||||
input := "anyssl://example.com:8080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
198
test/parser/hysteria2_test.go
Normal file
198
test/parser/hysteria2_test.go
Normal file
@ -0,0 +1,198 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/parser"
|
||||
)
|
||||
|
||||
func TestHysteria2_Basic_SimpleLink(t *testing.T) {
|
||||
p := &parser.Hysteria2Parser{}
|
||||
input := "hysteria2://password123@127.0.0.1:8080#Hysteria2%20Proxy"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "hysteria2",
|
||||
Name: "Hysteria2 Proxy",
|
||||
Hysteria2: proxy.Hysteria2{
|
||||
Server: "127.0.0.1",
|
||||
Port: 8080,
|
||||
Password: "password123",
|
||||
SkipCertVerify: false,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestHysteria2_Basic_AltPrefix(t *testing.T) {
|
||||
p := &parser.Hysteria2Parser{}
|
||||
input := "hy2://password123@proxy.example.com:443?insecure=1&sni=proxy.example.com#Hysteria2%20Alt"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "hysteria2",
|
||||
Name: "Hysteria2 Alt",
|
||||
Hysteria2: proxy.Hysteria2{
|
||||
Server: "proxy.example.com",
|
||||
Port: 443,
|
||||
Password: "password123",
|
||||
SNI: "proxy.example.com",
|
||||
SkipCertVerify: true,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestHysteria2_Basic_WithObfs(t *testing.T) {
|
||||
p := &parser.Hysteria2Parser{}
|
||||
input := "hysteria2://password123@127.0.0.1:8080?obfs=salamander&obfs-password=obfs123#Hysteria2%20Obfs"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "hysteria2",
|
||||
Name: "Hysteria2 Obfs",
|
||||
Hysteria2: proxy.Hysteria2{
|
||||
Server: "127.0.0.1",
|
||||
Port: 8080,
|
||||
Password: "password123",
|
||||
Obfs: "salamander",
|
||||
ObfsPassword: "obfs123",
|
||||
SkipCertVerify: false,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestHysteria2_Basic_IPv6Address(t *testing.T) {
|
||||
p := &parser.Hysteria2Parser{}
|
||||
input := "hysteria2://password123@[2001:db8::1]:8080#Hysteria2%20IPv6"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "hysteria2",
|
||||
Name: "Hysteria2 IPv6",
|
||||
Hysteria2: proxy.Hysteria2{
|
||||
Server: "2001:db8::1",
|
||||
Port: 8080,
|
||||
Password: "password123",
|
||||
SkipCertVerify: false,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestHysteria2_Basic_FullConfig(t *testing.T) {
|
||||
p := &parser.Hysteria2Parser{}
|
||||
input := "hysteria2://password123@proxy.example.com:443?insecure=1&sni=proxy.example.com&obfs=salamander&obfs-password=obfs123#Hysteria2%20Full"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "hysteria2",
|
||||
Name: "Hysteria2 Full",
|
||||
Hysteria2: proxy.Hysteria2{
|
||||
Server: "proxy.example.com",
|
||||
Port: 443,
|
||||
Password: "password123",
|
||||
SNI: "proxy.example.com",
|
||||
Obfs: "salamander",
|
||||
ObfsPassword: "obfs123",
|
||||
SkipCertVerify: true,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestHysteria2_Basic_NoPassword(t *testing.T) {
|
||||
p := &parser.Hysteria2Parser{}
|
||||
input := "hysteria2://@127.0.0.1:8080#No%20Password"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "hysteria2",
|
||||
Name: "No Password",
|
||||
Hysteria2: proxy.Hysteria2{
|
||||
Server: "127.0.0.1",
|
||||
Port: 8080,
|
||||
Password: "",
|
||||
SkipCertVerify: false,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestHysteria2_Error_MissingServer(t *testing.T) {
|
||||
p := &parser.Hysteria2Parser{}
|
||||
input := "hysteria2://password123@:8080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHysteria2_Error_MissingPort(t *testing.T) {
|
||||
p := &parser.Hysteria2Parser{}
|
||||
input := "hysteria2://password123@127.0.0.1"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHysteria2_Error_InvalidPort(t *testing.T) {
|
||||
p := &parser.Hysteria2Parser{}
|
||||
input := "hysteria2://password123@127.0.0.1:99999"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHysteria2_Error_InvalidProtocol(t *testing.T) {
|
||||
p := &parser.Hysteria2Parser{}
|
||||
input := "hysteria://example.com:8080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
183
test/parser/hysteria_test.go
Normal file
183
test/parser/hysteria_test.go
Normal file
@ -0,0 +1,183 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/parser"
|
||||
)
|
||||
|
||||
func TestHysteria_Basic_SimpleLink(t *testing.T) {
|
||||
p := &parser.HysteriaParser{}
|
||||
input := "hysteria://127.0.0.1:8080?protocol=udp&auth=password123&upmbps=100&downmbps=100#Hysteria%20Proxy"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "hysteria",
|
||||
Name: "Hysteria Proxy",
|
||||
Hysteria: proxy.Hysteria{
|
||||
Server: "127.0.0.1",
|
||||
Port: 8080,
|
||||
Protocol: "udp",
|
||||
Auth: "password123",
|
||||
Up: "100",
|
||||
Down: "100",
|
||||
SkipCertVerify: false,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestHysteria_Basic_WithAuthString(t *testing.T) {
|
||||
p := &parser.HysteriaParser{}
|
||||
input := "hysteria://proxy.example.com:443?protocol=wechat-video&auth-str=myauth&upmbps=50&downmbps=200&insecure=true#Hysteria%20Auth"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "hysteria",
|
||||
Name: "Hysteria Auth",
|
||||
Hysteria: proxy.Hysteria{
|
||||
Server: "proxy.example.com",
|
||||
Port: 443,
|
||||
Protocol: "wechat-video",
|
||||
AuthString: "myauth",
|
||||
Up: "50",
|
||||
Down: "200",
|
||||
SkipCertVerify: true,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestHysteria_Basic_WithObfs(t *testing.T) {
|
||||
p := &parser.HysteriaParser{}
|
||||
input := "hysteria://127.0.0.1:8080?auth=password123&upmbps=100&downmbps=100&obfs=xplus&alpn=h3#Hysteria%20Obfs"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "hysteria",
|
||||
Name: "Hysteria Obfs",
|
||||
Hysteria: proxy.Hysteria{
|
||||
Server: "127.0.0.1",
|
||||
Port: 8080,
|
||||
Auth: "password123",
|
||||
Up: "100",
|
||||
Down: "100",
|
||||
Obfs: "xplus",
|
||||
ALPN: []string{"h3"},
|
||||
SkipCertVerify: false,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestHysteria_Basic_IPv6Address(t *testing.T) {
|
||||
p := &parser.HysteriaParser{}
|
||||
input := "hysteria://[2001:db8::1]:8080?auth=password123&upmbps=100&downmbps=100#Hysteria%20IPv6"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "hysteria",
|
||||
Name: "Hysteria IPv6",
|
||||
Hysteria: proxy.Hysteria{
|
||||
Server: "2001:db8::1",
|
||||
Port: 8080,
|
||||
Auth: "password123",
|
||||
Up: "100",
|
||||
Down: "100",
|
||||
SkipCertVerify: false,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestHysteria_Basic_MultiALPN(t *testing.T) {
|
||||
p := &parser.HysteriaParser{}
|
||||
input := "hysteria://proxy.example.com:443?auth=password123&upmbps=100&downmbps=100&alpn=h3,h2,http/1.1#Hysteria%20Multi%20ALPN"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "hysteria",
|
||||
Name: "Hysteria Multi ALPN",
|
||||
Hysteria: proxy.Hysteria{
|
||||
Server: "proxy.example.com",
|
||||
Port: 443,
|
||||
Auth: "password123",
|
||||
Up: "100",
|
||||
Down: "100",
|
||||
ALPN: []string{"h3", "h2", "http/1.1"},
|
||||
SkipCertVerify: false,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestHysteria_Error_MissingServer(t *testing.T) {
|
||||
p := &parser.HysteriaParser{}
|
||||
input := "hysteria://:8080?auth=password123"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHysteria_Error_MissingPort(t *testing.T) {
|
||||
p := &parser.HysteriaParser{}
|
||||
input := "hysteria://127.0.0.1?auth=password123"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHysteria_Error_InvalidPort(t *testing.T) {
|
||||
p := &parser.HysteriaParser{}
|
||||
input := "hysteria://127.0.0.1:99999?auth=password123"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHysteria_Error_InvalidProtocol(t *testing.T) {
|
||||
p := &parser.HysteriaParser{}
|
||||
input := "hysteria2://example.com:8080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
184
test/parser/shadowsocks_test.go
Normal file
184
test/parser/shadowsocks_test.go
Normal file
@ -0,0 +1,184 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/parser"
|
||||
)
|
||||
|
||||
func TestShadowsocks_Basic_SimpleLink(t *testing.T) {
|
||||
p := &parser.ShadowsocksParser{}
|
||||
input := "ss://YWVzLTI1Ni1nY206cGFzc3dvcmQ=@127.0.0.1:8080"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "ss",
|
||||
Name: "127.0.0.1:8080",
|
||||
ShadowSocks: proxy.ShadowSocks{
|
||||
Server: "127.0.0.1",
|
||||
Port: 8080,
|
||||
Cipher: "aes-256-gcm",
|
||||
Password: "password",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestShadowsocks_Basic_IPv6Address(t *testing.T) {
|
||||
p := &parser.ShadowsocksParser{}
|
||||
input := "ss://YWVzLTI1Ni1nY206cGFzc3dvcmQ=@[2001:db8::1]:8080"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "ss",
|
||||
Name: "2001:db8::1:8080",
|
||||
ShadowSocks: proxy.ShadowSocks{
|
||||
Server: "2001:db8::1",
|
||||
Port: 8080,
|
||||
Cipher: "aes-256-gcm",
|
||||
Password: "password",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestShadowsocks_Basic_WithRemark(t *testing.T) {
|
||||
p := &parser.ShadowsocksParser{}
|
||||
input := "ss://YWVzLTI1Ni1nY206cGFzc3dvcmQ=@proxy.example.com:8080#My%20SS%20Proxy"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "ss",
|
||||
Name: "My SS Proxy",
|
||||
ShadowSocks: proxy.ShadowSocks{
|
||||
Server: "proxy.example.com",
|
||||
Port: 8080,
|
||||
Cipher: "aes-256-gcm",
|
||||
Password: "password",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestShadowsocks_Advanced_Base64FullEncoded(t *testing.T) {
|
||||
p := &parser.ShadowsocksParser{}
|
||||
input := "ss://YWVzLTI1Ni1nY206cGFzc3dvcmRAbG9jYWxob3N0OjgwODA=#Local%20SS"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "ss",
|
||||
Name: "Local SS",
|
||||
ShadowSocks: proxy.ShadowSocks{
|
||||
Server: "localhost",
|
||||
Port: 8080,
|
||||
Cipher: "aes-256-gcm",
|
||||
Password: "password",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestShadowsocks_Advanced_PlainUserPassword(t *testing.T) {
|
||||
p := &parser.ShadowsocksParser{}
|
||||
input := "ss://aes-256-gcm:password@192.168.1.1:8080"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "ss",
|
||||
Name: "192.168.1.1:8080",
|
||||
ShadowSocks: proxy.ShadowSocks{
|
||||
Server: "192.168.1.1",
|
||||
Port: 8080,
|
||||
Cipher: "aes-256-gcm",
|
||||
Password: "password",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestShadowsocks_Advanced_ChaCha20Cipher(t *testing.T) {
|
||||
p := &parser.ShadowsocksParser{}
|
||||
input := "ss://chacha20-poly1305:mypassword@server.com:443#ChaCha20"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "ss",
|
||||
Name: "ChaCha20",
|
||||
ShadowSocks: proxy.ShadowSocks{
|
||||
Server: "server.com",
|
||||
Port: 443,
|
||||
Cipher: "chacha20-poly1305",
|
||||
Password: "mypassword",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
// 错误处理测试
|
||||
func TestShadowsocks_Error_MissingServer(t *testing.T) {
|
||||
p := &parser.ShadowsocksParser{}
|
||||
input := "ss://YWVzLTI1Ni1nY206cGFzc3dvcmQ=@:8080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if !errors.Is(err, parser.ErrInvalidStruct) {
|
||||
t.Errorf("Error is not expected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShadowsocks_Error_MissingPort(t *testing.T) {
|
||||
p := &parser.ShadowsocksParser{}
|
||||
input := "ss://YWVzLTI1Ni1nY206cGFzc3dvcmQ=@127.0.0.1"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if !errors.Is(err, parser.ErrInvalidStruct) {
|
||||
t.Errorf("Error is not expected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShadowsocks_Error_InvalidProtocol(t *testing.T) {
|
||||
p := &parser.ShadowsocksParser{}
|
||||
input := "http://example.com:8080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if !errors.Is(err, parser.ErrInvalidPrefix) {
|
||||
t.Errorf("Error is not expected: %v", err)
|
||||
}
|
||||
}
|
112
test/parser/shadowsocksr_test.go
Normal file
112
test/parser/shadowsocksr_test.go
Normal file
@ -0,0 +1,112 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/parser"
|
||||
)
|
||||
|
||||
func TestShadowsocksR_Basic_SimpleLink(t *testing.T) {
|
||||
p := &parser.ShadowsocksRParser{}
|
||||
input := "ssr://MTI3LjAuMC4xOjQ0MzpvcmlnaW46YWVzLTE5Mi1jZmI6cGxhaW46TVRJek1USXovP2dyb3VwPVpHVm1ZWFZzZEEmcmVtYXJrcz1TRUZJUVE"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "ssr",
|
||||
Name: "HAHA",
|
||||
ShadowSocksR: proxy.ShadowSocksR{
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
Cipher: "aes-192-cfb",
|
||||
Password: "123123",
|
||||
ObfsParam: "",
|
||||
Obfs: "plain",
|
||||
Protocol: "origin",
|
||||
ProtocolParam: "",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestShadowsocksR_Basic_WithParams(t *testing.T) {
|
||||
p := &parser.ShadowsocksRParser{}
|
||||
input := "ssr://MTI3LjAuMC4xOjQ0MzpvcmlnaW46YWVzLTE5Mi1jZmI6dGxzMS4wX3Nlc3Npb25fYXV0aDpNVEl6TVRJei8/b2Jmc3BhcmFtPWIySm1jeTF3WVhKaGJXVjBaWEkmcHJvdG9wYXJhbT1jSEp2ZEc5allXd3RjR0Z5WVcxbGRHVnkmZ3JvdXA9WkdWbVlYVnNkQSZyZW1hcmtzPVNFRklRUQ"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "ssr",
|
||||
Name: "HAHA",
|
||||
ShadowSocksR: proxy.ShadowSocksR{
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
Cipher: "aes-192-cfb",
|
||||
Password: "123123",
|
||||
ObfsParam: "obfs-parameter",
|
||||
Obfs: "tls1.0_session_auth",
|
||||
Protocol: "origin",
|
||||
ProtocolParam: "protocal-parameter",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestShadowsocksR_Basic_IPv6Address(t *testing.T) {
|
||||
p := &parser.ShadowsocksRParser{}
|
||||
input := "ssr://WzIwMDE6MGRiODo4NWEzOjAwMDA6MDAwMDo4YTJlOjAzNzA6NzMzNF06NDQzOm9yaWdpbjphZXMtMTkyLWNmYjpwbGFpbjpNVEl6TVRJei8/Z3JvdXA9WkdWbVlYVnNkQSZyZW1hcmtzPVNFRklRUQ"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "ssr",
|
||||
Name: "HAHA",
|
||||
ShadowSocksR: proxy.ShadowSocksR{
|
||||
Server: "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
|
||||
Port: 443,
|
||||
Cipher: "aes-192-cfb",
|
||||
Password: "123123",
|
||||
ObfsParam: "",
|
||||
Obfs: "plain",
|
||||
Protocol: "origin",
|
||||
ProtocolParam: "",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestShadowsocksR_Error_InvalidBase64(t *testing.T) {
|
||||
p := &parser.ShadowsocksRParser{}
|
||||
input := "ssr://invalid_base64"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShadowsocksR_Error_InvalidProtocol(t *testing.T) {
|
||||
p := &parser.ShadowsocksRParser{}
|
||||
input := "ss://example.com:8080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
168
test/parser/socks_test.go
Normal file
168
test/parser/socks_test.go
Normal file
@ -0,0 +1,168 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/parser"
|
||||
)
|
||||
|
||||
func TestSocks_Basic_SimpleLink(t *testing.T) {
|
||||
p := &parser.SocksParser{}
|
||||
input := "socks://user:pass@127.0.0.1:1080#SOCKS%20Proxy"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "socks5",
|
||||
Name: "SOCKS Proxy",
|
||||
Socks: proxy.Socks{
|
||||
Server: "127.0.0.1",
|
||||
Port: 1080,
|
||||
UserName: "user",
|
||||
Password: "pass",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestSocks_Basic_NoAuth(t *testing.T) {
|
||||
p := &parser.SocksParser{}
|
||||
input := "socks://127.0.0.1:1080#SOCKS%20No%20Auth"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "socks5",
|
||||
Name: "SOCKS No Auth",
|
||||
Socks: proxy.Socks{
|
||||
Server: "127.0.0.1",
|
||||
Port: 1080,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestSocks_Basic_IPv6Address(t *testing.T) {
|
||||
p := &parser.SocksParser{}
|
||||
input := "socks://user:pass@[2001:db8::1]:1080#SOCKS%20IPv6"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "socks5",
|
||||
Name: "SOCKS IPv6",
|
||||
Socks: proxy.Socks{
|
||||
Server: "2001:db8::1",
|
||||
Port: 1080,
|
||||
UserName: "user",
|
||||
Password: "pass",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestSocks_Basic_WithTLS(t *testing.T) {
|
||||
p := &parser.SocksParser{}
|
||||
input := "socks://user:pass@127.0.0.1:1080?tls=true&sni=example.com#SOCKS%20TLS"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "socks5",
|
||||
Name: "SOCKS TLS",
|
||||
Socks: proxy.Socks{
|
||||
Server: "127.0.0.1",
|
||||
Port: 1080,
|
||||
UserName: "user",
|
||||
Password: "pass",
|
||||
TLS: true,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestSocks_Basic_WithUDP(t *testing.T) {
|
||||
p := &parser.SocksParser{}
|
||||
input := "socks://user:pass@127.0.0.1:1080?udp=true#SOCKS%20UDP"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "socks5",
|
||||
Name: "SOCKS UDP",
|
||||
Socks: proxy.Socks{
|
||||
Server: "127.0.0.1",
|
||||
Port: 1080,
|
||||
UserName: "user",
|
||||
Password: "pass",
|
||||
UDP: true,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestSocks_Error_MissingServer(t *testing.T) {
|
||||
p := &parser.SocksParser{}
|
||||
input := "socks://user:pass@:1080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocks_Error_MissingPort(t *testing.T) {
|
||||
p := &parser.SocksParser{}
|
||||
input := "socks://user:pass@127.0.0.1"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocks_Error_InvalidPort(t *testing.T) {
|
||||
p := &parser.SocksParser{}
|
||||
input := "socks://user:pass@127.0.0.1:99999"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocks_Error_InvalidProtocol(t *testing.T) {
|
||||
p := &parser.SocksParser{}
|
||||
input := "ss://example.com:8080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
182
test/parser/trojan_test.go
Normal file
182
test/parser/trojan_test.go
Normal file
@ -0,0 +1,182 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/parser"
|
||||
)
|
||||
|
||||
func TestTrojan_Basic_SimpleLink(t *testing.T) {
|
||||
p := &parser.TrojanParser{}
|
||||
input := "trojan://password@127.0.0.1:443#Trojan%20Proxy"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "trojan",
|
||||
Name: "Trojan Proxy",
|
||||
Trojan: proxy.Trojan{
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
Password: "password",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestTrojan_Basic_WithTLS(t *testing.T) {
|
||||
p := &parser.TrojanParser{}
|
||||
input := "trojan://password@127.0.0.1:443?security=tls&sni=example.com&alpn=h2,http/1.1#Trojan%20TLS"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "trojan",
|
||||
Name: "Trojan TLS",
|
||||
Trojan: proxy.Trojan{
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
Password: "password",
|
||||
ALPN: []string{"h2", "http/1.1"},
|
||||
SNI: "example.com",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestTrojan_Basic_WithReality(t *testing.T) {
|
||||
p := &parser.TrojanParser{}
|
||||
input := "trojan://password@127.0.0.1:443?security=reality&sni=example.com&pbk=publickey123&sid=shortid123&fp=chrome#Trojan%20Reality"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "trojan",
|
||||
Name: "Trojan Reality",
|
||||
Trojan: proxy.Trojan{
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
Password: "password",
|
||||
SNI: "example.com",
|
||||
RealityOpts: proxy.RealityOptions{
|
||||
PublicKey: "publickey123",
|
||||
ShortID: "shortid123",
|
||||
},
|
||||
Fingerprint: "chrome",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestTrojan_Basic_WithWebSocket(t *testing.T) {
|
||||
p := &parser.TrojanParser{}
|
||||
input := "trojan://password@127.0.0.1:443?type=ws&path=/ws&host=example.com#Trojan%20WS"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "trojan",
|
||||
Name: "Trojan WS",
|
||||
Trojan: proxy.Trojan{
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
Password: "password",
|
||||
Network: "ws",
|
||||
WSOpts: proxy.WSOptions{
|
||||
Path: "/ws",
|
||||
Headers: map[string]string{
|
||||
"Host": "example.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestTrojan_Basic_WithGrpc(t *testing.T) {
|
||||
p := &parser.TrojanParser{}
|
||||
input := "trojan://password@127.0.0.1:443?type=grpc&serviceName=grpc_service#Trojan%20gRPC"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "trojan",
|
||||
Name: "Trojan gRPC",
|
||||
Trojan: proxy.Trojan{
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
Password: "password",
|
||||
Network: "grpc",
|
||||
GrpcOpts: proxy.GrpcOptions{
|
||||
GrpcServiceName: "grpc_service",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestTrojan_Error_MissingServer(t *testing.T) {
|
||||
p := &parser.TrojanParser{}
|
||||
input := "trojan://password@:443"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrojan_Error_MissingPort(t *testing.T) {
|
||||
p := &parser.TrojanParser{}
|
||||
input := "trojan://password@127.0.0.1"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrojan_Error_InvalidPort(t *testing.T) {
|
||||
p := &parser.TrojanParser{}
|
||||
input := "trojan://password@127.0.0.1:99999"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrojan_Error_InvalidProtocol(t *testing.T) {
|
||||
p := &parser.TrojanParser{}
|
||||
input := "ss://example.com:8080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
24
test/parser/utils.go
Normal file
24
test/parser/utils.go
Normal file
@ -0,0 +1,24 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/bestnite/sub2clash/model/proxy"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func validateResult(t *testing.T, expected proxy.Proxy, result proxy.Proxy) {
|
||||
t.Helper()
|
||||
|
||||
if result.Type != expected.Type {
|
||||
t.Errorf("Type mismatch: expected %s, got %s", expected.Type, result.Type)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(expected, result) {
|
||||
expectedYaml, _ := yaml.Marshal(expected)
|
||||
resultYaml, _ := yaml.Marshal(result)
|
||||
|
||||
t.Errorf("Structure mismatch: \nexpected:\n %s\ngot:\n %s", string(expectedYaml), string(resultYaml))
|
||||
}
|
||||
}
|
214
test/parser/vless_test.go
Normal file
214
test/parser/vless_test.go
Normal file
@ -0,0 +1,214 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/parser"
|
||||
)
|
||||
|
||||
func TestVless_Basic_SimpleLink(t *testing.T) {
|
||||
p := &parser.VlessParser{}
|
||||
input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:8080#VLESS%20Proxy"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "vless",
|
||||
Name: "VLESS Proxy",
|
||||
Vless: proxy.Vless{
|
||||
Server: "127.0.0.1",
|
||||
Port: 8080,
|
||||
UUID: "b831b0c4-33b7-4873-9834-28d66d87d4ce",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestVless_Basic_WithTLS(t *testing.T) {
|
||||
p := &parser.VlessParser{}
|
||||
input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:443?security=tls&sni=example.com&alpn=h2,http/1.1#VLESS%20TLS"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "vless",
|
||||
Name: "VLESS TLS",
|
||||
Vless: proxy.Vless{
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
UUID: "b831b0c4-33b7-4873-9834-28d66d87d4ce",
|
||||
TLS: true,
|
||||
ALPN: []string{"h2", "http/1.1"},
|
||||
ServerName: "example.com",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestVless_Basic_WithReality(t *testing.T) {
|
||||
p := &parser.VlessParser{}
|
||||
input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:443?security=reality&sni=example.com&pbk=publickey123&sid=shortid123&fp=chrome#VLESS%20Reality"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "vless",
|
||||
Name: "VLESS Reality",
|
||||
Vless: proxy.Vless{
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
UUID: "b831b0c4-33b7-4873-9834-28d66d87d4ce",
|
||||
TLS: true,
|
||||
ServerName: "example.com",
|
||||
RealityOpts: proxy.RealityOptions{
|
||||
PublicKey: "publickey123",
|
||||
ShortID: "shortid123",
|
||||
},
|
||||
Fingerprint: "chrome",
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestVless_Basic_WithWebSocket(t *testing.T) {
|
||||
p := &parser.VlessParser{}
|
||||
input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:443?type=ws&path=/ws&host=example.com#VLESS%20WS"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "vless",
|
||||
Name: "VLESS WS",
|
||||
Vless: proxy.Vless{
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
UUID: "b831b0c4-33b7-4873-9834-28d66d87d4ce",
|
||||
Network: "ws",
|
||||
WSOpts: proxy.WSOptions{
|
||||
Path: "/ws",
|
||||
Headers: map[string]string{
|
||||
"Host": "example.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestVless_Basic_WithGrpc(t *testing.T) {
|
||||
p := &parser.VlessParser{}
|
||||
input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:443?type=grpc&serviceName=grpc_service#VLESS%20gRPC"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "vless",
|
||||
Name: "VLESS gRPC",
|
||||
Vless: proxy.Vless{
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
UUID: "b831b0c4-33b7-4873-9834-28d66d87d4ce",
|
||||
Network: "grpc",
|
||||
GrpcOpts: proxy.GrpcOptions{
|
||||
GrpcServiceName: "grpc_service",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestVless_Basic_WithHTTP(t *testing.T) {
|
||||
p := &parser.VlessParser{}
|
||||
input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:443?type=http&path=/path1,/path2&host=host1.com,host2.com#VLESS%20HTTP"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "vless",
|
||||
Name: "VLESS HTTP",
|
||||
Vless: proxy.Vless{
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
UUID: "b831b0c4-33b7-4873-9834-28d66d87d4ce",
|
||||
Network: "http",
|
||||
HTTPOpts: proxy.HTTPOptions{
|
||||
Path: []string{"/path1", "/path2"},
|
||||
Headers: map[string][]string{
|
||||
"host": {"host1.com", "host2.com"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestVless_Error_MissingServer(t *testing.T) {
|
||||
p := &parser.VlessParser{}
|
||||
input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@:8080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVless_Error_MissingPort(t *testing.T) {
|
||||
p := &parser.VlessParser{}
|
||||
input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVless_Error_InvalidPort(t *testing.T) {
|
||||
p := &parser.VlessParser{}
|
||||
input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:99999"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVless_Error_InvalidProtocol(t *testing.T) {
|
||||
p := &parser.VlessParser{}
|
||||
input := "ss://example.com:8080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
232
test/parser/vmess_test.go
Normal file
232
test/parser/vmess_test.go
Normal file
@ -0,0 +1,232 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/parser"
|
||||
)
|
||||
|
||||
func TestVmess_Basic_SimpleLink(t *testing.T) {
|
||||
p := &parser.VmessParser{}
|
||||
input := "vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJhaWQiOiIwIiwiaWQiOiIxMjM0NTY3OC05MDEyLTM0NTYtNzg5MC0xMjM0NTY3ODkwMTIiLCJuZXQiOiJ3cyIsInBvcnQiOiI0NDMiLCJwcyI6IkhBSEEiLCJ0bHMiOiJ0bHMiLCJ0eXBlIjoibm9uZSIsInYiOiIyIn0="
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "vmess",
|
||||
Name: "HAHA",
|
||||
Vmess: proxy.Vmess{
|
||||
UUID: "12345678-9012-3456-7890-123456789012",
|
||||
AlterID: 0,
|
||||
Cipher: "auto",
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
TLS: true,
|
||||
Network: "ws",
|
||||
WSOpts: proxy.WSOptions{
|
||||
Path: "/",
|
||||
Headers: map[string]string{
|
||||
"Host": "127.0.0.1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestVmess_Basic_WithPath(t *testing.T) {
|
||||
p := &parser.VmessParser{}
|
||||
input := "vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJhaWQiOiIwIiwiaWQiOiIxMjM0NTY3OC05MDEyLTM0NTYtNzg5MC0xMjM0NTY3ODkwMTIiLCJuZXQiOiJ3cyIsInBhdGgiOiIvd3MiLCJwb3J0IjoiNDQzIiwicHMiOiJIQUNLIiwidGxzIjoidGxzIiwidHlwZSI6Im5vbmUiLCJ2IjoiMiJ9"
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "vmess",
|
||||
Name: "HACK",
|
||||
Vmess: proxy.Vmess{
|
||||
UUID: "12345678-9012-3456-7890-123456789012",
|
||||
AlterID: 0,
|
||||
Cipher: "auto",
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
TLS: true,
|
||||
Network: "ws",
|
||||
WSOpts: proxy.WSOptions{
|
||||
Path: "/ws",
|
||||
Headers: map[string]string{
|
||||
"Host": "127.0.0.1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestVmess_Basic_WithHost(t *testing.T) {
|
||||
p := &parser.VmessParser{}
|
||||
input := "vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJhaWQiOiIwIiwiaG9zdCI6ImV4YW1wbGUuY29tIiwiaWQiOiIxMjM0NTY3OC05MDEyLTM0NTYtNzg5MC0xMjM0NTY3ODkwMTIiLCJuZXQiOiJ3cyIsInBvcnQiOiI0NDMiLCJwcyI6IkhBSEEiLCJ0bHMiOiJ0bHMiLCJ0eXBlIjoibm9uZSIsInYiOiIyIn0="
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "vmess",
|
||||
Name: "HAHA",
|
||||
Vmess: proxy.Vmess{
|
||||
UUID: "12345678-9012-3456-7890-123456789012",
|
||||
AlterID: 0,
|
||||
Cipher: "auto",
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
TLS: true,
|
||||
Network: "ws",
|
||||
WSOpts: proxy.WSOptions{
|
||||
Path: "/",
|
||||
Headers: map[string]string{
|
||||
"Host": "example.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestVmess_Basic_WithSNI(t *testing.T) {
|
||||
p := &parser.VmessParser{}
|
||||
input := "vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJhaWQiOiIwIiwiaWQiOiIxMjM0NTY3OC05MDEyLTM0NTYtNzg5MC0xMjM0NTY3ODkwMTIiLCJuZXQiOiJ3cyIsInBvcnQiOiI0NDMiLCJwcyI6IkhBSEEiLCJzbmkiOiJleGFtcGxlLmNvbSIsInRscyI6InRscyIsInR5cGUiOiJub25lIiwidiI6IjIifQ=="
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "vmess",
|
||||
Name: "HAHA",
|
||||
Vmess: proxy.Vmess{
|
||||
UUID: "12345678-9012-3456-7890-123456789012",
|
||||
AlterID: 0,
|
||||
Cipher: "auto",
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
TLS: true,
|
||||
Network: "ws",
|
||||
ServerName: "example.com",
|
||||
WSOpts: proxy.WSOptions{
|
||||
Path: "/",
|
||||
Headers: map[string]string{
|
||||
"Host": "127.0.0.1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestVmess_Basic_WithAlterID(t *testing.T) {
|
||||
p := &parser.VmessParser{}
|
||||
input := "vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJhaWQiOiIxIiwiaWQiOiIxMjM0NTY3OC05MDEyLTM0NTYtNzg5MC0xMjM0NTY3ODkwMTIiLCJuZXQiOiJ3cyIsInBvcnQiOiI0NDMiLCJwcyI6IkhBSEEiLCJ0bHMiOiJ0bHMiLCJ0eXBlIjoibm9uZSIsInYiOiIyIn0="
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "vmess",
|
||||
Name: "HAHA",
|
||||
Vmess: proxy.Vmess{
|
||||
UUID: "12345678-9012-3456-7890-123456789012",
|
||||
AlterID: 1,
|
||||
Cipher: "auto",
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
TLS: true,
|
||||
Network: "ws",
|
||||
WSOpts: proxy.WSOptions{
|
||||
Path: "/",
|
||||
Headers: map[string]string{
|
||||
"Host": "127.0.0.1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestVmess_Basic_GRPC(t *testing.T) {
|
||||
p := &parser.VmessParser{}
|
||||
input := "vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJhaWQiOiIwIiwiaWQiOiIxMjM0NTY3OC05MDEyLTM0NTYtNzg5MC0xMjM0NTY3ODkwMTIiLCJuZXQiOiJncnBjIiwicG9ydCI6IjQ0MyIsInBzIjoiSEFIQSIsInRscyI6InRscyIsInR5cGUiOiJub25lIiwidiI6IjIifQ=="
|
||||
|
||||
expected := proxy.Proxy{
|
||||
Type: "vmess",
|
||||
Name: "HAHA",
|
||||
Vmess: proxy.Vmess{
|
||||
UUID: "12345678-9012-3456-7890-123456789012",
|
||||
AlterID: 0,
|
||||
Cipher: "auto",
|
||||
Server: "127.0.0.1",
|
||||
Port: 443,
|
||||
TLS: true,
|
||||
Network: "grpc",
|
||||
GrpcOpts: proxy.GrpcOptions{},
|
||||
},
|
||||
}
|
||||
|
||||
result, err := p.Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
validateResult(t, expected, result)
|
||||
}
|
||||
|
||||
func TestVmess_Error_InvalidBase64(t *testing.T) {
|
||||
p := &parser.VmessParser{}
|
||||
input := "vmess://invalid_base64"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVmess_Error_InvalidJSON(t *testing.T) {
|
||||
p := &parser.VmessParser{}
|
||||
input := "vmess://eyJpbnZhbGlkIjoianNvbn0="
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVmess_Error_InvalidProtocol(t *testing.T) {
|
||||
p := &parser.VmessParser{}
|
||||
input := "ss://example.com:8080"
|
||||
|
||||
_, err := p.Parse(input)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got none")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user