chat_test.go 3.1 KB

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