| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- package server
- import (
- "bytes"
- "os"
- "testing"
- "github.com/google/uuid"
- "github.com/mkaminski/goaim/oscar"
- "github.com/stretchr/testify/assert"
- )
- func TestReceiveAndSendBUCPLoginRequest(t *testing.T) {
- userGoodPwd := 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 Config
- userInDB User
- sessionUUID uuid.UUID
- inputSNAC oscar.SNAC_0x17_0x02_BUCPLoginRequest
- expectSnacFrame oscar.SnacFrame
- expectSNACBody oscar.SNAC_0x17_0x03_BUCPLoginResponse
- }{
- {
- name: "login with valid password, expect OK login response",
- cfg: 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.TLV{
- TType: oscar.TLVPasswordHash, Val: userGoodPwd.PassHash,
- },
- oscar.TLV{
- TType: oscar.TLVScreenName, Val: userGoodPwd.ScreenName,
- },
- },
- },
- },
- expectSnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUCP,
- SubGroup: BUCPLoginResponse,
- },
- expectSNACBody: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- TType: oscar.TLVScreenName,
- Val: userGoodPwd.ScreenName,
- },
- {
- TType: oscar.TLVReconnectHere,
- Val: "127.0.0.1:1234",
- },
- {
- TType: oscar.TLVAuthorizationCookie,
- Val: uuid.UUID{1, 2, 3}.String(),
- },
- },
- },
- },
- },
- {
- name: "login with bad password, expect OK login response (cfg.DisableAuth=true)",
- cfg: 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.TLV{
- TType: oscar.TLVPasswordHash, Val: userBadPwd.PassHash,
- },
- oscar.TLV{
- TType: oscar.TLVScreenName, Val: userBadPwd.ScreenName,
- },
- },
- },
- },
- expectSnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUCP,
- SubGroup: BUCPLoginResponse,
- },
- expectSNACBody: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- TType: oscar.TLVScreenName,
- Val: userBadPwd.ScreenName,
- },
- {
- TType: oscar.TLVReconnectHere,
- Val: "127.0.0.1:1234",
- },
- {
- TType: oscar.TLVAuthorizationCookie,
- Val: uuid.UUID{1, 2, 3}.String(),
- },
- },
- },
- },
- },
- {
- name: "login with bad password, expect failed login response (cfg.DisableAuth=false)",
- cfg: 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.TLV{
- TType: oscar.TLVPasswordHash, Val: userBadPwd.PassHash,
- },
- oscar.TLV{
- TType: oscar.TLVScreenName, Val: userBadPwd.ScreenName,
- },
- },
- },
- },
- expectSnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUCP,
- SubGroup: BUCPLoginResponse,
- },
- expectSNACBody: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- TType: oscar.TLVScreenName,
- Val: userBadPwd.ScreenName,
- },
- {
- TType: oscar.TLVErrorSubcode,
- Val: uint16(0x01),
- },
- },
- },
- },
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- //
- // initialize dependencies
- //
- const testFile string = "aim_test.db"
- defer func() {
- assert.NoError(t, os.Remove(testFile))
- }()
- fs, err := NewFeedbagStore(testFile)
- if err != nil {
- assert.NoError(t, err)
- }
- assert.NoError(t, fs.InsertUser(tc.userInDB))
- sm := NewSessionManager()
- //
- // send input SNAC
- //
- input := &bytes.Buffer{}
- var seq uint32
- assert.NoError(t, writeOutSNAC(oscar.SnacFrame{}, oscar.SnacFrame{}, tc.inputSNAC, &seq, input))
- //
- // receive response
- //
- output := &bytes.Buffer{}
- fnNewUUID := func() uuid.UUID {
- return tc.sessionUUID
- }
- assert.NoError(t, ReceiveAndSendBUCPLoginRequest(tc.cfg, sm, fs, input, output, &seq, fnNewUUID))
- flap := oscar.FlapFrame{}
- assert.NoError(t, oscar.Unmarshal(&flap, output))
- //
- // verify output SNAC frame
- //
- SnacFrame := oscar.SnacFrame{}
- assert.NoError(t, oscar.Unmarshal(&SnacFrame, output))
- assert.Equal(t, tc.expectSnacFrame, SnacFrame)
- //
- // verify output SNAC body
- //
- assert.NoError(t, tc.expectSNACBody.SerializeInPlace())
- actual := oscar.SNAC_0x17_0x03_BUCPLoginResponse{}
- assert.NoError(t, oscar.Unmarshal(&actual, output))
- assert.Equal(t, tc.expectSNACBody, actual)
- assert.Equalf(t, 0, output.Len(), "the rest of the buffer is unread")
- })
- }
- }
- func TestReceiveAndSendAuthChallenge(t *testing.T) {
- cases := []struct {
- name string
- cfg Config
- userInDB User
- fnNewUUID uuid.UUID
- inputSNAC oscar.SNAC_0x17_0x06_BUCPChallengeRequest
- expectSnacFrame oscar.SnacFrame
- expectSNACBody any
- }{
- {
- name: "login with valid username, expect OK login response",
- cfg: Config{
- OSCARHost: "127.0.0.1",
- BOSPort: 1234,
- },
- userInDB: 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.TLV{
- TType: oscar.TLVScreenName, Val: "sn_user_a",
- },
- },
- },
- },
- expectSnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUCP,
- SubGroup: BUCPChallengeResponse,
- },
- expectSNACBody: oscar.SNAC_0x17_0x07_BUCPChallengeResponse{
- AuthKey: "auth_key_user_a",
- },
- },
- {
- name: "login with invalid username, expect OK login response (cfg.DisableAuth=true)",
- cfg: Config{
- OSCARHost: "127.0.0.1",
- BOSPort: 1234,
- DisableAuth: true,
- },
- userInDB: 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.TLV{
- TType: oscar.TLVScreenName, Val: "sn_user_b",
- },
- },
- },
- },
- expectSnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUCP,
- SubGroup: BUCPChallengeResponse,
- },
- expectSNACBody: 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: Config{
- OSCARHost: "127.0.0.1",
- BOSPort: 1234,
- },
- userInDB: 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.TLV{
- TType: oscar.TLVScreenName, Val: "sn_user_b",
- },
- },
- },
- },
- expectSnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUCP,
- SubGroup: BUCPLoginResponse,
- },
- expectSNACBody: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- TType: oscar.TLVErrorSubcode,
- Val: uint16(0x01),
- },
- },
- },
- },
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- //
- // initialize dependencies
- //
- const testFile string = "aim_test.db"
- defer func() {
- assert.NoError(t, os.Remove(testFile))
- }()
- fs, err := NewFeedbagStore(testFile)
- if err != nil {
- assert.NoError(t, err)
- }
- assert.NoError(t, fs.InsertUser(tc.userInDB))
- //
- // send input SNAC
- //
- input := &bytes.Buffer{}
- var seq uint32
- assert.NoError(t, writeOutSNAC(oscar.SnacFrame{}, oscar.SnacFrame{}, tc.inputSNAC, &seq, input))
- //
- // receive response
- //
- output := &bytes.Buffer{}
- fnNewUUID := func() uuid.UUID {
- return tc.fnNewUUID
- }
- assert.NoError(t, ReceiveAndSendAuthChallenge(tc.cfg, fs, input, output, &seq, fnNewUUID))
- flap := oscar.FlapFrame{}
- assert.NoError(t, oscar.Unmarshal(&flap, output))
- //
- // verify output SNAC frame
- //
- SnacFrame := oscar.SnacFrame{}
- assert.NoError(t, oscar.Unmarshal(&SnacFrame, output))
- assert.Equal(t, tc.expectSnacFrame, SnacFrame)
- //
- // verify output SNAC body
- //
- switch v := tc.expectSNACBody.(type) {
- case oscar.SNAC_0x17_0x07_BUCPChallengeResponse:
- actual := oscar.SNAC_0x17_0x07_BUCPChallengeResponse{}
- assert.NoError(t, oscar.Unmarshal(&actual, output))
- assert.Equal(t, v, actual)
- case oscar.SNAC_0x17_0x03_BUCPLoginResponse:
- assert.NoError(t, v.SerializeInPlace())
- actual := oscar.SNAC_0x17_0x03_BUCPLoginResponse{}
- assert.NoError(t, oscar.Unmarshal(&actual, output))
- assert.Equal(t, v, actual)
- default:
- t.Fatalf("unexpected output SNAC type")
- }
- assert.Equalf(t, 0, output.Len(), "the rest of the buffer is unread")
- })
- }
- }
|