bart_test.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. package foodgroup
  2. import (
  3. "bytes"
  4. "context"
  5. "io"
  6. "log/slog"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/mock"
  10. "github.com/mk6i/open-oscar-server/state"
  11. "github.com/mk6i/open-oscar-server/wire"
  12. )
  13. func TestBARTService_UpsertItem(t *testing.T) {
  14. itemHash := []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed}
  15. itemData := []byte{'i', 't', 'e', 'm', 'd', 'a', 't', 'a'}
  16. cases := []struct {
  17. // name is the unit test name
  18. name string
  19. // instance is the session of the user adding to feedbag
  20. instance *state.SessionInstance
  21. // inputSNAC is the SNAC sent from the client to the server
  22. inputSNAC wire.SNACMessage
  23. // mockParams is the list of params sent to mocks that satisfy this
  24. // method's dependencies
  25. mockParams mockParams
  26. // expectOutput is the SNAC sent from the server to client
  27. expectOutput wire.SNACMessage
  28. // wantErr is the expected error
  29. wantErr error
  30. // instanceMatch verifies the session state after completion
  31. instanceMatch func(instance *state.SessionInstance)
  32. }{
  33. {
  34. name: "awaiting upload, icon not yet in the BART store",
  35. instance: newTestInstance("user_screen_name", sessOptBuddyIcon(wire.BARTID{
  36. Type: wire.BARTTypesBuddyIcon,
  37. BARTInfo: wire.BARTInfo{
  38. Flags: wire.BARTFlagsCustom | wire.BARTFlagsUnknown,
  39. Hash: itemHash,
  40. },
  41. })),
  42. inputSNAC: wire.SNACMessage{
  43. Frame: wire.SNACFrame{
  44. RequestID: 1234,
  45. },
  46. Body: wire.SNAC_0x10_0x02_BARTUploadQuery{
  47. Type: wire.BARTTypesBuddyIcon,
  48. Data: itemData,
  49. },
  50. },
  51. mockParams: mockParams{
  52. bartItemManagerParams: bartItemManagerParams{
  53. bartItemManagerUpsertParams: bartItemManagerUpsertParams{
  54. {
  55. itemHash: itemHash,
  56. payload: itemData,
  57. bartType: wire.BARTTypesBuddyIcon,
  58. },
  59. },
  60. },
  61. buddyBroadcasterParams: buddyBroadcasterParams{
  62. broadcastBuddyArrivedParams: broadcastBuddyArrivedParams{
  63. {
  64. screenName: state.DisplayScreenName("user_screen_name"),
  65. bodyMatcher: func(tlvInfo wire.TLVUserInfo) bool {
  66. bartID, exists := tlvInfo.Bytes(wire.OServiceUserInfoBARTInfo)
  67. return exists &&
  68. tlvInfo.ScreenName == "user_screen_name" &&
  69. bytes.Contains(bartID, itemHash)
  70. },
  71. },
  72. },
  73. },
  74. messageRelayerParams: messageRelayerParams{
  75. relayToSelfParams: relayToSelfParams{
  76. {
  77. screenName: state.NewIdentScreenName("user_screen_name"),
  78. message: wire.SNACMessage{
  79. Frame: wire.SNACFrame{
  80. FoodGroup: wire.OService,
  81. SubGroup: wire.OServiceUserInfoUpdate,
  82. RequestID: wire.ReqIDFromServer,
  83. },
  84. Body: func(val any) bool {
  85. snac, ok := val.(wire.SNAC_0x01_0x0F_OServiceUserInfoUpdate)
  86. if !ok {
  87. return false
  88. }
  89. bartID, exists := snac.UserInfo[0].Bytes(wire.OServiceUserInfoBARTInfo)
  90. return exists &&
  91. snac.UserInfo[0].ScreenName == "user_screen_name" &&
  92. bytes.Contains(bartID, itemHash)
  93. },
  94. },
  95. },
  96. },
  97. },
  98. },
  99. expectOutput: wire.SNACMessage{
  100. Frame: wire.SNACFrame{
  101. FoodGroup: wire.BART,
  102. SubGroup: wire.BARTUploadReply,
  103. RequestID: 1234,
  104. },
  105. Body: wire.SNAC_0x10_0x03_BARTUploadReply{
  106. Code: wire.BARTReplyCodesSuccess,
  107. ID: wire.BARTID{
  108. Type: wire.BARTTypesBuddyIcon,
  109. BARTInfo: wire.BARTInfo{
  110. Flags: wire.BARTFlagsCustom | wire.BARTFlagsKnown,
  111. Hash: itemHash,
  112. },
  113. },
  114. },
  115. },
  116. instanceMatch: func(instance *state.SessionInstance) {
  117. have, hasIcon := instance.Session().BuddyIcon()
  118. assert.True(t, hasIcon)
  119. want := wire.BARTID{
  120. Type: wire.BARTTypesBuddyIcon,
  121. BARTInfo: wire.BARTInfo{
  122. Flags: wire.BARTFlagsCustom | wire.BARTFlagsKnown,
  123. Hash: itemHash,
  124. },
  125. }
  126. assert.Equal(t, want, have)
  127. },
  128. },
  129. {
  130. name: "awaiting upload, icon already in the BART store",
  131. instance: newTestInstance("user_screen_name", sessOptBuddyIcon(wire.BARTID{
  132. Type: wire.BARTTypesBuddyIcon,
  133. BARTInfo: wire.BARTInfo{
  134. Flags: wire.BARTFlagsCustom | wire.BARTFlagsUnknown,
  135. Hash: itemHash,
  136. },
  137. })),
  138. inputSNAC: wire.SNACMessage{
  139. Frame: wire.SNACFrame{
  140. RequestID: 1234,
  141. },
  142. Body: wire.SNAC_0x10_0x02_BARTUploadQuery{
  143. Type: wire.BARTTypesBuddyIcon,
  144. Data: itemData,
  145. },
  146. },
  147. mockParams: mockParams{
  148. bartItemManagerParams: bartItemManagerParams{
  149. bartItemManagerUpsertParams: bartItemManagerUpsertParams{
  150. {
  151. itemHash: itemHash,
  152. payload: itemData,
  153. bartType: wire.BARTTypesBuddyIcon,
  154. err: state.ErrBARTItemExists,
  155. },
  156. },
  157. },
  158. buddyBroadcasterParams: buddyBroadcasterParams{
  159. broadcastBuddyArrivedParams: broadcastBuddyArrivedParams{
  160. {
  161. screenName: state.DisplayScreenName("user_screen_name"),
  162. bodyMatcher: func(tlvInfo wire.TLVUserInfo) bool {
  163. bartID, exists := tlvInfo.Bytes(wire.OServiceUserInfoBARTInfo)
  164. return exists &&
  165. tlvInfo.ScreenName == "user_screen_name" &&
  166. bytes.Contains(bartID, itemHash)
  167. },
  168. },
  169. },
  170. },
  171. messageRelayerParams: messageRelayerParams{
  172. relayToSelfParams: relayToSelfParams{
  173. {
  174. screenName: state.NewIdentScreenName("user_screen_name"),
  175. message: wire.SNACMessage{
  176. Frame: wire.SNACFrame{
  177. FoodGroup: wire.OService,
  178. SubGroup: wire.OServiceUserInfoUpdate,
  179. RequestID: wire.ReqIDFromServer,
  180. },
  181. Body: func(val any) bool {
  182. snac, ok := val.(wire.SNAC_0x01_0x0F_OServiceUserInfoUpdate)
  183. if !ok {
  184. return false
  185. }
  186. bartID, exists := snac.UserInfo[0].Bytes(wire.OServiceUserInfoBARTInfo)
  187. return exists &&
  188. snac.UserInfo[0].ScreenName == "user_screen_name" &&
  189. bytes.Contains(bartID, itemHash)
  190. },
  191. },
  192. },
  193. },
  194. },
  195. },
  196. expectOutput: wire.SNACMessage{
  197. Frame: wire.SNACFrame{
  198. FoodGroup: wire.BART,
  199. SubGroup: wire.BARTUploadReply,
  200. RequestID: 1234,
  201. },
  202. Body: wire.SNAC_0x10_0x03_BARTUploadReply{
  203. Code: wire.BARTReplyCodesSuccess,
  204. ID: wire.BARTID{
  205. Type: wire.BARTTypesBuddyIcon,
  206. BARTInfo: wire.BARTInfo{
  207. Flags: wire.BARTFlagsCustom | wire.BARTFlagsKnown,
  208. Hash: itemHash,
  209. },
  210. },
  211. },
  212. },
  213. instanceMatch: func(instance *state.SessionInstance) {
  214. have, hasIcon := instance.Session().BuddyIcon()
  215. assert.True(t, hasIcon)
  216. want := wire.BARTID{
  217. Type: wire.BARTTypesBuddyIcon,
  218. BARTInfo: wire.BARTInfo{
  219. Flags: wire.BARTFlagsCustom | wire.BARTFlagsKnown,
  220. Hash: itemHash,
  221. },
  222. }
  223. assert.Equal(t, want, have)
  224. },
  225. },
  226. {
  227. name: "re-upload of an icon already uploaded this session",
  228. instance: newTestInstance("user_screen_name", sessOptBuddyIcon(wire.BARTID{
  229. Type: wire.BARTTypesBuddyIcon,
  230. BARTInfo: wire.BARTInfo{
  231. Flags: wire.BARTFlagsCustom,
  232. Hash: itemHash,
  233. },
  234. })),
  235. inputSNAC: wire.SNACMessage{
  236. Frame: wire.SNACFrame{
  237. RequestID: 1234,
  238. },
  239. Body: wire.SNAC_0x10_0x02_BARTUploadQuery{
  240. Type: wire.BARTTypesBuddyIcon,
  241. Data: itemData,
  242. },
  243. },
  244. mockParams: mockParams{
  245. bartItemManagerParams: bartItemManagerParams{
  246. bartItemManagerUpsertParams: bartItemManagerUpsertParams{
  247. {
  248. itemHash: itemHash,
  249. payload: itemData,
  250. bartType: wire.BARTTypesBuddyIcon,
  251. err: state.ErrBARTItemExists,
  252. },
  253. },
  254. },
  255. buddyBroadcasterParams: buddyBroadcasterParams{
  256. broadcastBuddyArrivedParams: broadcastBuddyArrivedParams{
  257. {
  258. screenName: state.DisplayScreenName("user_screen_name"),
  259. bodyMatcher: func(tlvInfo wire.TLVUserInfo) bool {
  260. b, exists := tlvInfo.Bytes(wire.OServiceUserInfoBARTInfo)
  261. if !exists || !bytes.Contains(b, itemHash) {
  262. return false
  263. }
  264. // buddies must not be told the icon still
  265. // needs uploading
  266. bartID := wire.BARTID{}
  267. if err := wire.UnmarshalBE(&bartID, bytes.NewBuffer(b)); err != nil {
  268. return false
  269. }
  270. return bartID.Flags&wire.BARTFlagsUnknown == 0
  271. },
  272. },
  273. },
  274. },
  275. messageRelayerParams: messageRelayerParams{
  276. relayToSelfParams: relayToSelfParams{
  277. {
  278. screenName: state.NewIdentScreenName("user_screen_name"),
  279. message: wire.SNACMessage{
  280. Frame: wire.SNACFrame{
  281. FoodGroup: wire.OService,
  282. SubGroup: wire.OServiceUserInfoUpdate,
  283. RequestID: wire.ReqIDFromServer,
  284. },
  285. Body: func(val any) bool {
  286. snac, ok := val.(wire.SNAC_0x01_0x0F_OServiceUserInfoUpdate)
  287. if !ok {
  288. return false
  289. }
  290. bartID, exists := snac.UserInfo[0].Bytes(wire.OServiceUserInfoBARTInfo)
  291. return exists &&
  292. snac.UserInfo[0].ScreenName == "user_screen_name" &&
  293. bytes.Contains(bartID, itemHash)
  294. },
  295. },
  296. },
  297. },
  298. },
  299. },
  300. expectOutput: wire.SNACMessage{
  301. Frame: wire.SNACFrame{
  302. FoodGroup: wire.BART,
  303. SubGroup: wire.BARTUploadReply,
  304. RequestID: 1234,
  305. },
  306. Body: wire.SNAC_0x10_0x03_BARTUploadReply{
  307. Code: wire.BARTReplyCodesSuccess,
  308. ID: wire.BARTID{
  309. Type: wire.BARTTypesBuddyIcon,
  310. BARTInfo: wire.BARTInfo{
  311. Flags: wire.BARTFlagsCustom,
  312. Hash: itemHash,
  313. },
  314. },
  315. },
  316. },
  317. instanceMatch: func(instance *state.SessionInstance) {
  318. have, hasIcon := instance.Session().BuddyIcon()
  319. assert.True(t, hasIcon)
  320. want := wire.BARTID{
  321. Type: wire.BARTTypesBuddyIcon,
  322. BARTInfo: wire.BARTInfo{
  323. Flags: wire.BARTFlagsCustom,
  324. Hash: itemHash,
  325. },
  326. }
  327. assert.Equal(t, want, have)
  328. },
  329. },
  330. {
  331. name: "insert new buddy icon, get insertion error",
  332. instance: newTestInstance("user_screen_name"),
  333. inputSNAC: wire.SNACMessage{
  334. Frame: wire.SNACFrame{
  335. RequestID: 1234,
  336. },
  337. Body: wire.SNAC_0x10_0x02_BARTUploadQuery{
  338. Type: wire.BARTTypesBuddyIcon,
  339. Data: itemData,
  340. },
  341. },
  342. mockParams: mockParams{
  343. bartItemManagerParams: bartItemManagerParams{
  344. bartItemManagerUpsertParams: bartItemManagerUpsertParams{
  345. {
  346. itemHash: itemHash,
  347. payload: itemData,
  348. bartType: wire.BARTTypesBuddyIcon,
  349. err: io.EOF,
  350. },
  351. },
  352. },
  353. },
  354. expectOutput: wire.SNACMessage{},
  355. wantErr: io.EOF,
  356. },
  357. {
  358. name: "insert new buddy icon unrelated to current session",
  359. instance: newTestInstance("user_screen_name", sessOptBuddyIcon(wire.BARTID{
  360. Type: wire.BARTTypesBuddyIcon,
  361. BARTInfo: wire.BARTInfo{
  362. Flags: wire.BARTFlagsCustom | wire.BARTFlagsKnown,
  363. Hash: []byte("unrelated icon"),
  364. },
  365. })),
  366. inputSNAC: wire.SNACMessage{
  367. Frame: wire.SNACFrame{
  368. RequestID: 1234,
  369. },
  370. Body: wire.SNAC_0x10_0x02_BARTUploadQuery{
  371. Type: wire.BARTTypesBuddyIcon,
  372. Data: itemData,
  373. },
  374. },
  375. mockParams: mockParams{
  376. bartItemManagerParams: bartItemManagerParams{
  377. bartItemManagerUpsertParams: bartItemManagerUpsertParams{
  378. {
  379. itemHash: itemHash,
  380. payload: itemData,
  381. bartType: wire.BARTTypesBuddyIcon,
  382. },
  383. },
  384. },
  385. messageRelayerParams: messageRelayerParams{},
  386. },
  387. expectOutput: wire.SNACMessage{
  388. Frame: wire.SNACFrame{
  389. FoodGroup: wire.BART,
  390. SubGroup: wire.BARTUploadReply,
  391. RequestID: 1234,
  392. },
  393. Body: wire.SNAC_0x10_0x03_BARTUploadReply{
  394. Code: wire.BARTReplyCodesSuccess,
  395. ID: wire.BARTID{
  396. Type: wire.BARTTypesBuddyIcon,
  397. BARTInfo: wire.BARTInfo{
  398. Flags: wire.BARTFlagsCustom,
  399. Hash: itemHash,
  400. },
  401. },
  402. },
  403. },
  404. instanceMatch: func(instance *state.SessionInstance) {
  405. // assert session icon didn't change
  406. have, hasIcon := instance.Session().BuddyIcon()
  407. assert.True(t, hasIcon)
  408. want := wire.BARTID{
  409. Type: wire.BARTTypesBuddyIcon,
  410. BARTInfo: wire.BARTInfo{
  411. Flags: wire.BARTFlagsCustom | wire.BARTFlagsKnown,
  412. Hash: []byte("unrelated icon"),
  413. },
  414. }
  415. assert.Equal(t, want, have)
  416. },
  417. },
  418. }
  419. for _, tc := range cases {
  420. t.Run(tc.name, func(t *testing.T) {
  421. bartItemManager := newMockBARTItemManager(t)
  422. for _, params := range tc.mockParams.bartItemManagerUpsertParams {
  423. bartItemManager.EXPECT().
  424. InsertBARTItem(matchContext(), params.itemHash, params.payload, params.bartType).
  425. Return(params.err)
  426. }
  427. buddyUpdateBroadcaster := newMockbuddyBroadcaster(t)
  428. for _, params := range tc.mockParams.broadcastBuddyArrivedParams {
  429. buddyUpdateBroadcaster.EXPECT().
  430. BroadcastBuddyArrived(mock.Anything,
  431. state.NewIdentScreenName(params.screenName.String()),
  432. mock.MatchedBy(params.bodyMatcher)).
  433. Return(params.err)
  434. }
  435. messageRelayer := newMockMessageRelayer(t)
  436. for _, params := range tc.mockParams.relayToSelfParams {
  437. messageRelayer.EXPECT().
  438. RelayToSelf(matchContext(), matchSession(params.screenName), mock.MatchedBy(func(message wire.SNACMessage) bool {
  439. return params.message.Frame == message.Frame &&
  440. params.message.Body.(func(any) bool)(message.Body)
  441. }))
  442. }
  443. svc := NewBARTService(slog.Default(), bartItemManager, messageRelayer, nil, nil)
  444. svc.buddyUpdateBroadcaster = buddyUpdateBroadcaster
  445. output, err := svc.UpsertItem(context.Background(), tc.instance, tc.inputSNAC.Frame,
  446. tc.inputSNAC.Body.(wire.SNAC_0x10_0x02_BARTUploadQuery))
  447. assert.ErrorIs(t, err, tc.wantErr)
  448. assert.Equal(t, output, tc.expectOutput)
  449. if tc.instanceMatch != nil {
  450. tc.instanceMatch(tc.instance)
  451. }
  452. })
  453. }
  454. }
  455. func TestBARTService_RetrieveItem(t *testing.T) {
  456. cases := []struct {
  457. // name is the unit test name
  458. name string
  459. // instance is the session of the user adding to feedbag
  460. instance *state.SessionInstance
  461. // inputSNAC is the SNAC sent from the client to the server
  462. inputSNAC wire.SNACMessage
  463. // mockParams is the list of params sent to mocks that satisfy this
  464. // method's dependencies
  465. mockParams mockParams
  466. // expectOutput is the SNAC sent from the server to client
  467. expectOutput wire.SNACMessage
  468. // expectErr is the expected error
  469. expectErr error
  470. }{
  471. {
  472. name: "retrieve buddy icon",
  473. instance: newTestInstance("user_screen_name"),
  474. inputSNAC: wire.SNACMessage{
  475. Frame: wire.SNACFrame{
  476. RequestID: 1234,
  477. },
  478. Body: wire.SNAC_0x10_0x04_BARTDownloadQuery{
  479. ScreenName: "user_screen_name",
  480. Command: 1,
  481. BARTID: wire.BARTID{
  482. Type: wire.BARTTypesBuddyIcon,
  483. BARTInfo: wire.BARTInfo{
  484. Flags: wire.BARTFlagsKnown,
  485. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  486. },
  487. },
  488. },
  489. },
  490. mockParams: mockParams{
  491. bartItemManagerParams: bartItemManagerParams{
  492. bartItemManagerRetrieveParams: bartItemManagerRetrieveParams{
  493. {
  494. itemHash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  495. result: []byte{'i', 't', 'e', 'm', 'd', 'a', 't', 'a'},
  496. },
  497. },
  498. },
  499. },
  500. expectOutput: wire.SNACMessage{
  501. Frame: wire.SNACFrame{
  502. FoodGroup: wire.BART,
  503. SubGroup: wire.BARTDownloadReply,
  504. RequestID: 1234,
  505. },
  506. Body: wire.SNAC_0x10_0x05_BARTDownloadReply{
  507. ScreenName: "user_screen_name",
  508. BARTID: wire.BARTID{
  509. Type: wire.BARTTypesBuddyIcon,
  510. BARTInfo: wire.BARTInfo{
  511. Flags: wire.BARTFlagsKnown,
  512. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  513. },
  514. },
  515. Data: []byte{'i', 't', 'e', 'm', 'd', 'a', 't', 'a'},
  516. },
  517. },
  518. },
  519. {
  520. name: "retrieve blank icon used for clearing buddy icon",
  521. instance: newTestInstance("user_screen_name"),
  522. inputSNAC: wire.SNACMessage{
  523. Frame: wire.SNACFrame{
  524. RequestID: 1234,
  525. },
  526. Body: wire.SNAC_0x10_0x04_BARTDownloadQuery{
  527. ScreenName: "user_screen_name",
  528. Command: 1,
  529. BARTID: wire.BARTID{
  530. Type: wire.BARTTypesBuddyIcon,
  531. BARTInfo: wire.BARTInfo{
  532. Flags: wire.BARTFlagsKnown,
  533. Hash: wire.GetClearIconHash(),
  534. },
  535. },
  536. },
  537. },
  538. expectOutput: wire.SNACMessage{
  539. Frame: wire.SNACFrame{
  540. FoodGroup: wire.BART,
  541. SubGroup: wire.BARTDownloadReply,
  542. RequestID: 1234,
  543. },
  544. Body: wire.SNAC_0x10_0x05_BARTDownloadReply{
  545. ScreenName: "user_screen_name",
  546. BARTID: wire.BARTID{
  547. Type: wire.BARTTypesBuddyIcon,
  548. BARTInfo: wire.BARTInfo{
  549. Flags: wire.BARTFlagsKnown,
  550. Hash: wire.GetClearIconHash(),
  551. },
  552. },
  553. Data: blankGIF,
  554. },
  555. },
  556. },
  557. {
  558. name: "retrieve item with error",
  559. instance: newTestInstance("user_screen_name"),
  560. inputSNAC: wire.SNACMessage{
  561. Frame: wire.SNACFrame{
  562. RequestID: 1234,
  563. },
  564. Body: wire.SNAC_0x10_0x04_BARTDownloadQuery{
  565. ScreenName: "user_screen_name",
  566. Command: 1,
  567. BARTID: wire.BARTID{
  568. Type: wire.BARTTypesBuddyIcon,
  569. BARTInfo: wire.BARTInfo{
  570. Flags: wire.BARTFlagsKnown,
  571. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  572. },
  573. },
  574. },
  575. },
  576. mockParams: mockParams{
  577. bartItemManagerParams: bartItemManagerParams{
  578. bartItemManagerRetrieveParams: bartItemManagerRetrieveParams{
  579. {
  580. itemHash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  581. result: nil,
  582. err: assert.AnError,
  583. },
  584. },
  585. },
  586. },
  587. expectOutput: wire.SNACMessage{},
  588. expectErr: assert.AnError,
  589. },
  590. }
  591. for _, tc := range cases {
  592. t.Run(tc.name, func(t *testing.T) {
  593. bartItemManager := newMockBARTItemManager(t)
  594. for _, params := range tc.mockParams.bartItemManagerRetrieveParams {
  595. bartItemManager.EXPECT().
  596. BARTItem(matchContext(), params.itemHash).
  597. Return(params.result, params.err)
  598. }
  599. svc := NewBARTService(slog.Default(), bartItemManager, nil, nil, nil)
  600. output, err := svc.RetrieveItem(context.Background(), tc.inputSNAC.Frame, tc.inputSNAC.Body.(wire.SNAC_0x10_0x04_BARTDownloadQuery))
  601. assert.ErrorIs(t, err, tc.expectErr)
  602. assert.Equal(t, output, tc.expectOutput)
  603. })
  604. }
  605. }
  606. func TestBARTService_RetrieveItemV2(t *testing.T) {
  607. cases := []struct {
  608. // name is the unit test name
  609. name string
  610. // instance is the session of the user adding to feedbag
  611. instance *state.SessionInstance
  612. // inputSNAC is the SNAC sent from the client to the server
  613. inputSNAC wire.SNACMessage
  614. // mockParams is the list of params sent to mocks that satisfy this
  615. // method's dependencies
  616. mockParams mockParams
  617. // expectOutput is the SNAC sent from the server to client
  618. expectOutput []wire.SNACMessage
  619. // expectErr is the expected error
  620. expectErr error
  621. }{
  622. {
  623. name: "retrieve single buddy icon",
  624. instance: newTestInstance("user_screen_name"),
  625. inputSNAC: wire.SNACMessage{
  626. Frame: wire.SNACFrame{
  627. RequestID: 1234,
  628. },
  629. Body: wire.SNAC_0x10_0x06_BARTDownload2Query{
  630. ScreenName: "user_screen_name",
  631. IDs: []wire.BARTID{
  632. {
  633. Type: wire.BARTTypesBuddyIcon,
  634. BARTInfo: wire.BARTInfo{
  635. Flags: wire.BARTFlagsKnown,
  636. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  637. },
  638. },
  639. },
  640. },
  641. },
  642. mockParams: mockParams{
  643. bartItemManagerParams: bartItemManagerParams{
  644. bartItemManagerRetrieveParams: bartItemManagerRetrieveParams{
  645. {
  646. itemHash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  647. result: []byte{'i', 't', 'e', 'm', 'd', 'a', 't', 'a'},
  648. },
  649. },
  650. },
  651. },
  652. expectOutput: []wire.SNACMessage{
  653. {
  654. Frame: wire.SNACFrame{
  655. FoodGroup: wire.BART,
  656. SubGroup: wire.BARTDownload2Reply,
  657. RequestID: 1234,
  658. },
  659. Body: wire.SNAC_0x10_0x07_BARTDownload2Reply{
  660. ScreenName: "user_screen_name",
  661. ReplyID: wire.BartQueryReplyID{
  662. QueryID: wire.BARTID{
  663. Type: wire.BARTTypesBuddyIcon,
  664. BARTInfo: wire.BARTInfo{
  665. Flags: wire.BARTFlagsKnown,
  666. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  667. },
  668. },
  669. Code: 0x00, // found
  670. ReplyID: wire.BARTID{
  671. Type: wire.BARTTypesBuddyIcon,
  672. BARTInfo: wire.BARTInfo{
  673. Flags: wire.BARTFlagsKnown,
  674. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  675. },
  676. },
  677. },
  678. Data: []byte{'i', 't', 'e', 'm', 'd', 'a', 't', 'a'},
  679. },
  680. },
  681. },
  682. },
  683. {
  684. name: "retrieve multiple buddy icons",
  685. instance: newTestInstance("user_screen_name"),
  686. inputSNAC: wire.SNACMessage{
  687. Frame: wire.SNACFrame{
  688. RequestID: 1234,
  689. },
  690. Body: wire.SNAC_0x10_0x06_BARTDownload2Query{
  691. ScreenName: "user_screen_name",
  692. IDs: []wire.BARTID{
  693. {
  694. Type: wire.BARTTypesBuddyIcon,
  695. BARTInfo: wire.BARTInfo{
  696. Flags: wire.BARTFlagsKnown,
  697. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  698. },
  699. },
  700. {
  701. Type: wire.BARTTypesBuddyIcon,
  702. BARTInfo: wire.BARTInfo{
  703. Flags: wire.BARTFlagsKnown,
  704. Hash: []byte{0x5f, 0xea, 0xd2, 0xa7, 0x56, 0xec, 0x6b, 0xfd, 0xec, 0x06, 0xd8, 0xb3, 0x5f, 0x9f, 0xb1, 0xfe},
  705. },
  706. },
  707. },
  708. },
  709. },
  710. mockParams: mockParams{
  711. bartItemManagerParams: bartItemManagerParams{
  712. bartItemManagerRetrieveParams: bartItemManagerRetrieveParams{
  713. {
  714. itemHash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  715. result: []byte{'f', 'i', 'r', 's', 't', 'i', 'c', 'o', 'n'},
  716. },
  717. {
  718. itemHash: []byte{0x5f, 0xea, 0xd2, 0xa7, 0x56, 0xec, 0x6b, 0xfd, 0xec, 0x06, 0xd8, 0xb3, 0x5f, 0x9f, 0xb1, 0xfe},
  719. result: []byte{'s', 'e', 'c', 'o', 'n', 'd', 'i', 'c', 'o', 'n'},
  720. },
  721. },
  722. },
  723. },
  724. expectOutput: []wire.SNACMessage{
  725. {
  726. Frame: wire.SNACFrame{
  727. FoodGroup: wire.BART,
  728. SubGroup: wire.BARTDownload2Reply,
  729. RequestID: 1234,
  730. },
  731. Body: wire.SNAC_0x10_0x07_BARTDownload2Reply{
  732. ScreenName: "user_screen_name",
  733. ReplyID: wire.BartQueryReplyID{
  734. QueryID: wire.BARTID{
  735. Type: wire.BARTTypesBuddyIcon,
  736. BARTInfo: wire.BARTInfo{
  737. Flags: wire.BARTFlagsKnown,
  738. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  739. },
  740. },
  741. Code: 0x00, // found
  742. ReplyID: wire.BARTID{
  743. Type: wire.BARTTypesBuddyIcon,
  744. BARTInfo: wire.BARTInfo{
  745. Flags: wire.BARTFlagsKnown,
  746. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  747. },
  748. },
  749. },
  750. Data: []byte{'f', 'i', 'r', 's', 't', 'i', 'c', 'o', 'n'},
  751. },
  752. },
  753. {
  754. Frame: wire.SNACFrame{
  755. FoodGroup: wire.BART,
  756. SubGroup: wire.BARTDownload2Reply,
  757. RequestID: 1234,
  758. },
  759. Body: wire.SNAC_0x10_0x07_BARTDownload2Reply{
  760. ScreenName: "user_screen_name",
  761. ReplyID: wire.BartQueryReplyID{
  762. QueryID: wire.BARTID{
  763. Type: wire.BARTTypesBuddyIcon,
  764. BARTInfo: wire.BARTInfo{
  765. Flags: wire.BARTFlagsKnown,
  766. Hash: []byte{0x5f, 0xea, 0xd2, 0xa7, 0x56, 0xec, 0x6b, 0xfd, 0xec, 0x06, 0xd8, 0xb3, 0x5f, 0x9f, 0xb1, 0xfe},
  767. },
  768. },
  769. Code: 0x00, // found
  770. ReplyID: wire.BARTID{
  771. Type: wire.BARTTypesBuddyIcon,
  772. BARTInfo: wire.BARTInfo{
  773. Flags: wire.BARTFlagsKnown,
  774. Hash: []byte{0x5f, 0xea, 0xd2, 0xa7, 0x56, 0xec, 0x6b, 0xfd, 0xec, 0x06, 0xd8, 0xb3, 0x5f, 0x9f, 0xb1, 0xfe},
  775. },
  776. },
  777. },
  778. Data: []byte{'s', 'e', 'c', 'o', 'n', 'd', 'i', 'c', 'o', 'n'},
  779. },
  780. },
  781. },
  782. },
  783. {
  784. name: "retrieve single item with error",
  785. instance: newTestInstance("user_screen_name"),
  786. inputSNAC: wire.SNACMessage{
  787. Frame: wire.SNACFrame{
  788. RequestID: 1234,
  789. },
  790. Body: wire.SNAC_0x10_0x06_BARTDownload2Query{
  791. ScreenName: "user_screen_name",
  792. IDs: []wire.BARTID{
  793. {
  794. Type: wire.BARTTypesBuddyIcon,
  795. BARTInfo: wire.BARTInfo{
  796. Flags: wire.BARTFlagsKnown,
  797. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  798. },
  799. },
  800. },
  801. },
  802. },
  803. mockParams: mockParams{
  804. bartItemManagerParams: bartItemManagerParams{
  805. bartItemManagerRetrieveParams: bartItemManagerRetrieveParams{
  806. {
  807. itemHash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  808. result: nil,
  809. err: assert.AnError,
  810. },
  811. },
  812. },
  813. },
  814. expectOutput: nil,
  815. expectErr: assert.AnError,
  816. },
  817. {
  818. name: "retrieve multiple items with error on second item",
  819. instance: newTestInstance("user_screen_name"),
  820. inputSNAC: wire.SNACMessage{
  821. Frame: wire.SNACFrame{
  822. RequestID: 1234,
  823. },
  824. Body: wire.SNAC_0x10_0x06_BARTDownload2Query{
  825. ScreenName: "user_screen_name",
  826. IDs: []wire.BARTID{
  827. {
  828. Type: wire.BARTTypesBuddyIcon,
  829. BARTInfo: wire.BARTInfo{
  830. Flags: wire.BARTFlagsKnown,
  831. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  832. },
  833. },
  834. {
  835. Type: wire.BARTTypesBuddyIcon,
  836. BARTInfo: wire.BARTInfo{
  837. Flags: wire.BARTFlagsKnown,
  838. Hash: []byte{0x5f, 0xea, 0xd2, 0xa7, 0x56, 0xec, 0x6b, 0xfd, 0xec, 0x06, 0xd8, 0xb3, 0x5f, 0x9f, 0xb1, 0xfe},
  839. },
  840. },
  841. },
  842. },
  843. },
  844. mockParams: mockParams{
  845. bartItemManagerParams: bartItemManagerParams{
  846. bartItemManagerRetrieveParams: bartItemManagerRetrieveParams{
  847. {
  848. itemHash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  849. result: []byte{'f', 'i', 'r', 's', 't', 'i', 'c', 'o', 'n'},
  850. err: nil,
  851. },
  852. {
  853. itemHash: []byte{0x5f, 0xea, 0xd2, 0xa7, 0x56, 0xec, 0x6b, 0xfd, 0xec, 0x06, 0xd8, 0xb3, 0x5f, 0x9f, 0xb1, 0xfe},
  854. result: nil,
  855. err: assert.AnError,
  856. },
  857. },
  858. },
  859. },
  860. expectOutput: nil,
  861. expectErr: assert.AnError,
  862. },
  863. {
  864. name: "retrieve mixed items (regular icon and clear icon)",
  865. instance: newTestInstance("user_screen_name"),
  866. inputSNAC: wire.SNACMessage{
  867. Frame: wire.SNACFrame{
  868. RequestID: 1234,
  869. },
  870. Body: wire.SNAC_0x10_0x06_BARTDownload2Query{
  871. ScreenName: "user_screen_name",
  872. IDs: []wire.BARTID{
  873. {
  874. Type: wire.BARTTypesBuddyIcon,
  875. BARTInfo: wire.BARTInfo{
  876. Flags: wire.BARTFlagsKnown,
  877. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  878. },
  879. },
  880. {
  881. Type: wire.BARTTypesBuddyIcon,
  882. BARTInfo: wire.BARTInfo{
  883. Flags: wire.BARTFlagsKnown,
  884. Hash: wire.GetClearIconHash(),
  885. },
  886. },
  887. },
  888. },
  889. },
  890. mockParams: mockParams{
  891. bartItemManagerParams: bartItemManagerParams{
  892. bartItemManagerRetrieveParams: bartItemManagerRetrieveParams{
  893. {
  894. itemHash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  895. result: []byte{'r', 'e', 'g', 'u', 'l', 'a', 'r', 'i', 'c', 'o', 'n'},
  896. },
  897. },
  898. },
  899. },
  900. expectOutput: []wire.SNACMessage{
  901. {
  902. Frame: wire.SNACFrame{
  903. FoodGroup: wire.BART,
  904. SubGroup: wire.BARTDownload2Reply,
  905. RequestID: 1234,
  906. },
  907. Body: wire.SNAC_0x10_0x07_BARTDownload2Reply{
  908. ScreenName: "user_screen_name",
  909. ReplyID: wire.BartQueryReplyID{
  910. QueryID: wire.BARTID{
  911. Type: wire.BARTTypesBuddyIcon,
  912. BARTInfo: wire.BARTInfo{
  913. Flags: wire.BARTFlagsKnown,
  914. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  915. },
  916. },
  917. Code: 0x00, // found
  918. ReplyID: wire.BARTID{
  919. Type: wire.BARTTypesBuddyIcon,
  920. BARTInfo: wire.BARTInfo{
  921. Flags: wire.BARTFlagsKnown,
  922. Hash: []byte{0x4e, 0xd9, 0xc1, 0x96, 0x45, 0xdb, 0x5a, 0xec, 0xdb, 0xf5, 0xc7, 0xa2, 0x4e, 0x8e, 0xa0, 0xed},
  923. },
  924. },
  925. },
  926. Data: []byte{'r', 'e', 'g', 'u', 'l', 'a', 'r', 'i', 'c', 'o', 'n'},
  927. },
  928. },
  929. {
  930. Frame: wire.SNACFrame{
  931. FoodGroup: wire.BART,
  932. SubGroup: wire.BARTDownload2Reply,
  933. RequestID: 1234,
  934. },
  935. Body: wire.SNAC_0x10_0x07_BARTDownload2Reply{
  936. ScreenName: "user_screen_name",
  937. ReplyID: wire.BartQueryReplyID{
  938. QueryID: wire.BARTID{
  939. Type: wire.BARTTypesBuddyIcon,
  940. BARTInfo: wire.BARTInfo{
  941. Flags: wire.BARTFlagsKnown,
  942. Hash: wire.GetClearIconHash(),
  943. },
  944. },
  945. Code: 0x00, // found
  946. ReplyID: wire.BARTID{
  947. Type: wire.BARTTypesBuddyIcon,
  948. BARTInfo: wire.BARTInfo{
  949. Flags: wire.BARTFlagsKnown,
  950. Hash: wire.GetClearIconHash(),
  951. },
  952. },
  953. },
  954. Data: blankGIF,
  955. },
  956. },
  957. },
  958. },
  959. }
  960. for _, tc := range cases {
  961. t.Run(tc.name, func(t *testing.T) {
  962. bartItemManager := newMockBARTItemManager(t)
  963. for _, params := range tc.mockParams.bartItemManagerRetrieveParams {
  964. bartItemManager.EXPECT().
  965. BARTItem(matchContext(), params.itemHash).
  966. Return(params.result, params.err)
  967. }
  968. svc := NewBARTService(slog.Default(), bartItemManager, nil, nil, nil)
  969. output, err := svc.RetrieveItemV2(context.Background(), tc.inputSNAC.Frame, tc.inputSNAC.Body.(wire.SNAC_0x10_0x06_BARTDownload2Query))
  970. assert.ErrorIs(t, err, tc.expectErr)
  971. assert.Equal(t, output, tc.expectOutput)
  972. })
  973. }
  974. }