buddy_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package server
  2. import (
  3. "bytes"
  4. "github.com/mkaminski/goaim/oscar"
  5. "github.com/stretchr/testify/assert"
  6. "testing"
  7. )
  8. func TestBuddyRouter_RouteBuddy(t *testing.T) {
  9. cases := []struct {
  10. // name is the unit test name
  11. name string
  12. // input is the request payload
  13. input XMessage
  14. // output is the response payload
  15. output XMessage
  16. // handlerErr is the mocked handler error response
  17. handlerErr error
  18. // expectErr is the expected error returned by the router
  19. expectErr error
  20. }{
  21. {
  22. name: "receive BuddyRightsQuery, return BuddyRightsReply",
  23. input: XMessage{
  24. snacFrame: oscar.SnacFrame{
  25. FoodGroup: oscar.BUDDY,
  26. SubGroup: oscar.BuddyRightsQuery,
  27. },
  28. snacOut: oscar.SNAC_0x03_0x02_BuddyRightsQuery{
  29. TLVRestBlock: oscar.TLVRestBlock{
  30. TLVList: oscar.TLVList{
  31. {
  32. TType: 0x01,
  33. Val: []byte{1, 2, 3, 4},
  34. },
  35. },
  36. },
  37. },
  38. },
  39. output: XMessage{
  40. snacFrame: oscar.SnacFrame{
  41. FoodGroup: oscar.BUDDY,
  42. SubGroup: oscar.BuddyRightsReply,
  43. },
  44. snacOut: oscar.SNAC_0x03_0x03_BuddyRightsReply{
  45. TLVRestBlock: oscar.TLVRestBlock{
  46. TLVList: oscar.TLVList{
  47. {
  48. TType: 0x01,
  49. Val: []byte{1, 2, 3, 4},
  50. },
  51. },
  52. },
  53. },
  54. },
  55. },
  56. {
  57. name: "receive ErrorCodeReplyTooBig, expect ErrUnsupportedSubGroup",
  58. input: XMessage{
  59. snacFrame: oscar.SnacFrame{
  60. FoodGroup: oscar.BUDDY,
  61. SubGroup: ErrorCodeReplyTooBig,
  62. },
  63. snacOut: struct{}{}, // empty SNAC
  64. },
  65. output: XMessage{},
  66. expectErr: ErrUnsupportedSubGroup,
  67. },
  68. }
  69. for _, tc := range cases {
  70. t.Run(tc.name, func(t *testing.T) {
  71. svc := NewMockBuddyHandler(t)
  72. svc.EXPECT().
  73. RightsQueryHandler().
  74. Return(tc.output).
  75. Maybe()
  76. router := BuddyRouter{
  77. BuddyHandler: svc,
  78. }
  79. bufIn := &bytes.Buffer{}
  80. assert.NoError(t, oscar.Marshal(tc.input.snacOut, bufIn))
  81. bufOut := &bytes.Buffer{}
  82. seq := uint32(1)
  83. err := router.RouteBuddy(tc.input.snacFrame, bufIn, bufOut, &seq)
  84. assert.ErrorIs(t, err, tc.expectErr)
  85. if tc.expectErr != nil {
  86. return
  87. }
  88. if tc.output == (XMessage{}) {
  89. // make sure no response was sent
  90. assert.Empty(t, bufOut.Bytes())
  91. return
  92. }
  93. // verify the FLAP frame
  94. flap := oscar.FlapFrame{}
  95. assert.NoError(t, oscar.Unmarshal(&flap, bufOut))
  96. // make sure the sequence number was incremented
  97. assert.Equal(t, uint32(2), seq)
  98. flapBuf, err := flap.SNACBuffer(bufOut)
  99. assert.NoError(t, err)
  100. // verify the SNAC frame
  101. snacFrame := oscar.SnacFrame{}
  102. assert.NoError(t, oscar.Unmarshal(&snacFrame, flapBuf))
  103. assert.Equal(t, tc.output.snacFrame, snacFrame)
  104. // verify the SNAC message
  105. snacBuf := &bytes.Buffer{}
  106. assert.NoError(t, oscar.Marshal(tc.output.snacOut, snacBuf))
  107. assert.Equal(t, snacBuf.Bytes(), flapBuf.Bytes())
  108. })
  109. }
  110. }