本文整理汇总了Golang中v2ray/com/core/testing/assert.Error函数的典型用法代码示例。如果您正苦于以下问题:Golang Error函数的具体用法?Golang Error怎么用?Golang Error使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Error函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestTCPRequest
func TestTCPRequest(t *testing.T) {
assert := assert.On(t)
request := &protocol.RequestHeader{
Version: Version,
Command: protocol.RequestCommandTCP,
Address: v2net.LocalHostIP,
Option: RequestOptionOneTimeAuth,
Port: 1234,
User: &protocol.User{
Email: "[email protected]",
Account: loader.NewTypedSettings(&Account{
Password: "tcp-password",
CipherType: CipherType_CHACHA20,
}),
},
}
data := alloc.NewLocalBuffer(256).Clear().AppendString("test string")
cache := alloc.NewBuffer().Clear()
writer, err := WriteTCPRequest(request, cache)
assert.Error(err).IsNil()
writer.Write(data)
decodedRequest, reader, err := ReadTCPSession(request.User, cache)
assert.Error(err).IsNil()
assert.Address(decodedRequest.Address).Equals(request.Address)
assert.Port(decodedRequest.Port).Equals(request.Port)
decodedData, err := reader.Read()
assert.Error(err).IsNil()
assert.Bytes(decodedData.Value).Equals([]byte("test string"))
}
示例2: TestUDPEncoding
func TestUDPEncoding(t *testing.T) {
assert := assert.On(t)
request := &protocol.RequestHeader{
Version: Version,
Command: protocol.RequestCommandUDP,
Address: v2net.LocalHostIP,
Port: 1234,
User: &protocol.User{
Email: "[email protected]",
Account: serial.ToTypedMessage(&Account{
Password: "shadowsocks-password",
CipherType: CipherType_AES_128_CFB,
Ota: Account_Disabled,
}),
},
}
data := buf.NewLocal(256)
data.AppendSupplier(serial.WriteString("test string"))
encodedData, err := EncodeUDPPacket(request, data)
assert.Error(err).IsNil()
decodedRequest, decodedData, err := DecodeUDPPacket(request.User, encodedData)
assert.Error(err).IsNil()
assert.Bytes(decodedData.Bytes()).Equals(data.Bytes())
assert.Address(decodedRequest.Address).Equals(request.Address)
assert.Port(decodedRequest.Port).Equals(request.Port)
}
示例3: TestUDPEncoding
func TestUDPEncoding(t *testing.T) {
assert := assert.On(t)
request := &protocol.RequestHeader{
Version: Version,
Command: protocol.RequestCommandUDP,
Address: v2net.LocalHostIP,
Port: 1234,
User: &protocol.User{
Email: "[email protected]",
Account: loader.NewTypedSettings(&Account{
Password: "shadowsocks-password",
CipherType: CipherType_AES_128_CFB,
Ota: Account_Disabled,
}),
},
}
data := alloc.NewLocalBuffer(256).Clear().AppendString("test string")
encodedData, err := EncodeUDPPacket(request, data)
assert.Error(err).IsNil()
decodedRequest, decodedData, err := DecodeUDPPacket(request.User, encodedData)
assert.Error(err).IsNil()
assert.Bytes(decodedData.Value).Equals(data.Value)
assert.Address(decodedRequest.Address).Equals(request.Address)
assert.Port(decodedRequest.Port).Equals(request.Port)
}
示例4: TestNormalGetRequest
func TestNormalGetRequest(t *testing.T) {
assert := assert.On(t)
testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
port := v2net.Port(dice.Roll(20000) + 10000)
httpProxy := NewServer(
&Config{},
testPacketDispatcher,
&proxy.InboundHandlerMeta{
Address: v2net.LocalHostIP,
Port: port,
StreamSettings: &internet.StreamSettings{
Type: internet.StreamConnectionTypeRawTCP,
}})
defer httpProxy.Close()
err := httpProxy.Start()
assert.Error(err).IsNil()
assert.Port(port).Equals(httpProxy.Port())
httpClient := &http.Client{}
resp, err := httpClient.Get("http://127.0.0.1:" + port.String() + "/")
assert.Error(err).IsNil()
assert.Int(resp.StatusCode).Equals(400)
}
示例5: TestSwitchAccount
func TestSwitchAccount(t *testing.T) {
assert := assert.On(t)
sa := &protocol.CommandSwitchAccount{
Port: 1234,
ID: uuid.New(),
AlterIds: 1024,
Level: 128,
ValidMin: 16,
}
buffer := buf.New()
err := MarshalCommand(sa, buffer)
assert.Error(err).IsNil()
cmd, err := UnmarshalCommand(1, buffer.BytesFrom(2))
assert.Error(err).IsNil()
sa2, ok := cmd.(*protocol.CommandSwitchAccount)
assert.Bool(ok).IsTrue()
assert.Pointer(sa.Host).IsNil()
assert.Pointer(sa2.Host).IsNil()
assert.Port(sa.Port).Equals(sa2.Port)
assert.String(sa.ID.String()).Equals(sa2.ID.String())
assert.Uint16(sa.AlterIds).Equals(sa2.AlterIds)
assert.Byte(byte(sa.Level)).Equals(byte(sa2.Level))
assert.Byte(sa.ValidMin).Equals(sa2.ValidMin)
}
示例6: TestBufferedReader
func TestBufferedReader(t *testing.T) {
assert := assert.On(t)
content := buf.New()
content.AppendSupplier(buf.ReadFrom(rand.Reader))
len := content.Len()
reader := NewReader(content)
assert.Bool(reader.Cached()).IsTrue()
payload := make([]byte, 16)
nBytes, err := reader.Read(payload)
assert.Int(nBytes).Equals(16)
assert.Error(err).IsNil()
len2 := content.Len()
assert.Int(len - len2).GreaterThan(16)
nBytes, err = reader.Read(payload)
assert.Int(nBytes).Equals(16)
assert.Error(err).IsNil()
assert.Int(content.Len()).Equals(len2)
}
示例7: TestHubSocksOption
func TestHubSocksOption(t *testing.T) {
assert := assert.On(t)
if os.Geteuid() != 0 {
// This test case requires root permission.
return
}
hub, err := ListenUDP(v2net.LocalHostIP, v2net.Port(0), ListenOption{
Callback: func(*buf.Buffer, *proxy.SessionInfo) {},
ReceiveOriginalDest: true,
})
assert.Error(err).IsNil()
conn := hub.Connection()
fd, err := internal.GetSysFd(conn)
assert.Error(err).IsNil()
val, err := syscall.GetsockoptInt(fd, syscall.SOL_IP, syscall.IP_TRANSPARENT)
assert.Error(err).IsNil()
assert.Int(val).Equals(1)
val, err = syscall.GetsockoptInt(fd, syscall.SOL_IP, syscall.IP_RECVORIGDSTADDR)
assert.Error(err).IsNil()
assert.Int(val).Equals(1)
}
示例8: TestConfigTargetParsing
func TestConfigTargetParsing(t *testing.T) {
assert := assert.On(t)
rawJson := `{
"vnext": [{
"address": "127.0.0.1",
"port": 80,
"users": [
{
"id": "e641f5ad-9397-41e3-bf1a-e8740dfed019",
"email": "[email protected]",
"level": 255
}
]
}]
}`
rawConfig := new(VMessOutboundConfig)
err := json.Unmarshal([]byte(rawJson), &rawConfig)
assert.Error(err).IsNil()
ts, err := rawConfig.Build()
assert.Error(err).IsNil()
iConfig, err := ts.GetInstance()
assert.Error(err).IsNil()
config := iConfig.(*outbound.Config)
specPB := config.Receiver[0]
spec := protocol.NewServerSpecFromPB(*specPB)
assert.Destination(spec.Destination()).EqualsString("tcp:127.0.0.1:80")
}
示例9: TestSimpleRouter
func TestSimpleRouter(t *testing.T) {
assert := assert.On(t)
config := &Config{
Rule: []*RoutingRule{
{
Tag: "test",
NetworkList: &v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP},
},
},
},
}
space := app.NewSpace()
space.BindApp(dns.APP_ID, dns.NewCacheServer(space, &dns.Config{}))
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager())
r := NewRouter(config, space)
space.BindApp(APP_ID, r)
assert.Error(space.Initialize()).IsNil()
tag, err := r.TakeDetour(&proxy.SessionInfo{Destination: v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80)})
assert.Error(err).IsNil()
assert.String(tag).Equals("test")
}
示例10: TestSocksInboundConfig
func TestSocksInboundConfig(t *testing.T) {
assert := assert.On(t)
rawJson := `{
"auth": "password",
"accounts": [
{
"user": "my-username",
"pass": "my-password"
}
],
"udp": false,
"ip": "127.0.0.1",
"timeout": 5
}`
config := new(SocksServerConfig)
err := json.Unmarshal([]byte(rawJson), &config)
assert.Error(err).IsNil()
message, err := config.Build()
assert.Error(err).IsNil()
iConfig, err := message.GetInstance()
assert.Error(err).IsNil()
socksConfig := iConfig.(*socks.ServerConfig)
assert.Bool(socksConfig.AuthType == socks.AuthType_PASSWORD).IsTrue()
assert.Int(len(socksConfig.Accounts)).Equals(1)
assert.String(socksConfig.Accounts["my-username"]).Equals("my-password")
assert.Bool(socksConfig.UdpEnabled).IsFalse()
assert.Address(socksConfig.Address.AsAddress()).Equals(net.LocalHostIP)
assert.Uint32(socksConfig.Timeout).Equals(5)
}
示例11: TestHttpProxy
func TestHttpProxy(t *testing.T) {
assert := assert.On(t)
httpServer := &v2http.Server{
Port: v2net.Port(50042),
PathHandler: make(map[string]http.HandlerFunc),
}
_, err := httpServer.Start()
assert.Error(err).IsNil()
defer httpServer.Close()
assert.Error(InitializeServerSetOnce("test_5")).IsNil()
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse("http://127.0.0.1:50040/")
},
}
client := &http.Client{
Transport: transport,
}
resp, err := client.Get("http://127.0.0.1:50042/")
assert.Error(err).IsNil()
assert.Int(resp.StatusCode).Equals(200)
content, err := ioutil.ReadAll(resp.Body)
assert.Error(err).IsNil()
assert.String(string(content)).Equals("Home")
CloseAllServers()
}
示例12: TestBufferedReader
func TestBufferedReader(t *testing.T) {
assert := assert.On(t)
content := alloc.NewLargeBuffer()
len := content.Len()
reader := NewBufferedReader(content)
assert.Bool(reader.Cached()).IsTrue()
payload := make([]byte, 16)
nBytes, err := reader.Read(payload)
assert.Int(nBytes).Equals(16)
assert.Error(err).IsNil()
len2 := content.Len()
assert.Int(len - len2).GreaterThan(16)
nBytes, err = reader.Read(payload)
assert.Int(nBytes).Equals(16)
assert.Error(err).IsNil()
assert.Int(content.Len()).Equals(len2)
reader.SetCached(false)
payload2 := alloc.NewBuffer()
reader.Read(payload2.Value)
assert.Int(content.Len()).Equals(len2)
reader.Read(payload2.Value)
assert.Int(content.Len()).LessThan(len2)
}
示例13: TestBlockHTTP
func TestBlockHTTP(t *testing.T) {
assert := assert.On(t)
httpServer := &v2http.Server{
Port: net.Port(50042),
PathHandler: make(map[string]http.HandlerFunc),
}
_, err := httpServer.Start()
assert.Error(err).IsNil()
defer httpServer.Close()
assert.Error(InitializeServerSetOnce("test_5")).IsNil()
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse("http://127.0.0.1:50040/")
},
}
client := &http.Client{
Transport: transport,
}
resp, err := client.Get("http://127.0.0.1:50049/")
assert.Error(err).IsNil()
assert.Int(resp.StatusCode).Equals(403)
CloseAllServers()
}
示例14: TestShadowsocksServerConfigParsing
func TestShadowsocksServerConfigParsing(t *testing.T) {
assert := assert.On(t)
rawJson := `{
"method": "aes-128-cfb",
"password": "v2ray-password"
}`
rawConfig := new(ShadowsocksServerConfig)
err := json.Unmarshal([]byte(rawJson), rawConfig)
assert.Error(err).IsNil()
ts, err := rawConfig.Build()
assert.Error(err).IsNil()
iConfig, err := ts.GetInstance()
assert.Error(err).IsNil()
config := iConfig.(*shadowsocks.ServerConfig)
rawAccount, err := config.User.GetTypedAccount()
assert.Error(err).IsNil()
account, ok := rawAccount.(*shadowsocks.ShadowsocksAccount)
assert.Bool(ok).IsTrue()
assert.Int(account.Cipher.KeySize()).Equals(16)
assert.Bytes(account.Key).Equals([]byte{160, 224, 26, 2, 22, 110, 9, 80, 65, 52, 80, 20, 38, 243, 224, 241})
}
示例15: TestShadowsocksTCP
func TestShadowsocksTCP(t *testing.T) {
assert := assert.On(t)
tcpServer := &tcp.Server{
Port: v2net.Port(50052),
MsgProcessor: func(data []byte) []byte {
buffer := make([]byte, 0, 2048)
buffer = append(buffer, []byte("Processed: ")...)
buffer = append(buffer, data...)
return buffer
},
}
_, err := tcpServer.Start()
assert.Error(err).IsNil()
defer tcpServer.Close()
assert.Error(InitializeServerSetOnce("test_6")).IsNil()
for i := 0; i < 1; i++ {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: 50050,
})
assert.Error(err).IsNil()
payload := "dokodemo request."
nBytes, err := conn.Write([]byte(payload))
assert.Error(err).IsNil()
assert.Int(nBytes).Equals(len(payload))
//conn.CloseWrite()
response := buf.New()
finished := false
expectedResponse := "Processed: " + payload
for {
err := response.AppendSupplier(buf.ReadFrom(conn))
assert.Error(err).IsNil()
if err != nil {
break
}
if response.String() == expectedResponse {
finished = true
break
}
if response.Len() > len(expectedResponse) {
fmt.Printf("Unexpected response: %v\n", response.Bytes())
break
}
}
assert.Bool(finished).IsTrue()
conn.Close()
}
CloseAllServers()
}