"bytes"
"context"
"fmt"
+ "github.com/davecgh/go-spew/spew"
+ "github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"net"
"os"
- "reflect"
"testing"
)
}
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 {
+ if err := srv.NewUser(tt.account.Login, tt.account.Name, string(negateString([]byte(tt.account.Password))), *tt.account.Access); err != nil {
return err
}
func StartTestServer() (*Server, context.Context, context.CancelFunc) {
ctx, cancelRoot := context.WithCancel(context.Background())
+ FS = &OSFileStore{}
+
srv, err := NewServer("test/config/", "localhost", 0, NewTestLogger())
if err != nil {
panic(err)
}
func TestHandshake(t *testing.T) {
+ mfs := &MockFileStore{}
+ fh, _ := os.Open("./test/config/Agreement.txt")
+ mfs.On("Open", "/test/config/Agreement.txt").Return(fh, nil)
+ fh, _ = os.Open("./test/config/config.yaml")
+ mfs.On("Open", "/test/config/config.yaml").Return(fh, nil)
+ FS = mfs
+ spew.Dump(mfs)
+
srv, _, cancelFunc := StartTestServer()
defer cancelFunc()
}
defer conn.Close()
- conn.Write([]byte{0x54, 0x52, 0x54, 0x50, 0x00, 0x01, 0x00, 0x00})
+ conn.Write([]byte{0x54, 0x52, 0x54, 0x50, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02})
replyBuf := make([]byte, 8)
_, _ = conn.Read(replyBuf)
}
-//func TestLogin(t *testing.T) {
+// func TestLogin(t *testing.T) {
//
// tests := []struct {
// name string
//
// })
// }
-//}
-
+// }
func TestNewUser(t *testing.T) {
srv, _, _ := StartTestServer()
tests := []testCase{
- //{
+ // {
// name: "a valid new account",
// mockHandler: func() mockClientHandler {
// mh := mockClientHandler{}
// want: &Transaction{
// Fields: []Field{},
// },
- //},
- //{
+ // },
+ // {
// name: "a newUser request from a user without the required access",
// mockHandler: func() *mockClientHandler {
// mh := mockClientHandler{}
// 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)
// 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() {
//
// NewField(fieldError, []byte("Cannot create account guest because there is already an account with that login.")),
// },
// },
- //},
+ // },
}
for _, test := range tests {
// send test case request
_ = c.Send(*test.request)
- //time.Sleep(1 * time.Second)
+ // time.Sleep(1 * time.Second)
// ===
transactions, _ := readN(c.Connection, 1)
}
}
+// tranAssertEqual compares equality of transactions slices after stripping out the random ID
+func tranAssertEqual(t *testing.T, tran1, tran2 []Transaction) bool {
+ var newT1 []Transaction
+ var newT2 []Transaction
+ for _, trans := range tran1 {
+ trans.ID = []byte{0, 0, 0, 0}
+ newT1 = append(newT1, trans)
+ }
+ for _, trans := range tran2 {
+ trans.ID = []byte{0, 0, 0, 0}
+ newT2 = append(newT2, trans)
-// 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)
+ return assert.Equal(t, newT1, newT2)
}