chat_nav_test.go 6.3 KB

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