chat_nav_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. oscar.NewTLV(oscar.ChatTLVRoomName, "the-chat-room-name"),
  40. },
  41. },
  42. }
  43. svc := ChatNavService{}
  44. outputSNAC, err := svc.CreateRoomHandler(userSess, cr, crf, inputSNAC)
  45. assert.NoError(t, err)
  46. //
  47. // verify chat room created by handler
  48. //
  49. expectChatRoom := ChatRoom{
  50. SessionManager: sm,
  51. Cookie: "dummy-cookie",
  52. CreateTime: time.UnixMilli(0),
  53. DetailLevel: 3,
  54. Exchange: 1,
  55. InstanceNumber: 2,
  56. Name: "the-chat-room-name",
  57. }
  58. chatRoom, err := cr.Retrieve("dummy-cookie")
  59. assert.NoError(t, err)
  60. assert.Equal(t, expectChatRoom, chatRoom)
  61. //
  62. // send input SNAC
  63. //
  64. expectSNAC := XMessage{
  65. snacFrame: oscar.SnacFrame{
  66. FoodGroup: oscar.CHAT_NAV,
  67. SubGroup: oscar.ChatNavNavInfo,
  68. },
  69. snacOut: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
  70. TLVRestBlock: oscar.TLVRestBlock{
  71. TLVList: oscar.TLVList{
  72. oscar.NewTLV(
  73. oscar.ChatNavTLVRoomInfo,
  74. oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  75. Exchange: chatRoom.Exchange,
  76. Cookie: chatRoom.Cookie,
  77. InstanceNumber: chatRoom.InstanceNumber,
  78. DetailLevel: chatRoom.DetailLevel,
  79. TLVBlock: oscar.TLVBlock{
  80. TLVList: chatRoom.TLVList(),
  81. },
  82. },
  83. ),
  84. },
  85. },
  86. },
  87. }
  88. assert.Equal(t, expectSNAC, outputSNAC)
  89. }
  90. func TestChatNavRouter_RouteChatNavRouter(t *testing.T) {
  91. cases := []struct {
  92. // name is the unit test name
  93. name string
  94. // input is the request payload
  95. input XMessage
  96. // output is the response payload
  97. output XMessage
  98. // handlerErr is the mocked handler error response
  99. handlerErr error
  100. // expectErr is the expected error returned by the router
  101. expectErr error
  102. }{
  103. {
  104. name: "receive ChatNavRequestChatRights, return ChatNavNavInfo",
  105. input: XMessage{
  106. snacFrame: oscar.SnacFrame{
  107. FoodGroup: oscar.CHAT_NAV,
  108. SubGroup: oscar.ChatNavRequestChatRights,
  109. },
  110. snacOut: struct{}{},
  111. },
  112. output: XMessage{
  113. snacFrame: oscar.SnacFrame{
  114. FoodGroup: oscar.CHAT_NAV,
  115. SubGroup: oscar.ChatNavNavInfo,
  116. },
  117. snacOut: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
  118. TLVRestBlock: oscar.TLVRestBlock{
  119. TLVList: oscar.TLVList{
  120. oscar.NewTLV(0x02, uint8(10)),
  121. },
  122. },
  123. },
  124. },
  125. },
  126. {
  127. name: "receive ChatNavRequestRoomInfo, return ChatNavNavInfo",
  128. input: XMessage{
  129. snacFrame: oscar.SnacFrame{
  130. FoodGroup: oscar.CHAT_NAV,
  131. SubGroup: oscar.ChatNavRequestRoomInfo,
  132. },
  133. snacOut: oscar.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  134. Exchange: 1,
  135. },
  136. },
  137. output: XMessage{
  138. snacFrame: oscar.SnacFrame{
  139. FoodGroup: oscar.CHAT_NAV,
  140. SubGroup: oscar.ChatNavNavInfo,
  141. },
  142. snacOut: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
  143. TLVRestBlock: oscar.TLVRestBlock{
  144. TLVList: oscar.TLVList{
  145. oscar.NewTLV(0x02, uint8(10)),
  146. },
  147. },
  148. },
  149. },
  150. },
  151. {
  152. name: "receive ChatNavCreateRoom, return ChatNavNavInfo",
  153. input: XMessage{
  154. snacFrame: oscar.SnacFrame{
  155. FoodGroup: oscar.CHAT_NAV,
  156. SubGroup: oscar.ChatNavCreateRoom,
  157. },
  158. snacOut: oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  159. Exchange: 1,
  160. },
  161. },
  162. output: XMessage{
  163. snacFrame: oscar.SnacFrame{
  164. FoodGroup: oscar.CHAT_NAV,
  165. SubGroup: oscar.ChatNavNavInfo,
  166. },
  167. snacOut: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
  168. TLVRestBlock: oscar.TLVRestBlock{
  169. TLVList: oscar.TLVList{
  170. oscar.NewTLV(0x02, uint8(10)),
  171. },
  172. },
  173. },
  174. },
  175. },
  176. {
  177. name: "receive ChatNavRequestOccupantList, return ErrUnsupportedSubGroup",
  178. input: XMessage{
  179. snacFrame: oscar.SnacFrame{
  180. FoodGroup: oscar.CHAT_NAV,
  181. SubGroup: oscar.ChatNavRequestOccupantList,
  182. },
  183. snacOut: struct{}{},
  184. },
  185. output: XMessage{},
  186. expectErr: ErrUnsupportedSubGroup,
  187. },
  188. }
  189. for _, tc := range cases {
  190. t.Run(tc.name, func(t *testing.T) {
  191. svc := NewMockChatNavHandler(t)
  192. svc.EXPECT().
  193. RequestChatRightsHandler().
  194. Return(tc.output).
  195. Maybe()
  196. svc.EXPECT().
  197. RequestRoomInfoHandler(mock.Anything, tc.input.snacOut).
  198. Return(tc.output, tc.handlerErr).
  199. Maybe()
  200. svc.EXPECT().
  201. CreateRoomHandler(mock.Anything, mock.Anything, mock.Anything, tc.input.snacOut).
  202. Return(tc.output, tc.handlerErr).
  203. Maybe()
  204. router := ChatNavRouter{
  205. ChatNavHandler: svc,
  206. }
  207. bufIn := &bytes.Buffer{}
  208. assert.NoError(t, oscar.Marshal(tc.input.snacOut, bufIn))
  209. bufOut := &bytes.Buffer{}
  210. seq := uint32(0)
  211. err := router.RouteChatNav(nil, nil, tc.input.snacFrame, bufIn, bufOut, &seq)
  212. assert.ErrorIs(t, err, tc.expectErr)
  213. if tc.expectErr != nil {
  214. return
  215. }
  216. if tc.output.snacFrame == (oscar.SnacFrame{}) {
  217. return
  218. }
  219. // verify the FLAP frame
  220. flap := oscar.FlapFrame{}
  221. assert.NoError(t, oscar.Unmarshal(&flap, bufOut))
  222. // make sure the sequence increments
  223. assert.Equal(t, seq, uint32(1))
  224. assert.Equal(t, flap.Sequence, uint16(0))
  225. flapBuf, err := flap.SNACBuffer(bufOut)
  226. assert.NoError(t, err)
  227. // verify the SNAC frame
  228. snacFrame := oscar.SnacFrame{}
  229. assert.NoError(t, oscar.Unmarshal(&snacFrame, flapBuf))
  230. assert.Equal(t, tc.output.snacFrame, snacFrame)
  231. // verify the SNAC message
  232. snacBuf := &bytes.Buffer{}
  233. assert.NoError(t, oscar.Marshal(tc.output.snacOut, snacBuf))
  234. assert.Equal(t, snacBuf.Bytes(), flapBuf.Bytes())
  235. })
  236. }
  237. }