cmd_client_test.go 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285
  1. package toc
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "io"
  6. "log/slog"
  7. "testing"
  8. "github.com/google/uuid"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/mock"
  11. "github.com/mk6i/retro-aim-server/state"
  12. "github.com/mk6i/retro-aim-server/wire"
  13. )
  14. func TestOSCARProxy_AddBuddy(t *testing.T) {
  15. cases := []struct {
  16. // name is the unit test name
  17. name string
  18. // me is the TOC user session
  19. me *state.Session
  20. // givenCmd is the TOC command
  21. givenCmd []byte
  22. // wantMsg is the expected TOC response
  23. wantMsg string
  24. // mockParams is the list of params sent to mocks that satisfy this
  25. // method's dependencies
  26. mockParams mockParams
  27. }{
  28. {
  29. name: "successfully add buddies",
  30. me: newTestSession("me"),
  31. givenCmd: []byte("toc_add_buddy friend1 friend2 friend3"),
  32. mockParams: mockParams{
  33. buddyParams: buddyParams{
  34. addBuddiesParams: addBuddiesParams{
  35. {
  36. me: state.NewIdentScreenName("me"),
  37. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  38. Buddies: []struct {
  39. ScreenName string `oscar:"len_prefix=uint8"`
  40. }{
  41. {ScreenName: "friend1"},
  42. {ScreenName: "friend2"},
  43. {ScreenName: "friend3"},
  44. },
  45. },
  46. },
  47. },
  48. },
  49. },
  50. },
  51. {
  52. name: "add buddies, receive error from buddy service",
  53. me: newTestSession("me"),
  54. givenCmd: []byte("toc_add_buddy friend1"),
  55. mockParams: mockParams{
  56. buddyParams: buddyParams{
  57. addBuddiesParams: addBuddiesParams{
  58. {
  59. me: state.NewIdentScreenName("me"),
  60. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  61. Buddies: []struct {
  62. ScreenName string `oscar:"len_prefix=uint8"`
  63. }{
  64. {ScreenName: "friend1"},
  65. },
  66. },
  67. err: io.EOF,
  68. },
  69. },
  70. },
  71. },
  72. wantMsg: cmdInternalSvcErr,
  73. },
  74. {
  75. name: "bad command",
  76. givenCmd: []byte(`toc_add_buddy_bad`),
  77. wantMsg: cmdInternalSvcErr,
  78. },
  79. }
  80. for _, tc := range cases {
  81. t.Run(tc.name, func(t *testing.T) {
  82. ctx := context.Background()
  83. buddySvc := newMockBuddyService(t)
  84. for _, params := range tc.mockParams.addBuddiesParams {
  85. buddySvc.EXPECT().
  86. AddBuddies(ctx, matchSession(params.me), params.inBody).
  87. Return(params.err)
  88. }
  89. svc := OSCARProxy{
  90. Logger: slog.Default(),
  91. BuddyService: buddySvc,
  92. }
  93. msg := svc.AddBuddy(ctx, tc.me, tc.givenCmd)
  94. assert.Equal(t, tc.wantMsg, msg)
  95. })
  96. }
  97. }
  98. func TestOSCARProxy_AddPermit(t *testing.T) {
  99. cases := []struct {
  100. // name is the unit test name
  101. name string
  102. // me is the TOC user session
  103. me *state.Session
  104. // givenCmd is the TOC command
  105. givenCmd []byte
  106. // wantMsg is the expected TOC response
  107. wantMsg string
  108. // mockParams is the list of params sent to mocks that satisfy this
  109. // method's dependencies
  110. mockParams mockParams
  111. }{
  112. {
  113. name: "successfully permit buddies",
  114. me: newTestSession("me"),
  115. givenCmd: []byte("toc_add_permit friend1 friend2 friend3"),
  116. mockParams: mockParams{
  117. permitDenyParams: permitDenyParams{
  118. addPermListEntriesParams: addPermListEntriesParams{
  119. {
  120. me: state.NewIdentScreenName("me"),
  121. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  122. Users: []struct {
  123. ScreenName string `oscar:"len_prefix=uint8"`
  124. }{
  125. {ScreenName: "friend1"},
  126. {ScreenName: "friend2"},
  127. {ScreenName: "friend3"},
  128. },
  129. },
  130. },
  131. },
  132. },
  133. },
  134. },
  135. {
  136. name: "permit buddies, receive error from buddy service",
  137. me: newTestSession("me"),
  138. givenCmd: []byte("toc_add_permit friend1"),
  139. mockParams: mockParams{
  140. permitDenyParams: permitDenyParams{
  141. addPermListEntriesParams: addPermListEntriesParams{
  142. {
  143. me: state.NewIdentScreenName("me"),
  144. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  145. Users: []struct {
  146. ScreenName string `oscar:"len_prefix=uint8"`
  147. }{
  148. {ScreenName: "friend1"},
  149. },
  150. },
  151. err: io.EOF,
  152. },
  153. },
  154. },
  155. },
  156. wantMsg: cmdInternalSvcErr,
  157. },
  158. {
  159. name: "bad command",
  160. givenCmd: []byte(`toc_remove_buddy_bad`),
  161. wantMsg: cmdInternalSvcErr,
  162. },
  163. }
  164. for _, tc := range cases {
  165. t.Run(tc.name, func(t *testing.T) {
  166. ctx := context.Background()
  167. pdSvc := newMockPermitDenyService(t)
  168. for _, params := range tc.mockParams.addPermListEntriesParams {
  169. pdSvc.EXPECT().
  170. AddPermListEntries(ctx, matchSession(params.me), params.body).
  171. Return(params.err)
  172. }
  173. svc := OSCARProxy{
  174. Logger: slog.Default(),
  175. PermitDenyService: pdSvc,
  176. }
  177. msg := svc.AddPermit(ctx, tc.me, tc.givenCmd)
  178. assert.Equal(t, tc.wantMsg, msg)
  179. })
  180. }
  181. }
  182. func TestOSCARProxy_AddDeny(t *testing.T) {
  183. cases := []struct {
  184. // name is the unit test name
  185. name string
  186. // me is the TOC user session
  187. me *state.Session
  188. // givenCmd is the TOC command
  189. givenCmd []byte
  190. // wantMsg is the expected TOC response
  191. wantMsg string
  192. // mockParams is the list of params sent to mocks that satisfy this
  193. // method's dependencies
  194. mockParams mockParams
  195. }{
  196. {
  197. name: "successfully deny buddies",
  198. me: newTestSession("me"),
  199. givenCmd: []byte("toc_add_deny friend1 friend2 friend3"),
  200. mockParams: mockParams{
  201. permitDenyParams: permitDenyParams{
  202. addDenyListEntriesParams: addDenyListEntriesParams{
  203. {
  204. me: state.NewIdentScreenName("me"),
  205. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  206. Users: []struct {
  207. ScreenName string `oscar:"len_prefix=uint8"`
  208. }{
  209. {ScreenName: "friend1"},
  210. {ScreenName: "friend2"},
  211. {ScreenName: "friend3"},
  212. },
  213. },
  214. },
  215. },
  216. },
  217. },
  218. },
  219. {
  220. name: "deny buddies, receive error from buddy service",
  221. me: newTestSession("me"),
  222. givenCmd: []byte("toc_add_deny friend1"),
  223. mockParams: mockParams{
  224. permitDenyParams: permitDenyParams{
  225. addDenyListEntriesParams: addDenyListEntriesParams{
  226. {
  227. me: state.NewIdentScreenName("me"),
  228. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  229. Users: []struct {
  230. ScreenName string `oscar:"len_prefix=uint8"`
  231. }{
  232. {ScreenName: "friend1"},
  233. },
  234. },
  235. err: io.EOF,
  236. },
  237. },
  238. },
  239. },
  240. wantMsg: cmdInternalSvcErr,
  241. },
  242. {
  243. name: "bad command",
  244. givenCmd: []byte(`toc_add_deny_bad`),
  245. wantMsg: cmdInternalSvcErr,
  246. },
  247. }
  248. for _, tc := range cases {
  249. t.Run(tc.name, func(t *testing.T) {
  250. ctx := context.Background()
  251. pdSvc := newMockPermitDenyService(t)
  252. for _, params := range tc.mockParams.addDenyListEntriesParams {
  253. pdSvc.EXPECT().
  254. AddDenyListEntries(ctx, matchSession(params.me), params.body).
  255. Return(params.err)
  256. }
  257. svc := OSCARProxy{
  258. Logger: slog.Default(),
  259. PermitDenyService: pdSvc,
  260. }
  261. msg := svc.AddDeny(ctx, tc.me, tc.givenCmd)
  262. assert.Equal(t, tc.wantMsg, msg)
  263. })
  264. }
  265. }
  266. func TestOSCARProxy_FormatNickname(t *testing.T) {
  267. cases := []struct {
  268. // name is the unit test name
  269. name string
  270. // me is the TOC user session
  271. me *state.Session
  272. // givenCmd is the TOC command
  273. givenCmd []byte
  274. // wantMsg is the expected TOC response
  275. wantMsg string
  276. // mockParams is the list of params sent to mocks that satisfy this
  277. // method's dependencies
  278. mockParams mockParams
  279. }{
  280. {
  281. name: "successfully change screen name format",
  282. me: newTestSession("myScreenName"),
  283. givenCmd: []byte("toc_format_nickname mYsCrEeNnAmE"),
  284. mockParams: mockParams{
  285. adminParams: adminParams{
  286. infoChangeRequestParams: infoChangeRequestParams{
  287. {
  288. me: state.NewIdentScreenName("myScreenName"),
  289. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  290. TLVRestBlock: wire.TLVRestBlock{
  291. TLVList: wire.TLVList{
  292. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "mYsCrEeNnAmE"),
  293. },
  294. },
  295. },
  296. msg: wire.SNACMessage{
  297. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  298. Permissions: wire.AdminInfoPermissionsReadWrite,
  299. TLVBlock: wire.TLVBlock{
  300. TLVList: wire.TLVList{
  301. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "mYsCrEeNnAmE"),
  302. },
  303. },
  304. },
  305. },
  306. },
  307. },
  308. },
  309. },
  310. wantMsg: "ADMIN_NICK_STATUS:0",
  311. },
  312. {
  313. name: "format nickname - invalid length",
  314. me: newTestSession("sn"),
  315. givenCmd: []byte("toc_format_nickname sN"),
  316. mockParams: mockParams{
  317. adminParams: adminParams{
  318. infoChangeRequestParams: infoChangeRequestParams{
  319. {
  320. me: state.NewIdentScreenName("sn"),
  321. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  322. TLVRestBlock: wire.TLVRestBlock{
  323. TLVList: wire.TLVList{
  324. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "sN"),
  325. },
  326. },
  327. },
  328. msg: wire.SNACMessage{
  329. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  330. Permissions: wire.AdminInfoPermissionsReadWrite,
  331. TLVBlock: wire.TLVBlock{
  332. TLVList: wire.TLVList{
  333. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorInvalidNickNameLength),
  334. wire.NewTLVBE(wire.AdminTLVUrl, ""),
  335. },
  336. },
  337. },
  338. },
  339. },
  340. },
  341. },
  342. },
  343. wantMsg: "ERROR:911",
  344. },
  345. {
  346. name: "format nickname - invalid screen name",
  347. me: newTestSession("sn"),
  348. givenCmd: []byte("toc_format_nickname sN"),
  349. mockParams: mockParams{
  350. adminParams: adminParams{
  351. infoChangeRequestParams: infoChangeRequestParams{
  352. {
  353. me: state.NewIdentScreenName("sn"),
  354. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  355. TLVRestBlock: wire.TLVRestBlock{
  356. TLVList: wire.TLVList{
  357. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "sN"),
  358. },
  359. },
  360. },
  361. msg: wire.SNACMessage{
  362. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  363. Permissions: wire.AdminInfoPermissionsReadWrite,
  364. TLVBlock: wire.TLVBlock{
  365. TLVList: wire.TLVList{
  366. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorInvalidNickName),
  367. wire.NewTLVBE(wire.AdminTLVUrl, ""),
  368. },
  369. },
  370. },
  371. },
  372. },
  373. },
  374. },
  375. },
  376. wantMsg: "ERROR:911",
  377. },
  378. {
  379. name: "format nickname - catch-all error",
  380. me: newTestSession("sn"),
  381. givenCmd: []byte("toc_format_nickname sN"),
  382. mockParams: mockParams{
  383. adminParams: adminParams{
  384. infoChangeRequestParams: infoChangeRequestParams{
  385. {
  386. me: state.NewIdentScreenName("sn"),
  387. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  388. TLVRestBlock: wire.TLVRestBlock{
  389. TLVList: wire.TLVList{
  390. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "sN"),
  391. },
  392. },
  393. },
  394. msg: wire.SNACMessage{
  395. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  396. Permissions: wire.AdminInfoPermissionsReadWrite,
  397. TLVBlock: wire.TLVBlock{
  398. TLVList: wire.TLVList{
  399. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorValidateNickName),
  400. wire.NewTLVBE(wire.AdminTLVUrl, ""),
  401. },
  402. },
  403. },
  404. },
  405. },
  406. },
  407. },
  408. },
  409. wantMsg: "ERROR:913",
  410. },
  411. {
  412. name: "format nickname - runtime error from admin svc",
  413. me: newTestSession("myScreenName"),
  414. givenCmd: []byte("toc_format_nickname mYsCrEeNnAmE"),
  415. mockParams: mockParams{
  416. adminParams: adminParams{
  417. infoChangeRequestParams: infoChangeRequestParams{
  418. {
  419. me: state.NewIdentScreenName("myScreenName"),
  420. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  421. TLVRestBlock: wire.TLVRestBlock{
  422. TLVList: wire.TLVList{
  423. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "mYsCrEeNnAmE"),
  424. },
  425. },
  426. },
  427. err: io.EOF,
  428. },
  429. },
  430. },
  431. },
  432. wantMsg: cmdInternalSvcErr,
  433. },
  434. {
  435. name: "change password - unexpected response from admin svc",
  436. me: newTestSession("myScreenName"),
  437. givenCmd: []byte("toc_format_nickname mYsCrEeNnAmE"),
  438. mockParams: mockParams{
  439. adminParams: adminParams{
  440. infoChangeRequestParams: infoChangeRequestParams{
  441. {
  442. me: state.NewIdentScreenName("myScreenName"),
  443. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  444. TLVRestBlock: wire.TLVRestBlock{
  445. TLVList: wire.TLVList{
  446. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "mYsCrEeNnAmE"),
  447. },
  448. },
  449. },
  450. msg: wire.SNACMessage{
  451. Body: wire.SNACError{},
  452. },
  453. },
  454. },
  455. },
  456. },
  457. wantMsg: cmdInternalSvcErr,
  458. },
  459. {
  460. name: "bad command",
  461. givenCmd: []byte(`toc_format_nickname`),
  462. wantMsg: cmdInternalSvcErr,
  463. },
  464. }
  465. for _, tc := range cases {
  466. t.Run(tc.name, func(t *testing.T) {
  467. ctx := context.Background()
  468. adminSvc := newMockAdminService(t)
  469. for _, params := range tc.mockParams.infoChangeRequestParams {
  470. adminSvc.EXPECT().
  471. InfoChangeRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  472. Return(params.msg, params.err)
  473. }
  474. svc := OSCARProxy{
  475. Logger: slog.Default(),
  476. AdminService: adminSvc,
  477. }
  478. msg := svc.FormatNickname(ctx, tc.me, tc.givenCmd)
  479. assert.Equal(t, tc.wantMsg, msg)
  480. })
  481. }
  482. }
  483. func TestOSCARProxy_ChatAccept(t *testing.T) {
  484. fnNewChatNavParams := func(err error) chatNavParams {
  485. ret := chatNavParams{
  486. requestRoomInfoParams: requestRoomInfoParams{
  487. {
  488. inBody: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  489. Cookie: "the-cookie",
  490. Exchange: 4,
  491. InstanceNumber: 0,
  492. },
  493. },
  494. },
  495. }
  496. if err != nil {
  497. ret.requestRoomInfoParams[0].err = err
  498. } else {
  499. ret.requestRoomInfoParams[0].msg = wire.SNACMessage{
  500. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  501. TLVRestBlock: wire.TLVRestBlock{
  502. TLVList: wire.TLVList{
  503. wire.NewTLVBE(wire.ChatNavTLVRoomInfo, wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  504. Cookie: "the-cookie",
  505. TLVBlock: wire.TLVBlock{
  506. TLVList: wire.TLVList{
  507. wire.NewTLVBE(wire.ChatRoomTLVRoomName, "cool room"),
  508. },
  509. },
  510. }),
  511. },
  512. },
  513. },
  514. }
  515. }
  516. return ret
  517. }
  518. fnNewOServiceBOSParams := func(err error) oServiceParams {
  519. ret := oServiceParams{
  520. serviceRequestParams: serviceRequestParams{
  521. {
  522. me: state.NewIdentScreenName("me"),
  523. bodyIn: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  524. FoodGroup: wire.Chat,
  525. TLVRestBlock: wire.TLVRestBlock{
  526. TLVList: wire.TLVList{
  527. wire.NewTLVBE(0x01, wire.SNAC_0x01_0x04_TLVRoomInfo{
  528. Cookie: "the-cookie",
  529. }),
  530. },
  531. },
  532. },
  533. },
  534. },
  535. }
  536. if err != nil {
  537. ret.serviceRequestParams[0].err = err
  538. } else {
  539. ret.serviceRequestParams[0].msg = wire.SNACMessage{
  540. Body: wire.SNAC_0x01_0x05_OServiceServiceResponse{
  541. TLVRestBlock: wire.TLVRestBlock{
  542. TLVList: wire.TLVList{
  543. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, "chat-auth-cookie"),
  544. },
  545. },
  546. },
  547. }
  548. }
  549. return ret
  550. }
  551. fnNewAuthParams := func(err error) authParams {
  552. ret := authParams{
  553. registerChatSessionParams: registerChatSessionParams{
  554. {
  555. authCookie: []byte("chat-auth-cookie"),
  556. },
  557. },
  558. }
  559. if err != nil {
  560. ret.registerChatSessionParams[0].err = err
  561. } else {
  562. ret.registerChatSessionParams[0].sess = newTestSession("me-chat")
  563. }
  564. return ret
  565. }
  566. fnNewOServiceChatParams := func(err error) oServiceParams {
  567. ret := oServiceParams{
  568. clientOnlineParams: clientOnlineParams{
  569. {
  570. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  571. me: state.NewIdentScreenName("me-chat"),
  572. },
  573. },
  574. }
  575. if err != nil {
  576. ret.clientOnlineParams[0].err = err
  577. }
  578. return ret
  579. }
  580. cases := []struct {
  581. // name is the unit test name
  582. name string
  583. // me is the TOC user session
  584. me *state.Session
  585. // givenCmd is the TOC command
  586. givenCmd []byte
  587. // givenChatRegistry is the chat registry passed to the function
  588. givenChatRegistry *ChatRegistry
  589. // wantMsg is the expected TOC response
  590. wantMsg string
  591. // wantChatID is the expected chat ID
  592. wantChatID int
  593. // mockParams is the list of params sent to mocks that satisfy this
  594. // method's dependencies
  595. mockParams mockParams
  596. }{
  597. {
  598. name: "successfully accept chat",
  599. me: newTestSession("me"),
  600. givenCmd: []byte(`toc_chat_accept 0`),
  601. givenChatRegistry: func() *ChatRegistry {
  602. reg := NewChatRegistry()
  603. reg.Add(wire.ICBMRoomInfo{
  604. Cookie: "the-cookie",
  605. Exchange: 4,
  606. Instance: 0,
  607. })
  608. return reg
  609. }(),
  610. mockParams: mockParams{
  611. chatNavParams: fnNewChatNavParams(nil),
  612. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  613. authParams: fnNewAuthParams(nil),
  614. oServiceChatParams: fnNewOServiceChatParams(nil),
  615. },
  616. wantMsg: "CHAT_JOIN:0:cool room",
  617. },
  618. {
  619. name: "accept chat, receive error from chat oservice svc",
  620. me: newTestSession("me"),
  621. givenCmd: []byte(`toc_chat_accept 0`),
  622. givenChatRegistry: func() *ChatRegistry {
  623. reg := NewChatRegistry()
  624. reg.Add(wire.ICBMRoomInfo{
  625. Cookie: "the-cookie",
  626. Exchange: 4,
  627. Instance: 0,
  628. })
  629. return reg
  630. }(),
  631. mockParams: mockParams{
  632. chatNavParams: fnNewChatNavParams(nil),
  633. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  634. authParams: fnNewAuthParams(nil),
  635. oServiceChatParams: fnNewOServiceChatParams(io.EOF),
  636. },
  637. wantMsg: cmdInternalSvcErr,
  638. },
  639. {
  640. name: "accept chat, receive error from auth svc",
  641. me: newTestSession("me"),
  642. givenCmd: []byte(`toc_chat_accept 0`),
  643. givenChatRegistry: func() *ChatRegistry {
  644. reg := NewChatRegistry()
  645. reg.Add(wire.ICBMRoomInfo{
  646. Cookie: "the-cookie",
  647. Exchange: 4,
  648. Instance: 0,
  649. })
  650. return reg
  651. }(),
  652. mockParams: mockParams{
  653. chatNavParams: fnNewChatNavParams(nil),
  654. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  655. authParams: fnNewAuthParams(io.EOF),
  656. },
  657. wantMsg: cmdInternalSvcErr,
  658. },
  659. {
  660. name: "accept chat, receive error from BOS oservice svc",
  661. me: newTestSession("me"),
  662. givenCmd: []byte(`toc_chat_accept 0`),
  663. givenChatRegistry: func() *ChatRegistry {
  664. reg := NewChatRegistry()
  665. reg.Add(wire.ICBMRoomInfo{
  666. Cookie: "the-cookie",
  667. Exchange: 4,
  668. Instance: 0,
  669. })
  670. return reg
  671. }(),
  672. mockParams: mockParams{
  673. chatNavParams: fnNewChatNavParams(nil),
  674. oServiceBOSParams: fnNewOServiceBOSParams(io.EOF),
  675. },
  676. wantMsg: cmdInternalSvcErr,
  677. },
  678. {
  679. name: "accept chat, receive error from chat nav svc",
  680. me: newTestSession("me"),
  681. givenCmd: []byte(`toc_chat_accept 0`),
  682. givenChatRegistry: func() *ChatRegistry {
  683. reg := NewChatRegistry()
  684. reg.Add(wire.ICBMRoomInfo{
  685. Cookie: "the-cookie",
  686. Exchange: 4,
  687. Instance: 0,
  688. })
  689. return reg
  690. }(),
  691. mockParams: mockParams{
  692. chatNavParams: fnNewChatNavParams(io.EOF),
  693. },
  694. wantMsg: cmdInternalSvcErr,
  695. },
  696. {
  697. name: "chat doesn't exist",
  698. givenCmd: []byte(`toc_chat_accept 0`),
  699. givenChatRegistry: NewChatRegistry(),
  700. wantMsg: cmdInternalSvcErr,
  701. },
  702. {
  703. name: "bad command",
  704. givenCmd: []byte(`toc_chat_accept`),
  705. wantMsg: cmdInternalSvcErr,
  706. },
  707. {
  708. name: "bad exchange number",
  709. givenCmd: []byte(`toc_chat_accept four`),
  710. wantMsg: cmdInternalSvcErr,
  711. },
  712. }
  713. for _, tc := range cases {
  714. t.Run(tc.name, func(t *testing.T) {
  715. ctx := context.Background()
  716. chatNavSvc := newMockChatNavService(t)
  717. for _, params := range tc.mockParams.requestRoomInfoParams {
  718. chatNavSvc.EXPECT().
  719. RequestRoomInfo(ctx, wire.SNACFrame{}, params.inBody).
  720. Return(params.msg, params.err)
  721. }
  722. bosOServiceSvc := newMockOServiceService(t)
  723. for _, params := range tc.mockParams.oServiceBOSParams.serviceRequestParams {
  724. bosOServiceSvc.EXPECT().
  725. ServiceRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.bodyIn).
  726. Return(params.msg, params.err)
  727. }
  728. chatOServiceSvc := newMockOServiceService(t)
  729. for _, params := range tc.mockParams.oServiceChatParams.clientOnlineParams {
  730. chatOServiceSvc.EXPECT().
  731. ClientOnline(ctx, params.body, matchSession(params.me)).
  732. Return(params.err)
  733. }
  734. authSvc := newMockAuthService(t)
  735. for _, params := range tc.mockParams.authParams.registerChatSessionParams {
  736. authSvc.EXPECT().
  737. RegisterChatSession(ctx, params.authCookie).
  738. Return(params.sess, params.err)
  739. }
  740. svc := OSCARProxy{
  741. AuthService: authSvc,
  742. ChatNavService: chatNavSvc,
  743. Logger: slog.Default(),
  744. OServiceServiceBOS: bosOServiceSvc,
  745. OServiceServiceChat: chatOServiceSvc,
  746. }
  747. chatID, msg := svc.ChatAccept(ctx, tc.me, tc.givenChatRegistry, tc.givenCmd)
  748. assert.Equal(t, tc.wantMsg, msg)
  749. assert.Equal(t, tc.wantChatID, chatID)
  750. })
  751. }
  752. }
  753. func TestOSCARProxy_ChatInvite(t *testing.T) {
  754. cases := []struct {
  755. // name is the unit test name
  756. name string
  757. // me is the TOC user session
  758. me *state.Session
  759. // givenCmd is the TOC command
  760. givenCmd []byte
  761. // givenChatRegistry is the chat registry passed to the function
  762. givenChatRegistry *ChatRegistry
  763. // wantMsg is the expected TOC response
  764. wantMsg string
  765. // mockParams is the list of params sent to mocks that satisfy this
  766. // method's dependencies
  767. mockParams mockParams
  768. }{
  769. {
  770. name: "successfully send chat invitation",
  771. me: newTestSession("me"),
  772. givenCmd: []byte(`toc_chat_invite 0 "join my chat!" friend1`),
  773. givenChatRegistry: func() *ChatRegistry {
  774. reg := NewChatRegistry()
  775. reg.Add(wire.ICBMRoomInfo{
  776. Exchange: 4,
  777. Cookie: "the-cookie",
  778. Instance: 0,
  779. })
  780. return reg
  781. }(),
  782. mockParams: mockParams{
  783. icbmParams: icbmParams{
  784. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  785. {
  786. sender: state.NewIdentScreenName("me"),
  787. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  788. ChannelID: wire.ICBMChannelRendezvous,
  789. ScreenName: "friend1",
  790. TLVRestBlock: wire.TLVRestBlock{
  791. TLVList: wire.TLVList{
  792. wire.NewTLVBE(0x05, wire.ICBMCh2Fragment{
  793. Type: 0,
  794. Capability: capChat,
  795. TLVRestBlock: wire.TLVRestBlock{
  796. TLVList: wire.TLVList{
  797. wire.NewTLVBE(10, uint16(1)),
  798. wire.NewTLVBE(12, "join my chat!"),
  799. wire.NewTLVBE(13, "us-ascii"),
  800. wire.NewTLVBE(14, "en"),
  801. wire.NewTLVBE(10001, wire.ICBMRoomInfo{
  802. Exchange: 4,
  803. Cookie: "the-cookie",
  804. Instance: 0,
  805. }),
  806. },
  807. },
  808. }),
  809. },
  810. },
  811. },
  812. },
  813. },
  814. },
  815. },
  816. },
  817. {
  818. name: "send chat invitation, receive error from ICBM svc",
  819. me: newTestSession("me"),
  820. givenCmd: []byte(`toc_chat_invite 0 "join my chat!" friend1`),
  821. givenChatRegistry: func() *ChatRegistry {
  822. reg := NewChatRegistry()
  823. reg.Add(wire.ICBMRoomInfo{
  824. Exchange: 4,
  825. Cookie: "the-cookie",
  826. Instance: 0,
  827. })
  828. return reg
  829. }(),
  830. mockParams: mockParams{
  831. icbmParams: icbmParams{
  832. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  833. {
  834. sender: state.NewIdentScreenName("me"),
  835. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  836. ChannelID: wire.ICBMChannelRendezvous,
  837. ScreenName: "friend1",
  838. TLVRestBlock: wire.TLVRestBlock{
  839. TLVList: wire.TLVList{
  840. wire.NewTLVBE(0x05, wire.ICBMCh2Fragment{
  841. Type: 0,
  842. Capability: capChat,
  843. TLVRestBlock: wire.TLVRestBlock{
  844. TLVList: wire.TLVList{
  845. wire.NewTLVBE(10, uint16(1)),
  846. wire.NewTLVBE(12, "join my chat!"),
  847. wire.NewTLVBE(13, "us-ascii"),
  848. wire.NewTLVBE(14, "en"),
  849. wire.NewTLVBE(10001, wire.ICBMRoomInfo{
  850. Exchange: 4,
  851. Cookie: "the-cookie",
  852. Instance: 0,
  853. }),
  854. },
  855. },
  856. }),
  857. },
  858. },
  859. },
  860. err: io.EOF,
  861. },
  862. },
  863. },
  864. },
  865. wantMsg: cmdInternalSvcErr,
  866. },
  867. {
  868. name: "send chat invitation to non-existent room",
  869. me: newTestSession("me"),
  870. givenCmd: []byte(`toc_chat_invite 0 "join my chat!" friend1`),
  871. givenChatRegistry: NewChatRegistry(),
  872. wantMsg: cmdInternalSvcErr,
  873. },
  874. {
  875. name: "bad chat room ID",
  876. givenCmd: []byte(`toc_chat_invite zero "join my chat!" friend1`),
  877. wantMsg: cmdInternalSvcErr,
  878. },
  879. {
  880. name: "bad command",
  881. givenCmd: []byte(`toc_chat_invite`),
  882. wantMsg: cmdInternalSvcErr,
  883. },
  884. }
  885. for _, tc := range cases {
  886. t.Run(tc.name, func(t *testing.T) {
  887. ctx := context.Background()
  888. icbmSvc := newMockICBMService(t)
  889. for _, params := range tc.mockParams.channelMsgToHostParamsICBM {
  890. icbmSvc.EXPECT().
  891. ChannelMsgToHost(ctx, matchSession(params.sender), wire.SNACFrame{}, params.inBody).
  892. Return(nil, params.err)
  893. }
  894. svc := OSCARProxy{
  895. Logger: slog.Default(),
  896. ICBMService: icbmSvc,
  897. }
  898. msg := svc.ChatInvite(ctx, tc.me, tc.givenChatRegistry, tc.givenCmd)
  899. assert.Equal(t, tc.wantMsg, msg)
  900. })
  901. }
  902. }
  903. func TestOSCARProxy_ChatJoin(t *testing.T) {
  904. fnNewChatNavParams := func(err error) chatNavParams {
  905. ret := chatNavParams{
  906. createRoomParams: createRoomParams{
  907. {
  908. me: state.NewIdentScreenName("me"),
  909. inBody: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  910. Exchange: 4,
  911. Cookie: "create",
  912. TLVBlock: wire.TLVBlock{
  913. TLVList: wire.TLVList{
  914. wire.NewTLVBE(wire.ChatRoomTLVRoomName, "cool room"),
  915. },
  916. },
  917. },
  918. },
  919. },
  920. }
  921. if err != nil {
  922. ret.createRoomParams[0].err = err
  923. } else {
  924. ret.createRoomParams[0].msg = wire.SNACMessage{
  925. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  926. TLVRestBlock: wire.TLVRestBlock{
  927. TLVList: wire.TLVList{
  928. wire.NewTLVBE(wire.ChatNavTLVRoomInfo, wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  929. Cookie: "the-cookie",
  930. }),
  931. },
  932. },
  933. },
  934. }
  935. }
  936. return ret
  937. }
  938. fnNewOServiceBOSParams := func(err error) oServiceParams {
  939. ret := oServiceParams{
  940. serviceRequestParams: serviceRequestParams{
  941. {
  942. me: state.NewIdentScreenName("me"),
  943. bodyIn: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  944. FoodGroup: wire.Chat,
  945. TLVRestBlock: wire.TLVRestBlock{
  946. TLVList: wire.TLVList{
  947. wire.NewTLVBE(0x01, wire.SNAC_0x01_0x04_TLVRoomInfo{
  948. Cookie: "the-cookie",
  949. }),
  950. },
  951. },
  952. },
  953. },
  954. },
  955. }
  956. if err != nil {
  957. ret.serviceRequestParams[0].err = err
  958. } else {
  959. ret.serviceRequestParams[0].msg = wire.SNACMessage{
  960. Body: wire.SNAC_0x01_0x05_OServiceServiceResponse{
  961. TLVRestBlock: wire.TLVRestBlock{
  962. TLVList: wire.TLVList{
  963. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, "chat-auth-cookie"),
  964. },
  965. },
  966. },
  967. }
  968. }
  969. return ret
  970. }
  971. fnNewAuthParams := func(err error) authParams {
  972. ret := authParams{
  973. registerChatSessionParams: registerChatSessionParams{
  974. {
  975. authCookie: []byte("chat-auth-cookie"),
  976. },
  977. },
  978. }
  979. if err != nil {
  980. ret.registerChatSessionParams[0].err = err
  981. } else {
  982. ret.registerChatSessionParams[0].sess = newTestSession("me-chat")
  983. }
  984. return ret
  985. }
  986. fnNewOServiceChatParams := func(err error) oServiceParams {
  987. ret := oServiceParams{
  988. clientOnlineParams: clientOnlineParams{
  989. {
  990. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  991. me: state.NewIdentScreenName("me-chat"),
  992. },
  993. },
  994. }
  995. if err != nil {
  996. ret.clientOnlineParams[0].err = err
  997. }
  998. return ret
  999. }
  1000. cases := []struct {
  1001. // name is the unit test name
  1002. name string
  1003. // me is the TOC user session
  1004. me *state.Session
  1005. // givenCmd is the TOC command
  1006. givenCmd []byte
  1007. // givenChatRegistry is the chat registry passed to the function
  1008. givenChatRegistry *ChatRegistry
  1009. // wantMsg is the expected TOC response
  1010. wantMsg string
  1011. // wantChatID is the expected chat ID
  1012. wantChatID int
  1013. // mockParams is the list of params sent to mocks that satisfy this
  1014. // method's dependencies
  1015. mockParams mockParams
  1016. }{
  1017. {
  1018. name: "successfully join chat",
  1019. me: newTestSession("me"),
  1020. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  1021. givenChatRegistry: NewChatRegistry(),
  1022. mockParams: mockParams{
  1023. chatNavParams: fnNewChatNavParams(nil),
  1024. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  1025. authParams: fnNewAuthParams(nil),
  1026. oServiceChatParams: fnNewOServiceChatParams(nil),
  1027. },
  1028. wantMsg: "CHAT_JOIN:0:cool room",
  1029. },
  1030. {
  1031. name: "join chat, receive error from chat oservice svc",
  1032. me: newTestSession("me"),
  1033. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  1034. givenChatRegistry: NewChatRegistry(),
  1035. mockParams: mockParams{
  1036. chatNavParams: fnNewChatNavParams(nil),
  1037. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  1038. authParams: fnNewAuthParams(nil),
  1039. oServiceChatParams: fnNewOServiceChatParams(io.EOF),
  1040. },
  1041. wantMsg: cmdInternalSvcErr,
  1042. },
  1043. {
  1044. name: "join chat, receive error from auth svc",
  1045. me: newTestSession("me"),
  1046. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  1047. givenChatRegistry: NewChatRegistry(),
  1048. mockParams: mockParams{
  1049. chatNavParams: fnNewChatNavParams(nil),
  1050. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  1051. authParams: fnNewAuthParams(io.EOF),
  1052. },
  1053. wantMsg: cmdInternalSvcErr,
  1054. },
  1055. {
  1056. name: "join chat, receive error from BOS oservice svc",
  1057. me: newTestSession("me"),
  1058. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  1059. givenChatRegistry: NewChatRegistry(),
  1060. mockParams: mockParams{
  1061. chatNavParams: fnNewChatNavParams(nil),
  1062. oServiceBOSParams: fnNewOServiceBOSParams(io.EOF),
  1063. },
  1064. wantMsg: cmdInternalSvcErr,
  1065. },
  1066. {
  1067. name: "join chat, receive error from chat nav svc",
  1068. me: newTestSession("me"),
  1069. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  1070. givenChatRegistry: NewChatRegistry(),
  1071. mockParams: mockParams{
  1072. chatNavParams: fnNewChatNavParams(io.EOF),
  1073. },
  1074. wantMsg: cmdInternalSvcErr,
  1075. },
  1076. {
  1077. name: "bad command",
  1078. givenCmd: []byte(`toc_chat_join`),
  1079. wantMsg: cmdInternalSvcErr,
  1080. },
  1081. {
  1082. name: "bad exchange number",
  1083. givenCmd: []byte(`toc_chat_join four "cool room"`),
  1084. wantMsg: cmdInternalSvcErr,
  1085. },
  1086. }
  1087. for _, tc := range cases {
  1088. t.Run(tc.name, func(t *testing.T) {
  1089. ctx := context.Background()
  1090. chatNavSvc := newMockChatNavService(t)
  1091. for _, params := range tc.mockParams.createRoomParams {
  1092. chatNavSvc.EXPECT().
  1093. CreateRoom(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  1094. Return(params.msg, params.err)
  1095. }
  1096. bosOServiceSvc := newMockOServiceService(t)
  1097. for _, params := range tc.mockParams.oServiceBOSParams.serviceRequestParams {
  1098. bosOServiceSvc.EXPECT().
  1099. ServiceRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.bodyIn).
  1100. Return(params.msg, params.err)
  1101. }
  1102. chatOServiceSvc := newMockOServiceService(t)
  1103. for _, params := range tc.mockParams.oServiceChatParams.clientOnlineParams {
  1104. chatOServiceSvc.EXPECT().
  1105. ClientOnline(ctx, params.body, matchSession(params.me)).
  1106. Return(params.err)
  1107. }
  1108. authSvc := newMockAuthService(t)
  1109. for _, params := range tc.mockParams.authParams.registerChatSessionParams {
  1110. authSvc.EXPECT().
  1111. RegisterChatSession(ctx, params.authCookie).
  1112. Return(params.sess, params.err)
  1113. }
  1114. svc := OSCARProxy{
  1115. AuthService: authSvc,
  1116. ChatNavService: chatNavSvc,
  1117. Logger: slog.Default(),
  1118. OServiceServiceBOS: bosOServiceSvc,
  1119. OServiceServiceChat: chatOServiceSvc,
  1120. }
  1121. chatID, msg := svc.ChatJoin(ctx, tc.me, tc.givenChatRegistry, tc.givenCmd)
  1122. assert.Equal(t, tc.wantMsg, msg)
  1123. assert.Equal(t, tc.wantChatID, chatID)
  1124. })
  1125. }
  1126. }
  1127. func TestOSCARProxy_ChatLeave(t *testing.T) {
  1128. cases := []struct {
  1129. // name is the unit test name
  1130. name string
  1131. // me is the TOC user session
  1132. me *state.Session
  1133. // givenCmd is the TOC command
  1134. givenCmd []byte
  1135. // givenChatRegistry is the chat registry passed to the function
  1136. givenChatRegistry *ChatRegistry
  1137. // wantMsg is the expected TOC response
  1138. wantMsg string
  1139. // mockParams is the list of params sent to mocks that satisfy this
  1140. // method's dependencies
  1141. mockParams mockParams
  1142. }{
  1143. {
  1144. name: "successfully leave chat",
  1145. me: newTestSession("me"),
  1146. givenCmd: []byte(`toc_chat_leave 0`),
  1147. givenChatRegistry: func() *ChatRegistry {
  1148. reg := NewChatRegistry()
  1149. reg.RegisterSess(0, newTestSession("me"))
  1150. return reg
  1151. }(),
  1152. mockParams: mockParams{
  1153. authParams: authParams{
  1154. signoutChatParams: signoutChatParams{
  1155. {
  1156. me: state.NewIdentScreenName("me"),
  1157. },
  1158. },
  1159. },
  1160. },
  1161. wantMsg: "CHAT_LEFT:0",
  1162. },
  1163. {
  1164. name: "chat room ID with invalid format",
  1165. givenCmd: []byte(`toc_chat_leave zero`),
  1166. wantMsg: cmdInternalSvcErr,
  1167. },
  1168. {
  1169. name: "missing chat session",
  1170. givenCmd: []byte(`toc_chat_leave 0`),
  1171. givenChatRegistry: NewChatRegistry(),
  1172. wantMsg: cmdInternalSvcErr,
  1173. },
  1174. {
  1175. name: "bad command",
  1176. givenCmd: []byte(`toc_chat_leave`),
  1177. wantMsg: cmdInternalSvcErr,
  1178. },
  1179. }
  1180. for _, tc := range cases {
  1181. t.Run(tc.name, func(t *testing.T) {
  1182. ctx := context.Background()
  1183. authSvc := newMockAuthService(t)
  1184. for _, params := range tc.mockParams.signoutChatParams {
  1185. authSvc.EXPECT().SignoutChat(ctx, matchSession(params.me))
  1186. }
  1187. svc := OSCARProxy{
  1188. Logger: slog.Default(),
  1189. AuthService: authSvc,
  1190. }
  1191. msg := svc.ChatLeave(ctx, tc.givenChatRegistry, tc.givenCmd)
  1192. assert.Equal(t, tc.wantMsg, msg)
  1193. })
  1194. }
  1195. }
  1196. func TestOSCARProxy_ChatSend(t *testing.T) {
  1197. cases := []struct {
  1198. // name is the unit test name
  1199. name string
  1200. // me is the TOC user session
  1201. me *state.Session
  1202. // givenCmd is the TOC command
  1203. givenCmd []byte
  1204. // givenChatRegistry is the chat registry passed to the function
  1205. givenChatRegistry *ChatRegistry
  1206. // wantMsg is the expected TOC response
  1207. wantMsg string
  1208. // mockParams is the list of params sent to mocks that satisfy this
  1209. // method's dependencies
  1210. mockParams mockParams
  1211. }{
  1212. {
  1213. name: "successfully send chat message",
  1214. me: newTestSession("me"),
  1215. givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
  1216. givenChatRegistry: func() *ChatRegistry {
  1217. reg := NewChatRegistry()
  1218. reg.RegisterSess(0, newTestSession("me"))
  1219. return reg
  1220. }(),
  1221. mockParams: mockParams{
  1222. chatParams: chatParams{
  1223. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1224. {
  1225. sender: state.NewIdentScreenName("me"),
  1226. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1227. Channel: wire.ICBMChannelMIME,
  1228. TLVRestBlock: wire.TLVRestBlock{
  1229. TLVList: wire.TLVList{
  1230. wire.NewTLVBE(wire.ChatTLVEnableReflectionFlag, uint8(1)),
  1231. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1232. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  1233. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1234. TLVList: wire.TLVList{
  1235. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1236. },
  1237. }),
  1238. },
  1239. },
  1240. },
  1241. result: &wire.SNACMessage{
  1242. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  1243. Channel: wire.ICBMChannelMIME,
  1244. TLVRestBlock: wire.TLVRestBlock{
  1245. TLVList: wire.TLVList{
  1246. wire.NewTLVBE(wire.ChatTLVSenderInformation,
  1247. newTestSession("me").TLVUserInfo()),
  1248. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  1249. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1250. TLVList: wire.TLVList{
  1251. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1252. },
  1253. }),
  1254. },
  1255. },
  1256. },
  1257. },
  1258. },
  1259. },
  1260. },
  1261. },
  1262. wantMsg: "CHAT_IN:0:me:F:Hello world!",
  1263. },
  1264. {
  1265. name: "send chat message, receive error from chat svc",
  1266. me: newTestSession("me"),
  1267. givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
  1268. givenChatRegistry: func() *ChatRegistry {
  1269. reg := NewChatRegistry()
  1270. reg.RegisterSess(0, newTestSession("me"))
  1271. return reg
  1272. }(),
  1273. mockParams: mockParams{
  1274. chatParams: chatParams{
  1275. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1276. {
  1277. sender: state.NewIdentScreenName("me"),
  1278. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1279. Channel: wire.ICBMChannelMIME,
  1280. TLVRestBlock: wire.TLVRestBlock{
  1281. TLVList: wire.TLVList{
  1282. wire.NewTLVBE(wire.ChatTLVEnableReflectionFlag, uint8(1)),
  1283. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1284. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  1285. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1286. TLVList: wire.TLVList{
  1287. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1288. },
  1289. }),
  1290. },
  1291. },
  1292. },
  1293. err: io.EOF,
  1294. },
  1295. },
  1296. },
  1297. },
  1298. wantMsg: cmdInternalSvcErr,
  1299. },
  1300. {
  1301. name: "send chat message, receive nil response from chat svc",
  1302. me: newTestSession("me"),
  1303. givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
  1304. givenChatRegistry: func() *ChatRegistry {
  1305. reg := NewChatRegistry()
  1306. reg.RegisterSess(0, newTestSession("me"))
  1307. return reg
  1308. }(),
  1309. mockParams: mockParams{
  1310. chatParams: chatParams{
  1311. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1312. {
  1313. sender: state.NewIdentScreenName("me"),
  1314. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1315. Channel: wire.ICBMChannelMIME,
  1316. TLVRestBlock: wire.TLVRestBlock{
  1317. TLVList: wire.TLVList{
  1318. wire.NewTLVBE(wire.ChatTLVEnableReflectionFlag, uint8(1)),
  1319. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1320. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  1321. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1322. TLVList: wire.TLVList{
  1323. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1324. },
  1325. }),
  1326. },
  1327. },
  1328. },
  1329. result: nil,
  1330. },
  1331. },
  1332. },
  1333. },
  1334. wantMsg: cmdInternalSvcErr,
  1335. },
  1336. {
  1337. name: "send chat message, receive unexpected response from chat svc",
  1338. me: newTestSession("me"),
  1339. givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
  1340. givenChatRegistry: func() *ChatRegistry {
  1341. reg := NewChatRegistry()
  1342. reg.RegisterSess(0, newTestSession("me"))
  1343. return reg
  1344. }(),
  1345. mockParams: mockParams{
  1346. chatParams: chatParams{
  1347. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1348. {
  1349. sender: state.NewIdentScreenName("me"),
  1350. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1351. Channel: wire.ICBMChannelMIME,
  1352. TLVRestBlock: wire.TLVRestBlock{
  1353. TLVList: wire.TLVList{
  1354. wire.NewTLVBE(wire.ChatTLVEnableReflectionFlag, uint8(1)),
  1355. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1356. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  1357. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1358. TLVList: wire.TLVList{
  1359. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1360. },
  1361. }),
  1362. },
  1363. },
  1364. },
  1365. result: &wire.SNACMessage{
  1366. Body: wire.SNACError{},
  1367. },
  1368. },
  1369. },
  1370. },
  1371. },
  1372. wantMsg: cmdInternalSvcErr,
  1373. },
  1374. {
  1375. name: "chat room ID with invalid format",
  1376. givenCmd: []byte(`toc_chat_send zero "Hello world!"`),
  1377. wantMsg: cmdInternalSvcErr,
  1378. },
  1379. {
  1380. name: "missing chat session",
  1381. givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
  1382. givenChatRegistry: NewChatRegistry(),
  1383. wantMsg: cmdInternalSvcErr,
  1384. },
  1385. {
  1386. name: "bad command",
  1387. givenCmd: []byte(`toc_chat_send`),
  1388. wantMsg: cmdInternalSvcErr,
  1389. },
  1390. }
  1391. for _, tc := range cases {
  1392. t.Run(tc.name, func(t *testing.T) {
  1393. ctx := context.Background()
  1394. chatSvc := newMockChatService(t)
  1395. for _, params := range tc.mockParams.channelMsgToHostParamsChat {
  1396. chatSvc.EXPECT().
  1397. ChannelMsgToHost(ctx, matchSession(params.sender), wire.SNACFrame{}, params.inBody).
  1398. Return(params.result, params.err)
  1399. }
  1400. svc := OSCARProxy{
  1401. Logger: slog.Default(),
  1402. ChatService: chatSvc,
  1403. }
  1404. msg := svc.ChatSend(ctx, tc.givenChatRegistry, tc.givenCmd)
  1405. assert.Equal(t, tc.wantMsg, msg)
  1406. })
  1407. }
  1408. }
  1409. func TestOSCARProxy_ChatWhisper(t *testing.T) {
  1410. cases := []struct {
  1411. // name is the unit test name
  1412. name string
  1413. // me is the TOC user session
  1414. me *state.Session
  1415. // givenCmd is the TOC command
  1416. givenCmd []byte
  1417. // givenChatRegistry is the chat registry passed to the function
  1418. givenChatRegistry *ChatRegistry
  1419. // wantMsg is the expected TOC response
  1420. wantMsg string
  1421. // mockParams is the list of params sent to mocks that satisfy this
  1422. // method's dependencies
  1423. mockParams mockParams
  1424. }{
  1425. {
  1426. name: "successfully send chat whisper",
  1427. me: newTestSession("me"),
  1428. givenCmd: []byte(`toc_chat_whisper 0 them "Hello world!"`),
  1429. givenChatRegistry: func() *ChatRegistry {
  1430. reg := NewChatRegistry()
  1431. reg.RegisterSess(0, newTestSession("me"))
  1432. return reg
  1433. }(),
  1434. mockParams: mockParams{
  1435. chatParams: chatParams{
  1436. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1437. {
  1438. sender: state.NewIdentScreenName("me"),
  1439. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1440. Channel: wire.ICBMChannelMIME,
  1441. TLVRestBlock: wire.TLVRestBlock{
  1442. TLVList: wire.TLVList{
  1443. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1444. wire.NewTLVBE(wire.ChatTLVWhisperToUser, "them"),
  1445. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1446. TLVList: wire.TLVList{
  1447. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1448. },
  1449. }),
  1450. },
  1451. },
  1452. },
  1453. result: nil,
  1454. },
  1455. },
  1456. },
  1457. },
  1458. wantMsg: "",
  1459. },
  1460. {
  1461. name: "send chat whisper, receive error from chat svc",
  1462. me: newTestSession("me"),
  1463. givenCmd: []byte(`toc_chat_whisper 0 them "Hello world!"`),
  1464. givenChatRegistry: func() *ChatRegistry {
  1465. reg := NewChatRegistry()
  1466. reg.RegisterSess(0, newTestSession("me"))
  1467. return reg
  1468. }(),
  1469. mockParams: mockParams{
  1470. chatParams: chatParams{
  1471. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1472. {
  1473. sender: state.NewIdentScreenName("me"),
  1474. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1475. Channel: wire.ICBMChannelMIME,
  1476. TLVRestBlock: wire.TLVRestBlock{
  1477. TLVList: wire.TLVList{
  1478. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1479. wire.NewTLVBE(wire.ChatTLVWhisperToUser, "them"),
  1480. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1481. TLVList: wire.TLVList{
  1482. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1483. },
  1484. }),
  1485. },
  1486. },
  1487. },
  1488. err: io.EOF,
  1489. },
  1490. },
  1491. },
  1492. },
  1493. wantMsg: cmdInternalSvcErr,
  1494. },
  1495. {
  1496. name: "chat room ID with invalid format",
  1497. givenCmd: []byte(`toc_chat_whisper zero them "Hello world!"`),
  1498. wantMsg: cmdInternalSvcErr,
  1499. },
  1500. {
  1501. name: "missing chat session",
  1502. givenCmd: []byte(`toc_chat_whisper 0 them "Hello world!"`),
  1503. givenChatRegistry: NewChatRegistry(),
  1504. wantMsg: cmdInternalSvcErr,
  1505. },
  1506. {
  1507. name: "bad command",
  1508. givenCmd: []byte(`toc_chat_whisper`),
  1509. wantMsg: cmdInternalSvcErr,
  1510. },
  1511. }
  1512. for _, tc := range cases {
  1513. t.Run(tc.name, func(t *testing.T) {
  1514. ctx := context.Background()
  1515. chatSvc := newMockChatService(t)
  1516. for _, params := range tc.mockParams.channelMsgToHostParamsChat {
  1517. chatSvc.EXPECT().
  1518. ChannelMsgToHost(ctx, matchSession(params.sender), wire.SNACFrame{}, params.inBody).
  1519. Return(params.result, params.err)
  1520. }
  1521. svc := OSCARProxy{
  1522. Logger: slog.Default(),
  1523. ChatService: chatSvc,
  1524. }
  1525. msg := svc.ChatWhisper(ctx, tc.givenChatRegistry, tc.givenCmd)
  1526. assert.Equal(t, tc.wantMsg, msg)
  1527. })
  1528. }
  1529. }
  1530. func TestOSCARProxy_Evil(t *testing.T) {
  1531. cases := []struct {
  1532. // name is the unit test name
  1533. name string
  1534. // me is the TOC user session
  1535. me *state.Session
  1536. // givenCmd is the TOC command
  1537. givenCmd []byte
  1538. // wantMsg is the expected TOC response
  1539. wantMsg string
  1540. // mockParams is the list of params sent to mocks that satisfy this
  1541. // method's dependencies
  1542. mockParams mockParams
  1543. }{
  1544. {
  1545. name: "successfully warn normally",
  1546. me: newTestSession("me"),
  1547. givenCmd: []byte(`toc_evil them norm`),
  1548. mockParams: mockParams{
  1549. icbmParams: icbmParams{
  1550. evilRequestParams: evilRequestParams{
  1551. {
  1552. me: state.NewIdentScreenName("me"),
  1553. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1554. SendAs: 0,
  1555. ScreenName: "them",
  1556. },
  1557. msg: wire.SNACMessage{
  1558. Body: wire.SNAC_0x04_0x09_ICBMEvilReply{},
  1559. },
  1560. },
  1561. },
  1562. },
  1563. },
  1564. },
  1565. {
  1566. name: "successfully warn anonymously",
  1567. me: newTestSession("me"),
  1568. givenCmd: []byte(`toc_evil them anon`),
  1569. mockParams: mockParams{
  1570. icbmParams: icbmParams{
  1571. evilRequestParams: evilRequestParams{
  1572. {
  1573. me: state.NewIdentScreenName("me"),
  1574. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1575. SendAs: 1,
  1576. ScreenName: "them",
  1577. },
  1578. msg: wire.SNACMessage{
  1579. Body: wire.SNAC_0x04_0x09_ICBMEvilReply{},
  1580. },
  1581. },
  1582. },
  1583. },
  1584. },
  1585. },
  1586. {
  1587. name: "warn, receive error from ICBM service",
  1588. me: newTestSession("me"),
  1589. givenCmd: []byte(`toc_evil them anon`),
  1590. mockParams: mockParams{
  1591. icbmParams: icbmParams{
  1592. evilRequestParams: evilRequestParams{
  1593. {
  1594. me: state.NewIdentScreenName("me"),
  1595. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1596. SendAs: 1,
  1597. ScreenName: "them",
  1598. },
  1599. err: io.EOF,
  1600. },
  1601. },
  1602. },
  1603. },
  1604. wantMsg: cmdInternalSvcErr,
  1605. },
  1606. {
  1607. name: "warn, receive snac err",
  1608. me: newTestSession("me"),
  1609. givenCmd: []byte(`toc_evil them anon`),
  1610. mockParams: mockParams{
  1611. icbmParams: icbmParams{
  1612. evilRequestParams: evilRequestParams{
  1613. {
  1614. me: state.NewIdentScreenName("me"),
  1615. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1616. SendAs: 1,
  1617. ScreenName: "them",
  1618. },
  1619. msg: wire.SNACMessage{
  1620. Body: wire.SNACError{},
  1621. },
  1622. },
  1623. },
  1624. },
  1625. },
  1626. },
  1627. {
  1628. name: "warn, ICBM svc returns unexpected snac type",
  1629. me: newTestSession("me"),
  1630. givenCmd: []byte(`toc_evil them anon`),
  1631. mockParams: mockParams{
  1632. icbmParams: icbmParams{
  1633. evilRequestParams: evilRequestParams{
  1634. {
  1635. me: state.NewIdentScreenName("me"),
  1636. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1637. SendAs: 1,
  1638. ScreenName: "them",
  1639. },
  1640. msg: wire.SNACMessage{
  1641. Body: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{},
  1642. },
  1643. },
  1644. },
  1645. },
  1646. },
  1647. wantMsg: cmdInternalSvcErr,
  1648. },
  1649. {
  1650. name: "warn with incorrect type",
  1651. me: newTestSession("me"),
  1652. givenCmd: []byte(`toc_evil them blah`),
  1653. wantMsg: cmdInternalSvcErr,
  1654. },
  1655. {
  1656. name: "bad command",
  1657. givenCmd: []byte(`toc_evil`),
  1658. wantMsg: cmdInternalSvcErr,
  1659. },
  1660. }
  1661. for _, tc := range cases {
  1662. t.Run(tc.name, func(t *testing.T) {
  1663. ctx := context.Background()
  1664. icbmSvc := newMockICBMService(t)
  1665. for _, params := range tc.mockParams.evilRequestParams {
  1666. icbmSvc.EXPECT().
  1667. EvilRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  1668. Return(params.msg, params.err)
  1669. }
  1670. svc := OSCARProxy{
  1671. Logger: slog.Default(),
  1672. ICBMService: icbmSvc,
  1673. }
  1674. msg := svc.Evil(ctx, tc.me, tc.givenCmd)
  1675. assert.Equal(t, tc.wantMsg, msg)
  1676. })
  1677. }
  1678. }
  1679. func TestOSCARProxy_ChangePassword(t *testing.T) {
  1680. cases := []struct {
  1681. // name is the unit test name
  1682. name string
  1683. // me is the TOC user session
  1684. me *state.Session
  1685. // givenCmd is the TOC command
  1686. givenCmd []byte
  1687. // wantMsg is the expected TOC response
  1688. wantMsg string
  1689. // mockParams is the list of params sent to mocks that satisfy this
  1690. // method's dependencies
  1691. mockParams mockParams
  1692. }{
  1693. {
  1694. name: "successfully change password",
  1695. me: newTestSession("me"),
  1696. givenCmd: []byte("toc_change_passwd oldpass newpass"),
  1697. mockParams: mockParams{
  1698. adminParams: adminParams{
  1699. infoChangeRequestParams: infoChangeRequestParams{
  1700. {
  1701. me: state.NewIdentScreenName("me"),
  1702. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1703. TLVRestBlock: wire.TLVRestBlock{
  1704. TLVList: wire.TLVList{
  1705. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1706. wire.NewTLVBE(wire.AdminTLVNewPassword, "newpass"),
  1707. },
  1708. },
  1709. },
  1710. msg: wire.SNACMessage{
  1711. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  1712. Permissions: wire.AdminInfoPermissionsReadWrite,
  1713. TLVBlock: wire.TLVBlock{
  1714. TLVList: wire.TLVList{
  1715. wire.NewTLVBE(wire.AdminTLVNewPassword, []byte{}),
  1716. },
  1717. },
  1718. },
  1719. },
  1720. },
  1721. },
  1722. },
  1723. },
  1724. wantMsg: "ADMIN_PASSWD_STATUS:0",
  1725. },
  1726. {
  1727. name: "change password - invalid password length",
  1728. me: newTestSession("me"),
  1729. givenCmd: []byte("toc_change_passwd oldpass np"),
  1730. mockParams: mockParams{
  1731. adminParams: adminParams{
  1732. infoChangeRequestParams: infoChangeRequestParams{
  1733. {
  1734. me: state.NewIdentScreenName("me"),
  1735. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1736. TLVRestBlock: wire.TLVRestBlock{
  1737. TLVList: wire.TLVList{
  1738. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1739. wire.NewTLVBE(wire.AdminTLVNewPassword, "np"),
  1740. },
  1741. },
  1742. },
  1743. msg: wire.SNACMessage{
  1744. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  1745. Permissions: wire.AdminInfoPermissionsReadWrite,
  1746. TLVBlock: wire.TLVBlock{
  1747. TLVList: wire.TLVList{
  1748. wire.NewTLVBE(wire.AdminTLVNewPassword, []byte{}),
  1749. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorInvalidPasswordLength),
  1750. },
  1751. },
  1752. },
  1753. },
  1754. },
  1755. },
  1756. },
  1757. },
  1758. wantMsg: "ERROR:911",
  1759. },
  1760. {
  1761. name: "change password - incorrect password",
  1762. me: newTestSession("me"),
  1763. givenCmd: []byte("toc_change_passwd oldpass baddpass"),
  1764. mockParams: mockParams{
  1765. adminParams: adminParams{
  1766. infoChangeRequestParams: infoChangeRequestParams{
  1767. {
  1768. me: state.NewIdentScreenName("me"),
  1769. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1770. TLVRestBlock: wire.TLVRestBlock{
  1771. TLVList: wire.TLVList{
  1772. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1773. wire.NewTLVBE(wire.AdminTLVNewPassword, "baddpass"),
  1774. },
  1775. },
  1776. },
  1777. msg: wire.SNACMessage{
  1778. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  1779. Permissions: wire.AdminInfoPermissionsReadWrite,
  1780. TLVBlock: wire.TLVBlock{
  1781. TLVList: wire.TLVList{
  1782. wire.NewTLVBE(wire.AdminTLVNewPassword, []byte{}),
  1783. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorValidatePassword),
  1784. },
  1785. },
  1786. },
  1787. },
  1788. },
  1789. },
  1790. },
  1791. },
  1792. wantMsg: "ERROR:912",
  1793. },
  1794. {
  1795. name: "change password - catch-all error response",
  1796. me: newTestSession("me"),
  1797. givenCmd: []byte("toc_change_passwd oldpass baddpass"),
  1798. mockParams: mockParams{
  1799. adminParams: adminParams{
  1800. infoChangeRequestParams: infoChangeRequestParams{
  1801. {
  1802. me: state.NewIdentScreenName("me"),
  1803. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1804. TLVRestBlock: wire.TLVRestBlock{
  1805. TLVList: wire.TLVList{
  1806. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1807. wire.NewTLVBE(wire.AdminTLVNewPassword, "baddpass"),
  1808. },
  1809. },
  1810. },
  1811. msg: wire.SNACMessage{
  1812. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  1813. Permissions: wire.AdminInfoPermissionsReadWrite,
  1814. TLVBlock: wire.TLVBlock{
  1815. TLVList: wire.TLVList{
  1816. wire.NewTLVBE(wire.AdminTLVNewPassword, []byte{}),
  1817. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorAllOtherErrors),
  1818. },
  1819. },
  1820. },
  1821. },
  1822. },
  1823. },
  1824. },
  1825. },
  1826. wantMsg: "ERROR:913",
  1827. },
  1828. {
  1829. name: "change password - runtime error from admin svc",
  1830. me: newTestSession("me"),
  1831. givenCmd: []byte("toc_change_passwd oldpass baddpass"),
  1832. mockParams: mockParams{
  1833. adminParams: adminParams{
  1834. infoChangeRequestParams: infoChangeRequestParams{
  1835. {
  1836. me: state.NewIdentScreenName("me"),
  1837. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1838. TLVRestBlock: wire.TLVRestBlock{
  1839. TLVList: wire.TLVList{
  1840. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1841. wire.NewTLVBE(wire.AdminTLVNewPassword, "baddpass"),
  1842. },
  1843. },
  1844. },
  1845. err: io.EOF,
  1846. },
  1847. },
  1848. },
  1849. },
  1850. wantMsg: cmdInternalSvcErr,
  1851. },
  1852. {
  1853. name: "change password - unexpected response from admin svc",
  1854. me: newTestSession("me"),
  1855. givenCmd: []byte("toc_change_passwd oldpass baddpass"),
  1856. mockParams: mockParams{
  1857. adminParams: adminParams{
  1858. infoChangeRequestParams: infoChangeRequestParams{
  1859. {
  1860. me: state.NewIdentScreenName("me"),
  1861. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1862. TLVRestBlock: wire.TLVRestBlock{
  1863. TLVList: wire.TLVList{
  1864. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1865. wire.NewTLVBE(wire.AdminTLVNewPassword, "baddpass"),
  1866. },
  1867. },
  1868. },
  1869. msg: wire.SNACMessage{
  1870. Body: wire.SNACError{},
  1871. },
  1872. },
  1873. },
  1874. },
  1875. },
  1876. wantMsg: cmdInternalSvcErr,
  1877. },
  1878. {
  1879. name: "bad command",
  1880. givenCmd: []byte(`toc_change_passwd`),
  1881. wantMsg: cmdInternalSvcErr,
  1882. },
  1883. }
  1884. for _, tc := range cases {
  1885. t.Run(tc.name, func(t *testing.T) {
  1886. ctx := context.Background()
  1887. adminSvc := newMockAdminService(t)
  1888. for _, params := range tc.mockParams.infoChangeRequestParams {
  1889. adminSvc.EXPECT().
  1890. InfoChangeRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  1891. Return(params.msg, params.err)
  1892. }
  1893. svc := OSCARProxy{
  1894. Logger: slog.Default(),
  1895. AdminService: adminSvc,
  1896. }
  1897. msg := svc.ChangePassword(ctx, tc.me, tc.givenCmd)
  1898. assert.Equal(t, tc.wantMsg, msg)
  1899. })
  1900. }
  1901. }
  1902. func TestOSCARProxy_GetDirSearchURL(t *testing.T) {
  1903. cases := []struct {
  1904. // name is the unit test name
  1905. name string
  1906. // me is the TOC user session
  1907. me *state.Session
  1908. // givenCmd is the TOC command
  1909. givenCmd []byte
  1910. // wantMsg is the expected TOC response
  1911. wantMsg string
  1912. // mockParams is the list of params sent to mocks that satisfy this
  1913. // method's dependencies
  1914. mockParams mockParams
  1915. }{
  1916. {
  1917. name: "successfully request user info",
  1918. me: newTestSession("me"),
  1919. givenCmd: []byte(`toc_dir_search "first name":"middle name":"last name":"maiden name":"city":"state":"country":"email"`),
  1920. mockParams: mockParams{
  1921. cookieBakerParams: cookieBakerParams{
  1922. issueParams: issueParams{
  1923. {
  1924. data: []byte("me"),
  1925. returnData: []byte("monster"),
  1926. },
  1927. },
  1928. },
  1929. },
  1930. wantMsg: "GOTO_URL:search results:dir_search?city=city&cookie=6d6f6e73746572&country=country&email=email&first_name=first+name&last_name=last+name&maiden_name=maiden+name&middle_name=middle+name&state=state",
  1931. },
  1932. {
  1933. name: "successfully request user info by keywords",
  1934. me: newTestSession("me"),
  1935. givenCmd: []byte(`toc_dir_search ::::::::::"searchkw"`),
  1936. mockParams: mockParams{
  1937. cookieBakerParams: cookieBakerParams{
  1938. issueParams: issueParams{
  1939. {
  1940. data: []byte("me"),
  1941. returnData: []byte("monster"),
  1942. },
  1943. },
  1944. },
  1945. },
  1946. wantMsg: "GOTO_URL:search results:dir_search?cookie=6d6f6e73746572&keyword=searchkw",
  1947. },
  1948. {
  1949. name: "request user info with too many params",
  1950. me: newTestSession("me"),
  1951. givenCmd: []byte(`toc_dir_search ::::::::::::::::::::"searchkw"`),
  1952. wantMsg: cmdInternalSvcErr,
  1953. },
  1954. {
  1955. name: "request user info, get cookie issue error",
  1956. me: newTestSession("me"),
  1957. givenCmd: []byte(`toc_dir_search them`),
  1958. mockParams: mockParams{
  1959. cookieBakerParams: cookieBakerParams{
  1960. issueParams: issueParams{
  1961. {
  1962. data: []byte("me"),
  1963. returnErr: io.EOF,
  1964. },
  1965. },
  1966. },
  1967. },
  1968. wantMsg: cmdInternalSvcErr,
  1969. },
  1970. {
  1971. name: "bad command",
  1972. givenCmd: []byte(`toc_dir_search`),
  1973. wantMsg: cmdInternalSvcErr,
  1974. },
  1975. }
  1976. for _, tc := range cases {
  1977. t.Run(tc.name, func(t *testing.T) {
  1978. ctx := context.Background()
  1979. cookieBaker := newMockCookieBaker(t)
  1980. for _, params := range tc.mockParams.issueParams {
  1981. cookieBaker.EXPECT().
  1982. Issue(params.data).
  1983. Return(params.returnData, params.returnErr)
  1984. }
  1985. svc := OSCARProxy{
  1986. Logger: slog.Default(),
  1987. CookieBaker: cookieBaker,
  1988. }
  1989. msg := svc.GetDirSearchURL(ctx, tc.me, tc.givenCmd)
  1990. assert.Equal(t, tc.wantMsg, msg)
  1991. })
  1992. }
  1993. }
  1994. func TestOSCARProxy_GetDirURL(t *testing.T) {
  1995. cases := []struct {
  1996. // name is the unit test name
  1997. name string
  1998. // me is the TOC user session
  1999. me *state.Session
  2000. // givenCmd is the TOC command
  2001. givenCmd []byte
  2002. // wantMsg is the expected TOC response
  2003. wantMsg string
  2004. // mockParams is the list of params sent to mocks that satisfy this
  2005. // method's dependencies
  2006. mockParams mockParams
  2007. }{
  2008. {
  2009. name: "successfully request user dir info",
  2010. me: newTestSession("me"),
  2011. givenCmd: []byte(`toc_get_dir them`),
  2012. mockParams: mockParams{
  2013. cookieBakerParams: cookieBakerParams{
  2014. issueParams: issueParams{
  2015. {
  2016. data: []byte("me"),
  2017. returnData: []byte("monster"),
  2018. },
  2019. },
  2020. },
  2021. },
  2022. wantMsg: "GOTO_URL:directory info:dir_info?cookie=6d6f6e73746572&user=them",
  2023. },
  2024. {
  2025. name: "request user info, get cookie issue error",
  2026. me: newTestSession("me"),
  2027. givenCmd: []byte(`toc_get_dir them`),
  2028. mockParams: mockParams{
  2029. cookieBakerParams: cookieBakerParams{
  2030. issueParams: issueParams{
  2031. {
  2032. data: []byte("me"),
  2033. returnErr: io.EOF,
  2034. },
  2035. },
  2036. },
  2037. },
  2038. wantMsg: cmdInternalSvcErr,
  2039. },
  2040. {
  2041. name: "bad command",
  2042. givenCmd: []byte(`toc_get_dir`),
  2043. wantMsg: cmdInternalSvcErr,
  2044. },
  2045. }
  2046. for _, tc := range cases {
  2047. t.Run(tc.name, func(t *testing.T) {
  2048. ctx := context.Background()
  2049. cookieBaker := newMockCookieBaker(t)
  2050. for _, params := range tc.mockParams.issueParams {
  2051. cookieBaker.EXPECT().
  2052. Issue(params.data).
  2053. Return(params.returnData, params.returnErr)
  2054. }
  2055. svc := OSCARProxy{
  2056. Logger: slog.Default(),
  2057. CookieBaker: cookieBaker,
  2058. }
  2059. msg := svc.GetDirURL(ctx, tc.me, tc.givenCmd)
  2060. assert.Equal(t, tc.wantMsg, msg)
  2061. })
  2062. }
  2063. }
  2064. func TestOSCARProxy_GetInfoURL(t *testing.T) {
  2065. cases := []struct {
  2066. // name is the unit test name
  2067. name string
  2068. // me is the TOC user session
  2069. me *state.Session
  2070. // givenCmd is the TOC command
  2071. givenCmd []byte
  2072. // wantMsg is the expected TOC response
  2073. wantMsg string
  2074. // mockParams is the list of params sent to mocks that satisfy this
  2075. // method's dependencies
  2076. mockParams mockParams
  2077. }{
  2078. {
  2079. name: "successfully request user info",
  2080. me: newTestSession("me"),
  2081. givenCmd: []byte(`toc_get_info them`),
  2082. mockParams: mockParams{
  2083. cookieBakerParams: cookieBakerParams{
  2084. issueParams: issueParams{
  2085. {
  2086. data: []byte("me"),
  2087. returnData: []byte("monster"),
  2088. },
  2089. },
  2090. },
  2091. },
  2092. wantMsg: "GOTO_URL:profile:info?cookie=6d6f6e73746572&from=me&user=them",
  2093. },
  2094. {
  2095. name: "request user info, get cookie issue error",
  2096. me: newTestSession("me"),
  2097. givenCmd: []byte(`toc_get_info them`),
  2098. mockParams: mockParams{
  2099. cookieBakerParams: cookieBakerParams{
  2100. issueParams: issueParams{
  2101. {
  2102. data: []byte("me"),
  2103. returnErr: io.EOF,
  2104. },
  2105. },
  2106. },
  2107. },
  2108. wantMsg: cmdInternalSvcErr,
  2109. },
  2110. {
  2111. name: "bad command",
  2112. givenCmd: []byte(`toc_get_info`),
  2113. wantMsg: cmdInternalSvcErr,
  2114. },
  2115. }
  2116. for _, tc := range cases {
  2117. t.Run(tc.name, func(t *testing.T) {
  2118. ctx := context.Background()
  2119. cookieBaker := newMockCookieBaker(t)
  2120. for _, params := range tc.mockParams.issueParams {
  2121. cookieBaker.EXPECT().
  2122. Issue(params.data).
  2123. Return(params.returnData, params.returnErr)
  2124. }
  2125. svc := OSCARProxy{
  2126. Logger: slog.Default(),
  2127. CookieBaker: cookieBaker,
  2128. }
  2129. msg := svc.GetInfoURL(ctx, tc.me, tc.givenCmd)
  2130. assert.Equal(t, tc.wantMsg, msg)
  2131. })
  2132. }
  2133. }
  2134. func TestOSCARProxy_GetStatus(t *testing.T) {
  2135. cases := []struct {
  2136. // name is the unit test name
  2137. name string
  2138. // me is the TOC user session
  2139. me *state.Session
  2140. // givenCmd is the TOC command
  2141. givenCmd []byte
  2142. // wantMsg is the expected TOC response
  2143. wantMsg string
  2144. // mockParams is the list of params sent to mocks that satisfy this
  2145. // method's dependencies
  2146. mockParams mockParams
  2147. }{
  2148. {
  2149. name: "successfully request status",
  2150. me: newTestSession("me"),
  2151. givenCmd: []byte("toc_get_status them"),
  2152. mockParams: mockParams{
  2153. locateParams: locateParams{
  2154. userInfoQueryParams: userInfoQueryParams{
  2155. {
  2156. me: state.NewIdentScreenName("me"),
  2157. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2158. ScreenName: "them",
  2159. },
  2160. msg: wire.SNACMessage{
  2161. Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  2162. TLVUserInfo: wire.TLVUserInfo{
  2163. ScreenName: "them",
  2164. WarningLevel: 0,
  2165. TLVBlock: wire.TLVBlock{
  2166. TLVList: wire.TLVList{
  2167. wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
  2168. wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
  2169. },
  2170. },
  2171. },
  2172. },
  2173. },
  2174. },
  2175. },
  2176. },
  2177. },
  2178. wantMsg: "UPDATE_BUDDY:them:T:0:1234:5678: O ",
  2179. },
  2180. {
  2181. name: "request status, receive err from locate svc",
  2182. me: newTestSession("me"),
  2183. givenCmd: []byte("toc_get_status them"),
  2184. mockParams: mockParams{
  2185. locateParams: locateParams{
  2186. userInfoQueryParams: userInfoQueryParams{
  2187. {
  2188. me: state.NewIdentScreenName("me"),
  2189. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2190. ScreenName: "them",
  2191. },
  2192. err: io.EOF,
  2193. },
  2194. },
  2195. },
  2196. },
  2197. wantMsg: cmdInternalSvcErr,
  2198. },
  2199. {
  2200. name: "request status, user not online",
  2201. me: newTestSession("me"),
  2202. givenCmd: []byte("toc_get_status them"),
  2203. mockParams: mockParams{
  2204. locateParams: locateParams{
  2205. userInfoQueryParams: userInfoQueryParams{
  2206. {
  2207. me: state.NewIdentScreenName("me"),
  2208. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2209. ScreenName: "them",
  2210. },
  2211. msg: wire.SNACMessage{
  2212. Body: wire.SNACError{
  2213. Code: wire.ErrorCodeNotLoggedOn,
  2214. },
  2215. },
  2216. },
  2217. },
  2218. },
  2219. },
  2220. wantMsg: "ERROR:901:them",
  2221. },
  2222. {
  2223. name: "request status, receive unexpected error code",
  2224. me: newTestSession("me"),
  2225. givenCmd: []byte("toc_get_status them"),
  2226. mockParams: mockParams{
  2227. locateParams: locateParams{
  2228. userInfoQueryParams: userInfoQueryParams{
  2229. {
  2230. me: state.NewIdentScreenName("me"),
  2231. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2232. ScreenName: "them",
  2233. },
  2234. msg: wire.SNACMessage{
  2235. Body: wire.SNACError{
  2236. Code: wire.ErrorCodeInvalidSnac,
  2237. },
  2238. },
  2239. },
  2240. },
  2241. },
  2242. },
  2243. wantMsg: cmdInternalSvcErr,
  2244. },
  2245. {
  2246. name: "request status, unexpected response from locate svc",
  2247. me: newTestSession("me"),
  2248. givenCmd: []byte("toc_get_status them"),
  2249. mockParams: mockParams{
  2250. locateParams: locateParams{
  2251. userInfoQueryParams: userInfoQueryParams{
  2252. {
  2253. me: state.NewIdentScreenName("me"),
  2254. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2255. ScreenName: "them",
  2256. },
  2257. msg: wire.SNACMessage{
  2258. Body: wire.SNAC_0x0E_0x04_ChatUsersLeft{},
  2259. },
  2260. },
  2261. },
  2262. },
  2263. },
  2264. wantMsg: cmdInternalSvcErr,
  2265. },
  2266. {
  2267. name: "bad command",
  2268. givenCmd: []byte(`toc_get_status`),
  2269. wantMsg: cmdInternalSvcErr,
  2270. },
  2271. }
  2272. for _, tc := range cases {
  2273. t.Run(tc.name, func(t *testing.T) {
  2274. ctx := context.Background()
  2275. locateSvc := newMockLocateService(t)
  2276. for _, params := range tc.mockParams.userInfoQueryParams {
  2277. locateSvc.EXPECT().
  2278. UserInfoQuery(mock.Anything, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  2279. Return(params.msg, params.err)
  2280. }
  2281. svc := OSCARProxy{
  2282. Logger: slog.Default(),
  2283. LocateService: locateSvc,
  2284. }
  2285. msg := svc.GetStatus(ctx, tc.me, tc.givenCmd)
  2286. assert.Equal(t, tc.wantMsg, msg)
  2287. })
  2288. }
  2289. }
  2290. func TestOSCARProxy_InitDone(t *testing.T) {
  2291. cases := []struct {
  2292. // name is the unit test name
  2293. name string
  2294. // me is the TOC user session
  2295. me *state.Session
  2296. // givenCmd is the TOC command
  2297. givenCmd []byte
  2298. // wantMsg is the expected TOC response
  2299. wantMsg string
  2300. // mockParams is the list of params sent to mocks that satisfy this
  2301. // method's dependencies
  2302. mockParams mockParams
  2303. }{
  2304. {
  2305. name: "successfully initialize connection",
  2306. me: newTestSession("me"),
  2307. givenCmd: []byte(`toc_init_done`),
  2308. mockParams: mockParams{
  2309. oServiceBOSParams: oServiceParams{
  2310. clientOnlineParams: clientOnlineParams{
  2311. {
  2312. me: state.NewIdentScreenName("me"),
  2313. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  2314. },
  2315. },
  2316. },
  2317. },
  2318. },
  2319. {
  2320. name: "initialize connection, receive err from BOS oservice svc",
  2321. me: newTestSession("me"),
  2322. givenCmd: []byte(`toc_init_done`),
  2323. mockParams: mockParams{
  2324. oServiceBOSParams: oServiceParams{
  2325. clientOnlineParams: clientOnlineParams{
  2326. {
  2327. me: state.NewIdentScreenName("me"),
  2328. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  2329. err: io.EOF,
  2330. },
  2331. },
  2332. },
  2333. },
  2334. wantMsg: cmdInternalSvcErr,
  2335. },
  2336. {
  2337. name: "bad command",
  2338. givenCmd: []byte(`toc_init_done_diff`),
  2339. wantMsg: cmdInternalSvcErr,
  2340. },
  2341. }
  2342. for _, tc := range cases {
  2343. t.Run(tc.name, func(t *testing.T) {
  2344. ctx := context.Background()
  2345. oSvc := newMockOServiceService(t)
  2346. for _, params := range tc.mockParams.oServiceBOSParams.clientOnlineParams {
  2347. oSvc.EXPECT().
  2348. ClientOnline(ctx, params.body, matchSession(params.me)).
  2349. Return(params.err)
  2350. }
  2351. svc := OSCARProxy{
  2352. Logger: slog.Default(),
  2353. OServiceServiceBOS: oSvc,
  2354. }
  2355. msg := svc.InitDone(ctx, tc.me, tc.givenCmd)
  2356. assert.Equal(t, tc.wantMsg, msg)
  2357. })
  2358. }
  2359. }
  2360. func TestOSCARProxy_RemoveBuddy(t *testing.T) {
  2361. cases := []struct {
  2362. // name is the unit test name
  2363. name string
  2364. // me is the TOC user session
  2365. me *state.Session
  2366. // givenCmd is the TOC command
  2367. givenCmd []byte
  2368. // wantMsg is the expected TOC response
  2369. wantMsg string
  2370. // mockParams is the list of params sent to mocks that satisfy this
  2371. // method's dependencies
  2372. mockParams mockParams
  2373. }{
  2374. {
  2375. name: "successfully remove buddies",
  2376. me: newTestSession("me"),
  2377. givenCmd: []byte("toc_remove_buddy friend1 friend2 friend3"),
  2378. mockParams: mockParams{
  2379. buddyParams: buddyParams{
  2380. delBuddiesParams: delBuddiesParams{
  2381. {
  2382. me: state.NewIdentScreenName("me"),
  2383. inBody: wire.SNAC_0x03_0x05_BuddyDelBuddies{
  2384. Buddies: []struct {
  2385. ScreenName string `oscar:"len_prefix=uint8"`
  2386. }{
  2387. {ScreenName: "friend1"},
  2388. {ScreenName: "friend2"},
  2389. {ScreenName: "friend3"},
  2390. },
  2391. },
  2392. },
  2393. },
  2394. },
  2395. },
  2396. },
  2397. {
  2398. name: "remove buddies, receive error from buddy service",
  2399. me: newTestSession("me"),
  2400. givenCmd: []byte("toc_remove_buddy friend1"),
  2401. mockParams: mockParams{
  2402. buddyParams: buddyParams{
  2403. delBuddiesParams: delBuddiesParams{
  2404. {
  2405. me: state.NewIdentScreenName("me"),
  2406. inBody: wire.SNAC_0x03_0x05_BuddyDelBuddies{
  2407. Buddies: []struct {
  2408. ScreenName string `oscar:"len_prefix=uint8"`
  2409. }{
  2410. {ScreenName: "friend1"},
  2411. },
  2412. },
  2413. err: io.EOF,
  2414. },
  2415. },
  2416. },
  2417. },
  2418. wantMsg: cmdInternalSvcErr,
  2419. },
  2420. {
  2421. name: "bad command",
  2422. givenCmd: []byte(`toc_remove_buddy_bad`),
  2423. wantMsg: cmdInternalSvcErr,
  2424. },
  2425. }
  2426. for _, tc := range cases {
  2427. t.Run(tc.name, func(t *testing.T) {
  2428. ctx := context.Background()
  2429. buddySvc := newMockBuddyService(t)
  2430. for _, params := range tc.mockParams.delBuddiesParams {
  2431. buddySvc.EXPECT().
  2432. DelBuddies(ctx, matchSession(params.me), params.inBody).
  2433. Return(params.err)
  2434. }
  2435. svc := OSCARProxy{
  2436. Logger: slog.Default(),
  2437. BuddyService: buddySvc,
  2438. }
  2439. msg := svc.RemoveBuddy(ctx, tc.me, tc.givenCmd)
  2440. assert.Equal(t, tc.wantMsg, msg)
  2441. })
  2442. }
  2443. }
  2444. func TestOSCARProxy_SendIM(t *testing.T) {
  2445. cases := []struct {
  2446. // name is the unit test name
  2447. name string
  2448. // me is the TOC user session
  2449. me *state.Session
  2450. // givenCmd is the TOC command
  2451. givenCmd []byte
  2452. // wantMsg is the expected TOC response
  2453. wantMsg string
  2454. // mockParams is the list of params sent to mocks that satisfy this
  2455. // method's dependencies
  2456. mockParams mockParams
  2457. }{
  2458. {
  2459. name: "successfully send instant message",
  2460. me: newTestSession("me"),
  2461. givenCmd: []byte(`toc_send_im chattingChuck "hello world!"`),
  2462. mockParams: mockParams{
  2463. icbmParams: icbmParams{
  2464. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  2465. {
  2466. sender: state.NewIdentScreenName("me"),
  2467. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2468. ChannelID: wire.ICBMChannelIM,
  2469. ScreenName: "chattingChuck",
  2470. TLVRestBlock: wire.TLVRestBlock{
  2471. TLVList: wire.TLVList{
  2472. wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
  2473. {
  2474. ID: 5,
  2475. Version: 1,
  2476. Payload: []byte{1, 1, 2},
  2477. },
  2478. {
  2479. ID: 1,
  2480. Version: 1,
  2481. Payload: []byte{
  2482. 0x00, 0x00,
  2483. 0x00, 0x00,
  2484. 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
  2485. },
  2486. },
  2487. }),
  2488. },
  2489. },
  2490. },
  2491. },
  2492. },
  2493. },
  2494. },
  2495. },
  2496. {
  2497. name: "successfully auto-reply send instant message",
  2498. me: newTestSession("me"),
  2499. givenCmd: []byte(`toc_send_im chattingChuck "hello world!" auto`),
  2500. mockParams: mockParams{
  2501. icbmParams: icbmParams{
  2502. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  2503. {
  2504. sender: state.NewIdentScreenName("me"),
  2505. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2506. ChannelID: wire.ICBMChannelIM,
  2507. ScreenName: "chattingChuck",
  2508. TLVRestBlock: wire.TLVRestBlock{
  2509. TLVList: wire.TLVList{
  2510. wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
  2511. {
  2512. ID: 5,
  2513. Version: 1,
  2514. Payload: []byte{1, 1, 2},
  2515. },
  2516. {
  2517. ID: 1,
  2518. Version: 1,
  2519. Payload: []byte{
  2520. 0x00, 0x00,
  2521. 0x00, 0x00,
  2522. 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
  2523. },
  2524. },
  2525. }),
  2526. wire.NewTLVBE(wire.ICBMTLVAutoResponse, []byte{}),
  2527. },
  2528. },
  2529. },
  2530. },
  2531. },
  2532. },
  2533. },
  2534. },
  2535. {
  2536. name: "send instant message, receive error from ICBM service",
  2537. me: newTestSession("me"),
  2538. givenCmd: []byte(`toc_send_im chattingChuck "hello world!"`),
  2539. mockParams: mockParams{
  2540. icbmParams: icbmParams{
  2541. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  2542. {
  2543. sender: state.NewIdentScreenName("me"),
  2544. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2545. ChannelID: wire.ICBMChannelIM,
  2546. ScreenName: "chattingChuck",
  2547. TLVRestBlock: wire.TLVRestBlock{
  2548. TLVList: wire.TLVList{
  2549. wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
  2550. {
  2551. ID: 5,
  2552. Version: 1,
  2553. Payload: []byte{1, 1, 2},
  2554. },
  2555. {
  2556. ID: 1,
  2557. Version: 1,
  2558. Payload: []byte{
  2559. 0x00, 0x00,
  2560. 0x00, 0x00,
  2561. 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
  2562. },
  2563. },
  2564. }),
  2565. },
  2566. },
  2567. },
  2568. err: io.EOF,
  2569. },
  2570. },
  2571. },
  2572. },
  2573. wantMsg: cmdInternalSvcErr,
  2574. },
  2575. {
  2576. name: "bad command",
  2577. givenCmd: []byte(`toc_send_im`),
  2578. wantMsg: cmdInternalSvcErr,
  2579. },
  2580. }
  2581. for _, tc := range cases {
  2582. t.Run(tc.name, func(t *testing.T) {
  2583. ctx := context.Background()
  2584. icbmSvc := newMockICBMService(t)
  2585. for _, params := range tc.mockParams.channelMsgToHostParamsICBM {
  2586. icbmSvc.EXPECT().
  2587. ChannelMsgToHost(ctx, matchSession(params.sender), params.inFrame, params.inBody).
  2588. Return(params.result, params.err)
  2589. }
  2590. svc := OSCARProxy{
  2591. Logger: slog.Default(),
  2592. ICBMService: icbmSvc,
  2593. }
  2594. msg := svc.SendIM(ctx, tc.me, tc.givenCmd)
  2595. assert.Equal(t, tc.wantMsg, msg)
  2596. })
  2597. }
  2598. }
  2599. func TestOSCARProxy_SetAway(t *testing.T) {
  2600. cases := []struct {
  2601. // name is the unit test name
  2602. name string
  2603. // me is the TOC user session
  2604. me *state.Session
  2605. // givenCmd is the TOC command
  2606. givenCmd []byte
  2607. // wantMsg is the expected TOC response
  2608. wantMsg string
  2609. // mockParams is the list of params sent to mocks that satisfy this
  2610. // method's dependencies
  2611. mockParams mockParams
  2612. }{
  2613. {
  2614. name: "successfully set away with message",
  2615. me: newTestSession("me"),
  2616. givenCmd: []byte(`toc_set_away "I'm away from my computer right now."`),
  2617. mockParams: mockParams{
  2618. locateParams: locateParams{
  2619. setInfoParams: setInfoParams{
  2620. {
  2621. me: state.NewIdentScreenName("me"),
  2622. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2623. TLVRestBlock: wire.TLVRestBlock{
  2624. TLVList: wire.TLVList{
  2625. wire.NewTLVBE(wire.LocateTLVTagsInfoUnavailableData, "I'm away from my computer right now."),
  2626. },
  2627. },
  2628. },
  2629. },
  2630. },
  2631. },
  2632. },
  2633. },
  2634. {
  2635. name: "successfully set away without message",
  2636. me: newTestSession("me"),
  2637. givenCmd: []byte(`toc_set_away`),
  2638. mockParams: mockParams{
  2639. locateParams: locateParams{
  2640. setInfoParams: setInfoParams{
  2641. {
  2642. me: state.NewIdentScreenName("me"),
  2643. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2644. TLVRestBlock: wire.TLVRestBlock{
  2645. TLVList: wire.TLVList{
  2646. wire.NewTLVBE(wire.LocateTLVTagsInfoUnavailableData, ""),
  2647. },
  2648. },
  2649. },
  2650. },
  2651. },
  2652. },
  2653. },
  2654. },
  2655. {
  2656. name: "set away message, receive error from locate service",
  2657. me: newTestSession("me"),
  2658. givenCmd: []byte(`toc_set_away "I'm away from my computer right now."`),
  2659. mockParams: mockParams{
  2660. locateParams: locateParams{
  2661. setInfoParams: setInfoParams{
  2662. {
  2663. me: state.NewIdentScreenName("me"),
  2664. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2665. TLVRestBlock: wire.TLVRestBlock{
  2666. TLVList: wire.TLVList{
  2667. wire.NewTLVBE(wire.LocateTLVTagsInfoUnavailableData, "I'm away from my computer right now."),
  2668. },
  2669. },
  2670. },
  2671. err: io.EOF,
  2672. },
  2673. },
  2674. },
  2675. },
  2676. wantMsg: cmdInternalSvcErr,
  2677. },
  2678. {
  2679. name: "bad command",
  2680. givenCmd: []byte(`toc_set_away_bad`),
  2681. wantMsg: cmdInternalSvcErr,
  2682. },
  2683. }
  2684. for _, tc := range cases {
  2685. t.Run(tc.name, func(t *testing.T) {
  2686. ctx := context.Background()
  2687. locateSvc := newMockLocateService(t)
  2688. for _, params := range tc.mockParams.setInfoParams {
  2689. locateSvc.EXPECT().
  2690. SetInfo(ctx, matchSession(params.me), params.inBody).
  2691. Return(params.err)
  2692. }
  2693. svc := OSCARProxy{
  2694. Logger: slog.Default(),
  2695. LocateService: locateSvc,
  2696. }
  2697. msg := svc.SetAway(ctx, tc.me, tc.givenCmd)
  2698. assert.Equal(t, tc.wantMsg, msg)
  2699. })
  2700. }
  2701. }
  2702. func TestOSCARProxy_SetCaps(t *testing.T) {
  2703. cases := []struct {
  2704. // name is the unit test name
  2705. name string
  2706. // me is the TOC user session
  2707. me *state.Session
  2708. // givenCmd is the TOC command
  2709. givenCmd []byte
  2710. // wantMsg is the expected TOC response
  2711. wantMsg string
  2712. // mockParams is the list of params sent to mocks that satisfy this
  2713. // method's dependencies
  2714. mockParams mockParams
  2715. }{
  2716. {
  2717. name: "successfully set capabilities",
  2718. me: newTestSession("me"),
  2719. givenCmd: []byte(`toc_set_caps 09460000-4C7F-11D1-8222-444553540000 09460001-4C7F-11D1-8222-444553540000`),
  2720. mockParams: mockParams{
  2721. locateParams: locateParams{
  2722. setInfoParams: setInfoParams{
  2723. {
  2724. me: state.NewIdentScreenName("me"),
  2725. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2726. TLVRestBlock: wire.TLVRestBlock{
  2727. TLVList: wire.TLVList{
  2728. wire.NewTLVBE(wire.LocateTLVTagsInfoCapabilities, []uuid.UUID{
  2729. uuid.MustParse("09460000-4C7F-11D1-8222-444553540000"),
  2730. uuid.MustParse("09460001-4C7F-11D1-8222-444553540000"),
  2731. capChat,
  2732. }),
  2733. },
  2734. },
  2735. },
  2736. },
  2737. },
  2738. },
  2739. },
  2740. },
  2741. {
  2742. name: "set capability, receive error from locate service",
  2743. me: newTestSession("me"),
  2744. givenCmd: []byte(`toc_set_caps 09460000-4C7F-11D1-8222-444553540000`),
  2745. mockParams: mockParams{
  2746. locateParams: locateParams{
  2747. setInfoParams: setInfoParams{
  2748. {
  2749. me: state.NewIdentScreenName("me"),
  2750. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2751. TLVRestBlock: wire.TLVRestBlock{
  2752. TLVList: wire.TLVList{
  2753. wire.NewTLVBE(wire.LocateTLVTagsInfoCapabilities, []uuid.UUID{
  2754. uuid.MustParse("09460000-4C7F-11D1-8222-444553540000"),
  2755. capChat,
  2756. }),
  2757. },
  2758. },
  2759. },
  2760. err: io.EOF,
  2761. },
  2762. },
  2763. },
  2764. },
  2765. wantMsg: cmdInternalSvcErr,
  2766. },
  2767. {
  2768. name: "set malformed capability UUID",
  2769. me: newTestSession("me"),
  2770. givenCmd: []byte(`toc_set_caps 09460000-`),
  2771. wantMsg: cmdInternalSvcErr,
  2772. },
  2773. {
  2774. name: "bad command",
  2775. givenCmd: []byte(`toc_set_caps_bad`),
  2776. wantMsg: cmdInternalSvcErr,
  2777. },
  2778. }
  2779. for _, tc := range cases {
  2780. t.Run(tc.name, func(t *testing.T) {
  2781. ctx := context.Background()
  2782. locateSvc := newMockLocateService(t)
  2783. for _, params := range tc.mockParams.setInfoParams {
  2784. locateSvc.EXPECT().
  2785. SetInfo(ctx, matchSession(params.me), params.inBody).
  2786. Return(params.err)
  2787. }
  2788. svc := OSCARProxy{
  2789. Logger: slog.Default(),
  2790. LocateService: locateSvc,
  2791. }
  2792. msg := svc.SetCaps(ctx, tc.me, tc.givenCmd)
  2793. assert.Equal(t, tc.wantMsg, msg)
  2794. })
  2795. }
  2796. }
  2797. func TestOSCARProxy_SetConfig(t *testing.T) {
  2798. cases := []struct {
  2799. // name is the unit test name
  2800. name string
  2801. // me is the TOC user session
  2802. me *state.Session
  2803. // givenCmd is the TOC command
  2804. givenCmd []byte
  2805. // wantMsg is the expected TOC response
  2806. wantMsg string
  2807. // mockParams is the list of params sent to mocks that satisfy this
  2808. // method's dependencies
  2809. mockParams mockParams
  2810. }{
  2811. {
  2812. name: "successfully set permit all config",
  2813. me: newTestSession("me"),
  2814. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\nb friend2\n}\n"),
  2815. mockParams: mockParams{
  2816. permitDenyParams: permitDenyParams{
  2817. // confusingly, setting "permit all" mode requires adding a
  2818. // deny list entry
  2819. addDenyListEntriesParams: addDenyListEntriesParams{
  2820. {
  2821. me: state.NewIdentScreenName("me"),
  2822. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2823. Users: []struct {
  2824. ScreenName string `oscar:"len_prefix=uint8"`
  2825. }{
  2826. {ScreenName: "me"},
  2827. },
  2828. },
  2829. },
  2830. },
  2831. },
  2832. buddyParams: buddyParams{
  2833. addBuddiesParams: addBuddiesParams{
  2834. {
  2835. me: state.NewIdentScreenName("me"),
  2836. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2837. Buddies: []struct {
  2838. ScreenName string `oscar:"len_prefix=uint8"`
  2839. }{
  2840. {ScreenName: "friend1"},
  2841. {ScreenName: "friend2"},
  2842. },
  2843. },
  2844. },
  2845. },
  2846. },
  2847. tocConfigParams: tocConfigParams{
  2848. setTOCConfigParams: setTOCConfigParams{
  2849. {
  2850. user: state.NewIdentScreenName("me"),
  2851. config: "m 1\ng Buddies\nb friend1\nb friend2",
  2852. },
  2853. },
  2854. },
  2855. },
  2856. },
  2857. {
  2858. name: "set permit all config, receive err from config store svc",
  2859. me: newTestSession("me"),
  2860. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\n}\n"),
  2861. mockParams: mockParams{
  2862. permitDenyParams: permitDenyParams{
  2863. // confusingly, setting "permit all" mode requires adding a
  2864. // deny list entry
  2865. addDenyListEntriesParams: addDenyListEntriesParams{
  2866. {
  2867. me: state.NewIdentScreenName("me"),
  2868. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2869. Users: []struct {
  2870. ScreenName string `oscar:"len_prefix=uint8"`
  2871. }{
  2872. {ScreenName: "me"},
  2873. },
  2874. },
  2875. },
  2876. },
  2877. },
  2878. buddyParams: buddyParams{
  2879. addBuddiesParams: addBuddiesParams{
  2880. {
  2881. me: state.NewIdentScreenName("me"),
  2882. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2883. Buddies: []struct {
  2884. ScreenName string `oscar:"len_prefix=uint8"`
  2885. }{
  2886. {ScreenName: "friend1"},
  2887. },
  2888. },
  2889. },
  2890. },
  2891. },
  2892. tocConfigParams: tocConfigParams{
  2893. setTOCConfigParams: setTOCConfigParams{
  2894. {
  2895. user: state.NewIdentScreenName("me"),
  2896. config: "m 1\ng Buddies\nb friend1",
  2897. err: io.EOF,
  2898. },
  2899. },
  2900. },
  2901. },
  2902. wantMsg: cmdInternalSvcErr,
  2903. },
  2904. {
  2905. name: "set permit all config, receive err from buddy svc",
  2906. me: newTestSession("me"),
  2907. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\n}\n"),
  2908. mockParams: mockParams{
  2909. permitDenyParams: permitDenyParams{
  2910. // confusingly, setting "permit all" mode requires adding a
  2911. // deny list entry
  2912. addDenyListEntriesParams: addDenyListEntriesParams{
  2913. {
  2914. me: state.NewIdentScreenName("me"),
  2915. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2916. Users: []struct {
  2917. ScreenName string `oscar:"len_prefix=uint8"`
  2918. }{
  2919. {ScreenName: "me"},
  2920. },
  2921. },
  2922. },
  2923. },
  2924. },
  2925. buddyParams: buddyParams{
  2926. addBuddiesParams: addBuddiesParams{
  2927. {
  2928. me: state.NewIdentScreenName("me"),
  2929. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2930. Buddies: []struct {
  2931. ScreenName string `oscar:"len_prefix=uint8"`
  2932. }{
  2933. {ScreenName: "friend1"},
  2934. },
  2935. },
  2936. err: io.EOF,
  2937. },
  2938. },
  2939. },
  2940. },
  2941. wantMsg: cmdInternalSvcErr,
  2942. },
  2943. {
  2944. name: "set permit all config, receive err from permit-deny svc",
  2945. me: newTestSession("me"),
  2946. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\nb friend2\n}\n"),
  2947. mockParams: mockParams{
  2948. permitDenyParams: permitDenyParams{
  2949. // confusingly, setting "permit all" mode requires adding a
  2950. // deny list entry
  2951. addDenyListEntriesParams: addDenyListEntriesParams{
  2952. {
  2953. me: state.NewIdentScreenName("me"),
  2954. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2955. Users: []struct {
  2956. ScreenName string `oscar:"len_prefix=uint8"`
  2957. }{
  2958. {ScreenName: "me"},
  2959. },
  2960. },
  2961. err: io.EOF,
  2962. },
  2963. },
  2964. },
  2965. },
  2966. wantMsg: cmdInternalSvcErr,
  2967. },
  2968. {
  2969. name: "successfully set deny all config",
  2970. me: newTestSession("me"),
  2971. givenCmd: []byte("toc_set_config {m 2\ng Buddies\nb friend1\nb friend2\n}\n"),
  2972. mockParams: mockParams{
  2973. permitDenyParams: permitDenyParams{
  2974. // confusingly, setting "deny all" mode requires adding a
  2975. // permit list entry
  2976. addPermListEntriesParams: addPermListEntriesParams{
  2977. {
  2978. me: state.NewIdentScreenName("me"),
  2979. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  2980. Users: []struct {
  2981. ScreenName string `oscar:"len_prefix=uint8"`
  2982. }{
  2983. {ScreenName: "me"},
  2984. },
  2985. },
  2986. },
  2987. },
  2988. },
  2989. buddyParams: buddyParams{
  2990. addBuddiesParams: addBuddiesParams{
  2991. {
  2992. me: state.NewIdentScreenName("me"),
  2993. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2994. Buddies: []struct {
  2995. ScreenName string `oscar:"len_prefix=uint8"`
  2996. }{
  2997. {ScreenName: "friend1"},
  2998. {ScreenName: "friend2"},
  2999. },
  3000. },
  3001. },
  3002. },
  3003. },
  3004. tocConfigParams: tocConfigParams{
  3005. setTOCConfigParams: setTOCConfigParams{
  3006. {
  3007. user: state.NewIdentScreenName("me"),
  3008. config: "m 2\ng Buddies\nb friend1\nb friend2",
  3009. },
  3010. },
  3011. },
  3012. },
  3013. },
  3014. {
  3015. name: "set deny all config, receive err from permit-deny svc",
  3016. me: newTestSession("me"),
  3017. givenCmd: []byte("toc_set_config {m 2\ng Buddies\nb friend1\nb friend2\n}\n"),
  3018. mockParams: mockParams{
  3019. permitDenyParams: permitDenyParams{
  3020. // confusingly, setting "deny all" mode requires adding a
  3021. // permit list entry
  3022. addPermListEntriesParams: addPermListEntriesParams{
  3023. {
  3024. me: state.NewIdentScreenName("me"),
  3025. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  3026. Users: []struct {
  3027. ScreenName string `oscar:"len_prefix=uint8"`
  3028. }{
  3029. {ScreenName: "me"},
  3030. },
  3031. },
  3032. err: io.EOF,
  3033. },
  3034. },
  3035. },
  3036. },
  3037. wantMsg: cmdInternalSvcErr,
  3038. },
  3039. {
  3040. name: "successfully set permit some config",
  3041. me: newTestSession("me"),
  3042. givenCmd: []byte("toc_set_config {m 3\np friend3\np friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  3043. mockParams: mockParams{
  3044. permitDenyParams: permitDenyParams{
  3045. addPermListEntriesParams: addPermListEntriesParams{
  3046. {
  3047. me: state.NewIdentScreenName("me"),
  3048. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  3049. Users: []struct {
  3050. ScreenName string `oscar:"len_prefix=uint8"`
  3051. }{
  3052. {ScreenName: "friend3"},
  3053. {ScreenName: "friend4"},
  3054. },
  3055. },
  3056. },
  3057. },
  3058. },
  3059. buddyParams: buddyParams{
  3060. addBuddiesParams: addBuddiesParams{
  3061. {
  3062. me: state.NewIdentScreenName("me"),
  3063. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  3064. Buddies: []struct {
  3065. ScreenName string `oscar:"len_prefix=uint8"`
  3066. }{
  3067. {ScreenName: "friend1"},
  3068. {ScreenName: "friend2"},
  3069. },
  3070. },
  3071. },
  3072. },
  3073. },
  3074. tocConfigParams: tocConfigParams{
  3075. setTOCConfigParams: setTOCConfigParams{
  3076. {
  3077. user: state.NewIdentScreenName("me"),
  3078. config: "m 3\np friend3\np friend4\n\ng Buddies\nb friend1\nb friend2",
  3079. },
  3080. },
  3081. },
  3082. },
  3083. },
  3084. {
  3085. name: "set permit some config, receive err from permit-deny svc",
  3086. me: newTestSession("me"),
  3087. givenCmd: []byte("toc_set_config {m 3\np friend3\np friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  3088. mockParams: mockParams{
  3089. permitDenyParams: permitDenyParams{
  3090. addPermListEntriesParams: addPermListEntriesParams{
  3091. {
  3092. me: state.NewIdentScreenName("me"),
  3093. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  3094. Users: []struct {
  3095. ScreenName string `oscar:"len_prefix=uint8"`
  3096. }{
  3097. {ScreenName: "friend3"},
  3098. {ScreenName: "friend4"},
  3099. },
  3100. },
  3101. err: io.EOF,
  3102. },
  3103. },
  3104. },
  3105. },
  3106. wantMsg: cmdInternalSvcErr,
  3107. },
  3108. {
  3109. name: "successfully set deny some config",
  3110. me: newTestSession("me"),
  3111. givenCmd: []byte("toc_set_config {m 4\nd friend3\nd friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  3112. mockParams: mockParams{
  3113. permitDenyParams: permitDenyParams{
  3114. addDenyListEntriesParams: addDenyListEntriesParams{
  3115. {
  3116. me: state.NewIdentScreenName("me"),
  3117. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  3118. Users: []struct {
  3119. ScreenName string `oscar:"len_prefix=uint8"`
  3120. }{
  3121. {ScreenName: "friend3"},
  3122. {ScreenName: "friend4"},
  3123. },
  3124. },
  3125. },
  3126. },
  3127. },
  3128. buddyParams: buddyParams{
  3129. addBuddiesParams: addBuddiesParams{
  3130. {
  3131. me: state.NewIdentScreenName("me"),
  3132. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  3133. Buddies: []struct {
  3134. ScreenName string `oscar:"len_prefix=uint8"`
  3135. }{
  3136. {ScreenName: "friend1"},
  3137. {ScreenName: "friend2"},
  3138. },
  3139. },
  3140. },
  3141. },
  3142. },
  3143. tocConfigParams: tocConfigParams{
  3144. setTOCConfigParams: setTOCConfigParams{
  3145. {
  3146. user: state.NewIdentScreenName("me"),
  3147. config: "m 4\nd friend3\nd friend4\n\ng Buddies\nb friend1\nb friend2",
  3148. },
  3149. },
  3150. },
  3151. },
  3152. },
  3153. {
  3154. name: "set deny some config, receive err from permit-deny svc",
  3155. me: newTestSession("me"),
  3156. givenCmd: []byte("toc_set_config {m 4\nd friend3\nd friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  3157. mockParams: mockParams{
  3158. permitDenyParams: permitDenyParams{
  3159. addDenyListEntriesParams: addDenyListEntriesParams{
  3160. {
  3161. me: state.NewIdentScreenName("me"),
  3162. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  3163. Users: []struct {
  3164. ScreenName string `oscar:"len_prefix=uint8"`
  3165. }{
  3166. {ScreenName: "friend3"},
  3167. {ScreenName: "friend4"},
  3168. },
  3169. },
  3170. err: io.EOF,
  3171. },
  3172. },
  3173. },
  3174. },
  3175. wantMsg: cmdInternalSvcErr,
  3176. },
  3177. {
  3178. name: "set unknown PD mode",
  3179. me: newTestSession("me"),
  3180. givenCmd: []byte("toc_set_config {m 5\nd friend3\nd friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  3181. wantMsg: cmdInternalSvcErr,
  3182. },
  3183. {
  3184. name: "bad command",
  3185. givenCmd: []byte(`toc_chat_leave`),
  3186. wantMsg: cmdInternalSvcErr,
  3187. },
  3188. }
  3189. for _, tc := range cases {
  3190. t.Run(tc.name, func(t *testing.T) {
  3191. ctx := context.Background()
  3192. pdSvc := newMockPermitDenyService(t)
  3193. for _, params := range tc.mockParams.addDenyListEntriesParams {
  3194. pdSvc.EXPECT().
  3195. AddDenyListEntries(ctx, matchSession(params.me), params.body).
  3196. Return(params.err)
  3197. }
  3198. for _, params := range tc.mockParams.addPermListEntriesParams {
  3199. pdSvc.EXPECT().
  3200. AddPermListEntries(ctx, matchSession(params.me), params.body).
  3201. Return(params.err)
  3202. }
  3203. buddySvc := newMockBuddyService(t)
  3204. for _, params := range tc.mockParams.addBuddiesParams {
  3205. buddySvc.EXPECT().
  3206. AddBuddies(ctx, matchSession(params.me), params.inBody).
  3207. Return(params.err)
  3208. }
  3209. tocConfigSvc := newMockTOCConfigStore(t)
  3210. for _, params := range tc.mockParams.setTOCConfigParams {
  3211. tocConfigSvc.EXPECT().
  3212. SetTOCConfig(params.user, params.config).
  3213. Return(params.err)
  3214. }
  3215. svc := OSCARProxy{
  3216. BuddyService: buddySvc,
  3217. Logger: slog.Default(),
  3218. PermitDenyService: pdSvc,
  3219. TOCConfigStore: tocConfigSvc,
  3220. }
  3221. msg := svc.SetConfig(ctx, tc.me, tc.givenCmd)
  3222. assert.Equal(t, tc.wantMsg, msg)
  3223. })
  3224. }
  3225. }
  3226. func TestOSCARProxy_SetDir(t *testing.T) {
  3227. cases := []struct {
  3228. // name is the unit test name
  3229. name string
  3230. // me is the TOC user session
  3231. me *state.Session
  3232. // givenCmd is the TOC command
  3233. givenCmd []byte
  3234. // wantMsg is the expected TOC response
  3235. wantMsg string
  3236. // mockParams is the list of params sent to mocks that satisfy this
  3237. // method's dependencies
  3238. mockParams mockParams
  3239. }{
  3240. {
  3241. name: "successfully set directory info with quoted fields",
  3242. me: newTestSession("me"),
  3243. givenCmd: []byte(`toc_set_dir "first name":"middle name":"last name":"maiden name":"city":"state":"country":"email":"allow web searches"`),
  3244. mockParams: mockParams{
  3245. locateParams: locateParams{
  3246. setDirInfoParams: setDirInfoParams{
  3247. {
  3248. me: state.NewIdentScreenName("me"),
  3249. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  3250. TLVRestBlock: wire.TLVRestBlock{
  3251. TLVList: wire.TLVList{
  3252. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  3253. wire.NewTLVBE(wire.ODirTLVMiddleName, "middle name"),
  3254. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  3255. wire.NewTLVBE(wire.ODirTLVMaidenName, "maiden name"),
  3256. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  3257. wire.NewTLVBE(wire.ODirTLVState, "state"),
  3258. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  3259. },
  3260. },
  3261. },
  3262. },
  3263. },
  3264. },
  3265. },
  3266. },
  3267. {
  3268. name: "successfully set directory info with some blank fields",
  3269. me: newTestSession("me"),
  3270. givenCmd: []byte(`toc_set_dir "first name"::"last name"::"city":"state":"country":"email":"allow web searches"`),
  3271. mockParams: mockParams{
  3272. locateParams: locateParams{
  3273. setDirInfoParams: setDirInfoParams{
  3274. {
  3275. me: state.NewIdentScreenName("me"),
  3276. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  3277. TLVRestBlock: wire.TLVRestBlock{
  3278. TLVList: wire.TLVList{
  3279. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  3280. wire.NewTLVBE(wire.ODirTLVMiddleName, ""),
  3281. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  3282. wire.NewTLVBE(wire.ODirTLVMaidenName, ""),
  3283. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  3284. wire.NewTLVBE(wire.ODirTLVState, "state"),
  3285. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  3286. },
  3287. },
  3288. },
  3289. },
  3290. },
  3291. },
  3292. },
  3293. },
  3294. {
  3295. name: "successfully set directory info with last two fields absent",
  3296. me: newTestSession("me"),
  3297. givenCmd: []byte(`toc_set_dir "first name"::"last name"::"city":"state":"country"`),
  3298. mockParams: mockParams{
  3299. locateParams: locateParams{
  3300. setDirInfoParams: setDirInfoParams{
  3301. {
  3302. me: state.NewIdentScreenName("me"),
  3303. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  3304. TLVRestBlock: wire.TLVRestBlock{
  3305. TLVList: wire.TLVList{
  3306. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  3307. wire.NewTLVBE(wire.ODirTLVMiddleName, ""),
  3308. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  3309. wire.NewTLVBE(wire.ODirTLVMaidenName, ""),
  3310. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  3311. wire.NewTLVBE(wire.ODirTLVState, "state"),
  3312. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  3313. },
  3314. },
  3315. },
  3316. },
  3317. },
  3318. },
  3319. },
  3320. },
  3321. {
  3322. name: "set directory info, receive error from locate svc",
  3323. me: newTestSession("me"),
  3324. givenCmd: []byte(`toc_set_dir "first name":"middle name":"last name":"maiden name":"city":"state":"country":"email":"allow web searches"`),
  3325. mockParams: mockParams{
  3326. locateParams: locateParams{
  3327. setDirInfoParams: setDirInfoParams{
  3328. {
  3329. me: state.NewIdentScreenName("me"),
  3330. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  3331. TLVRestBlock: wire.TLVRestBlock{
  3332. TLVList: wire.TLVList{
  3333. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  3334. wire.NewTLVBE(wire.ODirTLVMiddleName, "middle name"),
  3335. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  3336. wire.NewTLVBE(wire.ODirTLVMaidenName, "maiden name"),
  3337. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  3338. wire.NewTLVBE(wire.ODirTLVState, "state"),
  3339. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  3340. },
  3341. },
  3342. },
  3343. err: io.EOF,
  3344. },
  3345. },
  3346. },
  3347. },
  3348. wantMsg: cmdInternalSvcErr,
  3349. },
  3350. {
  3351. name: "set directory with too many fields present",
  3352. me: newTestSession("me"),
  3353. givenCmd: []byte(`toc_set_dir "first name"::"last name"::"city":"state":"country":"email":"allow web searches":"extra":"extra"`),
  3354. wantMsg: cmdInternalSvcErr,
  3355. },
  3356. {
  3357. name: "bad command",
  3358. givenCmd: []byte(`toc_set_dir`),
  3359. wantMsg: cmdInternalSvcErr,
  3360. },
  3361. }
  3362. for _, tc := range cases {
  3363. t.Run(tc.name, func(t *testing.T) {
  3364. ctx := context.Background()
  3365. locateSvc := newMockLocateService(t)
  3366. for _, params := range tc.mockParams.setDirInfoParams {
  3367. locateSvc.EXPECT().
  3368. SetDirInfo(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  3369. Return(params.msg, params.err)
  3370. }
  3371. svc := OSCARProxy{
  3372. Logger: slog.Default(),
  3373. LocateService: locateSvc,
  3374. }
  3375. msg := svc.SetDir(ctx, tc.me, tc.givenCmd)
  3376. assert.Equal(t, tc.wantMsg, msg)
  3377. })
  3378. }
  3379. }
  3380. func TestOSCARProxy_SetIdle(t *testing.T) {
  3381. cases := []struct {
  3382. // name is the unit test name
  3383. name string
  3384. // me is the TOC user session
  3385. me *state.Session
  3386. // givenCmd is the TOC command
  3387. givenCmd []byte
  3388. // wantMsg is the expected TOC response
  3389. wantMsg string
  3390. // mockParams is the list of params sent to mocks that satisfy this
  3391. // method's dependencies
  3392. mockParams mockParams
  3393. }{
  3394. {
  3395. name: "successfully set idle status",
  3396. me: newTestSession("me"),
  3397. givenCmd: []byte(`toc_set_idle 10`),
  3398. mockParams: mockParams{
  3399. oServiceBOSParams: oServiceParams{
  3400. idleNotificationParams: idleNotificationParams{
  3401. {
  3402. me: state.NewIdentScreenName("me"),
  3403. bodyIn: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  3404. IdleTime: uint32(10),
  3405. },
  3406. },
  3407. },
  3408. },
  3409. },
  3410. },
  3411. {
  3412. name: "set idle status, receive err from BOS oservice svc",
  3413. me: newTestSession("me"),
  3414. givenCmd: []byte(`toc_set_idle 10`),
  3415. mockParams: mockParams{
  3416. oServiceBOSParams: oServiceParams{
  3417. idleNotificationParams: idleNotificationParams{
  3418. {
  3419. me: state.NewIdentScreenName("me"),
  3420. bodyIn: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  3421. IdleTime: uint32(10),
  3422. },
  3423. err: io.EOF,
  3424. },
  3425. },
  3426. },
  3427. },
  3428. wantMsg: cmdInternalSvcErr,
  3429. },
  3430. {
  3431. name: "bad secs param",
  3432. givenCmd: []byte(`toc_set_idle zero`),
  3433. wantMsg: cmdInternalSvcErr,
  3434. },
  3435. {
  3436. name: "bad command",
  3437. givenCmd: []byte(`toc_set_idle`),
  3438. wantMsg: cmdInternalSvcErr,
  3439. },
  3440. }
  3441. for _, tc := range cases {
  3442. t.Run(tc.name, func(t *testing.T) {
  3443. ctx := context.Background()
  3444. oServiceSvc := newMockOServiceService(t)
  3445. for _, params := range tc.mockParams.oServiceBOSParams.idleNotificationParams {
  3446. oServiceSvc.EXPECT().
  3447. IdleNotification(ctx, matchSession(params.me), params.bodyIn).
  3448. Return(params.err)
  3449. }
  3450. svc := OSCARProxy{
  3451. Logger: slog.Default(),
  3452. OServiceServiceBOS: oServiceSvc,
  3453. }
  3454. msg := svc.SetIdle(ctx, tc.me, tc.givenCmd)
  3455. assert.Equal(t, tc.wantMsg, msg)
  3456. })
  3457. }
  3458. }
  3459. func TestOSCARProxy_SetInfo(t *testing.T) {
  3460. cases := []struct {
  3461. // name is the unit test name
  3462. name string
  3463. // me is the TOC user session
  3464. me *state.Session
  3465. // givenCmd is the TOC command
  3466. givenCmd []byte
  3467. // wantMsg is the expected TOC response
  3468. wantMsg string
  3469. // mockParams is the list of params sent to mocks that satisfy this
  3470. // method's dependencies
  3471. mockParams mockParams
  3472. }{
  3473. {
  3474. name: "successfully set profile",
  3475. me: newTestSession("me"),
  3476. givenCmd: []byte(`toc_set_info "my profile!"`),
  3477. mockParams: mockParams{
  3478. locateParams: locateParams{
  3479. setInfoParams: setInfoParams{
  3480. {
  3481. me: state.NewIdentScreenName("me"),
  3482. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  3483. TLVRestBlock: wire.TLVRestBlock{
  3484. TLVList: wire.TLVList{
  3485. wire.NewTLVBE(wire.LocateTLVTagsInfoSigData, "my profile!"),
  3486. },
  3487. },
  3488. },
  3489. },
  3490. },
  3491. },
  3492. },
  3493. },
  3494. {
  3495. name: "set profile, receive error from locate svc",
  3496. me: newTestSession("me"),
  3497. givenCmd: []byte(`toc_set_info "my profile!"`),
  3498. mockParams: mockParams{
  3499. locateParams: locateParams{
  3500. setInfoParams: setInfoParams{
  3501. {
  3502. me: state.NewIdentScreenName("me"),
  3503. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  3504. TLVRestBlock: wire.TLVRestBlock{
  3505. TLVList: wire.TLVList{
  3506. wire.NewTLVBE(wire.LocateTLVTagsInfoSigData, "my profile!"),
  3507. },
  3508. },
  3509. },
  3510. err: io.EOF,
  3511. },
  3512. },
  3513. },
  3514. },
  3515. wantMsg: cmdInternalSvcErr,
  3516. },
  3517. {
  3518. name: "bad command",
  3519. givenCmd: []byte(`toc_set_info`),
  3520. wantMsg: cmdInternalSvcErr,
  3521. },
  3522. }
  3523. for _, tc := range cases {
  3524. t.Run(tc.name, func(t *testing.T) {
  3525. ctx := context.Background()
  3526. locateSvc := newMockLocateService(t)
  3527. for _, params := range tc.mockParams.setInfoParams {
  3528. locateSvc.EXPECT().
  3529. SetInfo(ctx, matchSession(params.me), params.inBody).
  3530. Return(params.err)
  3531. }
  3532. svc := OSCARProxy{
  3533. Logger: slog.Default(),
  3534. LocateService: locateSvc,
  3535. }
  3536. msg := svc.SetInfo(ctx, tc.me, tc.givenCmd)
  3537. assert.Equal(t, tc.wantMsg, msg)
  3538. })
  3539. }
  3540. }
  3541. func TestOSCARProxy_Signon(t *testing.T) {
  3542. roastedPass := wire.RoastTOCPassword([]byte("thepass"))
  3543. cases := []struct {
  3544. // name is the unit test name
  3545. name string
  3546. // me is the TOC user session
  3547. me *state.Session
  3548. // givenCmd is the TOC command
  3549. givenCmd []byte
  3550. // wantMsg is the expected TOC response
  3551. wantMsg []string
  3552. // mockParams is the list of params sent to mocks that satisfy this
  3553. // method's dependencies
  3554. mockParams mockParams
  3555. }{
  3556. {
  3557. name: "successfully login",
  3558. me: newTestSession("me", func(session *state.Session) {
  3559. session.SetCaps([][16]byte{capChat})
  3560. }),
  3561. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3562. mockParams: mockParams{
  3563. authParams: authParams{
  3564. flapLoginParams: flapLoginParams{
  3565. {
  3566. frame: wire.FLAPSignonFrame{
  3567. TLVRestBlock: wire.TLVRestBlock{
  3568. TLVList: wire.TLVList{
  3569. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3570. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3571. },
  3572. },
  3573. },
  3574. newUserFn: state.NewStubUser,
  3575. tlv: wire.TLVRestBlock{
  3576. TLVList: wire.TLVList{
  3577. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3578. },
  3579. },
  3580. },
  3581. },
  3582. registerBOSSessionParams: registerBOSSessionParams{
  3583. {
  3584. authCookie: []byte("thecookie"),
  3585. sess: newTestSession("me"),
  3586. },
  3587. },
  3588. },
  3589. buddyListRegistryParams: buddyListRegistryParams{
  3590. registerBuddyListParams: registerBuddyListParams{
  3591. {
  3592. user: state.NewIdentScreenName("me"),
  3593. },
  3594. },
  3595. },
  3596. tocConfigParams: tocConfigParams{
  3597. userParams: userParams{
  3598. {
  3599. screenName: state.NewIdentScreenName("me"),
  3600. returnedUser: &state.User{
  3601. TOCConfig: "my-toc-config",
  3602. },
  3603. },
  3604. },
  3605. },
  3606. },
  3607. wantMsg: []string{"SIGN_ON:TOC1.0", "CONFIG:my-toc-config"},
  3608. },
  3609. {
  3610. name: "login, receive error from auth svc FLAP login",
  3611. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3612. mockParams: mockParams{
  3613. authParams: authParams{
  3614. flapLoginParams: flapLoginParams{
  3615. {
  3616. frame: wire.FLAPSignonFrame{
  3617. TLVRestBlock: wire.TLVRestBlock{
  3618. TLVList: wire.TLVList{
  3619. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3620. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3621. },
  3622. },
  3623. },
  3624. newUserFn: state.NewStubUser,
  3625. err: io.EOF,
  3626. },
  3627. },
  3628. },
  3629. },
  3630. wantMsg: []string{string(cmdInternalSvcErr)},
  3631. },
  3632. {
  3633. name: "login, receive error from auth svc registration",
  3634. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3635. mockParams: mockParams{
  3636. authParams: authParams{
  3637. flapLoginParams: flapLoginParams{
  3638. {
  3639. frame: wire.FLAPSignonFrame{
  3640. TLVRestBlock: wire.TLVRestBlock{
  3641. TLVList: wire.TLVList{
  3642. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3643. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3644. },
  3645. },
  3646. },
  3647. newUserFn: state.NewStubUser,
  3648. tlv: wire.TLVRestBlock{
  3649. TLVList: wire.TLVList{
  3650. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3651. },
  3652. },
  3653. },
  3654. },
  3655. registerBOSSessionParams: registerBOSSessionParams{
  3656. {
  3657. authCookie: []byte("thecookie"),
  3658. err: io.EOF,
  3659. },
  3660. },
  3661. },
  3662. },
  3663. wantMsg: []string{string(cmdInternalSvcErr)},
  3664. },
  3665. {
  3666. name: "login, receive error from buddy list registry",
  3667. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3668. mockParams: mockParams{
  3669. authParams: authParams{
  3670. flapLoginParams: flapLoginParams{
  3671. {
  3672. frame: wire.FLAPSignonFrame{
  3673. TLVRestBlock: wire.TLVRestBlock{
  3674. TLVList: wire.TLVList{
  3675. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3676. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3677. },
  3678. },
  3679. },
  3680. newUserFn: state.NewStubUser,
  3681. tlv: wire.TLVRestBlock{
  3682. TLVList: wire.TLVList{
  3683. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3684. },
  3685. },
  3686. },
  3687. },
  3688. registerBOSSessionParams: registerBOSSessionParams{
  3689. {
  3690. authCookie: []byte("thecookie"),
  3691. sess: newTestSession("me"),
  3692. },
  3693. },
  3694. },
  3695. buddyListRegistryParams: buddyListRegistryParams{
  3696. registerBuddyListParams: registerBuddyListParams{
  3697. {
  3698. user: state.NewIdentScreenName("me"),
  3699. err: io.EOF,
  3700. },
  3701. },
  3702. },
  3703. },
  3704. wantMsg: []string{string(cmdInternalSvcErr)},
  3705. },
  3706. {
  3707. name: "login, receive error from TOC config store",
  3708. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3709. mockParams: mockParams{
  3710. authParams: authParams{
  3711. flapLoginParams: flapLoginParams{
  3712. {
  3713. frame: wire.FLAPSignonFrame{
  3714. TLVRestBlock: wire.TLVRestBlock{
  3715. TLVList: wire.TLVList{
  3716. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3717. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3718. },
  3719. },
  3720. },
  3721. newUserFn: state.NewStubUser,
  3722. tlv: wire.TLVRestBlock{
  3723. TLVList: wire.TLVList{
  3724. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3725. },
  3726. },
  3727. },
  3728. },
  3729. registerBOSSessionParams: registerBOSSessionParams{
  3730. {
  3731. authCookie: []byte("thecookie"),
  3732. sess: newTestSession("me"),
  3733. },
  3734. },
  3735. },
  3736. buddyListRegistryParams: buddyListRegistryParams{
  3737. registerBuddyListParams: registerBuddyListParams{
  3738. {
  3739. user: state.NewIdentScreenName("me"),
  3740. },
  3741. },
  3742. },
  3743. tocConfigParams: tocConfigParams{
  3744. userParams: userParams{
  3745. {
  3746. screenName: state.NewIdentScreenName("me"),
  3747. err: io.EOF,
  3748. },
  3749. },
  3750. },
  3751. },
  3752. wantMsg: []string{string(cmdInternalSvcErr)},
  3753. },
  3754. {
  3755. name: "login, user not found after login",
  3756. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3757. mockParams: mockParams{
  3758. authParams: authParams{
  3759. flapLoginParams: flapLoginParams{
  3760. {
  3761. frame: wire.FLAPSignonFrame{
  3762. TLVRestBlock: wire.TLVRestBlock{
  3763. TLVList: wire.TLVList{
  3764. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3765. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3766. },
  3767. },
  3768. },
  3769. newUserFn: state.NewStubUser,
  3770. tlv: wire.TLVRestBlock{
  3771. TLVList: wire.TLVList{
  3772. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3773. },
  3774. },
  3775. },
  3776. },
  3777. registerBOSSessionParams: registerBOSSessionParams{
  3778. {
  3779. authCookie: []byte("thecookie"),
  3780. sess: newTestSession("me"),
  3781. },
  3782. },
  3783. },
  3784. buddyListRegistryParams: buddyListRegistryParams{
  3785. registerBuddyListParams: registerBuddyListParams{
  3786. {
  3787. user: state.NewIdentScreenName("me"),
  3788. },
  3789. },
  3790. },
  3791. tocConfigParams: tocConfigParams{
  3792. userParams: userParams{
  3793. {
  3794. screenName: state.NewIdentScreenName("me"),
  3795. returnedUser: nil,
  3796. },
  3797. },
  3798. },
  3799. },
  3800. wantMsg: []string{string(cmdInternalSvcErr)},
  3801. },
  3802. {
  3803. name: "login with bad credentials",
  3804. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3805. mockParams: mockParams{
  3806. authParams: authParams{
  3807. flapLoginParams: flapLoginParams{
  3808. {
  3809. frame: wire.FLAPSignonFrame{
  3810. TLVRestBlock: wire.TLVRestBlock{
  3811. TLVList: wire.TLVList{
  3812. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3813. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3814. },
  3815. },
  3816. },
  3817. newUserFn: state.NewStubUser,
  3818. tlv: wire.TLVRestBlock{
  3819. TLVList: wire.TLVList{
  3820. wire.NewTLVBE(wire.LoginTLVTagsErrorSubcode, wire.LoginErrInvalidUsernameOrPassword),
  3821. },
  3822. },
  3823. },
  3824. },
  3825. },
  3826. },
  3827. wantMsg: []string{"ERROR:980"},
  3828. },
  3829. {
  3830. name: "bad command",
  3831. givenCmd: []byte(`toc_init_done_diff`),
  3832. wantMsg: []string{string(cmdInternalSvcErr)},
  3833. },
  3834. }
  3835. for _, tc := range cases {
  3836. t.Run(tc.name, func(t *testing.T) {
  3837. ctx := context.Background()
  3838. authSvc := newMockAuthService(t)
  3839. for _, params := range tc.mockParams.flapLoginParams {
  3840. authSvc.EXPECT().
  3841. FLAPLogin(params.frame, mock.Anything).
  3842. Return(params.tlv, params.err)
  3843. }
  3844. for _, params := range tc.mockParams.registerBOSSessionParams {
  3845. authSvc.EXPECT().
  3846. RegisterBOSSession(ctx, params.authCookie).
  3847. Return(params.sess, params.err)
  3848. }
  3849. buddyRegistry := newMockBuddyListRegistry(t)
  3850. for _, params := range tc.mockParams.registerBuddyListParams {
  3851. buddyRegistry.EXPECT().
  3852. RegisterBuddyList(params.user).
  3853. Return(params.err)
  3854. }
  3855. tocCfg := newMockTOCConfigStore(t)
  3856. for _, params := range tc.mockParams.userParams {
  3857. tocCfg.EXPECT().
  3858. User(params.screenName).
  3859. Return(params.returnedUser, params.err)
  3860. }
  3861. svc := OSCARProxy{
  3862. AuthService: authSvc,
  3863. BuddyListRegistry: buddyRegistry,
  3864. Logger: slog.Default(),
  3865. TOCConfigStore: tocCfg,
  3866. }
  3867. sess, msg := svc.Signon(ctx, tc.givenCmd)
  3868. assert.Equal(t, tc.wantMsg, msg)
  3869. if tc.me == nil {
  3870. assert.Nil(t, sess)
  3871. } else if assert.NotNil(t, sess) {
  3872. assert.Equal(t, tc.me.IdentScreenName(), sess.IdentScreenName())
  3873. assert.Equal(t, tc.me.Caps(), sess.Caps())
  3874. }
  3875. })
  3876. }
  3877. }
  3878. func TestOSCARProxy_Signout(t *testing.T) {
  3879. cases := []struct {
  3880. // name is the unit test name
  3881. name string
  3882. // me is the TOC user session
  3883. me *state.Session
  3884. // givenChatRegistry is the chat registry passed to the function
  3885. chatRegistry *ChatRegistry
  3886. // mockParams is the list of params sent to mocks that satisfy this
  3887. // method's dependencies
  3888. mockParams mockParams
  3889. }{
  3890. {
  3891. name: "successfully sign out",
  3892. me: newTestSession("me"),
  3893. chatRegistry: func() *ChatRegistry {
  3894. cr := NewChatRegistry()
  3895. s1 := state.NewSession()
  3896. s1.SetIdentScreenName(state.NewIdentScreenName("me1"))
  3897. cr.RegisterSess(0, s1)
  3898. s2 := state.NewSession()
  3899. s2.SetIdentScreenName(state.NewIdentScreenName("me2"))
  3900. cr.RegisterSess(1, s2)
  3901. return cr
  3902. }(),
  3903. mockParams: mockParams{
  3904. buddyParams: buddyParams{
  3905. broadcastBuddyDepartedParams: broadcastBuddyDepartedParams{
  3906. {
  3907. me: state.NewIdentScreenName("me"),
  3908. },
  3909. },
  3910. },
  3911. buddyListRegistryParams: buddyListRegistryParams{
  3912. unregisterBuddyListParams: unregisterBuddyListParams{
  3913. {
  3914. user: state.NewIdentScreenName("me"),
  3915. },
  3916. },
  3917. },
  3918. authParams: authParams{
  3919. signoutParams: signoutParams{
  3920. {
  3921. me: state.NewIdentScreenName("me"),
  3922. },
  3923. },
  3924. signoutChatParams: signoutChatParams{
  3925. {
  3926. me: state.NewIdentScreenName("me1"),
  3927. },
  3928. {
  3929. me: state.NewIdentScreenName("me2"),
  3930. },
  3931. },
  3932. },
  3933. },
  3934. },
  3935. {
  3936. name: "sign out, receive error from buddy service",
  3937. me: newTestSession("me"),
  3938. chatRegistry: func() *ChatRegistry {
  3939. return NewChatRegistry()
  3940. }(),
  3941. mockParams: mockParams{
  3942. buddyParams: buddyParams{
  3943. broadcastBuddyDepartedParams: broadcastBuddyDepartedParams{
  3944. {
  3945. me: state.NewIdentScreenName("me"),
  3946. err: io.EOF,
  3947. },
  3948. },
  3949. },
  3950. buddyListRegistryParams: buddyListRegistryParams{
  3951. unregisterBuddyListParams: unregisterBuddyListParams{
  3952. {
  3953. user: state.NewIdentScreenName("me"),
  3954. },
  3955. },
  3956. },
  3957. authParams: authParams{
  3958. signoutParams: signoutParams{
  3959. {
  3960. me: state.NewIdentScreenName("me"),
  3961. },
  3962. },
  3963. },
  3964. },
  3965. },
  3966. {
  3967. name: "sign out, receive error from buddy list registry",
  3968. me: newTestSession("me"),
  3969. chatRegistry: func() *ChatRegistry {
  3970. return NewChatRegistry()
  3971. }(),
  3972. mockParams: mockParams{
  3973. buddyParams: buddyParams{
  3974. broadcastBuddyDepartedParams: broadcastBuddyDepartedParams{
  3975. {
  3976. me: state.NewIdentScreenName("me"),
  3977. },
  3978. },
  3979. },
  3980. buddyListRegistryParams: buddyListRegistryParams{
  3981. unregisterBuddyListParams: unregisterBuddyListParams{
  3982. {
  3983. user: state.NewIdentScreenName("me"),
  3984. err: io.EOF,
  3985. },
  3986. },
  3987. },
  3988. authParams: authParams{
  3989. signoutParams: signoutParams{
  3990. {
  3991. me: state.NewIdentScreenName("me"),
  3992. },
  3993. },
  3994. },
  3995. },
  3996. },
  3997. }
  3998. for _, tc := range cases {
  3999. t.Run(tc.name, func(t *testing.T) {
  4000. ctx := context.Background()
  4001. buddySvc := newMockBuddyService(t)
  4002. for _, params := range tc.mockParams.broadcastBuddyDepartedParams {
  4003. buddySvc.EXPECT().
  4004. BroadcastBuddyDeparted(ctx, matchSession(params.me)).
  4005. Return(params.err)
  4006. }
  4007. buddyListSvc := newMockBuddyListRegistry(t)
  4008. for _, params := range tc.mockParams.unregisterBuddyListParams {
  4009. buddyListSvc.EXPECT().
  4010. UnregisterBuddyList(params.user).
  4011. Return(params.err)
  4012. }
  4013. authSvc := newMockAuthService(t)
  4014. for _, params := range tc.mockParams.signoutParams {
  4015. authSvc.EXPECT().Signout(ctx, matchSession(params.me))
  4016. }
  4017. for _, params := range tc.mockParams.signoutChatParams {
  4018. authSvc.EXPECT().SignoutChat(ctx, matchSession(params.me))
  4019. }
  4020. svc := OSCARProxy{
  4021. AuthService: authSvc,
  4022. BuddyListRegistry: buddyListSvc,
  4023. BuddyService: buddySvc,
  4024. Logger: slog.Default(),
  4025. }
  4026. svc.Signout(ctx, tc.me, tc.chatRegistry)
  4027. })
  4028. }
  4029. }
  4030. func Test_parseArgs(t *testing.T) {
  4031. type testCase struct {
  4032. name string
  4033. givenPayload string
  4034. givenCmd string
  4035. givenArgs []*string
  4036. wantVarArgs []string
  4037. wantArgs []string
  4038. wantErrMsg string
  4039. }
  4040. tests := []testCase{
  4041. {
  4042. name: "no positional args or varargs",
  4043. givenPayload: `toc_chat_invite`,
  4044. givenCmd: "toc_chat_invite",
  4045. givenArgs: nil,
  4046. wantVarArgs: []string{},
  4047. },
  4048. {
  4049. name: "positional args with varargs",
  4050. givenPayload: `toc_chat_invite 1234 "Join me!" user1 user2 user3`,
  4051. givenCmd: "toc_chat_invite",
  4052. givenArgs: []*string{new(string), new(string)},
  4053. wantVarArgs: []string{"user1", "user2", "user3"},
  4054. wantArgs: []string{"1234", "Join me!"},
  4055. },
  4056. {
  4057. name: "nil positional argument placeholders should get skipped",
  4058. givenPayload: `toc_chat_invite 1234 "Join me!" user1 user2 user3`,
  4059. givenCmd: "toc_chat_invite",
  4060. givenArgs: []*string{nil, nil}, // still 2 placeholders, both nil
  4061. wantVarArgs: []string{"user1", "user2", "user3"},
  4062. wantArgs: []string{"", ""},
  4063. },
  4064. {
  4065. name: "positional args with no varargs",
  4066. givenPayload: `toc_chat_invite 1234 "Join me!"`,
  4067. givenCmd: "toc_chat_invite",
  4068. givenArgs: []*string{new(string), new(string)}, // roomID + msg
  4069. wantVarArgs: []string{},
  4070. wantArgs: []string{"1234", "Join me!"},
  4071. },
  4072. {
  4073. name: "varargs only",
  4074. givenPayload: `toc_chat_invite user1 user2 user3`,
  4075. givenCmd: "toc_chat_invite",
  4076. givenArgs: nil,
  4077. wantVarArgs: []string{"user1", "user2", "user3"},
  4078. },
  4079. {
  4080. name: "command mismatch",
  4081. givenPayload: `toc_chat_invite user1 user2 user3`,
  4082. givenCmd: "toc_chat_accept",
  4083. givenArgs: nil,
  4084. wantVarArgs: []string{},
  4085. wantErrMsg: "mismatch",
  4086. },
  4087. {
  4088. name: "too many positional arg placeholders",
  4089. givenPayload: `toc_chat_invite`,
  4090. givenCmd: "toc_chat_invite",
  4091. givenArgs: []*string{new(string), new(string)},
  4092. wantVarArgs: []string{},
  4093. wantErrMsg: "command contains fewer arguments than expected",
  4094. },
  4095. {
  4096. name: "CSV parser error",
  4097. givenPayload: ``,
  4098. givenCmd: "toc_chat_invite",
  4099. givenArgs: []*string{nil},
  4100. wantVarArgs: []string{},
  4101. wantErrMsg: "CSV reader error",
  4102. },
  4103. }
  4104. for _, tt := range tests {
  4105. t.Run(tt.name, func(t *testing.T) {
  4106. varArgs, err := parseArgs([]byte(tt.givenPayload), tt.givenCmd, tt.givenArgs...)
  4107. if tt.wantErrMsg != "" {
  4108. assert.ErrorContains(t, err, tt.wantErrMsg)
  4109. return
  4110. }
  4111. assert.NoError(t, err)
  4112. // verify the placeholder pointers got populated
  4113. for i, want := range tt.wantArgs {
  4114. if want == "" {
  4115. assert.Nil(t, tt.givenArgs[i])
  4116. } else {
  4117. got := *tt.givenArgs[i]
  4118. assert.Equal(t, want, got)
  4119. }
  4120. }
  4121. // verify we have the same varargs
  4122. assert.Equal(t, tt.wantVarArgs, varArgs)
  4123. assert.Equal(t, len(tt.wantArgs), len(tt.givenArgs))
  4124. })
  4125. }
  4126. }