handler_test.go 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742
  1. package oscar
  2. import (
  3. "bytes"
  4. "context"
  5. "log/slog"
  6. "math"
  7. "testing"
  8. "github.com/mk6i/open-oscar-server/config"
  9. "github.com/mk6i/open-oscar-server/server/oscar/middleware"
  10. "github.com/mk6i/open-oscar-server/state"
  11. "github.com/mk6i/open-oscar-server/wire"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/mock"
  14. )
  15. func TestHandler_AdminConfirmRequest(t *testing.T) {
  16. tests := []struct {
  17. name string
  18. inputBody wire.SNAC_0x07_0x06_AdminConfirmRequest
  19. serviceError error
  20. responseError error
  21. expectedError error
  22. }{
  23. {
  24. name: "success",
  25. inputBody: wire.SNAC_0x07_0x06_AdminConfirmRequest{},
  26. },
  27. {
  28. name: "service error",
  29. inputBody: wire.SNAC_0x07_0x06_AdminConfirmRequest{},
  30. serviceError: assert.AnError,
  31. expectedError: assert.AnError,
  32. },
  33. {
  34. name: "response writer error",
  35. inputBody: wire.SNAC_0x07_0x06_AdminConfirmRequest{},
  36. responseError: assert.AnError,
  37. expectedError: assert.AnError,
  38. },
  39. }
  40. for _, tt := range tests {
  41. t.Run(tt.name, func(t *testing.T) {
  42. input := wire.SNACMessage{
  43. Frame: wire.SNACFrame{
  44. FoodGroup: wire.Admin,
  45. SubGroup: wire.AdminAcctConfirmRequest,
  46. },
  47. Body: tt.inputBody,
  48. }
  49. output := wire.SNACMessage{
  50. Frame: wire.SNACFrame{
  51. FoodGroup: wire.Admin,
  52. SubGroup: wire.AdminAcctConfirmReply,
  53. },
  54. Body: wire.SNAC_0x07_0x07_AdminConfirmReply{
  55. Status: wire.AdminAcctConfirmStatusEmailSent,
  56. },
  57. }
  58. svc := newMockAdminService(t)
  59. svc.EXPECT().
  60. ConfirmRequest(mock.Anything, mock.Anything, input.Frame).
  61. Return(output, tt.serviceError)
  62. h := Handler{
  63. AdminService: svc,
  64. RouteLogger: middleware.RouteLogger{
  65. Logger: slog.Default(),
  66. },
  67. }
  68. responseWriter := newMockResponseWriter(t)
  69. if tt.serviceError == nil {
  70. responseWriter.EXPECT().
  71. SendSNAC(output.Frame, output.Body).
  72. Return(tt.responseError)
  73. }
  74. buf := &bytes.Buffer{}
  75. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  76. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  77. if tt.expectedError != nil {
  78. assert.ErrorIs(t, err, tt.expectedError)
  79. } else {
  80. assert.NoError(t, err)
  81. }
  82. })
  83. }
  84. }
  85. func TestHandler_AdminInfoQuery_RegistrationStatus(t *testing.T) {
  86. tests := []struct {
  87. name string
  88. inputBody wire.SNAC_0x07_0x02_AdminInfoQuery
  89. serviceError error
  90. responseError error
  91. expectedError error
  92. }{
  93. {
  94. name: "success",
  95. inputBody: wire.SNAC_0x07_0x02_AdminInfoQuery{
  96. TLVRestBlock: wire.TLVRestBlock{
  97. TLVList: wire.TLVList{
  98. wire.NewTLVBE(wire.AdminTLVRegistrationStatus, uint16(0x00)),
  99. },
  100. },
  101. },
  102. },
  103. {
  104. name: "service error",
  105. inputBody: wire.SNAC_0x07_0x02_AdminInfoQuery{
  106. TLVRestBlock: wire.TLVRestBlock{
  107. TLVList: wire.TLVList{
  108. wire.NewTLVBE(wire.AdminTLVRegistrationStatus, uint16(0x00)),
  109. },
  110. },
  111. },
  112. serviceError: assert.AnError,
  113. expectedError: assert.AnError,
  114. },
  115. {
  116. name: "response writer error",
  117. inputBody: wire.SNAC_0x07_0x02_AdminInfoQuery{
  118. TLVRestBlock: wire.TLVRestBlock{
  119. TLVList: wire.TLVList{
  120. wire.NewTLVBE(wire.AdminTLVRegistrationStatus, uint16(0x00)),
  121. },
  122. },
  123. },
  124. responseError: assert.AnError,
  125. expectedError: assert.AnError,
  126. },
  127. }
  128. for _, tt := range tests {
  129. t.Run(tt.name, func(t *testing.T) {
  130. input := wire.SNACMessage{
  131. Frame: wire.SNACFrame{
  132. FoodGroup: wire.Admin,
  133. SubGroup: wire.AdminInfoQuery,
  134. },
  135. Body: tt.inputBody,
  136. }
  137. output := wire.SNACMessage{
  138. Frame: wire.SNACFrame{
  139. FoodGroup: wire.Admin,
  140. SubGroup: wire.AdminInfoReply,
  141. },
  142. Body: wire.SNAC_0x07_0x03_AdminInfoReply{
  143. Permissions: wire.AdminInfoPermissionsReadWrite,
  144. TLVBlock: wire.TLVBlock{
  145. TLVList: wire.TLVList{
  146. wire.NewTLVBE(wire.AdminTLVRegistrationStatus, wire.AdminInfoRegStatusFullDisclosure),
  147. },
  148. },
  149. },
  150. }
  151. svc := newMockAdminService(t)
  152. svc.EXPECT().
  153. InfoQuery(mock.Anything, mock.Anything, input.Frame, input.Body).
  154. Return(output, tt.serviceError)
  155. h := Handler{
  156. AdminService: svc,
  157. RouteLogger: middleware.RouteLogger{
  158. Logger: slog.Default(),
  159. },
  160. }
  161. responseWriter := newMockResponseWriter(t)
  162. if tt.serviceError == nil {
  163. responseWriter.EXPECT().
  164. SendSNAC(output.Frame, output.Body).
  165. Return(tt.responseError)
  166. }
  167. buf := &bytes.Buffer{}
  168. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  169. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  170. if tt.expectedError != nil {
  171. assert.ErrorIs(t, err, tt.expectedError)
  172. } else {
  173. assert.NoError(t, err)
  174. }
  175. })
  176. }
  177. }
  178. func TestHandler_AdminInfoChangeRequest_ScreenNameFormatted(t *testing.T) {
  179. tests := []struct {
  180. name string
  181. inputBody wire.SNAC_0x07_0x04_AdminInfoChangeRequest
  182. serviceError error
  183. responseError error
  184. expectedError error
  185. }{
  186. {
  187. name: "success",
  188. inputBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  189. TLVRestBlock: wire.TLVRestBlock{
  190. TLVList: wire.TLVList{
  191. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "Chatting Chuck"),
  192. },
  193. },
  194. },
  195. },
  196. {
  197. name: "service error",
  198. inputBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  199. TLVRestBlock: wire.TLVRestBlock{
  200. TLVList: wire.TLVList{
  201. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "Chatting Chuck"),
  202. },
  203. },
  204. },
  205. serviceError: assert.AnError,
  206. expectedError: assert.AnError,
  207. },
  208. {
  209. name: "response writer error",
  210. inputBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  211. TLVRestBlock: wire.TLVRestBlock{
  212. TLVList: wire.TLVList{
  213. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "Chatting Chuck"),
  214. },
  215. },
  216. },
  217. responseError: assert.AnError,
  218. expectedError: assert.AnError,
  219. },
  220. }
  221. for _, tt := range tests {
  222. t.Run(tt.name, func(t *testing.T) {
  223. input := wire.SNACMessage{
  224. Frame: wire.SNACFrame{
  225. FoodGroup: wire.Admin,
  226. SubGroup: wire.AdminInfoChangeRequest,
  227. },
  228. Body: tt.inputBody,
  229. }
  230. output := wire.SNACMessage{
  231. Frame: wire.SNACFrame{
  232. FoodGroup: wire.Admin,
  233. SubGroup: wire.AdminInfoChangeReply,
  234. },
  235. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  236. Permissions: wire.AdminInfoPermissionsReadWrite,
  237. TLVBlock: wire.TLVBlock{
  238. TLVList: wire.TLVList{
  239. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "Chatting Chuck"),
  240. },
  241. },
  242. },
  243. }
  244. svc := newMockAdminService(t)
  245. svc.EXPECT().
  246. InfoChangeRequest(mock.Anything, mock.Anything, input.Frame, input.Body).
  247. Return(output, tt.serviceError)
  248. h := Handler{
  249. AdminService: svc,
  250. RouteLogger: middleware.RouteLogger{
  251. Logger: slog.Default(),
  252. },
  253. }
  254. responseWriter := newMockResponseWriter(t)
  255. if tt.serviceError == nil {
  256. responseWriter.EXPECT().
  257. SendSNAC(output.Frame, output.Body).
  258. Return(tt.responseError)
  259. }
  260. buf := &bytes.Buffer{}
  261. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  262. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  263. if tt.expectedError != nil {
  264. assert.ErrorIs(t, err, tt.expectedError)
  265. } else {
  266. assert.NoError(t, err)
  267. }
  268. })
  269. }
  270. }
  271. func TestHandler_AlertNotifyCapabilities(t *testing.T) {
  272. tests := []struct {
  273. name string
  274. inputBody wire.SNACFrame
  275. expectedError error
  276. }{
  277. {
  278. name: "success",
  279. inputBody: wire.SNACFrame{},
  280. },
  281. {
  282. name: "empty body",
  283. inputBody: wire.SNACFrame{},
  284. },
  285. }
  286. for _, tt := range tests {
  287. t.Run(tt.name, func(t *testing.T) {
  288. input := wire.SNACMessage{
  289. Frame: wire.SNACFrame{
  290. FoodGroup: wire.Alert,
  291. SubGroup: wire.AlertNotifyCapabilities,
  292. },
  293. Body: tt.inputBody,
  294. }
  295. h := Handler{
  296. RouteLogger: middleware.RouteLogger{
  297. Logger: slog.Default(),
  298. },
  299. }
  300. buf := &bytes.Buffer{}
  301. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  302. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, nil, config.Listener{})
  303. if tt.expectedError != nil {
  304. assert.ErrorIs(t, err, tt.expectedError)
  305. } else {
  306. assert.NoError(t, err)
  307. }
  308. })
  309. }
  310. }
  311. func TestHandler_AlertNotifyDisplayCapabilities(t *testing.T) {
  312. tests := []struct {
  313. name string
  314. inputBody wire.SNACFrame
  315. expectedError error
  316. }{
  317. {
  318. name: "success",
  319. inputBody: wire.SNACFrame{},
  320. },
  321. {
  322. name: "empty body",
  323. inputBody: wire.SNACFrame{},
  324. },
  325. }
  326. for _, tt := range tests {
  327. t.Run(tt.name, func(t *testing.T) {
  328. input := wire.SNACMessage{
  329. Frame: wire.SNACFrame{
  330. FoodGroup: wire.Alert,
  331. SubGroup: wire.AlertNotifyDisplayCapabilities,
  332. },
  333. Body: tt.inputBody,
  334. }
  335. h := Handler{
  336. RouteLogger: middleware.RouteLogger{
  337. Logger: slog.Default(),
  338. },
  339. }
  340. buf := &bytes.Buffer{}
  341. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  342. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, nil, config.Listener{})
  343. if tt.expectedError != nil {
  344. assert.ErrorIs(t, err, tt.expectedError)
  345. } else {
  346. assert.NoError(t, err)
  347. }
  348. })
  349. }
  350. }
  351. func TestHandler_BARTDownloadQuery(t *testing.T) {
  352. tests := []struct {
  353. name string
  354. inputBody wire.SNAC_0x10_0x04_BARTDownloadQuery
  355. serviceError error
  356. responseError error
  357. expectedError error
  358. }{
  359. {
  360. name: "success",
  361. inputBody: wire.SNAC_0x10_0x04_BARTDownloadQuery{},
  362. },
  363. {
  364. name: "service error",
  365. inputBody: wire.SNAC_0x10_0x04_BARTDownloadQuery{},
  366. serviceError: assert.AnError,
  367. expectedError: assert.AnError,
  368. },
  369. {
  370. name: "response writer error",
  371. inputBody: wire.SNAC_0x10_0x04_BARTDownloadQuery{},
  372. responseError: assert.AnError,
  373. expectedError: assert.AnError,
  374. },
  375. }
  376. for _, tt := range tests {
  377. t.Run(tt.name, func(t *testing.T) {
  378. input := wire.SNACMessage{
  379. Frame: wire.SNACFrame{
  380. FoodGroup: wire.BART,
  381. SubGroup: wire.BARTDownloadQuery,
  382. },
  383. Body: tt.inputBody,
  384. }
  385. output := wire.SNACMessage{
  386. Frame: wire.SNACFrame{
  387. FoodGroup: wire.BART,
  388. SubGroup: wire.BARTDownloadReply,
  389. },
  390. Body: wire.SNAC_0x10_0x05_BARTDownloadReply{
  391. ScreenName: "the-screen-name",
  392. },
  393. }
  394. svc := newMockBARTService(t)
  395. svc.EXPECT().
  396. RetrieveItem(mock.Anything, input.Frame, input.Body).
  397. Return(output, tt.serviceError)
  398. h := Handler{
  399. BARTService: svc,
  400. RouteLogger: middleware.RouteLogger{
  401. Logger: slog.Default(),
  402. },
  403. }
  404. responseWriter := newMockResponseWriter(t)
  405. if tt.serviceError == nil {
  406. responseWriter.EXPECT().
  407. SendSNAC(output.Frame, output.Body).
  408. Return(tt.responseError)
  409. }
  410. buf := &bytes.Buffer{}
  411. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  412. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  413. if tt.expectedError != nil {
  414. assert.ErrorIs(t, err, tt.expectedError)
  415. } else {
  416. assert.NoError(t, err)
  417. }
  418. })
  419. }
  420. }
  421. func TestHandler_BARTDownload2Query(t *testing.T) {
  422. tests := []struct {
  423. name string
  424. inputBody wire.SNAC_0x10_0x06_BARTDownload2Query
  425. serviceError error
  426. responseError error
  427. expectedError error
  428. }{
  429. {
  430. name: "success",
  431. inputBody: wire.SNAC_0x10_0x06_BARTDownload2Query{},
  432. },
  433. {
  434. name: "service error",
  435. inputBody: wire.SNAC_0x10_0x06_BARTDownload2Query{},
  436. serviceError: assert.AnError,
  437. expectedError: assert.AnError,
  438. },
  439. {
  440. name: "response writer error",
  441. inputBody: wire.SNAC_0x10_0x06_BARTDownload2Query{},
  442. responseError: assert.AnError,
  443. expectedError: assert.AnError,
  444. },
  445. }
  446. for _, tt := range tests {
  447. t.Run(tt.name, func(t *testing.T) {
  448. input := wire.SNACMessage{
  449. Frame: wire.SNACFrame{
  450. FoodGroup: wire.BART,
  451. SubGroup: wire.BARTDownload2Query,
  452. },
  453. Body: tt.inputBody,
  454. }
  455. output := []wire.SNACMessage{
  456. {
  457. Frame: wire.SNACFrame{
  458. FoodGroup: wire.BART,
  459. SubGroup: wire.BARTDownload2Reply,
  460. },
  461. Body: wire.SNAC_0x10_0x07_BARTDownload2Reply{
  462. ScreenName: "the-screen-name",
  463. },
  464. },
  465. }
  466. svc := newMockBARTService(t)
  467. svc.EXPECT().
  468. RetrieveItemV2(mock.Anything, input.Frame, input.Body).
  469. Return(output, tt.serviceError)
  470. h := Handler{
  471. BARTService: svc,
  472. RouteLogger: middleware.RouteLogger{
  473. Logger: slog.Default(),
  474. },
  475. }
  476. responseWriter := newMockResponseWriter(t)
  477. if tt.serviceError == nil {
  478. for _, msg := range output {
  479. responseWriter.EXPECT().
  480. SendSNAC(msg.Frame, msg.Body).
  481. Return(tt.responseError)
  482. }
  483. }
  484. buf := &bytes.Buffer{}
  485. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  486. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  487. if tt.expectedError != nil {
  488. assert.ErrorIs(t, err, tt.expectedError)
  489. } else {
  490. assert.NoError(t, err)
  491. }
  492. })
  493. }
  494. }
  495. func TestHandler_BARTUploadQuery(t *testing.T) {
  496. tests := []struct {
  497. name string
  498. inputBody wire.SNAC_0x10_0x02_BARTUploadQuery
  499. serviceError error
  500. responseError error
  501. expectedError error
  502. }{
  503. {
  504. name: "success",
  505. inputBody: wire.SNAC_0x10_0x02_BARTUploadQuery{
  506. Type: 1,
  507. },
  508. },
  509. {
  510. name: "service error",
  511. inputBody: wire.SNAC_0x10_0x02_BARTUploadQuery{
  512. Type: 1,
  513. },
  514. serviceError: assert.AnError,
  515. expectedError: assert.AnError,
  516. },
  517. {
  518. name: "response writer error",
  519. inputBody: wire.SNAC_0x10_0x02_BARTUploadQuery{
  520. Type: 1,
  521. },
  522. responseError: assert.AnError,
  523. expectedError: assert.AnError,
  524. },
  525. }
  526. for _, tt := range tests {
  527. t.Run(tt.name, func(t *testing.T) {
  528. input := wire.SNACMessage{
  529. Frame: wire.SNACFrame{
  530. FoodGroup: wire.BART,
  531. SubGroup: wire.BARTUploadQuery,
  532. },
  533. Body: tt.inputBody,
  534. }
  535. output := wire.SNACMessage{
  536. Frame: wire.SNACFrame{
  537. FoodGroup: wire.BART,
  538. SubGroup: wire.BARTUploadReply,
  539. },
  540. Body: wire.SNAC_0x10_0x03_BARTUploadReply{
  541. Code: wire.BARTReplyCodesSuccess,
  542. },
  543. }
  544. svc := newMockBARTService(t)
  545. svc.EXPECT().
  546. UpsertItem(mock.Anything, mock.Anything, input.Frame, input.Body).
  547. Return(output, tt.serviceError)
  548. h := Handler{
  549. BARTService: svc,
  550. RouteLogger: middleware.RouteLogger{
  551. Logger: slog.Default(),
  552. },
  553. }
  554. responseWriter := newMockResponseWriter(t)
  555. if tt.serviceError == nil {
  556. responseWriter.EXPECT().
  557. SendSNAC(output.Frame, output.Body).
  558. Return(tt.responseError)
  559. }
  560. buf := &bytes.Buffer{}
  561. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  562. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  563. if tt.expectedError != nil {
  564. assert.ErrorIs(t, err, tt.expectedError)
  565. } else {
  566. assert.NoError(t, err)
  567. }
  568. })
  569. }
  570. }
  571. func TestHandler_BuddyRightsQuery(t *testing.T) {
  572. tests := []struct {
  573. name string
  574. inputBody wire.SNAC_0x03_0x02_BuddyRightsQuery
  575. responseError error
  576. expectedError error
  577. }{
  578. {
  579. name: "success",
  580. inputBody: wire.SNAC_0x03_0x02_BuddyRightsQuery{
  581. TLVRestBlock: wire.TLVRestBlock{
  582. TLVList: wire.TLVList{
  583. wire.NewTLVBE(0x01, uint16(1000)),
  584. },
  585. },
  586. },
  587. },
  588. {
  589. name: "response writer error",
  590. inputBody: wire.SNAC_0x03_0x02_BuddyRightsQuery{
  591. TLVRestBlock: wire.TLVRestBlock{
  592. TLVList: wire.TLVList{
  593. wire.NewTLVBE(0x01, uint16(1000)),
  594. },
  595. },
  596. },
  597. responseError: assert.AnError,
  598. expectedError: assert.AnError,
  599. },
  600. }
  601. for _, tt := range tests {
  602. t.Run(tt.name, func(t *testing.T) {
  603. input := wire.SNACMessage{
  604. Frame: wire.SNACFrame{
  605. FoodGroup: wire.Buddy,
  606. SubGroup: wire.BuddyRightsQuery,
  607. },
  608. Body: tt.inputBody,
  609. }
  610. output := wire.SNACMessage{
  611. Frame: wire.SNACFrame{
  612. FoodGroup: wire.Buddy,
  613. SubGroup: wire.BuddyRightsReply,
  614. },
  615. Body: wire.SNAC_0x03_0x03_BuddyRightsReply{
  616. TLVRestBlock: wire.TLVRestBlock{
  617. TLVList: wire.TLVList{
  618. wire.NewTLVBE(0x01, uint16(1000)),
  619. },
  620. },
  621. },
  622. }
  623. svc := newMockBuddyService(t)
  624. svc.EXPECT().
  625. RightsQuery(mock.Anything, input.Frame).
  626. Return(output)
  627. h := Handler{
  628. BuddyService: svc,
  629. RouteLogger: middleware.RouteLogger{
  630. Logger: slog.Default(),
  631. },
  632. }
  633. responseWriter := newMockResponseWriter(t)
  634. responseWriter.EXPECT().
  635. SendSNAC(output.Frame, output.Body).
  636. Return(tt.responseError)
  637. buf := &bytes.Buffer{}
  638. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  639. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  640. if tt.expectedError != nil {
  641. assert.ErrorIs(t, err, tt.expectedError)
  642. } else {
  643. assert.NoError(t, err)
  644. }
  645. })
  646. }
  647. }
  648. func TestHandler_BuddyAddBuddies(t *testing.T) {
  649. tests := []struct {
  650. name string
  651. inputBody wire.SNAC_0x03_0x04_BuddyAddBuddies
  652. serviceError error
  653. expectedError error
  654. }{
  655. {
  656. name: "success",
  657. inputBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  658. Buddies: []struct {
  659. ScreenName string `oscar:"len_prefix=uint8"`
  660. }{
  661. {
  662. ScreenName: "user1",
  663. },
  664. },
  665. },
  666. },
  667. {
  668. name: "service error",
  669. inputBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  670. Buddies: []struct {
  671. ScreenName string `oscar:"len_prefix=uint8"`
  672. }{
  673. {
  674. ScreenName: "user1",
  675. },
  676. },
  677. },
  678. serviceError: assert.AnError,
  679. expectedError: assert.AnError,
  680. },
  681. }
  682. for _, tt := range tests {
  683. t.Run(tt.name, func(t *testing.T) {
  684. input := wire.SNACMessage{
  685. Frame: wire.SNACFrame{
  686. FoodGroup: wire.Buddy,
  687. SubGroup: wire.BuddyAddBuddies,
  688. },
  689. Body: tt.inputBody,
  690. }
  691. svc := newMockBuddyService(t)
  692. svc.EXPECT().
  693. AddBuddies(mock.Anything, mock.Anything, input.Body).
  694. Return(tt.serviceError)
  695. h := Handler{
  696. BuddyService: svc,
  697. RouteLogger: middleware.RouteLogger{
  698. Logger: slog.Default(),
  699. },
  700. }
  701. responseWriter := newMockResponseWriter(t)
  702. buf := &bytes.Buffer{}
  703. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  704. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  705. if tt.expectedError != nil {
  706. assert.ErrorIs(t, err, tt.expectedError)
  707. } else {
  708. assert.NoError(t, err)
  709. }
  710. })
  711. }
  712. }
  713. func TestHandler_BuddyDelBuddies(t *testing.T) {
  714. tests := []struct {
  715. name string
  716. inputBody wire.SNAC_0x03_0x05_BuddyDelBuddies
  717. serviceError error
  718. expectedError error
  719. }{
  720. {
  721. name: "success",
  722. inputBody: wire.SNAC_0x03_0x05_BuddyDelBuddies{
  723. Buddies: []struct {
  724. ScreenName string `oscar:"len_prefix=uint8"`
  725. }{
  726. {
  727. ScreenName: "user1",
  728. },
  729. },
  730. },
  731. },
  732. {
  733. name: "service error",
  734. inputBody: wire.SNAC_0x03_0x05_BuddyDelBuddies{
  735. Buddies: []struct {
  736. ScreenName string `oscar:"len_prefix=uint8"`
  737. }{
  738. {
  739. ScreenName: "user1",
  740. },
  741. },
  742. },
  743. serviceError: assert.AnError,
  744. expectedError: assert.AnError,
  745. },
  746. }
  747. for _, tt := range tests {
  748. t.Run(tt.name, func(t *testing.T) {
  749. input := wire.SNACMessage{
  750. Frame: wire.SNACFrame{
  751. FoodGroup: wire.Buddy,
  752. SubGroup: wire.BuddyDelBuddies,
  753. },
  754. Body: tt.inputBody,
  755. }
  756. svc := newMockBuddyService(t)
  757. svc.EXPECT().
  758. DelBuddies(mock.Anything, mock.Anything, input.Body).
  759. Return(tt.serviceError)
  760. h := Handler{
  761. BuddyService: svc,
  762. RouteLogger: middleware.RouteLogger{
  763. Logger: slog.Default(),
  764. },
  765. }
  766. responseWriter := newMockResponseWriter(t)
  767. buf := &bytes.Buffer{}
  768. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  769. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  770. if tt.expectedError != nil {
  771. assert.ErrorIs(t, err, tt.expectedError)
  772. } else {
  773. assert.NoError(t, err)
  774. }
  775. })
  776. }
  777. }
  778. func TestHandler_BuddyAddTempBuddies(t *testing.T) {
  779. tests := []struct {
  780. name string
  781. inputBody wire.SNAC_0x03_0x0F_BuddyAddTempBuddies
  782. serviceError error
  783. expectedError error
  784. }{
  785. {
  786. name: "success",
  787. inputBody: wire.SNAC_0x03_0x0F_BuddyAddTempBuddies{
  788. Buddies: []struct {
  789. ScreenName string `oscar:"len_prefix=uint8"`
  790. }{
  791. {
  792. ScreenName: "user1",
  793. },
  794. },
  795. },
  796. },
  797. {
  798. name: "service error",
  799. inputBody: wire.SNAC_0x03_0x0F_BuddyAddTempBuddies{
  800. Buddies: []struct {
  801. ScreenName string `oscar:"len_prefix=uint8"`
  802. }{
  803. {
  804. ScreenName: "user1",
  805. },
  806. },
  807. },
  808. serviceError: assert.AnError,
  809. expectedError: assert.AnError,
  810. },
  811. }
  812. for _, tt := range tests {
  813. t.Run(tt.name, func(t *testing.T) {
  814. input := wire.SNACMessage{
  815. Frame: wire.SNACFrame{
  816. FoodGroup: wire.Buddy,
  817. SubGroup: wire.BuddyAddTempBuddies,
  818. },
  819. Body: tt.inputBody,
  820. }
  821. svc := newMockBuddyService(t)
  822. svc.EXPECT().
  823. AddTempBuddies(mock.Anything, mock.Anything, input.Body).
  824. Return(tt.serviceError)
  825. h := Handler{
  826. BuddyService: svc,
  827. RouteLogger: middleware.RouteLogger{
  828. Logger: slog.Default(),
  829. },
  830. }
  831. responseWriter := newMockResponseWriter(t)
  832. buf := &bytes.Buffer{}
  833. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  834. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  835. if tt.expectedError != nil {
  836. assert.ErrorIs(t, err, tt.expectedError)
  837. } else {
  838. assert.NoError(t, err)
  839. }
  840. })
  841. }
  842. }
  843. func TestHandler_BuddyDelTempBuddies(t *testing.T) {
  844. tests := []struct {
  845. name string
  846. inputBody wire.SNAC_0x03_0x10_BuddyDelTempBuddies
  847. serviceError error
  848. expectedError error
  849. }{
  850. {
  851. name: "success",
  852. inputBody: wire.SNAC_0x03_0x10_BuddyDelTempBuddies{
  853. Buddies: []struct {
  854. ScreenName string `oscar:"len_prefix=uint8"`
  855. }{
  856. {
  857. ScreenName: "user1",
  858. },
  859. },
  860. },
  861. },
  862. {
  863. name: "service error",
  864. inputBody: wire.SNAC_0x03_0x10_BuddyDelTempBuddies{
  865. Buddies: []struct {
  866. ScreenName string `oscar:"len_prefix=uint8"`
  867. }{
  868. {
  869. ScreenName: "user1",
  870. },
  871. },
  872. },
  873. serviceError: assert.AnError,
  874. expectedError: assert.AnError,
  875. },
  876. }
  877. for _, tt := range tests {
  878. t.Run(tt.name, func(t *testing.T) {
  879. input := wire.SNACMessage{
  880. Frame: wire.SNACFrame{
  881. FoodGroup: wire.Buddy,
  882. SubGroup: wire.BuddyDelTempBuddies,
  883. },
  884. Body: tt.inputBody,
  885. }
  886. svc := newMockBuddyService(t)
  887. svc.EXPECT().
  888. DelTempBuddies(mock.Anything, mock.Anything, input.Body).
  889. Return(tt.serviceError)
  890. h := Handler{
  891. BuddyService: svc,
  892. RouteLogger: middleware.RouteLogger{
  893. Logger: slog.Default(),
  894. },
  895. }
  896. responseWriter := newMockResponseWriter(t)
  897. buf := &bytes.Buffer{}
  898. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  899. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  900. if tt.expectedError != nil {
  901. assert.ErrorIs(t, err, tt.expectedError)
  902. } else {
  903. assert.NoError(t, err)
  904. }
  905. })
  906. }
  907. }
  908. func TestHandler_ChatNavCreateRoom(t *testing.T) {
  909. tests := []struct {
  910. name string
  911. inputBody wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate
  912. serviceError error
  913. responseError error
  914. expectedError error
  915. }{
  916. {
  917. name: "success",
  918. inputBody: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  919. Exchange: 1,
  920. },
  921. },
  922. {
  923. name: "service error",
  924. inputBody: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  925. Exchange: 1,
  926. },
  927. serviceError: assert.AnError,
  928. expectedError: assert.AnError,
  929. },
  930. {
  931. name: "response writer error",
  932. inputBody: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  933. Exchange: 1,
  934. },
  935. responseError: assert.AnError,
  936. expectedError: assert.AnError,
  937. },
  938. }
  939. for _, tt := range tests {
  940. t.Run(tt.name, func(t *testing.T) {
  941. input := wire.SNACMessage{
  942. Frame: wire.SNACFrame{
  943. FoodGroup: wire.ChatNav,
  944. SubGroup: wire.ChatNavCreateRoom,
  945. },
  946. Body: tt.inputBody,
  947. }
  948. output := wire.SNACMessage{
  949. Frame: wire.SNACFrame{
  950. FoodGroup: wire.ChatNav,
  951. SubGroup: wire.ChatNavNavInfo,
  952. },
  953. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  954. }
  955. instance := state.NewSession().AddInstance()
  956. svc := newMockChatNavService(t)
  957. svc.EXPECT().
  958. CreateRoom(mock.Anything, instance, input.Frame, input.Body).
  959. Return(output, tt.serviceError)
  960. h := Handler{
  961. ChatNavService: svc,
  962. RouteLogger: middleware.RouteLogger{
  963. Logger: slog.Default(),
  964. },
  965. }
  966. responseWriter := newMockResponseWriter(t)
  967. if tt.serviceError == nil {
  968. responseWriter.EXPECT().
  969. SendSNAC(output.Frame, output.Body).
  970. Return(tt.responseError)
  971. }
  972. buf := &bytes.Buffer{}
  973. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  974. err := h.Handle(context.TODO(), wire.BOS, instance, input.Frame, buf, responseWriter, config.Listener{})
  975. if tt.expectedError != nil {
  976. assert.ErrorIs(t, err, tt.expectedError)
  977. } else {
  978. assert.NoError(t, err)
  979. }
  980. })
  981. }
  982. }
  983. func TestHandler_ChatNavCreateRoom_ReadErr(t *testing.T) {
  984. input := wire.SNACMessage{
  985. Frame: wire.SNACFrame{
  986. FoodGroup: wire.ChatNav,
  987. SubGroup: wire.ChatNavCreateRoom,
  988. },
  989. Body: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  990. Exchange: 1,
  991. },
  992. }
  993. output := wire.SNACMessage{
  994. Frame: wire.SNACFrame{
  995. FoodGroup: wire.ChatNav,
  996. SubGroup: wire.ChatNavNavInfo,
  997. },
  998. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  999. }
  1000. instance := state.NewSession().AddInstance()
  1001. svc := newMockChatNavService(t)
  1002. svc.EXPECT().
  1003. CreateRoom(mock.Anything, instance, input.Frame, input.Body).
  1004. Return(output, nil)
  1005. h := Handler{
  1006. ChatNavService: svc,
  1007. RouteLogger: middleware.RouteLogger{
  1008. Logger: slog.Default(),
  1009. },
  1010. }
  1011. ss := newMockResponseWriter(t)
  1012. ss.EXPECT().
  1013. SendSNAC(output.Frame, output.Body).
  1014. Return(nil)
  1015. buf := &bytes.Buffer{}
  1016. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1017. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, instance, input.Frame, buf, ss, config.Listener{}))
  1018. }
  1019. func TestHandler_ChatNavRequestChatRights(t *testing.T) {
  1020. tests := []struct {
  1021. name string
  1022. inputBody struct{}
  1023. responseError error
  1024. expectedError error
  1025. }{
  1026. {
  1027. name: "success",
  1028. inputBody: struct{}{},
  1029. },
  1030. {
  1031. name: "response writer error",
  1032. inputBody: struct{}{},
  1033. responseError: assert.AnError,
  1034. expectedError: assert.AnError,
  1035. },
  1036. }
  1037. for _, tt := range tests {
  1038. t.Run(tt.name, func(t *testing.T) {
  1039. input := wire.SNACMessage{
  1040. Frame: wire.SNACFrame{
  1041. FoodGroup: wire.ChatNav,
  1042. SubGroup: wire.ChatNavRequestChatRights,
  1043. },
  1044. Body: tt.inputBody,
  1045. }
  1046. output := wire.SNACMessage{
  1047. Frame: wire.SNACFrame{
  1048. FoodGroup: wire.ChatNav,
  1049. SubGroup: wire.ChatNavNavInfo,
  1050. },
  1051. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  1052. }
  1053. svc := newMockChatNavService(t)
  1054. svc.EXPECT().
  1055. RequestChatRights(mock.Anything, input.Frame).
  1056. Return(output)
  1057. h := Handler{
  1058. ChatNavService: svc,
  1059. RouteLogger: middleware.RouteLogger{
  1060. Logger: slog.Default(),
  1061. },
  1062. }
  1063. responseWriter := newMockResponseWriter(t)
  1064. responseWriter.EXPECT().
  1065. SendSNAC(output.Frame, output.Body).
  1066. Return(tt.responseError)
  1067. buf := &bytes.Buffer{}
  1068. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1069. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1070. if tt.expectedError != nil {
  1071. assert.ErrorIs(t, err, tt.expectedError)
  1072. } else {
  1073. assert.NoError(t, err)
  1074. }
  1075. })
  1076. }
  1077. }
  1078. func TestHandler_ChatNavRequestRoomInfo(t *testing.T) {
  1079. tests := []struct {
  1080. name string
  1081. inputBody wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo
  1082. serviceError error
  1083. responseError error
  1084. expectedError error
  1085. }{
  1086. {
  1087. name: "success",
  1088. inputBody: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  1089. Exchange: 1,
  1090. },
  1091. },
  1092. {
  1093. name: "service error",
  1094. inputBody: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  1095. Exchange: 1,
  1096. },
  1097. serviceError: assert.AnError,
  1098. expectedError: assert.AnError,
  1099. },
  1100. {
  1101. name: "response writer error",
  1102. inputBody: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  1103. Exchange: 1,
  1104. },
  1105. responseError: assert.AnError,
  1106. expectedError: assert.AnError,
  1107. },
  1108. }
  1109. for _, tt := range tests {
  1110. t.Run(tt.name, func(t *testing.T) {
  1111. input := wire.SNACMessage{
  1112. Frame: wire.SNACFrame{
  1113. FoodGroup: wire.ChatNav,
  1114. SubGroup: wire.ChatNavRequestRoomInfo,
  1115. },
  1116. Body: tt.inputBody,
  1117. }
  1118. output := wire.SNACMessage{
  1119. Frame: wire.SNACFrame{
  1120. FoodGroup: wire.ChatNav,
  1121. SubGroup: wire.ChatNavNavInfo,
  1122. },
  1123. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  1124. }
  1125. svc := newMockChatNavService(t)
  1126. svc.EXPECT().
  1127. RequestRoomInfo(mock.Anything, input.Frame, input.Body).
  1128. Return(output, tt.serviceError)
  1129. h := Handler{
  1130. ChatNavService: svc,
  1131. RouteLogger: middleware.RouteLogger{
  1132. Logger: slog.Default(),
  1133. },
  1134. }
  1135. responseWriter := newMockResponseWriter(t)
  1136. if tt.serviceError == nil {
  1137. responseWriter.EXPECT().
  1138. SendSNAC(output.Frame, output.Body).
  1139. Return(tt.responseError)
  1140. }
  1141. buf := &bytes.Buffer{}
  1142. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1143. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1144. if tt.expectedError != nil {
  1145. assert.ErrorIs(t, err, tt.expectedError)
  1146. } else {
  1147. assert.NoError(t, err)
  1148. }
  1149. })
  1150. }
  1151. }
  1152. func TestHandler_ChatNavRequestExchangeInfo(t *testing.T) {
  1153. tests := []struct {
  1154. name string
  1155. inputBody wire.SNAC_0x0D_0x03_ChatNavRequestExchangeInfo
  1156. serviceError error
  1157. responseError error
  1158. expectedError error
  1159. }{
  1160. {
  1161. name: "success",
  1162. inputBody: wire.SNAC_0x0D_0x03_ChatNavRequestExchangeInfo{
  1163. Exchange: 4,
  1164. },
  1165. },
  1166. {
  1167. name: "service error",
  1168. inputBody: wire.SNAC_0x0D_0x03_ChatNavRequestExchangeInfo{
  1169. Exchange: 4,
  1170. },
  1171. serviceError: assert.AnError,
  1172. expectedError: assert.AnError,
  1173. },
  1174. {
  1175. name: "response writer error",
  1176. inputBody: wire.SNAC_0x0D_0x03_ChatNavRequestExchangeInfo{
  1177. Exchange: 4,
  1178. },
  1179. responseError: assert.AnError,
  1180. expectedError: assert.AnError,
  1181. },
  1182. }
  1183. for _, tt := range tests {
  1184. t.Run(tt.name, func(t *testing.T) {
  1185. input := wire.SNACMessage{
  1186. Frame: wire.SNACFrame{
  1187. FoodGroup: wire.ChatNav,
  1188. SubGroup: wire.ChatNavRequestExchangeInfo,
  1189. },
  1190. Body: tt.inputBody,
  1191. }
  1192. output := wire.SNACMessage{
  1193. Frame: wire.SNACFrame{
  1194. FoodGroup: wire.ChatNav,
  1195. SubGroup: wire.ChatNavNavInfo,
  1196. },
  1197. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  1198. }
  1199. svc := newMockChatNavService(t)
  1200. svc.EXPECT().
  1201. ExchangeInfo(mock.Anything, input.Frame, input.Body).
  1202. Return(output, tt.serviceError)
  1203. h := Handler{
  1204. ChatNavService: svc,
  1205. RouteLogger: middleware.RouteLogger{
  1206. Logger: slog.Default(),
  1207. },
  1208. }
  1209. responseWriter := newMockResponseWriter(t)
  1210. if tt.serviceError == nil {
  1211. responseWriter.EXPECT().
  1212. SendSNAC(output.Frame, output.Body).
  1213. Return(tt.responseError)
  1214. }
  1215. buf := &bytes.Buffer{}
  1216. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1217. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1218. if tt.expectedError != nil {
  1219. assert.ErrorIs(t, err, tt.expectedError)
  1220. } else {
  1221. assert.NoError(t, err)
  1222. }
  1223. })
  1224. }
  1225. }
  1226. func TestHandler_ChatChannelMsgToHost(t *testing.T) {
  1227. tests := []struct {
  1228. name string
  1229. inputBody wire.SNAC_0x0E_0x05_ChatChannelMsgToHost
  1230. serviceResponse *wire.SNACMessage
  1231. serviceError error
  1232. responseError error
  1233. expectedError error
  1234. }{
  1235. {
  1236. name: "success with reflected response",
  1237. inputBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1238. Channel: 4,
  1239. },
  1240. serviceResponse: &wire.SNACMessage{
  1241. Frame: wire.SNACFrame{
  1242. FoodGroup: wire.Chat,
  1243. SubGroup: wire.ChatChannelMsgToClient,
  1244. },
  1245. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  1246. Channel: 4,
  1247. },
  1248. },
  1249. },
  1250. {
  1251. name: "service error with reflected response",
  1252. inputBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1253. Channel: 4,
  1254. },
  1255. serviceResponse: &wire.SNACMessage{
  1256. Frame: wire.SNACFrame{
  1257. FoodGroup: wire.Chat,
  1258. SubGroup: wire.ChatChannelMsgToClient,
  1259. },
  1260. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  1261. Channel: 4,
  1262. },
  1263. },
  1264. serviceError: assert.AnError,
  1265. expectedError: assert.AnError,
  1266. },
  1267. {
  1268. name: "response writer error with reflected response",
  1269. inputBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1270. Channel: 4,
  1271. },
  1272. serviceResponse: &wire.SNACMessage{
  1273. Frame: wire.SNACFrame{
  1274. FoodGroup: wire.Chat,
  1275. SubGroup: wire.ChatChannelMsgToClient,
  1276. },
  1277. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  1278. Channel: 4,
  1279. },
  1280. },
  1281. responseError: assert.AnError,
  1282. expectedError: assert.AnError,
  1283. },
  1284. {
  1285. name: "success without reflected response",
  1286. inputBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1287. Channel: 4,
  1288. },
  1289. serviceResponse: nil, // nil response means no reflection back to caller
  1290. },
  1291. {
  1292. name: "service error without reflected response",
  1293. inputBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1294. Channel: 4,
  1295. },
  1296. serviceResponse: nil,
  1297. serviceError: assert.AnError,
  1298. expectedError: assert.AnError,
  1299. },
  1300. }
  1301. for _, tt := range tests {
  1302. t.Run(tt.name, func(t *testing.T) {
  1303. input := wire.SNACMessage{
  1304. Frame: wire.SNACFrame{
  1305. FoodGroup: wire.Chat,
  1306. SubGroup: wire.ChatChannelMsgToHost,
  1307. },
  1308. Body: tt.inputBody,
  1309. }
  1310. svc := newMockChatService(t)
  1311. svc.EXPECT().
  1312. ChannelMsgToHost(mock.Anything, mock.Anything, input.Frame, input.Body).
  1313. Return(tt.serviceResponse, tt.serviceError)
  1314. h := Handler{
  1315. ChatService: svc,
  1316. RouteLogger: middleware.RouteLogger{
  1317. Logger: slog.Default(),
  1318. },
  1319. }
  1320. responseWriter := newMockResponseWriter(t)
  1321. if tt.serviceError == nil && tt.serviceResponse != nil {
  1322. responseWriter.EXPECT().
  1323. SendSNAC(tt.serviceResponse.Frame, tt.serviceResponse.Body).
  1324. Return(tt.responseError)
  1325. }
  1326. buf := &bytes.Buffer{}
  1327. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1328. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1329. if tt.expectedError != nil {
  1330. assert.ErrorIs(t, err, tt.expectedError)
  1331. } else {
  1332. assert.NoError(t, err)
  1333. }
  1334. })
  1335. }
  1336. }
  1337. func TestHandler_FeedbagDeleteItem(t *testing.T) {
  1338. tests := []struct {
  1339. name string
  1340. inputBody wire.SNAC_0x13_0x0A_FeedbagDeleteItem
  1341. expectedOutput *wire.SNACMessage
  1342. serviceError error
  1343. responseError error
  1344. expectedError error
  1345. }{
  1346. {
  1347. name: "success",
  1348. inputBody: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
  1349. Items: []wire.FeedbagItem{
  1350. {
  1351. Name: "my-item",
  1352. },
  1353. },
  1354. },
  1355. expectedOutput: &wire.SNACMessage{
  1356. Frame: wire.SNACFrame{
  1357. FoodGroup: wire.Feedbag,
  1358. SubGroup: wire.FeedbagStatus,
  1359. },
  1360. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1361. Results: []uint16{1234},
  1362. },
  1363. },
  1364. },
  1365. {
  1366. name: "success (nil output)",
  1367. inputBody: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
  1368. Items: []wire.FeedbagItem{
  1369. {
  1370. Name: "my-item",
  1371. },
  1372. },
  1373. },
  1374. expectedOutput: nil,
  1375. },
  1376. {
  1377. name: "service error",
  1378. inputBody: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
  1379. Items: []wire.FeedbagItem{
  1380. {
  1381. Name: "my-item",
  1382. },
  1383. },
  1384. },
  1385. expectedOutput: &wire.SNACMessage{
  1386. Frame: wire.SNACFrame{
  1387. FoodGroup: wire.Feedbag,
  1388. SubGroup: wire.FeedbagStatus,
  1389. },
  1390. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1391. Results: []uint16{1234},
  1392. },
  1393. },
  1394. serviceError: assert.AnError,
  1395. expectedError: assert.AnError,
  1396. },
  1397. {
  1398. name: "response writer error",
  1399. inputBody: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
  1400. Items: []wire.FeedbagItem{
  1401. {
  1402. Name: "my-item",
  1403. },
  1404. },
  1405. },
  1406. expectedOutput: &wire.SNACMessage{
  1407. Frame: wire.SNACFrame{
  1408. FoodGroup: wire.Feedbag,
  1409. SubGroup: wire.FeedbagStatus,
  1410. },
  1411. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1412. Results: []uint16{1234},
  1413. },
  1414. },
  1415. responseError: assert.AnError,
  1416. expectedError: assert.AnError,
  1417. },
  1418. }
  1419. for _, tt := range tests {
  1420. t.Run(tt.name, func(t *testing.T) {
  1421. input := wire.SNACMessage{
  1422. Frame: wire.SNACFrame{
  1423. FoodGroup: wire.Feedbag,
  1424. SubGroup: wire.FeedbagDeleteItem,
  1425. },
  1426. Body: tt.inputBody,
  1427. }
  1428. svc := newMockFeedbagService(t)
  1429. svc.EXPECT().
  1430. DeleteItem(mock.Anything, mock.Anything, input.Frame, input.Body).
  1431. Return(tt.expectedOutput, tt.serviceError)
  1432. h := Handler{
  1433. FeedbagService: svc,
  1434. RouteLogger: middleware.RouteLogger{
  1435. Logger: slog.Default(),
  1436. },
  1437. }
  1438. responseWriter := newMockResponseWriter(t)
  1439. if tt.serviceError == nil && tt.expectedOutput != nil {
  1440. responseWriter.EXPECT().
  1441. SendSNAC(tt.expectedOutput.Frame, tt.expectedOutput.Body).
  1442. Return(tt.responseError)
  1443. }
  1444. buf := &bytes.Buffer{}
  1445. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1446. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1447. if tt.expectedError != nil {
  1448. assert.ErrorIs(t, err, tt.expectedError)
  1449. } else {
  1450. assert.NoError(t, err)
  1451. }
  1452. })
  1453. }
  1454. }
  1455. func TestHandler_FeedbagEndCluster(t *testing.T) {
  1456. tests := []struct {
  1457. name string
  1458. inputBody struct{}
  1459. expectedError error
  1460. }{
  1461. {
  1462. name: "success",
  1463. inputBody: struct{}{},
  1464. },
  1465. }
  1466. for _, tt := range tests {
  1467. t.Run(tt.name, func(t *testing.T) {
  1468. input := wire.SNACMessage{
  1469. Frame: wire.SNACFrame{
  1470. FoodGroup: wire.Feedbag,
  1471. SubGroup: wire.FeedbagEndCluster,
  1472. },
  1473. Body: tt.inputBody,
  1474. }
  1475. svc := newMockFeedbagService(t)
  1476. h := Handler{
  1477. FeedbagService: svc,
  1478. RouteLogger: middleware.RouteLogger{
  1479. Logger: slog.Default(),
  1480. },
  1481. }
  1482. responseWriter := newMockResponseWriter(t)
  1483. buf := &bytes.Buffer{}
  1484. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1485. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1486. if tt.expectedError != nil {
  1487. assert.ErrorIs(t, err, tt.expectedError)
  1488. } else {
  1489. assert.NoError(t, err)
  1490. }
  1491. })
  1492. }
  1493. }
  1494. func TestHandler_FeedbagInsertItem(t *testing.T) {
  1495. tests := []struct {
  1496. name string
  1497. inputBody wire.SNAC_0x13_0x08_FeedbagInsertItem
  1498. expectedOutput *wire.SNACMessage
  1499. serviceError error
  1500. responseError error
  1501. expectedError error
  1502. }{
  1503. {
  1504. name: "success",
  1505. inputBody: wire.SNAC_0x13_0x08_FeedbagInsertItem{
  1506. Items: []wire.FeedbagItem{
  1507. {
  1508. Name: "my-item",
  1509. },
  1510. },
  1511. },
  1512. expectedOutput: nil,
  1513. },
  1514. {
  1515. name: "success (non-nil output)",
  1516. inputBody: wire.SNAC_0x13_0x08_FeedbagInsertItem{
  1517. Items: []wire.FeedbagItem{
  1518. {
  1519. Name: "my-item",
  1520. },
  1521. },
  1522. },
  1523. expectedOutput: &wire.SNACMessage{
  1524. Frame: wire.SNACFrame{
  1525. FoodGroup: wire.Feedbag,
  1526. SubGroup: wire.FeedbagStatus,
  1527. },
  1528. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1529. Results: []uint16{1234},
  1530. },
  1531. },
  1532. },
  1533. {
  1534. name: "service error",
  1535. inputBody: wire.SNAC_0x13_0x08_FeedbagInsertItem{
  1536. Items: []wire.FeedbagItem{
  1537. {
  1538. Name: "my-item",
  1539. },
  1540. },
  1541. },
  1542. expectedOutput: &wire.SNACMessage{
  1543. Frame: wire.SNACFrame{
  1544. FoodGroup: wire.Feedbag,
  1545. SubGroup: wire.FeedbagStatus,
  1546. },
  1547. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1548. Results: []uint16{1234},
  1549. },
  1550. },
  1551. serviceError: assert.AnError,
  1552. expectedError: assert.AnError,
  1553. },
  1554. {
  1555. name: "response writer error",
  1556. inputBody: wire.SNAC_0x13_0x08_FeedbagInsertItem{
  1557. Items: []wire.FeedbagItem{
  1558. {
  1559. Name: "my-item",
  1560. },
  1561. },
  1562. },
  1563. expectedOutput: &wire.SNACMessage{
  1564. Frame: wire.SNACFrame{
  1565. FoodGroup: wire.Feedbag,
  1566. SubGroup: wire.FeedbagStatus,
  1567. },
  1568. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1569. Results: []uint16{1234},
  1570. },
  1571. },
  1572. responseError: assert.AnError,
  1573. expectedError: assert.AnError,
  1574. },
  1575. }
  1576. for _, tt := range tests {
  1577. t.Run(tt.name, func(t *testing.T) {
  1578. input := wire.SNACMessage{
  1579. Frame: wire.SNACFrame{
  1580. FoodGroup: wire.Feedbag,
  1581. SubGroup: wire.FeedbagInsertItem,
  1582. },
  1583. Body: tt.inputBody,
  1584. }
  1585. svc := newMockFeedbagService(t)
  1586. svc.EXPECT().
  1587. UpsertItem(mock.Anything, mock.Anything, input.Frame, tt.inputBody.Items).
  1588. Return(tt.expectedOutput, tt.serviceError)
  1589. h := Handler{
  1590. FeedbagService: svc,
  1591. RouteLogger: middleware.RouteLogger{
  1592. Logger: slog.Default(),
  1593. },
  1594. }
  1595. responseWriter := newMockResponseWriter(t)
  1596. if tt.serviceError == nil && tt.expectedOutput != nil {
  1597. responseWriter.EXPECT().
  1598. SendSNAC(tt.expectedOutput.Frame, tt.expectedOutput.Body).
  1599. Return(tt.responseError)
  1600. }
  1601. buf := &bytes.Buffer{}
  1602. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1603. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1604. if tt.expectedError != nil {
  1605. assert.ErrorIs(t, err, tt.expectedError)
  1606. } else {
  1607. assert.NoError(t, err)
  1608. }
  1609. })
  1610. }
  1611. }
  1612. func TestHandler_FeedbagQuery(t *testing.T) {
  1613. tests := []struct {
  1614. name string
  1615. inputBody wire.SNAC_0x13_0x02_FeedbagRightsQuery
  1616. serviceError error
  1617. responseError error
  1618. expectedError error
  1619. }{
  1620. {
  1621. name: "success",
  1622. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1623. TLVRestBlock: wire.TLVRestBlock{
  1624. TLVList: wire.TLVList{
  1625. {
  1626. Tag: 0x01,
  1627. Value: []byte{1, 2, 3, 4},
  1628. },
  1629. },
  1630. },
  1631. },
  1632. },
  1633. {
  1634. name: "service error",
  1635. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1636. TLVRestBlock: wire.TLVRestBlock{
  1637. TLVList: wire.TLVList{
  1638. {
  1639. Tag: 0x01,
  1640. Value: []byte{1, 2, 3, 4},
  1641. },
  1642. },
  1643. },
  1644. },
  1645. serviceError: assert.AnError,
  1646. expectedError: assert.AnError,
  1647. },
  1648. {
  1649. name: "response writer error",
  1650. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1651. TLVRestBlock: wire.TLVRestBlock{
  1652. TLVList: wire.TLVList{
  1653. {
  1654. Tag: 0x01,
  1655. Value: []byte{1, 2, 3, 4},
  1656. },
  1657. },
  1658. },
  1659. },
  1660. responseError: assert.AnError,
  1661. expectedError: assert.AnError,
  1662. },
  1663. }
  1664. for _, tt := range tests {
  1665. t.Run(tt.name, func(t *testing.T) {
  1666. input := wire.SNACMessage{
  1667. Frame: wire.SNACFrame{
  1668. FoodGroup: wire.Feedbag,
  1669. SubGroup: wire.FeedbagQuery,
  1670. },
  1671. Body: tt.inputBody,
  1672. }
  1673. output := wire.SNACMessage{
  1674. Frame: wire.SNACFrame{
  1675. FoodGroup: wire.Feedbag,
  1676. SubGroup: wire.FeedbagReply,
  1677. },
  1678. Body: wire.SNAC_0x13_0x06_FeedbagReply{
  1679. Version: 4,
  1680. },
  1681. }
  1682. svc := newMockFeedbagService(t)
  1683. svc.EXPECT().
  1684. Query(mock.Anything, mock.Anything, input.Frame).
  1685. Return(output, tt.serviceError)
  1686. h := Handler{
  1687. FeedbagService: svc,
  1688. RouteLogger: middleware.RouteLogger{
  1689. Logger: slog.Default(),
  1690. },
  1691. }
  1692. responseWriter := newMockResponseWriter(t)
  1693. if tt.serviceError == nil {
  1694. responseWriter.EXPECT().
  1695. SendSNAC(output.Frame, output.Body).
  1696. Return(tt.responseError)
  1697. }
  1698. buf := &bytes.Buffer{}
  1699. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1700. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1701. if tt.expectedError != nil {
  1702. assert.ErrorIs(t, err, tt.expectedError)
  1703. } else {
  1704. assert.NoError(t, err)
  1705. }
  1706. })
  1707. }
  1708. }
  1709. func TestHandler_FeedbagQueryIfModified(t *testing.T) {
  1710. tests := []struct {
  1711. name string
  1712. inputBody wire.SNAC_0x13_0x05_FeedbagQueryIfModified
  1713. serviceError error
  1714. responseError error
  1715. expectedError error
  1716. }{
  1717. {
  1718. name: "success",
  1719. inputBody: wire.SNAC_0x13_0x05_FeedbagQueryIfModified{
  1720. LastUpdate: 1234,
  1721. },
  1722. },
  1723. {
  1724. name: "service error",
  1725. inputBody: wire.SNAC_0x13_0x05_FeedbagQueryIfModified{
  1726. LastUpdate: 1234,
  1727. },
  1728. serviceError: assert.AnError,
  1729. expectedError: assert.AnError,
  1730. },
  1731. {
  1732. name: "response writer error",
  1733. inputBody: wire.SNAC_0x13_0x05_FeedbagQueryIfModified{
  1734. LastUpdate: 1234,
  1735. },
  1736. responseError: assert.AnError,
  1737. expectedError: assert.AnError,
  1738. },
  1739. }
  1740. for _, tt := range tests {
  1741. t.Run(tt.name, func(t *testing.T) {
  1742. input := wire.SNACMessage{
  1743. Frame: wire.SNACFrame{
  1744. FoodGroup: wire.Feedbag,
  1745. SubGroup: wire.FeedbagQueryIfModified,
  1746. },
  1747. Body: tt.inputBody,
  1748. }
  1749. output := wire.SNACMessage{
  1750. Frame: wire.SNACFrame{
  1751. FoodGroup: wire.Feedbag,
  1752. SubGroup: wire.FeedbagReply,
  1753. },
  1754. Body: wire.SNAC_0x13_0x06_FeedbagReply{
  1755. LastUpdate: 1234,
  1756. },
  1757. }
  1758. svc := newMockFeedbagService(t)
  1759. svc.EXPECT().
  1760. QueryIfModified(mock.Anything, mock.Anything, input.Frame, input.Body).
  1761. Return(output, tt.serviceError)
  1762. h := Handler{
  1763. FeedbagService: svc,
  1764. RouteLogger: middleware.RouteLogger{
  1765. Logger: slog.Default(),
  1766. },
  1767. }
  1768. responseWriter := newMockResponseWriter(t)
  1769. if tt.serviceError == nil {
  1770. responseWriter.EXPECT().
  1771. SendSNAC(output.Frame, output.Body).
  1772. Return(tt.responseError)
  1773. }
  1774. buf := &bytes.Buffer{}
  1775. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1776. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1777. if tt.expectedError != nil {
  1778. assert.ErrorIs(t, err, tt.expectedError)
  1779. } else {
  1780. assert.NoError(t, err)
  1781. }
  1782. })
  1783. }
  1784. }
  1785. func TestHandler_FeedbagRightsQuery(t *testing.T) {
  1786. tests := []struct {
  1787. name string
  1788. inputBody wire.SNAC_0x13_0x02_FeedbagRightsQuery
  1789. responseError error
  1790. expectedError error
  1791. }{
  1792. {
  1793. name: "success",
  1794. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1795. TLVRestBlock: wire.TLVRestBlock{
  1796. TLVList: wire.TLVList{
  1797. {
  1798. Tag: 0x01,
  1799. Value: []byte{1, 2, 3, 4},
  1800. },
  1801. },
  1802. },
  1803. },
  1804. },
  1805. {
  1806. name: "response writer error",
  1807. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1808. TLVRestBlock: wire.TLVRestBlock{
  1809. TLVList: wire.TLVList{
  1810. {
  1811. Tag: 0x01,
  1812. Value: []byte{1, 2, 3, 4},
  1813. },
  1814. },
  1815. },
  1816. },
  1817. responseError: assert.AnError,
  1818. expectedError: assert.AnError,
  1819. },
  1820. }
  1821. for _, tt := range tests {
  1822. t.Run(tt.name, func(t *testing.T) {
  1823. input := wire.SNACMessage{
  1824. Frame: wire.SNACFrame{
  1825. FoodGroup: wire.Feedbag,
  1826. SubGroup: wire.FeedbagRightsQuery,
  1827. },
  1828. Body: tt.inputBody,
  1829. }
  1830. output := wire.SNACMessage{
  1831. Frame: wire.SNACFrame{
  1832. FoodGroup: wire.Feedbag,
  1833. SubGroup: wire.FeedbagRightsReply,
  1834. },
  1835. Body: wire.SNAC_0x13_0x03_FeedbagRightsReply{
  1836. TLVRestBlock: wire.TLVRestBlock{
  1837. TLVList: wire.TLVList{
  1838. {
  1839. Tag: 0x01,
  1840. Value: []byte{1, 2, 3, 4},
  1841. },
  1842. },
  1843. },
  1844. },
  1845. }
  1846. svc := newMockFeedbagService(t)
  1847. svc.EXPECT().
  1848. RightsQuery(mock.Anything, input.Frame).
  1849. Return(output)
  1850. h := Handler{
  1851. FeedbagService: svc,
  1852. RouteLogger: middleware.RouteLogger{
  1853. Logger: slog.Default(),
  1854. },
  1855. }
  1856. responseWriter := newMockResponseWriter(t)
  1857. responseWriter.EXPECT().
  1858. SendSNAC(output.Frame, output.Body).
  1859. Return(tt.responseError)
  1860. buf := &bytes.Buffer{}
  1861. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1862. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1863. if tt.expectedError != nil {
  1864. assert.ErrorIs(t, err, tt.expectedError)
  1865. } else {
  1866. assert.NoError(t, err)
  1867. }
  1868. })
  1869. }
  1870. }
  1871. func TestHandler_FeedbagStartCluster(t *testing.T) {
  1872. tests := []struct {
  1873. name string
  1874. inputBody wire.SNAC_0x13_0x11_FeedbagStartCluster
  1875. expectedError error
  1876. }{
  1877. {
  1878. name: "success",
  1879. inputBody: wire.SNAC_0x13_0x11_FeedbagStartCluster{
  1880. TLVRestBlock: wire.TLVRestBlock{
  1881. TLVList: wire.TLVList{
  1882. {
  1883. Tag: 0x01,
  1884. Value: []byte{1, 2, 3, 4},
  1885. },
  1886. },
  1887. },
  1888. },
  1889. },
  1890. }
  1891. for _, tt := range tests {
  1892. t.Run(tt.name, func(t *testing.T) {
  1893. input := wire.SNACMessage{
  1894. Frame: wire.SNACFrame{
  1895. FoodGroup: wire.Feedbag,
  1896. SubGroup: wire.FeedbagStartCluster,
  1897. },
  1898. Body: tt.inputBody,
  1899. }
  1900. svc := newMockFeedbagService(t)
  1901. svc.EXPECT().
  1902. StartCluster(mock.Anything, input.Frame, input.Body)
  1903. h := Handler{
  1904. FeedbagService: svc,
  1905. RouteLogger: middleware.RouteLogger{
  1906. Logger: slog.Default(),
  1907. },
  1908. }
  1909. responseWriter := newMockResponseWriter(t)
  1910. buf := &bytes.Buffer{}
  1911. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1912. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1913. if tt.expectedError != nil {
  1914. assert.ErrorIs(t, err, tt.expectedError)
  1915. } else {
  1916. assert.NoError(t, err)
  1917. }
  1918. })
  1919. }
  1920. }
  1921. func TestHandler_FeedbagUpdateItem(t *testing.T) {
  1922. tests := []struct {
  1923. name string
  1924. inputBody wire.SNAC_0x13_0x09_FeedbagUpdateItem
  1925. expectedOutput *wire.SNACMessage
  1926. serviceError error
  1927. responseError error
  1928. expectedError error
  1929. }{
  1930. {
  1931. name: "success",
  1932. inputBody: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
  1933. Items: []wire.FeedbagItem{
  1934. {
  1935. Name: "my-item",
  1936. },
  1937. },
  1938. },
  1939. expectedOutput: nil,
  1940. },
  1941. {
  1942. name: "success (non-nil output)",
  1943. inputBody: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
  1944. Items: []wire.FeedbagItem{
  1945. {
  1946. Name: "my-item",
  1947. },
  1948. },
  1949. },
  1950. expectedOutput: &wire.SNACMessage{
  1951. Frame: wire.SNACFrame{
  1952. FoodGroup: wire.Feedbag,
  1953. SubGroup: wire.FeedbagStatus,
  1954. },
  1955. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1956. Results: []uint16{1234},
  1957. },
  1958. },
  1959. },
  1960. {
  1961. name: "service error",
  1962. inputBody: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
  1963. Items: []wire.FeedbagItem{
  1964. {
  1965. Name: "my-item",
  1966. },
  1967. },
  1968. },
  1969. expectedOutput: &wire.SNACMessage{
  1970. Frame: wire.SNACFrame{
  1971. FoodGroup: wire.Feedbag,
  1972. SubGroup: wire.FeedbagStatus,
  1973. },
  1974. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1975. Results: []uint16{1234},
  1976. },
  1977. },
  1978. serviceError: assert.AnError,
  1979. expectedError: assert.AnError,
  1980. },
  1981. {
  1982. name: "response writer error",
  1983. inputBody: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
  1984. Items: []wire.FeedbagItem{
  1985. {
  1986. Name: "my-item",
  1987. },
  1988. },
  1989. },
  1990. expectedOutput: &wire.SNACMessage{
  1991. Frame: wire.SNACFrame{
  1992. FoodGroup: wire.Feedbag,
  1993. SubGroup: wire.FeedbagStatus,
  1994. },
  1995. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1996. Results: []uint16{1234},
  1997. },
  1998. },
  1999. responseError: assert.AnError,
  2000. expectedError: assert.AnError,
  2001. },
  2002. }
  2003. for _, tt := range tests {
  2004. t.Run(tt.name, func(t *testing.T) {
  2005. input := wire.SNACMessage{
  2006. Frame: wire.SNACFrame{
  2007. FoodGroup: wire.Feedbag,
  2008. SubGroup: wire.FeedbagUpdateItem,
  2009. },
  2010. Body: tt.inputBody,
  2011. }
  2012. svc := newMockFeedbagService(t)
  2013. svc.EXPECT().
  2014. UpsertItem(mock.Anything, mock.Anything, input.Frame, tt.inputBody.Items).
  2015. Return(tt.expectedOutput, tt.serviceError)
  2016. h := Handler{
  2017. FeedbagService: svc,
  2018. RouteLogger: middleware.RouteLogger{
  2019. Logger: slog.Default(),
  2020. },
  2021. }
  2022. responseWriter := newMockResponseWriter(t)
  2023. if tt.serviceError == nil && tt.expectedOutput != nil {
  2024. responseWriter.EXPECT().
  2025. SendSNAC(tt.expectedOutput.Frame, tt.expectedOutput.Body).
  2026. Return(tt.responseError)
  2027. }
  2028. buf := &bytes.Buffer{}
  2029. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2030. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2031. if tt.expectedError != nil {
  2032. assert.ErrorIs(t, err, tt.expectedError)
  2033. } else {
  2034. assert.NoError(t, err)
  2035. }
  2036. })
  2037. }
  2038. }
  2039. func TestHandler_FeedbagUse(t *testing.T) {
  2040. tests := []struct {
  2041. name string
  2042. inputBody struct{}
  2043. serviceError error
  2044. expectedError error
  2045. }{
  2046. {
  2047. name: "success",
  2048. inputBody: struct{}{},
  2049. },
  2050. {
  2051. name: "service error",
  2052. inputBody: struct{}{},
  2053. serviceError: assert.AnError,
  2054. expectedError: assert.AnError,
  2055. },
  2056. }
  2057. for _, tt := range tests {
  2058. t.Run(tt.name, func(t *testing.T) {
  2059. input := wire.SNACMessage{
  2060. Frame: wire.SNACFrame{
  2061. FoodGroup: wire.Feedbag,
  2062. SubGroup: wire.FeedbagUse,
  2063. },
  2064. Body: tt.inputBody,
  2065. }
  2066. svc := newMockFeedbagService(t)
  2067. svc.EXPECT().
  2068. Use(mock.Anything, mock.Anything).
  2069. Return(tt.serviceError)
  2070. h := Handler{
  2071. FeedbagService: svc,
  2072. RouteLogger: middleware.RouteLogger{
  2073. Logger: slog.Default(),
  2074. },
  2075. }
  2076. responseWriter := newMockResponseWriter(t)
  2077. buf := &bytes.Buffer{}
  2078. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2079. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2080. if tt.expectedError != nil {
  2081. assert.ErrorIs(t, err, tt.expectedError)
  2082. } else {
  2083. assert.NoError(t, err)
  2084. }
  2085. })
  2086. }
  2087. }
  2088. func TestHandler_FeedbagRespondAuthorizeToHost(t *testing.T) {
  2089. input := wire.SNACMessage{
  2090. Frame: wire.SNACFrame{
  2091. FoodGroup: wire.Feedbag,
  2092. SubGroup: wire.FeedbagRespondAuthorizeToHost,
  2093. },
  2094. Body: wire.SNAC_0x13_0x1A_FeedbagRespondAuthorizeToHost{
  2095. ScreenName: "theScreenName",
  2096. },
  2097. }
  2098. svc := newMockFeedbagService(t)
  2099. svc.EXPECT().
  2100. RespondAuthorizeToHost(mock.Anything, mock.Anything, input.Frame, input.Body).
  2101. Return(nil)
  2102. h := Handler{
  2103. FeedbagService: svc,
  2104. RouteLogger: middleware.RouteLogger{
  2105. Logger: slog.Default(),
  2106. },
  2107. }
  2108. responseWriter := newMockResponseWriter(t)
  2109. buf := &bytes.Buffer{}
  2110. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2111. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  2112. }
  2113. func TestHandler_ICBMAddParameters(t *testing.T) {
  2114. tests := []struct {
  2115. name string
  2116. inputBody wire.SNAC_0x04_0x02_ICBMAddParameters
  2117. expectedError error
  2118. }{
  2119. {
  2120. name: "success",
  2121. inputBody: wire.SNAC_0x04_0x02_ICBMAddParameters{
  2122. Channel: 1,
  2123. },
  2124. },
  2125. }
  2126. for _, tt := range tests {
  2127. t.Run(tt.name, func(t *testing.T) {
  2128. input := wire.SNACMessage{
  2129. Frame: wire.SNACFrame{
  2130. FoodGroup: wire.ICBM,
  2131. SubGroup: wire.ICBMAddParameters,
  2132. },
  2133. Body: tt.inputBody,
  2134. }
  2135. svc := newMockICBMService(t)
  2136. h := Handler{
  2137. ICBMService: svc,
  2138. RouteLogger: middleware.RouteLogger{
  2139. Logger: slog.Default(),
  2140. },
  2141. }
  2142. responseWriter := newMockResponseWriter(t)
  2143. buf := &bytes.Buffer{}
  2144. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2145. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2146. if tt.expectedError != nil {
  2147. assert.ErrorIs(t, err, tt.expectedError)
  2148. } else {
  2149. assert.NoError(t, err)
  2150. }
  2151. })
  2152. }
  2153. }
  2154. func TestHandler_ICBMChannelMsgToHost(t *testing.T) {
  2155. tests := []struct {
  2156. name string
  2157. inputBody wire.SNAC_0x04_0x06_ICBMChannelMsgToHost
  2158. serviceError error
  2159. responseError error
  2160. expectedError error
  2161. }{
  2162. {
  2163. name: "success",
  2164. inputBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2165. ScreenName: "recipient-screen-name",
  2166. },
  2167. },
  2168. {
  2169. name: "service error",
  2170. inputBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2171. ScreenName: "recipient-screen-name",
  2172. },
  2173. serviceError: assert.AnError,
  2174. expectedError: assert.AnError,
  2175. },
  2176. {
  2177. name: "response writer error",
  2178. inputBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2179. ScreenName: "recipient-screen-name",
  2180. },
  2181. responseError: assert.AnError,
  2182. expectedError: assert.AnError,
  2183. },
  2184. }
  2185. for _, tt := range tests {
  2186. t.Run(tt.name, func(t *testing.T) {
  2187. input := wire.SNACMessage{
  2188. Frame: wire.SNACFrame{
  2189. FoodGroup: wire.ICBM,
  2190. SubGroup: wire.ICBMChannelMsgToHost,
  2191. },
  2192. Body: tt.inputBody,
  2193. }
  2194. output := wire.SNACMessage{
  2195. Frame: wire.SNACFrame{
  2196. FoodGroup: wire.ICBM,
  2197. SubGroup: wire.ICBMHostAck,
  2198. },
  2199. Body: wire.SNAC_0x04_0x0C_ICBMHostAck{
  2200. ChannelID: 4,
  2201. },
  2202. }
  2203. svc := newMockICBMService(t)
  2204. svc.EXPECT().
  2205. ChannelMsgToHost(mock.Anything, mock.Anything, input.Frame, input.Body).
  2206. Return(&output, tt.serviceError)
  2207. h := Handler{
  2208. ICBMService: svc,
  2209. RouteLogger: middleware.RouteLogger{
  2210. Logger: slog.Default(),
  2211. },
  2212. }
  2213. responseWriter := newMockResponseWriter(t)
  2214. if tt.serviceError == nil {
  2215. responseWriter.EXPECT().
  2216. SendSNAC(output.Frame, output.Body).
  2217. Return(tt.responseError)
  2218. }
  2219. buf := &bytes.Buffer{}
  2220. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2221. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2222. if tt.expectedError != nil {
  2223. assert.ErrorIs(t, err, tt.expectedError)
  2224. } else {
  2225. assert.NoError(t, err)
  2226. }
  2227. })
  2228. }
  2229. }
  2230. func TestHandler_ICBMClientErr(t *testing.T) {
  2231. tests := []struct {
  2232. name string
  2233. inputBody wire.SNAC_0x04_0x0B_ICBMClientErr
  2234. serviceError error
  2235. expectedError error
  2236. }{
  2237. {
  2238. name: "success",
  2239. inputBody: wire.SNAC_0x04_0x0B_ICBMClientErr{
  2240. Code: 4,
  2241. },
  2242. },
  2243. {
  2244. name: "service error",
  2245. inputBody: wire.SNAC_0x04_0x0B_ICBMClientErr{
  2246. Code: 4,
  2247. },
  2248. serviceError: assert.AnError,
  2249. expectedError: assert.AnError,
  2250. },
  2251. }
  2252. for _, tt := range tests {
  2253. t.Run(tt.name, func(t *testing.T) {
  2254. input := wire.SNACMessage{
  2255. Frame: wire.SNACFrame{
  2256. FoodGroup: wire.ICBM,
  2257. SubGroup: wire.ICBMClientErr,
  2258. },
  2259. Body: tt.inputBody,
  2260. }
  2261. svc := newMockICBMService(t)
  2262. svc.EXPECT().
  2263. ClientErr(mock.Anything, mock.Anything, input.Frame, input.Body).
  2264. Return(tt.serviceError)
  2265. h := Handler{
  2266. ICBMService: svc,
  2267. RouteLogger: middleware.RouteLogger{
  2268. Logger: slog.Default(),
  2269. },
  2270. }
  2271. responseWriter := newMockResponseWriter(t)
  2272. buf := &bytes.Buffer{}
  2273. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2274. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2275. if tt.expectedError != nil {
  2276. assert.ErrorIs(t, err, tt.expectedError)
  2277. } else {
  2278. assert.NoError(t, err)
  2279. }
  2280. })
  2281. }
  2282. }
  2283. func TestHandler_ICBMClientEvent(t *testing.T) {
  2284. tests := []struct {
  2285. name string
  2286. inputBody wire.SNAC_0x04_0x14_ICBMClientEvent
  2287. serviceError error
  2288. expectedError error
  2289. }{
  2290. {
  2291. name: "success",
  2292. inputBody: wire.SNAC_0x04_0x14_ICBMClientEvent{
  2293. ScreenName: "recipient-screen-name",
  2294. },
  2295. },
  2296. {
  2297. name: "service error",
  2298. inputBody: wire.SNAC_0x04_0x14_ICBMClientEvent{
  2299. ScreenName: "recipient-screen-name",
  2300. },
  2301. serviceError: assert.AnError,
  2302. expectedError: assert.AnError,
  2303. },
  2304. }
  2305. for _, tt := range tests {
  2306. t.Run(tt.name, func(t *testing.T) {
  2307. input := wire.SNACMessage{
  2308. Frame: wire.SNACFrame{
  2309. FoodGroup: wire.ICBM,
  2310. SubGroup: wire.ICBMClientEvent,
  2311. },
  2312. Body: tt.inputBody,
  2313. }
  2314. svc := newMockICBMService(t)
  2315. svc.EXPECT().
  2316. ClientEvent(mock.Anything, mock.Anything, input.Frame, input.Body).
  2317. Return(tt.serviceError)
  2318. h := Handler{
  2319. ICBMService: svc,
  2320. RouteLogger: middleware.RouteLogger{
  2321. Logger: slog.Default(),
  2322. },
  2323. }
  2324. responseWriter := newMockResponseWriter(t)
  2325. buf := &bytes.Buffer{}
  2326. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2327. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2328. if tt.expectedError != nil {
  2329. assert.ErrorIs(t, err, tt.expectedError)
  2330. } else {
  2331. assert.NoError(t, err)
  2332. }
  2333. })
  2334. }
  2335. }
  2336. func TestHandler_ICBMEvilRequest(t *testing.T) {
  2337. tests := []struct {
  2338. name string
  2339. inputBody wire.SNAC_0x04_0x08_ICBMEvilRequest
  2340. serviceError error
  2341. responseError error
  2342. expectedError error
  2343. }{
  2344. {
  2345. name: "success",
  2346. inputBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  2347. ScreenName: "recipient-screen-name",
  2348. },
  2349. },
  2350. {
  2351. name: "service error",
  2352. inputBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  2353. ScreenName: "recipient-screen-name",
  2354. },
  2355. serviceError: assert.AnError,
  2356. expectedError: assert.AnError,
  2357. },
  2358. {
  2359. name: "response writer error",
  2360. inputBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  2361. ScreenName: "recipient-screen-name",
  2362. },
  2363. responseError: assert.AnError,
  2364. expectedError: assert.AnError,
  2365. },
  2366. }
  2367. for _, tt := range tests {
  2368. t.Run(tt.name, func(t *testing.T) {
  2369. input := wire.SNACMessage{
  2370. Frame: wire.SNACFrame{
  2371. FoodGroup: wire.ICBM,
  2372. SubGroup: wire.ICBMEvilRequest,
  2373. },
  2374. Body: tt.inputBody,
  2375. }
  2376. output := wire.SNACMessage{
  2377. Frame: wire.SNACFrame{
  2378. FoodGroup: wire.ICBM,
  2379. SubGroup: wire.ICBMEvilReply,
  2380. },
  2381. Body: wire.SNAC_0x04_0x09_ICBMEvilReply{
  2382. EvilDeltaApplied: 100,
  2383. },
  2384. }
  2385. svc := newMockICBMService(t)
  2386. svc.EXPECT().
  2387. EvilRequest(mock.Anything, mock.Anything, input.Frame, input.Body).
  2388. Return(output, tt.serviceError)
  2389. h := Handler{
  2390. ICBMService: svc,
  2391. RouteLogger: middleware.RouteLogger{
  2392. Logger: slog.Default(),
  2393. },
  2394. }
  2395. responseWriter := newMockResponseWriter(t)
  2396. if tt.serviceError == nil {
  2397. responseWriter.EXPECT().
  2398. SendSNAC(output.Frame, output.Body).
  2399. Return(tt.responseError)
  2400. }
  2401. buf := &bytes.Buffer{}
  2402. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2403. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2404. if tt.expectedError != nil {
  2405. assert.ErrorIs(t, err, tt.expectedError)
  2406. } else {
  2407. assert.NoError(t, err)
  2408. }
  2409. })
  2410. }
  2411. }
  2412. func TestHandler_ICBMOfflineRetrieve(t *testing.T) {
  2413. tests := []struct {
  2414. name string
  2415. serviceError error
  2416. responseError error
  2417. expectedError error
  2418. }{
  2419. {
  2420. name: "success",
  2421. },
  2422. {
  2423. name: "service error",
  2424. serviceError: assert.AnError,
  2425. expectedError: assert.AnError,
  2426. },
  2427. {
  2428. name: "response writer error",
  2429. responseError: assert.AnError,
  2430. expectedError: assert.AnError,
  2431. },
  2432. }
  2433. for _, tt := range tests {
  2434. t.Run(tt.name, func(t *testing.T) {
  2435. input := wire.SNACMessage{
  2436. Frame: wire.SNACFrame{
  2437. FoodGroup: wire.ICBM,
  2438. SubGroup: wire.ICBMOfflineRetrieve,
  2439. },
  2440. Body: wire.SNAC_0x04_0x0A_ICBMOfflineRetrieve{},
  2441. }
  2442. output := wire.SNACMessage{
  2443. Frame: wire.SNACFrame{
  2444. FoodGroup: wire.ICBM,
  2445. SubGroup: wire.ICBMOfflineRetrieveReply,
  2446. },
  2447. Body: wire.SNAC_0x04_0x17_ICBMOfflineRetrieveReply{},
  2448. }
  2449. svc := newMockICBMService(t)
  2450. svc.EXPECT().
  2451. OfflineRetrieve(mock.Anything, mock.Anything, input.Frame).
  2452. Return(output, tt.serviceError)
  2453. h := Handler{
  2454. ICBMService: svc,
  2455. RouteLogger: middleware.RouteLogger{
  2456. Logger: slog.Default(),
  2457. },
  2458. }
  2459. responseWriter := newMockResponseWriter(t)
  2460. if tt.serviceError == nil {
  2461. responseWriter.EXPECT().
  2462. SendSNAC(output.Frame, output.Body).
  2463. Return(tt.responseError)
  2464. }
  2465. buf := &bytes.Buffer{}
  2466. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2467. if tt.expectedError != nil {
  2468. assert.ErrorIs(t, err, tt.expectedError)
  2469. } else {
  2470. assert.NoError(t, err)
  2471. }
  2472. })
  2473. }
  2474. }
  2475. func TestHandler_ICBMParameterQuery(t *testing.T) {
  2476. tests := []struct {
  2477. name string
  2478. inputBody struct{}
  2479. responseError error
  2480. expectedError error
  2481. }{
  2482. {
  2483. name: "success",
  2484. inputBody: struct{}{},
  2485. },
  2486. {
  2487. name: "response writer error",
  2488. inputBody: struct{}{},
  2489. responseError: assert.AnError,
  2490. expectedError: assert.AnError,
  2491. },
  2492. }
  2493. for _, tt := range tests {
  2494. t.Run(tt.name, func(t *testing.T) {
  2495. input := wire.SNACMessage{
  2496. Frame: wire.SNACFrame{
  2497. FoodGroup: wire.ICBM,
  2498. SubGroup: wire.ICBMParameterQuery,
  2499. },
  2500. Body: tt.inputBody,
  2501. }
  2502. output := wire.SNACMessage{
  2503. Frame: wire.SNACFrame{
  2504. FoodGroup: wire.ICBM,
  2505. SubGroup: wire.ICBMParameterReply,
  2506. },
  2507. Body: wire.SNAC_0x04_0x05_ICBMParameterReply{
  2508. MaxSlots: 100,
  2509. },
  2510. }
  2511. svc := newMockICBMService(t)
  2512. svc.EXPECT().
  2513. ParameterQuery(mock.Anything, input.Frame).
  2514. Return(output)
  2515. h := Handler{
  2516. ICBMService: svc,
  2517. RouteLogger: middleware.RouteLogger{
  2518. Logger: slog.Default(),
  2519. },
  2520. }
  2521. responseWriter := newMockResponseWriter(t)
  2522. responseWriter.EXPECT().
  2523. SendSNAC(output.Frame, output.Body).
  2524. Return(tt.responseError)
  2525. buf := &bytes.Buffer{}
  2526. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2527. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2528. if tt.expectedError != nil {
  2529. assert.ErrorIs(t, err, tt.expectedError)
  2530. } else {
  2531. assert.NoError(t, err)
  2532. }
  2533. })
  2534. }
  2535. }
  2536. func TestHandler_ICQDBQuery(t *testing.T) {
  2537. type ICQMetaRequest struct {
  2538. wire.ICQMetadata
  2539. ReqSubType uint16
  2540. MetaRequest any
  2541. }
  2542. type reqParams struct {
  2543. ctx context.Context
  2544. instance *state.SessionInstance
  2545. inFrame wire.SNACFrame
  2546. inBody wire.SNAC_0x15_0x02_BQuery
  2547. rw ResponseWriter
  2548. seq uint16
  2549. wantErr error
  2550. }
  2551. type mockParam struct {
  2552. req any
  2553. wantErr error
  2554. }
  2555. type allMockParams struct {
  2556. deleteMsgReq *mockParam
  2557. findByDetails *mockParam
  2558. findByEmail *mockParam
  2559. findByEmail3 *mockParam
  2560. findByInterests *mockParam
  2561. findByUIN *mockParam
  2562. findByUIN2 *mockParam
  2563. findByWhitePages2 *mockParam
  2564. fullUserInfo *mockParam
  2565. offlineMsgReq *mockParam
  2566. setAffiliations *mockParam
  2567. setBasicInfo *mockParam
  2568. setEmails *mockParam
  2569. setInterests *mockParam
  2570. setMoreInfo *mockParam
  2571. setPermissions *mockParam
  2572. setUserNotes *mockParam
  2573. setWorkInfo *mockParam
  2574. shortUserInfo *mockParam
  2575. xmlReqData *mockParam
  2576. }
  2577. tests := []struct {
  2578. name string
  2579. reqParams reqParams
  2580. allMockParams allMockParams
  2581. }{
  2582. {
  2583. name: "MetaReqFullInfo - happy path",
  2584. reqParams: reqParams{
  2585. instance: state.NewSession().AddInstance(),
  2586. inBody: wire.SNAC_0x15_0x02_BQuery{
  2587. TLVRestBlock: wire.TLVRestBlock{
  2588. TLVList: wire.TLVList{
  2589. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2590. Message: ICQMetaRequest{
  2591. ICQMetadata: wire.ICQMetadata{
  2592. ReqType: wire.ICQDBQueryMetaReq,
  2593. Seq: 1,
  2594. },
  2595. ReqSubType: wire.ICQDBQueryMetaReqFullInfo,
  2596. MetaRequest: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2597. UIN: 123456789,
  2598. },
  2599. },
  2600. }),
  2601. },
  2602. },
  2603. },
  2604. seq: 1,
  2605. },
  2606. allMockParams: allMockParams{
  2607. fullUserInfo: &mockParam{
  2608. req: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2609. UIN: 123456789,
  2610. },
  2611. },
  2612. },
  2613. },
  2614. {
  2615. name: "MetaReqShortInfo - happy path",
  2616. reqParams: reqParams{
  2617. instance: state.NewSession().AddInstance(),
  2618. inBody: wire.SNAC_0x15_0x02_BQuery{
  2619. TLVRestBlock: wire.TLVRestBlock{
  2620. TLVList: wire.TLVList{
  2621. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2622. Message: ICQMetaRequest{
  2623. ICQMetadata: wire.ICQMetadata{
  2624. ReqType: wire.ICQDBQueryMetaReq,
  2625. Seq: 1,
  2626. },
  2627. ReqSubType: wire.ICQDBQueryMetaReqShortInfo,
  2628. MetaRequest: wire.ICQ_0x07D0_0x04BA_DBQueryMetaReqShortInfo{
  2629. UIN: 123456789,
  2630. },
  2631. },
  2632. }),
  2633. },
  2634. },
  2635. },
  2636. seq: 1,
  2637. },
  2638. allMockParams: allMockParams{
  2639. shortUserInfo: &mockParam{
  2640. req: wire.ICQ_0x07D0_0x04BA_DBQueryMetaReqShortInfo{
  2641. UIN: 123456789,
  2642. },
  2643. },
  2644. },
  2645. },
  2646. {
  2647. name: "MetaReqFullInfo2 - happy path",
  2648. reqParams: reqParams{
  2649. instance: state.NewSession().AddInstance(),
  2650. inBody: wire.SNAC_0x15_0x02_BQuery{
  2651. TLVRestBlock: wire.TLVRestBlock{
  2652. TLVList: wire.TLVList{
  2653. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2654. Message: ICQMetaRequest{
  2655. ICQMetadata: wire.ICQMetadata{
  2656. ReqType: wire.ICQDBQueryMetaReq,
  2657. Seq: 1,
  2658. },
  2659. ReqSubType: wire.ICQDBQueryMetaReqFullInfo2,
  2660. MetaRequest: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2661. UIN: 123456789,
  2662. },
  2663. },
  2664. }),
  2665. },
  2666. },
  2667. },
  2668. seq: 1,
  2669. },
  2670. allMockParams: allMockParams{
  2671. fullUserInfo: &mockParam{
  2672. req: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2673. UIN: 123456789,
  2674. },
  2675. },
  2676. },
  2677. },
  2678. {
  2679. name: "MetaReqXMLReq - happy path",
  2680. reqParams: reqParams{
  2681. instance: state.NewSession().AddInstance(),
  2682. inBody: wire.SNAC_0x15_0x02_BQuery{
  2683. TLVRestBlock: wire.TLVRestBlock{
  2684. TLVList: wire.TLVList{
  2685. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2686. Message: ICQMetaRequest{
  2687. ICQMetadata: wire.ICQMetadata{
  2688. ReqType: wire.ICQDBQueryMetaReq,
  2689. Seq: 1,
  2690. },
  2691. ReqSubType: wire.ICQDBQueryMetaReqXMLReq,
  2692. MetaRequest: wire.ICQ_0x07D0_0x0898_DBQueryMetaReqXMLReq{
  2693. XMLRequest: "<xml></xml>",
  2694. },
  2695. },
  2696. }),
  2697. },
  2698. },
  2699. },
  2700. seq: 1,
  2701. },
  2702. allMockParams: allMockParams{
  2703. xmlReqData: &mockParam{
  2704. req: wire.ICQ_0x07D0_0x0898_DBQueryMetaReqXMLReq{
  2705. XMLRequest: "<xml></xml>",
  2706. },
  2707. },
  2708. },
  2709. },
  2710. {
  2711. name: "MetaReqSetPermissions - happy path",
  2712. reqParams: reqParams{
  2713. instance: state.NewSession().AddInstance(),
  2714. inBody: wire.SNAC_0x15_0x02_BQuery{
  2715. TLVRestBlock: wire.TLVRestBlock{
  2716. TLVList: wire.TLVList{
  2717. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2718. Message: ICQMetaRequest{
  2719. ICQMetadata: wire.ICQMetadata{
  2720. ReqType: wire.ICQDBQueryMetaReq,
  2721. Seq: 1,
  2722. },
  2723. ReqSubType: wire.ICQDBQueryMetaReqSetPermissions,
  2724. MetaRequest: wire.ICQ_0x07D0_0x0424_DBQueryMetaReqSetPermissions{
  2725. Authorization: 1,
  2726. },
  2727. },
  2728. }),
  2729. },
  2730. },
  2731. },
  2732. seq: 1,
  2733. },
  2734. allMockParams: allMockParams{
  2735. setPermissions: &mockParam{
  2736. req: wire.ICQ_0x07D0_0x0424_DBQueryMetaReqSetPermissions{
  2737. Authorization: 1,
  2738. },
  2739. },
  2740. },
  2741. },
  2742. {
  2743. name: "MetaReqSearchByUIN - happy path",
  2744. reqParams: reqParams{
  2745. instance: state.NewSession().AddInstance(),
  2746. inBody: wire.SNAC_0x15_0x02_BQuery{
  2747. TLVRestBlock: wire.TLVRestBlock{
  2748. TLVList: wire.TLVList{
  2749. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2750. Message: ICQMetaRequest{
  2751. ICQMetadata: wire.ICQMetadata{
  2752. ReqType: wire.ICQDBQueryMetaReq,
  2753. Seq: 1,
  2754. },
  2755. ReqSubType: wire.ICQDBQueryMetaReqSearchByUIN,
  2756. MetaRequest: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2757. UIN: 123456789,
  2758. },
  2759. },
  2760. }),
  2761. },
  2762. },
  2763. },
  2764. seq: 1,
  2765. },
  2766. allMockParams: allMockParams{
  2767. findByUIN: &mockParam{
  2768. req: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2769. UIN: 123456789,
  2770. },
  2771. },
  2772. },
  2773. },
  2774. {
  2775. name: "MetaReqSearchByUIN2 - happy path",
  2776. reqParams: reqParams{
  2777. instance: state.NewSession().AddInstance(),
  2778. inBody: wire.SNAC_0x15_0x02_BQuery{
  2779. TLVRestBlock: wire.TLVRestBlock{
  2780. TLVList: wire.TLVList{
  2781. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2782. Message: ICQMetaRequest{
  2783. ICQMetadata: wire.ICQMetadata{
  2784. ReqType: wire.ICQDBQueryMetaReq,
  2785. Seq: 1,
  2786. },
  2787. ReqSubType: wire.ICQDBQueryMetaReqSearchByUIN2,
  2788. MetaRequest: wire.ICQ_0x07D0_0x0569_DBQueryMetaReqSearchByUIN2{
  2789. TLVRestBlock: wire.TLVRestBlock{
  2790. TLVList: wire.TLVList{
  2791. wire.NewTLVBE(1, uint16(1)),
  2792. },
  2793. },
  2794. },
  2795. },
  2796. }),
  2797. },
  2798. },
  2799. },
  2800. seq: 1,
  2801. },
  2802. allMockParams: allMockParams{
  2803. findByUIN2: &mockParam{
  2804. req: wire.ICQ_0x07D0_0x0569_DBQueryMetaReqSearchByUIN2{
  2805. TLVRestBlock: wire.TLVRestBlock{
  2806. TLVList: wire.TLVList{
  2807. wire.NewTLVBE(1, uint16(1)),
  2808. },
  2809. },
  2810. },
  2811. },
  2812. },
  2813. },
  2814. {
  2815. name: "MetaReqSearchByEmail - happy path",
  2816. reqParams: reqParams{
  2817. instance: state.NewSession().AddInstance(),
  2818. inBody: wire.SNAC_0x15_0x02_BQuery{
  2819. TLVRestBlock: wire.TLVRestBlock{
  2820. TLVList: wire.TLVList{
  2821. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2822. Message: ICQMetaRequest{
  2823. ICQMetadata: wire.ICQMetadata{
  2824. ReqType: wire.ICQDBQueryMetaReq,
  2825. Seq: 1,
  2826. },
  2827. ReqSubType: wire.ICQDBQueryMetaReqSearchByEmail,
  2828. MetaRequest: wire.ICQ_0x07D0_0x0529_DBQueryMetaReqSearchByEmail{
  2829. Email: "test@aol.com",
  2830. },
  2831. },
  2832. }),
  2833. },
  2834. },
  2835. },
  2836. seq: 1,
  2837. },
  2838. allMockParams: allMockParams{
  2839. findByEmail: &mockParam{
  2840. req: wire.ICQ_0x07D0_0x0529_DBQueryMetaReqSearchByEmail{
  2841. Email: "test@aol.com",
  2842. },
  2843. },
  2844. },
  2845. },
  2846. {
  2847. name: "MetaReqSearchByEmail3 - happy path",
  2848. reqParams: reqParams{
  2849. instance: state.NewSession().AddInstance(),
  2850. inBody: wire.SNAC_0x15_0x02_BQuery{
  2851. TLVRestBlock: wire.TLVRestBlock{
  2852. TLVList: wire.TLVList{
  2853. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2854. Message: ICQMetaRequest{
  2855. ICQMetadata: wire.ICQMetadata{
  2856. ReqType: wire.ICQDBQueryMetaReq,
  2857. Seq: 1,
  2858. },
  2859. ReqSubType: wire.ICQDBQueryMetaReqSearchByEmail3,
  2860. MetaRequest: wire.ICQ_0x07D0_0x0573_DBQueryMetaReqSearchByEmail3{
  2861. TLVRestBlock: wire.TLVRestBlock{
  2862. TLVList: wire.TLVList{
  2863. wire.NewTLVBE(1, uint16(1)),
  2864. },
  2865. },
  2866. },
  2867. },
  2868. }),
  2869. },
  2870. },
  2871. },
  2872. seq: 1,
  2873. },
  2874. allMockParams: allMockParams{
  2875. findByEmail3: &mockParam{
  2876. req: wire.ICQ_0x07D0_0x0573_DBQueryMetaReqSearchByEmail3{
  2877. TLVRestBlock: wire.TLVRestBlock{
  2878. TLVList: wire.TLVList{
  2879. wire.NewTLVBE(1, uint16(1)),
  2880. },
  2881. },
  2882. },
  2883. },
  2884. },
  2885. },
  2886. {
  2887. name: "MetaReqSearchByDetails - happy path",
  2888. reqParams: reqParams{
  2889. instance: state.NewSession().AddInstance(),
  2890. inBody: wire.SNAC_0x15_0x02_BQuery{
  2891. TLVRestBlock: wire.TLVRestBlock{
  2892. TLVList: wire.TLVList{
  2893. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2894. Message: ICQMetaRequest{
  2895. ICQMetadata: wire.ICQMetadata{
  2896. ReqType: wire.ICQDBQueryMetaReq,
  2897. Seq: 1,
  2898. },
  2899. ReqSubType: wire.ICQDBQueryMetaReqSearchByDetails,
  2900. MetaRequest: wire.ICQ_0x07D0_0x0515_DBQueryMetaReqSearchByDetails{
  2901. FirstName: "john",
  2902. },
  2903. },
  2904. }),
  2905. },
  2906. },
  2907. },
  2908. seq: 1,
  2909. },
  2910. allMockParams: allMockParams{
  2911. findByDetails: &mockParam{
  2912. req: wire.ICQ_0x07D0_0x0515_DBQueryMetaReqSearchByDetails{
  2913. FirstName: "john",
  2914. },
  2915. },
  2916. },
  2917. },
  2918. {
  2919. name: "MetaReqSearchWhitePages - happy path",
  2920. reqParams: reqParams{
  2921. instance: state.NewSession().AddInstance(),
  2922. inBody: wire.SNAC_0x15_0x02_BQuery{
  2923. TLVRestBlock: wire.TLVRestBlock{
  2924. TLVList: wire.TLVList{
  2925. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2926. Message: ICQMetaRequest{
  2927. ICQMetadata: wire.ICQMetadata{
  2928. ReqType: wire.ICQDBQueryMetaReq,
  2929. Seq: 1,
  2930. },
  2931. ReqSubType: wire.ICQDBQueryMetaReqSearchWhitePages,
  2932. MetaRequest: wire.ICQ_0x07D0_0x0533_DBQueryMetaReqSearchWhitePages{
  2933. InterestsCode: 1,
  2934. },
  2935. },
  2936. }),
  2937. },
  2938. },
  2939. },
  2940. seq: 1,
  2941. },
  2942. allMockParams: allMockParams{
  2943. findByInterests: &mockParam{
  2944. req: wire.ICQ_0x07D0_0x0533_DBQueryMetaReqSearchWhitePages{
  2945. InterestsCode: 1,
  2946. },
  2947. },
  2948. },
  2949. },
  2950. {
  2951. name: "MetaReqSearchWhitePages2 - happy path",
  2952. reqParams: reqParams{
  2953. instance: state.NewSession().AddInstance(),
  2954. inBody: wire.SNAC_0x15_0x02_BQuery{
  2955. TLVRestBlock: wire.TLVRestBlock{
  2956. TLVList: wire.TLVList{
  2957. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2958. Message: ICQMetaRequest{
  2959. ICQMetadata: wire.ICQMetadata{
  2960. ReqType: wire.ICQDBQueryMetaReq,
  2961. Seq: 1,
  2962. },
  2963. ReqSubType: wire.ICQDBQueryMetaReqSearchWhitePages2,
  2964. MetaRequest: wire.ICQ_0x07D0_0x055F_DBQueryMetaReqSearchWhitePages2{
  2965. TLVRestBlock: wire.TLVRestBlock{
  2966. TLVList: wire.TLVList{
  2967. wire.NewTLVBE(1, uint16(1)),
  2968. },
  2969. },
  2970. },
  2971. },
  2972. }),
  2973. },
  2974. },
  2975. },
  2976. seq: 1,
  2977. },
  2978. allMockParams: allMockParams{
  2979. findByWhitePages2: &mockParam{
  2980. req: wire.ICQ_0x07D0_0x055F_DBQueryMetaReqSearchWhitePages2{
  2981. TLVRestBlock: wire.TLVRestBlock{
  2982. TLVList: wire.TLVList{
  2983. wire.NewTLVBE(1, uint16(1)),
  2984. },
  2985. },
  2986. },
  2987. },
  2988. },
  2989. },
  2990. {
  2991. name: "MetaReqSetBasicInfo - happy path",
  2992. reqParams: reqParams{
  2993. instance: state.NewSession().AddInstance(),
  2994. inBody: wire.SNAC_0x15_0x02_BQuery{
  2995. TLVRestBlock: wire.TLVRestBlock{
  2996. TLVList: wire.TLVList{
  2997. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2998. Message: ICQMetaRequest{
  2999. ICQMetadata: wire.ICQMetadata{
  3000. ReqType: wire.ICQDBQueryMetaReq,
  3001. Seq: 1,
  3002. },
  3003. ReqSubType: wire.ICQDBQueryMetaReqSetBasicInfo,
  3004. MetaRequest: wire.ICQ_0x07D0_0x03EA_DBQueryMetaReqSetBasicInfo{
  3005. FirstName: "john",
  3006. },
  3007. },
  3008. }),
  3009. },
  3010. },
  3011. },
  3012. seq: 1,
  3013. },
  3014. allMockParams: allMockParams{
  3015. setBasicInfo: &mockParam{
  3016. req: wire.ICQ_0x07D0_0x03EA_DBQueryMetaReqSetBasicInfo{
  3017. FirstName: "john",
  3018. },
  3019. },
  3020. },
  3021. },
  3022. {
  3023. name: "MetaReqSetWorkInfo - happy path",
  3024. reqParams: reqParams{
  3025. instance: state.NewSession().AddInstance(),
  3026. inBody: wire.SNAC_0x15_0x02_BQuery{
  3027. TLVRestBlock: wire.TLVRestBlock{
  3028. TLVList: wire.TLVList{
  3029. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3030. Message: ICQMetaRequest{
  3031. ICQMetadata: wire.ICQMetadata{
  3032. ReqType: wire.ICQDBQueryMetaReq,
  3033. Seq: 1,
  3034. },
  3035. ReqSubType: wire.ICQDBQueryMetaReqSetWorkInfo,
  3036. MetaRequest: wire.ICQ_0x07D0_0x03F3_DBQueryMetaReqSetWorkInfo{
  3037. ZIP: "11111",
  3038. },
  3039. },
  3040. }),
  3041. },
  3042. },
  3043. },
  3044. seq: 1,
  3045. },
  3046. allMockParams: allMockParams{
  3047. setWorkInfo: &mockParam{
  3048. req: wire.ICQ_0x07D0_0x03F3_DBQueryMetaReqSetWorkInfo{
  3049. ZIP: "11111",
  3050. },
  3051. },
  3052. },
  3053. },
  3054. {
  3055. name: "MetaReqSetMoreInfo - happy path",
  3056. reqParams: reqParams{
  3057. instance: state.NewSession().AddInstance(),
  3058. inBody: wire.SNAC_0x15_0x02_BQuery{
  3059. TLVRestBlock: wire.TLVRestBlock{
  3060. TLVList: wire.TLVList{
  3061. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3062. Message: ICQMetaRequest{
  3063. ICQMetadata: wire.ICQMetadata{
  3064. ReqType: wire.ICQDBQueryMetaReq,
  3065. Seq: 1,
  3066. },
  3067. ReqSubType: wire.ICQDBQueryMetaReqSetMoreInfo,
  3068. MetaRequest: wire.ICQ_0x07D0_0x03FD_DBQueryMetaReqSetMoreInfo{
  3069. Age: 100,
  3070. },
  3071. },
  3072. }),
  3073. },
  3074. },
  3075. },
  3076. seq: 1,
  3077. },
  3078. allMockParams: allMockParams{
  3079. setMoreInfo: &mockParam{
  3080. req: wire.ICQ_0x07D0_0x03FD_DBQueryMetaReqSetMoreInfo{
  3081. Age: 100,
  3082. },
  3083. },
  3084. },
  3085. },
  3086. {
  3087. name: "MetaReqSetNotes - happy path",
  3088. reqParams: reqParams{
  3089. instance: state.NewSession().AddInstance(),
  3090. inBody: wire.SNAC_0x15_0x02_BQuery{
  3091. TLVRestBlock: wire.TLVRestBlock{
  3092. TLVList: wire.TLVList{
  3093. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3094. Message: ICQMetaRequest{
  3095. ICQMetadata: wire.ICQMetadata{
  3096. ReqType: wire.ICQDBQueryMetaReq,
  3097. Seq: 1,
  3098. },
  3099. ReqSubType: wire.ICQDBQueryMetaReqSetNotes,
  3100. MetaRequest: wire.ICQ_0x07D0_0x0406_DBQueryMetaReqSetNotes{
  3101. Notes: "my note",
  3102. },
  3103. },
  3104. }),
  3105. },
  3106. },
  3107. },
  3108. seq: 1,
  3109. },
  3110. allMockParams: allMockParams{
  3111. setUserNotes: &mockParam{
  3112. req: wire.ICQ_0x07D0_0x0406_DBQueryMetaReqSetNotes{
  3113. Notes: "my note",
  3114. },
  3115. },
  3116. },
  3117. },
  3118. {
  3119. name: "MetaReqSetEmails - happy path",
  3120. reqParams: reqParams{
  3121. instance: state.NewSession().AddInstance(),
  3122. inBody: wire.SNAC_0x15_0x02_BQuery{
  3123. TLVRestBlock: wire.TLVRestBlock{
  3124. TLVList: wire.TLVList{
  3125. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3126. Message: ICQMetaRequest{
  3127. ICQMetadata: wire.ICQMetadata{
  3128. ReqType: wire.ICQDBQueryMetaReq,
  3129. Seq: 1,
  3130. },
  3131. ReqSubType: wire.ICQDBQueryMetaReqSetEmails,
  3132. MetaRequest: wire.ICQ_0x07D0_0x040B_DBQueryMetaReqSetEmails{
  3133. Emails: []struct {
  3134. Publish uint8
  3135. Email string `oscar:"len_prefix=uint16,nullterm"`
  3136. }{
  3137. {
  3138. Email: "test@aol.com",
  3139. },
  3140. },
  3141. },
  3142. },
  3143. }),
  3144. },
  3145. },
  3146. },
  3147. seq: 1,
  3148. },
  3149. allMockParams: allMockParams{
  3150. setEmails: &mockParam{
  3151. req: wire.ICQ_0x07D0_0x040B_DBQueryMetaReqSetEmails{
  3152. Emails: []struct {
  3153. Publish uint8
  3154. Email string `oscar:"len_prefix=uint16,nullterm"`
  3155. }{
  3156. {
  3157. Email: "test@aol.com",
  3158. },
  3159. },
  3160. },
  3161. },
  3162. },
  3163. },
  3164. {
  3165. name: "MetaReqSetInterests - happy path",
  3166. reqParams: reqParams{
  3167. instance: state.NewSession().AddInstance(),
  3168. inBody: wire.SNAC_0x15_0x02_BQuery{
  3169. TLVRestBlock: wire.TLVRestBlock{
  3170. TLVList: wire.TLVList{
  3171. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3172. Message: ICQMetaRequest{
  3173. ICQMetadata: wire.ICQMetadata{
  3174. ReqType: wire.ICQDBQueryMetaReq,
  3175. Seq: 1,
  3176. },
  3177. ReqSubType: wire.ICQDBQueryMetaReqSetInterests,
  3178. MetaRequest: wire.ICQ_0x07D0_0x0410_DBQueryMetaReqSetInterests{
  3179. Interests: []struct {
  3180. Code uint16
  3181. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  3182. }{
  3183. {
  3184. Keyword: "an_interest",
  3185. },
  3186. },
  3187. },
  3188. },
  3189. }),
  3190. },
  3191. },
  3192. },
  3193. seq: 1,
  3194. },
  3195. allMockParams: allMockParams{
  3196. setInterests: &mockParam{
  3197. req: wire.ICQ_0x07D0_0x0410_DBQueryMetaReqSetInterests{
  3198. Interests: []struct {
  3199. Code uint16
  3200. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  3201. }{
  3202. {
  3203. Keyword: "an_interest",
  3204. },
  3205. },
  3206. },
  3207. },
  3208. },
  3209. },
  3210. {
  3211. name: "MetaReqSetAffiliations - happy path",
  3212. reqParams: reqParams{
  3213. instance: state.NewSession().AddInstance(),
  3214. inBody: wire.SNAC_0x15_0x02_BQuery{
  3215. TLVRestBlock: wire.TLVRestBlock{
  3216. TLVList: wire.TLVList{
  3217. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3218. Message: ICQMetaRequest{
  3219. ICQMetadata: wire.ICQMetadata{
  3220. ReqType: wire.ICQDBQueryMetaReq,
  3221. Seq: 1,
  3222. },
  3223. ReqSubType: wire.ICQDBQueryMetaReqSetAffiliations,
  3224. MetaRequest: wire.ICQ_0x07D0_0x041A_DBQueryMetaReqSetAffiliations{
  3225. PastAffiliations: []struct {
  3226. Code uint16
  3227. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  3228. }{
  3229. {
  3230. Keyword: "a_past_affiliation",
  3231. },
  3232. },
  3233. Affiliations: []struct {
  3234. Code uint16
  3235. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  3236. }{
  3237. {
  3238. Keyword: "an_affiliation",
  3239. },
  3240. },
  3241. },
  3242. },
  3243. }),
  3244. },
  3245. },
  3246. },
  3247. seq: 1,
  3248. },
  3249. allMockParams: allMockParams{
  3250. setAffiliations: &mockParam{
  3251. req: wire.ICQ_0x07D0_0x041A_DBQueryMetaReqSetAffiliations{
  3252. PastAffiliations: []struct {
  3253. Code uint16
  3254. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  3255. }{
  3256. {
  3257. Keyword: "a_past_affiliation",
  3258. },
  3259. },
  3260. Affiliations: []struct {
  3261. Code uint16
  3262. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  3263. }{
  3264. {
  3265. Keyword: "an_affiliation",
  3266. },
  3267. },
  3268. },
  3269. },
  3270. },
  3271. },
  3272. {
  3273. name: "MetaReqStat - happy path",
  3274. reqParams: reqParams{
  3275. instance: state.NewSession().AddInstance(),
  3276. inBody: wire.SNAC_0x15_0x02_BQuery{
  3277. TLVRestBlock: wire.TLVRestBlock{
  3278. TLVList: wire.TLVList{
  3279. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3280. Message: ICQMetaRequest{
  3281. ICQMetadata: wire.ICQMetadata{
  3282. ReqType: wire.ICQDBQueryMetaReq,
  3283. Seq: 1,
  3284. },
  3285. ReqSubType: wire.ICQDBQueryMetaReqStat0a8c,
  3286. MetaRequest: struct{}{},
  3287. },
  3288. }),
  3289. },
  3290. },
  3291. },
  3292. seq: 1,
  3293. },
  3294. },
  3295. {
  3296. name: "unknown metadata request subtype",
  3297. reqParams: reqParams{
  3298. instance: state.NewSession().AddInstance(),
  3299. inBody: wire.SNAC_0x15_0x02_BQuery{
  3300. TLVRestBlock: wire.TLVRestBlock{
  3301. TLVList: wire.TLVList{
  3302. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3303. Message: ICQMetaRequest{
  3304. ICQMetadata: wire.ICQMetadata{
  3305. ReqType: wire.ICQDBQueryMetaReq,
  3306. Seq: 1,
  3307. },
  3308. ReqSubType: 0xCA8E,
  3309. MetaRequest: struct{}{},
  3310. },
  3311. }),
  3312. },
  3313. },
  3314. },
  3315. seq: 1,
  3316. wantErr: errUnknownICQMetaReqSubType,
  3317. },
  3318. },
  3319. {
  3320. name: "OfflineMsgReq - happy path",
  3321. reqParams: reqParams{
  3322. instance: state.NewSession().AddInstance(),
  3323. inBody: wire.SNAC_0x15_0x02_BQuery{
  3324. TLVRestBlock: wire.TLVRestBlock{
  3325. TLVList: wire.TLVList{
  3326. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3327. Message: wire.ICQMetadataWithSubType{
  3328. ICQMetadata: wire.ICQMetadata{
  3329. ReqType: wire.ICQDBQueryOfflineMsgReq,
  3330. Seq: 1,
  3331. },
  3332. },
  3333. }),
  3334. },
  3335. },
  3336. },
  3337. seq: 1,
  3338. },
  3339. allMockParams: allMockParams{
  3340. offlineMsgReq: &mockParam{},
  3341. },
  3342. },
  3343. {
  3344. name: "DeleteMsgReq - happy path",
  3345. reqParams: reqParams{
  3346. instance: state.NewSession().AddInstance(),
  3347. inBody: wire.SNAC_0x15_0x02_BQuery{
  3348. TLVRestBlock: wire.TLVRestBlock{
  3349. TLVList: wire.TLVList{
  3350. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3351. Message: wire.ICQMetadataWithSubType{
  3352. ICQMetadata: wire.ICQMetadata{
  3353. ReqType: wire.ICQDBQueryDeleteMsgReq,
  3354. Seq: 1,
  3355. },
  3356. },
  3357. }),
  3358. },
  3359. },
  3360. },
  3361. seq: 1,
  3362. },
  3363. allMockParams: allMockParams{
  3364. deleteMsgReq: &mockParam{},
  3365. },
  3366. },
  3367. {
  3368. name: "unknown request type",
  3369. reqParams: reqParams{
  3370. instance: state.NewSession().AddInstance(),
  3371. inBody: wire.SNAC_0x15_0x02_BQuery{
  3372. TLVRestBlock: wire.TLVRestBlock{
  3373. TLVList: wire.TLVList{
  3374. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3375. Message: ICQMetaRequest{
  3376. ICQMetadata: wire.ICQMetadata{
  3377. ReqType: 0x13B4,
  3378. Seq: 1,
  3379. },
  3380. ReqSubType: 0xCA8E,
  3381. MetaRequest: struct{}{},
  3382. },
  3383. }),
  3384. },
  3385. },
  3386. },
  3387. seq: 1,
  3388. wantErr: errUnknownICQMetaReqType,
  3389. },
  3390. }, // todo: add to a separate test
  3391. }
  3392. for _, tt := range tests {
  3393. t.Run(tt.name, func(t *testing.T) {
  3394. icqService := newMockICQService(t)
  3395. switch {
  3396. case tt.allMockParams.fullUserInfo != nil:
  3397. icqService.EXPECT().
  3398. FullUserInfo(mock.Anything, tt.reqParams.instance, tt.allMockParams.fullUserInfo.req, tt.reqParams.seq).
  3399. Return(tt.allMockParams.fullUserInfo.wantErr)
  3400. case tt.allMockParams.shortUserInfo != nil:
  3401. icqService.EXPECT().
  3402. ShortUserInfo(mock.Anything, tt.reqParams.instance, tt.allMockParams.shortUserInfo.req, tt.reqParams.seq).
  3403. Return(tt.allMockParams.shortUserInfo.wantErr)
  3404. case tt.allMockParams.xmlReqData != nil:
  3405. icqService.EXPECT().
  3406. XMLReqData(mock.Anything, tt.reqParams.instance, tt.allMockParams.xmlReqData.req, tt.reqParams.seq).
  3407. Return(tt.allMockParams.xmlReqData.wantErr)
  3408. case tt.allMockParams.setPermissions != nil:
  3409. icqService.EXPECT().
  3410. SetPermissions(mock.Anything, tt.reqParams.instance, tt.allMockParams.setPermissions.req, tt.reqParams.seq).
  3411. Return(tt.allMockParams.setPermissions.wantErr)
  3412. case tt.allMockParams.findByUIN != nil:
  3413. icqService.EXPECT().
  3414. FindByUIN(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByUIN.req, tt.reqParams.seq).
  3415. Return(tt.allMockParams.findByUIN.wantErr)
  3416. case tt.allMockParams.findByUIN2 != nil:
  3417. icqService.EXPECT().
  3418. FindByUIN2(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByUIN2.req, tt.reqParams.seq).
  3419. Return(tt.allMockParams.findByUIN2.wantErr)
  3420. case tt.allMockParams.findByEmail != nil:
  3421. icqService.EXPECT().
  3422. FindByICQEmail(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByEmail.req, tt.reqParams.seq).
  3423. Return(tt.allMockParams.findByEmail.wantErr)
  3424. case tt.allMockParams.findByEmail3 != nil:
  3425. icqService.EXPECT().
  3426. FindByEmail3(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByEmail3.req, tt.reqParams.seq).
  3427. Return(tt.allMockParams.findByEmail3.wantErr)
  3428. case tt.allMockParams.findByDetails != nil:
  3429. icqService.EXPECT().
  3430. FindByICQName(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByDetails.req, tt.reqParams.seq).
  3431. Return(tt.allMockParams.findByDetails.wantErr)
  3432. case tt.allMockParams.findByInterests != nil:
  3433. icqService.EXPECT().
  3434. FindByICQInterests(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByInterests.req, tt.reqParams.seq).
  3435. Return(tt.allMockParams.findByInterests.wantErr)
  3436. case tt.allMockParams.findByWhitePages2 != nil:
  3437. icqService.EXPECT().
  3438. FindByWhitePages2(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByWhitePages2.req, tt.reqParams.seq).
  3439. Return(tt.allMockParams.findByWhitePages2.wantErr)
  3440. case tt.allMockParams.setBasicInfo != nil:
  3441. icqService.EXPECT().
  3442. SetBasicInfo(mock.Anything, tt.reqParams.instance, tt.allMockParams.setBasicInfo.req, tt.reqParams.seq).
  3443. Return(tt.allMockParams.setBasicInfo.wantErr)
  3444. case tt.allMockParams.setWorkInfo != nil:
  3445. icqService.EXPECT().
  3446. SetWorkInfo(mock.Anything, tt.reqParams.instance, tt.allMockParams.setWorkInfo.req, tt.reqParams.seq).
  3447. Return(tt.allMockParams.setWorkInfo.wantErr)
  3448. case tt.allMockParams.setMoreInfo != nil:
  3449. icqService.EXPECT().
  3450. SetMoreInfo(mock.Anything, tt.reqParams.instance, tt.allMockParams.setMoreInfo.req, tt.reqParams.seq).
  3451. Return(tt.allMockParams.setMoreInfo.wantErr)
  3452. case tt.allMockParams.setUserNotes != nil:
  3453. icqService.EXPECT().
  3454. SetUserNotes(mock.Anything, tt.reqParams.instance, tt.allMockParams.setUserNotes.req, tt.reqParams.seq).
  3455. Return(tt.allMockParams.setUserNotes.wantErr)
  3456. case tt.allMockParams.setEmails != nil:
  3457. icqService.EXPECT().
  3458. SetEmails(mock.Anything, tt.reqParams.instance, tt.allMockParams.setEmails.req, tt.reqParams.seq).
  3459. Return(tt.allMockParams.setEmails.wantErr)
  3460. case tt.allMockParams.setInterests != nil:
  3461. icqService.EXPECT().
  3462. SetInterests(mock.Anything, tt.reqParams.instance, tt.allMockParams.setInterests.req, tt.reqParams.seq).
  3463. Return(tt.allMockParams.setInterests.wantErr)
  3464. case tt.allMockParams.setAffiliations != nil:
  3465. icqService.EXPECT().
  3466. SetAffiliations(mock.Anything, tt.reqParams.instance, tt.allMockParams.setAffiliations.req, tt.reqParams.seq).
  3467. Return(tt.allMockParams.setAffiliations.wantErr)
  3468. case tt.allMockParams.offlineMsgReq != nil:
  3469. icqService.EXPECT().
  3470. OfflineMsgReq(mock.Anything, tt.reqParams.instance, tt.reqParams.seq).
  3471. Return(tt.allMockParams.offlineMsgReq.wantErr)
  3472. case tt.allMockParams.deleteMsgReq != nil:
  3473. icqService.EXPECT().
  3474. DeleteMsgReq(mock.Anything, tt.reqParams.instance, tt.reqParams.seq).
  3475. Return(tt.allMockParams.deleteMsgReq.wantErr)
  3476. }
  3477. h := Handler{
  3478. ICQService: icqService,
  3479. RouteLogger: middleware.RouteLogger{
  3480. Logger: slog.Default(),
  3481. },
  3482. }
  3483. buf := &bytes.Buffer{}
  3484. assert.NoError(t, wire.MarshalBE(tt.reqParams.inBody, buf))
  3485. frame := wire.SNACFrame{
  3486. FoodGroup: wire.ICQ,
  3487. SubGroup: wire.ICQDBQuery,
  3488. }
  3489. err := h.Handle(context.TODO(), wire.BOS, tt.reqParams.instance, frame, buf, nil, config.Listener{})
  3490. assert.ErrorIs(t, err, tt.reqParams.wantErr)
  3491. })
  3492. }
  3493. }
  3494. // Test workaround for QIP 2005 bug where TLV length is incorrect.
  3495. func TestHandler_ICQDBQuery_QIP2005UINSearchBug(t *testing.T) {
  3496. icqService := newMockICQService(t)
  3497. type ICQMetaRequest struct {
  3498. wire.ICQMetadata
  3499. ReqSubType uint16
  3500. MetaRequest any
  3501. }
  3502. expect := wire.ICQ_0x07D0_0x0569_DBQueryMetaReqSearchByUIN2{
  3503. TLVRestBlock: wire.TLVRestBlock{
  3504. TLVList: wire.TLVList{
  3505. wire.NewTLVBE(wire.ICQTLVTagsUIN, uint32(100009)),
  3506. },
  3507. },
  3508. }
  3509. instance := state.NewSession().AddInstance()
  3510. icqService.EXPECT().
  3511. FindByUIN2(mock.Anything, instance, expect, uint16(1)).
  3512. Return(nil)
  3513. h := Handler{
  3514. ICQService: icqService,
  3515. RouteLogger: middleware.RouteLogger{
  3516. Logger: slog.Default(),
  3517. },
  3518. }
  3519. inBody := wire.SNAC_0x15_0x02_BQuery{
  3520. TLVRestBlock: wire.TLVRestBlock{
  3521. TLVList: wire.TLVList{
  3522. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3523. Message: ICQMetaRequest{
  3524. ICQMetadata: wire.ICQMetadata{
  3525. ReqType: wire.ICQDBQueryMetaReq,
  3526. Seq: 1,
  3527. },
  3528. ReqSubType: wire.ICQDBQueryMetaReqSearchByUIN2,
  3529. MetaRequest: wire.ICQ_0x07D0_0x0569_DBQueryMetaReqSearchByUIN2{
  3530. TLVRestBlock: wire.TLVRestBlock{
  3531. TLVList: wire.TLVList{
  3532. wire.NewTLVBE(wire.ICQTLVTagsUIN, uint32(100009)),
  3533. },
  3534. },
  3535. },
  3536. },
  3537. }),
  3538. },
  3539. },
  3540. }
  3541. buf := &bytes.Buffer{}
  3542. assert.NoError(t, wire.MarshalBE(inBody, buf))
  3543. b := buf.Bytes()
  3544. b[18] = 6 // incorrectly set TLV length to 6 (should be 4)
  3545. err := h.ICQDBQuery(nil, instance, wire.SNACFrame{}, buf, nil)
  3546. assert.NoError(t, err)
  3547. }
  3548. func TestHandler_ODirInfoQuery(t *testing.T) {
  3549. tests := []struct {
  3550. name string
  3551. inputBody wire.SNAC_0x0F_0x02_InfoQuery
  3552. serviceError error
  3553. responseError error
  3554. expectedError error
  3555. }{
  3556. {
  3557. name: "success",
  3558. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3559. TLVRestBlock: wire.TLVRestBlock{
  3560. TLVList: wire.TLVList{
  3561. wire.NewTLVBE(1, uint16(2)),
  3562. },
  3563. },
  3564. },
  3565. },
  3566. {
  3567. name: "service error",
  3568. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3569. TLVRestBlock: wire.TLVRestBlock{
  3570. TLVList: wire.TLVList{
  3571. wire.NewTLVBE(1, uint16(2)),
  3572. },
  3573. },
  3574. },
  3575. serviceError: assert.AnError,
  3576. expectedError: assert.AnError,
  3577. },
  3578. {
  3579. name: "response writer error",
  3580. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3581. TLVRestBlock: wire.TLVRestBlock{
  3582. TLVList: wire.TLVList{
  3583. wire.NewTLVBE(1, uint16(2)),
  3584. },
  3585. },
  3586. },
  3587. responseError: assert.AnError,
  3588. expectedError: assert.AnError,
  3589. },
  3590. }
  3591. for _, tt := range tests {
  3592. t.Run(tt.name, func(t *testing.T) {
  3593. input := wire.SNACMessage{
  3594. Frame: wire.SNACFrame{
  3595. FoodGroup: wire.ODir,
  3596. SubGroup: wire.ODirInfoQuery,
  3597. },
  3598. Body: tt.inputBody,
  3599. }
  3600. output := wire.SNACMessage{
  3601. Frame: wire.SNACFrame{
  3602. FoodGroup: wire.ODir,
  3603. SubGroup: wire.ODirInfoReply,
  3604. },
  3605. Body: wire.SNAC_0x0F_0x03_InfoReply{
  3606. Status: 5, // OK has results/not found
  3607. },
  3608. }
  3609. svc := newMockODirService(t)
  3610. svc.EXPECT().
  3611. InfoQuery(mock.Anything, input.Frame, input.Body).
  3612. Return(output, tt.serviceError)
  3613. h := Handler{
  3614. ODirService: svc,
  3615. RouteLogger: middleware.RouteLogger{
  3616. Logger: slog.Default(),
  3617. },
  3618. }
  3619. ss := newMockResponseWriter(t)
  3620. if tt.serviceError == nil {
  3621. ss.EXPECT().
  3622. SendSNAC(output.Frame, output.Body).
  3623. Return(tt.responseError)
  3624. }
  3625. buf := &bytes.Buffer{}
  3626. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3627. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, ss, config.Listener{})
  3628. if tt.expectedError != nil {
  3629. assert.ErrorIs(t, err, tt.expectedError)
  3630. } else {
  3631. assert.NoError(t, err)
  3632. }
  3633. })
  3634. }
  3635. }
  3636. func TestHandler_ODirKeywordListQuery(t *testing.T) {
  3637. tests := []struct {
  3638. name string
  3639. inputBody wire.SNAC_0x0F_0x02_InfoQuery
  3640. serviceError error
  3641. responseError error
  3642. expectedError error
  3643. }{
  3644. {
  3645. name: "success",
  3646. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3647. TLVRestBlock: wire.TLVRestBlock{
  3648. TLVList: wire.TLVList{
  3649. wire.NewTLVBE(1, uint16(2)),
  3650. },
  3651. },
  3652. },
  3653. },
  3654. {
  3655. name: "service error",
  3656. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3657. TLVRestBlock: wire.TLVRestBlock{
  3658. TLVList: wire.TLVList{
  3659. wire.NewTLVBE(1, uint16(2)),
  3660. },
  3661. },
  3662. },
  3663. serviceError: assert.AnError,
  3664. expectedError: assert.AnError,
  3665. },
  3666. {
  3667. name: "response writer error",
  3668. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3669. TLVRestBlock: wire.TLVRestBlock{
  3670. TLVList: wire.TLVList{
  3671. wire.NewTLVBE(1, uint16(2)),
  3672. },
  3673. },
  3674. },
  3675. responseError: assert.AnError,
  3676. expectedError: assert.AnError,
  3677. },
  3678. }
  3679. for _, tt := range tests {
  3680. t.Run(tt.name, func(t *testing.T) {
  3681. input := wire.SNACMessage{
  3682. Frame: wire.SNACFrame{
  3683. FoodGroup: wire.ODir,
  3684. SubGroup: wire.ODirKeywordListQuery,
  3685. },
  3686. Body: tt.inputBody,
  3687. }
  3688. output := wire.SNACMessage{
  3689. Frame: wire.SNACFrame{
  3690. FoodGroup: wire.ODir,
  3691. SubGroup: wire.ODirKeywordListReply,
  3692. },
  3693. Body: wire.SNAC_0x0F_0x04_KeywordListReply{
  3694. Status: 0x01,
  3695. },
  3696. }
  3697. svc := newMockODirService(t)
  3698. svc.EXPECT().
  3699. KeywordListQuery(mock.Anything, input.Frame).
  3700. Return(output, tt.serviceError)
  3701. h := Handler{
  3702. ODirService: svc,
  3703. RouteLogger: middleware.RouteLogger{
  3704. Logger: slog.Default(),
  3705. },
  3706. }
  3707. ss := newMockResponseWriter(t)
  3708. if tt.serviceError == nil {
  3709. ss.EXPECT().
  3710. SendSNAC(output.Frame, output.Body).
  3711. Return(tt.responseError)
  3712. }
  3713. buf := &bytes.Buffer{}
  3714. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3715. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, ss, config.Listener{})
  3716. if tt.expectedError != nil {
  3717. assert.ErrorIs(t, err, tt.expectedError)
  3718. } else {
  3719. assert.NoError(t, err)
  3720. }
  3721. })
  3722. }
  3723. }
  3724. func TestHandler_OServiceServiceClientOnline(t *testing.T) {
  3725. tests := []struct {
  3726. name string
  3727. inputBody wire.SNAC_0x01_0x02_OServiceClientOnline
  3728. serviceError error
  3729. expectedError error
  3730. }{
  3731. {
  3732. name: "success",
  3733. inputBody: wire.SNAC_0x01_0x02_OServiceClientOnline{
  3734. GroupVersions: []struct {
  3735. FoodGroup uint16
  3736. Version uint16
  3737. ToolID uint16
  3738. ToolVersion uint16
  3739. }{
  3740. {
  3741. FoodGroup: 10,
  3742. },
  3743. },
  3744. },
  3745. },
  3746. {
  3747. name: "service error",
  3748. inputBody: wire.SNAC_0x01_0x02_OServiceClientOnline{
  3749. GroupVersions: []struct {
  3750. FoodGroup uint16
  3751. Version uint16
  3752. ToolID uint16
  3753. ToolVersion uint16
  3754. }{
  3755. {
  3756. FoodGroup: 10,
  3757. },
  3758. },
  3759. },
  3760. serviceError: assert.AnError,
  3761. expectedError: assert.AnError,
  3762. },
  3763. }
  3764. for _, tt := range tests {
  3765. t.Run(tt.name, func(t *testing.T) {
  3766. input := wire.SNACMessage{
  3767. Frame: wire.SNACFrame{
  3768. FoodGroup: wire.OService,
  3769. SubGroup: wire.OServiceClientOnline,
  3770. },
  3771. Body: tt.inputBody,
  3772. }
  3773. svc := newMockOServiceService(t)
  3774. svc.EXPECT().
  3775. ClientOnline(mock.Anything, wire.BOS, input.Body, mock.Anything).
  3776. Return(tt.serviceError)
  3777. h := Handler{
  3778. OServiceService: svc,
  3779. RouteLogger: middleware.RouteLogger{
  3780. Logger: slog.Default(),
  3781. },
  3782. }
  3783. buf := &bytes.Buffer{}
  3784. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3785. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, nil, config.Listener{})
  3786. if tt.expectedError != nil {
  3787. assert.ErrorIs(t, err, tt.expectedError)
  3788. } else {
  3789. assert.NoError(t, err)
  3790. }
  3791. })
  3792. }
  3793. }
  3794. func TestHandler_OServiceServiceServiceRequest(t *testing.T) {
  3795. tests := []struct {
  3796. name string
  3797. inputBody wire.SNAC_0x01_0x04_OServiceServiceRequest
  3798. serviceError error
  3799. responseError error
  3800. expectedError error
  3801. }{
  3802. {
  3803. name: "success",
  3804. inputBody: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  3805. FoodGroup: wire.Chat,
  3806. },
  3807. },
  3808. {
  3809. name: "service error",
  3810. inputBody: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  3811. FoodGroup: wire.Chat,
  3812. },
  3813. serviceError: assert.AnError,
  3814. expectedError: assert.AnError,
  3815. },
  3816. {
  3817. name: "response writer error",
  3818. inputBody: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  3819. FoodGroup: wire.Chat,
  3820. },
  3821. responseError: assert.AnError,
  3822. expectedError: assert.AnError,
  3823. },
  3824. }
  3825. for _, tt := range tests {
  3826. t.Run(tt.name, func(t *testing.T) {
  3827. input := wire.SNACMessage{
  3828. Frame: wire.SNACFrame{
  3829. FoodGroup: wire.OService,
  3830. SubGroup: wire.OServiceServiceRequest,
  3831. },
  3832. Body: tt.inputBody,
  3833. }
  3834. output := wire.SNACMessage{
  3835. Frame: wire.SNACFrame{
  3836. FoodGroup: wire.OService,
  3837. SubGroup: wire.OServiceServiceResponse,
  3838. },
  3839. Body: wire.SNAC_0x01_0x05_OServiceServiceResponse{
  3840. TLVRestBlock: wire.TLVRestBlock{
  3841. TLVList: wire.TLVList{
  3842. wire.NewTLVBE(0x01, uint16(1000)),
  3843. },
  3844. },
  3845. },
  3846. }
  3847. svc := newMockOServiceService(t)
  3848. svc.EXPECT().
  3849. ServiceRequest(mock.Anything, wire.BOS, mock.Anything, input.Frame, input.Body, config.Listener{BOSAdvertisedHostPlain: "127.0.0.1:1234"}).
  3850. Return(output, tt.serviceError)
  3851. h := Handler{
  3852. OServiceService: svc,
  3853. RouteLogger: middleware.RouteLogger{
  3854. Logger: slog.Default(),
  3855. },
  3856. }
  3857. responseWriter := newMockResponseWriter(t)
  3858. if tt.serviceError == nil {
  3859. responseWriter.EXPECT().
  3860. SendSNAC(output.Frame, output.Body).
  3861. Return(tt.responseError)
  3862. }
  3863. buf := &bytes.Buffer{}
  3864. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3865. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{BOSAdvertisedHostPlain: "127.0.0.1:1234"})
  3866. if tt.expectedError != nil {
  3867. assert.ErrorIs(t, err, tt.expectedError)
  3868. } else {
  3869. assert.NoError(t, err)
  3870. }
  3871. })
  3872. }
  3873. }
  3874. func TestHandler_OServiceServiceIdleNotification(t *testing.T) {
  3875. tests := []struct {
  3876. name string
  3877. inputBody wire.SNAC_0x01_0x11_OServiceIdleNotification
  3878. serviceError error
  3879. expectedError error
  3880. }{
  3881. {
  3882. name: "success",
  3883. inputBody: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  3884. IdleTime: 10,
  3885. },
  3886. },
  3887. {
  3888. name: "service error",
  3889. inputBody: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  3890. IdleTime: 10,
  3891. },
  3892. serviceError: assert.AnError,
  3893. expectedError: assert.AnError,
  3894. },
  3895. }
  3896. for _, tt := range tests {
  3897. t.Run(tt.name, func(t *testing.T) {
  3898. input := wire.SNACMessage{
  3899. Frame: wire.SNACFrame{
  3900. FoodGroup: wire.OService,
  3901. SubGroup: wire.OServiceIdleNotification,
  3902. },
  3903. Body: tt.inputBody,
  3904. }
  3905. svc := newMockOServiceService(t)
  3906. svc.EXPECT().
  3907. IdleNotification(mock.Anything, mock.Anything, input.Body).
  3908. Return(tt.serviceError)
  3909. h := Handler{
  3910. OServiceService: svc,
  3911. RouteLogger: middleware.RouteLogger{
  3912. Logger: slog.Default(),
  3913. },
  3914. }
  3915. responseWriter := newMockResponseWriter(t)
  3916. buf := &bytes.Buffer{}
  3917. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3918. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  3919. if tt.expectedError != nil {
  3920. assert.ErrorIs(t, err, tt.expectedError)
  3921. } else {
  3922. assert.NoError(t, err)
  3923. }
  3924. })
  3925. }
  3926. }
  3927. func TestHandler_OServiceServiceClientVersions(t *testing.T) {
  3928. tests := []struct {
  3929. name string
  3930. inputBody wire.SNAC_0x01_0x17_OServiceClientVersions
  3931. responseError error
  3932. expectedError error
  3933. }{
  3934. {
  3935. name: "success",
  3936. inputBody: wire.SNAC_0x01_0x17_OServiceClientVersions{
  3937. Versions: []uint16{
  3938. 10,
  3939. },
  3940. },
  3941. },
  3942. {
  3943. name: "response writer error",
  3944. inputBody: wire.SNAC_0x01_0x17_OServiceClientVersions{
  3945. Versions: []uint16{
  3946. 10,
  3947. },
  3948. },
  3949. responseError: assert.AnError,
  3950. expectedError: assert.AnError,
  3951. },
  3952. }
  3953. for _, tt := range tests {
  3954. t.Run(tt.name, func(t *testing.T) {
  3955. input := wire.SNACMessage{
  3956. Frame: wire.SNACFrame{
  3957. FoodGroup: wire.OService,
  3958. SubGroup: wire.OServiceClientVersions,
  3959. },
  3960. Body: tt.inputBody,
  3961. }
  3962. output := []wire.SNACMessage{
  3963. {
  3964. Frame: wire.SNACFrame{
  3965. FoodGroup: wire.OService,
  3966. SubGroup: wire.OServiceHostVersions,
  3967. },
  3968. Body: wire.SNAC_0x01_0x18_OServiceHostVersions{
  3969. Versions: []uint16{
  3970. 10,
  3971. },
  3972. },
  3973. },
  3974. {
  3975. Frame: wire.SNACFrame{
  3976. FoodGroup: wire.OService,
  3977. SubGroup: wire.OServiceMotd,
  3978. RequestID: wire.ReqIDFromServer,
  3979. },
  3980. Body: wire.SNAC_0x01_0x13_OServiceMOTD{
  3981. MessageType: 0x0004,
  3982. TLVRestBlock: wire.TLVRestBlock{
  3983. TLVList: wire.TLVList{
  3984. wire.NewTLVBE(wire.OServiceTLVTagsMOTDMessage, "Welcome to Open OSCAR Server"),
  3985. },
  3986. },
  3987. },
  3988. },
  3989. }
  3990. instance := state.NewSession().AddInstance()
  3991. svc := newMockOServiceService(t)
  3992. svc.EXPECT().
  3993. ClientVersions(mock.Anything, instance, input.Frame, input.Body).
  3994. Return(output)
  3995. h := Handler{
  3996. OServiceService: svc,
  3997. RouteLogger: middleware.RouteLogger{
  3998. Logger: slog.Default(),
  3999. },
  4000. }
  4001. responseWriter := newMockResponseWriter(t)
  4002. if tt.responseError == nil {
  4003. for _, snac := range output {
  4004. responseWriter.EXPECT().
  4005. SendSNAC(snac.Frame, snac.Body).
  4006. Return(nil)
  4007. }
  4008. } else {
  4009. responseWriter.EXPECT().
  4010. SendSNAC(output[0].Frame, output[0].Body).
  4011. Return(tt.responseError)
  4012. }
  4013. buf := &bytes.Buffer{}
  4014. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4015. err := h.Handle(context.Background(), wire.BOS, instance, input.Frame, buf, responseWriter, config.Listener{})
  4016. if tt.expectedError != nil {
  4017. assert.ErrorIs(t, err, tt.expectedError)
  4018. } else {
  4019. assert.NoError(t, err)
  4020. }
  4021. })
  4022. }
  4023. }
  4024. func TestHandler_OServiceServiceRateParamsQuery(t *testing.T) {
  4025. tests := []struct {
  4026. name string
  4027. inputBody struct{}
  4028. responseError error
  4029. expectedError error
  4030. }{
  4031. {
  4032. name: "success",
  4033. inputBody: struct{}{},
  4034. },
  4035. {
  4036. name: "response writer error",
  4037. inputBody: struct{}{},
  4038. responseError: assert.AnError,
  4039. expectedError: assert.AnError,
  4040. },
  4041. }
  4042. for _, tt := range tests {
  4043. t.Run(tt.name, func(t *testing.T) {
  4044. input := wire.SNACMessage{
  4045. Frame: wire.SNACFrame{
  4046. FoodGroup: wire.OService,
  4047. SubGroup: wire.OServiceRateParamsQuery,
  4048. },
  4049. Body: tt.inputBody,
  4050. }
  4051. output := wire.SNACMessage{
  4052. Frame: wire.SNACFrame{
  4053. FoodGroup: wire.OService,
  4054. SubGroup: wire.OServiceRateParamsReply,
  4055. },
  4056. Body: wire.SNAC_0x01_0x07_OServiceRateParamsReply{
  4057. RateGroups: []struct {
  4058. ID uint16
  4059. Pairs []struct {
  4060. FoodGroup uint16
  4061. SubGroup uint16
  4062. } `oscar:"count_prefix=uint16"`
  4063. }{
  4064. {
  4065. ID: 1,
  4066. },
  4067. },
  4068. },
  4069. }
  4070. svc := newMockOServiceService(t)
  4071. svc.EXPECT().
  4072. RateParamsQuery(mock.Anything, mock.Anything, input.Frame).
  4073. Return(output)
  4074. h := Handler{
  4075. OServiceService: svc,
  4076. RouteLogger: middleware.RouteLogger{
  4077. Logger: slog.Default(),
  4078. },
  4079. }
  4080. responseWriter := newMockResponseWriter(t)
  4081. responseWriter.EXPECT().
  4082. SendSNAC(output.Frame, output.Body).
  4083. Return(tt.responseError)
  4084. buf := &bytes.Buffer{}
  4085. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4086. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4087. if tt.expectedError != nil {
  4088. assert.ErrorIs(t, err, tt.expectedError)
  4089. } else {
  4090. assert.NoError(t, err)
  4091. }
  4092. })
  4093. }
  4094. }
  4095. func TestHandler_OServiceServiceRateParamsSubAdd(t *testing.T) {
  4096. tests := []struct {
  4097. name string
  4098. inputBody wire.SNAC_0x01_0x08_OServiceRateParamsSubAdd
  4099. expectedError error
  4100. }{
  4101. {
  4102. name: "success",
  4103. inputBody: wire.SNAC_0x01_0x08_OServiceRateParamsSubAdd{
  4104. ClassIDs: []uint16{1, 2, 3, 4},
  4105. },
  4106. },
  4107. }
  4108. for _, tt := range tests {
  4109. t.Run(tt.name, func(t *testing.T) {
  4110. input := wire.SNACMessage{
  4111. Frame: wire.SNACFrame{
  4112. FoodGroup: wire.OService,
  4113. SubGroup: wire.OServiceRateParamsSubAdd,
  4114. },
  4115. Body: tt.inputBody,
  4116. }
  4117. instance := state.NewSession().AddInstance()
  4118. svc := newMockOServiceService(t)
  4119. svc.EXPECT().
  4120. RateParamsSubAdd(mock.Anything, instance, input.Body)
  4121. h := Handler{
  4122. OServiceService: svc,
  4123. RouteLogger: middleware.RouteLogger{
  4124. Logger: slog.Default(),
  4125. },
  4126. }
  4127. buf := &bytes.Buffer{}
  4128. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4129. err := h.Handle(context.Background(), wire.BOS, instance, input.Frame, buf, nil, config.Listener{})
  4130. if tt.expectedError != nil {
  4131. assert.ErrorIs(t, err, tt.expectedError)
  4132. } else {
  4133. assert.NoError(t, err)
  4134. }
  4135. })
  4136. }
  4137. }
  4138. func TestHandler_OServiceServiceSetUserInfoFields(t *testing.T) {
  4139. tests := []struct {
  4140. name string
  4141. inputBody wire.SNAC_0x01_0x1E_OServiceSetUserInfoFields
  4142. serviceError error
  4143. responseError error
  4144. expectedError error
  4145. }{
  4146. {
  4147. name: "success",
  4148. inputBody: wire.SNAC_0x01_0x1E_OServiceSetUserInfoFields{
  4149. TLVRestBlock: wire.TLVRestBlock{
  4150. TLVList: wire.TLVList{
  4151. wire.NewTLVBE(0x01, []byte{1, 2, 3, 4}),
  4152. },
  4153. },
  4154. },
  4155. },
  4156. {
  4157. name: "service error",
  4158. inputBody: wire.SNAC_0x01_0x1E_OServiceSetUserInfoFields{
  4159. TLVRestBlock: wire.TLVRestBlock{
  4160. TLVList: wire.TLVList{
  4161. wire.NewTLVBE(0x01, []byte{1, 2, 3, 4}),
  4162. },
  4163. },
  4164. },
  4165. serviceError: assert.AnError,
  4166. expectedError: assert.AnError,
  4167. },
  4168. {
  4169. name: "response writer error",
  4170. inputBody: wire.SNAC_0x01_0x1E_OServiceSetUserInfoFields{
  4171. TLVRestBlock: wire.TLVRestBlock{
  4172. TLVList: wire.TLVList{
  4173. wire.NewTLVBE(0x01, []byte{1, 2, 3, 4}),
  4174. },
  4175. },
  4176. },
  4177. responseError: assert.AnError,
  4178. expectedError: assert.AnError,
  4179. },
  4180. }
  4181. for _, tt := range tests {
  4182. t.Run(tt.name, func(t *testing.T) {
  4183. input := wire.SNACMessage{
  4184. Frame: wire.SNACFrame{
  4185. FoodGroup: wire.OService,
  4186. SubGroup: wire.OServiceSetUserInfoFields,
  4187. },
  4188. Body: tt.inputBody,
  4189. }
  4190. output := wire.SNACMessage{
  4191. Frame: wire.SNACFrame{
  4192. FoodGroup: wire.OService,
  4193. SubGroup: wire.OServiceUserInfoUpdate,
  4194. },
  4195. Body: wire.SNAC_0x01_0x0F_OServiceUserInfoUpdate{
  4196. UserInfo: []wire.TLVUserInfo{
  4197. {ScreenName: "screen-name"},
  4198. {ScreenName: "screen-name"},
  4199. },
  4200. },
  4201. }
  4202. svc := newMockOServiceService(t)
  4203. svc.EXPECT().
  4204. SetUserInfoFields(mock.Anything, mock.Anything, input.Frame, input.Body).
  4205. Return(output, tt.serviceError)
  4206. h := Handler{
  4207. OServiceService: svc,
  4208. RouteLogger: middleware.RouteLogger{
  4209. Logger: slog.Default(),
  4210. },
  4211. }
  4212. responseWriter := newMockResponseWriter(t)
  4213. if tt.serviceError == nil {
  4214. responseWriter.EXPECT().
  4215. SendSNAC(output.Frame, output.Body).
  4216. Return(tt.responseError)
  4217. }
  4218. buf := &bytes.Buffer{}
  4219. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4220. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4221. if tt.expectedError != nil {
  4222. assert.ErrorIs(t, err, tt.expectedError)
  4223. } else {
  4224. assert.NoError(t, err)
  4225. }
  4226. })
  4227. }
  4228. }
  4229. func TestHandler_OServiceServiceUserInfoQuery(t *testing.T) {
  4230. tests := []struct {
  4231. name string
  4232. inputBody struct{}
  4233. responseError error
  4234. expectedError error
  4235. }{
  4236. {
  4237. name: "success",
  4238. inputBody: struct{}{},
  4239. },
  4240. {
  4241. name: "response writer error",
  4242. inputBody: struct{}{},
  4243. responseError: assert.AnError,
  4244. expectedError: assert.AnError,
  4245. },
  4246. }
  4247. for _, tt := range tests {
  4248. t.Run(tt.name, func(t *testing.T) {
  4249. input := wire.SNACMessage{
  4250. Frame: wire.SNACFrame{
  4251. FoodGroup: wire.OService,
  4252. SubGroup: wire.OServiceUserInfoQuery,
  4253. },
  4254. Body: tt.inputBody,
  4255. }
  4256. output := wire.SNACMessage{
  4257. Frame: wire.SNACFrame{
  4258. FoodGroup: wire.OService,
  4259. SubGroup: wire.OServiceUserInfoUpdate,
  4260. },
  4261. Body: wire.SNAC_0x01_0x0F_OServiceUserInfoUpdate{
  4262. UserInfo: []wire.TLVUserInfo{
  4263. {ScreenName: "screen-name"},
  4264. {ScreenName: "screen-name"},
  4265. },
  4266. },
  4267. }
  4268. svc := newMockOServiceService(t)
  4269. svc.EXPECT().
  4270. UserInfoQuery(mock.Anything, mock.Anything, input.Frame).
  4271. Return(output)
  4272. h := Handler{
  4273. OServiceService: svc,
  4274. RouteLogger: middleware.RouteLogger{
  4275. Logger: slog.Default(),
  4276. },
  4277. }
  4278. responseWriter := newMockResponseWriter(t)
  4279. responseWriter.EXPECT().
  4280. SendSNAC(output.Frame, output.Body).
  4281. Return(tt.responseError)
  4282. buf := &bytes.Buffer{}
  4283. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4284. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4285. if tt.expectedError != nil {
  4286. assert.ErrorIs(t, err, tt.expectedError)
  4287. } else {
  4288. assert.NoError(t, err)
  4289. }
  4290. })
  4291. }
  4292. }
  4293. func TestHandler_OServiceServiceNoop(t *testing.T) {
  4294. tests := []struct {
  4295. name string
  4296. inputBody struct{}
  4297. expectedError error
  4298. }{
  4299. {
  4300. name: "success",
  4301. inputBody: struct{}{},
  4302. },
  4303. }
  4304. for _, tt := range tests {
  4305. t.Run(tt.name, func(t *testing.T) {
  4306. input := wire.SNACMessage{
  4307. Frame: wire.SNACFrame{
  4308. FoodGroup: wire.OService,
  4309. SubGroup: wire.OServiceNoop,
  4310. },
  4311. Body: tt.inputBody,
  4312. }
  4313. h := Handler{
  4314. RouteLogger: middleware.RouteLogger{
  4315. Logger: slog.Default(),
  4316. },
  4317. }
  4318. responseWriter := newMockResponseWriter(t)
  4319. buf := &bytes.Buffer{}
  4320. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4321. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4322. if tt.expectedError != nil {
  4323. assert.ErrorIs(t, err, tt.expectedError)
  4324. } else {
  4325. assert.NoError(t, err)
  4326. }
  4327. })
  4328. }
  4329. }
  4330. func TestHandler_OServiceServiceSetPrivacyFlags(t *testing.T) {
  4331. tests := []struct {
  4332. name string
  4333. inputBody wire.SNAC_0x01_0x14_OServiceSetPrivacyFlags
  4334. expectedError error
  4335. }{
  4336. {
  4337. name: "success",
  4338. inputBody: wire.SNAC_0x01_0x14_OServiceSetPrivacyFlags{
  4339. PrivacyFlags: wire.OServicePrivacyFlagMember,
  4340. },
  4341. },
  4342. }
  4343. for _, tt := range tests {
  4344. t.Run(tt.name, func(t *testing.T) {
  4345. input := wire.SNACMessage{
  4346. Frame: wire.SNACFrame{
  4347. FoodGroup: wire.OService,
  4348. SubGroup: wire.OServiceSetPrivacyFlags,
  4349. },
  4350. Body: tt.inputBody,
  4351. }
  4352. svc := newMockOServiceService(t)
  4353. svc.EXPECT().
  4354. SetPrivacyFlags(mock.Anything, input.Body)
  4355. h := Handler{
  4356. OServiceService: svc,
  4357. RouteLogger: middleware.RouteLogger{
  4358. Logger: slog.Default(),
  4359. },
  4360. }
  4361. responseWriter := newMockResponseWriter(t)
  4362. buf := &bytes.Buffer{}
  4363. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4364. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4365. if tt.expectedError != nil {
  4366. assert.ErrorIs(t, err, tt.expectedError)
  4367. } else {
  4368. assert.NoError(t, err)
  4369. }
  4370. })
  4371. }
  4372. }
  4373. func TestHandler_PermitDenyRightsQuery(t *testing.T) {
  4374. tests := []struct {
  4375. name string
  4376. inputBody struct{}
  4377. responseError error
  4378. expectedError error
  4379. }{
  4380. {
  4381. name: "success",
  4382. inputBody: struct{}{},
  4383. },
  4384. {
  4385. name: "response writer error",
  4386. inputBody: struct{}{},
  4387. responseError: assert.AnError,
  4388. expectedError: assert.AnError,
  4389. },
  4390. }
  4391. for _, tt := range tests {
  4392. t.Run(tt.name, func(t *testing.T) {
  4393. input := wire.SNACMessage{
  4394. Frame: wire.SNACFrame{
  4395. FoodGroup: wire.PermitDeny,
  4396. SubGroup: wire.PermitDenyRightsQuery,
  4397. },
  4398. Body: tt.inputBody,
  4399. }
  4400. output := wire.SNACMessage{
  4401. Frame: wire.SNACFrame{
  4402. FoodGroup: wire.PermitDeny,
  4403. SubGroup: wire.PermitDenyRightsReply,
  4404. },
  4405. Body: wire.SNAC_0x09_0x03_PermitDenyRightsReply{
  4406. TLVRestBlock: wire.TLVRestBlock{
  4407. TLVList: wire.TLVList{
  4408. wire.NewTLVBE(0x01, uint16(1000)),
  4409. },
  4410. },
  4411. },
  4412. }
  4413. svc := newMockPermitDenyService(t)
  4414. svc.EXPECT().
  4415. RightsQuery(mock.Anything, input.Frame).
  4416. Return(output)
  4417. h := Handler{
  4418. PermitDenyService: svc,
  4419. RouteLogger: middleware.RouteLogger{
  4420. Logger: slog.Default(),
  4421. },
  4422. }
  4423. responseWriter := newMockResponseWriter(t)
  4424. responseWriter.EXPECT().
  4425. SendSNAC(output.Frame, output.Body).
  4426. Return(tt.responseError)
  4427. buf := &bytes.Buffer{}
  4428. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4429. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4430. if tt.expectedError != nil {
  4431. assert.ErrorIs(t, err, tt.expectedError)
  4432. } else {
  4433. assert.NoError(t, err)
  4434. }
  4435. })
  4436. }
  4437. }
  4438. func TestHandler_PermitDenyAddDenyListEntries(t *testing.T) {
  4439. tests := []struct {
  4440. name string
  4441. inputBody wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries
  4442. serviceError error
  4443. expectedError error
  4444. }{
  4445. {
  4446. name: "success",
  4447. inputBody: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  4448. Users: []struct {
  4449. ScreenName string `oscar:"len_prefix=uint8"`
  4450. }{
  4451. {
  4452. ScreenName: "friend1",
  4453. },
  4454. {
  4455. ScreenName: "friend2",
  4456. },
  4457. },
  4458. },
  4459. },
  4460. {
  4461. name: "service error",
  4462. inputBody: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  4463. Users: []struct {
  4464. ScreenName string `oscar:"len_prefix=uint8"`
  4465. }{
  4466. {
  4467. ScreenName: "friend1",
  4468. },
  4469. {
  4470. ScreenName: "friend2",
  4471. },
  4472. },
  4473. },
  4474. serviceError: assert.AnError,
  4475. expectedError: assert.AnError,
  4476. },
  4477. }
  4478. for _, tt := range tests {
  4479. t.Run(tt.name, func(t *testing.T) {
  4480. instance := state.NewSession().AddInstance()
  4481. input := wire.SNACMessage{
  4482. Frame: wire.SNACFrame{
  4483. FoodGroup: wire.PermitDeny,
  4484. SubGroup: wire.PermitDenyAddDenyListEntries,
  4485. },
  4486. Body: tt.inputBody,
  4487. }
  4488. svc := newMockPermitDenyService(t)
  4489. svc.EXPECT().
  4490. AddDenyListEntries(mock.Anything, instance, input.Body).
  4491. Return(tt.serviceError)
  4492. h := Handler{
  4493. PermitDenyService: svc,
  4494. RouteLogger: middleware.RouteLogger{
  4495. Logger: slog.Default(),
  4496. },
  4497. }
  4498. buf := &bytes.Buffer{}
  4499. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4500. err := h.Handle(context.TODO(), wire.BOS, instance, input.Frame, buf, nil, config.Listener{})
  4501. if tt.expectedError != nil {
  4502. assert.ErrorIs(t, err, tt.expectedError)
  4503. } else {
  4504. assert.NoError(t, err)
  4505. }
  4506. })
  4507. }
  4508. }
  4509. func TestHandler_PermitDenyDelDenyListEntries(t *testing.T) {
  4510. tests := []struct {
  4511. name string
  4512. inputBody wire.SNAC_0x09_0x08_PermitDenyDelDenyListEntries
  4513. serviceError error
  4514. expectedError error
  4515. }{
  4516. {
  4517. name: "success",
  4518. inputBody: wire.SNAC_0x09_0x08_PermitDenyDelDenyListEntries{
  4519. Users: []struct {
  4520. ScreenName string `oscar:"len_prefix=uint8"`
  4521. }{
  4522. {
  4523. ScreenName: "friend1",
  4524. },
  4525. {
  4526. ScreenName: "friend2",
  4527. },
  4528. },
  4529. },
  4530. },
  4531. {
  4532. name: "service error",
  4533. inputBody: wire.SNAC_0x09_0x08_PermitDenyDelDenyListEntries{
  4534. Users: []struct {
  4535. ScreenName string `oscar:"len_prefix=uint8"`
  4536. }{
  4537. {
  4538. ScreenName: "friend1",
  4539. },
  4540. {
  4541. ScreenName: "friend2",
  4542. },
  4543. },
  4544. },
  4545. serviceError: assert.AnError,
  4546. expectedError: assert.AnError,
  4547. },
  4548. }
  4549. for _, tt := range tests {
  4550. t.Run(tt.name, func(t *testing.T) {
  4551. instance := state.NewSession().AddInstance()
  4552. input := wire.SNACMessage{
  4553. Frame: wire.SNACFrame{
  4554. FoodGroup: wire.PermitDeny,
  4555. SubGroup: wire.PermitDenyDelDenyListEntries,
  4556. },
  4557. Body: tt.inputBody,
  4558. }
  4559. svc := newMockPermitDenyService(t)
  4560. svc.EXPECT().
  4561. DelDenyListEntries(mock.Anything, instance, input.Body).
  4562. Return(tt.serviceError)
  4563. h := Handler{
  4564. PermitDenyService: svc,
  4565. RouteLogger: middleware.RouteLogger{
  4566. Logger: slog.Default(),
  4567. },
  4568. }
  4569. buf := &bytes.Buffer{}
  4570. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4571. err := h.Handle(context.TODO(), wire.BOS, instance, input.Frame, buf, nil, config.Listener{})
  4572. if tt.expectedError != nil {
  4573. assert.ErrorIs(t, err, tt.expectedError)
  4574. } else {
  4575. assert.NoError(t, err)
  4576. }
  4577. })
  4578. }
  4579. }
  4580. func TestHandler_PermitDenyAddPermListEntries(t *testing.T) {
  4581. tests := []struct {
  4582. name string
  4583. inputBody wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries
  4584. serviceError error
  4585. expectedError error
  4586. }{
  4587. {
  4588. name: "success",
  4589. inputBody: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  4590. Users: []struct {
  4591. ScreenName string `oscar:"len_prefix=uint8"`
  4592. }{
  4593. {
  4594. ScreenName: "friend1",
  4595. },
  4596. {
  4597. ScreenName: "friend2",
  4598. },
  4599. },
  4600. },
  4601. },
  4602. {
  4603. name: "service error",
  4604. inputBody: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  4605. Users: []struct {
  4606. ScreenName string `oscar:"len_prefix=uint8"`
  4607. }{
  4608. {
  4609. ScreenName: "friend1",
  4610. },
  4611. {
  4612. ScreenName: "friend2",
  4613. },
  4614. },
  4615. },
  4616. serviceError: assert.AnError,
  4617. expectedError: assert.AnError,
  4618. },
  4619. }
  4620. for _, tt := range tests {
  4621. t.Run(tt.name, func(t *testing.T) {
  4622. instance := state.NewSession().AddInstance()
  4623. input := wire.SNACMessage{
  4624. Frame: wire.SNACFrame{
  4625. FoodGroup: wire.PermitDeny,
  4626. SubGroup: wire.PermitDenyAddPermListEntries,
  4627. },
  4628. Body: tt.inputBody,
  4629. }
  4630. svc := newMockPermitDenyService(t)
  4631. svc.EXPECT().
  4632. AddPermListEntries(mock.Anything, instance, input.Body).
  4633. Return(tt.serviceError)
  4634. h := Handler{
  4635. PermitDenyService: svc,
  4636. RouteLogger: middleware.RouteLogger{
  4637. Logger: slog.Default(),
  4638. },
  4639. }
  4640. buf := &bytes.Buffer{}
  4641. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4642. err := h.Handle(context.TODO(), wire.BOS, instance, input.Frame, buf, nil, config.Listener{})
  4643. if tt.expectedError != nil {
  4644. assert.ErrorIs(t, err, tt.expectedError)
  4645. } else {
  4646. assert.NoError(t, err)
  4647. }
  4648. })
  4649. }
  4650. }
  4651. func TestHandler_PermitDenyDelPermListEntries(t *testing.T) {
  4652. tests := []struct {
  4653. name string
  4654. inputBody wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries
  4655. serviceError error
  4656. expectedError error
  4657. }{
  4658. {
  4659. name: "success",
  4660. inputBody: wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries{
  4661. Users: []struct {
  4662. ScreenName string `oscar:"len_prefix=uint8"`
  4663. }{
  4664. {
  4665. ScreenName: "friend1",
  4666. },
  4667. {
  4668. ScreenName: "friend2",
  4669. },
  4670. },
  4671. },
  4672. },
  4673. {
  4674. name: "service error",
  4675. inputBody: wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries{
  4676. Users: []struct {
  4677. ScreenName string `oscar:"len_prefix=uint8"`
  4678. }{
  4679. {
  4680. ScreenName: "friend1",
  4681. },
  4682. {
  4683. ScreenName: "friend2",
  4684. },
  4685. },
  4686. },
  4687. serviceError: assert.AnError,
  4688. expectedError: assert.AnError,
  4689. },
  4690. }
  4691. for _, tt := range tests {
  4692. t.Run(tt.name, func(t *testing.T) {
  4693. instance := state.NewSession().AddInstance()
  4694. input := wire.SNACMessage{
  4695. Frame: wire.SNACFrame{
  4696. FoodGroup: wire.PermitDeny,
  4697. SubGroup: wire.PermitDenyDelPermListEntries,
  4698. },
  4699. Body: tt.inputBody,
  4700. }
  4701. svc := newMockPermitDenyService(t)
  4702. svc.EXPECT().
  4703. DelPermListEntries(mock.Anything, instance, input.Body).
  4704. Return(tt.serviceError)
  4705. h := Handler{
  4706. PermitDenyService: svc,
  4707. RouteLogger: middleware.RouteLogger{
  4708. Logger: slog.Default(),
  4709. },
  4710. }
  4711. buf := &bytes.Buffer{}
  4712. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4713. err := h.Handle(context.TODO(), wire.BOS, instance, input.Frame, buf, nil, config.Listener{})
  4714. if tt.expectedError != nil {
  4715. assert.ErrorIs(t, err, tt.expectedError)
  4716. } else {
  4717. assert.NoError(t, err)
  4718. }
  4719. })
  4720. }
  4721. }
  4722. func TestHandler_PermitDenySetGroupPermitMask(t *testing.T) {
  4723. tests := []struct {
  4724. name string
  4725. inputBody wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries
  4726. expectedError error
  4727. }{
  4728. {
  4729. name: "success",
  4730. inputBody: wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries{
  4731. Users: []struct {
  4732. ScreenName string `oscar:"len_prefix=uint8"`
  4733. }{
  4734. {
  4735. ScreenName: "friend1",
  4736. },
  4737. },
  4738. },
  4739. },
  4740. }
  4741. for _, tt := range tests {
  4742. t.Run(tt.name, func(t *testing.T) {
  4743. instance := state.NewSession().AddInstance()
  4744. input := wire.SNACMessage{
  4745. Frame: wire.SNACFrame{
  4746. FoodGroup: wire.PermitDeny,
  4747. SubGroup: wire.PermitDenySetGroupPermitMask,
  4748. },
  4749. Body: tt.inputBody,
  4750. }
  4751. svc := newMockPermitDenyService(t)
  4752. h := Handler{
  4753. PermitDenyService: svc,
  4754. RouteLogger: middleware.RouteLogger{
  4755. Logger: slog.Default(),
  4756. },
  4757. }
  4758. buf := &bytes.Buffer{}
  4759. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4760. err := h.Handle(context.TODO(), wire.BOS, instance, input.Frame, buf, nil, config.Listener{})
  4761. if tt.expectedError != nil {
  4762. assert.ErrorIs(t, err, tt.expectedError)
  4763. } else {
  4764. assert.NoError(t, err)
  4765. }
  4766. })
  4767. }
  4768. }
  4769. func TestUserLookupHandler_FindByEmail(t *testing.T) {
  4770. tests := []struct {
  4771. name string
  4772. inputBody wire.SNAC_0x0A_0x02_UserLookupFindByEmail
  4773. serviceError error
  4774. responseError error
  4775. expectedError error
  4776. }{
  4777. {
  4778. name: "success",
  4779. inputBody: wire.SNAC_0x0A_0x02_UserLookupFindByEmail{
  4780. Email: []byte("haha@aol.com"),
  4781. },
  4782. },
  4783. {
  4784. name: "service error",
  4785. inputBody: wire.SNAC_0x0A_0x02_UserLookupFindByEmail{
  4786. Email: []byte("haha@aol.com"),
  4787. },
  4788. serviceError: assert.AnError,
  4789. expectedError: assert.AnError,
  4790. },
  4791. {
  4792. name: "response writer error",
  4793. inputBody: wire.SNAC_0x0A_0x02_UserLookupFindByEmail{
  4794. Email: []byte("haha@aol.com"),
  4795. },
  4796. responseError: assert.AnError,
  4797. expectedError: assert.AnError,
  4798. },
  4799. }
  4800. for _, tt := range tests {
  4801. t.Run(tt.name, func(t *testing.T) {
  4802. input := wire.SNACMessage{
  4803. Frame: wire.SNACFrame{
  4804. FoodGroup: wire.UserLookup,
  4805. SubGroup: wire.UserLookupFindByEmail,
  4806. },
  4807. Body: tt.inputBody,
  4808. }
  4809. output := wire.SNACMessage{
  4810. Frame: wire.SNACFrame{
  4811. FoodGroup: wire.UserLookup,
  4812. SubGroup: wire.UserLookupFindReply,
  4813. },
  4814. Body: wire.SNAC_0x0A_0x03_UserLookupFindReply{
  4815. TLVRestBlock: wire.TLVRestBlock{
  4816. TLVList: wire.TLVList{
  4817. wire.NewTLVBE(0x01, uint16(0x02)),
  4818. },
  4819. },
  4820. },
  4821. }
  4822. svc := newMockUserLookupService(t)
  4823. svc.EXPECT().
  4824. FindByEmail(mock.Anything, input.Frame, input.Body).
  4825. Return(output, tt.serviceError)
  4826. h := Handler{
  4827. UserLookupService: svc,
  4828. RouteLogger: middleware.RouteLogger{
  4829. Logger: slog.Default(),
  4830. },
  4831. }
  4832. ss := newMockResponseWriter(t)
  4833. if tt.serviceError == nil {
  4834. ss.EXPECT().
  4835. SendSNAC(output.Frame, output.Body).
  4836. Return(tt.responseError)
  4837. }
  4838. buf := &bytes.Buffer{}
  4839. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4840. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, ss, config.Listener{})
  4841. if tt.expectedError != nil {
  4842. assert.ErrorIs(t, err, tt.expectedError)
  4843. } else {
  4844. assert.NoError(t, err)
  4845. }
  4846. })
  4847. }
  4848. }
  4849. func TestHandler_LocateGetDirInfo(t *testing.T) {
  4850. tests := []struct {
  4851. name string
  4852. inputBody wire.SNAC_0x02_0x0B_LocateGetDirInfo
  4853. serviceError error
  4854. responseError error
  4855. expectedError error
  4856. }{
  4857. {
  4858. name: "success",
  4859. inputBody: wire.SNAC_0x02_0x0B_LocateGetDirInfo{
  4860. ScreenName: "screen-name",
  4861. },
  4862. },
  4863. {
  4864. name: "service error",
  4865. inputBody: wire.SNAC_0x02_0x0B_LocateGetDirInfo{
  4866. ScreenName: "screen-name",
  4867. },
  4868. serviceError: assert.AnError,
  4869. expectedError: assert.AnError,
  4870. },
  4871. {
  4872. name: "response writer error",
  4873. inputBody: wire.SNAC_0x02_0x0B_LocateGetDirInfo{
  4874. ScreenName: "screen-name",
  4875. },
  4876. responseError: assert.AnError,
  4877. expectedError: assert.AnError,
  4878. },
  4879. }
  4880. for _, tt := range tests {
  4881. t.Run(tt.name, func(t *testing.T) {
  4882. input := wire.SNACMessage{
  4883. Frame: wire.SNACFrame{
  4884. FoodGroup: wire.Locate,
  4885. SubGroup: wire.LocateGetDirInfo,
  4886. },
  4887. Body: tt.inputBody,
  4888. }
  4889. output := wire.SNACMessage{
  4890. Frame: wire.SNACFrame{
  4891. FoodGroup: wire.Locate,
  4892. SubGroup: wire.LocateGetDirReply,
  4893. },
  4894. Body: wire.SNAC_0x02_0x0C_LocateGetDirReply{
  4895. Status: 1,
  4896. },
  4897. }
  4898. svc := newMockLocateService(t)
  4899. svc.EXPECT().
  4900. DirInfo(mock.Anything, input.Frame, input.Body).
  4901. Return(output, tt.serviceError)
  4902. h := Handler{
  4903. LocateService: svc,
  4904. RouteLogger: middleware.RouteLogger{
  4905. Logger: slog.Default(),
  4906. },
  4907. }
  4908. responseWriter := newMockResponseWriter(t)
  4909. if tt.serviceError == nil {
  4910. responseWriter.EXPECT().
  4911. SendSNAC(output.Frame, output.Body).
  4912. Return(tt.responseError)
  4913. }
  4914. buf := &bytes.Buffer{}
  4915. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4916. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4917. if tt.expectedError != nil {
  4918. assert.ErrorIs(t, err, tt.expectedError)
  4919. } else {
  4920. assert.NoError(t, err)
  4921. }
  4922. })
  4923. }
  4924. }
  4925. func TestHandler_LocateRightsQuery(t *testing.T) {
  4926. tests := []struct {
  4927. name string
  4928. inputBody struct{}
  4929. responseError error
  4930. expectedError error
  4931. }{
  4932. {
  4933. name: "success",
  4934. inputBody: struct{}{},
  4935. },
  4936. {
  4937. name: "response writer error",
  4938. inputBody: struct{}{},
  4939. responseError: assert.AnError,
  4940. expectedError: assert.AnError,
  4941. },
  4942. }
  4943. for _, tt := range tests {
  4944. t.Run(tt.name, func(t *testing.T) {
  4945. input := wire.SNACMessage{
  4946. Frame: wire.SNACFrame{
  4947. FoodGroup: wire.Locate,
  4948. SubGroup: wire.LocateRightsQuery,
  4949. },
  4950. Body: tt.inputBody,
  4951. }
  4952. output := wire.SNACMessage{
  4953. Frame: wire.SNACFrame{
  4954. FoodGroup: wire.Locate,
  4955. SubGroup: wire.LocateRightsReply,
  4956. },
  4957. Body: wire.SNAC_0x02_0x03_LocateRightsReply{
  4958. TLVRestBlock: wire.TLVRestBlock{
  4959. TLVList: wire.TLVList{
  4960. wire.NewTLVBE(0x01, uint16(1000)),
  4961. },
  4962. },
  4963. },
  4964. }
  4965. svc := newMockLocateService(t)
  4966. svc.EXPECT().
  4967. RightsQuery(mock.Anything, input.Frame).
  4968. Return(output)
  4969. h := Handler{
  4970. LocateService: svc,
  4971. RouteLogger: middleware.RouteLogger{
  4972. Logger: slog.Default(),
  4973. },
  4974. }
  4975. responseWriter := newMockResponseWriter(t)
  4976. responseWriter.EXPECT().
  4977. SendSNAC(output.Frame, output.Body).
  4978. Return(tt.responseError)
  4979. buf := &bytes.Buffer{}
  4980. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4981. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4982. if tt.expectedError != nil {
  4983. assert.ErrorIs(t, err, tt.expectedError)
  4984. } else {
  4985. assert.NoError(t, err)
  4986. }
  4987. })
  4988. }
  4989. }
  4990. func TestHandler_LocateSetDirInfo(t *testing.T) {
  4991. tests := []struct {
  4992. name string
  4993. inputBody wire.SNAC_0x02_0x09_LocateSetDirInfo
  4994. serviceError error
  4995. responseError error
  4996. expectedError error
  4997. }{
  4998. {
  4999. name: "success",
  5000. inputBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  5001. TLVRestBlock: wire.TLVRestBlock{
  5002. TLVList: wire.TLVList{
  5003. {
  5004. Tag: 0x01,
  5005. Value: []byte{1, 2, 3, 4},
  5006. },
  5007. },
  5008. },
  5009. },
  5010. },
  5011. {
  5012. name: "service error",
  5013. inputBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  5014. TLVRestBlock: wire.TLVRestBlock{
  5015. TLVList: wire.TLVList{
  5016. {
  5017. Tag: 0x01,
  5018. Value: []byte{1, 2, 3, 4},
  5019. },
  5020. },
  5021. },
  5022. },
  5023. serviceError: assert.AnError,
  5024. expectedError: assert.AnError,
  5025. },
  5026. {
  5027. name: "response writer error",
  5028. inputBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  5029. TLVRestBlock: wire.TLVRestBlock{
  5030. TLVList: wire.TLVList{
  5031. {
  5032. Tag: 0x01,
  5033. Value: []byte{1, 2, 3, 4},
  5034. },
  5035. },
  5036. },
  5037. },
  5038. responseError: assert.AnError,
  5039. expectedError: assert.AnError,
  5040. },
  5041. }
  5042. for _, tt := range tests {
  5043. t.Run(tt.name, func(t *testing.T) {
  5044. input := wire.SNACMessage{
  5045. Frame: wire.SNACFrame{
  5046. FoodGroup: wire.Locate,
  5047. SubGroup: wire.LocateSetDirInfo,
  5048. },
  5049. Body: tt.inputBody,
  5050. }
  5051. output := wire.SNACMessage{
  5052. Frame: wire.SNACFrame{
  5053. FoodGroup: wire.Locate,
  5054. SubGroup: wire.LocateSetDirReply,
  5055. },
  5056. Body: wire.SNAC_0x02_0x0A_LocateSetDirReply{
  5057. Result: 1,
  5058. },
  5059. }
  5060. svc := newMockLocateService(t)
  5061. svc.EXPECT().
  5062. SetDirInfo(mock.Anything, mock.Anything, input.Frame, input.Body).
  5063. Return(output, tt.serviceError)
  5064. h := Handler{
  5065. LocateService: svc,
  5066. RouteLogger: middleware.RouteLogger{
  5067. Logger: slog.Default(),
  5068. },
  5069. }
  5070. responseWriter := newMockResponseWriter(t)
  5071. if tt.serviceError == nil {
  5072. responseWriter.EXPECT().
  5073. SendSNAC(output.Frame, output.Body).
  5074. Return(tt.responseError)
  5075. }
  5076. buf := &bytes.Buffer{}
  5077. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  5078. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  5079. if tt.expectedError != nil {
  5080. assert.ErrorIs(t, err, tt.expectedError)
  5081. } else {
  5082. assert.NoError(t, err)
  5083. }
  5084. })
  5085. }
  5086. }
  5087. func TestHandler_LocateSetInfo(t *testing.T) {
  5088. input := wire.SNACMessage{
  5089. Frame: wire.SNACFrame{
  5090. FoodGroup: wire.Locate,
  5091. SubGroup: wire.LocateSetInfo,
  5092. },
  5093. Body: wire.SNAC_0x02_0x04_LocateSetInfo{
  5094. TLVRestBlock: wire.TLVRestBlock{
  5095. TLVList: wire.TLVList{
  5096. {
  5097. Tag: 0x01,
  5098. Value: []byte{1, 2, 3, 4},
  5099. },
  5100. },
  5101. },
  5102. },
  5103. }
  5104. svc := newMockLocateService(t)
  5105. svc.EXPECT().
  5106. SetInfo(mock.Anything, mock.Anything, input.Body).
  5107. Return(nil)
  5108. h := Handler{
  5109. LocateService: svc,
  5110. RouteLogger: middleware.RouteLogger{
  5111. Logger: slog.Default(),
  5112. },
  5113. }
  5114. responseWriter := newMockResponseWriter(t)
  5115. buf := &bytes.Buffer{}
  5116. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  5117. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  5118. }
  5119. func TestHandler_LocateSetKeywordInfo(t *testing.T) {
  5120. input := wire.SNACMessage{
  5121. Frame: wire.SNACFrame{
  5122. FoodGroup: wire.Locate,
  5123. SubGroup: wire.LocateSetKeywordInfo,
  5124. },
  5125. Body: wire.SNAC_0x02_0x0F_LocateSetKeywordInfo{
  5126. TLVRestBlock: wire.TLVRestBlock{
  5127. TLVList: wire.TLVList{
  5128. {
  5129. Tag: 0x01,
  5130. Value: []byte{1, 2, 3, 4},
  5131. },
  5132. },
  5133. },
  5134. },
  5135. }
  5136. output := wire.SNACMessage{
  5137. Frame: wire.SNACFrame{
  5138. FoodGroup: wire.Locate,
  5139. SubGroup: wire.LocateSetKeywordReply,
  5140. },
  5141. Body: wire.SNAC_0x02_0x10_LocateSetKeywordReply{
  5142. Unknown: 1,
  5143. },
  5144. }
  5145. svc := newMockLocateService(t)
  5146. svc.EXPECT().
  5147. SetKeywordInfo(mock.Anything, mock.Anything, input.Frame, input.Body).
  5148. Return(output, nil)
  5149. h := Handler{
  5150. LocateService: svc,
  5151. RouteLogger: middleware.RouteLogger{
  5152. Logger: slog.Default(),
  5153. },
  5154. }
  5155. responseWriter := newMockResponseWriter(t)
  5156. responseWriter.EXPECT().
  5157. SendSNAC(output.Frame, output.Body).
  5158. Return(nil)
  5159. buf := &bytes.Buffer{}
  5160. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  5161. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  5162. }
  5163. func TestHandler_LocateUserInfoQuery(t *testing.T) {
  5164. input := wire.SNACMessage{
  5165. Frame: wire.SNACFrame{
  5166. FoodGroup: wire.Locate,
  5167. SubGroup: wire.LocateUserInfoQuery,
  5168. },
  5169. Body: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  5170. Type: 1,
  5171. },
  5172. }
  5173. output := wire.SNACMessage{
  5174. Frame: wire.SNACFrame{
  5175. FoodGroup: wire.Locate,
  5176. SubGroup: wire.LocateUserInfoReply,
  5177. },
  5178. Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  5179. TLVUserInfo: wire.TLVUserInfo{
  5180. ScreenName: "screen-name",
  5181. },
  5182. LocateInfo: wire.TLVRestBlock{
  5183. TLVList: wire.TLVList{
  5184. {
  5185. Tag: 0x01,
  5186. Value: []byte{1, 2, 3, 4},
  5187. },
  5188. },
  5189. },
  5190. },
  5191. }
  5192. svc := newMockLocateService(t)
  5193. svc.EXPECT().
  5194. UserInfoQuery(mock.Anything, mock.Anything, input.Frame, input.Body).
  5195. Return(output, nil)
  5196. h := Handler{
  5197. LocateService: svc,
  5198. RouteLogger: middleware.RouteLogger{
  5199. Logger: slog.Default(),
  5200. },
  5201. }
  5202. responseWriter := newMockResponseWriter(t)
  5203. responseWriter.EXPECT().
  5204. SendSNAC(output.Frame, output.Body).
  5205. Return(nil)
  5206. buf := &bytes.Buffer{}
  5207. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  5208. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  5209. }
  5210. func TestHandler_LocateUserInfoQuery2(t *testing.T) {
  5211. input := wire.SNACMessage{
  5212. Frame: wire.SNACFrame{
  5213. FoodGroup: wire.Locate,
  5214. SubGroup: wire.LocateUserInfoQuery2,
  5215. },
  5216. Body: wire.SNAC_0x02_0x15_LocateUserInfoQuery2{
  5217. Type2: 1,
  5218. },
  5219. }
  5220. output := wire.SNACMessage{
  5221. Frame: wire.SNACFrame{
  5222. FoodGroup: wire.Locate,
  5223. SubGroup: wire.LocateUserInfoReply,
  5224. },
  5225. Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  5226. TLVUserInfo: wire.TLVUserInfo{
  5227. ScreenName: "screen-name",
  5228. },
  5229. LocateInfo: wire.TLVRestBlock{
  5230. TLVList: wire.TLVList{
  5231. {
  5232. Tag: 0x01,
  5233. Value: []byte{1, 2, 3, 4},
  5234. },
  5235. },
  5236. },
  5237. },
  5238. }
  5239. svc := newMockLocateService(t)
  5240. svc.EXPECT().
  5241. UserInfoQuery(mock.Anything, mock.Anything, input.Frame, wire.SNAC_0x02_0x05_LocateUserInfoQuery{Type: 1}).
  5242. Return(output, nil)
  5243. h := Handler{
  5244. LocateService: svc,
  5245. RouteLogger: middleware.RouteLogger{
  5246. Logger: slog.Default(),
  5247. },
  5248. }
  5249. responseWriter := newMockResponseWriter(t)
  5250. responseWriter.EXPECT().
  5251. SendSNAC(output.Frame, output.Body).
  5252. Return(nil)
  5253. buf := &bytes.Buffer{}
  5254. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  5255. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  5256. }
  5257. func TestHandler_StatsReportEvents(t *testing.T) {
  5258. input := wire.SNACMessage{
  5259. Frame: wire.SNACFrame{
  5260. FoodGroup: wire.Stats,
  5261. SubGroup: wire.StatsReportEvents,
  5262. },
  5263. Body: wire.SNAC_0x0B_0x03_StatsReportEvents{},
  5264. }
  5265. output := wire.SNACMessage{
  5266. Frame: wire.SNACFrame{
  5267. FoodGroup: wire.Stats,
  5268. SubGroup: wire.StatsReportAck,
  5269. },
  5270. Body: wire.SNAC_0x0B_0x04_StatsReportAck{},
  5271. }
  5272. svc := newMockStatsService(t)
  5273. svc.EXPECT().
  5274. ReportEvents(mock.Anything, input.Frame, input.Body).
  5275. Return(output)
  5276. h := Handler{
  5277. StatsService: svc,
  5278. RouteLogger: middleware.RouteLogger{
  5279. Logger: slog.Default(),
  5280. },
  5281. }
  5282. ss := newMockResponseWriter(t)
  5283. ss.EXPECT().
  5284. SendSNAC(output.Frame, output.Body).
  5285. Return(nil)
  5286. buf := &bytes.Buffer{}
  5287. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  5288. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, ss, config.Listener{}))
  5289. }
  5290. func TestHandler_RouteNotFound(t *testing.T) {
  5291. input := wire.SNACMessage{
  5292. Frame: wire.SNACFrame{
  5293. FoodGroup: wire.Stats,
  5294. SubGroup: math.MaxUint16,
  5295. },
  5296. Body: wire.SNAC_0x0B_0x03_StatsReportEvents{},
  5297. }
  5298. h := Handler{
  5299. RouteLogger: middleware.RouteLogger{
  5300. Logger: slog.Default(),
  5301. },
  5302. }
  5303. assert.ErrorIs(t, ErrRouteNotFound, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, nil, nil, config.Listener{}))
  5304. }