chat_test.go 7.0 KB

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