| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package server
- import (
- "bytes"
- "github.com/mkaminski/goaim/oscar"
- "github.com/stretchr/testify/assert"
- "testing"
- )
- func TestBuddyRouter_RouteBuddy(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // input is the request payload
- input XMessage
- // output is the response payload
- output XMessage
- // handlerErr is the mocked handler error response
- handlerErr error
- // expectErr is the expected error returned by the router
- expectErr error
- }{
- {
- name: "receive BuddyRightsQuery, return BuddyRightsReply",
- input: XMessage{
- snacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUDDY,
- SubGroup: oscar.BuddyRightsQuery,
- },
- snacOut: oscar.SNAC_0x03_0x02_BuddyRightsQuery{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- TType: 0x01,
- Val: []byte{1, 2, 3, 4},
- },
- },
- },
- },
- },
- output: XMessage{
- snacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUDDY,
- SubGroup: oscar.BuddyRightsReply,
- },
- snacOut: oscar.SNAC_0x03_0x03_BuddyRightsReply{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- TType: 0x01,
- Val: []byte{1, 2, 3, 4},
- },
- },
- },
- },
- },
- },
- {
- name: "receive ErrorCodeReplyTooBig, expect ErrUnsupportedSubGroup",
- input: XMessage{
- snacFrame: oscar.SnacFrame{
- FoodGroup: oscar.BUDDY,
- SubGroup: ErrorCodeReplyTooBig,
- },
- snacOut: struct{}{}, // empty SNAC
- },
- output: XMessage{},
- expectErr: ErrUnsupportedSubGroup,
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- svc := NewMockBuddyHandler(t)
- svc.EXPECT().
- RightsQueryHandler().
- Return(tc.output).
- Maybe()
- router := BuddyRouter{
- BuddyHandler: svc,
- }
- bufIn := &bytes.Buffer{}
- assert.NoError(t, oscar.Marshal(tc.input.snacOut, bufIn))
- bufOut := &bytes.Buffer{}
- seq := uint32(1)
- err := router.RouteBuddy(tc.input.snacFrame, bufIn, bufOut, &seq)
- assert.ErrorIs(t, err, tc.expectErr)
- if tc.expectErr != nil {
- return
- }
- if tc.output == (XMessage{}) {
- // make sure no response was sent
- assert.Empty(t, bufOut.Bytes())
- return
- }
- // verify the FLAP frame
- flap := oscar.FlapFrame{}
- assert.NoError(t, oscar.Unmarshal(&flap, bufOut))
- // make sure the sequence number was incremented
- assert.Equal(t, uint32(2), seq)
- 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.snacFrame, snacFrame)
- // verify the SNAC message
- snacBuf := &bytes.Buffer{}
- assert.NoError(t, oscar.Marshal(tc.output.snacOut, snacBuf))
- assert.Equal(t, snacBuf.Bytes(), flapBuf.Bytes())
- })
- }
- }
|