| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- package server
- import (
- "bytes"
- "testing"
- "github.com/mkaminski/goaim/oscar"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/mock"
- )
- func TestChatNavRouter_RouteChatNavRouter(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // input is the request payload
- input oscar.SNACMessage
- // output is the response payload
- output oscar.SNACMessage
- // handlerErr is the mocked handler error response
- handlerErr error
- // expectErr is the expected error returned by the router
- expectErr error
- }{
- {
- name: "receive ChatNavRequestChatRights, return ChatNavNavInfo",
- input: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.ChatNav,
- SubGroup: oscar.ChatNavRequestChatRights,
- },
- Body: struct{}{},
- },
- output: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.ChatNav,
- SubGroup: oscar.ChatNavNavInfo,
- },
- Body: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(0x02, uint8(10)),
- },
- },
- },
- },
- },
- {
- name: "receive ChatNavRequestRoomInfo, return ChatNavNavInfo",
- input: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.ChatNav,
- SubGroup: oscar.ChatNavRequestRoomInfo,
- },
- Body: oscar.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
- Exchange: 1,
- },
- },
- output: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.ChatNav,
- SubGroup: oscar.ChatNavNavInfo,
- },
- Body: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(0x02, uint8(10)),
- },
- },
- },
- },
- },
- {
- name: "receive ChatNavCreateRoom, return ChatNavNavInfo",
- input: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.ChatNav,
- SubGroup: oscar.ChatNavCreateRoom,
- },
- Body: oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
- Exchange: 1,
- },
- },
- output: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.ChatNav,
- SubGroup: oscar.ChatNavNavInfo,
- },
- Body: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(0x02, uint8(10)),
- },
- },
- },
- },
- },
- {
- name: "receive ChatNavRequestOccupantList, return ErrUnsupportedSubGroup",
- input: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.ChatNav,
- SubGroup: oscar.ChatNavRequestOccupantList,
- },
- Body: struct{}{},
- },
- output: oscar.SNACMessage{},
- expectErr: ErrUnsupportedSubGroup,
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- svc := newMockChatNavHandler(t)
- svc.EXPECT().
- RequestChatRightsHandler(mock.Anything, tc.input.Frame).
- Return(tc.output).
- Maybe()
- svc.EXPECT().
- RequestRoomInfoHandler(mock.Anything, tc.input.Frame, tc.input.Body).
- Return(tc.output, tc.handlerErr).
- Maybe()
- svc.EXPECT().
- CreateRoomHandler(mock.Anything, mock.Anything, tc.input.Frame, tc.input.Body).
- Return(tc.output, tc.handlerErr).
- Maybe()
- router := ChatNavRouter{
- ChatNavHandler: svc,
- RouteLogger: RouteLogger{
- Logger: NewLogger(Config{}),
- },
- }
- bufIn := &bytes.Buffer{}
- assert.NoError(t, oscar.Marshal(tc.input.Body, bufIn))
- bufOut := &bytes.Buffer{}
- seq := uint32(0)
- err := router.RouteChatNav(nil, nil, tc.input.Frame, bufIn, bufOut, &seq)
- assert.ErrorIs(t, err, tc.expectErr)
- if tc.expectErr != nil {
- return
- }
- if tc.output.Frame == (oscar.SNACFrame{}) {
- return
- }
- // verify the FLAP frame
- flap := oscar.FLAPFrame{}
- assert.NoError(t, oscar.Unmarshal(&flap, bufOut))
- // make sure the sequence increments
- assert.Equal(t, seq, uint32(1))
- assert.Equal(t, flap.Sequence, uint16(0))
- flapBuf, err := flap.SNACBuffer(bufOut)
- assert.NoError(t, err)
- // verify the SNAC frame
- snacFrame := oscar.SNACFrame{}
- assert.NoError(t, oscar.Unmarshal(&snacFrame, flapBuf))
- assert.Equal(t, tc.output.Frame, snacFrame)
- // verify the SNAC message
- snacBuf := &bytes.Buffer{}
- assert.NoError(t, oscar.Marshal(tc.output.Body, snacBuf))
- assert.Equal(t, snacBuf.Bytes(), flapBuf.Bytes())
- })
- }
- }
|