oservice_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. package handler
  2. import (
  3. "bytes"
  4. "log/slog"
  5. "testing"
  6. "github.com/mk6i/retro-aim-server/server/oscar/middleware"
  7. "github.com/mk6i/retro-aim-server/wire"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/mock"
  10. )
  11. func TestOServiceBOSHandler_ClientOnline(t *testing.T) {
  12. input := wire.SNACMessage{
  13. Frame: wire.SNACFrame{
  14. FoodGroup: wire.OService,
  15. SubGroup: wire.OServiceClientOnline,
  16. },
  17. Body: wire.SNAC_0x01_0x02_OServiceClientOnline{
  18. GroupVersions: []struct {
  19. FoodGroup uint16
  20. Version uint16
  21. ToolID uint16
  22. ToolVersion uint16
  23. }{
  24. {
  25. FoodGroup: 10,
  26. },
  27. },
  28. },
  29. }
  30. svc := newMockOServiceService(t)
  31. svc.EXPECT().
  32. ClientOnline(mock.Anything, input.Body, mock.Anything).
  33. Return(nil)
  34. h := NewOServiceHandler(slog.Default(), svc)
  35. responseWriter := newMockResponseWriter(t)
  36. buf := &bytes.Buffer{}
  37. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  38. assert.NoError(t, h.ClientOnline(nil, nil, input.Frame, buf, responseWriter))
  39. }
  40. func TestOServiceBOSHandler_ServiceRequest(t *testing.T) {
  41. input := wire.SNACMessage{
  42. Frame: wire.SNACFrame{
  43. FoodGroup: wire.OService,
  44. SubGroup: wire.OServiceServiceRequest,
  45. },
  46. Body: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  47. FoodGroup: wire.Chat,
  48. },
  49. }
  50. output := wire.SNACMessage{
  51. Frame: wire.SNACFrame{
  52. FoodGroup: wire.OService,
  53. SubGroup: wire.OServiceServiceResponse,
  54. },
  55. Body: wire.SNAC_0x01_0x05_OServiceServiceResponse{
  56. TLVRestBlock: wire.TLVRestBlock{
  57. TLVList: wire.TLVList{
  58. wire.NewTLVBE(0x01, uint16(1000)),
  59. },
  60. },
  61. },
  62. }
  63. svc := newMockOServiceService(t)
  64. svc.EXPECT().
  65. ServiceRequest(mock.Anything, mock.Anything, input.Frame, input.Body).
  66. Return(output, nil)
  67. h := NewOServiceHandler(slog.Default(), svc)
  68. responseWriter := newMockResponseWriter(t)
  69. responseWriter.EXPECT().
  70. SendSNAC(output.Frame, output.Body).
  71. Return(nil)
  72. buf := &bytes.Buffer{}
  73. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  74. assert.NoError(t, h.ServiceRequest(nil, nil, input.Frame, buf, responseWriter))
  75. }
  76. func TestOServiceChatHandler_ClientOnline(t *testing.T) {
  77. input := wire.SNACMessage{
  78. Frame: wire.SNACFrame{
  79. FoodGroup: wire.OService,
  80. SubGroup: wire.OServiceClientOnline,
  81. },
  82. Body: wire.SNAC_0x01_0x02_OServiceClientOnline{
  83. GroupVersions: []struct {
  84. FoodGroup uint16
  85. Version uint16
  86. ToolID uint16
  87. ToolVersion uint16
  88. }{
  89. {
  90. FoodGroup: 10,
  91. },
  92. },
  93. },
  94. }
  95. svc := newMockOServiceService(t)
  96. svc.EXPECT().
  97. ClientOnline(mock.Anything, mock.Anything, mock.Anything).
  98. Return(nil)
  99. h := NewOServiceHandler(slog.Default(), svc)
  100. responseWriter := newMockResponseWriter(t)
  101. buf := &bytes.Buffer{}
  102. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  103. assert.NoError(t, h.ClientOnline(nil, nil, input.Frame, buf, responseWriter))
  104. }
  105. func TestOServiceHandler_IdleNotification(t *testing.T) {
  106. input := wire.SNACMessage{
  107. Frame: wire.SNACFrame{
  108. FoodGroup: wire.OService,
  109. SubGroup: wire.OServiceIdleNotification,
  110. },
  111. Body: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  112. IdleTime: 10,
  113. },
  114. }
  115. svc := newMockOServiceService(t)
  116. svc.EXPECT().
  117. IdleNotification(mock.Anything, mock.Anything, input.Body).
  118. Return(nil)
  119. h := OServiceHandler{
  120. OServiceService: svc,
  121. RouteLogger: middleware.RouteLogger{
  122. Logger: slog.Default(),
  123. },
  124. }
  125. responseWriter := newMockResponseWriter(t)
  126. buf := &bytes.Buffer{}
  127. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  128. assert.NoError(t, h.IdleNotification(nil, nil, input.Frame, buf, responseWriter))
  129. }
  130. func TestOServiceHandler_ClientVersions(t *testing.T) {
  131. input := wire.SNACMessage{
  132. Frame: wire.SNACFrame{
  133. FoodGroup: wire.OService,
  134. SubGroup: wire.OServiceClientVersions,
  135. },
  136. Body: wire.SNAC_0x01_0x17_OServiceClientVersions{
  137. Versions: []uint16{
  138. 10,
  139. },
  140. },
  141. }
  142. output := wire.SNACMessage{
  143. Frame: wire.SNACFrame{
  144. FoodGroup: wire.OService,
  145. SubGroup: wire.OServiceHostVersions,
  146. },
  147. Body: wire.SNAC_0x01_0x18_OServiceHostVersions{
  148. Versions: []uint16{
  149. 10,
  150. },
  151. },
  152. }
  153. svc := newMockOServiceService(t)
  154. svc.EXPECT().
  155. ClientVersions(mock.Anything, input.Frame, input.Body).
  156. Return(output)
  157. h := OServiceHandler{
  158. OServiceService: svc,
  159. RouteLogger: middleware.RouteLogger{
  160. Logger: slog.Default(),
  161. },
  162. }
  163. responseWriter := newMockResponseWriter(t)
  164. responseWriter.EXPECT().
  165. SendSNAC(output.Frame, output.Body).
  166. Return(nil)
  167. buf := &bytes.Buffer{}
  168. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  169. assert.NoError(t, h.ClientVersions(nil, nil, input.Frame, buf, responseWriter))
  170. }
  171. func TestOServiceHandler_RateParamsQuery(t *testing.T) {
  172. input := wire.SNACMessage{
  173. Frame: wire.SNACFrame{
  174. FoodGroup: wire.OService,
  175. SubGroup: wire.OServiceRateParamsQuery,
  176. },
  177. Body: struct{}{},
  178. }
  179. output := wire.SNACMessage{
  180. Frame: wire.SNACFrame{
  181. FoodGroup: wire.OService,
  182. SubGroup: wire.OServiceRateParamsReply,
  183. },
  184. Body: wire.SNAC_0x01_0x07_OServiceRateParamsReply{
  185. RateGroups: []struct {
  186. ID uint16
  187. Pairs []struct {
  188. FoodGroup uint16
  189. SubGroup uint16
  190. } `oscar:"count_prefix=uint16"`
  191. }{
  192. {
  193. ID: 1,
  194. },
  195. },
  196. },
  197. }
  198. svc := newMockOServiceService(t)
  199. svc.EXPECT().
  200. RateParamsQuery(mock.Anything, mock.Anything, input.Frame).
  201. Return(output)
  202. h := OServiceHandler{
  203. OServiceService: svc,
  204. RouteLogger: middleware.RouteLogger{
  205. Logger: slog.Default(),
  206. },
  207. }
  208. responseWriter := newMockResponseWriter(t)
  209. responseWriter.EXPECT().
  210. SendSNAC(output.Frame, output.Body).
  211. Return(nil)
  212. buf := &bytes.Buffer{}
  213. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  214. assert.NoError(t, h.RateParamsQuery(nil, nil, input.Frame, buf, responseWriter))
  215. }
  216. func TestOServiceHandler_RateParamsSubAdd(t *testing.T) {
  217. input := wire.SNACMessage{
  218. Frame: wire.SNACFrame{
  219. FoodGroup: wire.OService,
  220. SubGroup: wire.OServiceRateParamsSubAdd,
  221. },
  222. Body: wire.SNAC_0x01_0x08_OServiceRateParamsSubAdd{
  223. TLVRestBlock: wire.TLVRestBlock{
  224. TLVList: wire.TLVList{
  225. wire.NewTLVBE(0x01, []byte{1, 2, 3, 4}),
  226. },
  227. },
  228. },
  229. }
  230. svc := newMockOServiceService(t)
  231. svc.EXPECT().
  232. RateParamsSubAdd(mock.Anything, input.Body)
  233. h := OServiceHandler{
  234. OServiceService: svc,
  235. RouteLogger: middleware.RouteLogger{
  236. Logger: slog.Default(),
  237. },
  238. }
  239. responseWriter := newMockResponseWriter(t)
  240. buf := &bytes.Buffer{}
  241. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  242. assert.NoError(t, h.RateParamsSubAdd(nil, nil, input.Frame, buf, responseWriter))
  243. }
  244. func TestOServiceHandler_SetUserInfoFields(t *testing.T) {
  245. input := wire.SNACMessage{
  246. Frame: wire.SNACFrame{
  247. FoodGroup: wire.OService,
  248. SubGroup: wire.OServiceSetUserInfoFields,
  249. },
  250. Body: wire.SNAC_0x01_0x1E_OServiceSetUserInfoFields{
  251. TLVRestBlock: wire.TLVRestBlock{
  252. TLVList: wire.TLVList{
  253. wire.NewTLVBE(0x01, []byte{1, 2, 3, 4}),
  254. },
  255. },
  256. },
  257. }
  258. output := wire.SNACMessage{
  259. Frame: wire.SNACFrame{
  260. FoodGroup: wire.OService,
  261. SubGroup: wire.OServiceUserInfoUpdate,
  262. },
  263. Body: wire.SNAC_0x01_0x0F_OServiceUserInfoUpdate{
  264. TLVUserInfo: wire.TLVUserInfo{
  265. ScreenName: "screen-name",
  266. },
  267. },
  268. }
  269. svc := newMockOServiceService(t)
  270. svc.EXPECT().
  271. SetUserInfoFields(mock.Anything, mock.Anything, input.Frame, input.Body).
  272. Return(output, nil)
  273. h := OServiceHandler{
  274. OServiceService: svc,
  275. RouteLogger: middleware.RouteLogger{
  276. Logger: slog.Default(),
  277. },
  278. }
  279. responseWriter := newMockResponseWriter(t)
  280. responseWriter.EXPECT().
  281. SendSNAC(output.Frame, output.Body).
  282. Return(nil)
  283. buf := &bytes.Buffer{}
  284. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  285. assert.NoError(t, h.SetUserInfoFields(nil, nil, input.Frame, buf, responseWriter))
  286. }
  287. func TestOServiceHandler_UserInfoQuery(t *testing.T) {
  288. input := wire.SNACMessage{
  289. Frame: wire.SNACFrame{
  290. FoodGroup: wire.OService,
  291. SubGroup: wire.OServiceUserInfoQuery,
  292. },
  293. Body: struct{}{},
  294. }
  295. output := wire.SNACMessage{
  296. Frame: wire.SNACFrame{
  297. FoodGroup: wire.OService,
  298. SubGroup: wire.OServiceUserInfoUpdate,
  299. },
  300. Body: wire.SNAC_0x01_0x0F_OServiceUserInfoUpdate{
  301. TLVUserInfo: wire.TLVUserInfo{
  302. ScreenName: "screen-name",
  303. },
  304. },
  305. }
  306. svc := newMockOServiceService(t)
  307. svc.EXPECT().
  308. UserInfoQuery(mock.Anything, mock.Anything, input.Frame).
  309. Return(output)
  310. h := OServiceHandler{
  311. OServiceService: svc,
  312. RouteLogger: middleware.RouteLogger{
  313. Logger: slog.Default(),
  314. },
  315. }
  316. responseWriter := newMockResponseWriter(t)
  317. responseWriter.EXPECT().
  318. SendSNAC(output.Frame, output.Body).
  319. Return(nil)
  320. buf := &bytes.Buffer{}
  321. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  322. assert.NoError(t, h.UserInfoQuery(nil, nil, input.Frame, buf, responseWriter))
  323. }
  324. func TestOServiceHandler_Noop(t *testing.T) {
  325. input := wire.SNACMessage{
  326. Frame: wire.SNACFrame{
  327. FoodGroup: wire.OService,
  328. SubGroup: wire.OServiceNoop,
  329. },
  330. Body: struct{}{},
  331. }
  332. h := OServiceHandler{
  333. RouteLogger: middleware.RouteLogger{
  334. Logger: slog.Default(),
  335. },
  336. }
  337. responseWriter := newMockResponseWriter(t)
  338. buf := &bytes.Buffer{}
  339. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  340. assert.NoError(t, h.Noop(nil, nil, input.Frame, buf, responseWriter))
  341. }
  342. func TestOServiceHandler_SetPrivacyFlags(t *testing.T) {
  343. input := wire.SNACMessage{
  344. Frame: wire.SNACFrame{
  345. FoodGroup: wire.OService,
  346. SubGroup: wire.OServiceSetPrivacyFlags,
  347. },
  348. Body: wire.SNAC_0x01_0x14_OServiceSetPrivacyFlags{
  349. PrivacyFlags: wire.OServicePrivacyFlagMember,
  350. },
  351. }
  352. svc := newMockOServiceService(t)
  353. svc.EXPECT().
  354. SetPrivacyFlags(mock.Anything, input.Body)
  355. h := OServiceHandler{
  356. OServiceService: svc,
  357. RouteLogger: middleware.RouteLogger{
  358. Logger: slog.Default(),
  359. },
  360. }
  361. responseWriter := newMockResponseWriter(t)
  362. buf := &bytes.Buffer{}
  363. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  364. assert.NoError(t, h.SetPrivacyFlags(nil, nil, input.Frame, buf, responseWriter))
  365. }
  366. func TestOServiceChatNavHandler_ClientOnline(t *testing.T) {
  367. input := wire.SNACMessage{
  368. Frame: wire.SNACFrame{
  369. FoodGroup: wire.OService,
  370. SubGroup: wire.OServiceClientOnline,
  371. },
  372. Body: wire.SNAC_0x01_0x02_OServiceClientOnline{
  373. GroupVersions: []struct {
  374. FoodGroup uint16
  375. Version uint16
  376. ToolID uint16
  377. ToolVersion uint16
  378. }{
  379. {
  380. FoodGroup: 10,
  381. },
  382. },
  383. },
  384. }
  385. svc := newMockOServiceService(t)
  386. svc.EXPECT().
  387. ClientOnline(mock.Anything, input.Body, mock.Anything).
  388. Return(nil)
  389. h := NewOServiceHandler(slog.Default(), svc)
  390. responseWriter := newMockResponseWriter(t)
  391. buf := &bytes.Buffer{}
  392. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  393. assert.NoError(t, h.ClientOnline(nil, nil, input.Frame, buf, responseWriter))
  394. }
  395. func TestOServiceAlertHandler_ClientOnline(t *testing.T) {
  396. input := wire.SNACMessage{
  397. Frame: wire.SNACFrame{
  398. FoodGroup: wire.OService,
  399. SubGroup: wire.OServiceClientOnline,
  400. },
  401. Body: wire.SNAC_0x01_0x02_OServiceClientOnline{
  402. GroupVersions: []struct {
  403. FoodGroup uint16
  404. Version uint16
  405. ToolID uint16
  406. ToolVersion uint16
  407. }{
  408. {
  409. FoodGroup: 10,
  410. },
  411. },
  412. },
  413. }
  414. svc := newMockOServiceService(t)
  415. svc.EXPECT().
  416. ClientOnline(mock.Anything, input.Body, mock.Anything).
  417. Return(nil)
  418. h := NewOServiceHandler(slog.Default(), svc)
  419. responseWriter := newMockResponseWriter(t)
  420. buf := &bytes.Buffer{}
  421. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  422. assert.NoError(t, h.ClientOnline(nil, nil, input.Frame, buf, responseWriter))
  423. }