You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.4 KiB
71 lines
1.4 KiB
package socks5
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func TestNewServer(t *testing.T) {
|
|
logger := logrus.New()
|
|
logger.SetLevel(logrus.ErrorLevel) // 减少测试输出
|
|
|
|
config := Config{
|
|
Auth: AuthConfig{
|
|
Username: "test",
|
|
Password: "pass",
|
|
},
|
|
Timeout: 30 * time.Second,
|
|
}
|
|
|
|
server := NewServer(config, logger)
|
|
if server == nil {
|
|
t.Fatal("Expected server to be created, got nil")
|
|
}
|
|
|
|
if server.logger != logger {
|
|
t.Error("Logger not set correctly")
|
|
}
|
|
}
|
|
|
|
func TestAuthHandler(t *testing.T) {
|
|
config := AuthConfig{
|
|
Username: "testuser",
|
|
Password: "testpass",
|
|
Methods: []string{"password"},
|
|
}
|
|
|
|
handler := NewAuthHandler(config)
|
|
|
|
// 测试正确的认证
|
|
if !handler.Authenticate("testuser", "testpass") {
|
|
t.Error("Valid authentication failed")
|
|
}
|
|
|
|
// 测试错误的认证
|
|
if handler.Authenticate("wronguser", "wrongpass") {
|
|
t.Error("Invalid authentication succeeded")
|
|
}
|
|
|
|
// 检查支持的方法
|
|
methods := handler.Methods()
|
|
if len(methods) != 1 || methods[0] != AuthPassword {
|
|
t.Error("Expected password authentication method")
|
|
}
|
|
}
|
|
|
|
func TestRule(t *testing.T) {
|
|
config := RuleConfig{
|
|
Action: "allow",
|
|
IPs: []string{"192.168.1.0/24", "127.0.0.1"},
|
|
Ports: []int{80, 443},
|
|
}
|
|
|
|
rule := NewRule(config)
|
|
if rule == nil {
|
|
t.Fatal("Expected rule to be created, got nil")
|
|
}
|
|
|
|
// 这里可以添加更多的规则测试
|
|
}
|
|
|