mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-06 07:43:38 +00:00
74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package blackhole_test
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"testing"
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/buf"
|
|
"github.com/xtls/xray-core/common/session"
|
|
"github.com/xtls/xray-core/proxy/blackhole"
|
|
"github.com/xtls/xray-core/transport"
|
|
"github.com/xtls/xray-core/transport/pipe"
|
|
)
|
|
|
|
func TestBlackholeHTTPResponse(t *testing.T) {
|
|
ctx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{}})
|
|
handler, err := blackhole.New(ctx, &blackhole.Config{
|
|
Response: &blackhole.Response{Type: "http"},
|
|
})
|
|
common.Must(err)
|
|
|
|
reader, writer := pipe.New(pipe.WithoutSizeLimit())
|
|
|
|
var mb buf.MultiBuffer
|
|
var rerr error
|
|
go func() {
|
|
b, e := reader.ReadMultiBuffer()
|
|
mb = b
|
|
rerr = e
|
|
}()
|
|
|
|
link := transport.Link{
|
|
Reader: reader,
|
|
Writer: writer,
|
|
}
|
|
common.Must(handler.Process(ctx, &link, nil))
|
|
common.Must(rerr)
|
|
if mb.IsEmpty() {
|
|
t.Error("expect http response, but nothing")
|
|
}
|
|
}
|
|
|
|
func TestBlackholeCustomResponse(t *testing.T) {
|
|
ctx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{}})
|
|
// slightly bigger than a buffer
|
|
expected := make([]byte, buf.Size+1000)
|
|
if _, err := rand.Read(expected); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
handler, err := blackhole.New(ctx, &blackhole.Config{
|
|
Response: &blackhole.Response{
|
|
Type: "custom",
|
|
CustomResponseData: expected,
|
|
},
|
|
})
|
|
common.Must(err)
|
|
|
|
reader, writer := pipe.New(pipe.WithoutSizeLimit())
|
|
var actual buf.MultiBuffer
|
|
var rerr error
|
|
go func() {
|
|
actual, rerr = reader.ReadMultiBuffer()
|
|
}()
|
|
|
|
link := transport.Link{Reader: reader, Writer: writer}
|
|
common.Must(handler.Process(ctx, &link, nil))
|
|
common.Must(rerr)
|
|
|
|
if actual.String() != string(expected) {
|
|
t.Errorf("custom response mismatch")
|
|
}
|
|
}
|