]> git.r.bdr.sh - rbdr/mobius/blob - server_blackbox_test.go
Initial squashed commit
[rbdr/mobius] / server_blackbox_test.go
1 package hotline
2
3 // Guest login
4 // Admin login is
5 //
6 //type testCase struct {
7 // name string // test case description
8 // account Account // Account struct for a user that will test transaction will execute under
9 // request *Transaction // transaction that will be sent by the client to the server
10 // setup func() // Optional test-specific setup required for the scenario
11 // teardown func() // Optional test-specific teardown for the scenario
12 // mockHandler map[int]*mockClientHandler
13 //}
14 //
15 //func (tt *testCase) Setup(srv *Server) error {
16 // if err := srv.NewUser(tt.account.Login, tt.account.Name, NegatedUserString([]byte(tt.account.Password)), *tt.account.Access); err != nil {
17 // return err
18 // }
19 //
20 // if tt.setup != nil {
21 // tt.setup()
22 // }
23 //
24 // return nil
25 //}
26 //
27 //func (tt *testCase) Teardown(srv *Server) error {
28 // if err := srv.DeleteUser(tt.account.Login); err != nil {
29 // return err
30 // }
31 //
32 // if tt.teardown != nil {
33 // tt.teardown()
34 // }
35 //
36 // return nil
37 //}
38 //
39 //func NewTestLogger() *zap.SugaredLogger {
40 // encoderCfg := zap.NewProductionEncoderConfig()
41 // encoderCfg.TimeKey = "timestamp"
42 // encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
43 //
44 // core := zapcore.NewCore(
45 // zapcore.NewConsoleEncoder(encoderCfg),
46 // zapcore.Lock(os.Stdout),
47 // zap.DebugLevel,
48 // )
49 //
50 // cores := []zapcore.Core{core}
51 // l := zap.New(zapcore.NewTee(cores...))
52 // defer func() { _ = l.Sync() }()
53 // return l.Sugar()
54 //}
55 //
56 //func StartTestServer() (*Server, context.Context, context.CancelFunc) {
57 // ctx, cancelRoot := context.WithCancel(context.Background())
58 //
59 // srv, err := NewServer("test/config/", "localhost", 0, NewTestLogger())
60 // if err != nil {
61 // panic(err)
62 // }
63 //
64 // go func() {
65 // err := srv.ListenAndServe(ctx, cancelRoot)
66 // if err != nil {
67 // panic(err)
68 // }
69 // }()
70 //
71 // return srv, ctx, cancelRoot
72 //}
73 //
74 //func TestHandshake(t *testing.T) {
75 // srv, _, cancelFunc := StartTestServer()
76 // defer cancelFunc()
77 //
78 // port := srv.APIListener.Addr().(*net.TCPAddr).Port
79 //
80 // conn, err := net.Dial("tcp", fmt.Sprintf(":%v", port))
81 // if err != nil {
82 // t.Fatal(err)
83 // }
84 // defer conn.Close()
85 //
86 // conn.Write([]byte{0x54, 0x52, 0x54, 0x50, 0x00, 0x01, 0x00, 0x00})
87 //
88 // replyBuf := make([]byte, 8)
89 // _, _ = conn.Read(replyBuf)
90 //
91 // want := []byte{84, 82, 84, 80, 0, 0, 0, 0}
92 // if bytes.Compare(replyBuf, want) != 0 {
93 // t.Errorf("%q, want %q", replyBuf, want)
94 // }
95 //
96 //}
97 //
98 ////func TestLogin(t *testing.T) {
99 ////
100 //// tests := []struct {
101 //// name string
102 //// client *Client
103 //// }{
104 //// {
105 //// name: "when login is successful",
106 //// client: NewClient("guest", NewTestLogger()),
107 //// },
108 //// }
109 //// for _, test := range tests {
110 //// t.Run(test.name, func(t *testing.T) {
111 ////
112 //// })
113 //// }
114 ////}
115 //
116 //func TestNewUser(t *testing.T) {
117 // srv, _, _ := StartTestServer()
118 //
119 // tests := []testCase{
120 // //{
121 // // name: "a valid new account",
122 // // mockHandler: func() mockClientHandler {
123 // // mh := mockClientHandler{}
124 // // mh.On("Handle", mock.AnythingOfType("*hotline.Client"), mock.MatchedBy(func(t *Transaction) bool {
125 // // println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
126 // // spew.Dump(t.Type)
127 // // spew.Dump(bytes.Equal(t.Type, []byte{0x01, 0x5e}))
128 // // //if !bytes.Equal(t.GetField(fieldError).Data, []byte("You are not allowed to create new accounts.")) {
129 // // // return false
130 // // //}
131 // // return bytes.Equal(t.Type, []byte{0x01, 0x5e},
132 // // )
133 // // })).Return(
134 // // []Transaction{}, nil,
135 // // )
136 // //
137 // // clientHandlers[tranNewUser] = mh
138 // // return mh
139 // // }(),
140 // // client: func() *Client {
141 // // c := NewClient("testUser", NewTestLogger())
142 // // return c
143 // // }(),
144 // // teardown: func() {
145 // // _ = srv.DeleteUser("testUser")
146 // // },
147 // // account: Account{
148 // // Login: "test",
149 // // Name: "unnamed",
150 // // Password: "test",
151 // // Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
152 // // },
153 // // request: NewTransaction(
154 // // tranNewUser, nil,
155 // // NewField(fieldUserLogin, []byte(NegatedUserString([]byte("testUser")))),
156 // // NewField(fieldUserName, []byte("testUserName")),
157 // // NewField(fieldUserPassword, []byte(NegatedUserString([]byte("testPw")))),
158 // // NewField(fieldUserAccess, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
159 // // ),
160 // // want: &Transaction{
161 // // Fields: []Field{},
162 // // },
163 // //},
164 // //{
165 // // name: "a newUser request from a user without the required access",
166 // // mockHandler: func() *mockClientHandler {
167 // // mh := mockClientHandler{}
168 // // mh.On("Handle", mock.AnythingOfType("*hotline.Client"), mock.MatchedBy(func(t *Transaction) bool {
169 // // if !bytes.Equal(t.GetField(fieldError).Data, []byte("You are not allowed to create new accounts.")) {
170 // // return false
171 // // }
172 // // return bytes.Equal(t.Type, []byte{0x01, 0x5e})
173 // // })).Return(
174 // // []Transaction{}, nil,
175 // // )
176 // // return &mh
177 // // }(),
178 // // teardown: func() {
179 // // _ = srv.DeleteUser("testUser")
180 // // },
181 // // account: Account{
182 // // Login: "test",
183 // // Name: "unnamed",
184 // // Password: "test",
185 // // Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
186 // // },
187 // // request: NewTransaction(
188 // // tranNewUser, nil,
189 // // NewField(fieldUserLogin, []byte(NegatedUserString([]byte("testUser")))),
190 // // NewField(fieldUserName, []byte("testUserName")),
191 // // NewField(fieldUserPassword, []byte(NegatedUserString([]byte("testPw")))),
192 // // NewField(fieldUserAccess, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
193 // // ),
194 // //},
195 // {
196 // name: "when user does not have required permission",
197 // mockHandler: func() map[int]*mockClientHandler {
198 // mockHandlers := make(map[int]*mockClientHandler)
199 //
200 // mh := mockClientHandler{}
201 // mh.On("Handle", mock.AnythingOfType("*hotline.Client"), mock.MatchedBy(func(t *Transaction) bool {
202 // return t.equal(Transaction{
203 // Type: []byte{0x01, 0x5e},
204 // IsReply: 1,
205 // ErrorCode: []byte{0, 0, 0, 1},
206 // Fields: []Field{
207 // NewField(fieldError, []byte("You are not allowed to create new accounts.")),
208 // },
209 // })
210 // })).Return(
211 // []Transaction{}, nil,
212 // )
213 // mockHandlers[tranNewUser] = &mh
214 //
215 // return mockHandlers
216 // }(),
217 //
218 // teardown: func() {
219 // _ = srv.DeleteUser("testUser")
220 // },
221 // account: Account{
222 // Login: "test",
223 // Name: "unnamed",
224 // Password: "test",
225 // Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
226 // },
227 // request: NewTransaction(
228 // tranNewUser, nil,
229 // NewField(fieldUserLogin, []byte(NegatedUserString([]byte("testUser")))),
230 // NewField(fieldUserName, []byte("testUserName")),
231 // NewField(fieldUserPassword, []byte(NegatedUserString([]byte("testPw")))),
232 // NewField(fieldUserAccess, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
233 // ),
234 // },
235 //
236 // //{
237 // // name: "a request to create a user that already exists",
238 // // setup: func() {
239 // //
240 // // },
241 // // teardown: func() {
242 // // _ = srv.DeleteUser("testUser")
243 // // },
244 // // account: Account{
245 // // Login: "test",
246 // // Name: "unnamed",
247 // // Password: "test",
248 // // Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
249 // // },
250 // // request: NewTransaction(
251 // // tranNewUser, nil,
252 // // NewField(fieldUserLogin, []byte(NegatedUserString([]byte("guest")))),
253 // // NewField(fieldUserName, []byte("testUserName")),
254 // // NewField(fieldUserPassword, []byte(NegatedUserString([]byte("testPw")))),
255 // // NewField(fieldUserAccess, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
256 // // ),
257 // // want: &Transaction{
258 // // Fields: []Field{
259 // // NewField(fieldError, []byte("Cannot create account guest because there is already an account with that login.")),
260 // // },
261 // // },
262 // //},
263 // }
264 //
265 // for _, test := range tests {
266 // t.Run(test.name, func(t *testing.T) {
267 // test.Setup(srv)
268 //
269 // // move to Setup?
270 // c := NewClient(test.account.Name, NewTestLogger())
271 // err := c.JoinServer(fmt.Sprintf(":%v", srv.APIPort()), test.account.Login, test.account.Password)
272 // if err != nil {
273 // t.Errorf("login failed: %v", err)
274 // }
275 // // end move to Setup??
276 //
277 // for key, value := range test.mockHandler {
278 // c.Handlers[uint16(key)] = value
279 // }
280 //
281 // // send test case request
282 // _ = c.Send(*test.request)
283 //
284 // //time.Sleep(1 * time.Second)
285 // // ===
286 //
287 // transactions, _ := readN(c.Connection, 1)
288 // for _, t := range transactions {
289 // _ = c.HandleTransaction(&t)
290 // }
291 //
292 // // ===
293 //
294 // for _, handler := range test.mockHandler {
295 // handler.AssertExpectations(t)
296 // }
297 //
298 // test.Teardown(srv)
299 // })
300 // }
301 //}
302 //
303 //// equal is a utility function used only in tests that determines if transactions are equal enough
304 //func (t Transaction) equal(otherT Transaction) bool {
305 // t.ID = []byte{0, 0, 0, 0}
306 // otherT.ID = []byte{0, 0, 0, 0}
307 //
308 // t.TotalSize = []byte{0, 0, 0, 0}
309 // otherT.TotalSize = []byte{0, 0, 0, 0}
310 //
311 // t.DataSize = []byte{0, 0, 0, 0}
312 // otherT.DataSize = []byte{0, 0, 0, 0}
313 //
314 // t.ParamCount = []byte{0, 0}
315 // otherT.ParamCount = []byte{0, 0}
316 //
317 // //spew.Dump(t)
318 // //spew.Dump(otherT)
319 //
320 // return reflect.DeepEqual(t, otherT)
321 //}