t *Transaction
}
tests := []struct {
- setup func()
name string
args args
wantRes []Transaction
wantErr bool
}{
+ {
+ name: "without required permission",
+ args: args{
+ cc: &ClientConn{
+ Account: &Account{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ access := bits[:]
+ return &access
+ }(),
+ },
+ },
+ t: NewTransaction(
+ accessCreateFolder,
+ &[]byte{0, 0},
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0, 0x00},
+ ID: []byte{0x9a, 0xcb, 0x04, 0x42},
+ ErrorCode: []byte{0, 0, 0, 1},
+ Fields: []Field{
+ NewField(fieldError, []byte("You are not allowed to create folders.")),
+ },
+ },
+ },
+ wantErr: false,
+ },
{
name: "when path is nested",
args: args{
cc: &ClientConn{
+ Account: &Account{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ bits.Set(accessCreateFolder)
+ access := bits[:]
+ return &access
+ }(),
+ },
ID: &[]byte{0, 1},
Server: &Server{
Config: &Config{
FileRoot: "/Files/",
},
+ FS: func() *MockFileStore {
+ mfs := &MockFileStore{}
+ mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
+ mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
+ return mfs
+ }(),
},
},
t: NewTransaction(
}),
),
},
- setup: func() {
- mfs := &MockFileStore{}
- mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
- mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
- FS = mfs
- },
wantRes: []Transaction{
{
clientID: &[]byte{0, 1},
name: "when path is not nested",
args: args{
cc: &ClientConn{
+ Account: &Account{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ bits.Set(accessCreateFolder)
+ access := bits[:]
+ return &access
+ }(),
+ },
ID: &[]byte{0, 1},
Server: &Server{
Config: &Config{
FileRoot: "/Files",
},
+ FS: func() *MockFileStore {
+ mfs := &MockFileStore{}
+ mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
+ mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
+ return mfs
+ }(),
},
},
t: NewTransaction(
NewField(fieldFileName, []byte("testFolder")),
),
},
- setup: func() {
- mfs := &MockFileStore{}
- mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
- mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
- FS = mfs
- },
wantRes: []Transaction{
{
clientID: &[]byte{0, 1},
name: "when UnmarshalBinary returns an err",
args: args{
cc: &ClientConn{
+ Account: &Account{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ bits.Set(accessCreateFolder)
+ access := bits[:]
+ return &access
+ }(),
+ },
ID: &[]byte{0, 1},
Server: &Server{
Config: &Config{
FileRoot: "/Files/",
},
+ FS: func() *MockFileStore {
+ mfs := &MockFileStore{}
+ mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
+ mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
+ return mfs
+ }(),
},
},
t: NewTransaction(
}),
),
},
- setup: func() {
- mfs := &MockFileStore{}
- mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
- mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
- FS = mfs
- },
wantRes: []Transaction{},
wantErr: true,
},
name: "fieldFileName does not allow directory traversal",
args: args{
cc: &ClientConn{
+ Account: &Account{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ bits.Set(accessCreateFolder)
+ access := bits[:]
+ return &access
+ }(),
+ },
ID: &[]byte{0, 1},
Server: &Server{
Config: &Config{
FileRoot: "/Files/",
},
+ FS: func() *MockFileStore {
+ mfs := &MockFileStore{}
+ mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
+ mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
+ return mfs
+ }(),
},
},
t: NewTransaction(
NewField(fieldFileName, []byte("../../testFolder")),
),
},
- setup: func() {
- mfs := &MockFileStore{}
- mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
- mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
- FS = mfs
- },
wantRes: []Transaction{
{
clientID: &[]byte{0, 1},
name: "fieldFilePath does not allow directory traversal",
args: args{
cc: &ClientConn{
+ Account: &Account{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ bits.Set(accessCreateFolder)
+ access := bits[:]
+ return &access
+ }(),
+ },
ID: &[]byte{0, 1},
Server: &Server{
Config: &Config{
FileRoot: "/Files/",
},
+ FS: func() *MockFileStore {
+ mfs := &MockFileStore{}
+ mfs.On("Mkdir", "/Files/foo/testFolder", fs.FileMode(0777)).Return(nil)
+ mfs.On("Stat", "/Files/foo/testFolder").Return(nil, os.ErrNotExist)
+ return mfs
+ }(),
},
},
t: NewTransaction(
}),
),
},
- setup: func() {
- mfs := &MockFileStore{}
- mfs.On("Mkdir", "/Files/foo/testFolder", fs.FileMode(0777)).Return(nil)
- mfs.On("Stat", "/Files/foo/testFolder").Return(nil, os.ErrNotExist)
- FS = mfs
- },
wantRes: []Transaction{
{
clientID: &[]byte{0, 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- tt.setup()
gotRes, err := HandleNewFolder(tt.args.cc, tt.args.t)
if (err != nil) != tt.wantErr {
t.Errorf("HandleNewFolder() error = %v, wantErr %v", err, tt.wantErr)
return
}
+
if !tranAssertEqual(t, tt.wantRes, gotRes) {
t.Errorf("HandleNewFolder() gotRes = %v, want %v", gotRes, tt.wantRes)
}
}
tests := []struct {
name string
- setup func()
args args
wantRes []Transaction
wantErr bool
}{
{
name: "with valid input and required permissions",
- setup: func() {
- mfs := &MockFileStore{}
- path, _ := os.Getwd()
- mfs.On(
- "Symlink",
- path+"/test/config/Files/foo/testFile",
- path+"/test/config/Files/bar/testFile",
- ).Return(nil)
- FS = mfs
- },
args: args{
cc: &ClientConn{
Account: &Account{
}(),
},
Logger: NewTestLogger(),
+ FS: func() *MockFileStore {
+ mfs := &MockFileStore{}
+ path, _ := os.Getwd()
+ mfs.On(
+ "Symlink",
+ path+"/test/config/Files/foo/testFile",
+ path+"/test/config/Files/bar/testFile",
+ ).Return(nil)
+ return mfs
+ }(),
},
},
t: NewTransaction(
},
{
name: "when symlink returns an error",
- setup: func() {
- mfs := &MockFileStore{}
- path, _ := os.Getwd()
- mfs.On(
- "Symlink",
- path+"/test/config/Files/foo/testFile",
- path+"/test/config/Files/bar/testFile",
- ).Return(errors.New("ohno"))
- FS = mfs
- },
args: args{
cc: &ClientConn{
Account: &Account{
}(),
},
Logger: NewTestLogger(),
+ FS: func() *MockFileStore {
+ mfs := &MockFileStore{}
+ path, _ := os.Getwd()
+ mfs.On(
+ "Symlink",
+ path+"/test/config/Files/foo/testFile",
+ path+"/test/config/Files/bar/testFile",
+ ).Return(errors.New("ohno"))
+ return mfs
+ }(),
},
},
t: NewTransaction(
wantErr: false,
},
{
- name: "when user does not have required permission",
- setup: func() {},
+ name: "when user does not have required permission",
args: args{
cc: &ClientConn{
Account: &Account{
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- tt.setup()
-
gotRes, err := HandleMakeAlias(tt.args.cc, tt.args.t)
if (err != nil) != tt.wantErr {
t.Errorf("HandleMakeAlias(%v, %v)", tt.args.cc, tt.args.t)
}
tests := []struct {
name string
- setup func()
args args
wantRes []Transaction
wantErr assert.ErrorAssertionFunc
}{
{
name: "when user exists",
- setup: func() {
- mfs := &MockFileStore{}
- mfs.On("Remove", "Users/testuser.yaml").Return(nil)
- FS = mfs
- },
args: args{
cc: &ClientConn{
Account: &Account{
Access: &[]byte{1},
},
},
+ FS: func() *MockFileStore {
+ mfs := &MockFileStore{}
+ mfs.On("Remove", "Users/testuser.yaml").Return(nil)
+ return mfs
+ }(),
},
},
t: NewTransaction(
wantErr: assert.NoError,
},
{
- name: "when user does not have required permission",
- setup: func() {},
+ name: "when user does not have required permission",
args: args{
cc: &ClientConn{
Account: &Account{
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- tt.setup()
gotRes, err := HandleDeleteUser(tt.args.cc, tt.args.t)
if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteUser(%v, %v)", tt.args.cc, tt.args.t)) {
return
})
}
}
+
+func TestHandleDelNewsArt(t *testing.T) {
+ type args struct {
+ cc *ClientConn
+ t *Transaction
+ }
+ tests := []struct {
+ name string
+ args args
+ wantRes []Transaction
+ wantErr assert.ErrorAssertionFunc
+ }{
+ {
+ name: "without required permission",
+ args: args{
+ cc: &ClientConn{
+ Account: &Account{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ access := bits[:]
+ return &access
+ }(),
+ },
+ },
+ t: NewTransaction(
+ tranDelNewsArt,
+ &[]byte{0, 0},
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0, 0x00},
+ ID: []byte{0x9a, 0xcb, 0x04, 0x42},
+ ErrorCode: []byte{0, 0, 0, 1},
+ Fields: []Field{
+ NewField(fieldError, []byte("You are not allowed to delete news articles.")),
+ },
+ },
+ },
+ wantErr: assert.NoError,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotRes, err := HandleDelNewsArt(tt.args.cc, tt.args.t)
+ if !tt.wantErr(t, err, fmt.Sprintf("HandleDelNewsArt(%v, %v)", tt.args.cc, tt.args.t)) {
+ return
+ }
+ tranAssertEqual(t, tt.wantRes, gotRes)
+ })
+ }
+}
+
+func TestHandleDisconnectUser(t *testing.T) {
+ type args struct {
+ cc *ClientConn
+ t *Transaction
+ }
+ tests := []struct {
+ name string
+ args args
+ wantRes []Transaction
+ wantErr assert.ErrorAssertionFunc
+ }{
+ {
+ name: "without required permission",
+ args: args{
+ cc: &ClientConn{
+ Account: &Account{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ access := bits[:]
+ return &access
+ }(),
+ },
+ },
+ t: NewTransaction(
+ tranDelNewsArt,
+ &[]byte{0, 0},
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0, 0x00},
+ ID: []byte{0x9a, 0xcb, 0x04, 0x42},
+ ErrorCode: []byte{0, 0, 0, 1},
+ Fields: []Field{
+ NewField(fieldError, []byte("You are not allowed to disconnect users.")),
+ },
+ },
+ },
+ wantErr: assert.NoError,
+ },
+ {
+ name: "when target user has 'cannot be disconnected' priv",
+ args: args{
+ cc: &ClientConn{
+ Server: &Server{
+ Clients: map[uint16]*ClientConn{
+ uint16(1): {
+ Account: &Account{
+ Login: "unnamed",
+ Access: func() *[]byte {
+ var bits accessBitmap
+ bits.Set(accessCannotBeDiscon)
+ access := bits[:]
+ return &access
+ }(),
+ },
+ },
+ },
+ },
+ Account: &Account{
+ Access: func() *[]byte {
+ var bits accessBitmap
+ bits.Set(accessDisconUser)
+ access := bits[:]
+ return &access
+ }(),
+ },
+ },
+ t: NewTransaction(
+ tranDelNewsArt,
+ &[]byte{0, 0},
+ NewField(fieldUserID, []byte{0, 1}),
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0, 0x00},
+ ID: []byte{0x9a, 0xcb, 0x04, 0x42},
+ ErrorCode: []byte{0, 0, 0, 1},
+ Fields: []Field{
+ NewField(fieldError, []byte("unnamed is not allowed to be disconnected.")),
+ },
+ },
+ },
+ wantErr: assert.NoError,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotRes, err := HandleDisconnectUser(tt.args.cc, tt.args.t)
+ if !tt.wantErr(t, err, fmt.Sprintf("HandleDisconnectUser(%v, %v)", tt.args.cc, tt.args.t)) {
+ return
+ }
+ tranAssertEqual(t, tt.wantRes, gotRes)
+ })
+ }
+}
+
+func TestHandleSendInstantMsg(t *testing.T) {
+ type args struct {
+ cc *ClientConn
+ t *Transaction
+ }
+ tests := []struct {
+ name string
+ args args
+ wantRes []Transaction
+ wantErr assert.ErrorAssertionFunc
+ }{
+ {
+ name: "when client 1 sends a message to client 2",
+ args: args{
+ cc: &ClientConn{
+ ID: &[]byte{0, 1},
+ UserName: []byte("User1"),
+ Server: &Server{
+ Clients: map[uint16]*ClientConn{
+ uint16(2): {
+ AutoReply: []byte(nil),
+ },
+ },
+ },
+ },
+ t: NewTransaction(
+ tranSendInstantMsg,
+ &[]byte{0, 1},
+ NewField(fieldData, []byte("hai")),
+ NewField(fieldUserID, []byte{0, 2}),
+ ),
+ },
+ wantRes: []Transaction{
+ *NewTransaction(
+ tranServerMsg,
+ &[]byte{0, 2},
+ NewField(fieldData, []byte("hai")),
+ NewField(fieldUserName, []byte("User1")),
+ NewField(fieldUserID, []byte{0, 1}),
+ NewField(fieldOptions, []byte{0, 1}),
+ ),
+ {
+ clientID: &[]byte{0, 1},
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0x0, 0x6c},
+ ID: []byte{0, 0, 0, 0},
+ ErrorCode: []byte{0, 0, 0, 0},
+ Fields: []Field(nil),
+ },
+ },
+ wantErr: assert.NoError,
+ },
+ {
+ name: "when client 2 has autoreply enabled",
+ args: args{
+ cc: &ClientConn{
+ ID: &[]byte{0, 1},
+ UserName: []byte("User1"),
+ Server: &Server{
+ Clients: map[uint16]*ClientConn{
+ uint16(2): {
+ ID: &[]byte{0, 2},
+ UserName: []byte("User2"),
+ AutoReply: []byte("autohai"),
+ },
+ },
+ },
+ },
+ t: NewTransaction(
+ tranSendInstantMsg,
+ &[]byte{0, 1},
+ NewField(fieldData, []byte("hai")),
+ NewField(fieldUserID, []byte{0, 2}),
+ ),
+ },
+ wantRes: []Transaction{
+ *NewTransaction(
+ tranServerMsg,
+ &[]byte{0, 2},
+ NewField(fieldData, []byte("hai")),
+ NewField(fieldUserName, []byte("User1")),
+ NewField(fieldUserID, []byte{0, 1}),
+ NewField(fieldOptions, []byte{0, 1}),
+ ),
+ *NewTransaction(
+ tranServerMsg,
+ &[]byte{0, 1},
+ NewField(fieldData, []byte("autohai")),
+ NewField(fieldUserName, []byte("User2")),
+ NewField(fieldUserID, []byte{0, 2}),
+ NewField(fieldOptions, []byte{0, 1}),
+ ),
+ {
+ clientID: &[]byte{0, 1},
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0x0, 0x6c},
+ ID: []byte{0, 0, 0, 0},
+ ErrorCode: []byte{0, 0, 0, 0},
+ Fields: []Field(nil),
+ },
+ },
+ wantErr: assert.NoError,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotRes, err := HandleSendInstantMsg(tt.args.cc, tt.args.t)
+ if !tt.wantErr(t, err, fmt.Sprintf("HandleSendInstantMsg(%v, %v)", tt.args.cc, tt.args.t)) {
+ return
+ }
+
+ tranAssertEqual(t, tt.wantRes, gotRes)
+ })
+ }
+}