aboutsummaryrefslogtreecommitdiff
path: root/hotline/server_blackbox_test.go
blob: 3718bd29c560d334979e1b77bf239c92ad78d254 (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package hotline

// Guest login
// Admin login is
//
//type testCase struct {
//	name        string       // test case description
//	account     Account      // Account struct for a user that will test transaction will execute under
//	request     *Transaction // transaction that will be sent by the client to the server
//	setup       func()       // Optional test-specific setup required for the scenario
//	teardown    func()       // Optional test-specific teardown for the scenario
//	mockHandler map[int]*mockClientHandler
//}
//
//func (tt *testCase) Setup(srv *Server) error {
//	if err := srv.NewUser(tt.account.Login, tt.account.Name, NegatedUserString([]byte(tt.account.Password)), *tt.account.Access); err != nil {
//		return err
//	}
//
//	if tt.setup != nil {
//		tt.setup()
//	}
//
//	return nil
//}
//
//func (tt *testCase) Teardown(srv *Server) error {
//	if err := srv.DeleteUser(tt.account.Login); err != nil {
//		return err
//	}
//
//	if tt.teardown != nil {
//		tt.teardown()
//	}
//
//	return nil
//}
//
//func NewTestLogger() *zap.SugaredLogger {
//	encoderCfg := zap.NewProductionEncoderConfig()
//	encoderCfg.TimeKey = "timestamp"
//	encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
//
//	core := zapcore.NewCore(
//		zapcore.NewConsoleEncoder(encoderCfg),
//		zapcore.Lock(os.Stdout),
//		zap.DebugLevel,
//	)
//
//	cores := []zapcore.Core{core}
//	l := zap.New(zapcore.NewTee(cores...))
//	defer func() { _ = l.Sync() }()
//	return l.Sugar()
//}
//
//func StartTestServer() (*Server, context.Context, context.CancelFunc) {
//	ctx, cancelRoot := context.WithCancel(context.Background())
//
//	srv, err := NewServer("test/config/", "localhost", 0, NewTestLogger())
//	if err != nil {
//		panic(err)
//	}
//
//	go func() {
//		err := srv.ListenAndServe(ctx, cancelRoot)
//		if err != nil {
//			panic(err)
//		}
//	}()
//
//	return srv, ctx, cancelRoot
//}
//
//func TestHandshake(t *testing.T) {
//	srv, _, cancelFunc := StartTestServer()
//	defer cancelFunc()
//
//	port := srv.APIListener.Addr().(*net.TCPAddr).Port
//
//	conn, err := net.Dial("tcp", fmt.Sprintf(":%v", port))
//	if err != nil {
//		t.Fatal(err)
//	}
//	defer conn.Close()
//
//	conn.Write([]byte{0x54, 0x52, 0x54, 0x50, 0x00, 0x01, 0x00, 0x00})
//
//	replyBuf := make([]byte, 8)
//	_, _ = conn.Read(replyBuf)
//
//	want := []byte{84, 82, 84, 80, 0, 0, 0, 0}
//	if bytes.Compare(replyBuf, want) != 0 {
//		t.Errorf("%q, want %q", replyBuf, want)
//	}
//
//}
//
////func TestLogin(t *testing.T) {
////
////	tests := []struct {
////		name   string
////		client *Client
////	}{
////		{
////			name:   "when login is successful",
////			client: NewClient("guest", NewTestLogger()),
////		},
////	}
////	for _, test := range tests {
////		t.Run(test.name, func(t *testing.T) {
////
////		})
////	}
////}
//
//func TestNewUser(t *testing.T) {
//	srv, _, _ := StartTestServer()
//
//	tests := []testCase{
//		//{
//		//	name: "a valid new account",
//		//	mockHandler: func() mockClientHandler {
//		//		mh := mockClientHandler{}
//		//		mh.On("Handle", mock.AnythingOfType("*hotline.Client"), mock.MatchedBy(func(t *Transaction) bool {
//		//			println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
//		//			spew.Dump(t.Type)
//		//			spew.Dump(bytes.Equal(t.Type, []byte{0x01, 0x5e}))
//		//			//if !bytes.Equal(t.GetField(fieldError).Data, []byte("You are not allowed to create new accounts.")) {
//		//			//	return false
//		//			//}
//		//			return bytes.Equal(t.Type, []byte{0x01, 0x5e},
//		//			)
//		//		})).Return(
//		//			[]Transaction{}, nil,
//		//		)
//		//
//		//		clientHandlers[tranNewUser] = mh
//		//		return mh
//		//	}(),
//		//	client: func() *Client {
//		//		c := NewClient("testUser", NewTestLogger())
//		//		return c
//		//	}(),
//		//	teardown: func() {
//		//		_ = srv.DeleteUser("testUser")
//		//	},
//		//	account: Account{
//		//		Login:    "test",
//		//		Name:     "unnamed",
//		//		Password: "test",
//		//		Access:   &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
//		//	},
//		//	request: NewTransaction(
//		//		tranNewUser, nil,
//		//		NewField(fieldUserLogin, []byte(NegatedUserString([]byte("testUser")))),
//		//		NewField(fieldUserName, []byte("testUserName")),
//		//		NewField(fieldUserPassword, []byte(NegatedUserString([]byte("testPw")))),
//		//		NewField(fieldUserAccess, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
//		//	),
//		//	want: &Transaction{
//		//		Fields: []Field{},
//		//	},
//		//},
//		//{
//		//	name: "a newUser request from a user without the required access",
//		//	mockHandler: func() *mockClientHandler {
//		//		mh := mockClientHandler{}
//		//		mh.On("Handle", mock.AnythingOfType("*hotline.Client"), mock.MatchedBy(func(t *Transaction) bool {
//		//			if !bytes.Equal(t.GetField(fieldError).Data, []byte("You are not allowed to create new accounts.")) {
//		//				return false
//		//			}
//		//			return bytes.Equal(t.Type, []byte{0x01, 0x5e})
//		//		})).Return(
//		//			[]Transaction{}, nil,
//		//		)
//		//		return &mh
//		//	}(),
//		//	teardown: func() {
//		//		_ = srv.DeleteUser("testUser")
//		//	},
//		//	account: Account{
//		//		Login:    "test",
//		//		Name:     "unnamed",
//		//		Password: "test",
//		//		Access:   &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
//		//	},
//		//	request: NewTransaction(
//		//		tranNewUser, nil,
//		//		NewField(fieldUserLogin, []byte(NegatedUserString([]byte("testUser")))),
//		//		NewField(fieldUserName, []byte("testUserName")),
//		//		NewField(fieldUserPassword, []byte(NegatedUserString([]byte("testPw")))),
//		//		NewField(fieldUserAccess, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
//		//	),
//		//},
//		{
//			name: "when user does not have required permission",
//			mockHandler: func() map[int]*mockClientHandler {
//				mockHandlers := make(map[int]*mockClientHandler)
//
//				mh := mockClientHandler{}
//				mh.On("Handle", mock.AnythingOfType("*hotline.Client"), mock.MatchedBy(func(t *Transaction) bool {
//					return t.equal(Transaction{
//						Type:      []byte{0x01, 0x5e},
//						IsReply:   1,
//						ErrorCode: []byte{0, 0, 0, 1},
//						Fields: []Field{
//							NewField(fieldError, []byte("You are not allowed to create new accounts.")),
//						},
//					})
//				})).Return(
//					[]Transaction{}, nil,
//				)
//				mockHandlers[tranNewUser] = &mh
//
//				return mockHandlers
//			}(),
//
//			teardown: func() {
//				_ = srv.DeleteUser("testUser")
//			},
//			account: Account{
//				Login:    "test",
//				Name:     "unnamed",
//				Password: "test",
//				Access:   &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
//			},
//			request: NewTransaction(
//				tranNewUser, nil,
//				NewField(fieldUserLogin, []byte(NegatedUserString([]byte("testUser")))),
//				NewField(fieldUserName, []byte("testUserName")),
//				NewField(fieldUserPassword, []byte(NegatedUserString([]byte("testPw")))),
//				NewField(fieldUserAccess, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
//			),
//		},
//
//		//{
//		//	name: "a request to create a user that already exists",
//		//	setup: func() {
//		//
//		//	},
//		//	teardown: func() {
//		//		_ = srv.DeleteUser("testUser")
//		//	},
//		//	account: Account{
//		//		Login:    "test",
//		//		Name:     "unnamed",
//		//		Password: "test",
//		//		Access:   &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
//		//	},
//		//	request: NewTransaction(
//		//		tranNewUser, nil,
//		//		NewField(fieldUserLogin, []byte(NegatedUserString([]byte("guest")))),
//		//		NewField(fieldUserName, []byte("testUserName")),
//		//		NewField(fieldUserPassword, []byte(NegatedUserString([]byte("testPw")))),
//		//		NewField(fieldUserAccess, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
//		//	),
//		//	want: &Transaction{
//		//		Fields: []Field{
//		//			NewField(fieldError, []byte("Cannot create account guest because there is already an account with that login.")),
//		//		},
//		//	},
//		//},
//	}
//
//	for _, test := range tests {
//		t.Run(test.name, func(t *testing.T) {
//			test.Setup(srv)
//
//			// move to Setup?
//			c := NewClient(test.account.Name, NewTestLogger())
//			err := c.JoinServer(fmt.Sprintf(":%v", srv.APIPort()), test.account.Login, test.account.Password)
//			if err != nil {
//				t.Errorf("login failed: %v", err)
//			}
//			// end move to Setup??
//
//			for key, value := range test.mockHandler {
//				c.Handlers[uint16(key)] = value
//			}
//
//			// send test case request
//			_ = c.Send(*test.request)
//
//			//time.Sleep(1 * time.Second)
//			// ===
//
//			transactions, _ := readN(c.Connection, 1)
//			for _, t := range transactions {
//				_ = c.HandleTransaction(&t)
//			}
//
//			// ===
//
//			for _, handler := range test.mockHandler {
//				handler.AssertExpectations(t)
//			}
//
//			test.Teardown(srv)
//		})
//	}
//}
//
//// equal is a utility function used only in tests that determines if transactions are equal enough
//func (t Transaction) equal(otherT Transaction) bool {
//	t.ID = []byte{0, 0, 0, 0}
//	otherT.ID = []byte{0, 0, 0, 0}
//
//	t.TotalSize = []byte{0, 0, 0, 0}
//	otherT.TotalSize = []byte{0, 0, 0, 0}
//
//	t.DataSize = []byte{0, 0, 0, 0}
//	otherT.DataSize = []byte{0, 0, 0, 0}
//
//	t.ParamCount = []byte{0, 0}
//	otherT.ParamCount = []byte{0, 0}
//
//	//spew.Dump(t)
//	//spew.Dump(otherT)
//
//	return reflect.DeepEqual(t, otherT)
//}