1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// Copyright 2022 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package proxyapp
import (
"net"
"net/rpc"
"net/rpc/jsonrpc"
"net/url"
"sync"
"testing"
"github.com/google/syzkaller/vm/proxyapp/proxyrpc"
"github.com/google/syzkaller/vm/vmimpl"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func testTCPEnv(port string) *vmimpl.Env {
return &vmimpl.Env{
Config: []byte(`
{
"rpc_server_uri": "localhost:` + port + `",
"config": {
"internal_values": 123
}
}
`)}
}
func proxyAppServerTCPFixture(t *testing.T) (*mockProxyAppInterface, string, *proxyAppParams) {
mProxyAppServer, port, _ := makeMockProxyAppServer(t)
return initProxyAppServerFixture(mProxyAppServer), port, makeTestParams()
}
func TestCtor_TCP_Ok(t *testing.T) {
_, port, params := proxyAppServerTCPFixture(t)
p, err := ctor(params, testTCPEnv(port))
assert.Nil(t, err)
assert.Equal(t, 2, p.Count())
}
func TestCtor_TCP_WrongPort(t *testing.T) {
p, err := ctor(makeTestParams(), testTCPEnv("5"))
assert.NotNil(t, err)
assert.Nil(t, p)
}
func TestCtor_TCP_Reconnect_On_LostConnection(t *testing.T) {
mProxyAppServer, port, closeServerConnections := makeMockProxyAppServer(t)
onConnect := make(chan bool, 1)
mProxyAppServer.
On("CreatePool", mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
out := args.Get(1).(*proxyrpc.CreatePoolResult)
out.Count = 2
onConnect <- true
}).
Return(nil).
Times(2).
On("PoolLogs", mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
select {
case mProxyAppServer.OnLogsReceived <- true:
default:
}
}).
Return(nil)
ctor(makeTestParams(), testTCPEnv(port))
<-onConnect
<-mProxyAppServer.OnLogsReceived
closeServerConnections()
<-onConnect
<-mProxyAppServer.OnLogsReceived
}
func TestCtor_TCP_Reconnect_PoolChanged(t *testing.T) {
mProxyAppServer, port, closeServerConnections := makeMockProxyAppServer(t)
onConnect := make(chan bool, 1)
mProxyAppServer.
On("CreatePool", mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
out := args.Get(1).(*proxyrpc.CreatePoolResult)
out.Count = 2
onConnect <- true
}).
Return(nil).
Once().
On("CreatePool", mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
out := args.Get(1).(*proxyrpc.CreatePoolResult)
out.Count = 1
onConnect <- true
}).
Return(nil).
On("PoolLogs", mock.Anything, mock.Anything).
Return(nil)
p, _ := ctor(makeTestParams(), testTCPEnv(port))
<-onConnect
closeServerConnections()
for i := 0; i < 10; i++ {
<-onConnect
p.(*pool).mu.Lock()
assert.Nil(t, p.(*pool).proxy) // still can't initialize
p.(*pool).mu.Unlock()
}
}
func makeMockProxyAppServer(t *testing.T) (*mockProxyAppInterface, string, func()) {
handler := makeMockProxyAppInterface(t)
server := rpc.NewServer()
server.RegisterName("ProxyVM", struct{ proxyrpc.ProxyAppInterface }{handler})
l, e := net.Listen("tcp", ":0")
if e != nil {
t.Fatalf("listen error: %v", e)
}
dest, err := url.Parse("http://" + l.Addr().String())
if err != nil {
t.Fatalf("failed to get server endpoint addr: %v", err)
}
connsMu := sync.Mutex{}
var conns []net.Conn
go func() {
for {
conn, err := l.Accept()
if err != nil {
panic("failed to accept connection")
}
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
connsMu.Lock()
conns = append(conns, conn)
connsMu.Unlock()
}
}()
return handler, dest.Port(), func() {
connsMu.Lock()
defer connsMu.Unlock()
for _, conn := range conns {
conn.Close()
}
}
}
|