cmd_client_test.go 122 KB

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