chat_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package server
  2. import (
  3. "bytes"
  4. "context"
  5. "github.com/stretchr/testify/mock"
  6. "testing"
  7. "github.com/mkaminski/goaim/oscar"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestSendAndReceiveChatChannelMsgToHost(t *testing.T) {
  11. cases := []struct {
  12. // name is the unit test name
  13. name string
  14. // userSession is the session of the user sending the chat message
  15. userSession *Session
  16. // inputSNAC is the SNAC sent by the sender client
  17. inputSNAC oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost
  18. // expectSNACToParticipants is the message the server broadcast to chat
  19. // room participants (except the sender)
  20. expectSNACToParticipants XMessage
  21. expectOutput *XMessage
  22. }{
  23. {
  24. name: "send chat room message, expect acknowledgement to sender client",
  25. userSession: newTestSession(Session{
  26. ScreenName: "user_sending_chat_msg",
  27. }),
  28. inputSNAC: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  29. Cookie: 1234,
  30. Channel: 14,
  31. TLVRestBlock: oscar.TLVRestBlock{
  32. TLVList: oscar.TLVList{
  33. {
  34. TType: oscar.ChatTLVPublicWhisperFlag,
  35. Val: []byte{},
  36. },
  37. {
  38. TType: oscar.ChatTLVEnableReflectionFlag,
  39. Val: []byte{},
  40. },
  41. },
  42. },
  43. },
  44. expectSNACToParticipants: XMessage{
  45. snacFrame: oscar.SnacFrame{
  46. FoodGroup: oscar.CHAT,
  47. SubGroup: oscar.ChatChannelMsgToClient,
  48. },
  49. snacOut: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  50. Cookie: 1234,
  51. Channel: 14,
  52. TLVRestBlock: oscar.TLVRestBlock{
  53. TLVList: oscar.TLVList{
  54. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  55. oscar.NewTLV(oscar.ChatTLVEnableReflectionFlag, []byte{}),
  56. oscar.NewTLV(oscar.ChatTLVSenderInformation,
  57. newTestSession(Session{ScreenName: "user_sending_chat_msg"}).GetTLVUserInfo()),
  58. },
  59. },
  60. },
  61. },
  62. expectOutput: &XMessage{
  63. snacFrame: oscar.SnacFrame{
  64. FoodGroup: oscar.CHAT,
  65. SubGroup: oscar.ChatChannelMsgToClient,
  66. },
  67. snacOut: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  68. Cookie: 1234,
  69. Channel: 14,
  70. TLVRestBlock: oscar.TLVRestBlock{
  71. TLVList: oscar.TLVList{
  72. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  73. oscar.NewTLV(oscar.ChatTLVEnableReflectionFlag, []byte{}),
  74. oscar.NewTLV(oscar.ChatTLVSenderInformation, newTestSession(Session{
  75. ScreenName: "user_sending_chat_msg",
  76. }).GetTLVUserInfo()),
  77. },
  78. },
  79. },
  80. },
  81. },
  82. {
  83. name: "send chat room message, don't expect acknowledgement to sender client",
  84. userSession: newTestSession(Session{
  85. ScreenName: "user_sending_chat_msg",
  86. }),
  87. inputSNAC: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  88. Cookie: 1234,
  89. Channel: 14,
  90. TLVRestBlock: oscar.TLVRestBlock{
  91. TLVList: oscar.TLVList{
  92. {
  93. TType: oscar.ChatTLVPublicWhisperFlag,
  94. Val: []byte{},
  95. },
  96. },
  97. },
  98. },
  99. expectSNACToParticipants: XMessage{
  100. snacFrame: oscar.SnacFrame{
  101. FoodGroup: oscar.CHAT,
  102. SubGroup: oscar.ChatChannelMsgToClient,
  103. },
  104. snacOut: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  105. Cookie: 1234,
  106. Channel: 14,
  107. TLVRestBlock: oscar.TLVRestBlock{
  108. TLVList: oscar.TLVList{
  109. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  110. oscar.NewTLV(oscar.ChatTLVSenderInformation,
  111. newTestSession(Session{
  112. ScreenName: "user_sending_chat_msg",
  113. }).GetTLVUserInfo()),
  114. },
  115. },
  116. },
  117. },
  118. expectOutput: &XMessage{},
  119. },
  120. }
  121. for _, tc := range cases {
  122. t.Run(tc.name, func(t *testing.T) {
  123. //
  124. // initialize dependencies
  125. //
  126. crm := NewMockSessionManager(t)
  127. crm.EXPECT().
  128. BroadcastExcept(mock.Anything, tc.userSession, tc.expectSNACToParticipants)
  129. //
  130. // send input SNAC
  131. //
  132. svc := ChatService{}
  133. outputSNAC, err := svc.ChannelMsgToHostHandler(context.Background(), tc.userSession, crm, tc.inputSNAC)
  134. assert.NoError(t, err)
  135. if tc.expectOutput.snacFrame == (oscar.SnacFrame{}) {
  136. return // handler doesn't return response
  137. }
  138. assert.Equal(t, tc.expectOutput, outputSNAC)
  139. })
  140. }
  141. }
  142. func TestChatRouter_RouteChat(t *testing.T) {
  143. cases := []struct {
  144. // name is the unit test name
  145. name string
  146. // input is the request payload
  147. input XMessage
  148. // output is the response payload
  149. output *XMessage
  150. // handlerErr is the mocked handler error response
  151. handlerErr error
  152. // expectErr is the expected error returned by the router
  153. expectErr error
  154. }{
  155. {
  156. name: "receive ChatChannelMsgToHost, return ChatChannelMsgToClient",
  157. input: XMessage{
  158. snacFrame: oscar.SnacFrame{
  159. FoodGroup: oscar.CHAT,
  160. SubGroup: oscar.ChatChannelMsgToHost,
  161. },
  162. snacOut: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  163. Channel: 4,
  164. },
  165. },
  166. output: &XMessage{
  167. snacFrame: oscar.SnacFrame{
  168. FoodGroup: oscar.CHAT,
  169. SubGroup: oscar.ChatChannelMsgToClient,
  170. },
  171. snacOut: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  172. Channel: 4,
  173. },
  174. },
  175. },
  176. {
  177. name: "receive ChatChannelMsgToHost, return no response",
  178. input: XMessage{
  179. snacFrame: oscar.SnacFrame{
  180. FoodGroup: oscar.CHAT,
  181. SubGroup: oscar.ChatChannelMsgToHost,
  182. },
  183. snacOut: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  184. Channel: 4,
  185. },
  186. },
  187. output: nil,
  188. },
  189. {
  190. name: "receive ChatRowListInfo, return ErrUnsupportedSubGroup",
  191. input: XMessage{
  192. snacFrame: oscar.SnacFrame{
  193. FoodGroup: oscar.CHAT,
  194. SubGroup: oscar.ChatRowListInfo,
  195. },
  196. snacOut: struct{}{},
  197. },
  198. output: nil,
  199. expectErr: ErrUnsupportedSubGroup,
  200. },
  201. }
  202. for _, tc := range cases {
  203. t.Run(tc.name, func(t *testing.T) {
  204. svc := NewMockChatHandler(t)
  205. svc.EXPECT().
  206. ChannelMsgToHostHandler(mock.Anything, mock.Anything, mock.Anything, tc.input.snacOut).
  207. Return(tc.output, tc.handlerErr).
  208. Maybe()
  209. router := ChatRouter{
  210. ChatHandler: svc,
  211. RouteLogger: RouteLogger{
  212. Logger: NewLogger(Config{}),
  213. },
  214. }
  215. bufIn := &bytes.Buffer{}
  216. assert.NoError(t, oscar.Marshal(tc.input.snacOut, bufIn))
  217. bufOut := &bytes.Buffer{}
  218. seq := uint32(0)
  219. err := router.RouteChat(nil, nil, nil, tc.input.snacFrame, bufIn, bufOut, &seq)
  220. assert.ErrorIs(t, err, tc.expectErr)
  221. if tc.expectErr != nil {
  222. return
  223. }
  224. if tc.output == nil {
  225. // make sure no response was sent
  226. assert.Empty(t, bufOut.Bytes())
  227. return
  228. }
  229. // verify the FLAP frame
  230. flap := oscar.FlapFrame{}
  231. assert.NoError(t, oscar.Unmarshal(&flap, bufOut))
  232. // make sure the sequence increments
  233. assert.Equal(t, seq, uint32(1))
  234. assert.Equal(t, flap.Sequence, uint16(0))
  235. flapBuf, err := flap.SNACBuffer(bufOut)
  236. assert.NoError(t, err)
  237. // verify the SNAC frame
  238. snacFrame := oscar.SnacFrame{}
  239. assert.NoError(t, oscar.Unmarshal(&snacFrame, flapBuf))
  240. assert.Equal(t, tc.output.snacFrame, snacFrame)
  241. // verify the SNAC message
  242. snacBuf := &bytes.Buffer{}
  243. assert.NoError(t, oscar.Marshal(tc.output.snacOut, snacBuf))
  244. assert.Equal(t, snacBuf.Bytes(), flapBuf.Bytes())
  245. })
  246. }
  247. }