chat_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package server
  2. import (
  3. "bytes"
  4. "github.com/mkaminski/goaim/oscar"
  5. "github.com/stretchr/testify/assert"
  6. "testing"
  7. )
  8. func TestSendAndReceiveChatChannelMsgTohost(t *testing.T) {
  9. cases := []struct {
  10. // name is the unit test name
  11. name string
  12. // userSession is the session of the user sending the chat message
  13. userSession *Session
  14. // inputSNAC is the SNAC sent by the sender client
  15. inputSNAC oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost
  16. // expectSNACFrame is the SNAC frame sent from the server to the
  17. // recipient client
  18. expectSNACFrame oscar.SnacFrame
  19. // expectSNACBody is the SNAC payload sent from the server to the
  20. // recipient client
  21. expectSNACBody any
  22. // expectSNACToParticipants is the message the server broadcast to chat
  23. // room participants (except the sender)
  24. expectSNACToParticipants XMessage
  25. }{
  26. {
  27. name: "send chat room message, expect acknowledgement to sender client",
  28. userSession: newTestSession(Session{
  29. ScreenName: "user_sending_chat_msg",
  30. }),
  31. inputSNAC: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  32. Cookie: 1234,
  33. Channel: 14,
  34. TLVRestBlock: oscar.TLVRestBlock{
  35. TLVList: oscar.TLVList{
  36. {
  37. TType: oscar.ChatTLVPublicWhisperFlag,
  38. Val: []byte{},
  39. },
  40. {
  41. TType: oscar.ChatTLVEnableReflectionFlag,
  42. Val: []byte{},
  43. },
  44. },
  45. },
  46. },
  47. expectSNACFrame: oscar.SnacFrame{
  48. FoodGroup: CHAT,
  49. SubGroup: ChatChannelMsgToclient,
  50. },
  51. expectSNACBody: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  52. Cookie: 1234,
  53. Channel: 14,
  54. TLVRestBlock: oscar.TLVRestBlock{
  55. TLVList: oscar.TLVList{
  56. {
  57. TType: oscar.ChatTLVPublicWhisperFlag,
  58. Val: []byte{},
  59. },
  60. {
  61. TType: oscar.ChatTLVEnableReflectionFlag,
  62. Val: []byte{},
  63. },
  64. {
  65. TType: oscar.ChatTLVSenderInformation,
  66. Val: newTestSession(Session{
  67. ScreenName: "user_sending_chat_msg",
  68. }).GetTLVUserInfo(),
  69. },
  70. },
  71. },
  72. },
  73. expectSNACToParticipants: XMessage{
  74. snacFrame: oscar.SnacFrame{
  75. FoodGroup: CHAT,
  76. SubGroup: ChatChannelMsgToclient,
  77. },
  78. snacOut: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  79. Cookie: 1234,
  80. Channel: 14,
  81. TLVRestBlock: oscar.TLVRestBlock{
  82. TLVList: oscar.TLVList{
  83. {
  84. TType: oscar.ChatTLVPublicWhisperFlag,
  85. Val: []byte{},
  86. },
  87. {
  88. TType: oscar.ChatTLVEnableReflectionFlag,
  89. Val: []byte{},
  90. },
  91. {
  92. TType: oscar.ChatTLVSenderInformation,
  93. Val: newTestSession(Session{
  94. ScreenName: "user_sending_chat_msg",
  95. }).GetTLVUserInfo(),
  96. },
  97. },
  98. },
  99. },
  100. },
  101. },
  102. {
  103. name: "send chat room message, don't expect acknowledgement to sender client",
  104. userSession: newTestSession(Session{
  105. ScreenName: "user_sending_chat_msg",
  106. }),
  107. inputSNAC: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  108. Cookie: 1234,
  109. Channel: 14,
  110. TLVRestBlock: oscar.TLVRestBlock{
  111. TLVList: oscar.TLVList{
  112. {
  113. TType: oscar.ChatTLVPublicWhisperFlag,
  114. Val: []byte{},
  115. },
  116. },
  117. },
  118. },
  119. expectSNACBody: nil,
  120. expectSNACToParticipants: XMessage{
  121. snacFrame: oscar.SnacFrame{
  122. FoodGroup: CHAT,
  123. SubGroup: ChatChannelMsgToclient,
  124. },
  125. snacOut: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  126. Cookie: 1234,
  127. Channel: 14,
  128. TLVRestBlock: oscar.TLVRestBlock{
  129. TLVList: oscar.TLVList{
  130. {
  131. TType: oscar.ChatTLVPublicWhisperFlag,
  132. Val: []byte{},
  133. },
  134. {
  135. TType: oscar.ChatTLVSenderInformation,
  136. Val: newTestSession(Session{
  137. ScreenName: "user_sending_chat_msg",
  138. }).GetTLVUserInfo(),
  139. },
  140. },
  141. },
  142. },
  143. },
  144. },
  145. }
  146. for _, tc := range cases {
  147. t.Run(tc.name, func(t *testing.T) {
  148. //
  149. // initialize dependencies
  150. //
  151. crm := NewMockSessionManager(t)
  152. crm.EXPECT().
  153. BroadcastExcept(tc.userSession, tc.expectSNACToParticipants)
  154. //
  155. // send input SNAC
  156. //
  157. input := &bytes.Buffer{}
  158. var seq uint32
  159. assert.NoError(t, oscar.Marshal(tc.inputSNAC, input))
  160. output := &bytes.Buffer{}
  161. snac := oscar.SnacFrame{
  162. FoodGroup: CHAT,
  163. SubGroup: ChatChannelMsgTohost,
  164. }
  165. assert.NoError(t, SendAndReceiveChatChannelMsgTohost(tc.userSession, crm, snac, input, output, &seq))
  166. if tc.expectSNACBody != nil {
  167. //
  168. // verify output
  169. //
  170. flap := oscar.FlapFrame{}
  171. assert.NoError(t, oscar.Unmarshal(&flap, output))
  172. snacFrame := oscar.SnacFrame{}
  173. assert.NoError(t, oscar.Unmarshal(&snacFrame, output))
  174. assert.Equal(t, tc.expectSNACFrame, snacFrame)
  175. //
  176. // verify output SNAC body
  177. //
  178. switch v := tc.expectSNACBody.(type) {
  179. case oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient:
  180. assert.NoError(t, v.SerializeInPlace())
  181. outputSNAC := oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{}
  182. assert.NoError(t, oscar.Unmarshal(&outputSNAC, output))
  183. assert.Equal(t, v, outputSNAC)
  184. default:
  185. t.Fatalf("unexpected output SNAC type")
  186. }
  187. }
  188. assert.Equalf(t, 0, output.Len(), "the rest of the buffer is unread")
  189. })
  190. }
  191. }