cmd_client_test.go 117 KB

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