| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- package handler
- import (
- "github.com/google/uuid"
- "github.com/mkaminski/goaim/oscar"
- "github.com/mkaminski/goaim/server"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/mock"
- "testing"
- )
- func TestReceiveAndSendBUCPLoginRequest(t *testing.T) {
- userGoodPwd := server.User{
- ScreenName: "sn_user_a",
- AuthKey: "auth_key_user",
- }
- assert.NoError(t, userGoodPwd.HashPassword("good_pwd"))
- userBadPwd := userGoodPwd
- assert.NoError(t, userBadPwd.HashPassword("bad_pwd"))
- cases := []struct {
- name string
- cfg server.Config
- userInDB server.User
- sessionUUID uuid.UUID
- inputSNAC oscar.SNAC_0x17_0x02_BUCPLoginRequest
- // expectOutput is the SNAC payload sent from the server to the
- // recipient client
- expectOutput oscar.XMessage
- }{
- {
- name: "login with valid password, expect OK login response",
- cfg: server.Config{
- OSCARHost: "127.0.0.1",
- BOSPort: 1234,
- },
- userInDB: userGoodPwd,
- sessionUUID: uuid.UUID{1, 2, 3},
- inputSNAC: oscar.SNAC_0x17_0x02_BUCPLoginRequest{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(oscar.TLVPasswordHash, userGoodPwd.PassHash),
- oscar.NewTLV(oscar.TLVScreenName, userGoodPwd.ScreenName),
- },
- },
- },
- expectOutput: oscar.XMessage{
- SnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUCP,
- SubGroup: oscar.BUCPLoginResponse,
- },
- SnacOut: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(oscar.TLVScreenName, userGoodPwd.ScreenName),
- oscar.NewTLV(oscar.TLVReconnectHere, "127.0.0.1:1234"),
- oscar.NewTLV(oscar.TLVAuthorizationCookie, uuid.UUID{1, 2, 3}.String()),
- },
- },
- },
- },
- },
- {
- name: "login with bad password, expect OK login response (Cfg.DisableAuth=true)",
- cfg: server.Config{
- OSCARHost: "127.0.0.1",
- BOSPort: 1234,
- DisableAuth: true,
- },
- userInDB: userGoodPwd,
- sessionUUID: uuid.UUID{1, 2, 3},
- inputSNAC: oscar.SNAC_0x17_0x02_BUCPLoginRequest{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(oscar.TLVPasswordHash, userBadPwd.PassHash),
- oscar.NewTLV(oscar.TLVScreenName, userBadPwd.ScreenName),
- },
- },
- },
- expectOutput: oscar.XMessage{
- SnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUCP,
- SubGroup: oscar.BUCPLoginResponse,
- },
- SnacOut: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(oscar.TLVScreenName, userBadPwd.ScreenName),
- oscar.NewTLV(oscar.TLVReconnectHere, "127.0.0.1:1234"),
- oscar.NewTLV(oscar.TLVAuthorizationCookie, uuid.UUID{1, 2, 3}.String()),
- },
- },
- },
- },
- },
- {
- name: "login with bad password, expect failed login response (Cfg.DisableAuth=false)",
- cfg: server.Config{
- OSCARHost: "127.0.0.1",
- BOSPort: 1234,
- },
- userInDB: userGoodPwd,
- sessionUUID: uuid.UUID{1, 2, 3},
- inputSNAC: oscar.SNAC_0x17_0x02_BUCPLoginRequest{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(oscar.TLVPasswordHash, userBadPwd.PassHash),
- oscar.NewTLV(oscar.TLVScreenName, userBadPwd.ScreenName),
- },
- },
- },
- expectOutput: oscar.XMessage{
- SnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUCP,
- SubGroup: oscar.BUCPLoginResponse,
- },
- SnacOut: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(oscar.TLVScreenName, userBadPwd.ScreenName),
- oscar.NewTLV(oscar.TLVErrorSubcode, uint16(0x01)),
- },
- },
- },
- },
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- sess := newTestSession(tc.userInDB.ScreenName, sessOptID(tc.sessionUUID.String()))
- um := server.NewMockUserManager(t)
- um.EXPECT().
- GetUser(tc.userInDB.ScreenName).
- Return(&userGoodPwd, nil).
- Maybe()
- um.EXPECT().
- UpsertUser(mock.Anything).
- Return(nil).
- Maybe()
- sm := server.NewMockSessionManager(t)
- sm.EXPECT().
- NewSessionWithSN(tc.sessionUUID.String(), tc.userInDB.ScreenName).
- Return(sess).
- Maybe()
- svc := AuthService{
- cfg: tc.cfg,
- sm: sm,
- um: um,
- }
- fnNewUUID := func() uuid.UUID {
- return tc.sessionUUID
- }
- outputSNAC, err := svc.ReceiveAndSendBUCPLoginRequest(tc.inputSNAC, fnNewUUID)
- assert.NoError(t, err)
- assert.Equal(t, tc.expectOutput, outputSNAC)
- })
- }
- }
- func TestReceiveAndSendAuthChallenge(t *testing.T) {
- cases := []struct {
- name string
- cfg server.Config
- userInDB *server.User
- fnNewUUID uuid.UUID
- inputSNAC oscar.SNAC_0x17_0x06_BUCPChallengeRequest
- expectOutput oscar.XMessage
- }{
- {
- name: "login with valid username, expect OK login response",
- cfg: server.Config{
- OSCARHost: "127.0.0.1",
- BOSPort: 1234,
- },
- userInDB: &server.User{
- ScreenName: "sn_user_a",
- AuthKey: "auth_key_user_a",
- },
- fnNewUUID: uuid.UUID{1, 2, 3},
- inputSNAC: oscar.SNAC_0x17_0x06_BUCPChallengeRequest{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(oscar.TLVScreenName, "sn_user_a"),
- },
- },
- },
- expectOutput: oscar.XMessage{
- SnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUCP,
- SubGroup: oscar.BUCPChallengeResponse,
- },
- SnacOut: oscar.SNAC_0x17_0x07_BUCPChallengeResponse{
- AuthKey: "auth_key_user_a",
- },
- },
- },
- {
- name: "login with invalid username, expect OK login response (Cfg.DisableAuth=true)",
- cfg: server.Config{
- OSCARHost: "127.0.0.1",
- BOSPort: 1234,
- DisableAuth: true,
- },
- userInDB: nil,
- fnNewUUID: uuid.UUID{1, 2, 3},
- inputSNAC: oscar.SNAC_0x17_0x06_BUCPChallengeRequest{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(oscar.TLVScreenName, "sn_user_b"),
- },
- },
- },
- expectOutput: oscar.XMessage{
- SnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUCP,
- SubGroup: oscar.BUCPChallengeResponse,
- },
- SnacOut: oscar.SNAC_0x17_0x07_BUCPChallengeResponse{
- AuthKey: uuid.UUID{1, 2, 3}.String(),
- },
- },
- },
- {
- name: "login with invalid username, expect failed login response (Cfg.DisableAuth=false)",
- cfg: server.Config{
- OSCARHost: "127.0.0.1",
- BOSPort: 1234,
- },
- userInDB: nil,
- fnNewUUID: uuid.UUID{1, 2, 3},
- inputSNAC: oscar.SNAC_0x17_0x06_BUCPChallengeRequest{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(oscar.TLVScreenName, "sn_user_b"),
- },
- },
- },
- expectOutput: oscar.XMessage{
- SnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUCP,
- SubGroup: oscar.BUCPLoginResponse,
- },
- SnacOut: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(oscar.TLVErrorSubcode, uint16(0x01)),
- },
- },
- },
- },
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- um := server.NewMockUserManager(t)
- um.EXPECT().
- GetUser(string(tc.inputSNAC.TLVList[0].Val)).
- Return(tc.userInDB, nil).
- Maybe()
- svc := AuthService{
- cfg: tc.cfg,
- um: um,
- }
- fnNewUUID := func() uuid.UUID {
- return tc.fnNewUUID
- }
- outputSNAC, err := svc.ReceiveAndSendAuthChallenge(tc.inputSNAC, fnNewUUID)
- assert.NoError(t, err)
- assert.Equal(t, tc.expectOutput, outputSNAC)
- })
- }
- }
|