oservice_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. package server
  2. import (
  3. "bytes"
  4. "github.com/stretchr/testify/mock"
  5. "testing"
  6. "time"
  7. "github.com/mkaminski/goaim/oscar"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestReceiveAndSendServiceRequest(t *testing.T) {
  11. cases := []struct {
  12. // name is the unit test name
  13. name string
  14. // cfg is the application config
  15. cfg Config
  16. // chatRoom is the chat room the user connects to
  17. chatRoom *ChatRoom
  18. // userSession is the session of the user requesting the chat service
  19. // info
  20. userSession *Session
  21. // inputSNAC is the SNAC sent by the sender client
  22. inputSNAC oscar.SNAC_0x01_0x04_OServiceServiceRequest
  23. // expectSNACFrame is the SNAC frame sent from the server to the recipient
  24. // client
  25. expectOutput XMessage
  26. // expectErr is the expected error returned by the router
  27. expectErr error
  28. }{
  29. {
  30. name: "request info for ICBM service, return invalid SNAC err",
  31. userSession: &Session{
  32. ScreenName: "user_screen_name",
  33. },
  34. inputSNAC: oscar.SNAC_0x01_0x04_OServiceServiceRequest{
  35. FoodGroup: oscar.ICBM,
  36. },
  37. expectErr: ErrUnsupportedSubGroup,
  38. },
  39. {
  40. name: "request info for connecting to chat room, return chat service and chat room metadata",
  41. cfg: Config{
  42. OSCARHost: "127.0.0.1",
  43. ChatPort: 1234,
  44. },
  45. chatRoom: &ChatRoom{
  46. CreateTime: time.UnixMilli(0),
  47. DetailLevel: 4,
  48. Exchange: 8,
  49. Cookie: "the-chat-cookie",
  50. InstanceNumber: 16,
  51. Name: "my new chat",
  52. },
  53. userSession: &Session{
  54. ID: "user-sess-id",
  55. ScreenName: "user_screen_name",
  56. },
  57. inputSNAC: oscar.SNAC_0x01_0x04_OServiceServiceRequest{
  58. FoodGroup: oscar.CHAT,
  59. TLVRestBlock: oscar.TLVRestBlock{
  60. TLVList: oscar.TLVList{
  61. oscar.NewTLV(0x01, oscar.SNAC_0x01_0x04_TLVRoomInfo{
  62. Exchange: 8,
  63. Cookie: []byte("the-chat-cookie"),
  64. InstanceNumber: 16,
  65. }),
  66. },
  67. },
  68. },
  69. expectOutput: XMessage{
  70. snacFrame: oscar.SnacFrame{
  71. FoodGroup: oscar.OSERVICE,
  72. SubGroup: oscar.OServiceServiceResponse,
  73. },
  74. snacOut: oscar.SNAC_0x01_0x05_OServiceServiceResponse{
  75. TLVRestBlock: oscar.TLVRestBlock{
  76. TLVList: oscar.TLVList{
  77. oscar.NewTLV(oscar.OServiceTLVTagsReconnectHere, "127.0.0.1:1234"),
  78. oscar.NewTLV(oscar.OServiceTLVTagsLoginCookie, ChatCookie{
  79. Cookie: []byte("the-chat-cookie"),
  80. SessID: "user-sess-id",
  81. }),
  82. oscar.NewTLV(oscar.OServiceTLVTagsGroupID, oscar.CHAT),
  83. oscar.NewTLV(oscar.OServiceTLVTagsSSLCertName, ""),
  84. oscar.NewTLV(oscar.OServiceTLVTagsSSLState, uint8(0x00)),
  85. },
  86. },
  87. },
  88. },
  89. },
  90. {
  91. name: "request info for connecting to non-existent chat room, return SNAC error",
  92. cfg: Config{
  93. OSCARHost: "127.0.0.1",
  94. ChatPort: 1234,
  95. },
  96. chatRoom: nil,
  97. userSession: &Session{
  98. ID: "user-sess-id",
  99. ScreenName: "user_screen_name",
  100. },
  101. inputSNAC: oscar.SNAC_0x01_0x04_OServiceServiceRequest{
  102. FoodGroup: oscar.CHAT,
  103. TLVRestBlock: oscar.TLVRestBlock{
  104. TLVList: oscar.TLVList{
  105. oscar.NewTLV(0x01, oscar.SNAC_0x01_0x04_TLVRoomInfo{
  106. Exchange: 8,
  107. Cookie: []byte("the-chat-cookie"),
  108. InstanceNumber: 16,
  109. }),
  110. },
  111. },
  112. },
  113. expectErr: ErrUnsupportedSubGroup,
  114. },
  115. }
  116. for _, tc := range cases {
  117. t.Run(tc.name, func(t *testing.T) {
  118. //
  119. // initialize dependencies
  120. //
  121. sm := NewMockSessionManager(t)
  122. cr := NewChatRegistry()
  123. if tc.chatRoom != nil {
  124. sm.EXPECT().
  125. NewSessionWithSN(tc.userSession.ID, tc.userSession.ScreenName).
  126. Return(&Session{}).
  127. Maybe()
  128. tc.chatRoom.SessionManager = sm
  129. cr.Register(*tc.chatRoom)
  130. }
  131. //
  132. // send input SNAC
  133. //
  134. svc := OServiceService{}
  135. outputSNAC, err := svc.ServiceRequestHandler(tc.cfg, cr, tc.userSession, tc.inputSNAC)
  136. assert.ErrorIs(t, err, tc.expectErr)
  137. if tc.expectErr != nil {
  138. return
  139. }
  140. //
  141. // verify output
  142. //
  143. assert.Equal(t, tc.expectOutput, outputSNAC)
  144. })
  145. }
  146. }
  147. func TestOServiceRouter_RouteOService(t *testing.T) {
  148. cases := []struct {
  149. // name is the unit test name
  150. name string
  151. // input is the request payload
  152. input XMessage
  153. // output is the response payload
  154. output XMessage
  155. // handlerErr is the mocked handler error response
  156. handlerErr error
  157. // expectErr is the expected error returned by the router
  158. expectErr error
  159. }{
  160. {
  161. name: "receive OServiceClientOnline, return no response",
  162. input: XMessage{
  163. snacFrame: oscar.SnacFrame{
  164. FoodGroup: oscar.OSERVICE,
  165. SubGroup: oscar.OServiceClientOnline,
  166. },
  167. snacOut: oscar.SNAC_0x01_0x02_OServiceClientOnline{
  168. GroupVersions: []struct {
  169. FoodGroup uint16
  170. Version uint16
  171. ToolID uint16
  172. ToolVersion uint16
  173. }{
  174. {
  175. FoodGroup: 10,
  176. },
  177. },
  178. },
  179. },
  180. output: XMessage{},
  181. },
  182. {
  183. name: "receive OServiceServiceRequest, return OServiceServiceResponse",
  184. input: XMessage{
  185. snacFrame: oscar.SnacFrame{
  186. FoodGroup: oscar.OSERVICE,
  187. SubGroup: oscar.OServiceServiceRequest,
  188. },
  189. snacOut: oscar.SNAC_0x01_0x04_OServiceServiceRequest{
  190. FoodGroup: 10,
  191. },
  192. },
  193. output: XMessage{
  194. snacFrame: oscar.SnacFrame{
  195. FoodGroup: oscar.OSERVICE,
  196. SubGroup: oscar.OServiceServiceResponse,
  197. },
  198. snacOut: oscar.SNAC_0x01_0x05_OServiceServiceResponse{
  199. TLVRestBlock: oscar.TLVRestBlock{
  200. TLVList: oscar.TLVList{
  201. oscar.NewTLV(0x01, uint16(1000)),
  202. },
  203. },
  204. },
  205. },
  206. },
  207. {
  208. name: "receive OServiceRateParamsQuery, return OServiceRateParamsReply",
  209. input: XMessage{
  210. snacFrame: oscar.SnacFrame{
  211. FoodGroup: oscar.OSERVICE,
  212. SubGroup: oscar.OServiceRateParamsQuery,
  213. },
  214. snacOut: struct{}{},
  215. },
  216. output: XMessage{
  217. snacFrame: oscar.SnacFrame{
  218. FoodGroup: oscar.OSERVICE,
  219. SubGroup: oscar.OServiceRateParamsReply,
  220. },
  221. snacOut: oscar.SNAC_0x01_0x07_OServiceRateParamsReply{
  222. RateGroups: []struct {
  223. ID uint16
  224. Pairs []struct {
  225. FoodGroup uint16
  226. SubGroup uint16
  227. } `count_prefix:"uint16"`
  228. }{
  229. {
  230. ID: 1,
  231. },
  232. },
  233. },
  234. },
  235. },
  236. {
  237. name: "receive OServiceRateParamsSubAdd, return no response",
  238. input: XMessage{
  239. snacFrame: oscar.SnacFrame{
  240. FoodGroup: oscar.OSERVICE,
  241. SubGroup: oscar.OServiceRateParamsSubAdd,
  242. },
  243. snacOut: oscar.SNAC_0x01_0x08_OServiceRateParamsSubAdd{
  244. TLVRestBlock: oscar.TLVRestBlock{
  245. TLVList: oscar.TLVList{
  246. oscar.NewTLV(0x01, []byte{1, 2, 3, 4}),
  247. },
  248. },
  249. },
  250. },
  251. output: XMessage{},
  252. },
  253. {
  254. name: "receive OServiceUserInfoQuery, return OServiceUserInfoUpdate",
  255. input: XMessage{
  256. snacFrame: oscar.SnacFrame{
  257. FoodGroup: oscar.OSERVICE,
  258. SubGroup: oscar.OServiceUserInfoQuery,
  259. },
  260. snacOut: struct{}{},
  261. },
  262. output: XMessage{
  263. snacFrame: oscar.SnacFrame{
  264. FoodGroup: oscar.OSERVICE,
  265. SubGroup: oscar.OServiceUserInfoUpdate,
  266. },
  267. snacOut: oscar.SNAC_0x01_0x0F_OServiceUserInfoUpdate{
  268. TLVUserInfo: oscar.TLVUserInfo{
  269. ScreenName: "screen-name",
  270. },
  271. },
  272. },
  273. },
  274. {
  275. name: "receive OServiceIdleNotification, return no response",
  276. input: XMessage{
  277. snacFrame: oscar.SnacFrame{
  278. FoodGroup: oscar.OSERVICE,
  279. SubGroup: oscar.OServiceIdleNotification,
  280. },
  281. snacOut: oscar.SNAC_0x01_0x11_OServiceIdleNotification{
  282. IdleTime: 10,
  283. },
  284. },
  285. output: XMessage{},
  286. },
  287. {
  288. name: "receive OServiceClientVersions, return OServiceHostVersions",
  289. input: XMessage{
  290. snacFrame: oscar.SnacFrame{
  291. FoodGroup: oscar.OSERVICE,
  292. SubGroup: oscar.OServiceClientVersions,
  293. },
  294. snacOut: oscar.SNAC_0x01_0x17_OServiceClientVersions{
  295. Versions: []uint16{
  296. 10,
  297. },
  298. },
  299. },
  300. output: XMessage{
  301. snacFrame: oscar.SnacFrame{
  302. FoodGroup: oscar.OSERVICE,
  303. SubGroup: oscar.OServiceHostVersions,
  304. },
  305. snacOut: oscar.SNAC_0x01_0x18_OServiceHostVersions{
  306. Versions: []uint16{
  307. 10,
  308. },
  309. },
  310. },
  311. },
  312. {
  313. name: "receive OServiceSetUserInfoFields, return OServiceUserInfoUpdate",
  314. input: XMessage{
  315. snacFrame: oscar.SnacFrame{
  316. FoodGroup: oscar.OSERVICE,
  317. SubGroup: oscar.OServiceSetUserInfoFields,
  318. },
  319. snacOut: oscar.SNAC_0x01_0x1E_OServiceSetUserInfoFields{
  320. TLVRestBlock: oscar.TLVRestBlock{
  321. TLVList: oscar.TLVList{
  322. oscar.NewTLV(0x01, []byte{1, 2, 3, 4}),
  323. },
  324. },
  325. },
  326. },
  327. output: XMessage{
  328. snacFrame: oscar.SnacFrame{
  329. FoodGroup: oscar.OSERVICE,
  330. SubGroup: oscar.OServiceUserInfoUpdate,
  331. },
  332. snacOut: oscar.SNAC_0x01_0x0F_OServiceUserInfoUpdate{
  333. TLVUserInfo: oscar.TLVUserInfo{
  334. ScreenName: "screen-name",
  335. },
  336. },
  337. },
  338. },
  339. {
  340. name: "receive OServicePauseReq, expect ErrUnsupportedSubGroup",
  341. input: XMessage{
  342. snacFrame: oscar.SnacFrame{
  343. FoodGroup: oscar.OSERVICE,
  344. SubGroup: oscar.OServicePauseReq,
  345. },
  346. snacOut: struct{}{}, // empty SNAC
  347. },
  348. output: XMessage{}, // empty SNAC
  349. expectErr: ErrUnsupportedSubGroup,
  350. },
  351. }
  352. for _, tc := range cases {
  353. t.Run(tc.name, func(t *testing.T) {
  354. svc := NewMockOServiceHandler(t)
  355. svc.EXPECT().
  356. ServiceRequestHandler(mock.Anything, mock.Anything, mock.Anything, tc.input.snacOut).
  357. Return(tc.output, tc.handlerErr).
  358. Maybe()
  359. svc.EXPECT().
  360. RateParamsQueryHandler().
  361. Return(tc.output).
  362. Maybe()
  363. svc.EXPECT().
  364. UserInfoQueryHandler(mock.Anything).
  365. Return(tc.output).
  366. Maybe()
  367. svc.EXPECT().
  368. ClientVersionsHandler(tc.input.snacOut).
  369. Return(tc.output).
  370. Maybe()
  371. svc.EXPECT().
  372. SetUserInfoFieldsHandler(mock.Anything, mock.Anything, mock.Anything, tc.input.snacOut).
  373. Return(tc.output, tc.handlerErr).
  374. Maybe()
  375. svc.EXPECT().
  376. ClientOnlineHandler(tc.input.snacOut, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
  377. Return(tc.handlerErr).
  378. Maybe()
  379. svc.EXPECT().
  380. RateParamsSubAddHandler(tc.input.snacOut).
  381. Maybe()
  382. svc.EXPECT().
  383. IdleNotificationHandler(mock.Anything, mock.Anything, mock.Anything, tc.input.snacOut).
  384. Return(tc.handlerErr).
  385. Maybe()
  386. router := OServiceRouter{
  387. OServiceHandler: svc,
  388. }
  389. bufIn := &bytes.Buffer{}
  390. assert.NoError(t, oscar.Marshal(tc.input.snacOut, bufIn))
  391. bufOut := &bytes.Buffer{}
  392. seq := uint32(1)
  393. err := router.RouteOService(Config{}, nil, nil, nil, nil, ChatRoom{}, tc.input.snacFrame, bufIn, bufOut, &seq)
  394. assert.ErrorIs(t, err, tc.expectErr)
  395. if tc.expectErr != nil {
  396. return
  397. }
  398. if tc.output == (XMessage{}) {
  399. // make sure no response was sent
  400. assert.Empty(t, bufOut.Bytes())
  401. return
  402. }
  403. // verify the FLAP frame
  404. flap := oscar.FlapFrame{}
  405. assert.NoError(t, oscar.Unmarshal(&flap, bufOut))
  406. // make sure the sequence number was incremented
  407. assert.Equal(t, uint32(2), seq)
  408. flapBuf, err := flap.SNACBuffer(bufOut)
  409. assert.NoError(t, err)
  410. // verify the SNAC frame
  411. snacFrame := oscar.SnacFrame{}
  412. assert.NoError(t, oscar.Unmarshal(&snacFrame, flapBuf))
  413. assert.Equal(t, tc.output.snacFrame, snacFrame)
  414. // verify the SNAC message
  415. snacBuf := &bytes.Buffer{}
  416. assert.NoError(t, oscar.Marshal(tc.output.snacOut, snacBuf))
  417. assert.Equal(t, snacBuf.Bytes(), flapBuf.Bytes())
  418. })
  419. }
  420. }