4
0

chat_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package server
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/mkaminski/goaim/oscar"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/mock"
  8. )
  9. func TestChatRouter_RouteChat(t *testing.T) {
  10. cases := []struct {
  11. // name is the unit test name
  12. name string
  13. // input is the request payload
  14. input oscar.SNACMessage
  15. // output is the response payload
  16. output *oscar.SNACMessage
  17. // handlerErr is the mocked handler error response
  18. handlerErr error
  19. // expectErr is the expected error returned by the router
  20. expectErr error
  21. }{
  22. {
  23. name: "receive ChatChannelMsgToHost, return ChatChannelMsgToClient",
  24. input: oscar.SNACMessage{
  25. Frame: oscar.SNACFrame{
  26. FoodGroup: oscar.Chat,
  27. SubGroup: oscar.ChatChannelMsgToHost,
  28. },
  29. Body: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  30. Channel: 4,
  31. },
  32. },
  33. output: &oscar.SNACMessage{
  34. Frame: oscar.SNACFrame{
  35. FoodGroup: oscar.Chat,
  36. SubGroup: oscar.ChatChannelMsgToClient,
  37. },
  38. Body: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  39. Channel: 4,
  40. },
  41. },
  42. },
  43. {
  44. name: "receive ChatChannelMsgToHost, return no response",
  45. input: oscar.SNACMessage{
  46. Frame: oscar.SNACFrame{
  47. FoodGroup: oscar.Chat,
  48. SubGroup: oscar.ChatChannelMsgToHost,
  49. },
  50. Body: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  51. Channel: 4,
  52. },
  53. },
  54. output: nil,
  55. },
  56. {
  57. name: "receive ChatRowListInfo, return ErrUnsupportedSubGroup",
  58. input: oscar.SNACMessage{
  59. Frame: oscar.SNACFrame{
  60. FoodGroup: oscar.Chat,
  61. SubGroup: oscar.ChatRowListInfo,
  62. },
  63. Body: struct{}{},
  64. },
  65. output: nil,
  66. expectErr: ErrUnsupportedSubGroup,
  67. },
  68. }
  69. for _, tc := range cases {
  70. t.Run(tc.name, func(t *testing.T) {
  71. svc := newMockChatHandler(t)
  72. svc.EXPECT().
  73. ChannelMsgToHostHandler(mock.Anything, mock.Anything, mock.Anything, tc.input.Frame, tc.input.Body).
  74. Return(tc.output, tc.handlerErr).
  75. Maybe()
  76. router := ChatRouter{
  77. ChatHandler: svc,
  78. RouteLogger: RouteLogger{
  79. Logger: NewLogger(Config{}),
  80. },
  81. }
  82. bufIn := &bytes.Buffer{}
  83. assert.NoError(t, oscar.Marshal(tc.input.Body, bufIn))
  84. bufOut := &bytes.Buffer{}
  85. seq := uint32(0)
  86. err := router.RouteChat(nil, nil, "", tc.input.Frame, bufIn, bufOut, &seq)
  87. assert.ErrorIs(t, err, tc.expectErr)
  88. if tc.expectErr != nil {
  89. return
  90. }
  91. if tc.output == nil {
  92. // make sure no response was sent
  93. assert.Empty(t, bufOut.Bytes())
  94. return
  95. }
  96. // verify the FLAP frame
  97. flap := oscar.FLAPFrame{}
  98. assert.NoError(t, oscar.Unmarshal(&flap, bufOut))
  99. // make sure the sequence increments
  100. assert.Equal(t, seq, uint32(1))
  101. assert.Equal(t, flap.Sequence, uint16(0))
  102. flapBuf, err := flap.SNACBuffer(bufOut)
  103. assert.NoError(t, err)
  104. // verify the SNAC frame
  105. snacFrame := oscar.SNACFrame{}
  106. assert.NoError(t, oscar.Unmarshal(&snacFrame, flapBuf))
  107. assert.Equal(t, tc.output.Frame, snacFrame)
  108. // verify the SNAC message
  109. snacBuf := &bytes.Buffer{}
  110. assert.NoError(t, oscar.Marshal(tc.output.Body, snacBuf))
  111. assert.Equal(t, snacBuf.Bytes(), flapBuf.Bytes())
  112. })
  113. }
  114. }