Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 21, 2024
1 parent 7693e98 commit df8a3e9
Show file tree
Hide file tree
Showing 29 changed files with 453 additions and 391 deletions.
10 changes: 8 additions & 2 deletions test/box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/sagernet/quic-go/http3"
"github.com/sagernet/sing-box"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/include"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/debug"
Expand All @@ -28,6 +29,12 @@ func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}

var globalCtx context.Context

func init() {
globalCtx = box.Context(context.Background(), include.InboundRegistry(), include.OutboundRegistry(), include.EndpointRegistry())
}

func startInstance(t *testing.T, options option.Options) *box.Box {
if debug.Enabled {
options.Log = &option.LogOptions{
Expand All @@ -38,8 +45,7 @@ func startInstance(t *testing.T, options option.Options) *box.Box {
Level: "warning",
}
}
// ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(globalCtx)
var instance *box.Box
var err error
for retry := 0; retry < 3; retry++ {
Expand Down
58 changes: 30 additions & 28 deletions test/brutal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-shadowsocks/shadowaead_2022"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/json/badoption"

"github.com/gofrs/uuid/v5"
)
Expand All @@ -15,22 +17,22 @@ func TestBrutalShadowsocks(t *testing.T) {
method := shadowaead_2022.List[0]
password := mkBase64(t, 16)
startInstance(t, option.Options{
Inbounds: []option.LegacyInbound{
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
Options: &option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
ListenPort: clientPort,
},
},
},
{
Type: C.TypeShadowsocks,
ShadowsocksOptions: option.ShadowsocksInboundOptions{
Options: &option.ShadowsocksInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
ListenPort: serverPort,
},
Method: method,
Expand All @@ -46,14 +48,14 @@ func TestBrutalShadowsocks(t *testing.T) {
},
},
},
LegacyOutbounds: []option.LegacyOutbound{
Outbounds: []option.Outbound{
{
Type: C.TypeDirect,
},
{
Type: C.TypeShadowsocks,
Tag: "ss-out",
ShadowsocksOptions: option.ShadowsocksOutboundOptions{
Options: &option.ShadowsocksOutboundOptions{
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
Expand Down Expand Up @@ -100,22 +102,22 @@ func TestBrutalTrojan(t *testing.T) {
_, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
password := mkBase64(t, 16)
startInstance(t, option.Options{
Inbounds: []option.LegacyInbound{
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
Options: &option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
ListenPort: clientPort,
},
},
},
{
Type: C.TypeTrojan,
TrojanOptions: option.TrojanInboundOptions{
Options: &option.TrojanInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
ListenPort: serverPort,
},
Users: []option.TrojanUser{{Password: password}},
Expand All @@ -138,14 +140,14 @@ func TestBrutalTrojan(t *testing.T) {
},
},
},
LegacyOutbounds: []option.LegacyOutbound{
Outbounds: []option.Outbound{
{
Type: C.TypeDirect,
},
{
Type: C.TypeTrojan,
Tag: "ss-out",
TrojanOptions: option.TrojanOutboundOptions{
Options: &option.TrojanOutboundOptions{
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
Expand Down Expand Up @@ -197,22 +199,22 @@ func TestBrutalTrojan(t *testing.T) {
func TestBrutalVMess(t *testing.T) {
user, _ := uuid.NewV4()
startInstance(t, option.Options{
Inbounds: []option.LegacyInbound{
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
Options: &option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
ListenPort: clientPort,
},
},
},
{
Type: C.TypeVMess,
VMessOptions: option.VMessInboundOptions{
Options: &option.VMessInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
ListenPort: serverPort,
},
Users: []option.VMessUser{{UUID: user.String()}},
Expand All @@ -227,14 +229,14 @@ func TestBrutalVMess(t *testing.T) {
},
},
},
LegacyOutbounds: []option.LegacyOutbound{
Outbounds: []option.Outbound{
{
Type: C.TypeDirect,
},
{
Type: C.TypeVMess,
Tag: "ss-out",
VMessOptions: option.VMessOutboundOptions{
Options: &option.VMessOutboundOptions{
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
Expand Down Expand Up @@ -279,22 +281,22 @@ func TestBrutalVMess(t *testing.T) {
func TestBrutalVLESS(t *testing.T) {
user, _ := uuid.NewV4()
startInstance(t, option.Options{
Inbounds: []option.LegacyInbound{
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
Options: &option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
ListenPort: clientPort,
},
},
},
{
Type: C.TypeVLESS,
VLESSOptions: option.VLESSInboundOptions{
Options: &option.VLESSInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
ListenPort: serverPort,
},
Users: []option.VLESSUser{{UUID: user.String()}},
Expand Down Expand Up @@ -326,14 +328,14 @@ func TestBrutalVLESS(t *testing.T) {
},
},
},
LegacyOutbounds: []option.LegacyOutbound{
Outbounds: []option.Outbound{
{
Type: C.TypeDirect,
},
{
Type: C.TypeVLESS,
Tag: "ss-out",
VLESSOptions: option.VLESSOutboundOptions{
Options: &option.VLESSOutboundOptions{
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
Expand Down
16 changes: 9 additions & 7 deletions test/direct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,43 @@ import (

C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/json/badoption"
)

// Since this is a feature one-off added by outsiders, I won't address these anymore.
func _TestProxyProtocol(t *testing.T) {
startInstance(t, option.Options{
Inbounds: []option.LegacyInbound{
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
Options: &option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
ListenPort: clientPort,
},
},
},
{
Type: C.TypeDirect,
DirectOptions: option.DirectInboundOptions{
Options: &option.DirectInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
ListenPort: serverPort,
ProxyProtocol: true,
},
},
},
},
LegacyOutbounds: []option.LegacyOutbound{
Outbounds: []option.Outbound{
{
Type: C.TypeDirect,
},
{
Type: C.TypeDirect,
Tag: "proxy-out",
DirectOptions: option.DirectOutboundOptions{
Options: &option.DirectOutboundOptions{
OverrideAddress: "127.0.0.1",
OverridePort: serverPort,
ProxyProtocol: 2,
Expand Down
16 changes: 9 additions & 7 deletions test/domain_inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,31 @@ import (
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-dns"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/json/badoption"

"github.com/gofrs/uuid/v5"
)

func TestTUICDomainUDP(t *testing.T) {
_, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
startInstance(t, option.Options{
Inbounds: []option.LegacyInbound{
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
Options: &option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
ListenPort: clientPort,
},
},
},
{
Type: C.TypeTUIC,
TUICOptions: option.TUICInboundOptions{
Options: &option.TUICInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
ListenPort: serverPort,
InboundOptions: option.InboundOptions{
DomainStrategy: option.DomainStrategy(dns.DomainStrategyUseIPv6),
Expand All @@ -49,14 +51,14 @@ func TestTUICDomainUDP(t *testing.T) {
},
},
},
LegacyOutbounds: []option.LegacyOutbound{
Outbounds: []option.Outbound{
{
Type: C.TypeDirect,
},
{
Type: C.TypeTUIC,
Tag: "tuic-out",
TUICOptions: option.TUICOutboundOptions{
Options: &option.TUICOutboundOptions{
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
Expand Down
Loading

0 comments on commit df8a3e9

Please sign in to comment.