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
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 {
20 // if tt.setup != nil {
27 //func (tt *testCase) Teardown(srv *Server) error {
28 // if err := srv.DeleteUser(tt.account.Login); err != nil {
32 // if tt.teardown != nil {
39 //func NewTestLogger() *zap.SugaredLogger {
40 // encoderCfg := zap.NewProductionEncoderConfig()
41 // encoderCfg.TimeKey = "timestamp"
42 // encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
44 // core := zapcore.NewCore(
45 // zapcore.NewConsoleEncoder(encoderCfg),
46 // zapcore.Lock(os.Stdout),
50 // cores := []zapcore.Core{core}
51 // l := zap.New(zapcore.NewTee(cores...))
52 // defer func() { _ = l.Sync() }()
56 //func StartTestServer() (*Server, context.Context, context.CancelFunc) {
57 // ctx, cancelRoot := context.WithCancel(context.Background())
59 // srv, err := NewServer("test/config/", "localhost", 0, NewTestLogger())
65 // err := srv.ListenAndServe(ctx, cancelRoot)
71 // return srv, ctx, cancelRoot
74 //func TestHandshake(t *testing.T) {
75 // srv, _, cancelFunc := StartTestServer()
78 // port := srv.APIListener.Addr().(*net.TCPAddr).Port
80 // conn, err := net.Dial("tcp", fmt.Sprintf(":%v", port))
86 // conn.Write([]byte{0x54, 0x52, 0x54, 0x50, 0x00, 0x01, 0x00, 0x00})
88 // replyBuf := make([]byte, 8)
89 // _, _ = conn.Read(replyBuf)
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)
98 ////func TestLogin(t *testing.T) {
100 //// tests := []struct {
105 //// name: "when login is successful",
106 //// client: NewClient("guest", NewTestLogger()),
109 //// for _, test := range tests {
110 //// t.Run(test.name, func(t *testing.T) {
116 //func TestNewUser(t *testing.T) {
117 // srv, _, _ := StartTestServer()
119 // tests := []testCase{
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
131 // // return bytes.Equal(t.Type, []byte{0x01, 0x5e},
134 // // []Transaction{}, nil,
137 // // clientHandlers[tranNewUser] = mh
140 // // client: func() *Client {
141 // // c := NewClient("testUser", NewTestLogger())
144 // // teardown: func() {
145 // // _ = srv.DeleteUser("testUser")
147 // // account: Account{
149 // // Name: "unnamed",
150 // // Password: "test",
151 // // Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
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}),
160 // // want: &Transaction{
161 // // Fields: []Field{},
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.")) {
172 // // return bytes.Equal(t.Type, []byte{0x01, 0x5e})
174 // // []Transaction{}, nil,
178 // // teardown: func() {
179 // // _ = srv.DeleteUser("testUser")
181 // // account: Account{
183 // // Name: "unnamed",
184 // // Password: "test",
185 // // Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
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}),
196 // name: "when user does not have required permission",
197 // mockHandler: func() map[int]*mockClientHandler {
198 // mockHandlers := make(map[int]*mockClientHandler)
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},
205 // ErrorCode: []byte{0, 0, 0, 1},
207 // NewField(fieldError, []byte("You are not allowed to create new accounts.")),
211 // []Transaction{}, nil,
213 // mockHandlers[tranNewUser] = &mh
215 // return mockHandlers
218 // teardown: func() {
219 // _ = srv.DeleteUser("testUser")
225 // Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
227 // request: NewTransaction(
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}),
237 // // name: "a request to create a user that already exists",
238 // // setup: func() {
241 // // teardown: func() {
242 // // _ = srv.DeleteUser("testUser")
244 // // account: Account{
246 // // Name: "unnamed",
247 // // Password: "test",
248 // // Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
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}),
257 // // want: &Transaction{
258 // // Fields: []Field{
259 // // NewField(fieldError, []byte("Cannot create account guest because there is already an account with that login.")),
265 // for _, test := range tests {
266 // t.Run(test.name, func(t *testing.T) {
270 // c := NewClient(test.account.Name, NewTestLogger())
271 // err := c.JoinServer(fmt.Sprintf(":%v", srv.APIPort()), test.account.Login, test.account.Password)
273 // t.Errorf("login failed: %v", err)
275 // // end move to Setup??
277 // for key, value := range test.mockHandler {
278 // c.Handlers[uint16(key)] = value
281 // // send test case request
282 // _ = c.Send(*test.request)
284 // //time.Sleep(1 * time.Second)
287 // transactions, _ := readN(c.Connection, 1)
288 // for _, t := range transactions {
289 // _ = c.HandleTransaction(&t)
294 // for _, handler := range test.mockHandler {
295 // handler.AssertExpectations(t)
298 // test.Teardown(srv)
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}
308 // t.TotalSize = []byte{0, 0, 0, 0}
309 // otherT.TotalSize = []byte{0, 0, 0, 0}
311 // t.DataSize = []byte{0, 0, 0, 0}
312 // otherT.DataSize = []byte{0, 0, 0, 0}
314 // t.ParamCount = []byte{0, 0}
315 // otherT.ParamCount = []byte{0, 0}
318 // //spew.Dump(otherT)
320 // return reflect.DeepEqual(t, otherT)