handler_test.go 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778
  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. svc.EXPECT().
  1477. EndCluster(mock.Anything, mock.Anything, input.Frame)
  1478. h := Handler{
  1479. FeedbagService: svc,
  1480. RouteLogger: middleware.RouteLogger{
  1481. Logger: slog.Default(),
  1482. },
  1483. }
  1484. responseWriter := newMockResponseWriter(t)
  1485. buf := &bytes.Buffer{}
  1486. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1487. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1488. if tt.expectedError != nil {
  1489. assert.ErrorIs(t, err, tt.expectedError)
  1490. } else {
  1491. assert.NoError(t, err)
  1492. }
  1493. })
  1494. }
  1495. }
  1496. func TestHandler_FeedbagInsertItem(t *testing.T) {
  1497. tests := []struct {
  1498. name string
  1499. inputBody wire.SNAC_0x13_0x08_FeedbagInsertItem
  1500. expectedOutput *wire.SNACMessage
  1501. serviceError error
  1502. responseError error
  1503. expectedError error
  1504. }{
  1505. {
  1506. name: "success",
  1507. inputBody: wire.SNAC_0x13_0x08_FeedbagInsertItem{
  1508. Items: []wire.FeedbagItem{
  1509. {
  1510. Name: "my-item",
  1511. },
  1512. },
  1513. },
  1514. expectedOutput: nil,
  1515. },
  1516. {
  1517. name: "success (non-nil output)",
  1518. inputBody: wire.SNAC_0x13_0x08_FeedbagInsertItem{
  1519. Items: []wire.FeedbagItem{
  1520. {
  1521. Name: "my-item",
  1522. },
  1523. },
  1524. },
  1525. expectedOutput: &wire.SNACMessage{
  1526. Frame: wire.SNACFrame{
  1527. FoodGroup: wire.Feedbag,
  1528. SubGroup: wire.FeedbagStatus,
  1529. },
  1530. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1531. Results: []uint16{1234},
  1532. },
  1533. },
  1534. },
  1535. {
  1536. name: "service error",
  1537. inputBody: wire.SNAC_0x13_0x08_FeedbagInsertItem{
  1538. Items: []wire.FeedbagItem{
  1539. {
  1540. Name: "my-item",
  1541. },
  1542. },
  1543. },
  1544. expectedOutput: &wire.SNACMessage{
  1545. Frame: wire.SNACFrame{
  1546. FoodGroup: wire.Feedbag,
  1547. SubGroup: wire.FeedbagStatus,
  1548. },
  1549. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1550. Results: []uint16{1234},
  1551. },
  1552. },
  1553. serviceError: assert.AnError,
  1554. expectedError: assert.AnError,
  1555. },
  1556. {
  1557. name: "response writer error",
  1558. inputBody: wire.SNAC_0x13_0x08_FeedbagInsertItem{
  1559. Items: []wire.FeedbagItem{
  1560. {
  1561. Name: "my-item",
  1562. },
  1563. },
  1564. },
  1565. expectedOutput: &wire.SNACMessage{
  1566. Frame: wire.SNACFrame{
  1567. FoodGroup: wire.Feedbag,
  1568. SubGroup: wire.FeedbagStatus,
  1569. },
  1570. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1571. Results: []uint16{1234},
  1572. },
  1573. },
  1574. responseError: assert.AnError,
  1575. expectedError: assert.AnError,
  1576. },
  1577. }
  1578. for _, tt := range tests {
  1579. t.Run(tt.name, func(t *testing.T) {
  1580. input := wire.SNACMessage{
  1581. Frame: wire.SNACFrame{
  1582. FoodGroup: wire.Feedbag,
  1583. SubGroup: wire.FeedbagInsertItem,
  1584. },
  1585. Body: tt.inputBody,
  1586. }
  1587. svc := newMockFeedbagService(t)
  1588. svc.EXPECT().
  1589. UpsertItem(mock.Anything, mock.Anything, input.Frame, tt.inputBody.Items).
  1590. Return(tt.expectedOutput, tt.serviceError)
  1591. h := Handler{
  1592. FeedbagService: svc,
  1593. RouteLogger: middleware.RouteLogger{
  1594. Logger: slog.Default(),
  1595. },
  1596. }
  1597. responseWriter := newMockResponseWriter(t)
  1598. if tt.serviceError == nil && tt.expectedOutput != nil {
  1599. responseWriter.EXPECT().
  1600. SendSNAC(tt.expectedOutput.Frame, tt.expectedOutput.Body).
  1601. Return(tt.responseError)
  1602. }
  1603. buf := &bytes.Buffer{}
  1604. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1605. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1606. if tt.expectedError != nil {
  1607. assert.ErrorIs(t, err, tt.expectedError)
  1608. } else {
  1609. assert.NoError(t, err)
  1610. }
  1611. })
  1612. }
  1613. }
  1614. func TestHandler_FeedbagQuery(t *testing.T) {
  1615. tests := []struct {
  1616. name string
  1617. inputBody wire.SNAC_0x13_0x02_FeedbagRightsQuery
  1618. serviceError error
  1619. responseError error
  1620. expectedError error
  1621. }{
  1622. {
  1623. name: "success",
  1624. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1625. TLVRestBlock: wire.TLVRestBlock{
  1626. TLVList: wire.TLVList{
  1627. {
  1628. Tag: 0x01,
  1629. Value: []byte{1, 2, 3, 4},
  1630. },
  1631. },
  1632. },
  1633. },
  1634. },
  1635. {
  1636. name: "service error",
  1637. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1638. TLVRestBlock: wire.TLVRestBlock{
  1639. TLVList: wire.TLVList{
  1640. {
  1641. Tag: 0x01,
  1642. Value: []byte{1, 2, 3, 4},
  1643. },
  1644. },
  1645. },
  1646. },
  1647. serviceError: assert.AnError,
  1648. expectedError: assert.AnError,
  1649. },
  1650. {
  1651. name: "response writer error",
  1652. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1653. TLVRestBlock: wire.TLVRestBlock{
  1654. TLVList: wire.TLVList{
  1655. {
  1656. Tag: 0x01,
  1657. Value: []byte{1, 2, 3, 4},
  1658. },
  1659. },
  1660. },
  1661. },
  1662. responseError: assert.AnError,
  1663. expectedError: assert.AnError,
  1664. },
  1665. }
  1666. for _, tt := range tests {
  1667. t.Run(tt.name, func(t *testing.T) {
  1668. input := wire.SNACMessage{
  1669. Frame: wire.SNACFrame{
  1670. FoodGroup: wire.Feedbag,
  1671. SubGroup: wire.FeedbagQuery,
  1672. },
  1673. Body: tt.inputBody,
  1674. }
  1675. output := wire.SNACMessage{
  1676. Frame: wire.SNACFrame{
  1677. FoodGroup: wire.Feedbag,
  1678. SubGroup: wire.FeedbagReply,
  1679. },
  1680. Body: wire.SNAC_0x13_0x06_FeedbagReply{
  1681. Version: 4,
  1682. },
  1683. }
  1684. svc := newMockFeedbagService(t)
  1685. svc.EXPECT().
  1686. Query(mock.Anything, mock.Anything, input.Frame).
  1687. Return(output, tt.serviceError)
  1688. h := Handler{
  1689. FeedbagService: svc,
  1690. RouteLogger: middleware.RouteLogger{
  1691. Logger: slog.Default(),
  1692. },
  1693. }
  1694. responseWriter := newMockResponseWriter(t)
  1695. if tt.serviceError == nil {
  1696. responseWriter.EXPECT().
  1697. SendSNAC(output.Frame, output.Body).
  1698. Return(tt.responseError)
  1699. }
  1700. buf := &bytes.Buffer{}
  1701. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1702. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1703. if tt.expectedError != nil {
  1704. assert.ErrorIs(t, err, tt.expectedError)
  1705. } else {
  1706. assert.NoError(t, err)
  1707. }
  1708. })
  1709. }
  1710. }
  1711. func TestHandler_FeedbagQueryIfModified(t *testing.T) {
  1712. tests := []struct {
  1713. name string
  1714. inputBody wire.SNAC_0x13_0x05_FeedbagQueryIfModified
  1715. serviceError error
  1716. responseError error
  1717. expectedError error
  1718. }{
  1719. {
  1720. name: "success",
  1721. inputBody: wire.SNAC_0x13_0x05_FeedbagQueryIfModified{
  1722. LastUpdate: 1234,
  1723. },
  1724. },
  1725. {
  1726. name: "service error",
  1727. inputBody: wire.SNAC_0x13_0x05_FeedbagQueryIfModified{
  1728. LastUpdate: 1234,
  1729. },
  1730. serviceError: assert.AnError,
  1731. expectedError: assert.AnError,
  1732. },
  1733. {
  1734. name: "response writer error",
  1735. inputBody: wire.SNAC_0x13_0x05_FeedbagQueryIfModified{
  1736. LastUpdate: 1234,
  1737. },
  1738. responseError: assert.AnError,
  1739. expectedError: assert.AnError,
  1740. },
  1741. }
  1742. for _, tt := range tests {
  1743. t.Run(tt.name, func(t *testing.T) {
  1744. input := wire.SNACMessage{
  1745. Frame: wire.SNACFrame{
  1746. FoodGroup: wire.Feedbag,
  1747. SubGroup: wire.FeedbagQueryIfModified,
  1748. },
  1749. Body: tt.inputBody,
  1750. }
  1751. output := wire.SNACMessage{
  1752. Frame: wire.SNACFrame{
  1753. FoodGroup: wire.Feedbag,
  1754. SubGroup: wire.FeedbagReply,
  1755. },
  1756. Body: wire.SNAC_0x13_0x06_FeedbagReply{
  1757. LastUpdate: 1234,
  1758. },
  1759. }
  1760. svc := newMockFeedbagService(t)
  1761. svc.EXPECT().
  1762. QueryIfModified(mock.Anything, mock.Anything, input.Frame, input.Body).
  1763. Return(output, tt.serviceError)
  1764. h := Handler{
  1765. FeedbagService: svc,
  1766. RouteLogger: middleware.RouteLogger{
  1767. Logger: slog.Default(),
  1768. },
  1769. }
  1770. responseWriter := newMockResponseWriter(t)
  1771. if tt.serviceError == nil {
  1772. responseWriter.EXPECT().
  1773. SendSNAC(output.Frame, output.Body).
  1774. Return(tt.responseError)
  1775. }
  1776. buf := &bytes.Buffer{}
  1777. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1778. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1779. if tt.expectedError != nil {
  1780. assert.ErrorIs(t, err, tt.expectedError)
  1781. } else {
  1782. assert.NoError(t, err)
  1783. }
  1784. })
  1785. }
  1786. }
  1787. func TestHandler_FeedbagRightsQuery(t *testing.T) {
  1788. tests := []struct {
  1789. name string
  1790. inputBody wire.SNAC_0x13_0x02_FeedbagRightsQuery
  1791. responseError error
  1792. expectedError error
  1793. }{
  1794. {
  1795. name: "success",
  1796. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1797. TLVRestBlock: wire.TLVRestBlock{
  1798. TLVList: wire.TLVList{
  1799. {
  1800. Tag: 0x01,
  1801. Value: []byte{1, 2, 3, 4},
  1802. },
  1803. },
  1804. },
  1805. },
  1806. },
  1807. {
  1808. name: "response writer error",
  1809. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1810. TLVRestBlock: wire.TLVRestBlock{
  1811. TLVList: wire.TLVList{
  1812. {
  1813. Tag: 0x01,
  1814. Value: []byte{1, 2, 3, 4},
  1815. },
  1816. },
  1817. },
  1818. },
  1819. responseError: assert.AnError,
  1820. expectedError: assert.AnError,
  1821. },
  1822. }
  1823. for _, tt := range tests {
  1824. t.Run(tt.name, func(t *testing.T) {
  1825. input := wire.SNACMessage{
  1826. Frame: wire.SNACFrame{
  1827. FoodGroup: wire.Feedbag,
  1828. SubGroup: wire.FeedbagRightsQuery,
  1829. },
  1830. Body: tt.inputBody,
  1831. }
  1832. output := wire.SNACMessage{
  1833. Frame: wire.SNACFrame{
  1834. FoodGroup: wire.Feedbag,
  1835. SubGroup: wire.FeedbagRightsReply,
  1836. },
  1837. Body: wire.SNAC_0x13_0x03_FeedbagRightsReply{
  1838. TLVRestBlock: wire.TLVRestBlock{
  1839. TLVList: wire.TLVList{
  1840. {
  1841. Tag: 0x01,
  1842. Value: []byte{1, 2, 3, 4},
  1843. },
  1844. },
  1845. },
  1846. },
  1847. }
  1848. svc := newMockFeedbagService(t)
  1849. svc.EXPECT().
  1850. RightsQuery(mock.Anything, input.Frame).
  1851. Return(output)
  1852. h := Handler{
  1853. FeedbagService: svc,
  1854. RouteLogger: middleware.RouteLogger{
  1855. Logger: slog.Default(),
  1856. },
  1857. }
  1858. responseWriter := newMockResponseWriter(t)
  1859. responseWriter.EXPECT().
  1860. SendSNAC(output.Frame, output.Body).
  1861. Return(tt.responseError)
  1862. buf := &bytes.Buffer{}
  1863. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1864. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1865. if tt.expectedError != nil {
  1866. assert.ErrorIs(t, err, tt.expectedError)
  1867. } else {
  1868. assert.NoError(t, err)
  1869. }
  1870. })
  1871. }
  1872. }
  1873. func TestHandler_FeedbagStartCluster(t *testing.T) {
  1874. tests := []struct {
  1875. name string
  1876. inputBody wire.SNAC_0x13_0x11_FeedbagStartCluster
  1877. expectedError error
  1878. }{
  1879. {
  1880. name: "success",
  1881. inputBody: wire.SNAC_0x13_0x11_FeedbagStartCluster{
  1882. TLVRestBlock: wire.TLVRestBlock{
  1883. TLVList: wire.TLVList{
  1884. {
  1885. Tag: 0x01,
  1886. Value: []byte{1, 2, 3, 4},
  1887. },
  1888. },
  1889. },
  1890. },
  1891. },
  1892. }
  1893. for _, tt := range tests {
  1894. t.Run(tt.name, func(t *testing.T) {
  1895. input := wire.SNACMessage{
  1896. Frame: wire.SNACFrame{
  1897. FoodGroup: wire.Feedbag,
  1898. SubGroup: wire.FeedbagStartCluster,
  1899. },
  1900. Body: tt.inputBody,
  1901. }
  1902. svc := newMockFeedbagService(t)
  1903. svc.EXPECT().
  1904. StartCluster(mock.Anything, mock.Anything, input.Frame, input.Body)
  1905. h := Handler{
  1906. FeedbagService: svc,
  1907. RouteLogger: middleware.RouteLogger{
  1908. Logger: slog.Default(),
  1909. },
  1910. }
  1911. responseWriter := newMockResponseWriter(t)
  1912. buf := &bytes.Buffer{}
  1913. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1914. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1915. if tt.expectedError != nil {
  1916. assert.ErrorIs(t, err, tt.expectedError)
  1917. } else {
  1918. assert.NoError(t, err)
  1919. }
  1920. })
  1921. }
  1922. }
  1923. func TestHandler_FeedbagUpdateItem(t *testing.T) {
  1924. tests := []struct {
  1925. name string
  1926. inputBody wire.SNAC_0x13_0x09_FeedbagUpdateItem
  1927. expectedOutput *wire.SNACMessage
  1928. serviceError error
  1929. responseError error
  1930. expectedError error
  1931. }{
  1932. {
  1933. name: "success",
  1934. inputBody: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
  1935. Items: []wire.FeedbagItem{
  1936. {
  1937. Name: "my-item",
  1938. },
  1939. },
  1940. },
  1941. expectedOutput: nil,
  1942. },
  1943. {
  1944. name: "success (non-nil output)",
  1945. inputBody: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
  1946. Items: []wire.FeedbagItem{
  1947. {
  1948. Name: "my-item",
  1949. },
  1950. },
  1951. },
  1952. expectedOutput: &wire.SNACMessage{
  1953. Frame: wire.SNACFrame{
  1954. FoodGroup: wire.Feedbag,
  1955. SubGroup: wire.FeedbagStatus,
  1956. },
  1957. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1958. Results: []uint16{1234},
  1959. },
  1960. },
  1961. },
  1962. {
  1963. name: "service error",
  1964. inputBody: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
  1965. Items: []wire.FeedbagItem{
  1966. {
  1967. Name: "my-item",
  1968. },
  1969. },
  1970. },
  1971. expectedOutput: &wire.SNACMessage{
  1972. Frame: wire.SNACFrame{
  1973. FoodGroup: wire.Feedbag,
  1974. SubGroup: wire.FeedbagStatus,
  1975. },
  1976. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1977. Results: []uint16{1234},
  1978. },
  1979. },
  1980. serviceError: assert.AnError,
  1981. expectedError: assert.AnError,
  1982. },
  1983. {
  1984. name: "response writer error",
  1985. inputBody: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
  1986. Items: []wire.FeedbagItem{
  1987. {
  1988. Name: "my-item",
  1989. },
  1990. },
  1991. },
  1992. expectedOutput: &wire.SNACMessage{
  1993. Frame: wire.SNACFrame{
  1994. FoodGroup: wire.Feedbag,
  1995. SubGroup: wire.FeedbagStatus,
  1996. },
  1997. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1998. Results: []uint16{1234},
  1999. },
  2000. },
  2001. responseError: assert.AnError,
  2002. expectedError: assert.AnError,
  2003. },
  2004. }
  2005. for _, tt := range tests {
  2006. t.Run(tt.name, func(t *testing.T) {
  2007. input := wire.SNACMessage{
  2008. Frame: wire.SNACFrame{
  2009. FoodGroup: wire.Feedbag,
  2010. SubGroup: wire.FeedbagUpdateItem,
  2011. },
  2012. Body: tt.inputBody,
  2013. }
  2014. svc := newMockFeedbagService(t)
  2015. svc.EXPECT().
  2016. UpsertItem(mock.Anything, mock.Anything, input.Frame, tt.inputBody.Items).
  2017. Return(tt.expectedOutput, tt.serviceError)
  2018. h := Handler{
  2019. FeedbagService: svc,
  2020. RouteLogger: middleware.RouteLogger{
  2021. Logger: slog.Default(),
  2022. },
  2023. }
  2024. responseWriter := newMockResponseWriter(t)
  2025. if tt.serviceError == nil && tt.expectedOutput != nil {
  2026. responseWriter.EXPECT().
  2027. SendSNAC(tt.expectedOutput.Frame, tt.expectedOutput.Body).
  2028. Return(tt.responseError)
  2029. }
  2030. buf := &bytes.Buffer{}
  2031. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2032. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2033. if tt.expectedError != nil {
  2034. assert.ErrorIs(t, err, tt.expectedError)
  2035. } else {
  2036. assert.NoError(t, err)
  2037. }
  2038. })
  2039. }
  2040. }
  2041. func TestHandler_FeedbagUse(t *testing.T) {
  2042. tests := []struct {
  2043. name string
  2044. inputBody struct{}
  2045. serviceError error
  2046. expectedError error
  2047. }{
  2048. {
  2049. name: "success",
  2050. inputBody: struct{}{},
  2051. },
  2052. {
  2053. name: "service error",
  2054. inputBody: struct{}{},
  2055. serviceError: assert.AnError,
  2056. expectedError: assert.AnError,
  2057. },
  2058. }
  2059. for _, tt := range tests {
  2060. t.Run(tt.name, func(t *testing.T) {
  2061. input := wire.SNACMessage{
  2062. Frame: wire.SNACFrame{
  2063. FoodGroup: wire.Feedbag,
  2064. SubGroup: wire.FeedbagUse,
  2065. },
  2066. Body: tt.inputBody,
  2067. }
  2068. svc := newMockFeedbagService(t)
  2069. svc.EXPECT().
  2070. Use(mock.Anything, mock.Anything).
  2071. Return(tt.serviceError)
  2072. h := Handler{
  2073. FeedbagService: svc,
  2074. RouteLogger: middleware.RouteLogger{
  2075. Logger: slog.Default(),
  2076. },
  2077. }
  2078. responseWriter := newMockResponseWriter(t)
  2079. buf := &bytes.Buffer{}
  2080. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2081. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2082. if tt.expectedError != nil {
  2083. assert.ErrorIs(t, err, tt.expectedError)
  2084. } else {
  2085. assert.NoError(t, err)
  2086. }
  2087. })
  2088. }
  2089. }
  2090. func TestHandler_FeedbagRespondAuthorizeToHost(t *testing.T) {
  2091. input := wire.SNACMessage{
  2092. Frame: wire.SNACFrame{
  2093. FoodGroup: wire.Feedbag,
  2094. SubGroup: wire.FeedbagRespondAuthorizeToHost,
  2095. },
  2096. Body: wire.SNAC_0x13_0x1A_FeedbagRespondAuthorizeToHost{
  2097. ScreenName: "theScreenName",
  2098. },
  2099. }
  2100. svc := newMockFeedbagService(t)
  2101. svc.EXPECT().
  2102. RespondAuthorizeToHost(mock.Anything, mock.Anything, input.Frame, input.Body).
  2103. Return(nil)
  2104. h := Handler{
  2105. FeedbagService: svc,
  2106. RouteLogger: middleware.RouteLogger{
  2107. Logger: slog.Default(),
  2108. },
  2109. }
  2110. responseWriter := newMockResponseWriter(t)
  2111. buf := &bytes.Buffer{}
  2112. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2113. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  2114. }
  2115. func TestHandler_ICBMAddParameters(t *testing.T) {
  2116. tests := []struct {
  2117. name string
  2118. inputBody wire.SNAC_0x04_0x02_ICBMAddParameters
  2119. expectedError error
  2120. }{
  2121. {
  2122. name: "success",
  2123. inputBody: wire.SNAC_0x04_0x02_ICBMAddParameters{
  2124. Channel: 1,
  2125. },
  2126. },
  2127. }
  2128. for _, tt := range tests {
  2129. t.Run(tt.name, func(t *testing.T) {
  2130. input := wire.SNACMessage{
  2131. Frame: wire.SNACFrame{
  2132. FoodGroup: wire.ICBM,
  2133. SubGroup: wire.ICBMAddParameters,
  2134. },
  2135. Body: tt.inputBody,
  2136. }
  2137. svc := newMockICBMService(t)
  2138. h := Handler{
  2139. ICBMService: svc,
  2140. RouteLogger: middleware.RouteLogger{
  2141. Logger: slog.Default(),
  2142. },
  2143. }
  2144. responseWriter := newMockResponseWriter(t)
  2145. buf := &bytes.Buffer{}
  2146. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2147. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2148. if tt.expectedError != nil {
  2149. assert.ErrorIs(t, err, tt.expectedError)
  2150. } else {
  2151. assert.NoError(t, err)
  2152. }
  2153. })
  2154. }
  2155. }
  2156. func TestHandler_ICBMChannelMsgToHost(t *testing.T) {
  2157. tests := []struct {
  2158. name string
  2159. inputBody wire.SNAC_0x04_0x06_ICBMChannelMsgToHost
  2160. serviceError error
  2161. responseError error
  2162. expectedError error
  2163. }{
  2164. {
  2165. name: "success",
  2166. inputBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2167. ScreenName: "recipient-screen-name",
  2168. },
  2169. },
  2170. {
  2171. name: "service error",
  2172. inputBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2173. ScreenName: "recipient-screen-name",
  2174. },
  2175. serviceError: assert.AnError,
  2176. expectedError: assert.AnError,
  2177. },
  2178. {
  2179. name: "response writer error",
  2180. inputBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2181. ScreenName: "recipient-screen-name",
  2182. },
  2183. responseError: assert.AnError,
  2184. expectedError: assert.AnError,
  2185. },
  2186. }
  2187. for _, tt := range tests {
  2188. t.Run(tt.name, func(t *testing.T) {
  2189. input := wire.SNACMessage{
  2190. Frame: wire.SNACFrame{
  2191. FoodGroup: wire.ICBM,
  2192. SubGroup: wire.ICBMChannelMsgToHost,
  2193. },
  2194. Body: tt.inputBody,
  2195. }
  2196. output := wire.SNACMessage{
  2197. Frame: wire.SNACFrame{
  2198. FoodGroup: wire.ICBM,
  2199. SubGroup: wire.ICBMHostAck,
  2200. },
  2201. Body: wire.SNAC_0x04_0x0C_ICBMHostAck{
  2202. ChannelID: 4,
  2203. },
  2204. }
  2205. svc := newMockICBMService(t)
  2206. svc.EXPECT().
  2207. ChannelMsgToHost(mock.Anything, mock.Anything, input.Frame, input.Body).
  2208. Return(&output, tt.serviceError)
  2209. h := Handler{
  2210. ICBMService: svc,
  2211. RouteLogger: middleware.RouteLogger{
  2212. Logger: slog.Default(),
  2213. },
  2214. }
  2215. responseWriter := newMockResponseWriter(t)
  2216. if tt.serviceError == nil {
  2217. responseWriter.EXPECT().
  2218. SendSNAC(output.Frame, output.Body).
  2219. Return(tt.responseError)
  2220. }
  2221. buf := &bytes.Buffer{}
  2222. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2223. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2224. if tt.expectedError != nil {
  2225. assert.ErrorIs(t, err, tt.expectedError)
  2226. } else {
  2227. assert.NoError(t, err)
  2228. }
  2229. })
  2230. }
  2231. }
  2232. func TestHandler_ICBMClientErr(t *testing.T) {
  2233. tests := []struct {
  2234. name string
  2235. inputBody wire.SNAC_0x04_0x0B_ICBMClientErr
  2236. serviceError error
  2237. expectedError error
  2238. }{
  2239. {
  2240. name: "success",
  2241. inputBody: wire.SNAC_0x04_0x0B_ICBMClientErr{
  2242. Code: 4,
  2243. },
  2244. },
  2245. {
  2246. name: "service error",
  2247. inputBody: wire.SNAC_0x04_0x0B_ICBMClientErr{
  2248. Code: 4,
  2249. },
  2250. serviceError: assert.AnError,
  2251. expectedError: assert.AnError,
  2252. },
  2253. }
  2254. for _, tt := range tests {
  2255. t.Run(tt.name, func(t *testing.T) {
  2256. input := wire.SNACMessage{
  2257. Frame: wire.SNACFrame{
  2258. FoodGroup: wire.ICBM,
  2259. SubGroup: wire.ICBMClientErr,
  2260. },
  2261. Body: tt.inputBody,
  2262. }
  2263. svc := newMockICBMService(t)
  2264. svc.EXPECT().
  2265. ClientErr(mock.Anything, mock.Anything, input.Frame, input.Body).
  2266. Return(tt.serviceError)
  2267. h := Handler{
  2268. ICBMService: svc,
  2269. RouteLogger: middleware.RouteLogger{
  2270. Logger: slog.Default(),
  2271. },
  2272. }
  2273. responseWriter := newMockResponseWriter(t)
  2274. buf := &bytes.Buffer{}
  2275. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2276. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2277. if tt.expectedError != nil {
  2278. assert.ErrorIs(t, err, tt.expectedError)
  2279. } else {
  2280. assert.NoError(t, err)
  2281. }
  2282. })
  2283. }
  2284. }
  2285. func TestHandler_ICBMClientEvent(t *testing.T) {
  2286. tests := []struct {
  2287. name string
  2288. inputBody wire.SNAC_0x04_0x14_ICBMClientEvent
  2289. serviceError error
  2290. expectedError error
  2291. }{
  2292. {
  2293. name: "success",
  2294. inputBody: wire.SNAC_0x04_0x14_ICBMClientEvent{
  2295. ScreenName: "recipient-screen-name",
  2296. },
  2297. },
  2298. {
  2299. name: "service error",
  2300. inputBody: wire.SNAC_0x04_0x14_ICBMClientEvent{
  2301. ScreenName: "recipient-screen-name",
  2302. },
  2303. serviceError: assert.AnError,
  2304. expectedError: assert.AnError,
  2305. },
  2306. }
  2307. for _, tt := range tests {
  2308. t.Run(tt.name, func(t *testing.T) {
  2309. input := wire.SNACMessage{
  2310. Frame: wire.SNACFrame{
  2311. FoodGroup: wire.ICBM,
  2312. SubGroup: wire.ICBMClientEvent,
  2313. },
  2314. Body: tt.inputBody,
  2315. }
  2316. svc := newMockICBMService(t)
  2317. svc.EXPECT().
  2318. ClientEvent(mock.Anything, mock.Anything, input.Frame, input.Body).
  2319. Return(tt.serviceError)
  2320. h := Handler{
  2321. ICBMService: svc,
  2322. RouteLogger: middleware.RouteLogger{
  2323. Logger: slog.Default(),
  2324. },
  2325. }
  2326. responseWriter := newMockResponseWriter(t)
  2327. buf := &bytes.Buffer{}
  2328. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2329. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2330. if tt.expectedError != nil {
  2331. assert.ErrorIs(t, err, tt.expectedError)
  2332. } else {
  2333. assert.NoError(t, err)
  2334. }
  2335. })
  2336. }
  2337. }
  2338. func TestHandler_ICBMEvilRequest(t *testing.T) {
  2339. tests := []struct {
  2340. name string
  2341. inputBody wire.SNAC_0x04_0x08_ICBMEvilRequest
  2342. serviceError error
  2343. responseError error
  2344. expectedError error
  2345. }{
  2346. {
  2347. name: "success",
  2348. inputBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  2349. ScreenName: "recipient-screen-name",
  2350. },
  2351. },
  2352. {
  2353. name: "service error",
  2354. inputBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  2355. ScreenName: "recipient-screen-name",
  2356. },
  2357. serviceError: assert.AnError,
  2358. expectedError: assert.AnError,
  2359. },
  2360. {
  2361. name: "response writer error",
  2362. inputBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  2363. ScreenName: "recipient-screen-name",
  2364. },
  2365. responseError: assert.AnError,
  2366. expectedError: assert.AnError,
  2367. },
  2368. }
  2369. for _, tt := range tests {
  2370. t.Run(tt.name, func(t *testing.T) {
  2371. input := wire.SNACMessage{
  2372. Frame: wire.SNACFrame{
  2373. FoodGroup: wire.ICBM,
  2374. SubGroup: wire.ICBMEvilRequest,
  2375. },
  2376. Body: tt.inputBody,
  2377. }
  2378. output := wire.SNACMessage{
  2379. Frame: wire.SNACFrame{
  2380. FoodGroup: wire.ICBM,
  2381. SubGroup: wire.ICBMEvilReply,
  2382. },
  2383. Body: wire.SNAC_0x04_0x09_ICBMEvilReply{
  2384. EvilDeltaApplied: 100,
  2385. },
  2386. }
  2387. svc := newMockICBMService(t)
  2388. svc.EXPECT().
  2389. EvilRequest(mock.Anything, mock.Anything, input.Frame, input.Body).
  2390. Return(output, tt.serviceError)
  2391. h := Handler{
  2392. ICBMService: svc,
  2393. RouteLogger: middleware.RouteLogger{
  2394. Logger: slog.Default(),
  2395. },
  2396. }
  2397. responseWriter := newMockResponseWriter(t)
  2398. if tt.serviceError == nil {
  2399. responseWriter.EXPECT().
  2400. SendSNAC(output.Frame, output.Body).
  2401. Return(tt.responseError)
  2402. }
  2403. buf := &bytes.Buffer{}
  2404. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2405. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2406. if tt.expectedError != nil {
  2407. assert.ErrorIs(t, err, tt.expectedError)
  2408. } else {
  2409. assert.NoError(t, err)
  2410. }
  2411. })
  2412. }
  2413. }
  2414. func TestHandler_ICBMOfflineRetrieve(t *testing.T) {
  2415. tests := []struct {
  2416. name string
  2417. serviceError error
  2418. responseError error
  2419. expectedError error
  2420. }{
  2421. {
  2422. name: "success",
  2423. },
  2424. {
  2425. name: "service error",
  2426. serviceError: assert.AnError,
  2427. expectedError: assert.AnError,
  2428. },
  2429. {
  2430. name: "response writer error",
  2431. responseError: assert.AnError,
  2432. expectedError: assert.AnError,
  2433. },
  2434. }
  2435. for _, tt := range tests {
  2436. t.Run(tt.name, func(t *testing.T) {
  2437. input := wire.SNACMessage{
  2438. Frame: wire.SNACFrame{
  2439. FoodGroup: wire.ICBM,
  2440. SubGroup: wire.ICBMOfflineRetrieve,
  2441. },
  2442. Body: wire.SNAC_0x04_0x0A_ICBMOfflineRetrieve{},
  2443. }
  2444. output := wire.SNACMessage{
  2445. Frame: wire.SNACFrame{
  2446. FoodGroup: wire.ICBM,
  2447. SubGroup: wire.ICBMOfflineRetrieveReply,
  2448. },
  2449. Body: wire.SNAC_0x04_0x17_ICBMOfflineRetrieveReply{},
  2450. }
  2451. svc := newMockICBMService(t)
  2452. svc.EXPECT().
  2453. OfflineRetrieve(mock.Anything, mock.Anything, input.Frame).
  2454. Return(output, tt.serviceError)
  2455. h := Handler{
  2456. ICBMService: svc,
  2457. RouteLogger: middleware.RouteLogger{
  2458. Logger: slog.Default(),
  2459. },
  2460. }
  2461. responseWriter := newMockResponseWriter(t)
  2462. if tt.serviceError == nil {
  2463. responseWriter.EXPECT().
  2464. SendSNAC(output.Frame, output.Body).
  2465. Return(tt.responseError)
  2466. }
  2467. buf := &bytes.Buffer{}
  2468. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2469. if tt.expectedError != nil {
  2470. assert.ErrorIs(t, err, tt.expectedError)
  2471. } else {
  2472. assert.NoError(t, err)
  2473. }
  2474. })
  2475. }
  2476. }
  2477. func TestHandler_ICBMParameterQuery(t *testing.T) {
  2478. tests := []struct {
  2479. name string
  2480. inputBody struct{}
  2481. responseError error
  2482. expectedError error
  2483. }{
  2484. {
  2485. name: "success",
  2486. inputBody: struct{}{},
  2487. },
  2488. {
  2489. name: "response writer error",
  2490. inputBody: struct{}{},
  2491. responseError: assert.AnError,
  2492. expectedError: assert.AnError,
  2493. },
  2494. }
  2495. for _, tt := range tests {
  2496. t.Run(tt.name, func(t *testing.T) {
  2497. input := wire.SNACMessage{
  2498. Frame: wire.SNACFrame{
  2499. FoodGroup: wire.ICBM,
  2500. SubGroup: wire.ICBMParameterQuery,
  2501. },
  2502. Body: tt.inputBody,
  2503. }
  2504. output := wire.SNACMessage{
  2505. Frame: wire.SNACFrame{
  2506. FoodGroup: wire.ICBM,
  2507. SubGroup: wire.ICBMParameterReply,
  2508. },
  2509. Body: wire.SNAC_0x04_0x05_ICBMParameterReply{
  2510. MaxSlots: 100,
  2511. },
  2512. }
  2513. svc := newMockICBMService(t)
  2514. svc.EXPECT().
  2515. ParameterQuery(mock.Anything, input.Frame).
  2516. Return(output)
  2517. h := Handler{
  2518. ICBMService: svc,
  2519. RouteLogger: middleware.RouteLogger{
  2520. Logger: slog.Default(),
  2521. },
  2522. }
  2523. responseWriter := newMockResponseWriter(t)
  2524. responseWriter.EXPECT().
  2525. SendSNAC(output.Frame, output.Body).
  2526. Return(tt.responseError)
  2527. buf := &bytes.Buffer{}
  2528. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2529. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2530. if tt.expectedError != nil {
  2531. assert.ErrorIs(t, err, tt.expectedError)
  2532. } else {
  2533. assert.NoError(t, err)
  2534. }
  2535. })
  2536. }
  2537. }
  2538. func TestHandler_ICQDBQuery(t *testing.T) {
  2539. type ICQMetaRequest struct {
  2540. wire.ICQMetadata
  2541. ReqSubType uint16
  2542. MetaRequest any
  2543. }
  2544. type reqParams struct {
  2545. ctx context.Context
  2546. instance *state.SessionInstance
  2547. inFrame wire.SNACFrame
  2548. inBody wire.SNAC_0x15_0x02_BQuery
  2549. rw ResponseWriter
  2550. seq uint16
  2551. wantErr error
  2552. }
  2553. type mockParam struct {
  2554. req any
  2555. wantErr error
  2556. }
  2557. type allMockParams struct {
  2558. deleteMsgReq *mockParam
  2559. findByDetails *mockParam
  2560. findByEmail *mockParam
  2561. findByEmail3 *mockParam
  2562. findByInterests *mockParam
  2563. findByUIN *mockParam
  2564. findByUIN2 *mockParam
  2565. findByWhitePages2 *mockParam
  2566. fullUserInfo *mockParam
  2567. offlineMsgReq *mockParam
  2568. setAffiliations *mockParam
  2569. setBasicInfo *mockParam
  2570. setEmails *mockParam
  2571. setInterests *mockParam
  2572. setMoreInfo *mockParam
  2573. setPermissions *mockParam
  2574. setICQPhone *mockParam
  2575. setUserNotes *mockParam
  2576. setWorkInfo *mockParam
  2577. shortUserInfo *mockParam
  2578. xmlReqData *mockParam
  2579. }
  2580. tests := []struct {
  2581. name string
  2582. reqParams reqParams
  2583. allMockParams allMockParams
  2584. }{
  2585. {
  2586. name: "MetaReqFullInfo - happy path",
  2587. reqParams: reqParams{
  2588. instance: state.NewSession().AddInstance(),
  2589. inBody: wire.SNAC_0x15_0x02_BQuery{
  2590. TLVRestBlock: wire.TLVRestBlock{
  2591. TLVList: wire.TLVList{
  2592. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2593. Message: ICQMetaRequest{
  2594. ICQMetadata: wire.ICQMetadata{
  2595. ReqType: wire.ICQDBQueryMetaReq,
  2596. Seq: 1,
  2597. },
  2598. ReqSubType: wire.ICQDBQueryMetaReqFullInfo,
  2599. MetaRequest: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2600. UIN: 123456789,
  2601. },
  2602. },
  2603. }),
  2604. },
  2605. },
  2606. },
  2607. seq: 1,
  2608. },
  2609. allMockParams: allMockParams{
  2610. fullUserInfo: &mockParam{
  2611. req: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2612. UIN: 123456789,
  2613. },
  2614. },
  2615. },
  2616. },
  2617. {
  2618. name: "MetaReqShortInfo - happy path",
  2619. reqParams: reqParams{
  2620. instance: state.NewSession().AddInstance(),
  2621. inBody: wire.SNAC_0x15_0x02_BQuery{
  2622. TLVRestBlock: wire.TLVRestBlock{
  2623. TLVList: wire.TLVList{
  2624. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2625. Message: ICQMetaRequest{
  2626. ICQMetadata: wire.ICQMetadata{
  2627. ReqType: wire.ICQDBQueryMetaReq,
  2628. Seq: 1,
  2629. },
  2630. ReqSubType: wire.ICQDBQueryMetaReqShortInfo,
  2631. MetaRequest: wire.ICQ_0x07D0_0x04BA_DBQueryMetaReqShortInfo{
  2632. UIN: 123456789,
  2633. },
  2634. },
  2635. }),
  2636. },
  2637. },
  2638. },
  2639. seq: 1,
  2640. },
  2641. allMockParams: allMockParams{
  2642. shortUserInfo: &mockParam{
  2643. req: wire.ICQ_0x07D0_0x04BA_DBQueryMetaReqShortInfo{
  2644. UIN: 123456789,
  2645. },
  2646. },
  2647. },
  2648. },
  2649. {
  2650. name: "MetaReqFullInfo2 - happy path",
  2651. reqParams: reqParams{
  2652. instance: state.NewSession().AddInstance(),
  2653. inBody: wire.SNAC_0x15_0x02_BQuery{
  2654. TLVRestBlock: wire.TLVRestBlock{
  2655. TLVList: wire.TLVList{
  2656. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2657. Message: ICQMetaRequest{
  2658. ICQMetadata: wire.ICQMetadata{
  2659. ReqType: wire.ICQDBQueryMetaReq,
  2660. Seq: 1,
  2661. },
  2662. ReqSubType: wire.ICQDBQueryMetaReqFullInfo2,
  2663. MetaRequest: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2664. UIN: 123456789,
  2665. },
  2666. },
  2667. }),
  2668. },
  2669. },
  2670. },
  2671. seq: 1,
  2672. },
  2673. allMockParams: allMockParams{
  2674. fullUserInfo: &mockParam{
  2675. req: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2676. UIN: 123456789,
  2677. },
  2678. },
  2679. },
  2680. },
  2681. {
  2682. name: "MetaReqXMLReq - happy path",
  2683. reqParams: reqParams{
  2684. instance: state.NewSession().AddInstance(),
  2685. inBody: wire.SNAC_0x15_0x02_BQuery{
  2686. TLVRestBlock: wire.TLVRestBlock{
  2687. TLVList: wire.TLVList{
  2688. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2689. Message: ICQMetaRequest{
  2690. ICQMetadata: wire.ICQMetadata{
  2691. ReqType: wire.ICQDBQueryMetaReq,
  2692. Seq: 1,
  2693. },
  2694. ReqSubType: wire.ICQDBQueryMetaReqXMLReq,
  2695. MetaRequest: wire.ICQ_0x07D0_0x0898_DBQueryMetaReqXMLReq{
  2696. XMLRequest: "<xml></xml>",
  2697. },
  2698. },
  2699. }),
  2700. },
  2701. },
  2702. },
  2703. seq: 1,
  2704. },
  2705. allMockParams: allMockParams{
  2706. xmlReqData: &mockParam{
  2707. req: wire.ICQ_0x07D0_0x0898_DBQueryMetaReqXMLReq{
  2708. XMLRequest: "<xml></xml>",
  2709. },
  2710. },
  2711. },
  2712. },
  2713. {
  2714. name: "MetaReqSetPermissions - happy path",
  2715. reqParams: reqParams{
  2716. instance: state.NewSession().AddInstance(),
  2717. inBody: wire.SNAC_0x15_0x02_BQuery{
  2718. TLVRestBlock: wire.TLVRestBlock{
  2719. TLVList: wire.TLVList{
  2720. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2721. Message: ICQMetaRequest{
  2722. ICQMetadata: wire.ICQMetadata{
  2723. ReqType: wire.ICQDBQueryMetaReq,
  2724. Seq: 1,
  2725. },
  2726. ReqSubType: wire.ICQDBQueryMetaReqSetPermissions,
  2727. MetaRequest: wire.ICQ_0x07D0_0x0424_DBQueryMetaReqSetPermissions{
  2728. Authorization: 1,
  2729. },
  2730. },
  2731. }),
  2732. },
  2733. },
  2734. },
  2735. seq: 1,
  2736. },
  2737. allMockParams: allMockParams{
  2738. setPermissions: &mockParam{
  2739. req: wire.ICQ_0x07D0_0x0424_DBQueryMetaReqSetPermissions{
  2740. Authorization: 1,
  2741. },
  2742. },
  2743. },
  2744. },
  2745. {
  2746. name: "MetaReqSetICQPhone - happy path",
  2747. reqParams: reqParams{
  2748. instance: state.NewSession().AddInstance(),
  2749. inBody: wire.SNAC_0x15_0x02_BQuery{
  2750. TLVRestBlock: wire.TLVRestBlock{
  2751. TLVList: wire.TLVList{
  2752. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2753. Message: ICQMetaRequest{
  2754. ICQMetadata: wire.ICQMetadata{
  2755. ReqType: wire.ICQDBQueryMetaReq,
  2756. Seq: 1,
  2757. },
  2758. ReqSubType: wire.ICQDBQueryMetaReqSetICQPhone,
  2759. MetaRequest: wire.ICQ_0x07D0_0x0654_DBQueryMetaReqSetICQPhone{},
  2760. },
  2761. }),
  2762. },
  2763. },
  2764. },
  2765. seq: 1,
  2766. },
  2767. allMockParams: allMockParams{
  2768. setICQPhone: &mockParam{
  2769. req: wire.ICQ_0x07D0_0x0654_DBQueryMetaReqSetICQPhone{},
  2770. },
  2771. },
  2772. },
  2773. {
  2774. name: "MetaReqSearchByUIN - happy path",
  2775. reqParams: reqParams{
  2776. instance: state.NewSession().AddInstance(),
  2777. inBody: wire.SNAC_0x15_0x02_BQuery{
  2778. TLVRestBlock: wire.TLVRestBlock{
  2779. TLVList: wire.TLVList{
  2780. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2781. Message: ICQMetaRequest{
  2782. ICQMetadata: wire.ICQMetadata{
  2783. ReqType: wire.ICQDBQueryMetaReq,
  2784. Seq: 1,
  2785. },
  2786. ReqSubType: wire.ICQDBQueryMetaReqSearchByUIN,
  2787. MetaRequest: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2788. UIN: 123456789,
  2789. },
  2790. },
  2791. }),
  2792. },
  2793. },
  2794. },
  2795. seq: 1,
  2796. },
  2797. allMockParams: allMockParams{
  2798. findByUIN: &mockParam{
  2799. req: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2800. UIN: 123456789,
  2801. },
  2802. },
  2803. },
  2804. },
  2805. {
  2806. name: "MetaReqSearchByUIN2 - happy path",
  2807. reqParams: reqParams{
  2808. instance: state.NewSession().AddInstance(),
  2809. inBody: wire.SNAC_0x15_0x02_BQuery{
  2810. TLVRestBlock: wire.TLVRestBlock{
  2811. TLVList: wire.TLVList{
  2812. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2813. Message: ICQMetaRequest{
  2814. ICQMetadata: wire.ICQMetadata{
  2815. ReqType: wire.ICQDBQueryMetaReq,
  2816. Seq: 1,
  2817. },
  2818. ReqSubType: wire.ICQDBQueryMetaReqSearchByUIN2,
  2819. MetaRequest: wire.ICQ_0x07D0_0x0569_DBQueryMetaReqSearchByUIN2{
  2820. TLVRestBlock: wire.TLVRestBlock{
  2821. TLVList: wire.TLVList{
  2822. wire.NewTLVBE(1, uint16(1)),
  2823. },
  2824. },
  2825. },
  2826. },
  2827. }),
  2828. },
  2829. },
  2830. },
  2831. seq: 1,
  2832. },
  2833. allMockParams: allMockParams{
  2834. findByUIN2: &mockParam{
  2835. req: wire.ICQ_0x07D0_0x0569_DBQueryMetaReqSearchByUIN2{
  2836. TLVRestBlock: wire.TLVRestBlock{
  2837. TLVList: wire.TLVList{
  2838. wire.NewTLVBE(1, uint16(1)),
  2839. },
  2840. },
  2841. },
  2842. },
  2843. },
  2844. },
  2845. {
  2846. name: "MetaReqSearchByEmail - happy path",
  2847. reqParams: reqParams{
  2848. instance: state.NewSession().AddInstance(),
  2849. inBody: wire.SNAC_0x15_0x02_BQuery{
  2850. TLVRestBlock: wire.TLVRestBlock{
  2851. TLVList: wire.TLVList{
  2852. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2853. Message: ICQMetaRequest{
  2854. ICQMetadata: wire.ICQMetadata{
  2855. ReqType: wire.ICQDBQueryMetaReq,
  2856. Seq: 1,
  2857. },
  2858. ReqSubType: wire.ICQDBQueryMetaReqSearchByEmail,
  2859. MetaRequest: wire.ICQ_0x07D0_0x0529_DBQueryMetaReqSearchByEmail{
  2860. Email: "test@aol.com",
  2861. },
  2862. },
  2863. }),
  2864. },
  2865. },
  2866. },
  2867. seq: 1,
  2868. },
  2869. allMockParams: allMockParams{
  2870. findByEmail: &mockParam{
  2871. req: wire.ICQ_0x07D0_0x0529_DBQueryMetaReqSearchByEmail{
  2872. Email: "test@aol.com",
  2873. },
  2874. },
  2875. },
  2876. },
  2877. {
  2878. name: "MetaReqSearchByEmail3 - happy path",
  2879. reqParams: reqParams{
  2880. instance: state.NewSession().AddInstance(),
  2881. inBody: wire.SNAC_0x15_0x02_BQuery{
  2882. TLVRestBlock: wire.TLVRestBlock{
  2883. TLVList: wire.TLVList{
  2884. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2885. Message: ICQMetaRequest{
  2886. ICQMetadata: wire.ICQMetadata{
  2887. ReqType: wire.ICQDBQueryMetaReq,
  2888. Seq: 1,
  2889. },
  2890. ReqSubType: wire.ICQDBQueryMetaReqSearchByEmail3,
  2891. MetaRequest: wire.ICQ_0x07D0_0x0573_DBQueryMetaReqSearchByEmail3{
  2892. TLVRestBlock: wire.TLVRestBlock{
  2893. TLVList: wire.TLVList{
  2894. wire.NewTLVBE(1, uint16(1)),
  2895. },
  2896. },
  2897. },
  2898. },
  2899. }),
  2900. },
  2901. },
  2902. },
  2903. seq: 1,
  2904. },
  2905. allMockParams: allMockParams{
  2906. findByEmail3: &mockParam{
  2907. req: wire.ICQ_0x07D0_0x0573_DBQueryMetaReqSearchByEmail3{
  2908. TLVRestBlock: wire.TLVRestBlock{
  2909. TLVList: wire.TLVList{
  2910. wire.NewTLVBE(1, uint16(1)),
  2911. },
  2912. },
  2913. },
  2914. },
  2915. },
  2916. },
  2917. {
  2918. name: "MetaReqSearchByDetails - happy path",
  2919. reqParams: reqParams{
  2920. instance: state.NewSession().AddInstance(),
  2921. inBody: wire.SNAC_0x15_0x02_BQuery{
  2922. TLVRestBlock: wire.TLVRestBlock{
  2923. TLVList: wire.TLVList{
  2924. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2925. Message: ICQMetaRequest{
  2926. ICQMetadata: wire.ICQMetadata{
  2927. ReqType: wire.ICQDBQueryMetaReq,
  2928. Seq: 1,
  2929. },
  2930. ReqSubType: wire.ICQDBQueryMetaReqSearchByDetails,
  2931. MetaRequest: wire.ICQ_0x07D0_0x0515_DBQueryMetaReqSearchByDetails{
  2932. FirstName: "john",
  2933. },
  2934. },
  2935. }),
  2936. },
  2937. },
  2938. },
  2939. seq: 1,
  2940. },
  2941. allMockParams: allMockParams{
  2942. findByDetails: &mockParam{
  2943. req: wire.ICQ_0x07D0_0x0515_DBQueryMetaReqSearchByDetails{
  2944. FirstName: "john",
  2945. },
  2946. },
  2947. },
  2948. },
  2949. {
  2950. name: "MetaReqSearchWhitePages - happy path",
  2951. reqParams: reqParams{
  2952. instance: state.NewSession().AddInstance(),
  2953. inBody: wire.SNAC_0x15_0x02_BQuery{
  2954. TLVRestBlock: wire.TLVRestBlock{
  2955. TLVList: wire.TLVList{
  2956. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2957. Message: ICQMetaRequest{
  2958. ICQMetadata: wire.ICQMetadata{
  2959. ReqType: wire.ICQDBQueryMetaReq,
  2960. Seq: 1,
  2961. },
  2962. ReqSubType: wire.ICQDBQueryMetaReqSearchWhitePages,
  2963. MetaRequest: wire.ICQ_0x07D0_0x0533_DBQueryMetaReqSearchWhitePages{
  2964. InterestsCode: 1,
  2965. },
  2966. },
  2967. }),
  2968. },
  2969. },
  2970. },
  2971. seq: 1,
  2972. },
  2973. allMockParams: allMockParams{
  2974. findByInterests: &mockParam{
  2975. req: wire.ICQ_0x07D0_0x0533_DBQueryMetaReqSearchWhitePages{
  2976. InterestsCode: 1,
  2977. },
  2978. },
  2979. },
  2980. },
  2981. {
  2982. name: "MetaReqSearchWhitePages2 - happy path",
  2983. reqParams: reqParams{
  2984. instance: state.NewSession().AddInstance(),
  2985. inBody: wire.SNAC_0x15_0x02_BQuery{
  2986. TLVRestBlock: wire.TLVRestBlock{
  2987. TLVList: wire.TLVList{
  2988. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2989. Message: ICQMetaRequest{
  2990. ICQMetadata: wire.ICQMetadata{
  2991. ReqType: wire.ICQDBQueryMetaReq,
  2992. Seq: 1,
  2993. },
  2994. ReqSubType: wire.ICQDBQueryMetaReqSearchWhitePages2,
  2995. MetaRequest: wire.ICQ_0x07D0_0x055F_DBQueryMetaReqSearchWhitePages2{
  2996. TLVRestBlock: wire.TLVRestBlock{
  2997. TLVList: wire.TLVList{
  2998. wire.NewTLVBE(1, uint16(1)),
  2999. },
  3000. },
  3001. },
  3002. },
  3003. }),
  3004. },
  3005. },
  3006. },
  3007. seq: 1,
  3008. },
  3009. allMockParams: allMockParams{
  3010. findByWhitePages2: &mockParam{
  3011. req: wire.ICQ_0x07D0_0x055F_DBQueryMetaReqSearchWhitePages2{
  3012. TLVRestBlock: wire.TLVRestBlock{
  3013. TLVList: wire.TLVList{
  3014. wire.NewTLVBE(1, uint16(1)),
  3015. },
  3016. },
  3017. },
  3018. },
  3019. },
  3020. },
  3021. {
  3022. name: "MetaReqSetBasicInfo - happy path",
  3023. reqParams: reqParams{
  3024. instance: state.NewSession().AddInstance(),
  3025. inBody: wire.SNAC_0x15_0x02_BQuery{
  3026. TLVRestBlock: wire.TLVRestBlock{
  3027. TLVList: wire.TLVList{
  3028. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3029. Message: ICQMetaRequest{
  3030. ICQMetadata: wire.ICQMetadata{
  3031. ReqType: wire.ICQDBQueryMetaReq,
  3032. Seq: 1,
  3033. },
  3034. ReqSubType: wire.ICQDBQueryMetaReqSetBasicInfo,
  3035. MetaRequest: wire.ICQ_0x07D0_0x03EA_DBQueryMetaReqSetBasicInfo{
  3036. FirstName: "john",
  3037. },
  3038. },
  3039. }),
  3040. },
  3041. },
  3042. },
  3043. seq: 1,
  3044. },
  3045. allMockParams: allMockParams{
  3046. setBasicInfo: &mockParam{
  3047. req: wire.ICQ_0x07D0_0x03EA_DBQueryMetaReqSetBasicInfo{
  3048. FirstName: "john",
  3049. },
  3050. },
  3051. },
  3052. },
  3053. {
  3054. name: "MetaReqSetWorkInfo - happy path",
  3055. reqParams: reqParams{
  3056. instance: state.NewSession().AddInstance(),
  3057. inBody: wire.SNAC_0x15_0x02_BQuery{
  3058. TLVRestBlock: wire.TLVRestBlock{
  3059. TLVList: wire.TLVList{
  3060. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3061. Message: ICQMetaRequest{
  3062. ICQMetadata: wire.ICQMetadata{
  3063. ReqType: wire.ICQDBQueryMetaReq,
  3064. Seq: 1,
  3065. },
  3066. ReqSubType: wire.ICQDBQueryMetaReqSetWorkInfo,
  3067. MetaRequest: wire.ICQ_0x07D0_0x03F3_DBQueryMetaReqSetWorkInfo{
  3068. ZIP: "11111",
  3069. },
  3070. },
  3071. }),
  3072. },
  3073. },
  3074. },
  3075. seq: 1,
  3076. },
  3077. allMockParams: allMockParams{
  3078. setWorkInfo: &mockParam{
  3079. req: wire.ICQ_0x07D0_0x03F3_DBQueryMetaReqSetWorkInfo{
  3080. ZIP: "11111",
  3081. },
  3082. },
  3083. },
  3084. },
  3085. {
  3086. name: "MetaReqSetMoreInfo - happy path",
  3087. reqParams: reqParams{
  3088. instance: state.NewSession().AddInstance(),
  3089. inBody: wire.SNAC_0x15_0x02_BQuery{
  3090. TLVRestBlock: wire.TLVRestBlock{
  3091. TLVList: wire.TLVList{
  3092. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3093. Message: ICQMetaRequest{
  3094. ICQMetadata: wire.ICQMetadata{
  3095. ReqType: wire.ICQDBQueryMetaReq,
  3096. Seq: 1,
  3097. },
  3098. ReqSubType: wire.ICQDBQueryMetaReqSetMoreInfo,
  3099. MetaRequest: wire.ICQ_0x07D0_0x03FD_DBQueryMetaReqSetMoreInfo{
  3100. Age: 100,
  3101. },
  3102. },
  3103. }),
  3104. },
  3105. },
  3106. },
  3107. seq: 1,
  3108. },
  3109. allMockParams: allMockParams{
  3110. setMoreInfo: &mockParam{
  3111. req: wire.ICQ_0x07D0_0x03FD_DBQueryMetaReqSetMoreInfo{
  3112. Age: 100,
  3113. },
  3114. },
  3115. },
  3116. },
  3117. {
  3118. name: "MetaReqSetNotes - happy path",
  3119. reqParams: reqParams{
  3120. instance: state.NewSession().AddInstance(),
  3121. inBody: wire.SNAC_0x15_0x02_BQuery{
  3122. TLVRestBlock: wire.TLVRestBlock{
  3123. TLVList: wire.TLVList{
  3124. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3125. Message: ICQMetaRequest{
  3126. ICQMetadata: wire.ICQMetadata{
  3127. ReqType: wire.ICQDBQueryMetaReq,
  3128. Seq: 1,
  3129. },
  3130. ReqSubType: wire.ICQDBQueryMetaReqSetNotes,
  3131. MetaRequest: wire.ICQ_0x07D0_0x0406_DBQueryMetaReqSetNotes{
  3132. Notes: "my note",
  3133. },
  3134. },
  3135. }),
  3136. },
  3137. },
  3138. },
  3139. seq: 1,
  3140. },
  3141. allMockParams: allMockParams{
  3142. setUserNotes: &mockParam{
  3143. req: wire.ICQ_0x07D0_0x0406_DBQueryMetaReqSetNotes{
  3144. Notes: "my note",
  3145. },
  3146. },
  3147. },
  3148. },
  3149. {
  3150. name: "MetaReqSetEmails - happy path",
  3151. reqParams: reqParams{
  3152. instance: state.NewSession().AddInstance(),
  3153. inBody: wire.SNAC_0x15_0x02_BQuery{
  3154. TLVRestBlock: wire.TLVRestBlock{
  3155. TLVList: wire.TLVList{
  3156. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3157. Message: ICQMetaRequest{
  3158. ICQMetadata: wire.ICQMetadata{
  3159. ReqType: wire.ICQDBQueryMetaReq,
  3160. Seq: 1,
  3161. },
  3162. ReqSubType: wire.ICQDBQueryMetaReqSetEmails,
  3163. MetaRequest: wire.ICQ_0x07D0_0x040B_DBQueryMetaReqSetEmails{
  3164. Emails: []struct {
  3165. Publish uint8
  3166. Email string `oscar:"len_prefix=uint16,nullterm"`
  3167. }{
  3168. {
  3169. Email: "test@aol.com",
  3170. },
  3171. },
  3172. },
  3173. },
  3174. }),
  3175. },
  3176. },
  3177. },
  3178. seq: 1,
  3179. },
  3180. allMockParams: allMockParams{
  3181. setEmails: &mockParam{
  3182. req: wire.ICQ_0x07D0_0x040B_DBQueryMetaReqSetEmails{
  3183. Emails: []struct {
  3184. Publish uint8
  3185. Email string `oscar:"len_prefix=uint16,nullterm"`
  3186. }{
  3187. {
  3188. Email: "test@aol.com",
  3189. },
  3190. },
  3191. },
  3192. },
  3193. },
  3194. },
  3195. {
  3196. name: "MetaReqSetInterests - happy path",
  3197. reqParams: reqParams{
  3198. instance: state.NewSession().AddInstance(),
  3199. inBody: wire.SNAC_0x15_0x02_BQuery{
  3200. TLVRestBlock: wire.TLVRestBlock{
  3201. TLVList: wire.TLVList{
  3202. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3203. Message: ICQMetaRequest{
  3204. ICQMetadata: wire.ICQMetadata{
  3205. ReqType: wire.ICQDBQueryMetaReq,
  3206. Seq: 1,
  3207. },
  3208. ReqSubType: wire.ICQDBQueryMetaReqSetInterests,
  3209. MetaRequest: wire.ICQ_0x07D0_0x0410_DBQueryMetaReqSetInterests{
  3210. Interests: []struct {
  3211. Code uint16
  3212. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  3213. }{
  3214. {
  3215. Keyword: "an_interest",
  3216. },
  3217. },
  3218. },
  3219. },
  3220. }),
  3221. },
  3222. },
  3223. },
  3224. seq: 1,
  3225. },
  3226. allMockParams: allMockParams{
  3227. setInterests: &mockParam{
  3228. req: wire.ICQ_0x07D0_0x0410_DBQueryMetaReqSetInterests{
  3229. Interests: []struct {
  3230. Code uint16
  3231. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  3232. }{
  3233. {
  3234. Keyword: "an_interest",
  3235. },
  3236. },
  3237. },
  3238. },
  3239. },
  3240. },
  3241. {
  3242. name: "MetaReqSetAffiliations - happy path",
  3243. reqParams: reqParams{
  3244. instance: state.NewSession().AddInstance(),
  3245. inBody: wire.SNAC_0x15_0x02_BQuery{
  3246. TLVRestBlock: wire.TLVRestBlock{
  3247. TLVList: wire.TLVList{
  3248. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3249. Message: ICQMetaRequest{
  3250. ICQMetadata: wire.ICQMetadata{
  3251. ReqType: wire.ICQDBQueryMetaReq,
  3252. Seq: 1,
  3253. },
  3254. ReqSubType: wire.ICQDBQueryMetaReqSetAffiliations,
  3255. MetaRequest: wire.ICQ_0x07D0_0x041A_DBQueryMetaReqSetAffiliations{
  3256. PastAffiliations: []struct {
  3257. Code uint16
  3258. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  3259. }{
  3260. {
  3261. Keyword: "a_past_affiliation",
  3262. },
  3263. },
  3264. Affiliations: []struct {
  3265. Code uint16
  3266. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  3267. }{
  3268. {
  3269. Keyword: "an_affiliation",
  3270. },
  3271. },
  3272. },
  3273. },
  3274. }),
  3275. },
  3276. },
  3277. },
  3278. seq: 1,
  3279. },
  3280. allMockParams: allMockParams{
  3281. setAffiliations: &mockParam{
  3282. req: wire.ICQ_0x07D0_0x041A_DBQueryMetaReqSetAffiliations{
  3283. PastAffiliations: []struct {
  3284. Code uint16
  3285. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  3286. }{
  3287. {
  3288. Keyword: "a_past_affiliation",
  3289. },
  3290. },
  3291. Affiliations: []struct {
  3292. Code uint16
  3293. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  3294. }{
  3295. {
  3296. Keyword: "an_affiliation",
  3297. },
  3298. },
  3299. },
  3300. },
  3301. },
  3302. },
  3303. {
  3304. name: "MetaReqStat - happy path",
  3305. reqParams: reqParams{
  3306. instance: state.NewSession().AddInstance(),
  3307. inBody: wire.SNAC_0x15_0x02_BQuery{
  3308. TLVRestBlock: wire.TLVRestBlock{
  3309. TLVList: wire.TLVList{
  3310. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3311. Message: ICQMetaRequest{
  3312. ICQMetadata: wire.ICQMetadata{
  3313. ReqType: wire.ICQDBQueryMetaReq,
  3314. Seq: 1,
  3315. },
  3316. ReqSubType: wire.ICQDBQueryMetaReqStat0a8c,
  3317. MetaRequest: struct{}{},
  3318. },
  3319. }),
  3320. },
  3321. },
  3322. },
  3323. seq: 1,
  3324. },
  3325. },
  3326. {
  3327. name: "unknown metadata request subtype",
  3328. reqParams: reqParams{
  3329. instance: state.NewSession().AddInstance(),
  3330. inBody: wire.SNAC_0x15_0x02_BQuery{
  3331. TLVRestBlock: wire.TLVRestBlock{
  3332. TLVList: wire.TLVList{
  3333. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3334. Message: ICQMetaRequest{
  3335. ICQMetadata: wire.ICQMetadata{
  3336. ReqType: wire.ICQDBQueryMetaReq,
  3337. Seq: 1,
  3338. },
  3339. ReqSubType: 0xCA8E,
  3340. MetaRequest: struct{}{},
  3341. },
  3342. }),
  3343. },
  3344. },
  3345. },
  3346. seq: 1,
  3347. wantErr: errUnknownICQMetaReqSubType,
  3348. },
  3349. },
  3350. {
  3351. name: "OfflineMsgReq - happy path",
  3352. reqParams: reqParams{
  3353. instance: state.NewSession().AddInstance(),
  3354. inBody: wire.SNAC_0x15_0x02_BQuery{
  3355. TLVRestBlock: wire.TLVRestBlock{
  3356. TLVList: wire.TLVList{
  3357. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3358. Message: wire.ICQMetadataWithSubType{
  3359. ICQMetadata: wire.ICQMetadata{
  3360. ReqType: wire.ICQDBQueryOfflineMsgReq,
  3361. Seq: 1,
  3362. },
  3363. },
  3364. }),
  3365. },
  3366. },
  3367. },
  3368. seq: 1,
  3369. },
  3370. allMockParams: allMockParams{
  3371. offlineMsgReq: &mockParam{},
  3372. },
  3373. },
  3374. {
  3375. name: "DeleteMsgReq - happy path",
  3376. reqParams: reqParams{
  3377. instance: state.NewSession().AddInstance(),
  3378. inBody: wire.SNAC_0x15_0x02_BQuery{
  3379. TLVRestBlock: wire.TLVRestBlock{
  3380. TLVList: wire.TLVList{
  3381. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3382. Message: wire.ICQMetadataWithSubType{
  3383. ICQMetadata: wire.ICQMetadata{
  3384. ReqType: wire.ICQDBQueryDeleteMsgReq,
  3385. Seq: 1,
  3386. },
  3387. },
  3388. }),
  3389. },
  3390. },
  3391. },
  3392. seq: 1,
  3393. },
  3394. allMockParams: allMockParams{
  3395. deleteMsgReq: &mockParam{},
  3396. },
  3397. },
  3398. {
  3399. name: "unknown request type",
  3400. reqParams: reqParams{
  3401. instance: state.NewSession().AddInstance(),
  3402. inBody: wire.SNAC_0x15_0x02_BQuery{
  3403. TLVRestBlock: wire.TLVRestBlock{
  3404. TLVList: wire.TLVList{
  3405. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3406. Message: ICQMetaRequest{
  3407. ICQMetadata: wire.ICQMetadata{
  3408. ReqType: 0x13B4,
  3409. Seq: 1,
  3410. },
  3411. ReqSubType: 0xCA8E,
  3412. MetaRequest: struct{}{},
  3413. },
  3414. }),
  3415. },
  3416. },
  3417. },
  3418. seq: 1,
  3419. wantErr: errUnknownICQMetaReqType,
  3420. },
  3421. }, // todo: add to a separate test
  3422. }
  3423. for _, tt := range tests {
  3424. t.Run(tt.name, func(t *testing.T) {
  3425. icqService := newMockICQService(t)
  3426. switch {
  3427. case tt.allMockParams.fullUserInfo != nil:
  3428. icqService.EXPECT().
  3429. FullUserInfo(mock.Anything, tt.reqParams.instance, tt.allMockParams.fullUserInfo.req, tt.reqParams.seq).
  3430. Return(tt.allMockParams.fullUserInfo.wantErr)
  3431. case tt.allMockParams.shortUserInfo != nil:
  3432. icqService.EXPECT().
  3433. ShortUserInfo(mock.Anything, tt.reqParams.instance, tt.allMockParams.shortUserInfo.req, tt.reqParams.seq).
  3434. Return(tt.allMockParams.shortUserInfo.wantErr)
  3435. case tt.allMockParams.xmlReqData != nil:
  3436. icqService.EXPECT().
  3437. XMLReqData(mock.Anything, tt.reqParams.instance, tt.allMockParams.xmlReqData.req, tt.reqParams.seq).
  3438. Return(tt.allMockParams.xmlReqData.wantErr)
  3439. case tt.allMockParams.setPermissions != nil:
  3440. icqService.EXPECT().
  3441. SetPermissions(mock.Anything, tt.reqParams.instance, tt.allMockParams.setPermissions.req, tt.reqParams.seq).
  3442. Return(tt.allMockParams.setPermissions.wantErr)
  3443. case tt.allMockParams.setICQPhone != nil:
  3444. icqService.EXPECT().
  3445. SetICQPhone(mock.Anything, tt.reqParams.instance, tt.allMockParams.setICQPhone.req, tt.reqParams.seq).
  3446. Return(tt.allMockParams.setICQPhone.wantErr)
  3447. case tt.allMockParams.findByUIN != nil:
  3448. icqService.EXPECT().
  3449. FindByUIN(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByUIN.req, tt.reqParams.seq).
  3450. Return(tt.allMockParams.findByUIN.wantErr)
  3451. case tt.allMockParams.findByUIN2 != nil:
  3452. icqService.EXPECT().
  3453. FindByUIN2(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByUIN2.req, tt.reqParams.seq).
  3454. Return(tt.allMockParams.findByUIN2.wantErr)
  3455. case tt.allMockParams.findByEmail != nil:
  3456. icqService.EXPECT().
  3457. FindByICQEmail(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByEmail.req, tt.reqParams.seq).
  3458. Return(tt.allMockParams.findByEmail.wantErr)
  3459. case tt.allMockParams.findByEmail3 != nil:
  3460. icqService.EXPECT().
  3461. FindByEmail3(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByEmail3.req, tt.reqParams.seq).
  3462. Return(tt.allMockParams.findByEmail3.wantErr)
  3463. case tt.allMockParams.findByDetails != nil:
  3464. icqService.EXPECT().
  3465. FindByICQName(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByDetails.req, tt.reqParams.seq).
  3466. Return(tt.allMockParams.findByDetails.wantErr)
  3467. case tt.allMockParams.findByInterests != nil:
  3468. icqService.EXPECT().
  3469. FindByICQInterests(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByInterests.req, tt.reqParams.seq).
  3470. Return(tt.allMockParams.findByInterests.wantErr)
  3471. case tt.allMockParams.findByWhitePages2 != nil:
  3472. icqService.EXPECT().
  3473. FindByWhitePages2(mock.Anything, tt.reqParams.instance, tt.allMockParams.findByWhitePages2.req, tt.reqParams.seq).
  3474. Return(tt.allMockParams.findByWhitePages2.wantErr)
  3475. case tt.allMockParams.setBasicInfo != nil:
  3476. icqService.EXPECT().
  3477. SetBasicInfo(mock.Anything, tt.reqParams.instance, tt.allMockParams.setBasicInfo.req, tt.reqParams.seq).
  3478. Return(tt.allMockParams.setBasicInfo.wantErr)
  3479. case tt.allMockParams.setWorkInfo != nil:
  3480. icqService.EXPECT().
  3481. SetWorkInfo(mock.Anything, tt.reqParams.instance, tt.allMockParams.setWorkInfo.req, tt.reqParams.seq).
  3482. Return(tt.allMockParams.setWorkInfo.wantErr)
  3483. case tt.allMockParams.setMoreInfo != nil:
  3484. icqService.EXPECT().
  3485. SetMoreInfo(mock.Anything, tt.reqParams.instance, tt.allMockParams.setMoreInfo.req, tt.reqParams.seq).
  3486. Return(tt.allMockParams.setMoreInfo.wantErr)
  3487. case tt.allMockParams.setUserNotes != nil:
  3488. icqService.EXPECT().
  3489. SetUserNotes(mock.Anything, tt.reqParams.instance, tt.allMockParams.setUserNotes.req, tt.reqParams.seq).
  3490. Return(tt.allMockParams.setUserNotes.wantErr)
  3491. case tt.allMockParams.setEmails != nil:
  3492. icqService.EXPECT().
  3493. SetEmails(mock.Anything, tt.reqParams.instance, tt.allMockParams.setEmails.req, tt.reqParams.seq).
  3494. Return(tt.allMockParams.setEmails.wantErr)
  3495. case tt.allMockParams.setInterests != nil:
  3496. icqService.EXPECT().
  3497. SetInterests(mock.Anything, tt.reqParams.instance, tt.allMockParams.setInterests.req, tt.reqParams.seq).
  3498. Return(tt.allMockParams.setInterests.wantErr)
  3499. case tt.allMockParams.setAffiliations != nil:
  3500. icqService.EXPECT().
  3501. SetAffiliations(mock.Anything, tt.reqParams.instance, tt.allMockParams.setAffiliations.req, tt.reqParams.seq).
  3502. Return(tt.allMockParams.setAffiliations.wantErr)
  3503. case tt.allMockParams.offlineMsgReq != nil:
  3504. icqService.EXPECT().
  3505. OfflineMsgReq(mock.Anything, tt.reqParams.instance, tt.reqParams.seq).
  3506. Return(tt.allMockParams.offlineMsgReq.wantErr)
  3507. case tt.allMockParams.deleteMsgReq != nil:
  3508. icqService.EXPECT().
  3509. DeleteMsgReq(mock.Anything, tt.reqParams.instance, tt.reqParams.seq).
  3510. Return(tt.allMockParams.deleteMsgReq.wantErr)
  3511. }
  3512. h := Handler{
  3513. ICQService: icqService,
  3514. RouteLogger: middleware.RouteLogger{
  3515. Logger: slog.Default(),
  3516. },
  3517. }
  3518. buf := &bytes.Buffer{}
  3519. assert.NoError(t, wire.MarshalBE(tt.reqParams.inBody, buf))
  3520. frame := wire.SNACFrame{
  3521. FoodGroup: wire.ICQ,
  3522. SubGroup: wire.ICQDBQuery,
  3523. }
  3524. err := h.Handle(context.TODO(), wire.BOS, tt.reqParams.instance, frame, buf, nil, config.Listener{})
  3525. assert.ErrorIs(t, err, tt.reqParams.wantErr)
  3526. })
  3527. }
  3528. }
  3529. // Test workaround for QIP 2005 bug where TLV length is incorrect.
  3530. func TestHandler_ICQDBQuery_QIP2005UINSearchBug(t *testing.T) {
  3531. icqService := newMockICQService(t)
  3532. type ICQMetaRequest struct {
  3533. wire.ICQMetadata
  3534. ReqSubType uint16
  3535. MetaRequest any
  3536. }
  3537. expect := wire.ICQ_0x07D0_0x0569_DBQueryMetaReqSearchByUIN2{
  3538. TLVRestBlock: wire.TLVRestBlock{
  3539. TLVList: wire.TLVList{
  3540. wire.NewTLVBE(wire.ICQTLVTagsUIN, uint32(100009)),
  3541. },
  3542. },
  3543. }
  3544. instance := state.NewSession().AddInstance()
  3545. icqService.EXPECT().
  3546. FindByUIN2(mock.Anything, instance, expect, uint16(1)).
  3547. Return(nil)
  3548. h := Handler{
  3549. ICQService: icqService,
  3550. RouteLogger: middleware.RouteLogger{
  3551. Logger: slog.Default(),
  3552. },
  3553. }
  3554. inBody := wire.SNAC_0x15_0x02_BQuery{
  3555. TLVRestBlock: wire.TLVRestBlock{
  3556. TLVList: wire.TLVList{
  3557. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3558. Message: ICQMetaRequest{
  3559. ICQMetadata: wire.ICQMetadata{
  3560. ReqType: wire.ICQDBQueryMetaReq,
  3561. Seq: 1,
  3562. },
  3563. ReqSubType: wire.ICQDBQueryMetaReqSearchByUIN2,
  3564. MetaRequest: wire.ICQ_0x07D0_0x0569_DBQueryMetaReqSearchByUIN2{
  3565. TLVRestBlock: wire.TLVRestBlock{
  3566. TLVList: wire.TLVList{
  3567. wire.NewTLVBE(wire.ICQTLVTagsUIN, uint32(100009)),
  3568. },
  3569. },
  3570. },
  3571. },
  3572. }),
  3573. },
  3574. },
  3575. }
  3576. buf := &bytes.Buffer{}
  3577. assert.NoError(t, wire.MarshalBE(inBody, buf))
  3578. b := buf.Bytes()
  3579. b[18] = 6 // incorrectly set TLV length to 6 (should be 4)
  3580. err := h.ICQDBQuery(nil, instance, wire.SNACFrame{}, buf, nil)
  3581. assert.NoError(t, err)
  3582. }
  3583. func TestHandler_ODirInfoQuery(t *testing.T) {
  3584. tests := []struct {
  3585. name string
  3586. inputBody wire.SNAC_0x0F_0x02_InfoQuery
  3587. serviceError error
  3588. responseError error
  3589. expectedError error
  3590. }{
  3591. {
  3592. name: "success",
  3593. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3594. TLVRestBlock: wire.TLVRestBlock{
  3595. TLVList: wire.TLVList{
  3596. wire.NewTLVBE(1, uint16(2)),
  3597. },
  3598. },
  3599. },
  3600. },
  3601. {
  3602. name: "service error",
  3603. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3604. TLVRestBlock: wire.TLVRestBlock{
  3605. TLVList: wire.TLVList{
  3606. wire.NewTLVBE(1, uint16(2)),
  3607. },
  3608. },
  3609. },
  3610. serviceError: assert.AnError,
  3611. expectedError: assert.AnError,
  3612. },
  3613. {
  3614. name: "response writer error",
  3615. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3616. TLVRestBlock: wire.TLVRestBlock{
  3617. TLVList: wire.TLVList{
  3618. wire.NewTLVBE(1, uint16(2)),
  3619. },
  3620. },
  3621. },
  3622. responseError: assert.AnError,
  3623. expectedError: assert.AnError,
  3624. },
  3625. }
  3626. for _, tt := range tests {
  3627. t.Run(tt.name, func(t *testing.T) {
  3628. input := wire.SNACMessage{
  3629. Frame: wire.SNACFrame{
  3630. FoodGroup: wire.ODir,
  3631. SubGroup: wire.ODirInfoQuery,
  3632. },
  3633. Body: tt.inputBody,
  3634. }
  3635. output := wire.SNACMessage{
  3636. Frame: wire.SNACFrame{
  3637. FoodGroup: wire.ODir,
  3638. SubGroup: wire.ODirInfoReply,
  3639. },
  3640. Body: wire.SNAC_0x0F_0x03_InfoReply{
  3641. Status: 5, // OK has results/not found
  3642. },
  3643. }
  3644. svc := newMockODirService(t)
  3645. svc.EXPECT().
  3646. InfoQuery(mock.Anything, input.Frame, input.Body).
  3647. Return(output, tt.serviceError)
  3648. h := Handler{
  3649. ODirService: svc,
  3650. RouteLogger: middleware.RouteLogger{
  3651. Logger: slog.Default(),
  3652. },
  3653. }
  3654. ss := newMockResponseWriter(t)
  3655. if tt.serviceError == nil {
  3656. ss.EXPECT().
  3657. SendSNAC(output.Frame, output.Body).
  3658. Return(tt.responseError)
  3659. }
  3660. buf := &bytes.Buffer{}
  3661. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3662. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, ss, config.Listener{})
  3663. if tt.expectedError != nil {
  3664. assert.ErrorIs(t, err, tt.expectedError)
  3665. } else {
  3666. assert.NoError(t, err)
  3667. }
  3668. })
  3669. }
  3670. }
  3671. func TestHandler_ODirKeywordListQuery(t *testing.T) {
  3672. tests := []struct {
  3673. name string
  3674. inputBody wire.SNAC_0x0F_0x02_InfoQuery
  3675. serviceError error
  3676. responseError error
  3677. expectedError error
  3678. }{
  3679. {
  3680. name: "success",
  3681. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3682. TLVRestBlock: wire.TLVRestBlock{
  3683. TLVList: wire.TLVList{
  3684. wire.NewTLVBE(1, uint16(2)),
  3685. },
  3686. },
  3687. },
  3688. },
  3689. {
  3690. name: "service error",
  3691. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3692. TLVRestBlock: wire.TLVRestBlock{
  3693. TLVList: wire.TLVList{
  3694. wire.NewTLVBE(1, uint16(2)),
  3695. },
  3696. },
  3697. },
  3698. serviceError: assert.AnError,
  3699. expectedError: assert.AnError,
  3700. },
  3701. {
  3702. name: "response writer error",
  3703. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3704. TLVRestBlock: wire.TLVRestBlock{
  3705. TLVList: wire.TLVList{
  3706. wire.NewTLVBE(1, uint16(2)),
  3707. },
  3708. },
  3709. },
  3710. responseError: assert.AnError,
  3711. expectedError: assert.AnError,
  3712. },
  3713. }
  3714. for _, tt := range tests {
  3715. t.Run(tt.name, func(t *testing.T) {
  3716. input := wire.SNACMessage{
  3717. Frame: wire.SNACFrame{
  3718. FoodGroup: wire.ODir,
  3719. SubGroup: wire.ODirKeywordListQuery,
  3720. },
  3721. Body: tt.inputBody,
  3722. }
  3723. output := wire.SNACMessage{
  3724. Frame: wire.SNACFrame{
  3725. FoodGroup: wire.ODir,
  3726. SubGroup: wire.ODirKeywordListReply,
  3727. },
  3728. Body: wire.SNAC_0x0F_0x04_KeywordListReply{
  3729. Status: 0x01,
  3730. },
  3731. }
  3732. svc := newMockODirService(t)
  3733. svc.EXPECT().
  3734. KeywordListQuery(mock.Anything, input.Frame).
  3735. Return(output, tt.serviceError)
  3736. h := Handler{
  3737. ODirService: svc,
  3738. RouteLogger: middleware.RouteLogger{
  3739. Logger: slog.Default(),
  3740. },
  3741. }
  3742. ss := newMockResponseWriter(t)
  3743. if tt.serviceError == nil {
  3744. ss.EXPECT().
  3745. SendSNAC(output.Frame, output.Body).
  3746. Return(tt.responseError)
  3747. }
  3748. buf := &bytes.Buffer{}
  3749. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3750. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, ss, config.Listener{})
  3751. if tt.expectedError != nil {
  3752. assert.ErrorIs(t, err, tt.expectedError)
  3753. } else {
  3754. assert.NoError(t, err)
  3755. }
  3756. })
  3757. }
  3758. }
  3759. func TestHandler_OServiceServiceClientOnline(t *testing.T) {
  3760. tests := []struct {
  3761. name string
  3762. inputBody wire.SNAC_0x01_0x02_OServiceClientOnline
  3763. serviceError error
  3764. expectedError error
  3765. }{
  3766. {
  3767. name: "success",
  3768. inputBody: wire.SNAC_0x01_0x02_OServiceClientOnline{
  3769. GroupVersions: []struct {
  3770. FoodGroup uint16
  3771. Version uint16
  3772. ToolID uint16
  3773. ToolVersion uint16
  3774. }{
  3775. {
  3776. FoodGroup: 10,
  3777. },
  3778. },
  3779. },
  3780. },
  3781. {
  3782. name: "service error",
  3783. inputBody: wire.SNAC_0x01_0x02_OServiceClientOnline{
  3784. GroupVersions: []struct {
  3785. FoodGroup uint16
  3786. Version uint16
  3787. ToolID uint16
  3788. ToolVersion uint16
  3789. }{
  3790. {
  3791. FoodGroup: 10,
  3792. },
  3793. },
  3794. },
  3795. serviceError: assert.AnError,
  3796. expectedError: assert.AnError,
  3797. },
  3798. }
  3799. for _, tt := range tests {
  3800. t.Run(tt.name, func(t *testing.T) {
  3801. input := wire.SNACMessage{
  3802. Frame: wire.SNACFrame{
  3803. FoodGroup: wire.OService,
  3804. SubGroup: wire.OServiceClientOnline,
  3805. },
  3806. Body: tt.inputBody,
  3807. }
  3808. svc := newMockOServiceService(t)
  3809. svc.EXPECT().
  3810. ClientOnline(mock.Anything, wire.BOS, input.Body, mock.Anything).
  3811. Return(tt.serviceError)
  3812. h := Handler{
  3813. OServiceService: svc,
  3814. RouteLogger: middleware.RouteLogger{
  3815. Logger: slog.Default(),
  3816. },
  3817. }
  3818. buf := &bytes.Buffer{}
  3819. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3820. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, nil, config.Listener{})
  3821. if tt.expectedError != nil {
  3822. assert.ErrorIs(t, err, tt.expectedError)
  3823. } else {
  3824. assert.NoError(t, err)
  3825. }
  3826. })
  3827. }
  3828. }
  3829. func TestHandler_OServiceServiceServiceRequest(t *testing.T) {
  3830. tests := []struct {
  3831. name string
  3832. inputBody wire.SNAC_0x01_0x04_OServiceServiceRequest
  3833. serviceError error
  3834. responseError error
  3835. expectedError error
  3836. }{
  3837. {
  3838. name: "success",
  3839. inputBody: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  3840. FoodGroup: wire.Chat,
  3841. },
  3842. },
  3843. {
  3844. name: "service error",
  3845. inputBody: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  3846. FoodGroup: wire.Chat,
  3847. },
  3848. serviceError: assert.AnError,
  3849. expectedError: assert.AnError,
  3850. },
  3851. {
  3852. name: "response writer error",
  3853. inputBody: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  3854. FoodGroup: wire.Chat,
  3855. },
  3856. responseError: assert.AnError,
  3857. expectedError: assert.AnError,
  3858. },
  3859. }
  3860. for _, tt := range tests {
  3861. t.Run(tt.name, func(t *testing.T) {
  3862. input := wire.SNACMessage{
  3863. Frame: wire.SNACFrame{
  3864. FoodGroup: wire.OService,
  3865. SubGroup: wire.OServiceServiceRequest,
  3866. },
  3867. Body: tt.inputBody,
  3868. }
  3869. output := wire.SNACMessage{
  3870. Frame: wire.SNACFrame{
  3871. FoodGroup: wire.OService,
  3872. SubGroup: wire.OServiceServiceResponse,
  3873. },
  3874. Body: wire.SNAC_0x01_0x05_OServiceServiceResponse{
  3875. TLVRestBlock: wire.TLVRestBlock{
  3876. TLVList: wire.TLVList{
  3877. wire.NewTLVBE(0x01, uint16(1000)),
  3878. },
  3879. },
  3880. },
  3881. }
  3882. svc := newMockOServiceService(t)
  3883. svc.EXPECT().
  3884. ServiceRequest(mock.Anything, wire.BOS, mock.Anything, input.Frame, input.Body, config.Listener{BOSAdvertisedHostPlain: "127.0.0.1:1234"}).
  3885. Return(output, tt.serviceError)
  3886. h := Handler{
  3887. OServiceService: svc,
  3888. RouteLogger: middleware.RouteLogger{
  3889. Logger: slog.Default(),
  3890. },
  3891. }
  3892. responseWriter := newMockResponseWriter(t)
  3893. if tt.serviceError == nil {
  3894. responseWriter.EXPECT().
  3895. SendSNAC(output.Frame, output.Body).
  3896. Return(tt.responseError)
  3897. }
  3898. buf := &bytes.Buffer{}
  3899. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3900. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{BOSAdvertisedHostPlain: "127.0.0.1:1234"})
  3901. if tt.expectedError != nil {
  3902. assert.ErrorIs(t, err, tt.expectedError)
  3903. } else {
  3904. assert.NoError(t, err)
  3905. }
  3906. })
  3907. }
  3908. }
  3909. func TestHandler_OServiceServiceIdleNotification(t *testing.T) {
  3910. tests := []struct {
  3911. name string
  3912. inputBody wire.SNAC_0x01_0x11_OServiceIdleNotification
  3913. serviceError error
  3914. expectedError error
  3915. }{
  3916. {
  3917. name: "success",
  3918. inputBody: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  3919. IdleTime: 10,
  3920. },
  3921. },
  3922. {
  3923. name: "service error",
  3924. inputBody: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  3925. IdleTime: 10,
  3926. },
  3927. serviceError: assert.AnError,
  3928. expectedError: assert.AnError,
  3929. },
  3930. }
  3931. for _, tt := range tests {
  3932. t.Run(tt.name, func(t *testing.T) {
  3933. input := wire.SNACMessage{
  3934. Frame: wire.SNACFrame{
  3935. FoodGroup: wire.OService,
  3936. SubGroup: wire.OServiceIdleNotification,
  3937. },
  3938. Body: tt.inputBody,
  3939. }
  3940. svc := newMockOServiceService(t)
  3941. svc.EXPECT().
  3942. IdleNotification(mock.Anything, mock.Anything, input.Body).
  3943. Return(tt.serviceError)
  3944. h := Handler{
  3945. OServiceService: svc,
  3946. RouteLogger: middleware.RouteLogger{
  3947. Logger: slog.Default(),
  3948. },
  3949. }
  3950. responseWriter := newMockResponseWriter(t)
  3951. buf := &bytes.Buffer{}
  3952. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3953. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  3954. if tt.expectedError != nil {
  3955. assert.ErrorIs(t, err, tt.expectedError)
  3956. } else {
  3957. assert.NoError(t, err)
  3958. }
  3959. })
  3960. }
  3961. }
  3962. func TestHandler_OServiceServiceClientVersions(t *testing.T) {
  3963. tests := []struct {
  3964. name string
  3965. inputBody wire.SNAC_0x01_0x17_OServiceClientVersions
  3966. responseError error
  3967. expectedError error
  3968. }{
  3969. {
  3970. name: "success",
  3971. inputBody: wire.SNAC_0x01_0x17_OServiceClientVersions{
  3972. Versions: []uint16{
  3973. 10,
  3974. },
  3975. },
  3976. },
  3977. {
  3978. name: "response writer error",
  3979. inputBody: wire.SNAC_0x01_0x17_OServiceClientVersions{
  3980. Versions: []uint16{
  3981. 10,
  3982. },
  3983. },
  3984. responseError: assert.AnError,
  3985. expectedError: assert.AnError,
  3986. },
  3987. }
  3988. for _, tt := range tests {
  3989. t.Run(tt.name, func(t *testing.T) {
  3990. input := wire.SNACMessage{
  3991. Frame: wire.SNACFrame{
  3992. FoodGroup: wire.OService,
  3993. SubGroup: wire.OServiceClientVersions,
  3994. },
  3995. Body: tt.inputBody,
  3996. }
  3997. output := []wire.SNACMessage{
  3998. {
  3999. Frame: wire.SNACFrame{
  4000. FoodGroup: wire.OService,
  4001. SubGroup: wire.OServiceHostVersions,
  4002. },
  4003. Body: wire.SNAC_0x01_0x18_OServiceHostVersions{
  4004. Versions: []uint16{
  4005. 10,
  4006. },
  4007. },
  4008. },
  4009. {
  4010. Frame: wire.SNACFrame{
  4011. FoodGroup: wire.OService,
  4012. SubGroup: wire.OServiceMotd,
  4013. RequestID: wire.ReqIDFromServer,
  4014. },
  4015. Body: wire.SNAC_0x01_0x13_OServiceMOTD{
  4016. MessageType: 0x0004,
  4017. TLVRestBlock: wire.TLVRestBlock{
  4018. TLVList: wire.TLVList{
  4019. wire.NewTLVBE(wire.OServiceTLVTagsMOTDMessage, "Welcome to Open OSCAR Server"),
  4020. },
  4021. },
  4022. },
  4023. },
  4024. }
  4025. instance := state.NewSession().AddInstance()
  4026. svc := newMockOServiceService(t)
  4027. svc.EXPECT().
  4028. ClientVersions(mock.Anything, instance, input.Frame, input.Body).
  4029. Return(output)
  4030. h := Handler{
  4031. OServiceService: svc,
  4032. RouteLogger: middleware.RouteLogger{
  4033. Logger: slog.Default(),
  4034. },
  4035. }
  4036. responseWriter := newMockResponseWriter(t)
  4037. if tt.responseError == nil {
  4038. for _, snac := range output {
  4039. responseWriter.EXPECT().
  4040. SendSNAC(snac.Frame, snac.Body).
  4041. Return(nil)
  4042. }
  4043. } else {
  4044. responseWriter.EXPECT().
  4045. SendSNAC(output[0].Frame, output[0].Body).
  4046. Return(tt.responseError)
  4047. }
  4048. buf := &bytes.Buffer{}
  4049. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4050. err := h.Handle(context.Background(), wire.BOS, instance, input.Frame, buf, responseWriter, config.Listener{})
  4051. if tt.expectedError != nil {
  4052. assert.ErrorIs(t, err, tt.expectedError)
  4053. } else {
  4054. assert.NoError(t, err)
  4055. }
  4056. })
  4057. }
  4058. }
  4059. func TestHandler_OServiceServiceRateParamsQuery(t *testing.T) {
  4060. tests := []struct {
  4061. name string
  4062. inputBody struct{}
  4063. responseError error
  4064. expectedError error
  4065. }{
  4066. {
  4067. name: "success",
  4068. inputBody: struct{}{},
  4069. },
  4070. {
  4071. name: "response writer error",
  4072. inputBody: struct{}{},
  4073. responseError: assert.AnError,
  4074. expectedError: assert.AnError,
  4075. },
  4076. }
  4077. for _, tt := range tests {
  4078. t.Run(tt.name, func(t *testing.T) {
  4079. input := wire.SNACMessage{
  4080. Frame: wire.SNACFrame{
  4081. FoodGroup: wire.OService,
  4082. SubGroup: wire.OServiceRateParamsQuery,
  4083. },
  4084. Body: tt.inputBody,
  4085. }
  4086. output := wire.SNACMessage{
  4087. Frame: wire.SNACFrame{
  4088. FoodGroup: wire.OService,
  4089. SubGroup: wire.OServiceRateParamsReply,
  4090. },
  4091. Body: wire.SNAC_0x01_0x07_OServiceRateParamsReply{
  4092. RateGroups: []struct {
  4093. ID uint16
  4094. Pairs []struct {
  4095. FoodGroup uint16
  4096. SubGroup uint16
  4097. } `oscar:"count_prefix=uint16"`
  4098. }{
  4099. {
  4100. ID: 1,
  4101. },
  4102. },
  4103. },
  4104. }
  4105. svc := newMockOServiceService(t)
  4106. svc.EXPECT().
  4107. RateParamsQuery(mock.Anything, mock.Anything, input.Frame).
  4108. Return(output)
  4109. h := Handler{
  4110. OServiceService: svc,
  4111. RouteLogger: middleware.RouteLogger{
  4112. Logger: slog.Default(),
  4113. },
  4114. }
  4115. responseWriter := newMockResponseWriter(t)
  4116. responseWriter.EXPECT().
  4117. SendSNAC(output.Frame, output.Body).
  4118. Return(tt.responseError)
  4119. buf := &bytes.Buffer{}
  4120. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4121. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4122. if tt.expectedError != nil {
  4123. assert.ErrorIs(t, err, tt.expectedError)
  4124. } else {
  4125. assert.NoError(t, err)
  4126. }
  4127. })
  4128. }
  4129. }
  4130. func TestHandler_OServiceServiceRateParamsSubAdd(t *testing.T) {
  4131. tests := []struct {
  4132. name string
  4133. inputBody wire.SNAC_0x01_0x08_OServiceRateParamsSubAdd
  4134. expectedError error
  4135. }{
  4136. {
  4137. name: "success",
  4138. inputBody: wire.SNAC_0x01_0x08_OServiceRateParamsSubAdd{
  4139. ClassIDs: []uint16{1, 2, 3, 4},
  4140. },
  4141. },
  4142. }
  4143. for _, tt := range tests {
  4144. t.Run(tt.name, func(t *testing.T) {
  4145. input := wire.SNACMessage{
  4146. Frame: wire.SNACFrame{
  4147. FoodGroup: wire.OService,
  4148. SubGroup: wire.OServiceRateParamsSubAdd,
  4149. },
  4150. Body: tt.inputBody,
  4151. }
  4152. instance := state.NewSession().AddInstance()
  4153. svc := newMockOServiceService(t)
  4154. svc.EXPECT().
  4155. RateParamsSubAdd(mock.Anything, instance, input.Body)
  4156. h := Handler{
  4157. OServiceService: svc,
  4158. RouteLogger: middleware.RouteLogger{
  4159. Logger: slog.Default(),
  4160. },
  4161. }
  4162. buf := &bytes.Buffer{}
  4163. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4164. err := h.Handle(context.Background(), wire.BOS, instance, input.Frame, buf, nil, config.Listener{})
  4165. if tt.expectedError != nil {
  4166. assert.ErrorIs(t, err, tt.expectedError)
  4167. } else {
  4168. assert.NoError(t, err)
  4169. }
  4170. })
  4171. }
  4172. }
  4173. func TestHandler_OServiceServiceSetUserInfoFields(t *testing.T) {
  4174. tests := []struct {
  4175. name string
  4176. inputBody wire.SNAC_0x01_0x1E_OServiceSetUserInfoFields
  4177. serviceError error
  4178. responseError error
  4179. expectedError error
  4180. }{
  4181. {
  4182. name: "success",
  4183. inputBody: wire.SNAC_0x01_0x1E_OServiceSetUserInfoFields{
  4184. TLVRestBlock: wire.TLVRestBlock{
  4185. TLVList: wire.TLVList{
  4186. wire.NewTLVBE(0x01, []byte{1, 2, 3, 4}),
  4187. },
  4188. },
  4189. },
  4190. },
  4191. {
  4192. name: "service error",
  4193. inputBody: wire.SNAC_0x01_0x1E_OServiceSetUserInfoFields{
  4194. TLVRestBlock: wire.TLVRestBlock{
  4195. TLVList: wire.TLVList{
  4196. wire.NewTLVBE(0x01, []byte{1, 2, 3, 4}),
  4197. },
  4198. },
  4199. },
  4200. serviceError: assert.AnError,
  4201. expectedError: assert.AnError,
  4202. },
  4203. {
  4204. name: "response writer error",
  4205. inputBody: wire.SNAC_0x01_0x1E_OServiceSetUserInfoFields{
  4206. TLVRestBlock: wire.TLVRestBlock{
  4207. TLVList: wire.TLVList{
  4208. wire.NewTLVBE(0x01, []byte{1, 2, 3, 4}),
  4209. },
  4210. },
  4211. },
  4212. responseError: assert.AnError,
  4213. expectedError: assert.AnError,
  4214. },
  4215. }
  4216. for _, tt := range tests {
  4217. t.Run(tt.name, func(t *testing.T) {
  4218. input := wire.SNACMessage{
  4219. Frame: wire.SNACFrame{
  4220. FoodGroup: wire.OService,
  4221. SubGroup: wire.OServiceSetUserInfoFields,
  4222. },
  4223. Body: tt.inputBody,
  4224. }
  4225. output := wire.SNACMessage{
  4226. Frame: wire.SNACFrame{
  4227. FoodGroup: wire.OService,
  4228. SubGroup: wire.OServiceUserInfoUpdate,
  4229. },
  4230. Body: wire.SNAC_0x01_0x0F_OServiceUserInfoUpdate{
  4231. UserInfo: []wire.TLVUserInfo{
  4232. {ScreenName: "screen-name"},
  4233. {ScreenName: "screen-name"},
  4234. },
  4235. },
  4236. }
  4237. svc := newMockOServiceService(t)
  4238. svc.EXPECT().
  4239. SetUserInfoFields(mock.Anything, mock.Anything, input.Frame, input.Body).
  4240. Return(output, tt.serviceError)
  4241. h := Handler{
  4242. OServiceService: svc,
  4243. RouteLogger: middleware.RouteLogger{
  4244. Logger: slog.Default(),
  4245. },
  4246. }
  4247. responseWriter := newMockResponseWriter(t)
  4248. if tt.serviceError == nil {
  4249. responseWriter.EXPECT().
  4250. SendSNAC(output.Frame, output.Body).
  4251. Return(tt.responseError)
  4252. }
  4253. buf := &bytes.Buffer{}
  4254. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4255. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4256. if tt.expectedError != nil {
  4257. assert.ErrorIs(t, err, tt.expectedError)
  4258. } else {
  4259. assert.NoError(t, err)
  4260. }
  4261. })
  4262. }
  4263. }
  4264. func TestHandler_OServiceServiceUserInfoQuery(t *testing.T) {
  4265. tests := []struct {
  4266. name string
  4267. inputBody struct{}
  4268. responseError error
  4269. expectedError error
  4270. }{
  4271. {
  4272. name: "success",
  4273. inputBody: struct{}{},
  4274. },
  4275. {
  4276. name: "response writer error",
  4277. inputBody: struct{}{},
  4278. responseError: assert.AnError,
  4279. expectedError: assert.AnError,
  4280. },
  4281. }
  4282. for _, tt := range tests {
  4283. t.Run(tt.name, func(t *testing.T) {
  4284. input := wire.SNACMessage{
  4285. Frame: wire.SNACFrame{
  4286. FoodGroup: wire.OService,
  4287. SubGroup: wire.OServiceUserInfoQuery,
  4288. },
  4289. Body: tt.inputBody,
  4290. }
  4291. output := wire.SNACMessage{
  4292. Frame: wire.SNACFrame{
  4293. FoodGroup: wire.OService,
  4294. SubGroup: wire.OServiceUserInfoUpdate,
  4295. },
  4296. Body: wire.SNAC_0x01_0x0F_OServiceUserInfoUpdate{
  4297. UserInfo: []wire.TLVUserInfo{
  4298. {ScreenName: "screen-name"},
  4299. {ScreenName: "screen-name"},
  4300. },
  4301. },
  4302. }
  4303. svc := newMockOServiceService(t)
  4304. svc.EXPECT().
  4305. UserInfoQuery(mock.Anything, mock.Anything, input.Frame).
  4306. Return(output)
  4307. h := Handler{
  4308. OServiceService: svc,
  4309. RouteLogger: middleware.RouteLogger{
  4310. Logger: slog.Default(),
  4311. },
  4312. }
  4313. responseWriter := newMockResponseWriter(t)
  4314. responseWriter.EXPECT().
  4315. SendSNAC(output.Frame, output.Body).
  4316. Return(tt.responseError)
  4317. buf := &bytes.Buffer{}
  4318. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4319. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4320. if tt.expectedError != nil {
  4321. assert.ErrorIs(t, err, tt.expectedError)
  4322. } else {
  4323. assert.NoError(t, err)
  4324. }
  4325. })
  4326. }
  4327. }
  4328. func TestHandler_OServiceServiceNoop(t *testing.T) {
  4329. tests := []struct {
  4330. name string
  4331. inputBody struct{}
  4332. expectedError error
  4333. }{
  4334. {
  4335. name: "success",
  4336. inputBody: struct{}{},
  4337. },
  4338. }
  4339. for _, tt := range tests {
  4340. t.Run(tt.name, func(t *testing.T) {
  4341. input := wire.SNACMessage{
  4342. Frame: wire.SNACFrame{
  4343. FoodGroup: wire.OService,
  4344. SubGroup: wire.OServiceNoop,
  4345. },
  4346. Body: tt.inputBody,
  4347. }
  4348. h := Handler{
  4349. RouteLogger: middleware.RouteLogger{
  4350. Logger: slog.Default(),
  4351. },
  4352. }
  4353. responseWriter := newMockResponseWriter(t)
  4354. buf := &bytes.Buffer{}
  4355. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4356. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4357. if tt.expectedError != nil {
  4358. assert.ErrorIs(t, err, tt.expectedError)
  4359. } else {
  4360. assert.NoError(t, err)
  4361. }
  4362. })
  4363. }
  4364. }
  4365. func TestHandler_OServiceServiceSetPrivacyFlags(t *testing.T) {
  4366. tests := []struct {
  4367. name string
  4368. inputBody wire.SNAC_0x01_0x14_OServiceSetPrivacyFlags
  4369. expectedError error
  4370. }{
  4371. {
  4372. name: "success",
  4373. inputBody: wire.SNAC_0x01_0x14_OServiceSetPrivacyFlags{
  4374. PrivacyFlags: wire.OServicePrivacyFlagMember,
  4375. },
  4376. },
  4377. }
  4378. for _, tt := range tests {
  4379. t.Run(tt.name, func(t *testing.T) {
  4380. input := wire.SNACMessage{
  4381. Frame: wire.SNACFrame{
  4382. FoodGroup: wire.OService,
  4383. SubGroup: wire.OServiceSetPrivacyFlags,
  4384. },
  4385. Body: tt.inputBody,
  4386. }
  4387. svc := newMockOServiceService(t)
  4388. svc.EXPECT().
  4389. SetPrivacyFlags(mock.Anything, input.Body)
  4390. h := Handler{
  4391. OServiceService: svc,
  4392. RouteLogger: middleware.RouteLogger{
  4393. Logger: slog.Default(),
  4394. },
  4395. }
  4396. responseWriter := newMockResponseWriter(t)
  4397. buf := &bytes.Buffer{}
  4398. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4399. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4400. if tt.expectedError != nil {
  4401. assert.ErrorIs(t, err, tt.expectedError)
  4402. } else {
  4403. assert.NoError(t, err)
  4404. }
  4405. })
  4406. }
  4407. }
  4408. func TestHandler_PermitDenyRightsQuery(t *testing.T) {
  4409. tests := []struct {
  4410. name string
  4411. inputBody struct{}
  4412. responseError error
  4413. expectedError error
  4414. }{
  4415. {
  4416. name: "success",
  4417. inputBody: struct{}{},
  4418. },
  4419. {
  4420. name: "response writer error",
  4421. inputBody: struct{}{},
  4422. responseError: assert.AnError,
  4423. expectedError: assert.AnError,
  4424. },
  4425. }
  4426. for _, tt := range tests {
  4427. t.Run(tt.name, func(t *testing.T) {
  4428. input := wire.SNACMessage{
  4429. Frame: wire.SNACFrame{
  4430. FoodGroup: wire.PermitDeny,
  4431. SubGroup: wire.PermitDenyRightsQuery,
  4432. },
  4433. Body: tt.inputBody,
  4434. }
  4435. output := wire.SNACMessage{
  4436. Frame: wire.SNACFrame{
  4437. FoodGroup: wire.PermitDeny,
  4438. SubGroup: wire.PermitDenyRightsReply,
  4439. },
  4440. Body: wire.SNAC_0x09_0x03_PermitDenyRightsReply{
  4441. TLVRestBlock: wire.TLVRestBlock{
  4442. TLVList: wire.TLVList{
  4443. wire.NewTLVBE(0x01, uint16(1000)),
  4444. },
  4445. },
  4446. },
  4447. }
  4448. svc := newMockPermitDenyService(t)
  4449. svc.EXPECT().
  4450. RightsQuery(mock.Anything, input.Frame).
  4451. Return(output)
  4452. h := Handler{
  4453. PermitDenyService: svc,
  4454. RouteLogger: middleware.RouteLogger{
  4455. Logger: slog.Default(),
  4456. },
  4457. }
  4458. responseWriter := newMockResponseWriter(t)
  4459. responseWriter.EXPECT().
  4460. SendSNAC(output.Frame, output.Body).
  4461. Return(tt.responseError)
  4462. buf := &bytes.Buffer{}
  4463. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4464. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4465. if tt.expectedError != nil {
  4466. assert.ErrorIs(t, err, tt.expectedError)
  4467. } else {
  4468. assert.NoError(t, err)
  4469. }
  4470. })
  4471. }
  4472. }
  4473. func TestHandler_PermitDenyAddDenyListEntries(t *testing.T) {
  4474. tests := []struct {
  4475. name string
  4476. inputBody wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries
  4477. serviceError error
  4478. expectedError error
  4479. }{
  4480. {
  4481. name: "success",
  4482. inputBody: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  4483. Users: []struct {
  4484. ScreenName string `oscar:"len_prefix=uint8"`
  4485. }{
  4486. {
  4487. ScreenName: "friend1",
  4488. },
  4489. {
  4490. ScreenName: "friend2",
  4491. },
  4492. },
  4493. },
  4494. },
  4495. {
  4496. name: "service error",
  4497. inputBody: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  4498. Users: []struct {
  4499. ScreenName string `oscar:"len_prefix=uint8"`
  4500. }{
  4501. {
  4502. ScreenName: "friend1",
  4503. },
  4504. {
  4505. ScreenName: "friend2",
  4506. },
  4507. },
  4508. },
  4509. serviceError: assert.AnError,
  4510. expectedError: assert.AnError,
  4511. },
  4512. }
  4513. for _, tt := range tests {
  4514. t.Run(tt.name, func(t *testing.T) {
  4515. instance := state.NewSession().AddInstance()
  4516. input := wire.SNACMessage{
  4517. Frame: wire.SNACFrame{
  4518. FoodGroup: wire.PermitDeny,
  4519. SubGroup: wire.PermitDenyAddDenyListEntries,
  4520. },
  4521. Body: tt.inputBody,
  4522. }
  4523. svc := newMockPermitDenyService(t)
  4524. svc.EXPECT().
  4525. AddDenyListEntries(mock.Anything, instance, input.Body).
  4526. Return(tt.serviceError)
  4527. h := Handler{
  4528. PermitDenyService: svc,
  4529. RouteLogger: middleware.RouteLogger{
  4530. Logger: slog.Default(),
  4531. },
  4532. }
  4533. buf := &bytes.Buffer{}
  4534. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4535. err := h.Handle(context.TODO(), wire.BOS, instance, input.Frame, buf, nil, config.Listener{})
  4536. if tt.expectedError != nil {
  4537. assert.ErrorIs(t, err, tt.expectedError)
  4538. } else {
  4539. assert.NoError(t, err)
  4540. }
  4541. })
  4542. }
  4543. }
  4544. func TestHandler_PermitDenyDelDenyListEntries(t *testing.T) {
  4545. tests := []struct {
  4546. name string
  4547. inputBody wire.SNAC_0x09_0x08_PermitDenyDelDenyListEntries
  4548. serviceError error
  4549. expectedError error
  4550. }{
  4551. {
  4552. name: "success",
  4553. inputBody: wire.SNAC_0x09_0x08_PermitDenyDelDenyListEntries{
  4554. Users: []struct {
  4555. ScreenName string `oscar:"len_prefix=uint8"`
  4556. }{
  4557. {
  4558. ScreenName: "friend1",
  4559. },
  4560. {
  4561. ScreenName: "friend2",
  4562. },
  4563. },
  4564. },
  4565. },
  4566. {
  4567. name: "service error",
  4568. inputBody: wire.SNAC_0x09_0x08_PermitDenyDelDenyListEntries{
  4569. Users: []struct {
  4570. ScreenName string `oscar:"len_prefix=uint8"`
  4571. }{
  4572. {
  4573. ScreenName: "friend1",
  4574. },
  4575. {
  4576. ScreenName: "friend2",
  4577. },
  4578. },
  4579. },
  4580. serviceError: assert.AnError,
  4581. expectedError: assert.AnError,
  4582. },
  4583. }
  4584. for _, tt := range tests {
  4585. t.Run(tt.name, func(t *testing.T) {
  4586. instance := state.NewSession().AddInstance()
  4587. input := wire.SNACMessage{
  4588. Frame: wire.SNACFrame{
  4589. FoodGroup: wire.PermitDeny,
  4590. SubGroup: wire.PermitDenyDelDenyListEntries,
  4591. },
  4592. Body: tt.inputBody,
  4593. }
  4594. svc := newMockPermitDenyService(t)
  4595. svc.EXPECT().
  4596. DelDenyListEntries(mock.Anything, instance, input.Body).
  4597. Return(tt.serviceError)
  4598. h := Handler{
  4599. PermitDenyService: svc,
  4600. RouteLogger: middleware.RouteLogger{
  4601. Logger: slog.Default(),
  4602. },
  4603. }
  4604. buf := &bytes.Buffer{}
  4605. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4606. err := h.Handle(context.TODO(), wire.BOS, instance, input.Frame, buf, nil, config.Listener{})
  4607. if tt.expectedError != nil {
  4608. assert.ErrorIs(t, err, tt.expectedError)
  4609. } else {
  4610. assert.NoError(t, err)
  4611. }
  4612. })
  4613. }
  4614. }
  4615. func TestHandler_PermitDenyAddPermListEntries(t *testing.T) {
  4616. tests := []struct {
  4617. name string
  4618. inputBody wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries
  4619. serviceError error
  4620. expectedError error
  4621. }{
  4622. {
  4623. name: "success",
  4624. inputBody: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  4625. Users: []struct {
  4626. ScreenName string `oscar:"len_prefix=uint8"`
  4627. }{
  4628. {
  4629. ScreenName: "friend1",
  4630. },
  4631. {
  4632. ScreenName: "friend2",
  4633. },
  4634. },
  4635. },
  4636. },
  4637. {
  4638. name: "service error",
  4639. inputBody: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  4640. Users: []struct {
  4641. ScreenName string `oscar:"len_prefix=uint8"`
  4642. }{
  4643. {
  4644. ScreenName: "friend1",
  4645. },
  4646. {
  4647. ScreenName: "friend2",
  4648. },
  4649. },
  4650. },
  4651. serviceError: assert.AnError,
  4652. expectedError: assert.AnError,
  4653. },
  4654. }
  4655. for _, tt := range tests {
  4656. t.Run(tt.name, func(t *testing.T) {
  4657. instance := state.NewSession().AddInstance()
  4658. input := wire.SNACMessage{
  4659. Frame: wire.SNACFrame{
  4660. FoodGroup: wire.PermitDeny,
  4661. SubGroup: wire.PermitDenyAddPermListEntries,
  4662. },
  4663. Body: tt.inputBody,
  4664. }
  4665. svc := newMockPermitDenyService(t)
  4666. svc.EXPECT().
  4667. AddPermListEntries(mock.Anything, instance, input.Body).
  4668. Return(tt.serviceError)
  4669. h := Handler{
  4670. PermitDenyService: svc,
  4671. RouteLogger: middleware.RouteLogger{
  4672. Logger: slog.Default(),
  4673. },
  4674. }
  4675. buf := &bytes.Buffer{}
  4676. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4677. err := h.Handle(context.TODO(), wire.BOS, instance, input.Frame, buf, nil, config.Listener{})
  4678. if tt.expectedError != nil {
  4679. assert.ErrorIs(t, err, tt.expectedError)
  4680. } else {
  4681. assert.NoError(t, err)
  4682. }
  4683. })
  4684. }
  4685. }
  4686. func TestHandler_PermitDenyDelPermListEntries(t *testing.T) {
  4687. tests := []struct {
  4688. name string
  4689. inputBody wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries
  4690. serviceError error
  4691. expectedError error
  4692. }{
  4693. {
  4694. name: "success",
  4695. inputBody: wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries{
  4696. Users: []struct {
  4697. ScreenName string `oscar:"len_prefix=uint8"`
  4698. }{
  4699. {
  4700. ScreenName: "friend1",
  4701. },
  4702. {
  4703. ScreenName: "friend2",
  4704. },
  4705. },
  4706. },
  4707. },
  4708. {
  4709. name: "service error",
  4710. inputBody: wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries{
  4711. Users: []struct {
  4712. ScreenName string `oscar:"len_prefix=uint8"`
  4713. }{
  4714. {
  4715. ScreenName: "friend1",
  4716. },
  4717. {
  4718. ScreenName: "friend2",
  4719. },
  4720. },
  4721. },
  4722. serviceError: assert.AnError,
  4723. expectedError: assert.AnError,
  4724. },
  4725. }
  4726. for _, tt := range tests {
  4727. t.Run(tt.name, func(t *testing.T) {
  4728. instance := state.NewSession().AddInstance()
  4729. input := wire.SNACMessage{
  4730. Frame: wire.SNACFrame{
  4731. FoodGroup: wire.PermitDeny,
  4732. SubGroup: wire.PermitDenyDelPermListEntries,
  4733. },
  4734. Body: tt.inputBody,
  4735. }
  4736. svc := newMockPermitDenyService(t)
  4737. svc.EXPECT().
  4738. DelPermListEntries(mock.Anything, instance, input.Body).
  4739. Return(tt.serviceError)
  4740. h := Handler{
  4741. PermitDenyService: svc,
  4742. RouteLogger: middleware.RouteLogger{
  4743. Logger: slog.Default(),
  4744. },
  4745. }
  4746. buf := &bytes.Buffer{}
  4747. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4748. err := h.Handle(context.TODO(), wire.BOS, instance, input.Frame, buf, nil, config.Listener{})
  4749. if tt.expectedError != nil {
  4750. assert.ErrorIs(t, err, tt.expectedError)
  4751. } else {
  4752. assert.NoError(t, err)
  4753. }
  4754. })
  4755. }
  4756. }
  4757. func TestHandler_PermitDenySetGroupPermitMask(t *testing.T) {
  4758. tests := []struct {
  4759. name string
  4760. inputBody wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries
  4761. expectedError error
  4762. }{
  4763. {
  4764. name: "success",
  4765. inputBody: wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries{
  4766. Users: []struct {
  4767. ScreenName string `oscar:"len_prefix=uint8"`
  4768. }{
  4769. {
  4770. ScreenName: "friend1",
  4771. },
  4772. },
  4773. },
  4774. },
  4775. }
  4776. for _, tt := range tests {
  4777. t.Run(tt.name, func(t *testing.T) {
  4778. instance := state.NewSession().AddInstance()
  4779. input := wire.SNACMessage{
  4780. Frame: wire.SNACFrame{
  4781. FoodGroup: wire.PermitDeny,
  4782. SubGroup: wire.PermitDenySetGroupPermitMask,
  4783. },
  4784. Body: tt.inputBody,
  4785. }
  4786. svc := newMockPermitDenyService(t)
  4787. h := Handler{
  4788. PermitDenyService: svc,
  4789. RouteLogger: middleware.RouteLogger{
  4790. Logger: slog.Default(),
  4791. },
  4792. }
  4793. buf := &bytes.Buffer{}
  4794. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4795. err := h.Handle(context.TODO(), wire.BOS, instance, input.Frame, buf, nil, config.Listener{})
  4796. if tt.expectedError != nil {
  4797. assert.ErrorIs(t, err, tt.expectedError)
  4798. } else {
  4799. assert.NoError(t, err)
  4800. }
  4801. })
  4802. }
  4803. }
  4804. func TestUserLookupHandler_FindByEmail(t *testing.T) {
  4805. tests := []struct {
  4806. name string
  4807. inputBody wire.SNAC_0x0A_0x02_UserLookupFindByEmail
  4808. serviceError error
  4809. responseError error
  4810. expectedError error
  4811. }{
  4812. {
  4813. name: "success",
  4814. inputBody: wire.SNAC_0x0A_0x02_UserLookupFindByEmail{
  4815. Email: []byte("haha@aol.com"),
  4816. },
  4817. },
  4818. {
  4819. name: "service error",
  4820. inputBody: wire.SNAC_0x0A_0x02_UserLookupFindByEmail{
  4821. Email: []byte("haha@aol.com"),
  4822. },
  4823. serviceError: assert.AnError,
  4824. expectedError: assert.AnError,
  4825. },
  4826. {
  4827. name: "response writer error",
  4828. inputBody: wire.SNAC_0x0A_0x02_UserLookupFindByEmail{
  4829. Email: []byte("haha@aol.com"),
  4830. },
  4831. responseError: assert.AnError,
  4832. expectedError: assert.AnError,
  4833. },
  4834. }
  4835. for _, tt := range tests {
  4836. t.Run(tt.name, func(t *testing.T) {
  4837. input := wire.SNACMessage{
  4838. Frame: wire.SNACFrame{
  4839. FoodGroup: wire.UserLookup,
  4840. SubGroup: wire.UserLookupFindByEmail,
  4841. },
  4842. Body: tt.inputBody,
  4843. }
  4844. output := wire.SNACMessage{
  4845. Frame: wire.SNACFrame{
  4846. FoodGroup: wire.UserLookup,
  4847. SubGroup: wire.UserLookupFindReply,
  4848. },
  4849. Body: wire.SNAC_0x0A_0x03_UserLookupFindReply{
  4850. TLVRestBlock: wire.TLVRestBlock{
  4851. TLVList: wire.TLVList{
  4852. wire.NewTLVBE(0x01, uint16(0x02)),
  4853. },
  4854. },
  4855. },
  4856. }
  4857. svc := newMockUserLookupService(t)
  4858. svc.EXPECT().
  4859. FindByEmail(mock.Anything, input.Frame, input.Body).
  4860. Return(output, tt.serviceError)
  4861. h := Handler{
  4862. UserLookupService: svc,
  4863. RouteLogger: middleware.RouteLogger{
  4864. Logger: slog.Default(),
  4865. },
  4866. }
  4867. ss := newMockResponseWriter(t)
  4868. if tt.serviceError == nil {
  4869. ss.EXPECT().
  4870. SendSNAC(output.Frame, output.Body).
  4871. Return(tt.responseError)
  4872. }
  4873. buf := &bytes.Buffer{}
  4874. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4875. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, ss, config.Listener{})
  4876. if tt.expectedError != nil {
  4877. assert.ErrorIs(t, err, tt.expectedError)
  4878. } else {
  4879. assert.NoError(t, err)
  4880. }
  4881. })
  4882. }
  4883. }
  4884. func TestHandler_LocateGetDirInfo(t *testing.T) {
  4885. tests := []struct {
  4886. name string
  4887. inputBody wire.SNAC_0x02_0x0B_LocateGetDirInfo
  4888. serviceError error
  4889. responseError error
  4890. expectedError error
  4891. }{
  4892. {
  4893. name: "success",
  4894. inputBody: wire.SNAC_0x02_0x0B_LocateGetDirInfo{
  4895. ScreenName: "screen-name",
  4896. },
  4897. },
  4898. {
  4899. name: "service error",
  4900. inputBody: wire.SNAC_0x02_0x0B_LocateGetDirInfo{
  4901. ScreenName: "screen-name",
  4902. },
  4903. serviceError: assert.AnError,
  4904. expectedError: assert.AnError,
  4905. },
  4906. {
  4907. name: "response writer error",
  4908. inputBody: wire.SNAC_0x02_0x0B_LocateGetDirInfo{
  4909. ScreenName: "screen-name",
  4910. },
  4911. responseError: assert.AnError,
  4912. expectedError: assert.AnError,
  4913. },
  4914. }
  4915. for _, tt := range tests {
  4916. t.Run(tt.name, func(t *testing.T) {
  4917. input := wire.SNACMessage{
  4918. Frame: wire.SNACFrame{
  4919. FoodGroup: wire.Locate,
  4920. SubGroup: wire.LocateGetDirInfo,
  4921. },
  4922. Body: tt.inputBody,
  4923. }
  4924. output := wire.SNACMessage{
  4925. Frame: wire.SNACFrame{
  4926. FoodGroup: wire.Locate,
  4927. SubGroup: wire.LocateGetDirReply,
  4928. },
  4929. Body: wire.SNAC_0x02_0x0C_LocateGetDirReply{
  4930. Status: 1,
  4931. },
  4932. }
  4933. svc := newMockLocateService(t)
  4934. svc.EXPECT().
  4935. DirInfo(mock.Anything, input.Frame, input.Body).
  4936. Return(output, tt.serviceError)
  4937. h := Handler{
  4938. LocateService: svc,
  4939. RouteLogger: middleware.RouteLogger{
  4940. Logger: slog.Default(),
  4941. },
  4942. }
  4943. responseWriter := newMockResponseWriter(t)
  4944. if tt.serviceError == nil {
  4945. responseWriter.EXPECT().
  4946. SendSNAC(output.Frame, output.Body).
  4947. Return(tt.responseError)
  4948. }
  4949. buf := &bytes.Buffer{}
  4950. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4951. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4952. if tt.expectedError != nil {
  4953. assert.ErrorIs(t, err, tt.expectedError)
  4954. } else {
  4955. assert.NoError(t, err)
  4956. }
  4957. })
  4958. }
  4959. }
  4960. func TestHandler_LocateRightsQuery(t *testing.T) {
  4961. tests := []struct {
  4962. name string
  4963. inputBody struct{}
  4964. responseError error
  4965. expectedError error
  4966. }{
  4967. {
  4968. name: "success",
  4969. inputBody: struct{}{},
  4970. },
  4971. {
  4972. name: "response writer error",
  4973. inputBody: struct{}{},
  4974. responseError: assert.AnError,
  4975. expectedError: assert.AnError,
  4976. },
  4977. }
  4978. for _, tt := range tests {
  4979. t.Run(tt.name, func(t *testing.T) {
  4980. input := wire.SNACMessage{
  4981. Frame: wire.SNACFrame{
  4982. FoodGroup: wire.Locate,
  4983. SubGroup: wire.LocateRightsQuery,
  4984. },
  4985. Body: tt.inputBody,
  4986. }
  4987. output := wire.SNACMessage{
  4988. Frame: wire.SNACFrame{
  4989. FoodGroup: wire.Locate,
  4990. SubGroup: wire.LocateRightsReply,
  4991. },
  4992. Body: wire.SNAC_0x02_0x03_LocateRightsReply{
  4993. TLVRestBlock: wire.TLVRestBlock{
  4994. TLVList: wire.TLVList{
  4995. wire.NewTLVBE(0x01, uint16(1000)),
  4996. },
  4997. },
  4998. },
  4999. }
  5000. svc := newMockLocateService(t)
  5001. svc.EXPECT().
  5002. RightsQuery(mock.Anything, input.Frame).
  5003. Return(output)
  5004. h := Handler{
  5005. LocateService: svc,
  5006. RouteLogger: middleware.RouteLogger{
  5007. Logger: slog.Default(),
  5008. },
  5009. }
  5010. responseWriter := newMockResponseWriter(t)
  5011. responseWriter.EXPECT().
  5012. SendSNAC(output.Frame, output.Body).
  5013. Return(tt.responseError)
  5014. buf := &bytes.Buffer{}
  5015. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  5016. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  5017. if tt.expectedError != nil {
  5018. assert.ErrorIs(t, err, tt.expectedError)
  5019. } else {
  5020. assert.NoError(t, err)
  5021. }
  5022. })
  5023. }
  5024. }
  5025. func TestHandler_LocateSetDirInfo(t *testing.T) {
  5026. tests := []struct {
  5027. name string
  5028. inputBody wire.SNAC_0x02_0x09_LocateSetDirInfo
  5029. serviceError error
  5030. responseError error
  5031. expectedError error
  5032. }{
  5033. {
  5034. name: "success",
  5035. inputBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  5036. TLVRestBlock: wire.TLVRestBlock{
  5037. TLVList: wire.TLVList{
  5038. {
  5039. Tag: 0x01,
  5040. Value: []byte{1, 2, 3, 4},
  5041. },
  5042. },
  5043. },
  5044. },
  5045. },
  5046. {
  5047. name: "service error",
  5048. inputBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  5049. TLVRestBlock: wire.TLVRestBlock{
  5050. TLVList: wire.TLVList{
  5051. {
  5052. Tag: 0x01,
  5053. Value: []byte{1, 2, 3, 4},
  5054. },
  5055. },
  5056. },
  5057. },
  5058. serviceError: assert.AnError,
  5059. expectedError: assert.AnError,
  5060. },
  5061. {
  5062. name: "response writer error",
  5063. inputBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  5064. TLVRestBlock: wire.TLVRestBlock{
  5065. TLVList: wire.TLVList{
  5066. {
  5067. Tag: 0x01,
  5068. Value: []byte{1, 2, 3, 4},
  5069. },
  5070. },
  5071. },
  5072. },
  5073. responseError: assert.AnError,
  5074. expectedError: assert.AnError,
  5075. },
  5076. }
  5077. for _, tt := range tests {
  5078. t.Run(tt.name, func(t *testing.T) {
  5079. input := wire.SNACMessage{
  5080. Frame: wire.SNACFrame{
  5081. FoodGroup: wire.Locate,
  5082. SubGroup: wire.LocateSetDirInfo,
  5083. },
  5084. Body: tt.inputBody,
  5085. }
  5086. output := wire.SNACMessage{
  5087. Frame: wire.SNACFrame{
  5088. FoodGroup: wire.Locate,
  5089. SubGroup: wire.LocateSetDirReply,
  5090. },
  5091. Body: wire.SNAC_0x02_0x0A_LocateSetDirReply{
  5092. Result: 1,
  5093. },
  5094. }
  5095. svc := newMockLocateService(t)
  5096. svc.EXPECT().
  5097. SetDirInfo(mock.Anything, mock.Anything, input.Frame, input.Body).
  5098. Return(output, tt.serviceError)
  5099. h := Handler{
  5100. LocateService: svc,
  5101. RouteLogger: middleware.RouteLogger{
  5102. Logger: slog.Default(),
  5103. },
  5104. }
  5105. responseWriter := newMockResponseWriter(t)
  5106. if tt.serviceError == nil {
  5107. responseWriter.EXPECT().
  5108. SendSNAC(output.Frame, output.Body).
  5109. Return(tt.responseError)
  5110. }
  5111. buf := &bytes.Buffer{}
  5112. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  5113. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  5114. if tt.expectedError != nil {
  5115. assert.ErrorIs(t, err, tt.expectedError)
  5116. } else {
  5117. assert.NoError(t, err)
  5118. }
  5119. })
  5120. }
  5121. }
  5122. func TestHandler_LocateSetInfo(t *testing.T) {
  5123. input := wire.SNACMessage{
  5124. Frame: wire.SNACFrame{
  5125. FoodGroup: wire.Locate,
  5126. SubGroup: wire.LocateSetInfo,
  5127. },
  5128. Body: wire.SNAC_0x02_0x04_LocateSetInfo{
  5129. TLVRestBlock: wire.TLVRestBlock{
  5130. TLVList: wire.TLVList{
  5131. {
  5132. Tag: 0x01,
  5133. Value: []byte{1, 2, 3, 4},
  5134. },
  5135. },
  5136. },
  5137. },
  5138. }
  5139. svc := newMockLocateService(t)
  5140. svc.EXPECT().
  5141. SetInfo(mock.Anything, mock.Anything, input.Body).
  5142. Return(nil)
  5143. h := Handler{
  5144. LocateService: svc,
  5145. RouteLogger: middleware.RouteLogger{
  5146. Logger: slog.Default(),
  5147. },
  5148. }
  5149. responseWriter := newMockResponseWriter(t)
  5150. buf := &bytes.Buffer{}
  5151. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  5152. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  5153. }
  5154. func TestHandler_LocateSetKeywordInfo(t *testing.T) {
  5155. input := wire.SNACMessage{
  5156. Frame: wire.SNACFrame{
  5157. FoodGroup: wire.Locate,
  5158. SubGroup: wire.LocateSetKeywordInfo,
  5159. },
  5160. Body: wire.SNAC_0x02_0x0F_LocateSetKeywordInfo{
  5161. TLVRestBlock: wire.TLVRestBlock{
  5162. TLVList: wire.TLVList{
  5163. {
  5164. Tag: 0x01,
  5165. Value: []byte{1, 2, 3, 4},
  5166. },
  5167. },
  5168. },
  5169. },
  5170. }
  5171. output := wire.SNACMessage{
  5172. Frame: wire.SNACFrame{
  5173. FoodGroup: wire.Locate,
  5174. SubGroup: wire.LocateSetKeywordReply,
  5175. },
  5176. Body: wire.SNAC_0x02_0x10_LocateSetKeywordReply{
  5177. Unknown: 1,
  5178. },
  5179. }
  5180. svc := newMockLocateService(t)
  5181. svc.EXPECT().
  5182. SetKeywordInfo(mock.Anything, mock.Anything, input.Frame, input.Body).
  5183. Return(output, nil)
  5184. h := Handler{
  5185. LocateService: svc,
  5186. RouteLogger: middleware.RouteLogger{
  5187. Logger: slog.Default(),
  5188. },
  5189. }
  5190. responseWriter := newMockResponseWriter(t)
  5191. responseWriter.EXPECT().
  5192. SendSNAC(output.Frame, output.Body).
  5193. Return(nil)
  5194. buf := &bytes.Buffer{}
  5195. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  5196. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  5197. }
  5198. func TestHandler_LocateUserInfoQuery(t *testing.T) {
  5199. input := wire.SNACMessage{
  5200. Frame: wire.SNACFrame{
  5201. FoodGroup: wire.Locate,
  5202. SubGroup: wire.LocateUserInfoQuery,
  5203. },
  5204. Body: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  5205. Type: 1,
  5206. },
  5207. }
  5208. output := wire.SNACMessage{
  5209. Frame: wire.SNACFrame{
  5210. FoodGroup: wire.Locate,
  5211. SubGroup: wire.LocateUserInfoReply,
  5212. },
  5213. Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  5214. TLVUserInfo: wire.TLVUserInfo{
  5215. ScreenName: "screen-name",
  5216. },
  5217. LocateInfo: wire.TLVRestBlock{
  5218. TLVList: wire.TLVList{
  5219. {
  5220. Tag: 0x01,
  5221. Value: []byte{1, 2, 3, 4},
  5222. },
  5223. },
  5224. },
  5225. },
  5226. }
  5227. svc := newMockLocateService(t)
  5228. svc.EXPECT().
  5229. UserInfoQuery(mock.Anything, mock.Anything, input.Frame, input.Body).
  5230. Return(output, nil)
  5231. h := Handler{
  5232. LocateService: svc,
  5233. RouteLogger: middleware.RouteLogger{
  5234. Logger: slog.Default(),
  5235. },
  5236. }
  5237. responseWriter := newMockResponseWriter(t)
  5238. responseWriter.EXPECT().
  5239. SendSNAC(output.Frame, output.Body).
  5240. Return(nil)
  5241. buf := &bytes.Buffer{}
  5242. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  5243. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  5244. }
  5245. func TestHandler_LocateUserInfoQuery2(t *testing.T) {
  5246. input := wire.SNACMessage{
  5247. Frame: wire.SNACFrame{
  5248. FoodGroup: wire.Locate,
  5249. SubGroup: wire.LocateUserInfoQuery2,
  5250. },
  5251. Body: wire.SNAC_0x02_0x15_LocateUserInfoQuery2{
  5252. Type2: 1,
  5253. },
  5254. }
  5255. output := wire.SNACMessage{
  5256. Frame: wire.SNACFrame{
  5257. FoodGroup: wire.Locate,
  5258. SubGroup: wire.LocateUserInfoReply,
  5259. },
  5260. Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  5261. TLVUserInfo: wire.TLVUserInfo{
  5262. ScreenName: "screen-name",
  5263. },
  5264. LocateInfo: wire.TLVRestBlock{
  5265. TLVList: wire.TLVList{
  5266. {
  5267. Tag: 0x01,
  5268. Value: []byte{1, 2, 3, 4},
  5269. },
  5270. },
  5271. },
  5272. },
  5273. }
  5274. svc := newMockLocateService(t)
  5275. svc.EXPECT().
  5276. UserInfoQuery(mock.Anything, mock.Anything, input.Frame, wire.SNAC_0x02_0x05_LocateUserInfoQuery{Type: 1}).
  5277. Return(output, nil)
  5278. h := Handler{
  5279. LocateService: svc,
  5280. RouteLogger: middleware.RouteLogger{
  5281. Logger: slog.Default(),
  5282. },
  5283. }
  5284. responseWriter := newMockResponseWriter(t)
  5285. responseWriter.EXPECT().
  5286. SendSNAC(output.Frame, output.Body).
  5287. Return(nil)
  5288. buf := &bytes.Buffer{}
  5289. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  5290. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  5291. }
  5292. func TestHandler_StatsReportEvents(t *testing.T) {
  5293. input := wire.SNACMessage{
  5294. Frame: wire.SNACFrame{
  5295. FoodGroup: wire.Stats,
  5296. SubGroup: wire.StatsReportEvents,
  5297. },
  5298. Body: wire.SNAC_0x0B_0x03_StatsReportEvents{},
  5299. }
  5300. output := wire.SNACMessage{
  5301. Frame: wire.SNACFrame{
  5302. FoodGroup: wire.Stats,
  5303. SubGroup: wire.StatsReportAck,
  5304. },
  5305. Body: wire.SNAC_0x0B_0x04_StatsReportAck{},
  5306. }
  5307. svc := newMockStatsService(t)
  5308. svc.EXPECT().
  5309. ReportEvents(mock.Anything, input.Frame, input.Body).
  5310. Return(output)
  5311. h := Handler{
  5312. StatsService: svc,
  5313. RouteLogger: middleware.RouteLogger{
  5314. Logger: slog.Default(),
  5315. },
  5316. }
  5317. ss := newMockResponseWriter(t)
  5318. ss.EXPECT().
  5319. SendSNAC(output.Frame, output.Body).
  5320. Return(nil)
  5321. buf := &bytes.Buffer{}
  5322. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  5323. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, ss, config.Listener{}))
  5324. }
  5325. func TestHandler_RouteNotFound(t *testing.T) {
  5326. input := wire.SNACMessage{
  5327. Frame: wire.SNACFrame{
  5328. FoodGroup: wire.Stats,
  5329. SubGroup: math.MaxUint16,
  5330. },
  5331. Body: wire.SNAC_0x0B_0x03_StatsReportEvents{},
  5332. }
  5333. h := Handler{
  5334. RouteLogger: middleware.RouteLogger{
  5335. Logger: slog.Default(),
  5336. },
  5337. }
  5338. assert.ErrorIs(t, ErrRouteNotFound, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, nil, nil, config.Listener{}))
  5339. }