chat_nav_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package server
  2. import (
  3. "bytes"
  4. "testing"
  5. "time"
  6. "github.com/mkaminski/goaim/oscar"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/mock"
  9. )
  10. func TestSendAndReceiveCreateRoom(t *testing.T) {
  11. //
  12. // build dependencies
  13. //
  14. userSess := newTestSession(Session{
  15. ID: "sess-id",
  16. ScreenName: "user-screen-name",
  17. })
  18. cr := NewChatRegistry()
  19. sm := NewMockSessionManager(t)
  20. sm.EXPECT().NewSessionWithSN(userSess.ID, userSess.ScreenName).
  21. Return(&Session{})
  22. crf := func() ChatRoom {
  23. return ChatRoom{
  24. Cookie: "dummy-cookie",
  25. CreateTime: time.UnixMilli(0),
  26. SessionManager: sm,
  27. }
  28. }
  29. //
  30. // send input SNAC
  31. //
  32. inputSNAC := oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  33. Exchange: 1,
  34. Cookie: "create", // actual canned value sent by AIM client
  35. InstanceNumber: 2,
  36. DetailLevel: 3,
  37. TLVBlock: oscar.TLVBlock{
  38. TLVList: oscar.TLVList{
  39. {
  40. TType: oscar.ChatTLVRoomName,
  41. Val: "the-chat-room-name",
  42. },
  43. },
  44. },
  45. }
  46. svc := ChatNavService{}
  47. assert.NoError(t, inputSNAC.SerializeInPlace())
  48. outputSNAC, err := svc.CreateRoomHandler(userSess, cr, crf, inputSNAC)
  49. assert.NoError(t, err)
  50. //
  51. // verify chat room created by handler
  52. //
  53. expectChatRoom := ChatRoom{
  54. SessionManager: sm,
  55. Cookie: "dummy-cookie",
  56. CreateTime: time.UnixMilli(0),
  57. DetailLevel: 3,
  58. Exchange: 1,
  59. InstanceNumber: 2,
  60. Name: "the-chat-room-name",
  61. }
  62. chatRoom, err := cr.Retrieve("dummy-cookie")
  63. assert.NoError(t, err)
  64. assert.Equal(t, expectChatRoom, chatRoom)
  65. //
  66. // send input SNAC
  67. //
  68. expectSNAC := XMessage{
  69. snacFrame: oscar.SnacFrame{
  70. FoodGroup: oscar.CHAT_NAV,
  71. SubGroup: oscar.ChatNavNavInfo,
  72. },
  73. snacOut: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
  74. TLVRestBlock: oscar.TLVRestBlock{
  75. TLVList: oscar.TLVList{
  76. {
  77. TType: oscar.ChatNavTLVRoomInfo,
  78. Val: oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  79. Exchange: chatRoom.Exchange,
  80. Cookie: chatRoom.Cookie,
  81. InstanceNumber: chatRoom.InstanceNumber,
  82. DetailLevel: chatRoom.DetailLevel,
  83. TLVBlock: oscar.TLVBlock{
  84. TLVList: chatRoom.TLVList(),
  85. },
  86. },
  87. },
  88. },
  89. },
  90. },
  91. }
  92. assert.Equal(t, expectSNAC, outputSNAC)
  93. }
  94. func TestChatNavRouter_RouteChatNavRouter(t *testing.T) {
  95. cases := []struct {
  96. // name is the unit test name
  97. name string
  98. // input is the request payload
  99. input XMessage
  100. // output is the response payload
  101. output XMessage
  102. // handlerErr is the mocked handler error response
  103. handlerErr error
  104. // expectErr is the expected error returned by the router
  105. expectErr error
  106. }{
  107. {
  108. name: "receive ChatNavRequestChatRights, return ChatNavNavInfo",
  109. input: XMessage{
  110. snacFrame: oscar.SnacFrame{
  111. FoodGroup: oscar.CHAT_NAV,
  112. SubGroup: oscar.ChatNavRequestChatRights,
  113. },
  114. snacOut: struct{}{},
  115. },
  116. output: XMessage{
  117. snacFrame: oscar.SnacFrame{
  118. FoodGroup: oscar.CHAT_NAV,
  119. SubGroup: oscar.ChatNavNavInfo,
  120. },
  121. snacOut: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
  122. TLVRestBlock: oscar.TLVRestBlock{
  123. TLVList: oscar.TLVList{
  124. {
  125. TType: 0x02,
  126. Val: uint8(10),
  127. },
  128. },
  129. },
  130. },
  131. },
  132. },
  133. {
  134. name: "receive ChatNavRequestRoomInfo, return ChatNavNavInfo",
  135. input: XMessage{
  136. snacFrame: oscar.SnacFrame{
  137. FoodGroup: oscar.CHAT_NAV,
  138. SubGroup: oscar.ChatNavRequestRoomInfo,
  139. },
  140. snacOut: oscar.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  141. Exchange: 1,
  142. },
  143. },
  144. output: XMessage{
  145. snacFrame: oscar.SnacFrame{
  146. FoodGroup: oscar.CHAT_NAV,
  147. SubGroup: oscar.ChatNavNavInfo,
  148. },
  149. snacOut: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
  150. TLVRestBlock: oscar.TLVRestBlock{
  151. TLVList: oscar.TLVList{
  152. {
  153. TType: 0x02,
  154. Val: uint8(10),
  155. },
  156. },
  157. },
  158. },
  159. },
  160. },
  161. {
  162. name: "receive ChatNavCreateRoom, return ChatNavNavInfo",
  163. input: XMessage{
  164. snacFrame: oscar.SnacFrame{
  165. FoodGroup: oscar.CHAT_NAV,
  166. SubGroup: oscar.ChatNavCreateRoom,
  167. },
  168. snacOut: oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  169. Exchange: 1,
  170. },
  171. },
  172. output: XMessage{
  173. snacFrame: oscar.SnacFrame{
  174. FoodGroup: oscar.CHAT_NAV,
  175. SubGroup: oscar.ChatNavNavInfo,
  176. },
  177. snacOut: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
  178. TLVRestBlock: oscar.TLVRestBlock{
  179. TLVList: oscar.TLVList{
  180. {
  181. TType: 0x02,
  182. Val: uint8(10),
  183. },
  184. },
  185. },
  186. },
  187. },
  188. },
  189. {
  190. name: "receive ChatNavRequestOccupantList, return ErrUnsupportedSubGroup",
  191. input: XMessage{
  192. snacFrame: oscar.SnacFrame{
  193. FoodGroup: oscar.CHAT_NAV,
  194. SubGroup: oscar.ChatNavRequestOccupantList,
  195. },
  196. snacOut: struct{}{},
  197. },
  198. output: XMessage{},
  199. expectErr: ErrUnsupportedSubGroup,
  200. },
  201. }
  202. for _, tc := range cases {
  203. t.Run(tc.name, func(t *testing.T) {
  204. svc := NewMockChatNavHandler(t)
  205. svc.EXPECT().
  206. RequestChatRightsHandler().
  207. Return(tc.output).
  208. Maybe()
  209. svc.EXPECT().
  210. RequestRoomInfoHandler(mock.Anything, tc.input.snacOut).
  211. Return(tc.output, tc.handlerErr).
  212. Maybe()
  213. svc.EXPECT().
  214. CreateRoomHandler(mock.Anything, mock.Anything, mock.Anything, tc.input.snacOut).
  215. Return(tc.output, tc.handlerErr).
  216. Maybe()
  217. router := ChatNavRouter{
  218. ChatNavHandler: svc,
  219. }
  220. bufIn := &bytes.Buffer{}
  221. assert.NoError(t, oscar.Marshal(tc.input.snacOut, bufIn))
  222. bufOut := &bytes.Buffer{}
  223. seq := uint32(0)
  224. err := router.RouteChatNav(nil, nil, tc.input.snacFrame, bufIn, bufOut, &seq)
  225. assert.ErrorIs(t, err, tc.expectErr)
  226. if tc.expectErr != nil {
  227. return
  228. }
  229. if tc.output.snacFrame == (oscar.SnacFrame{}) {
  230. return
  231. }
  232. // verify the FLAP frame
  233. flap := oscar.FlapFrame{}
  234. assert.NoError(t, oscar.Unmarshal(&flap, bufOut))
  235. // make sure the sequence increments
  236. assert.Equal(t, seq, uint32(1))
  237. assert.Equal(t, flap.Sequence, uint16(0))
  238. flapBuf, err := flap.SNACBuffer(bufOut)
  239. assert.NoError(t, err)
  240. // verify the SNAC frame
  241. snacFrame := oscar.SnacFrame{}
  242. assert.NoError(t, oscar.Unmarshal(&snacFrame, flapBuf))
  243. assert.Equal(t, tc.output.snacFrame, snacFrame)
  244. // verify the SNAC message
  245. snacBuf := &bytes.Buffer{}
  246. assert.NoError(t, oscar.Marshal(tc.output.snacOut, snacBuf))
  247. assert.Equal(t, snacBuf.Bytes(), flapBuf.Bytes())
  248. })
  249. }
  250. }