forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 3
/
blackbox_test.go
241 lines (196 loc) · 5.97 KB
/
blackbox_test.go
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// @FIXME(gfanton): auto relay can occasionally rise data race in some tests,
// disabling race for now
//go:build !race
// +build !race
package bertybridge_test
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"github.com/gogo/protobuf/proto"
"berty.tech/berty/v2/go/framework/bertybridge"
"berty.tech/berty/v2/go/pkg/accounttypes"
bridge_svc "berty.tech/berty/v2/go/pkg/bertybridge"
"berty.tech/berty/v2/go/pkg/protocoltypes"
)
func Example() {
tmpdir, err := ioutil.TempDir("", "example")
checkErr(err)
defer os.RemoveAll(tmpdir)
// create and start the bridge
var b *bertybridge.Bridge
{
config := bertybridge.NewConfig()
if false { // disabled in example, but not commented to be sure that compiler performs various checks
config.SetLifeCycleDriver(nil)
config.SetLoggerDriver(nil)
config.SetNotificationDriver(nil)
}
// set bridge root dir
config.SetRootDir(tmpdir)
b, err = bertybridge.NewBridge(config)
checkErr(err)
defer b.Close()
fmt.Println("[+] initialized.")
}
args := []string{
"--log.filters=info+:bty*,-*.grpc warn+:*.grpc error+:*",
"--log.format=console",
"--node.display-name=",
"--node.listeners=/ip4/127.0.0.1/tcp/0/grpcws",
"--p2p.swarm-listeners=/ip4/127.0.0.1/tcp/0,/ip6/::1/tcp/0",
"--p2p.mdns=false",
"--p2p.webui-listener=:3000",
"--store.dir=" + tmpdir,
}
var accountID string
// create account
{
// create `CreateAccount` Input
input := &accounttypes.CreateAccount_Request{}
payload, err := proto.Marshal(input)
checkErr(err)
// Serialize request
in := &bridge_svc.ClientInvokeUnary_Request{
MethodDesc: &bridge_svc.MethodDesc{
Name: "/berty.account.v1.AccountService/CreateAccount",
},
Payload: payload,
}
reqb64, err := encodeProtoMessage(in)
checkErr(err)
// invoke through bridge client
ret, err := b.InvokeBridgeMethod("/berty.bridge.v1.BridgeService/ClientInvokeUnary", reqb64)
checkErr(err)
// deserialize reply
var output bridge_svc.ClientInvokeUnary_Reply
err = decodeProtoMessage(ret, &output)
checkErr(err)
// deserialize reply
var res accounttypes.CreateAccount_Reply
err = proto.Unmarshal(output.Payload, &res)
checkErr(err)
accountID = res.GetAccountMetadata().GetAccountID()
fmt.Println("[+] account created.")
}
// open account
{
// create `CreateAccount` Input
input := &accounttypes.OpenAccount_Request{
AccountID: accountID,
Args: args,
}
payload, err := proto.Marshal(input)
checkErr(err)
// Serialize request
in := &bridge_svc.ClientInvokeUnary_Request{
MethodDesc: &bridge_svc.MethodDesc{
Name: "/berty.account.v1.AccountService/OpenAccount",
},
Payload: payload,
}
reqb64, err := encodeProtoMessage(in)
checkErr(err)
// invoke through bridge client
ret, err := b.InvokeBridgeMethod("/berty.bridge.v1.BridgeService/ClientInvokeUnary", reqb64)
checkErr(err)
// deserialize reply
var output bridge_svc.ClientInvokeUnary_Reply
err = decodeProtoMessage(ret, &output)
checkErr(err)
fmt.Println("[+] account opened.")
}
// check for GRPC listeners
{
// create `InstanceGetConfiguration` Input
input := &accounttypes.GetGRPCListenerAddrs_Request{}
payload, err := proto.Marshal(input)
checkErr(err)
// Serialize request
in := &bridge_svc.ClientInvokeUnary_Request{
MethodDesc: &bridge_svc.MethodDesc{
Name: "/berty.account.v1.AccountService/GetGRPCListenerAddrs",
},
Payload: payload,
}
reqb64, err := encodeProtoMessage(in)
checkErr(err)
// invoke through bridge client
ret, err := b.InvokeBridgeMethod("/berty.bridge.v1.BridgeService/ClientInvokeUnary", reqb64)
checkErr(err)
var output bridge_svc.ClientInvokeUnary_Reply
err = decodeProtoMessage(ret, &output)
checkErr(err)
// deserialize reply
var res accounttypes.GetGRPCListenerAddrs_Reply
err = proto.Unmarshal(output.Payload, &res)
checkErr(err)
hasGRPCWeb := false
hasGRPCWebSocket := false
for _, entry := range res.Entries {
switch entry.Proto {
case "ip4/tcp/grpcweb":
hasGRPCWeb = true
case "ip4/tcp/grpcws":
hasGRPCWebSocket = true
}
}
fmt.Println("[+] has grpc-web listener: ", hasGRPCWeb) // no, because `--node.listeners` does not contain grpcweb
fmt.Println("[+] has websocket listener: ", hasGRPCWebSocket) // yes, because `--node.listeners`` contains grpcws
}
// make unary call to underlying `BertyMessenger` Service
{
// create `InstanceGetConfiguration` Input
input := &protocoltypes.InstanceGetConfiguration_Request{}
payload, err := proto.Marshal(input)
checkErr(err)
// Serialize request
in := &bridge_svc.ClientInvokeUnary_Request{
MethodDesc: &bridge_svc.MethodDesc{
Name: "/berty.protocol.v1.ProtocolService/InstanceGetConfiguration",
},
Payload: payload,
}
reqb64, err := encodeProtoMessage(in)
checkErr(err)
// invoke through bridge client
ret, err := b.InvokeBridgeMethod("/berty.bridge.v1.BridgeService/ClientInvokeUnary", reqb64)
checkErr(err)
var output bridge_svc.ClientInvokeUnary_Reply
err = decodeProtoMessage(ret, &output)
checkErr(err)
var res protocoltypes.InstanceGetConfiguration_Reply
err = proto.Unmarshal(output.Payload, &res)
checkErr(err)
fmt.Println("[+] has more than one swarm listener:", len(res.Listeners) > 1)
// log.Println("ret", godev.PrettyJSON(output))
}
// Output:
// [+] initialized.
// [+] account created.
// [+] account opened.
// [+] has grpc-web listener: false
// [+] has websocket listener: true
// [+] has more than one swarm listener: true
}
func checkErr(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
panic(err)
}
}
func decodeProtoMessage(input string, output proto.Message) error {
dec, err := base64.StdEncoding.DecodeString(input)
if err != nil {
return err
}
return proto.Unmarshal(dec, output)
}
func encodeProtoMessage(input proto.Message) (string, error) {
data, err := proto.Marshal(input)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(data), nil
}