handler_test.go 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412
  1. package oscar
  2. import (
  3. "bytes"
  4. "context"
  5. "log/slog"
  6. "math"
  7. "testing"
  8. "github.com/mk6i/retro-aim-server/config"
  9. "github.com/mk6i/retro-aim-server/server/oscar/middleware"
  10. "github.com/mk6i/retro-aim-server/state"
  11. "github.com/mk6i/retro-aim-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_ChatNavCreateRoom(t *testing.T) {
  779. tests := []struct {
  780. name string
  781. inputBody wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate
  782. serviceError error
  783. responseError error
  784. expectedError error
  785. }{
  786. {
  787. name: "success",
  788. inputBody: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  789. Exchange: 1,
  790. },
  791. },
  792. {
  793. name: "service error",
  794. inputBody: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  795. Exchange: 1,
  796. },
  797. serviceError: assert.AnError,
  798. expectedError: assert.AnError,
  799. },
  800. {
  801. name: "response writer error",
  802. inputBody: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  803. Exchange: 1,
  804. },
  805. responseError: assert.AnError,
  806. expectedError: assert.AnError,
  807. },
  808. }
  809. for _, tt := range tests {
  810. t.Run(tt.name, func(t *testing.T) {
  811. input := wire.SNACMessage{
  812. Frame: wire.SNACFrame{
  813. FoodGroup: wire.ChatNav,
  814. SubGroup: wire.ChatNavCreateRoom,
  815. },
  816. Body: tt.inputBody,
  817. }
  818. output := wire.SNACMessage{
  819. Frame: wire.SNACFrame{
  820. FoodGroup: wire.ChatNav,
  821. SubGroup: wire.ChatNavNavInfo,
  822. },
  823. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  824. }
  825. sess := state.NewSession()
  826. svc := newMockChatNavService(t)
  827. svc.EXPECT().
  828. CreateRoom(mock.Anything, sess, input.Frame, input.Body).
  829. Return(output, tt.serviceError)
  830. h := Handler{
  831. ChatNavService: svc,
  832. RouteLogger: middleware.RouteLogger{
  833. Logger: slog.Default(),
  834. },
  835. }
  836. responseWriter := newMockResponseWriter(t)
  837. if tt.serviceError == nil {
  838. responseWriter.EXPECT().
  839. SendSNAC(output.Frame, output.Body).
  840. Return(tt.responseError)
  841. }
  842. buf := &bytes.Buffer{}
  843. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  844. err := h.Handle(context.TODO(), wire.BOS, sess, input.Frame, buf, responseWriter, config.Listener{})
  845. if tt.expectedError != nil {
  846. assert.ErrorIs(t, err, tt.expectedError)
  847. } else {
  848. assert.NoError(t, err)
  849. }
  850. })
  851. }
  852. }
  853. func TestHandler_ChatNavCreateRoom_ReadErr(t *testing.T) {
  854. input := wire.SNACMessage{
  855. Frame: wire.SNACFrame{
  856. FoodGroup: wire.ChatNav,
  857. SubGroup: wire.ChatNavCreateRoom,
  858. },
  859. Body: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  860. Exchange: 1,
  861. },
  862. }
  863. output := wire.SNACMessage{
  864. Frame: wire.SNACFrame{
  865. FoodGroup: wire.ChatNav,
  866. SubGroup: wire.ChatNavNavInfo,
  867. },
  868. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  869. }
  870. sess := state.NewSession()
  871. svc := newMockChatNavService(t)
  872. svc.EXPECT().
  873. CreateRoom(mock.Anything, sess, input.Frame, input.Body).
  874. Return(output, nil)
  875. h := Handler{
  876. ChatNavService: svc,
  877. RouteLogger: middleware.RouteLogger{
  878. Logger: slog.Default(),
  879. },
  880. }
  881. ss := newMockResponseWriter(t)
  882. ss.EXPECT().
  883. SendSNAC(output.Frame, output.Body).
  884. Return(nil)
  885. buf := &bytes.Buffer{}
  886. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  887. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, sess, input.Frame, buf, ss, config.Listener{}))
  888. }
  889. func TestHandler_ChatNavRequestChatRights(t *testing.T) {
  890. tests := []struct {
  891. name string
  892. inputBody struct{}
  893. responseError error
  894. expectedError error
  895. }{
  896. {
  897. name: "success",
  898. inputBody: struct{}{},
  899. },
  900. {
  901. name: "response writer error",
  902. inputBody: struct{}{},
  903. responseError: assert.AnError,
  904. expectedError: assert.AnError,
  905. },
  906. }
  907. for _, tt := range tests {
  908. t.Run(tt.name, func(t *testing.T) {
  909. input := wire.SNACMessage{
  910. Frame: wire.SNACFrame{
  911. FoodGroup: wire.ChatNav,
  912. SubGroup: wire.ChatNavRequestChatRights,
  913. },
  914. Body: tt.inputBody,
  915. }
  916. output := wire.SNACMessage{
  917. Frame: wire.SNACFrame{
  918. FoodGroup: wire.ChatNav,
  919. SubGroup: wire.ChatNavNavInfo,
  920. },
  921. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  922. }
  923. svc := newMockChatNavService(t)
  924. svc.EXPECT().
  925. RequestChatRights(mock.Anything, input.Frame).
  926. Return(output)
  927. h := Handler{
  928. ChatNavService: svc,
  929. RouteLogger: middleware.RouteLogger{
  930. Logger: slog.Default(),
  931. },
  932. }
  933. responseWriter := newMockResponseWriter(t)
  934. responseWriter.EXPECT().
  935. SendSNAC(output.Frame, output.Body).
  936. Return(tt.responseError)
  937. buf := &bytes.Buffer{}
  938. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  939. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  940. if tt.expectedError != nil {
  941. assert.ErrorIs(t, err, tt.expectedError)
  942. } else {
  943. assert.NoError(t, err)
  944. }
  945. })
  946. }
  947. }
  948. func TestHandler_ChatNavRequestRoomInfo(t *testing.T) {
  949. tests := []struct {
  950. name string
  951. inputBody wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo
  952. serviceError error
  953. responseError error
  954. expectedError error
  955. }{
  956. {
  957. name: "success",
  958. inputBody: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  959. Exchange: 1,
  960. },
  961. },
  962. {
  963. name: "service error",
  964. inputBody: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  965. Exchange: 1,
  966. },
  967. serviceError: assert.AnError,
  968. expectedError: assert.AnError,
  969. },
  970. {
  971. name: "response writer error",
  972. inputBody: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  973. Exchange: 1,
  974. },
  975. responseError: assert.AnError,
  976. expectedError: assert.AnError,
  977. },
  978. }
  979. for _, tt := range tests {
  980. t.Run(tt.name, func(t *testing.T) {
  981. input := wire.SNACMessage{
  982. Frame: wire.SNACFrame{
  983. FoodGroup: wire.ChatNav,
  984. SubGroup: wire.ChatNavRequestRoomInfo,
  985. },
  986. Body: tt.inputBody,
  987. }
  988. output := wire.SNACMessage{
  989. Frame: wire.SNACFrame{
  990. FoodGroup: wire.ChatNav,
  991. SubGroup: wire.ChatNavNavInfo,
  992. },
  993. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  994. }
  995. svc := newMockChatNavService(t)
  996. svc.EXPECT().
  997. RequestRoomInfo(mock.Anything, input.Frame, input.Body).
  998. Return(output, tt.serviceError)
  999. h := Handler{
  1000. ChatNavService: svc,
  1001. RouteLogger: middleware.RouteLogger{
  1002. Logger: slog.Default(),
  1003. },
  1004. }
  1005. responseWriter := newMockResponseWriter(t)
  1006. if tt.serviceError == nil {
  1007. responseWriter.EXPECT().
  1008. SendSNAC(output.Frame, output.Body).
  1009. Return(tt.responseError)
  1010. }
  1011. buf := &bytes.Buffer{}
  1012. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1013. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1014. if tt.expectedError != nil {
  1015. assert.ErrorIs(t, err, tt.expectedError)
  1016. } else {
  1017. assert.NoError(t, err)
  1018. }
  1019. })
  1020. }
  1021. }
  1022. func TestHandler_ChatNavRequestExchangeInfo(t *testing.T) {
  1023. tests := []struct {
  1024. name string
  1025. inputBody wire.SNAC_0x0D_0x03_ChatNavRequestExchangeInfo
  1026. serviceError error
  1027. responseError error
  1028. expectedError error
  1029. }{
  1030. {
  1031. name: "success",
  1032. inputBody: wire.SNAC_0x0D_0x03_ChatNavRequestExchangeInfo{
  1033. Exchange: 4,
  1034. },
  1035. },
  1036. {
  1037. name: "service error",
  1038. inputBody: wire.SNAC_0x0D_0x03_ChatNavRequestExchangeInfo{
  1039. Exchange: 4,
  1040. },
  1041. serviceError: assert.AnError,
  1042. expectedError: assert.AnError,
  1043. },
  1044. {
  1045. name: "response writer error",
  1046. inputBody: wire.SNAC_0x0D_0x03_ChatNavRequestExchangeInfo{
  1047. Exchange: 4,
  1048. },
  1049. responseError: assert.AnError,
  1050. expectedError: assert.AnError,
  1051. },
  1052. }
  1053. for _, tt := range tests {
  1054. t.Run(tt.name, func(t *testing.T) {
  1055. input := wire.SNACMessage{
  1056. Frame: wire.SNACFrame{
  1057. FoodGroup: wire.ChatNav,
  1058. SubGroup: wire.ChatNavRequestExchangeInfo,
  1059. },
  1060. Body: tt.inputBody,
  1061. }
  1062. output := wire.SNACMessage{
  1063. Frame: wire.SNACFrame{
  1064. FoodGroup: wire.ChatNav,
  1065. SubGroup: wire.ChatNavNavInfo,
  1066. },
  1067. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  1068. }
  1069. svc := newMockChatNavService(t)
  1070. svc.EXPECT().
  1071. ExchangeInfo(mock.Anything, input.Frame, input.Body).
  1072. Return(output, tt.serviceError)
  1073. h := Handler{
  1074. ChatNavService: svc,
  1075. RouteLogger: middleware.RouteLogger{
  1076. Logger: slog.Default(),
  1077. },
  1078. }
  1079. responseWriter := newMockResponseWriter(t)
  1080. if tt.serviceError == nil {
  1081. responseWriter.EXPECT().
  1082. SendSNAC(output.Frame, output.Body).
  1083. Return(tt.responseError)
  1084. }
  1085. buf := &bytes.Buffer{}
  1086. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1087. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1088. if tt.expectedError != nil {
  1089. assert.ErrorIs(t, err, tt.expectedError)
  1090. } else {
  1091. assert.NoError(t, err)
  1092. }
  1093. })
  1094. }
  1095. }
  1096. func TestHandler_ChatChannelMsgToHost(t *testing.T) {
  1097. tests := []struct {
  1098. name string
  1099. inputBody wire.SNAC_0x0E_0x05_ChatChannelMsgToHost
  1100. serviceResponse *wire.SNACMessage
  1101. serviceError error
  1102. responseError error
  1103. expectedError error
  1104. }{
  1105. {
  1106. name: "success with reflected response",
  1107. inputBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1108. Channel: 4,
  1109. },
  1110. serviceResponse: &wire.SNACMessage{
  1111. Frame: wire.SNACFrame{
  1112. FoodGroup: wire.Chat,
  1113. SubGroup: wire.ChatChannelMsgToClient,
  1114. },
  1115. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  1116. Channel: 4,
  1117. },
  1118. },
  1119. },
  1120. {
  1121. name: "service error with reflected response",
  1122. inputBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1123. Channel: 4,
  1124. },
  1125. serviceResponse: &wire.SNACMessage{
  1126. Frame: wire.SNACFrame{
  1127. FoodGroup: wire.Chat,
  1128. SubGroup: wire.ChatChannelMsgToClient,
  1129. },
  1130. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  1131. Channel: 4,
  1132. },
  1133. },
  1134. serviceError: assert.AnError,
  1135. expectedError: assert.AnError,
  1136. },
  1137. {
  1138. name: "response writer error with reflected response",
  1139. inputBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1140. Channel: 4,
  1141. },
  1142. serviceResponse: &wire.SNACMessage{
  1143. Frame: wire.SNACFrame{
  1144. FoodGroup: wire.Chat,
  1145. SubGroup: wire.ChatChannelMsgToClient,
  1146. },
  1147. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  1148. Channel: 4,
  1149. },
  1150. },
  1151. responseError: assert.AnError,
  1152. expectedError: assert.AnError,
  1153. },
  1154. {
  1155. name: "success without reflected response",
  1156. inputBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1157. Channel: 4,
  1158. },
  1159. serviceResponse: nil, // nil response means no reflection back to caller
  1160. },
  1161. {
  1162. name: "service error without reflected response",
  1163. inputBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1164. Channel: 4,
  1165. },
  1166. serviceResponse: nil,
  1167. serviceError: assert.AnError,
  1168. expectedError: assert.AnError,
  1169. },
  1170. }
  1171. for _, tt := range tests {
  1172. t.Run(tt.name, func(t *testing.T) {
  1173. input := wire.SNACMessage{
  1174. Frame: wire.SNACFrame{
  1175. FoodGroup: wire.Chat,
  1176. SubGroup: wire.ChatChannelMsgToHost,
  1177. },
  1178. Body: tt.inputBody,
  1179. }
  1180. svc := newMockChatService(t)
  1181. svc.EXPECT().
  1182. ChannelMsgToHost(mock.Anything, mock.Anything, input.Frame, input.Body).
  1183. Return(tt.serviceResponse, tt.serviceError)
  1184. h := Handler{
  1185. ChatService: svc,
  1186. RouteLogger: middleware.RouteLogger{
  1187. Logger: slog.Default(),
  1188. },
  1189. }
  1190. responseWriter := newMockResponseWriter(t)
  1191. if tt.serviceError == nil && tt.serviceResponse != nil {
  1192. responseWriter.EXPECT().
  1193. SendSNAC(tt.serviceResponse.Frame, tt.serviceResponse.Body).
  1194. Return(tt.responseError)
  1195. }
  1196. buf := &bytes.Buffer{}
  1197. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1198. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1199. if tt.expectedError != nil {
  1200. assert.ErrorIs(t, err, tt.expectedError)
  1201. } else {
  1202. assert.NoError(t, err)
  1203. }
  1204. })
  1205. }
  1206. }
  1207. func TestHandler_FeedbagDeleteItem(t *testing.T) {
  1208. tests := []struct {
  1209. name string
  1210. inputBody wire.SNAC_0x13_0x0A_FeedbagDeleteItem
  1211. serviceError error
  1212. responseError error
  1213. expectedError error
  1214. }{
  1215. {
  1216. name: "success",
  1217. inputBody: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
  1218. Items: []wire.FeedbagItem{
  1219. {
  1220. Name: "my-item",
  1221. },
  1222. },
  1223. },
  1224. },
  1225. {
  1226. name: "service error",
  1227. inputBody: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
  1228. Items: []wire.FeedbagItem{
  1229. {
  1230. Name: "my-item",
  1231. },
  1232. },
  1233. },
  1234. serviceError: assert.AnError,
  1235. expectedError: assert.AnError,
  1236. },
  1237. {
  1238. name: "response writer error",
  1239. inputBody: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
  1240. Items: []wire.FeedbagItem{
  1241. {
  1242. Name: "my-item",
  1243. },
  1244. },
  1245. },
  1246. responseError: assert.AnError,
  1247. expectedError: assert.AnError,
  1248. },
  1249. }
  1250. for _, tt := range tests {
  1251. t.Run(tt.name, func(t *testing.T) {
  1252. input := wire.SNACMessage{
  1253. Frame: wire.SNACFrame{
  1254. FoodGroup: wire.Feedbag,
  1255. SubGroup: wire.FeedbagDeleteItem,
  1256. },
  1257. Body: tt.inputBody,
  1258. }
  1259. output := wire.SNACMessage{
  1260. Frame: wire.SNACFrame{
  1261. FoodGroup: wire.Feedbag,
  1262. SubGroup: wire.FeedbagStatus,
  1263. },
  1264. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1265. Results: []uint16{1234},
  1266. },
  1267. }
  1268. svc := newMockFeedbagService(t)
  1269. svc.EXPECT().
  1270. DeleteItem(mock.Anything, mock.Anything, input.Frame, input.Body).
  1271. Return(output, tt.serviceError)
  1272. h := Handler{
  1273. FeedbagService: svc,
  1274. RouteLogger: middleware.RouteLogger{
  1275. Logger: slog.Default(),
  1276. },
  1277. }
  1278. responseWriter := newMockResponseWriter(t)
  1279. if tt.serviceError == nil {
  1280. responseWriter.EXPECT().
  1281. SendSNAC(output.Frame, output.Body).
  1282. Return(tt.responseError)
  1283. }
  1284. buf := &bytes.Buffer{}
  1285. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1286. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1287. if tt.expectedError != nil {
  1288. assert.ErrorIs(t, err, tt.expectedError)
  1289. } else {
  1290. assert.NoError(t, err)
  1291. }
  1292. })
  1293. }
  1294. }
  1295. func TestHandler_FeedbagEndCluster(t *testing.T) {
  1296. tests := []struct {
  1297. name string
  1298. inputBody struct{}
  1299. expectedError error
  1300. }{
  1301. {
  1302. name: "success",
  1303. inputBody: struct{}{},
  1304. },
  1305. }
  1306. for _, tt := range tests {
  1307. t.Run(tt.name, func(t *testing.T) {
  1308. input := wire.SNACMessage{
  1309. Frame: wire.SNACFrame{
  1310. FoodGroup: wire.Feedbag,
  1311. SubGroup: wire.FeedbagEndCluster,
  1312. },
  1313. Body: tt.inputBody,
  1314. }
  1315. svc := newMockFeedbagService(t)
  1316. h := Handler{
  1317. FeedbagService: svc,
  1318. RouteLogger: middleware.RouteLogger{
  1319. Logger: slog.Default(),
  1320. },
  1321. }
  1322. responseWriter := newMockResponseWriter(t)
  1323. buf := &bytes.Buffer{}
  1324. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1325. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1326. if tt.expectedError != nil {
  1327. assert.ErrorIs(t, err, tt.expectedError)
  1328. } else {
  1329. assert.NoError(t, err)
  1330. }
  1331. })
  1332. }
  1333. }
  1334. func TestHandler_FeedbagInsertItem(t *testing.T) {
  1335. tests := []struct {
  1336. name string
  1337. inputBody wire.SNAC_0x13_0x08_FeedbagInsertItem
  1338. serviceError error
  1339. responseError error
  1340. expectedError error
  1341. }{
  1342. {
  1343. name: "success",
  1344. inputBody: wire.SNAC_0x13_0x08_FeedbagInsertItem{
  1345. Items: []wire.FeedbagItem{
  1346. {
  1347. Name: "my-item",
  1348. },
  1349. },
  1350. },
  1351. },
  1352. {
  1353. name: "service error",
  1354. inputBody: wire.SNAC_0x13_0x08_FeedbagInsertItem{
  1355. Items: []wire.FeedbagItem{
  1356. {
  1357. Name: "my-item",
  1358. },
  1359. },
  1360. },
  1361. serviceError: assert.AnError,
  1362. expectedError: assert.AnError,
  1363. },
  1364. {
  1365. name: "response writer error",
  1366. inputBody: wire.SNAC_0x13_0x08_FeedbagInsertItem{
  1367. Items: []wire.FeedbagItem{
  1368. {
  1369. Name: "my-item",
  1370. },
  1371. },
  1372. },
  1373. responseError: assert.AnError,
  1374. expectedError: assert.AnError,
  1375. },
  1376. }
  1377. for _, tt := range tests {
  1378. t.Run(tt.name, func(t *testing.T) {
  1379. input := wire.SNACMessage{
  1380. Frame: wire.SNACFrame{
  1381. FoodGroup: wire.Feedbag,
  1382. SubGroup: wire.FeedbagInsertItem,
  1383. },
  1384. Body: tt.inputBody,
  1385. }
  1386. output := wire.SNACMessage{
  1387. Frame: wire.SNACFrame{
  1388. FoodGroup: wire.Feedbag,
  1389. SubGroup: wire.FeedbagStatus,
  1390. },
  1391. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1392. Results: []uint16{1234},
  1393. },
  1394. }
  1395. svc := newMockFeedbagService(t)
  1396. svc.EXPECT().
  1397. UpsertItem(mock.Anything, mock.Anything, input.Frame, tt.inputBody.Items).
  1398. Return(output, tt.serviceError)
  1399. h := Handler{
  1400. FeedbagService: svc,
  1401. RouteLogger: middleware.RouteLogger{
  1402. Logger: slog.Default(),
  1403. },
  1404. }
  1405. responseWriter := newMockResponseWriter(t)
  1406. if tt.serviceError == nil {
  1407. responseWriter.EXPECT().
  1408. SendSNAC(output.Frame, output.Body).
  1409. Return(tt.responseError)
  1410. }
  1411. buf := &bytes.Buffer{}
  1412. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1413. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1414. if tt.expectedError != nil {
  1415. assert.ErrorIs(t, err, tt.expectedError)
  1416. } else {
  1417. assert.NoError(t, err)
  1418. }
  1419. })
  1420. }
  1421. }
  1422. func TestHandler_FeedbagQuery(t *testing.T) {
  1423. tests := []struct {
  1424. name string
  1425. inputBody wire.SNAC_0x13_0x02_FeedbagRightsQuery
  1426. serviceError error
  1427. responseError error
  1428. expectedError error
  1429. }{
  1430. {
  1431. name: "success",
  1432. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1433. TLVRestBlock: wire.TLVRestBlock{
  1434. TLVList: wire.TLVList{
  1435. {
  1436. Tag: 0x01,
  1437. Value: []byte{1, 2, 3, 4},
  1438. },
  1439. },
  1440. },
  1441. },
  1442. },
  1443. {
  1444. name: "service error",
  1445. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1446. TLVRestBlock: wire.TLVRestBlock{
  1447. TLVList: wire.TLVList{
  1448. {
  1449. Tag: 0x01,
  1450. Value: []byte{1, 2, 3, 4},
  1451. },
  1452. },
  1453. },
  1454. },
  1455. serviceError: assert.AnError,
  1456. expectedError: assert.AnError,
  1457. },
  1458. {
  1459. name: "response writer error",
  1460. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1461. TLVRestBlock: wire.TLVRestBlock{
  1462. TLVList: wire.TLVList{
  1463. {
  1464. Tag: 0x01,
  1465. Value: []byte{1, 2, 3, 4},
  1466. },
  1467. },
  1468. },
  1469. },
  1470. responseError: assert.AnError,
  1471. expectedError: assert.AnError,
  1472. },
  1473. }
  1474. for _, tt := range tests {
  1475. t.Run(tt.name, func(t *testing.T) {
  1476. input := wire.SNACMessage{
  1477. Frame: wire.SNACFrame{
  1478. FoodGroup: wire.Feedbag,
  1479. SubGroup: wire.FeedbagQuery,
  1480. },
  1481. Body: tt.inputBody,
  1482. }
  1483. output := wire.SNACMessage{
  1484. Frame: wire.SNACFrame{
  1485. FoodGroup: wire.Feedbag,
  1486. SubGroup: wire.FeedbagReply,
  1487. },
  1488. Body: wire.SNAC_0x13_0x06_FeedbagReply{
  1489. Version: 4,
  1490. },
  1491. }
  1492. svc := newMockFeedbagService(t)
  1493. svc.EXPECT().
  1494. Query(mock.Anything, mock.Anything, input.Frame).
  1495. Return(output, tt.serviceError)
  1496. h := Handler{
  1497. FeedbagService: svc,
  1498. RouteLogger: middleware.RouteLogger{
  1499. Logger: slog.Default(),
  1500. },
  1501. }
  1502. responseWriter := newMockResponseWriter(t)
  1503. if tt.serviceError == nil {
  1504. responseWriter.EXPECT().
  1505. SendSNAC(output.Frame, output.Body).
  1506. Return(tt.responseError)
  1507. }
  1508. buf := &bytes.Buffer{}
  1509. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1510. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1511. if tt.expectedError != nil {
  1512. assert.ErrorIs(t, err, tt.expectedError)
  1513. } else {
  1514. assert.NoError(t, err)
  1515. }
  1516. })
  1517. }
  1518. }
  1519. func TestHandler_FeedbagQueryIfModified(t *testing.T) {
  1520. tests := []struct {
  1521. name string
  1522. inputBody wire.SNAC_0x13_0x05_FeedbagQueryIfModified
  1523. serviceError error
  1524. responseError error
  1525. expectedError error
  1526. }{
  1527. {
  1528. name: "success",
  1529. inputBody: wire.SNAC_0x13_0x05_FeedbagQueryIfModified{
  1530. LastUpdate: 1234,
  1531. },
  1532. },
  1533. {
  1534. name: "service error",
  1535. inputBody: wire.SNAC_0x13_0x05_FeedbagQueryIfModified{
  1536. LastUpdate: 1234,
  1537. },
  1538. serviceError: assert.AnError,
  1539. expectedError: assert.AnError,
  1540. },
  1541. {
  1542. name: "response writer error",
  1543. inputBody: wire.SNAC_0x13_0x05_FeedbagQueryIfModified{
  1544. LastUpdate: 1234,
  1545. },
  1546. responseError: assert.AnError,
  1547. expectedError: assert.AnError,
  1548. },
  1549. }
  1550. for _, tt := range tests {
  1551. t.Run(tt.name, func(t *testing.T) {
  1552. input := wire.SNACMessage{
  1553. Frame: wire.SNACFrame{
  1554. FoodGroup: wire.Feedbag,
  1555. SubGroup: wire.FeedbagQueryIfModified,
  1556. },
  1557. Body: tt.inputBody,
  1558. }
  1559. output := wire.SNACMessage{
  1560. Frame: wire.SNACFrame{
  1561. FoodGroup: wire.Feedbag,
  1562. SubGroup: wire.FeedbagReply,
  1563. },
  1564. Body: wire.SNAC_0x13_0x06_FeedbagReply{
  1565. LastUpdate: 1234,
  1566. },
  1567. }
  1568. svc := newMockFeedbagService(t)
  1569. svc.EXPECT().
  1570. QueryIfModified(mock.Anything, mock.Anything, input.Frame, input.Body).
  1571. Return(output, tt.serviceError)
  1572. h := Handler{
  1573. FeedbagService: svc,
  1574. RouteLogger: middleware.RouteLogger{
  1575. Logger: slog.Default(),
  1576. },
  1577. }
  1578. responseWriter := newMockResponseWriter(t)
  1579. if tt.serviceError == nil {
  1580. responseWriter.EXPECT().
  1581. SendSNAC(output.Frame, output.Body).
  1582. Return(tt.responseError)
  1583. }
  1584. buf := &bytes.Buffer{}
  1585. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1586. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1587. if tt.expectedError != nil {
  1588. assert.ErrorIs(t, err, tt.expectedError)
  1589. } else {
  1590. assert.NoError(t, err)
  1591. }
  1592. })
  1593. }
  1594. }
  1595. func TestHandler_FeedbagRightsQuery(t *testing.T) {
  1596. tests := []struct {
  1597. name string
  1598. inputBody wire.SNAC_0x13_0x02_FeedbagRightsQuery
  1599. responseError error
  1600. expectedError error
  1601. }{
  1602. {
  1603. name: "success",
  1604. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1605. TLVRestBlock: wire.TLVRestBlock{
  1606. TLVList: wire.TLVList{
  1607. {
  1608. Tag: 0x01,
  1609. Value: []byte{1, 2, 3, 4},
  1610. },
  1611. },
  1612. },
  1613. },
  1614. },
  1615. {
  1616. name: "response writer error",
  1617. inputBody: wire.SNAC_0x13_0x02_FeedbagRightsQuery{
  1618. TLVRestBlock: wire.TLVRestBlock{
  1619. TLVList: wire.TLVList{
  1620. {
  1621. Tag: 0x01,
  1622. Value: []byte{1, 2, 3, 4},
  1623. },
  1624. },
  1625. },
  1626. },
  1627. responseError: assert.AnError,
  1628. expectedError: assert.AnError,
  1629. },
  1630. }
  1631. for _, tt := range tests {
  1632. t.Run(tt.name, func(t *testing.T) {
  1633. input := wire.SNACMessage{
  1634. Frame: wire.SNACFrame{
  1635. FoodGroup: wire.Feedbag,
  1636. SubGroup: wire.FeedbagRightsQuery,
  1637. },
  1638. Body: tt.inputBody,
  1639. }
  1640. output := wire.SNACMessage{
  1641. Frame: wire.SNACFrame{
  1642. FoodGroup: wire.Feedbag,
  1643. SubGroup: wire.FeedbagRightsReply,
  1644. },
  1645. Body: wire.SNAC_0x13_0x03_FeedbagRightsReply{
  1646. TLVRestBlock: wire.TLVRestBlock{
  1647. TLVList: wire.TLVList{
  1648. {
  1649. Tag: 0x01,
  1650. Value: []byte{1, 2, 3, 4},
  1651. },
  1652. },
  1653. },
  1654. },
  1655. }
  1656. svc := newMockFeedbagService(t)
  1657. svc.EXPECT().
  1658. RightsQuery(mock.Anything, input.Frame).
  1659. Return(output)
  1660. h := Handler{
  1661. FeedbagService: svc,
  1662. RouteLogger: middleware.RouteLogger{
  1663. Logger: slog.Default(),
  1664. },
  1665. }
  1666. responseWriter := newMockResponseWriter(t)
  1667. responseWriter.EXPECT().
  1668. SendSNAC(output.Frame, output.Body).
  1669. Return(tt.responseError)
  1670. buf := &bytes.Buffer{}
  1671. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1672. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1673. if tt.expectedError != nil {
  1674. assert.ErrorIs(t, err, tt.expectedError)
  1675. } else {
  1676. assert.NoError(t, err)
  1677. }
  1678. })
  1679. }
  1680. }
  1681. func TestHandler_FeedbagStartCluster(t *testing.T) {
  1682. tests := []struct {
  1683. name string
  1684. inputBody wire.SNAC_0x13_0x11_FeedbagStartCluster
  1685. expectedError error
  1686. }{
  1687. {
  1688. name: "success",
  1689. inputBody: wire.SNAC_0x13_0x11_FeedbagStartCluster{
  1690. TLVRestBlock: wire.TLVRestBlock{
  1691. TLVList: wire.TLVList{
  1692. {
  1693. Tag: 0x01,
  1694. Value: []byte{1, 2, 3, 4},
  1695. },
  1696. },
  1697. },
  1698. },
  1699. },
  1700. }
  1701. for _, tt := range tests {
  1702. t.Run(tt.name, func(t *testing.T) {
  1703. input := wire.SNACMessage{
  1704. Frame: wire.SNACFrame{
  1705. FoodGroup: wire.Feedbag,
  1706. SubGroup: wire.FeedbagStartCluster,
  1707. },
  1708. Body: tt.inputBody,
  1709. }
  1710. svc := newMockFeedbagService(t)
  1711. svc.EXPECT().
  1712. StartCluster(mock.Anything, input.Frame, input.Body)
  1713. h := Handler{
  1714. FeedbagService: svc,
  1715. RouteLogger: middleware.RouteLogger{
  1716. Logger: slog.Default(),
  1717. },
  1718. }
  1719. responseWriter := newMockResponseWriter(t)
  1720. buf := &bytes.Buffer{}
  1721. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1722. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1723. if tt.expectedError != nil {
  1724. assert.ErrorIs(t, err, tt.expectedError)
  1725. } else {
  1726. assert.NoError(t, err)
  1727. }
  1728. })
  1729. }
  1730. }
  1731. func TestHandler_FeedbagUpdateItem(t *testing.T) {
  1732. tests := []struct {
  1733. name string
  1734. inputBody wire.SNAC_0x13_0x09_FeedbagUpdateItem
  1735. serviceError error
  1736. responseError error
  1737. expectedError error
  1738. }{
  1739. {
  1740. name: "success",
  1741. inputBody: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
  1742. Items: []wire.FeedbagItem{
  1743. {
  1744. Name: "my-item",
  1745. },
  1746. },
  1747. },
  1748. },
  1749. {
  1750. name: "service error",
  1751. inputBody: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
  1752. Items: []wire.FeedbagItem{
  1753. {
  1754. Name: "my-item",
  1755. },
  1756. },
  1757. },
  1758. serviceError: assert.AnError,
  1759. expectedError: assert.AnError,
  1760. },
  1761. {
  1762. name: "response writer error",
  1763. inputBody: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
  1764. Items: []wire.FeedbagItem{
  1765. {
  1766. Name: "my-item",
  1767. },
  1768. },
  1769. },
  1770. responseError: assert.AnError,
  1771. expectedError: assert.AnError,
  1772. },
  1773. }
  1774. for _, tt := range tests {
  1775. t.Run(tt.name, func(t *testing.T) {
  1776. input := wire.SNACMessage{
  1777. Frame: wire.SNACFrame{
  1778. FoodGroup: wire.Feedbag,
  1779. SubGroup: wire.FeedbagUpdateItem,
  1780. },
  1781. Body: tt.inputBody,
  1782. }
  1783. output := wire.SNACMessage{
  1784. Frame: wire.SNACFrame{
  1785. FoodGroup: wire.Feedbag,
  1786. SubGroup: wire.FeedbagStatus,
  1787. },
  1788. Body: wire.SNAC_0x13_0x0E_FeedbagStatus{
  1789. Results: []uint16{1234},
  1790. },
  1791. }
  1792. svc := newMockFeedbagService(t)
  1793. svc.EXPECT().
  1794. UpsertItem(mock.Anything, mock.Anything, input.Frame, tt.inputBody.Items).
  1795. Return(output, tt.serviceError)
  1796. h := Handler{
  1797. FeedbagService: svc,
  1798. RouteLogger: middleware.RouteLogger{
  1799. Logger: slog.Default(),
  1800. },
  1801. }
  1802. responseWriter := newMockResponseWriter(t)
  1803. if tt.serviceError == nil {
  1804. responseWriter.EXPECT().
  1805. SendSNAC(output.Frame, output.Body).
  1806. Return(tt.responseError)
  1807. }
  1808. buf := &bytes.Buffer{}
  1809. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1810. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1811. if tt.expectedError != nil {
  1812. assert.ErrorIs(t, err, tt.expectedError)
  1813. } else {
  1814. assert.NoError(t, err)
  1815. }
  1816. })
  1817. }
  1818. }
  1819. func TestHandler_FeedbagUse(t *testing.T) {
  1820. tests := []struct {
  1821. name string
  1822. inputBody struct{}
  1823. serviceError error
  1824. expectedError error
  1825. }{
  1826. {
  1827. name: "success",
  1828. inputBody: struct{}{},
  1829. },
  1830. {
  1831. name: "service error",
  1832. inputBody: struct{}{},
  1833. serviceError: assert.AnError,
  1834. expectedError: assert.AnError,
  1835. },
  1836. }
  1837. for _, tt := range tests {
  1838. t.Run(tt.name, func(t *testing.T) {
  1839. input := wire.SNACMessage{
  1840. Frame: wire.SNACFrame{
  1841. FoodGroup: wire.Feedbag,
  1842. SubGroup: wire.FeedbagUse,
  1843. },
  1844. Body: tt.inputBody,
  1845. }
  1846. svc := newMockFeedbagService(t)
  1847. svc.EXPECT().
  1848. Use(mock.Anything, mock.Anything).
  1849. Return(tt.serviceError)
  1850. h := Handler{
  1851. FeedbagService: svc,
  1852. RouteLogger: middleware.RouteLogger{
  1853. Logger: slog.Default(),
  1854. },
  1855. }
  1856. responseWriter := newMockResponseWriter(t)
  1857. buf := &bytes.Buffer{}
  1858. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1859. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1860. if tt.expectedError != nil {
  1861. assert.ErrorIs(t, err, tt.expectedError)
  1862. } else {
  1863. assert.NoError(t, err)
  1864. }
  1865. })
  1866. }
  1867. }
  1868. func TestHandler_FeedbagRespondAuthorizeToHost(t *testing.T) {
  1869. input := wire.SNACMessage{
  1870. Frame: wire.SNACFrame{
  1871. FoodGroup: wire.Feedbag,
  1872. SubGroup: wire.FeedbagRespondAuthorizeToHost,
  1873. },
  1874. Body: wire.SNAC_0x13_0x1A_FeedbagRespondAuthorizeToHost{
  1875. ScreenName: "theScreenName",
  1876. },
  1877. }
  1878. svc := newMockFeedbagService(t)
  1879. svc.EXPECT().
  1880. RespondAuthorizeToHost(mock.Anything, mock.Anything, input.Frame, input.Body).
  1881. Return(nil)
  1882. h := Handler{
  1883. FeedbagService: svc,
  1884. RouteLogger: middleware.RouteLogger{
  1885. Logger: slog.Default(),
  1886. },
  1887. }
  1888. responseWriter := newMockResponseWriter(t)
  1889. buf := &bytes.Buffer{}
  1890. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1891. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  1892. }
  1893. func TestHandler_ICBMAddParameters(t *testing.T) {
  1894. tests := []struct {
  1895. name string
  1896. inputBody wire.SNAC_0x04_0x02_ICBMAddParameters
  1897. expectedError error
  1898. }{
  1899. {
  1900. name: "success",
  1901. inputBody: wire.SNAC_0x04_0x02_ICBMAddParameters{
  1902. Channel: 1,
  1903. },
  1904. },
  1905. }
  1906. for _, tt := range tests {
  1907. t.Run(tt.name, func(t *testing.T) {
  1908. input := wire.SNACMessage{
  1909. Frame: wire.SNACFrame{
  1910. FoodGroup: wire.ICBM,
  1911. SubGroup: wire.ICBMAddParameters,
  1912. },
  1913. Body: tt.inputBody,
  1914. }
  1915. svc := newMockICBMService(t)
  1916. h := Handler{
  1917. ICBMService: svc,
  1918. RouteLogger: middleware.RouteLogger{
  1919. Logger: slog.Default(),
  1920. },
  1921. }
  1922. responseWriter := newMockResponseWriter(t)
  1923. buf := &bytes.Buffer{}
  1924. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  1925. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  1926. if tt.expectedError != nil {
  1927. assert.ErrorIs(t, err, tt.expectedError)
  1928. } else {
  1929. assert.NoError(t, err)
  1930. }
  1931. })
  1932. }
  1933. }
  1934. func TestHandler_ICBMChannelMsgToHost(t *testing.T) {
  1935. tests := []struct {
  1936. name string
  1937. inputBody wire.SNAC_0x04_0x06_ICBMChannelMsgToHost
  1938. serviceError error
  1939. responseError error
  1940. expectedError error
  1941. }{
  1942. {
  1943. name: "success",
  1944. inputBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  1945. ScreenName: "recipient-screen-name",
  1946. },
  1947. },
  1948. {
  1949. name: "service error",
  1950. inputBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  1951. ScreenName: "recipient-screen-name",
  1952. },
  1953. serviceError: assert.AnError,
  1954. expectedError: assert.AnError,
  1955. },
  1956. {
  1957. name: "response writer error",
  1958. inputBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  1959. ScreenName: "recipient-screen-name",
  1960. },
  1961. responseError: assert.AnError,
  1962. expectedError: assert.AnError,
  1963. },
  1964. }
  1965. for _, tt := range tests {
  1966. t.Run(tt.name, func(t *testing.T) {
  1967. input := wire.SNACMessage{
  1968. Frame: wire.SNACFrame{
  1969. FoodGroup: wire.ICBM,
  1970. SubGroup: wire.ICBMChannelMsgToHost,
  1971. },
  1972. Body: tt.inputBody,
  1973. }
  1974. output := wire.SNACMessage{
  1975. Frame: wire.SNACFrame{
  1976. FoodGroup: wire.ICBM,
  1977. SubGroup: wire.ICBMHostAck,
  1978. },
  1979. Body: wire.SNAC_0x04_0x0C_ICBMHostAck{
  1980. ChannelID: 4,
  1981. },
  1982. }
  1983. svc := newMockICBMService(t)
  1984. svc.EXPECT().
  1985. ChannelMsgToHost(mock.Anything, mock.Anything, input.Frame, input.Body).
  1986. Return(&output, tt.serviceError)
  1987. h := Handler{
  1988. ICBMService: svc,
  1989. RouteLogger: middleware.RouteLogger{
  1990. Logger: slog.Default(),
  1991. },
  1992. }
  1993. responseWriter := newMockResponseWriter(t)
  1994. if tt.serviceError == nil {
  1995. responseWriter.EXPECT().
  1996. SendSNAC(output.Frame, output.Body).
  1997. Return(tt.responseError)
  1998. }
  1999. buf := &bytes.Buffer{}
  2000. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2001. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2002. if tt.expectedError != nil {
  2003. assert.ErrorIs(t, err, tt.expectedError)
  2004. } else {
  2005. assert.NoError(t, err)
  2006. }
  2007. })
  2008. }
  2009. }
  2010. func TestHandler_ICBMClientErr(t *testing.T) {
  2011. tests := []struct {
  2012. name string
  2013. inputBody wire.SNAC_0x04_0x0B_ICBMClientErr
  2014. serviceError error
  2015. expectedError error
  2016. }{
  2017. {
  2018. name: "success",
  2019. inputBody: wire.SNAC_0x04_0x0B_ICBMClientErr{
  2020. Code: 4,
  2021. },
  2022. },
  2023. {
  2024. name: "service error",
  2025. inputBody: wire.SNAC_0x04_0x0B_ICBMClientErr{
  2026. Code: 4,
  2027. },
  2028. serviceError: assert.AnError,
  2029. expectedError: assert.AnError,
  2030. },
  2031. }
  2032. for _, tt := range tests {
  2033. t.Run(tt.name, func(t *testing.T) {
  2034. input := wire.SNACMessage{
  2035. Frame: wire.SNACFrame{
  2036. FoodGroup: wire.ICBM,
  2037. SubGroup: wire.ICBMClientErr,
  2038. },
  2039. Body: tt.inputBody,
  2040. }
  2041. svc := newMockICBMService(t)
  2042. svc.EXPECT().
  2043. ClientErr(mock.Anything, mock.Anything, input.Frame, input.Body).
  2044. Return(tt.serviceError)
  2045. h := Handler{
  2046. ICBMService: svc,
  2047. RouteLogger: middleware.RouteLogger{
  2048. Logger: slog.Default(),
  2049. },
  2050. }
  2051. responseWriter := newMockResponseWriter(t)
  2052. buf := &bytes.Buffer{}
  2053. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2054. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2055. if tt.expectedError != nil {
  2056. assert.ErrorIs(t, err, tt.expectedError)
  2057. } else {
  2058. assert.NoError(t, err)
  2059. }
  2060. })
  2061. }
  2062. }
  2063. func TestHandler_ICBMClientEvent(t *testing.T) {
  2064. tests := []struct {
  2065. name string
  2066. inputBody wire.SNAC_0x04_0x14_ICBMClientEvent
  2067. serviceError error
  2068. expectedError error
  2069. }{
  2070. {
  2071. name: "success",
  2072. inputBody: wire.SNAC_0x04_0x14_ICBMClientEvent{
  2073. ScreenName: "recipient-screen-name",
  2074. },
  2075. },
  2076. {
  2077. name: "service error",
  2078. inputBody: wire.SNAC_0x04_0x14_ICBMClientEvent{
  2079. ScreenName: "recipient-screen-name",
  2080. },
  2081. serviceError: assert.AnError,
  2082. expectedError: assert.AnError,
  2083. },
  2084. }
  2085. for _, tt := range tests {
  2086. t.Run(tt.name, func(t *testing.T) {
  2087. input := wire.SNACMessage{
  2088. Frame: wire.SNACFrame{
  2089. FoodGroup: wire.ICBM,
  2090. SubGroup: wire.ICBMClientEvent,
  2091. },
  2092. Body: tt.inputBody,
  2093. }
  2094. svc := newMockICBMService(t)
  2095. svc.EXPECT().
  2096. ClientEvent(mock.Anything, mock.Anything, input.Frame, input.Body).
  2097. Return(tt.serviceError)
  2098. h := Handler{
  2099. ICBMService: svc,
  2100. RouteLogger: middleware.RouteLogger{
  2101. Logger: slog.Default(),
  2102. },
  2103. }
  2104. responseWriter := newMockResponseWriter(t)
  2105. buf := &bytes.Buffer{}
  2106. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2107. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2108. if tt.expectedError != nil {
  2109. assert.ErrorIs(t, err, tt.expectedError)
  2110. } else {
  2111. assert.NoError(t, err)
  2112. }
  2113. })
  2114. }
  2115. }
  2116. func TestHandler_ICBMEvilRequest(t *testing.T) {
  2117. tests := []struct {
  2118. name string
  2119. inputBody wire.SNAC_0x04_0x08_ICBMEvilRequest
  2120. serviceError error
  2121. responseError error
  2122. expectedError error
  2123. }{
  2124. {
  2125. name: "success",
  2126. inputBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  2127. ScreenName: "recipient-screen-name",
  2128. },
  2129. },
  2130. {
  2131. name: "service error",
  2132. inputBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  2133. ScreenName: "recipient-screen-name",
  2134. },
  2135. serviceError: assert.AnError,
  2136. expectedError: assert.AnError,
  2137. },
  2138. {
  2139. name: "response writer error",
  2140. inputBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  2141. ScreenName: "recipient-screen-name",
  2142. },
  2143. responseError: assert.AnError,
  2144. expectedError: assert.AnError,
  2145. },
  2146. }
  2147. for _, tt := range tests {
  2148. t.Run(tt.name, func(t *testing.T) {
  2149. input := wire.SNACMessage{
  2150. Frame: wire.SNACFrame{
  2151. FoodGroup: wire.ICBM,
  2152. SubGroup: wire.ICBMEvilRequest,
  2153. },
  2154. Body: tt.inputBody,
  2155. }
  2156. output := wire.SNACMessage{
  2157. Frame: wire.SNACFrame{
  2158. FoodGroup: wire.ICBM,
  2159. SubGroup: wire.ICBMEvilReply,
  2160. },
  2161. Body: wire.SNAC_0x04_0x09_ICBMEvilReply{
  2162. EvilDeltaApplied: 100,
  2163. },
  2164. }
  2165. svc := newMockICBMService(t)
  2166. svc.EXPECT().
  2167. EvilRequest(mock.Anything, mock.Anything, input.Frame, input.Body).
  2168. Return(output, tt.serviceError)
  2169. h := Handler{
  2170. ICBMService: svc,
  2171. RouteLogger: middleware.RouteLogger{
  2172. Logger: slog.Default(),
  2173. },
  2174. }
  2175. responseWriter := newMockResponseWriter(t)
  2176. if tt.serviceError == nil {
  2177. responseWriter.EXPECT().
  2178. SendSNAC(output.Frame, output.Body).
  2179. Return(tt.responseError)
  2180. }
  2181. buf := &bytes.Buffer{}
  2182. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2183. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2184. if tt.expectedError != nil {
  2185. assert.ErrorIs(t, err, tt.expectedError)
  2186. } else {
  2187. assert.NoError(t, err)
  2188. }
  2189. })
  2190. }
  2191. }
  2192. func TestHandler_ICBMParameterQuery(t *testing.T) {
  2193. tests := []struct {
  2194. name string
  2195. inputBody struct{}
  2196. responseError error
  2197. expectedError error
  2198. }{
  2199. {
  2200. name: "success",
  2201. inputBody: struct{}{},
  2202. },
  2203. {
  2204. name: "response writer error",
  2205. inputBody: struct{}{},
  2206. responseError: assert.AnError,
  2207. expectedError: assert.AnError,
  2208. },
  2209. }
  2210. for _, tt := range tests {
  2211. t.Run(tt.name, func(t *testing.T) {
  2212. input := wire.SNACMessage{
  2213. Frame: wire.SNACFrame{
  2214. FoodGroup: wire.ICBM,
  2215. SubGroup: wire.ICBMParameterQuery,
  2216. },
  2217. Body: tt.inputBody,
  2218. }
  2219. output := wire.SNACMessage{
  2220. Frame: wire.SNACFrame{
  2221. FoodGroup: wire.ICBM,
  2222. SubGroup: wire.ICBMParameterReply,
  2223. },
  2224. Body: wire.SNAC_0x04_0x05_ICBMParameterReply{
  2225. MaxSlots: 100,
  2226. },
  2227. }
  2228. svc := newMockICBMService(t)
  2229. svc.EXPECT().
  2230. ParameterQuery(mock.Anything, input.Frame).
  2231. Return(output)
  2232. h := Handler{
  2233. ICBMService: svc,
  2234. RouteLogger: middleware.RouteLogger{
  2235. Logger: slog.Default(),
  2236. },
  2237. }
  2238. responseWriter := newMockResponseWriter(t)
  2239. responseWriter.EXPECT().
  2240. SendSNAC(output.Frame, output.Body).
  2241. Return(tt.responseError)
  2242. buf := &bytes.Buffer{}
  2243. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  2244. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  2245. if tt.expectedError != nil {
  2246. assert.ErrorIs(t, err, tt.expectedError)
  2247. } else {
  2248. assert.NoError(t, err)
  2249. }
  2250. })
  2251. }
  2252. }
  2253. func TestHandler_ICQDBQuery(t *testing.T) {
  2254. type ICQMetaRequest struct {
  2255. wire.ICQMetadata
  2256. ReqSubType uint16
  2257. MetaRequest any
  2258. }
  2259. type reqParams struct {
  2260. ctx context.Context
  2261. sess *state.Session
  2262. inFrame wire.SNACFrame
  2263. inBody wire.SNAC_0x15_0x02_BQuery
  2264. rw ResponseWriter
  2265. seq uint16
  2266. wantErr error
  2267. }
  2268. type mockParam struct {
  2269. req any
  2270. wantErr error
  2271. }
  2272. type allMockParams struct {
  2273. deleteMsgReq *mockParam
  2274. findByDetails *mockParam
  2275. findByEmail *mockParam
  2276. findByEmail3 *mockParam
  2277. findByInterests *mockParam
  2278. findByUIN *mockParam
  2279. findByUIN2 *mockParam
  2280. findByWhitePages2 *mockParam
  2281. fullUserInfo *mockParam
  2282. offlineMsgReq *mockParam
  2283. setAffiliations *mockParam
  2284. setBasicInfo *mockParam
  2285. setEmails *mockParam
  2286. setInterests *mockParam
  2287. setMoreInfo *mockParam
  2288. setPermissions *mockParam
  2289. setUserNotes *mockParam
  2290. setWorkInfo *mockParam
  2291. shortUserInfo *mockParam
  2292. xmlReqData *mockParam
  2293. }
  2294. tests := []struct {
  2295. name string
  2296. reqParams reqParams
  2297. allMockParams allMockParams
  2298. }{
  2299. {
  2300. name: "MetaReqFullInfo - happy path",
  2301. reqParams: reqParams{
  2302. sess: &state.Session{},
  2303. inBody: wire.SNAC_0x15_0x02_BQuery{
  2304. TLVRestBlock: wire.TLVRestBlock{
  2305. TLVList: wire.TLVList{
  2306. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2307. Message: ICQMetaRequest{
  2308. ICQMetadata: wire.ICQMetadata{
  2309. ReqType: wire.ICQDBQueryMetaReq,
  2310. Seq: 1,
  2311. },
  2312. ReqSubType: wire.ICQDBQueryMetaReqFullInfo,
  2313. MetaRequest: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2314. UIN: 123456789,
  2315. },
  2316. },
  2317. }),
  2318. },
  2319. },
  2320. },
  2321. seq: 1,
  2322. },
  2323. allMockParams: allMockParams{
  2324. fullUserInfo: &mockParam{
  2325. req: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2326. UIN: 123456789,
  2327. },
  2328. },
  2329. },
  2330. },
  2331. {
  2332. name: "MetaReqShortInfo - happy path",
  2333. reqParams: reqParams{
  2334. sess: &state.Session{},
  2335. inBody: wire.SNAC_0x15_0x02_BQuery{
  2336. TLVRestBlock: wire.TLVRestBlock{
  2337. TLVList: wire.TLVList{
  2338. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2339. Message: ICQMetaRequest{
  2340. ICQMetadata: wire.ICQMetadata{
  2341. ReqType: wire.ICQDBQueryMetaReq,
  2342. Seq: 1,
  2343. },
  2344. ReqSubType: wire.ICQDBQueryMetaReqShortInfo,
  2345. MetaRequest: wire.ICQ_0x07D0_0x04BA_DBQueryMetaReqShortInfo{
  2346. UIN: 123456789,
  2347. },
  2348. },
  2349. }),
  2350. },
  2351. },
  2352. },
  2353. seq: 1,
  2354. },
  2355. allMockParams: allMockParams{
  2356. shortUserInfo: &mockParam{
  2357. req: wire.ICQ_0x07D0_0x04BA_DBQueryMetaReqShortInfo{
  2358. UIN: 123456789,
  2359. },
  2360. },
  2361. },
  2362. },
  2363. {
  2364. name: "MetaReqFullInfo2 - happy path",
  2365. reqParams: reqParams{
  2366. sess: &state.Session{},
  2367. inBody: wire.SNAC_0x15_0x02_BQuery{
  2368. TLVRestBlock: wire.TLVRestBlock{
  2369. TLVList: wire.TLVList{
  2370. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2371. Message: ICQMetaRequest{
  2372. ICQMetadata: wire.ICQMetadata{
  2373. ReqType: wire.ICQDBQueryMetaReq,
  2374. Seq: 1,
  2375. },
  2376. ReqSubType: wire.ICQDBQueryMetaReqFullInfo2,
  2377. MetaRequest: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2378. UIN: 123456789,
  2379. },
  2380. },
  2381. }),
  2382. },
  2383. },
  2384. },
  2385. seq: 1,
  2386. },
  2387. allMockParams: allMockParams{
  2388. fullUserInfo: &mockParam{
  2389. req: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2390. UIN: 123456789,
  2391. },
  2392. },
  2393. },
  2394. },
  2395. {
  2396. name: "MetaReqXMLReq - happy path",
  2397. reqParams: reqParams{
  2398. sess: &state.Session{},
  2399. inBody: wire.SNAC_0x15_0x02_BQuery{
  2400. TLVRestBlock: wire.TLVRestBlock{
  2401. TLVList: wire.TLVList{
  2402. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2403. Message: ICQMetaRequest{
  2404. ICQMetadata: wire.ICQMetadata{
  2405. ReqType: wire.ICQDBQueryMetaReq,
  2406. Seq: 1,
  2407. },
  2408. ReqSubType: wire.ICQDBQueryMetaReqXMLReq,
  2409. MetaRequest: wire.ICQ_0x07D0_0x0898_DBQueryMetaReqXMLReq{
  2410. XMLRequest: "<xml></xml>",
  2411. },
  2412. },
  2413. }),
  2414. },
  2415. },
  2416. },
  2417. seq: 1,
  2418. },
  2419. allMockParams: allMockParams{
  2420. xmlReqData: &mockParam{
  2421. req: wire.ICQ_0x07D0_0x0898_DBQueryMetaReqXMLReq{
  2422. XMLRequest: "<xml></xml>",
  2423. },
  2424. },
  2425. },
  2426. },
  2427. {
  2428. name: "MetaReqSetPermissions - happy path",
  2429. reqParams: reqParams{
  2430. sess: &state.Session{},
  2431. inBody: wire.SNAC_0x15_0x02_BQuery{
  2432. TLVRestBlock: wire.TLVRestBlock{
  2433. TLVList: wire.TLVList{
  2434. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2435. Message: ICQMetaRequest{
  2436. ICQMetadata: wire.ICQMetadata{
  2437. ReqType: wire.ICQDBQueryMetaReq,
  2438. Seq: 1,
  2439. },
  2440. ReqSubType: wire.ICQDBQueryMetaReqSetPermissions,
  2441. MetaRequest: wire.ICQ_0x07D0_0x0424_DBQueryMetaReqSetPermissions{
  2442. Authorization: 1,
  2443. },
  2444. },
  2445. }),
  2446. },
  2447. },
  2448. },
  2449. seq: 1,
  2450. },
  2451. allMockParams: allMockParams{
  2452. setPermissions: &mockParam{
  2453. req: wire.ICQ_0x07D0_0x0424_DBQueryMetaReqSetPermissions{
  2454. Authorization: 1,
  2455. },
  2456. },
  2457. },
  2458. },
  2459. {
  2460. name: "MetaReqSearchByUIN - happy path",
  2461. reqParams: reqParams{
  2462. sess: &state.Session{},
  2463. inBody: wire.SNAC_0x15_0x02_BQuery{
  2464. TLVRestBlock: wire.TLVRestBlock{
  2465. TLVList: wire.TLVList{
  2466. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2467. Message: ICQMetaRequest{
  2468. ICQMetadata: wire.ICQMetadata{
  2469. ReqType: wire.ICQDBQueryMetaReq,
  2470. Seq: 1,
  2471. },
  2472. ReqSubType: wire.ICQDBQueryMetaReqSearchByUIN,
  2473. MetaRequest: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2474. UIN: 123456789,
  2475. },
  2476. },
  2477. }),
  2478. },
  2479. },
  2480. },
  2481. seq: 1,
  2482. },
  2483. allMockParams: allMockParams{
  2484. findByUIN: &mockParam{
  2485. req: wire.ICQ_0x07D0_0x051F_DBQueryMetaReqSearchByUIN{
  2486. UIN: 123456789,
  2487. },
  2488. },
  2489. },
  2490. },
  2491. {
  2492. name: "MetaReqSearchByUIN2 - happy path",
  2493. reqParams: reqParams{
  2494. sess: &state.Session{},
  2495. inBody: wire.SNAC_0x15_0x02_BQuery{
  2496. TLVRestBlock: wire.TLVRestBlock{
  2497. TLVList: wire.TLVList{
  2498. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2499. Message: ICQMetaRequest{
  2500. ICQMetadata: wire.ICQMetadata{
  2501. ReqType: wire.ICQDBQueryMetaReq,
  2502. Seq: 1,
  2503. },
  2504. ReqSubType: wire.ICQDBQueryMetaReqSearchByUIN2,
  2505. MetaRequest: wire.ICQ_0x07D0_0x0569_DBQueryMetaReqSearchByUIN2{
  2506. TLVRestBlock: wire.TLVRestBlock{
  2507. TLVList: wire.TLVList{
  2508. wire.NewTLVBE(1, uint16(1)),
  2509. },
  2510. },
  2511. },
  2512. },
  2513. }),
  2514. },
  2515. },
  2516. },
  2517. seq: 1,
  2518. },
  2519. allMockParams: allMockParams{
  2520. findByUIN2: &mockParam{
  2521. req: wire.ICQ_0x07D0_0x0569_DBQueryMetaReqSearchByUIN2{
  2522. TLVRestBlock: wire.TLVRestBlock{
  2523. TLVList: wire.TLVList{
  2524. wire.NewTLVBE(1, uint16(1)),
  2525. },
  2526. },
  2527. },
  2528. },
  2529. },
  2530. },
  2531. {
  2532. name: "MetaReqSearchByEmail - happy path",
  2533. reqParams: reqParams{
  2534. sess: &state.Session{},
  2535. inBody: wire.SNAC_0x15_0x02_BQuery{
  2536. TLVRestBlock: wire.TLVRestBlock{
  2537. TLVList: wire.TLVList{
  2538. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2539. Message: ICQMetaRequest{
  2540. ICQMetadata: wire.ICQMetadata{
  2541. ReqType: wire.ICQDBQueryMetaReq,
  2542. Seq: 1,
  2543. },
  2544. ReqSubType: wire.ICQDBQueryMetaReqSearchByEmail,
  2545. MetaRequest: wire.ICQ_0x07D0_0x0529_DBQueryMetaReqSearchByEmail{
  2546. Email: "test@aol.com",
  2547. },
  2548. },
  2549. }),
  2550. },
  2551. },
  2552. },
  2553. seq: 1,
  2554. },
  2555. allMockParams: allMockParams{
  2556. findByEmail: &mockParam{
  2557. req: wire.ICQ_0x07D0_0x0529_DBQueryMetaReqSearchByEmail{
  2558. Email: "test@aol.com",
  2559. },
  2560. },
  2561. },
  2562. },
  2563. {
  2564. name: "MetaReqSearchByEmail3 - happy path",
  2565. reqParams: reqParams{
  2566. sess: &state.Session{},
  2567. inBody: wire.SNAC_0x15_0x02_BQuery{
  2568. TLVRestBlock: wire.TLVRestBlock{
  2569. TLVList: wire.TLVList{
  2570. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2571. Message: ICQMetaRequest{
  2572. ICQMetadata: wire.ICQMetadata{
  2573. ReqType: wire.ICQDBQueryMetaReq,
  2574. Seq: 1,
  2575. },
  2576. ReqSubType: wire.ICQDBQueryMetaReqSearchByEmail3,
  2577. MetaRequest: wire.ICQ_0x07D0_0x0573_DBQueryMetaReqSearchByEmail3{
  2578. TLVRestBlock: wire.TLVRestBlock{
  2579. TLVList: wire.TLVList{
  2580. wire.NewTLVBE(1, uint16(1)),
  2581. },
  2582. },
  2583. },
  2584. },
  2585. }),
  2586. },
  2587. },
  2588. },
  2589. seq: 1,
  2590. },
  2591. allMockParams: allMockParams{
  2592. findByEmail3: &mockParam{
  2593. req: wire.ICQ_0x07D0_0x0573_DBQueryMetaReqSearchByEmail3{
  2594. TLVRestBlock: wire.TLVRestBlock{
  2595. TLVList: wire.TLVList{
  2596. wire.NewTLVBE(1, uint16(1)),
  2597. },
  2598. },
  2599. },
  2600. },
  2601. },
  2602. },
  2603. {
  2604. name: "MetaReqSearchByDetails - happy path",
  2605. reqParams: reqParams{
  2606. sess: &state.Session{},
  2607. inBody: wire.SNAC_0x15_0x02_BQuery{
  2608. TLVRestBlock: wire.TLVRestBlock{
  2609. TLVList: wire.TLVList{
  2610. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2611. Message: ICQMetaRequest{
  2612. ICQMetadata: wire.ICQMetadata{
  2613. ReqType: wire.ICQDBQueryMetaReq,
  2614. Seq: 1,
  2615. },
  2616. ReqSubType: wire.ICQDBQueryMetaReqSearchByDetails,
  2617. MetaRequest: wire.ICQ_0x07D0_0x0515_DBQueryMetaReqSearchByDetails{
  2618. FirstName: "john",
  2619. },
  2620. },
  2621. }),
  2622. },
  2623. },
  2624. },
  2625. seq: 1,
  2626. },
  2627. allMockParams: allMockParams{
  2628. findByDetails: &mockParam{
  2629. req: wire.ICQ_0x07D0_0x0515_DBQueryMetaReqSearchByDetails{
  2630. FirstName: "john",
  2631. },
  2632. },
  2633. },
  2634. },
  2635. {
  2636. name: "MetaReqSearchWhitePages - happy path",
  2637. reqParams: reqParams{
  2638. sess: &state.Session{},
  2639. inBody: wire.SNAC_0x15_0x02_BQuery{
  2640. TLVRestBlock: wire.TLVRestBlock{
  2641. TLVList: wire.TLVList{
  2642. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2643. Message: ICQMetaRequest{
  2644. ICQMetadata: wire.ICQMetadata{
  2645. ReqType: wire.ICQDBQueryMetaReq,
  2646. Seq: 1,
  2647. },
  2648. ReqSubType: wire.ICQDBQueryMetaReqSearchWhitePages,
  2649. MetaRequest: wire.ICQ_0x07D0_0x0533_DBQueryMetaReqSearchWhitePages{
  2650. InterestsCode: 1,
  2651. },
  2652. },
  2653. }),
  2654. },
  2655. },
  2656. },
  2657. seq: 1,
  2658. },
  2659. allMockParams: allMockParams{
  2660. findByInterests: &mockParam{
  2661. req: wire.ICQ_0x07D0_0x0533_DBQueryMetaReqSearchWhitePages{
  2662. InterestsCode: 1,
  2663. },
  2664. },
  2665. },
  2666. },
  2667. {
  2668. name: "MetaReqSearchWhitePages2 - happy path",
  2669. reqParams: reqParams{
  2670. sess: &state.Session{},
  2671. inBody: wire.SNAC_0x15_0x02_BQuery{
  2672. TLVRestBlock: wire.TLVRestBlock{
  2673. TLVList: wire.TLVList{
  2674. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2675. Message: ICQMetaRequest{
  2676. ICQMetadata: wire.ICQMetadata{
  2677. ReqType: wire.ICQDBQueryMetaReq,
  2678. Seq: 1,
  2679. },
  2680. ReqSubType: wire.ICQDBQueryMetaReqSearchWhitePages2,
  2681. MetaRequest: wire.ICQ_0x07D0_0x055F_DBQueryMetaReqSearchWhitePages2{
  2682. TLVRestBlock: wire.TLVRestBlock{
  2683. TLVList: wire.TLVList{
  2684. wire.NewTLVBE(1, uint16(1)),
  2685. },
  2686. },
  2687. },
  2688. },
  2689. }),
  2690. },
  2691. },
  2692. },
  2693. seq: 1,
  2694. },
  2695. allMockParams: allMockParams{
  2696. findByWhitePages2: &mockParam{
  2697. req: wire.ICQ_0x07D0_0x055F_DBQueryMetaReqSearchWhitePages2{
  2698. TLVRestBlock: wire.TLVRestBlock{
  2699. TLVList: wire.TLVList{
  2700. wire.NewTLVBE(1, uint16(1)),
  2701. },
  2702. },
  2703. },
  2704. },
  2705. },
  2706. },
  2707. {
  2708. name: "MetaReqSetBasicInfo - happy path",
  2709. reqParams: reqParams{
  2710. sess: &state.Session{},
  2711. inBody: wire.SNAC_0x15_0x02_BQuery{
  2712. TLVRestBlock: wire.TLVRestBlock{
  2713. TLVList: wire.TLVList{
  2714. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2715. Message: ICQMetaRequest{
  2716. ICQMetadata: wire.ICQMetadata{
  2717. ReqType: wire.ICQDBQueryMetaReq,
  2718. Seq: 1,
  2719. },
  2720. ReqSubType: wire.ICQDBQueryMetaReqSetBasicInfo,
  2721. MetaRequest: wire.ICQ_0x07D0_0x03EA_DBQueryMetaReqSetBasicInfo{
  2722. FirstName: "john",
  2723. },
  2724. },
  2725. }),
  2726. },
  2727. },
  2728. },
  2729. seq: 1,
  2730. },
  2731. allMockParams: allMockParams{
  2732. setBasicInfo: &mockParam{
  2733. req: wire.ICQ_0x07D0_0x03EA_DBQueryMetaReqSetBasicInfo{
  2734. FirstName: "john",
  2735. },
  2736. },
  2737. },
  2738. },
  2739. {
  2740. name: "MetaReqSetWorkInfo - happy path",
  2741. reqParams: reqParams{
  2742. sess: &state.Session{},
  2743. inBody: wire.SNAC_0x15_0x02_BQuery{
  2744. TLVRestBlock: wire.TLVRestBlock{
  2745. TLVList: wire.TLVList{
  2746. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2747. Message: ICQMetaRequest{
  2748. ICQMetadata: wire.ICQMetadata{
  2749. ReqType: wire.ICQDBQueryMetaReq,
  2750. Seq: 1,
  2751. },
  2752. ReqSubType: wire.ICQDBQueryMetaReqSetWorkInfo,
  2753. MetaRequest: wire.ICQ_0x07D0_0x03F3_DBQueryMetaReqSetWorkInfo{
  2754. ZIP: "11111",
  2755. },
  2756. },
  2757. }),
  2758. },
  2759. },
  2760. },
  2761. seq: 1,
  2762. },
  2763. allMockParams: allMockParams{
  2764. setWorkInfo: &mockParam{
  2765. req: wire.ICQ_0x07D0_0x03F3_DBQueryMetaReqSetWorkInfo{
  2766. ZIP: "11111",
  2767. },
  2768. },
  2769. },
  2770. },
  2771. {
  2772. name: "MetaReqSetMoreInfo - happy path",
  2773. reqParams: reqParams{
  2774. sess: &state.Session{},
  2775. inBody: wire.SNAC_0x15_0x02_BQuery{
  2776. TLVRestBlock: wire.TLVRestBlock{
  2777. TLVList: wire.TLVList{
  2778. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2779. Message: ICQMetaRequest{
  2780. ICQMetadata: wire.ICQMetadata{
  2781. ReqType: wire.ICQDBQueryMetaReq,
  2782. Seq: 1,
  2783. },
  2784. ReqSubType: wire.ICQDBQueryMetaReqSetMoreInfo,
  2785. MetaRequest: wire.ICQ_0x07D0_0x03FD_DBQueryMetaReqSetMoreInfo{
  2786. Age: 100,
  2787. },
  2788. },
  2789. }),
  2790. },
  2791. },
  2792. },
  2793. seq: 1,
  2794. },
  2795. allMockParams: allMockParams{
  2796. setMoreInfo: &mockParam{
  2797. req: wire.ICQ_0x07D0_0x03FD_DBQueryMetaReqSetMoreInfo{
  2798. Age: 100,
  2799. },
  2800. },
  2801. },
  2802. },
  2803. {
  2804. name: "MetaReqSetNotes - happy path",
  2805. reqParams: reqParams{
  2806. sess: &state.Session{},
  2807. inBody: wire.SNAC_0x15_0x02_BQuery{
  2808. TLVRestBlock: wire.TLVRestBlock{
  2809. TLVList: wire.TLVList{
  2810. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2811. Message: ICQMetaRequest{
  2812. ICQMetadata: wire.ICQMetadata{
  2813. ReqType: wire.ICQDBQueryMetaReq,
  2814. Seq: 1,
  2815. },
  2816. ReqSubType: wire.ICQDBQueryMetaReqSetNotes,
  2817. MetaRequest: wire.ICQ_0x07D0_0x0406_DBQueryMetaReqSetNotes{
  2818. Notes: "my note",
  2819. },
  2820. },
  2821. }),
  2822. },
  2823. },
  2824. },
  2825. seq: 1,
  2826. },
  2827. allMockParams: allMockParams{
  2828. setUserNotes: &mockParam{
  2829. req: wire.ICQ_0x07D0_0x0406_DBQueryMetaReqSetNotes{
  2830. Notes: "my note",
  2831. },
  2832. },
  2833. },
  2834. },
  2835. {
  2836. name: "MetaReqSetEmails - happy path",
  2837. reqParams: reqParams{
  2838. sess: &state.Session{},
  2839. inBody: wire.SNAC_0x15_0x02_BQuery{
  2840. TLVRestBlock: wire.TLVRestBlock{
  2841. TLVList: wire.TLVList{
  2842. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2843. Message: ICQMetaRequest{
  2844. ICQMetadata: wire.ICQMetadata{
  2845. ReqType: wire.ICQDBQueryMetaReq,
  2846. Seq: 1,
  2847. },
  2848. ReqSubType: wire.ICQDBQueryMetaReqSetEmails,
  2849. MetaRequest: wire.ICQ_0x07D0_0x040B_DBQueryMetaReqSetEmails{
  2850. Emails: []struct {
  2851. Publish uint8
  2852. Email string `oscar:"len_prefix=uint16,nullterm"`
  2853. }{
  2854. {
  2855. Email: "test@aol.com",
  2856. },
  2857. },
  2858. },
  2859. },
  2860. }),
  2861. },
  2862. },
  2863. },
  2864. seq: 1,
  2865. },
  2866. allMockParams: allMockParams{
  2867. setEmails: &mockParam{
  2868. req: wire.ICQ_0x07D0_0x040B_DBQueryMetaReqSetEmails{
  2869. Emails: []struct {
  2870. Publish uint8
  2871. Email string `oscar:"len_prefix=uint16,nullterm"`
  2872. }{
  2873. {
  2874. Email: "test@aol.com",
  2875. },
  2876. },
  2877. },
  2878. },
  2879. },
  2880. },
  2881. {
  2882. name: "MetaReqSetInterests - happy path",
  2883. reqParams: reqParams{
  2884. sess: &state.Session{},
  2885. inBody: wire.SNAC_0x15_0x02_BQuery{
  2886. TLVRestBlock: wire.TLVRestBlock{
  2887. TLVList: wire.TLVList{
  2888. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2889. Message: ICQMetaRequest{
  2890. ICQMetadata: wire.ICQMetadata{
  2891. ReqType: wire.ICQDBQueryMetaReq,
  2892. Seq: 1,
  2893. },
  2894. ReqSubType: wire.ICQDBQueryMetaReqSetInterests,
  2895. MetaRequest: wire.ICQ_0x07D0_0x0410_DBQueryMetaReqSetInterests{
  2896. Interests: []struct {
  2897. Code uint16
  2898. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  2899. }{
  2900. {
  2901. Keyword: "an_interest",
  2902. },
  2903. },
  2904. },
  2905. },
  2906. }),
  2907. },
  2908. },
  2909. },
  2910. seq: 1,
  2911. },
  2912. allMockParams: allMockParams{
  2913. setInterests: &mockParam{
  2914. req: wire.ICQ_0x07D0_0x0410_DBQueryMetaReqSetInterests{
  2915. Interests: []struct {
  2916. Code uint16
  2917. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  2918. }{
  2919. {
  2920. Keyword: "an_interest",
  2921. },
  2922. },
  2923. },
  2924. },
  2925. },
  2926. },
  2927. {
  2928. name: "MetaReqSetAffiliations - happy path",
  2929. reqParams: reqParams{
  2930. sess: &state.Session{},
  2931. inBody: wire.SNAC_0x15_0x02_BQuery{
  2932. TLVRestBlock: wire.TLVRestBlock{
  2933. TLVList: wire.TLVList{
  2934. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2935. Message: ICQMetaRequest{
  2936. ICQMetadata: wire.ICQMetadata{
  2937. ReqType: wire.ICQDBQueryMetaReq,
  2938. Seq: 1,
  2939. },
  2940. ReqSubType: wire.ICQDBQueryMetaReqSetAffiliations,
  2941. MetaRequest: wire.ICQ_0x07D0_0x041A_DBQueryMetaReqSetAffiliations{
  2942. PastAffiliations: []struct {
  2943. Code uint16
  2944. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  2945. }{
  2946. {
  2947. Keyword: "a_past_affiliation",
  2948. },
  2949. },
  2950. Affiliations: []struct {
  2951. Code uint16
  2952. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  2953. }{
  2954. {
  2955. Keyword: "an_affiliation",
  2956. },
  2957. },
  2958. },
  2959. },
  2960. }),
  2961. },
  2962. },
  2963. },
  2964. seq: 1,
  2965. },
  2966. allMockParams: allMockParams{
  2967. setAffiliations: &mockParam{
  2968. req: wire.ICQ_0x07D0_0x041A_DBQueryMetaReqSetAffiliations{
  2969. PastAffiliations: []struct {
  2970. Code uint16
  2971. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  2972. }{
  2973. {
  2974. Keyword: "a_past_affiliation",
  2975. },
  2976. },
  2977. Affiliations: []struct {
  2978. Code uint16
  2979. Keyword string `oscar:"len_prefix=uint16,nullterm"`
  2980. }{
  2981. {
  2982. Keyword: "an_affiliation",
  2983. },
  2984. },
  2985. },
  2986. },
  2987. },
  2988. },
  2989. {
  2990. name: "MetaReqStat - happy path",
  2991. reqParams: reqParams{
  2992. sess: &state.Session{},
  2993. inBody: wire.SNAC_0x15_0x02_BQuery{
  2994. TLVRestBlock: wire.TLVRestBlock{
  2995. TLVList: wire.TLVList{
  2996. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  2997. Message: ICQMetaRequest{
  2998. ICQMetadata: wire.ICQMetadata{
  2999. ReqType: wire.ICQDBQueryMetaReq,
  3000. Seq: 1,
  3001. },
  3002. ReqSubType: wire.ICQDBQueryMetaReqStat0a8c,
  3003. MetaRequest: struct{}{},
  3004. },
  3005. }),
  3006. },
  3007. },
  3008. },
  3009. seq: 1,
  3010. },
  3011. },
  3012. {
  3013. name: "unknown metadata request subtype",
  3014. reqParams: reqParams{
  3015. sess: &state.Session{},
  3016. inBody: wire.SNAC_0x15_0x02_BQuery{
  3017. TLVRestBlock: wire.TLVRestBlock{
  3018. TLVList: wire.TLVList{
  3019. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3020. Message: ICQMetaRequest{
  3021. ICQMetadata: wire.ICQMetadata{
  3022. ReqType: wire.ICQDBQueryMetaReq,
  3023. Seq: 1,
  3024. },
  3025. ReqSubType: 0xCA8E,
  3026. MetaRequest: struct{}{},
  3027. },
  3028. }),
  3029. },
  3030. },
  3031. },
  3032. seq: 1,
  3033. wantErr: errUnknownICQMetaReqSubType,
  3034. },
  3035. },
  3036. {
  3037. name: "OfflineMsgReq - happy path",
  3038. reqParams: reqParams{
  3039. sess: &state.Session{},
  3040. inBody: wire.SNAC_0x15_0x02_BQuery{
  3041. TLVRestBlock: wire.TLVRestBlock{
  3042. TLVList: wire.TLVList{
  3043. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3044. Message: wire.ICQMetadataWithSubType{
  3045. ICQMetadata: wire.ICQMetadata{
  3046. ReqType: wire.ICQDBQueryOfflineMsgReq,
  3047. Seq: 1,
  3048. },
  3049. },
  3050. }),
  3051. },
  3052. },
  3053. },
  3054. seq: 1,
  3055. },
  3056. allMockParams: allMockParams{
  3057. offlineMsgReq: &mockParam{},
  3058. },
  3059. },
  3060. {
  3061. name: "DeleteMsgReq - happy path",
  3062. reqParams: reqParams{
  3063. sess: &state.Session{},
  3064. inBody: wire.SNAC_0x15_0x02_BQuery{
  3065. TLVRestBlock: wire.TLVRestBlock{
  3066. TLVList: wire.TLVList{
  3067. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3068. Message: wire.ICQMetadataWithSubType{
  3069. ICQMetadata: wire.ICQMetadata{
  3070. ReqType: wire.ICQDBQueryDeleteMsgReq,
  3071. Seq: 1,
  3072. },
  3073. },
  3074. }),
  3075. },
  3076. },
  3077. },
  3078. seq: 1,
  3079. },
  3080. allMockParams: allMockParams{
  3081. deleteMsgReq: &mockParam{},
  3082. },
  3083. },
  3084. {
  3085. name: "unknown request type",
  3086. reqParams: reqParams{
  3087. sess: &state.Session{},
  3088. inBody: wire.SNAC_0x15_0x02_BQuery{
  3089. TLVRestBlock: wire.TLVRestBlock{
  3090. TLVList: wire.TLVList{
  3091. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3092. Message: ICQMetaRequest{
  3093. ICQMetadata: wire.ICQMetadata{
  3094. ReqType: 0x13B4,
  3095. Seq: 1,
  3096. },
  3097. ReqSubType: 0xCA8E,
  3098. MetaRequest: struct{}{},
  3099. },
  3100. }),
  3101. },
  3102. },
  3103. },
  3104. seq: 1,
  3105. wantErr: errUnknownICQMetaReqType,
  3106. },
  3107. }, // todo: add to a separate test
  3108. }
  3109. for _, tt := range tests {
  3110. t.Run(tt.name, func(t *testing.T) {
  3111. icqService := newMockICQService(t)
  3112. switch {
  3113. case tt.allMockParams.fullUserInfo != nil:
  3114. icqService.EXPECT().
  3115. FullUserInfo(mock.Anything, tt.reqParams.sess, tt.allMockParams.fullUserInfo.req, tt.reqParams.seq).
  3116. Return(tt.allMockParams.fullUserInfo.wantErr)
  3117. case tt.allMockParams.shortUserInfo != nil:
  3118. icqService.EXPECT().
  3119. ShortUserInfo(mock.Anything, tt.reqParams.sess, tt.allMockParams.shortUserInfo.req, tt.reqParams.seq).
  3120. Return(tt.allMockParams.shortUserInfo.wantErr)
  3121. case tt.allMockParams.xmlReqData != nil:
  3122. icqService.EXPECT().
  3123. XMLReqData(mock.Anything, tt.reqParams.sess, tt.allMockParams.xmlReqData.req, tt.reqParams.seq).
  3124. Return(tt.allMockParams.xmlReqData.wantErr)
  3125. case tt.allMockParams.setPermissions != nil:
  3126. icqService.EXPECT().
  3127. SetPermissions(mock.Anything, tt.reqParams.sess, tt.allMockParams.setPermissions.req, tt.reqParams.seq).
  3128. Return(tt.allMockParams.setPermissions.wantErr)
  3129. case tt.allMockParams.findByUIN != nil:
  3130. icqService.EXPECT().
  3131. FindByUIN(mock.Anything, tt.reqParams.sess, tt.allMockParams.findByUIN.req, tt.reqParams.seq).
  3132. Return(tt.allMockParams.findByUIN.wantErr)
  3133. case tt.allMockParams.findByUIN2 != nil:
  3134. icqService.EXPECT().
  3135. FindByUIN2(mock.Anything, tt.reqParams.sess, tt.allMockParams.findByUIN2.req, tt.reqParams.seq).
  3136. Return(tt.allMockParams.findByUIN2.wantErr)
  3137. case tt.allMockParams.findByEmail != nil:
  3138. icqService.EXPECT().
  3139. FindByICQEmail(mock.Anything, tt.reqParams.sess, tt.allMockParams.findByEmail.req, tt.reqParams.seq).
  3140. Return(tt.allMockParams.findByEmail.wantErr)
  3141. case tt.allMockParams.findByEmail3 != nil:
  3142. icqService.EXPECT().
  3143. FindByEmail3(mock.Anything, tt.reqParams.sess, tt.allMockParams.findByEmail3.req, tt.reqParams.seq).
  3144. Return(tt.allMockParams.findByEmail3.wantErr)
  3145. case tt.allMockParams.findByDetails != nil:
  3146. icqService.EXPECT().
  3147. FindByICQName(mock.Anything, tt.reqParams.sess, tt.allMockParams.findByDetails.req, tt.reqParams.seq).
  3148. Return(tt.allMockParams.findByDetails.wantErr)
  3149. case tt.allMockParams.findByInterests != nil:
  3150. icqService.EXPECT().
  3151. FindByICQInterests(mock.Anything, tt.reqParams.sess, tt.allMockParams.findByInterests.req, tt.reqParams.seq).
  3152. Return(tt.allMockParams.findByInterests.wantErr)
  3153. case tt.allMockParams.findByWhitePages2 != nil:
  3154. icqService.EXPECT().
  3155. FindByWhitePages2(mock.Anything, tt.reqParams.sess, tt.allMockParams.findByWhitePages2.req, tt.reqParams.seq).
  3156. Return(tt.allMockParams.findByWhitePages2.wantErr)
  3157. case tt.allMockParams.setBasicInfo != nil:
  3158. icqService.EXPECT().
  3159. SetBasicInfo(mock.Anything, tt.reqParams.sess, tt.allMockParams.setBasicInfo.req, tt.reqParams.seq).
  3160. Return(tt.allMockParams.setBasicInfo.wantErr)
  3161. case tt.allMockParams.setWorkInfo != nil:
  3162. icqService.EXPECT().
  3163. SetWorkInfo(mock.Anything, tt.reqParams.sess, tt.allMockParams.setWorkInfo.req, tt.reqParams.seq).
  3164. Return(tt.allMockParams.setWorkInfo.wantErr)
  3165. case tt.allMockParams.setMoreInfo != nil:
  3166. icqService.EXPECT().
  3167. SetMoreInfo(mock.Anything, tt.reqParams.sess, tt.allMockParams.setMoreInfo.req, tt.reqParams.seq).
  3168. Return(tt.allMockParams.setMoreInfo.wantErr)
  3169. case tt.allMockParams.setUserNotes != nil:
  3170. icqService.EXPECT().
  3171. SetUserNotes(mock.Anything, tt.reqParams.sess, tt.allMockParams.setUserNotes.req, tt.reqParams.seq).
  3172. Return(tt.allMockParams.setUserNotes.wantErr)
  3173. case tt.allMockParams.setEmails != nil:
  3174. icqService.EXPECT().
  3175. SetEmails(mock.Anything, tt.reqParams.sess, tt.allMockParams.setEmails.req, tt.reqParams.seq).
  3176. Return(tt.allMockParams.setEmails.wantErr)
  3177. case tt.allMockParams.setInterests != nil:
  3178. icqService.EXPECT().
  3179. SetInterests(mock.Anything, tt.reqParams.sess, tt.allMockParams.setInterests.req, tt.reqParams.seq).
  3180. Return(tt.allMockParams.setInterests.wantErr)
  3181. case tt.allMockParams.setAffiliations != nil:
  3182. icqService.EXPECT().
  3183. SetAffiliations(mock.Anything, tt.reqParams.sess, tt.allMockParams.setAffiliations.req, tt.reqParams.seq).
  3184. Return(tt.allMockParams.setAffiliations.wantErr)
  3185. case tt.allMockParams.offlineMsgReq != nil:
  3186. icqService.EXPECT().
  3187. OfflineMsgReq(mock.Anything, tt.reqParams.sess, tt.reqParams.seq).
  3188. Return(tt.allMockParams.offlineMsgReq.wantErr)
  3189. case tt.allMockParams.deleteMsgReq != nil:
  3190. icqService.EXPECT().
  3191. DeleteMsgReq(mock.Anything, tt.reqParams.sess, tt.reqParams.seq).
  3192. Return(tt.allMockParams.deleteMsgReq.wantErr)
  3193. }
  3194. h := Handler{
  3195. ICQService: icqService,
  3196. RouteLogger: middleware.RouteLogger{
  3197. Logger: slog.Default(),
  3198. },
  3199. }
  3200. buf := &bytes.Buffer{}
  3201. assert.NoError(t, wire.MarshalBE(tt.reqParams.inBody, buf))
  3202. frame := wire.SNACFrame{
  3203. FoodGroup: wire.ICQ,
  3204. SubGroup: wire.ICQDBQuery,
  3205. }
  3206. err := h.Handle(context.TODO(), wire.BOS, tt.reqParams.sess, frame, buf, nil, config.Listener{})
  3207. assert.ErrorIs(t, err, tt.reqParams.wantErr)
  3208. })
  3209. }
  3210. }
  3211. // Test workaround for QIP 2005 bug where TLV length is incorrect.
  3212. func TestHandler_ICQDBQuery_QIP2005UINSearchBug(t *testing.T) {
  3213. icqService := newMockICQService(t)
  3214. type ICQMetaRequest struct {
  3215. wire.ICQMetadata
  3216. ReqSubType uint16
  3217. MetaRequest any
  3218. }
  3219. expect := wire.ICQ_0x07D0_0x0569_DBQueryMetaReqSearchByUIN2{
  3220. TLVRestBlock: wire.TLVRestBlock{
  3221. TLVList: wire.TLVList{
  3222. wire.NewTLVBE(wire.ICQTLVTagsUIN, uint32(100009)),
  3223. },
  3224. },
  3225. }
  3226. icqService.EXPECT().
  3227. FindByUIN2(mock.Anything, &state.Session{}, expect, uint16(1)).
  3228. Return(nil)
  3229. h := Handler{
  3230. ICQService: icqService,
  3231. RouteLogger: middleware.RouteLogger{
  3232. Logger: slog.Default(),
  3233. },
  3234. }
  3235. inBody := wire.SNAC_0x15_0x02_BQuery{
  3236. TLVRestBlock: wire.TLVRestBlock{
  3237. TLVList: wire.TLVList{
  3238. wire.NewTLVBE(wire.ICQTLVTagsMetadata, wire.ICQMessageReplyEnvelope{
  3239. Message: ICQMetaRequest{
  3240. ICQMetadata: wire.ICQMetadata{
  3241. ReqType: wire.ICQDBQueryMetaReq,
  3242. Seq: 1,
  3243. },
  3244. ReqSubType: wire.ICQDBQueryMetaReqSearchByUIN2,
  3245. MetaRequest: wire.ICQ_0x07D0_0x0569_DBQueryMetaReqSearchByUIN2{
  3246. TLVRestBlock: wire.TLVRestBlock{
  3247. TLVList: wire.TLVList{
  3248. wire.NewTLVBE(wire.ICQTLVTagsUIN, uint32(100009)),
  3249. },
  3250. },
  3251. },
  3252. },
  3253. }),
  3254. },
  3255. },
  3256. }
  3257. buf := &bytes.Buffer{}
  3258. assert.NoError(t, wire.MarshalBE(inBody, buf))
  3259. b := buf.Bytes()
  3260. b[18] = 6 // incorrectly set TLV length to 6 (should be 4)
  3261. err := h.ICQDBQuery(nil, &state.Session{}, wire.SNACFrame{}, buf, nil)
  3262. assert.NoError(t, err)
  3263. }
  3264. func TestHandler_ODirInfoQuery(t *testing.T) {
  3265. tests := []struct {
  3266. name string
  3267. inputBody wire.SNAC_0x0F_0x02_InfoQuery
  3268. serviceError error
  3269. responseError error
  3270. expectedError error
  3271. }{
  3272. {
  3273. name: "success",
  3274. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3275. TLVRestBlock: wire.TLVRestBlock{
  3276. TLVList: wire.TLVList{
  3277. wire.NewTLVBE(1, uint16(2)),
  3278. },
  3279. },
  3280. },
  3281. },
  3282. {
  3283. name: "service error",
  3284. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3285. TLVRestBlock: wire.TLVRestBlock{
  3286. TLVList: wire.TLVList{
  3287. wire.NewTLVBE(1, uint16(2)),
  3288. },
  3289. },
  3290. },
  3291. serviceError: assert.AnError,
  3292. expectedError: assert.AnError,
  3293. },
  3294. {
  3295. name: "response writer error",
  3296. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3297. TLVRestBlock: wire.TLVRestBlock{
  3298. TLVList: wire.TLVList{
  3299. wire.NewTLVBE(1, uint16(2)),
  3300. },
  3301. },
  3302. },
  3303. responseError: assert.AnError,
  3304. expectedError: assert.AnError,
  3305. },
  3306. }
  3307. for _, tt := range tests {
  3308. t.Run(tt.name, func(t *testing.T) {
  3309. input := wire.SNACMessage{
  3310. Frame: wire.SNACFrame{
  3311. FoodGroup: wire.ODir,
  3312. SubGroup: wire.ODirInfoQuery,
  3313. },
  3314. Body: tt.inputBody,
  3315. }
  3316. output := wire.SNACMessage{
  3317. Frame: wire.SNACFrame{
  3318. FoodGroup: wire.ODir,
  3319. SubGroup: wire.ODirInfoReply,
  3320. },
  3321. Body: wire.SNAC_0x0F_0x03_InfoReply{
  3322. Status: 5, // OK has results/not found
  3323. },
  3324. }
  3325. svc := newMockODirService(t)
  3326. svc.EXPECT().
  3327. InfoQuery(mock.Anything, input.Frame, input.Body).
  3328. Return(output, tt.serviceError)
  3329. h := Handler{
  3330. ODirService: svc,
  3331. RouteLogger: middleware.RouteLogger{
  3332. Logger: slog.Default(),
  3333. },
  3334. }
  3335. ss := newMockResponseWriter(t)
  3336. if tt.serviceError == nil {
  3337. ss.EXPECT().
  3338. SendSNAC(output.Frame, output.Body).
  3339. Return(tt.responseError)
  3340. }
  3341. buf := &bytes.Buffer{}
  3342. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3343. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, ss, config.Listener{})
  3344. if tt.expectedError != nil {
  3345. assert.ErrorIs(t, err, tt.expectedError)
  3346. } else {
  3347. assert.NoError(t, err)
  3348. }
  3349. })
  3350. }
  3351. }
  3352. func TestHandler_ODirKeywordListQuery(t *testing.T) {
  3353. tests := []struct {
  3354. name string
  3355. inputBody wire.SNAC_0x0F_0x02_InfoQuery
  3356. serviceError error
  3357. responseError error
  3358. expectedError error
  3359. }{
  3360. {
  3361. name: "success",
  3362. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3363. TLVRestBlock: wire.TLVRestBlock{
  3364. TLVList: wire.TLVList{
  3365. wire.NewTLVBE(1, uint16(2)),
  3366. },
  3367. },
  3368. },
  3369. },
  3370. {
  3371. name: "service error",
  3372. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3373. TLVRestBlock: wire.TLVRestBlock{
  3374. TLVList: wire.TLVList{
  3375. wire.NewTLVBE(1, uint16(2)),
  3376. },
  3377. },
  3378. },
  3379. serviceError: assert.AnError,
  3380. expectedError: assert.AnError,
  3381. },
  3382. {
  3383. name: "response writer error",
  3384. inputBody: wire.SNAC_0x0F_0x02_InfoQuery{
  3385. TLVRestBlock: wire.TLVRestBlock{
  3386. TLVList: wire.TLVList{
  3387. wire.NewTLVBE(1, uint16(2)),
  3388. },
  3389. },
  3390. },
  3391. responseError: assert.AnError,
  3392. expectedError: assert.AnError,
  3393. },
  3394. }
  3395. for _, tt := range tests {
  3396. t.Run(tt.name, func(t *testing.T) {
  3397. input := wire.SNACMessage{
  3398. Frame: wire.SNACFrame{
  3399. FoodGroup: wire.ODir,
  3400. SubGroup: wire.ODirKeywordListQuery,
  3401. },
  3402. Body: tt.inputBody,
  3403. }
  3404. output := wire.SNACMessage{
  3405. Frame: wire.SNACFrame{
  3406. FoodGroup: wire.ODir,
  3407. SubGroup: wire.ODirKeywordListReply,
  3408. },
  3409. Body: wire.SNAC_0x0F_0x04_KeywordListReply{
  3410. Status: 0x01,
  3411. },
  3412. }
  3413. svc := newMockODirService(t)
  3414. svc.EXPECT().
  3415. KeywordListQuery(mock.Anything, input.Frame).
  3416. Return(output, tt.serviceError)
  3417. h := Handler{
  3418. ODirService: svc,
  3419. RouteLogger: middleware.RouteLogger{
  3420. Logger: slog.Default(),
  3421. },
  3422. }
  3423. ss := newMockResponseWriter(t)
  3424. if tt.serviceError == nil {
  3425. ss.EXPECT().
  3426. SendSNAC(output.Frame, output.Body).
  3427. Return(tt.responseError)
  3428. }
  3429. buf := &bytes.Buffer{}
  3430. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3431. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, ss, config.Listener{})
  3432. if tt.expectedError != nil {
  3433. assert.ErrorIs(t, err, tt.expectedError)
  3434. } else {
  3435. assert.NoError(t, err)
  3436. }
  3437. })
  3438. }
  3439. }
  3440. func TestHandler_OServiceServiceClientOnline(t *testing.T) {
  3441. tests := []struct {
  3442. name string
  3443. inputBody wire.SNAC_0x01_0x02_OServiceClientOnline
  3444. serviceError error
  3445. expectedError error
  3446. }{
  3447. {
  3448. name: "success",
  3449. inputBody: wire.SNAC_0x01_0x02_OServiceClientOnline{
  3450. GroupVersions: []struct {
  3451. FoodGroup uint16
  3452. Version uint16
  3453. ToolID uint16
  3454. ToolVersion uint16
  3455. }{
  3456. {
  3457. FoodGroup: 10,
  3458. },
  3459. },
  3460. },
  3461. },
  3462. {
  3463. name: "service error",
  3464. inputBody: wire.SNAC_0x01_0x02_OServiceClientOnline{
  3465. GroupVersions: []struct {
  3466. FoodGroup uint16
  3467. Version uint16
  3468. ToolID uint16
  3469. ToolVersion uint16
  3470. }{
  3471. {
  3472. FoodGroup: 10,
  3473. },
  3474. },
  3475. },
  3476. serviceError: assert.AnError,
  3477. expectedError: assert.AnError,
  3478. },
  3479. }
  3480. for _, tt := range tests {
  3481. t.Run(tt.name, func(t *testing.T) {
  3482. input := wire.SNACMessage{
  3483. Frame: wire.SNACFrame{
  3484. FoodGroup: wire.OService,
  3485. SubGroup: wire.OServiceClientOnline,
  3486. },
  3487. Body: tt.inputBody,
  3488. }
  3489. svc := newMockOServiceService(t)
  3490. svc.EXPECT().
  3491. ClientOnline(mock.Anything, wire.BOS, input.Body, mock.Anything).
  3492. Return(tt.serviceError)
  3493. h := Handler{
  3494. OServiceService: svc,
  3495. RouteLogger: middleware.RouteLogger{
  3496. Logger: slog.Default(),
  3497. },
  3498. }
  3499. buf := &bytes.Buffer{}
  3500. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3501. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, nil, config.Listener{})
  3502. if tt.expectedError != nil {
  3503. assert.ErrorIs(t, err, tt.expectedError)
  3504. } else {
  3505. assert.NoError(t, err)
  3506. }
  3507. })
  3508. }
  3509. }
  3510. func TestHandler_OServiceServiceServiceRequest(t *testing.T) {
  3511. tests := []struct {
  3512. name string
  3513. inputBody wire.SNAC_0x01_0x04_OServiceServiceRequest
  3514. serviceError error
  3515. responseError error
  3516. expectedError error
  3517. }{
  3518. {
  3519. name: "success",
  3520. inputBody: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  3521. FoodGroup: wire.Chat,
  3522. },
  3523. },
  3524. {
  3525. name: "service error",
  3526. inputBody: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  3527. FoodGroup: wire.Chat,
  3528. },
  3529. serviceError: assert.AnError,
  3530. expectedError: assert.AnError,
  3531. },
  3532. {
  3533. name: "response writer error",
  3534. inputBody: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  3535. FoodGroup: wire.Chat,
  3536. },
  3537. responseError: assert.AnError,
  3538. expectedError: assert.AnError,
  3539. },
  3540. }
  3541. for _, tt := range tests {
  3542. t.Run(tt.name, func(t *testing.T) {
  3543. input := wire.SNACMessage{
  3544. Frame: wire.SNACFrame{
  3545. FoodGroup: wire.OService,
  3546. SubGroup: wire.OServiceServiceRequest,
  3547. },
  3548. Body: tt.inputBody,
  3549. }
  3550. output := wire.SNACMessage{
  3551. Frame: wire.SNACFrame{
  3552. FoodGroup: wire.OService,
  3553. SubGroup: wire.OServiceServiceResponse,
  3554. },
  3555. Body: wire.SNAC_0x01_0x05_OServiceServiceResponse{
  3556. TLVRestBlock: wire.TLVRestBlock{
  3557. TLVList: wire.TLVList{
  3558. wire.NewTLVBE(0x01, uint16(1000)),
  3559. },
  3560. },
  3561. },
  3562. }
  3563. svc := newMockOServiceService(t)
  3564. svc.EXPECT().
  3565. ServiceRequest(mock.Anything, wire.BOS, mock.Anything, input.Frame, input.Body, config.Listener{BOSAdvertisedHostPlain: "127.0.0.1:1234"}).
  3566. Return(output, tt.serviceError)
  3567. h := Handler{
  3568. OServiceService: svc,
  3569. RouteLogger: middleware.RouteLogger{
  3570. Logger: slog.Default(),
  3571. },
  3572. }
  3573. responseWriter := newMockResponseWriter(t)
  3574. if tt.serviceError == nil {
  3575. responseWriter.EXPECT().
  3576. SendSNAC(output.Frame, output.Body).
  3577. Return(tt.responseError)
  3578. }
  3579. buf := &bytes.Buffer{}
  3580. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3581. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{BOSAdvertisedHostPlain: "127.0.0.1:1234"})
  3582. if tt.expectedError != nil {
  3583. assert.ErrorIs(t, err, tt.expectedError)
  3584. } else {
  3585. assert.NoError(t, err)
  3586. }
  3587. })
  3588. }
  3589. }
  3590. func TestHandler_OServiceServiceIdleNotification(t *testing.T) {
  3591. tests := []struct {
  3592. name string
  3593. inputBody wire.SNAC_0x01_0x11_OServiceIdleNotification
  3594. serviceError error
  3595. expectedError error
  3596. }{
  3597. {
  3598. name: "success",
  3599. inputBody: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  3600. IdleTime: 10,
  3601. },
  3602. },
  3603. {
  3604. name: "service error",
  3605. inputBody: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  3606. IdleTime: 10,
  3607. },
  3608. serviceError: assert.AnError,
  3609. expectedError: assert.AnError,
  3610. },
  3611. }
  3612. for _, tt := range tests {
  3613. t.Run(tt.name, func(t *testing.T) {
  3614. input := wire.SNACMessage{
  3615. Frame: wire.SNACFrame{
  3616. FoodGroup: wire.OService,
  3617. SubGroup: wire.OServiceIdleNotification,
  3618. },
  3619. Body: tt.inputBody,
  3620. }
  3621. svc := newMockOServiceService(t)
  3622. svc.EXPECT().
  3623. IdleNotification(mock.Anything, mock.Anything, input.Body).
  3624. Return(tt.serviceError)
  3625. h := Handler{
  3626. OServiceService: svc,
  3627. RouteLogger: middleware.RouteLogger{
  3628. Logger: slog.Default(),
  3629. },
  3630. }
  3631. responseWriter := newMockResponseWriter(t)
  3632. buf := &bytes.Buffer{}
  3633. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3634. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  3635. if tt.expectedError != nil {
  3636. assert.ErrorIs(t, err, tt.expectedError)
  3637. } else {
  3638. assert.NoError(t, err)
  3639. }
  3640. })
  3641. }
  3642. }
  3643. func TestHandler_OServiceServiceClientVersions(t *testing.T) {
  3644. tests := []struct {
  3645. name string
  3646. inputBody wire.SNAC_0x01_0x17_OServiceClientVersions
  3647. responseError error
  3648. expectedError error
  3649. }{
  3650. {
  3651. name: "success",
  3652. inputBody: wire.SNAC_0x01_0x17_OServiceClientVersions{
  3653. Versions: []uint16{
  3654. 10,
  3655. },
  3656. },
  3657. },
  3658. {
  3659. name: "response writer error",
  3660. inputBody: wire.SNAC_0x01_0x17_OServiceClientVersions{
  3661. Versions: []uint16{
  3662. 10,
  3663. },
  3664. },
  3665. responseError: assert.AnError,
  3666. expectedError: assert.AnError,
  3667. },
  3668. }
  3669. for _, tt := range tests {
  3670. t.Run(tt.name, func(t *testing.T) {
  3671. input := wire.SNACMessage{
  3672. Frame: wire.SNACFrame{
  3673. FoodGroup: wire.OService,
  3674. SubGroup: wire.OServiceClientVersions,
  3675. },
  3676. Body: tt.inputBody,
  3677. }
  3678. output := wire.SNACMessage{
  3679. Frame: wire.SNACFrame{
  3680. FoodGroup: wire.OService,
  3681. SubGroup: wire.OServiceHostVersions,
  3682. },
  3683. Body: wire.SNAC_0x01_0x18_OServiceHostVersions{
  3684. Versions: []uint16{
  3685. 10,
  3686. },
  3687. },
  3688. }
  3689. sess := state.NewSession()
  3690. svc := newMockOServiceService(t)
  3691. svc.EXPECT().
  3692. ClientVersions(mock.Anything, sess, input.Frame, input.Body).
  3693. Return(output)
  3694. h := Handler{
  3695. OServiceService: svc,
  3696. RouteLogger: middleware.RouteLogger{
  3697. Logger: slog.Default(),
  3698. },
  3699. }
  3700. responseWriter := newMockResponseWriter(t)
  3701. responseWriter.EXPECT().
  3702. SendSNAC(output.Frame, output.Body).
  3703. Return(tt.responseError)
  3704. buf := &bytes.Buffer{}
  3705. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3706. err := h.Handle(context.Background(), wire.BOS, sess, input.Frame, buf, responseWriter, config.Listener{})
  3707. if tt.expectedError != nil {
  3708. assert.ErrorIs(t, err, tt.expectedError)
  3709. } else {
  3710. assert.NoError(t, err)
  3711. }
  3712. })
  3713. }
  3714. }
  3715. func TestHandler_OServiceServiceRateParamsQuery(t *testing.T) {
  3716. tests := []struct {
  3717. name string
  3718. inputBody struct{}
  3719. responseError error
  3720. expectedError error
  3721. }{
  3722. {
  3723. name: "success",
  3724. inputBody: struct{}{},
  3725. },
  3726. {
  3727. name: "response writer error",
  3728. inputBody: struct{}{},
  3729. responseError: assert.AnError,
  3730. expectedError: assert.AnError,
  3731. },
  3732. }
  3733. for _, tt := range tests {
  3734. t.Run(tt.name, func(t *testing.T) {
  3735. input := wire.SNACMessage{
  3736. Frame: wire.SNACFrame{
  3737. FoodGroup: wire.OService,
  3738. SubGroup: wire.OServiceRateParamsQuery,
  3739. },
  3740. Body: tt.inputBody,
  3741. }
  3742. output := wire.SNACMessage{
  3743. Frame: wire.SNACFrame{
  3744. FoodGroup: wire.OService,
  3745. SubGroup: wire.OServiceRateParamsReply,
  3746. },
  3747. Body: wire.SNAC_0x01_0x07_OServiceRateParamsReply{
  3748. RateGroups: []struct {
  3749. ID uint16
  3750. Pairs []struct {
  3751. FoodGroup uint16
  3752. SubGroup uint16
  3753. } `oscar:"count_prefix=uint16"`
  3754. }{
  3755. {
  3756. ID: 1,
  3757. },
  3758. },
  3759. },
  3760. }
  3761. svc := newMockOServiceService(t)
  3762. svc.EXPECT().
  3763. RateParamsQuery(mock.Anything, mock.Anything, input.Frame).
  3764. Return(output)
  3765. h := Handler{
  3766. OServiceService: svc,
  3767. RouteLogger: middleware.RouteLogger{
  3768. Logger: slog.Default(),
  3769. },
  3770. }
  3771. responseWriter := newMockResponseWriter(t)
  3772. responseWriter.EXPECT().
  3773. SendSNAC(output.Frame, output.Body).
  3774. Return(tt.responseError)
  3775. buf := &bytes.Buffer{}
  3776. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3777. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  3778. if tt.expectedError != nil {
  3779. assert.ErrorIs(t, err, tt.expectedError)
  3780. } else {
  3781. assert.NoError(t, err)
  3782. }
  3783. })
  3784. }
  3785. }
  3786. func TestHandler_OServiceServiceRateParamsSubAdd(t *testing.T) {
  3787. tests := []struct {
  3788. name string
  3789. inputBody wire.SNAC_0x01_0x08_OServiceRateParamsSubAdd
  3790. expectedError error
  3791. }{
  3792. {
  3793. name: "success",
  3794. inputBody: wire.SNAC_0x01_0x08_OServiceRateParamsSubAdd{
  3795. ClassIDs: []uint16{1, 2, 3, 4},
  3796. },
  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.OServiceRateParamsSubAdd,
  3805. },
  3806. Body: tt.inputBody,
  3807. }
  3808. sess := state.NewSession()
  3809. svc := newMockOServiceService(t)
  3810. svc.EXPECT().
  3811. RateParamsSubAdd(mock.Anything, sess, input.Body)
  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.Background(), wire.BOS, sess, 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_OServiceServiceSetUserInfoFields(t *testing.T) {
  3830. tests := []struct {
  3831. name string
  3832. inputBody wire.SNAC_0x01_0x1E_OServiceSetUserInfoFields
  3833. serviceError error
  3834. responseError error
  3835. expectedError error
  3836. }{
  3837. {
  3838. name: "success",
  3839. inputBody: wire.SNAC_0x01_0x1E_OServiceSetUserInfoFields{
  3840. TLVRestBlock: wire.TLVRestBlock{
  3841. TLVList: wire.TLVList{
  3842. wire.NewTLVBE(0x01, []byte{1, 2, 3, 4}),
  3843. },
  3844. },
  3845. },
  3846. },
  3847. {
  3848. name: "service error",
  3849. inputBody: wire.SNAC_0x01_0x1E_OServiceSetUserInfoFields{
  3850. TLVRestBlock: wire.TLVRestBlock{
  3851. TLVList: wire.TLVList{
  3852. wire.NewTLVBE(0x01, []byte{1, 2, 3, 4}),
  3853. },
  3854. },
  3855. },
  3856. serviceError: assert.AnError,
  3857. expectedError: assert.AnError,
  3858. },
  3859. {
  3860. name: "response writer error",
  3861. inputBody: wire.SNAC_0x01_0x1E_OServiceSetUserInfoFields{
  3862. TLVRestBlock: wire.TLVRestBlock{
  3863. TLVList: wire.TLVList{
  3864. wire.NewTLVBE(0x01, []byte{1, 2, 3, 4}),
  3865. },
  3866. },
  3867. },
  3868. responseError: assert.AnError,
  3869. expectedError: assert.AnError,
  3870. },
  3871. }
  3872. for _, tt := range tests {
  3873. t.Run(tt.name, func(t *testing.T) {
  3874. input := wire.SNACMessage{
  3875. Frame: wire.SNACFrame{
  3876. FoodGroup: wire.OService,
  3877. SubGroup: wire.OServiceSetUserInfoFields,
  3878. },
  3879. Body: tt.inputBody,
  3880. }
  3881. output := wire.SNACMessage{
  3882. Frame: wire.SNACFrame{
  3883. FoodGroup: wire.OService,
  3884. SubGroup: wire.OServiceUserInfoUpdate,
  3885. },
  3886. Body: wire.SNAC_0x01_0x0F_OServiceUserInfoUpdate{
  3887. UserInfo: []wire.TLVUserInfo{
  3888. {ScreenName: "screen-name"},
  3889. {ScreenName: "screen-name"},
  3890. },
  3891. },
  3892. }
  3893. svc := newMockOServiceService(t)
  3894. svc.EXPECT().
  3895. SetUserInfoFields(mock.Anything, mock.Anything, input.Frame, input.Body).
  3896. Return(output, tt.serviceError)
  3897. h := Handler{
  3898. OServiceService: svc,
  3899. RouteLogger: middleware.RouteLogger{
  3900. Logger: slog.Default(),
  3901. },
  3902. }
  3903. responseWriter := newMockResponseWriter(t)
  3904. if tt.serviceError == nil {
  3905. responseWriter.EXPECT().
  3906. SendSNAC(output.Frame, output.Body).
  3907. Return(tt.responseError)
  3908. }
  3909. buf := &bytes.Buffer{}
  3910. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3911. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  3912. if tt.expectedError != nil {
  3913. assert.ErrorIs(t, err, tt.expectedError)
  3914. } else {
  3915. assert.NoError(t, err)
  3916. }
  3917. })
  3918. }
  3919. }
  3920. func TestHandler_OServiceServiceUserInfoQuery(t *testing.T) {
  3921. tests := []struct {
  3922. name string
  3923. inputBody struct{}
  3924. responseError error
  3925. expectedError error
  3926. }{
  3927. {
  3928. name: "success",
  3929. inputBody: struct{}{},
  3930. },
  3931. {
  3932. name: "response writer error",
  3933. inputBody: struct{}{},
  3934. responseError: assert.AnError,
  3935. expectedError: assert.AnError,
  3936. },
  3937. }
  3938. for _, tt := range tests {
  3939. t.Run(tt.name, func(t *testing.T) {
  3940. input := wire.SNACMessage{
  3941. Frame: wire.SNACFrame{
  3942. FoodGroup: wire.OService,
  3943. SubGroup: wire.OServiceUserInfoQuery,
  3944. },
  3945. Body: tt.inputBody,
  3946. }
  3947. output := wire.SNACMessage{
  3948. Frame: wire.SNACFrame{
  3949. FoodGroup: wire.OService,
  3950. SubGroup: wire.OServiceUserInfoUpdate,
  3951. },
  3952. Body: wire.SNAC_0x01_0x0F_OServiceUserInfoUpdate{
  3953. UserInfo: []wire.TLVUserInfo{
  3954. {ScreenName: "screen-name"},
  3955. {ScreenName: "screen-name"},
  3956. },
  3957. },
  3958. }
  3959. svc := newMockOServiceService(t)
  3960. svc.EXPECT().
  3961. UserInfoQuery(mock.Anything, mock.Anything, input.Frame).
  3962. Return(output)
  3963. h := Handler{
  3964. OServiceService: svc,
  3965. RouteLogger: middleware.RouteLogger{
  3966. Logger: slog.Default(),
  3967. },
  3968. }
  3969. responseWriter := newMockResponseWriter(t)
  3970. responseWriter.EXPECT().
  3971. SendSNAC(output.Frame, output.Body).
  3972. Return(tt.responseError)
  3973. buf := &bytes.Buffer{}
  3974. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  3975. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  3976. if tt.expectedError != nil {
  3977. assert.ErrorIs(t, err, tt.expectedError)
  3978. } else {
  3979. assert.NoError(t, err)
  3980. }
  3981. })
  3982. }
  3983. }
  3984. func TestHandler_OServiceServiceNoop(t *testing.T) {
  3985. tests := []struct {
  3986. name string
  3987. inputBody struct{}
  3988. expectedError error
  3989. }{
  3990. {
  3991. name: "success",
  3992. inputBody: struct{}{},
  3993. },
  3994. }
  3995. for _, tt := range tests {
  3996. t.Run(tt.name, func(t *testing.T) {
  3997. input := wire.SNACMessage{
  3998. Frame: wire.SNACFrame{
  3999. FoodGroup: wire.OService,
  4000. SubGroup: wire.OServiceNoop,
  4001. },
  4002. Body: tt.inputBody,
  4003. }
  4004. h := Handler{
  4005. RouteLogger: middleware.RouteLogger{
  4006. Logger: slog.Default(),
  4007. },
  4008. }
  4009. responseWriter := newMockResponseWriter(t)
  4010. buf := &bytes.Buffer{}
  4011. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4012. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4013. if tt.expectedError != nil {
  4014. assert.ErrorIs(t, err, tt.expectedError)
  4015. } else {
  4016. assert.NoError(t, err)
  4017. }
  4018. })
  4019. }
  4020. }
  4021. func TestHandler_OServiceServiceSetPrivacyFlags(t *testing.T) {
  4022. tests := []struct {
  4023. name string
  4024. inputBody wire.SNAC_0x01_0x14_OServiceSetPrivacyFlags
  4025. expectedError error
  4026. }{
  4027. {
  4028. name: "success",
  4029. inputBody: wire.SNAC_0x01_0x14_OServiceSetPrivacyFlags{
  4030. PrivacyFlags: wire.OServicePrivacyFlagMember,
  4031. },
  4032. },
  4033. }
  4034. for _, tt := range tests {
  4035. t.Run(tt.name, func(t *testing.T) {
  4036. input := wire.SNACMessage{
  4037. Frame: wire.SNACFrame{
  4038. FoodGroup: wire.OService,
  4039. SubGroup: wire.OServiceSetPrivacyFlags,
  4040. },
  4041. Body: tt.inputBody,
  4042. }
  4043. svc := newMockOServiceService(t)
  4044. svc.EXPECT().
  4045. SetPrivacyFlags(mock.Anything, input.Body)
  4046. h := Handler{
  4047. OServiceService: svc,
  4048. RouteLogger: middleware.RouteLogger{
  4049. Logger: slog.Default(),
  4050. },
  4051. }
  4052. responseWriter := newMockResponseWriter(t)
  4053. buf := &bytes.Buffer{}
  4054. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4055. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4056. if tt.expectedError != nil {
  4057. assert.ErrorIs(t, err, tt.expectedError)
  4058. } else {
  4059. assert.NoError(t, err)
  4060. }
  4061. })
  4062. }
  4063. }
  4064. func TestHandler_PermitDenyRightsQuery(t *testing.T) {
  4065. tests := []struct {
  4066. name string
  4067. inputBody struct{}
  4068. responseError error
  4069. expectedError error
  4070. }{
  4071. {
  4072. name: "success",
  4073. inputBody: struct{}{},
  4074. },
  4075. {
  4076. name: "response writer error",
  4077. inputBody: struct{}{},
  4078. responseError: assert.AnError,
  4079. expectedError: assert.AnError,
  4080. },
  4081. }
  4082. for _, tt := range tests {
  4083. t.Run(tt.name, func(t *testing.T) {
  4084. input := wire.SNACMessage{
  4085. Frame: wire.SNACFrame{
  4086. FoodGroup: wire.PermitDeny,
  4087. SubGroup: wire.PermitDenyRightsQuery,
  4088. },
  4089. Body: tt.inputBody,
  4090. }
  4091. output := wire.SNACMessage{
  4092. Frame: wire.SNACFrame{
  4093. FoodGroup: wire.PermitDeny,
  4094. SubGroup: wire.PermitDenyRightsReply,
  4095. },
  4096. Body: wire.SNAC_0x09_0x03_PermitDenyRightsReply{
  4097. TLVRestBlock: wire.TLVRestBlock{
  4098. TLVList: wire.TLVList{
  4099. wire.NewTLVBE(0x01, uint16(1000)),
  4100. },
  4101. },
  4102. },
  4103. }
  4104. svc := newMockPermitDenyService(t)
  4105. svc.EXPECT().
  4106. RightsQuery(mock.Anything, input.Frame).
  4107. Return(output)
  4108. h := Handler{
  4109. PermitDenyService: svc,
  4110. RouteLogger: middleware.RouteLogger{
  4111. Logger: slog.Default(),
  4112. },
  4113. }
  4114. responseWriter := newMockResponseWriter(t)
  4115. responseWriter.EXPECT().
  4116. SendSNAC(output.Frame, output.Body).
  4117. Return(tt.responseError)
  4118. buf := &bytes.Buffer{}
  4119. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4120. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4121. if tt.expectedError != nil {
  4122. assert.ErrorIs(t, err, tt.expectedError)
  4123. } else {
  4124. assert.NoError(t, err)
  4125. }
  4126. })
  4127. }
  4128. }
  4129. func TestHandler_PermitDenyAddDenyListEntries(t *testing.T) {
  4130. tests := []struct {
  4131. name string
  4132. inputBody wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries
  4133. serviceError error
  4134. expectedError error
  4135. }{
  4136. {
  4137. name: "success",
  4138. inputBody: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  4139. Users: []struct {
  4140. ScreenName string `oscar:"len_prefix=uint8"`
  4141. }{
  4142. {
  4143. ScreenName: "friend1",
  4144. },
  4145. {
  4146. ScreenName: "friend2",
  4147. },
  4148. },
  4149. },
  4150. },
  4151. {
  4152. name: "service error",
  4153. inputBody: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  4154. Users: []struct {
  4155. ScreenName string `oscar:"len_prefix=uint8"`
  4156. }{
  4157. {
  4158. ScreenName: "friend1",
  4159. },
  4160. {
  4161. ScreenName: "friend2",
  4162. },
  4163. },
  4164. },
  4165. serviceError: assert.AnError,
  4166. expectedError: assert.AnError,
  4167. },
  4168. }
  4169. for _, tt := range tests {
  4170. t.Run(tt.name, func(t *testing.T) {
  4171. sess := state.NewSession()
  4172. input := wire.SNACMessage{
  4173. Frame: wire.SNACFrame{
  4174. FoodGroup: wire.PermitDeny,
  4175. SubGroup: wire.PermitDenyAddDenyListEntries,
  4176. },
  4177. Body: tt.inputBody,
  4178. }
  4179. svc := newMockPermitDenyService(t)
  4180. svc.EXPECT().
  4181. AddDenyListEntries(mock.Anything, sess, input.Body).
  4182. Return(tt.serviceError)
  4183. h := Handler{
  4184. PermitDenyService: svc,
  4185. RouteLogger: middleware.RouteLogger{
  4186. Logger: slog.Default(),
  4187. },
  4188. }
  4189. buf := &bytes.Buffer{}
  4190. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4191. err := h.Handle(context.TODO(), wire.BOS, sess, input.Frame, buf, nil, config.Listener{})
  4192. if tt.expectedError != nil {
  4193. assert.ErrorIs(t, err, tt.expectedError)
  4194. } else {
  4195. assert.NoError(t, err)
  4196. }
  4197. })
  4198. }
  4199. }
  4200. func TestHandler_PermitDenyDelDenyListEntries(t *testing.T) {
  4201. tests := []struct {
  4202. name string
  4203. inputBody wire.SNAC_0x09_0x08_PermitDenyDelDenyListEntries
  4204. serviceError error
  4205. expectedError error
  4206. }{
  4207. {
  4208. name: "success",
  4209. inputBody: wire.SNAC_0x09_0x08_PermitDenyDelDenyListEntries{
  4210. Users: []struct {
  4211. ScreenName string `oscar:"len_prefix=uint8"`
  4212. }{
  4213. {
  4214. ScreenName: "friend1",
  4215. },
  4216. {
  4217. ScreenName: "friend2",
  4218. },
  4219. },
  4220. },
  4221. },
  4222. {
  4223. name: "service error",
  4224. inputBody: wire.SNAC_0x09_0x08_PermitDenyDelDenyListEntries{
  4225. Users: []struct {
  4226. ScreenName string `oscar:"len_prefix=uint8"`
  4227. }{
  4228. {
  4229. ScreenName: "friend1",
  4230. },
  4231. {
  4232. ScreenName: "friend2",
  4233. },
  4234. },
  4235. },
  4236. serviceError: assert.AnError,
  4237. expectedError: assert.AnError,
  4238. },
  4239. }
  4240. for _, tt := range tests {
  4241. t.Run(tt.name, func(t *testing.T) {
  4242. sess := state.NewSession()
  4243. input := wire.SNACMessage{
  4244. Frame: wire.SNACFrame{
  4245. FoodGroup: wire.PermitDeny,
  4246. SubGroup: wire.PermitDenyDelDenyListEntries,
  4247. },
  4248. Body: tt.inputBody,
  4249. }
  4250. svc := newMockPermitDenyService(t)
  4251. svc.EXPECT().
  4252. DelDenyListEntries(mock.Anything, sess, input.Body).
  4253. Return(tt.serviceError)
  4254. h := Handler{
  4255. PermitDenyService: svc,
  4256. RouteLogger: middleware.RouteLogger{
  4257. Logger: slog.Default(),
  4258. },
  4259. }
  4260. buf := &bytes.Buffer{}
  4261. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4262. err := h.Handle(context.TODO(), wire.BOS, sess, input.Frame, buf, nil, config.Listener{})
  4263. if tt.expectedError != nil {
  4264. assert.ErrorIs(t, err, tt.expectedError)
  4265. } else {
  4266. assert.NoError(t, err)
  4267. }
  4268. })
  4269. }
  4270. }
  4271. func TestHandler_PermitDenyAddPermListEntries(t *testing.T) {
  4272. tests := []struct {
  4273. name string
  4274. inputBody wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries
  4275. serviceError error
  4276. expectedError error
  4277. }{
  4278. {
  4279. name: "success",
  4280. inputBody: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  4281. Users: []struct {
  4282. ScreenName string `oscar:"len_prefix=uint8"`
  4283. }{
  4284. {
  4285. ScreenName: "friend1",
  4286. },
  4287. {
  4288. ScreenName: "friend2",
  4289. },
  4290. },
  4291. },
  4292. },
  4293. {
  4294. name: "service error",
  4295. inputBody: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  4296. Users: []struct {
  4297. ScreenName string `oscar:"len_prefix=uint8"`
  4298. }{
  4299. {
  4300. ScreenName: "friend1",
  4301. },
  4302. {
  4303. ScreenName: "friend2",
  4304. },
  4305. },
  4306. },
  4307. serviceError: assert.AnError,
  4308. expectedError: assert.AnError,
  4309. },
  4310. }
  4311. for _, tt := range tests {
  4312. t.Run(tt.name, func(t *testing.T) {
  4313. sess := state.NewSession()
  4314. input := wire.SNACMessage{
  4315. Frame: wire.SNACFrame{
  4316. FoodGroup: wire.PermitDeny,
  4317. SubGroup: wire.PermitDenyAddPermListEntries,
  4318. },
  4319. Body: tt.inputBody,
  4320. }
  4321. svc := newMockPermitDenyService(t)
  4322. svc.EXPECT().
  4323. AddPermListEntries(mock.Anything, sess, input.Body).
  4324. Return(tt.serviceError)
  4325. h := Handler{
  4326. PermitDenyService: svc,
  4327. RouteLogger: middleware.RouteLogger{
  4328. Logger: slog.Default(),
  4329. },
  4330. }
  4331. buf := &bytes.Buffer{}
  4332. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4333. err := h.Handle(context.TODO(), wire.BOS, sess, input.Frame, buf, nil, config.Listener{})
  4334. if tt.expectedError != nil {
  4335. assert.ErrorIs(t, err, tt.expectedError)
  4336. } else {
  4337. assert.NoError(t, err)
  4338. }
  4339. })
  4340. }
  4341. }
  4342. func TestHandler_PermitDenyDelPermListEntries(t *testing.T) {
  4343. tests := []struct {
  4344. name string
  4345. inputBody wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries
  4346. serviceError error
  4347. expectedError error
  4348. }{
  4349. {
  4350. name: "success",
  4351. inputBody: wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries{
  4352. Users: []struct {
  4353. ScreenName string `oscar:"len_prefix=uint8"`
  4354. }{
  4355. {
  4356. ScreenName: "friend1",
  4357. },
  4358. {
  4359. ScreenName: "friend2",
  4360. },
  4361. },
  4362. },
  4363. },
  4364. {
  4365. name: "service error",
  4366. inputBody: wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries{
  4367. Users: []struct {
  4368. ScreenName string `oscar:"len_prefix=uint8"`
  4369. }{
  4370. {
  4371. ScreenName: "friend1",
  4372. },
  4373. {
  4374. ScreenName: "friend2",
  4375. },
  4376. },
  4377. },
  4378. serviceError: assert.AnError,
  4379. expectedError: assert.AnError,
  4380. },
  4381. }
  4382. for _, tt := range tests {
  4383. t.Run(tt.name, func(t *testing.T) {
  4384. sess := state.NewSession()
  4385. input := wire.SNACMessage{
  4386. Frame: wire.SNACFrame{
  4387. FoodGroup: wire.PermitDeny,
  4388. SubGroup: wire.PermitDenyDelPermListEntries,
  4389. },
  4390. Body: tt.inputBody,
  4391. }
  4392. svc := newMockPermitDenyService(t)
  4393. svc.EXPECT().
  4394. DelPermListEntries(mock.Anything, sess, input.Body).
  4395. Return(tt.serviceError)
  4396. h := Handler{
  4397. PermitDenyService: svc,
  4398. RouteLogger: middleware.RouteLogger{
  4399. Logger: slog.Default(),
  4400. },
  4401. }
  4402. buf := &bytes.Buffer{}
  4403. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4404. err := h.Handle(context.TODO(), wire.BOS, sess, input.Frame, buf, nil, config.Listener{})
  4405. if tt.expectedError != nil {
  4406. assert.ErrorIs(t, err, tt.expectedError)
  4407. } else {
  4408. assert.NoError(t, err)
  4409. }
  4410. })
  4411. }
  4412. }
  4413. func TestHandler_PermitDenySetGroupPermitMask(t *testing.T) {
  4414. tests := []struct {
  4415. name string
  4416. inputBody wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries
  4417. expectedError error
  4418. }{
  4419. {
  4420. name: "success",
  4421. inputBody: wire.SNAC_0x09_0x06_PermitDenyDelPermListEntries{
  4422. Users: []struct {
  4423. ScreenName string `oscar:"len_prefix=uint8"`
  4424. }{
  4425. {
  4426. ScreenName: "friend1",
  4427. },
  4428. },
  4429. },
  4430. },
  4431. }
  4432. for _, tt := range tests {
  4433. t.Run(tt.name, func(t *testing.T) {
  4434. sess := state.NewSession()
  4435. input := wire.SNACMessage{
  4436. Frame: wire.SNACFrame{
  4437. FoodGroup: wire.PermitDeny,
  4438. SubGroup: wire.PermitDenySetGroupPermitMask,
  4439. },
  4440. Body: tt.inputBody,
  4441. }
  4442. svc := newMockPermitDenyService(t)
  4443. h := Handler{
  4444. PermitDenyService: svc,
  4445. RouteLogger: middleware.RouteLogger{
  4446. Logger: slog.Default(),
  4447. },
  4448. }
  4449. buf := &bytes.Buffer{}
  4450. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4451. err := h.Handle(context.TODO(), wire.BOS, sess, input.Frame, buf, nil, config.Listener{})
  4452. if tt.expectedError != nil {
  4453. assert.ErrorIs(t, err, tt.expectedError)
  4454. } else {
  4455. assert.NoError(t, err)
  4456. }
  4457. })
  4458. }
  4459. }
  4460. func TestUserLookupHandler_FindByEmail(t *testing.T) {
  4461. tests := []struct {
  4462. name string
  4463. inputBody wire.SNAC_0x0A_0x02_UserLookupFindByEmail
  4464. serviceError error
  4465. responseError error
  4466. expectedError error
  4467. }{
  4468. {
  4469. name: "success",
  4470. inputBody: wire.SNAC_0x0A_0x02_UserLookupFindByEmail{
  4471. Email: []byte("haha@aol.com"),
  4472. },
  4473. },
  4474. {
  4475. name: "service error",
  4476. inputBody: wire.SNAC_0x0A_0x02_UserLookupFindByEmail{
  4477. Email: []byte("haha@aol.com"),
  4478. },
  4479. serviceError: assert.AnError,
  4480. expectedError: assert.AnError,
  4481. },
  4482. {
  4483. name: "response writer error",
  4484. inputBody: wire.SNAC_0x0A_0x02_UserLookupFindByEmail{
  4485. Email: []byte("haha@aol.com"),
  4486. },
  4487. responseError: assert.AnError,
  4488. expectedError: assert.AnError,
  4489. },
  4490. }
  4491. for _, tt := range tests {
  4492. t.Run(tt.name, func(t *testing.T) {
  4493. input := wire.SNACMessage{
  4494. Frame: wire.SNACFrame{
  4495. FoodGroup: wire.UserLookup,
  4496. SubGroup: wire.UserLookupFindByEmail,
  4497. },
  4498. Body: tt.inputBody,
  4499. }
  4500. output := wire.SNACMessage{
  4501. Frame: wire.SNACFrame{
  4502. FoodGroup: wire.UserLookup,
  4503. SubGroup: wire.UserLookupFindReply,
  4504. },
  4505. Body: wire.SNAC_0x0A_0x03_UserLookupFindReply{
  4506. TLVRestBlock: wire.TLVRestBlock{
  4507. TLVList: wire.TLVList{
  4508. wire.NewTLVBE(0x01, uint16(0x02)),
  4509. },
  4510. },
  4511. },
  4512. }
  4513. svc := newMockUserLookupService(t)
  4514. svc.EXPECT().
  4515. FindByEmail(mock.Anything, input.Frame, input.Body).
  4516. Return(output, tt.serviceError)
  4517. h := Handler{
  4518. UserLookupService: svc,
  4519. RouteLogger: middleware.RouteLogger{
  4520. Logger: slog.Default(),
  4521. },
  4522. }
  4523. ss := newMockResponseWriter(t)
  4524. if tt.serviceError == nil {
  4525. ss.EXPECT().
  4526. SendSNAC(output.Frame, output.Body).
  4527. Return(tt.responseError)
  4528. }
  4529. buf := &bytes.Buffer{}
  4530. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4531. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, ss, config.Listener{})
  4532. if tt.expectedError != nil {
  4533. assert.ErrorIs(t, err, tt.expectedError)
  4534. } else {
  4535. assert.NoError(t, err)
  4536. }
  4537. })
  4538. }
  4539. }
  4540. func TestHandler_LocateGetDirInfo(t *testing.T) {
  4541. tests := []struct {
  4542. name string
  4543. inputBody wire.SNAC_0x02_0x0B_LocateGetDirInfo
  4544. serviceError error
  4545. responseError error
  4546. expectedError error
  4547. }{
  4548. {
  4549. name: "success",
  4550. inputBody: wire.SNAC_0x02_0x0B_LocateGetDirInfo{
  4551. ScreenName: "screen-name",
  4552. },
  4553. },
  4554. {
  4555. name: "service error",
  4556. inputBody: wire.SNAC_0x02_0x0B_LocateGetDirInfo{
  4557. ScreenName: "screen-name",
  4558. },
  4559. serviceError: assert.AnError,
  4560. expectedError: assert.AnError,
  4561. },
  4562. {
  4563. name: "response writer error",
  4564. inputBody: wire.SNAC_0x02_0x0B_LocateGetDirInfo{
  4565. ScreenName: "screen-name",
  4566. },
  4567. responseError: assert.AnError,
  4568. expectedError: assert.AnError,
  4569. },
  4570. }
  4571. for _, tt := range tests {
  4572. t.Run(tt.name, func(t *testing.T) {
  4573. input := wire.SNACMessage{
  4574. Frame: wire.SNACFrame{
  4575. FoodGroup: wire.Locate,
  4576. SubGroup: wire.LocateGetDirInfo,
  4577. },
  4578. Body: tt.inputBody,
  4579. }
  4580. output := wire.SNACMessage{
  4581. Frame: wire.SNACFrame{
  4582. FoodGroup: wire.Locate,
  4583. SubGroup: wire.LocateGetDirReply,
  4584. },
  4585. Body: wire.SNAC_0x02_0x0C_LocateGetDirReply{
  4586. Status: 1,
  4587. },
  4588. }
  4589. svc := newMockLocateService(t)
  4590. svc.EXPECT().
  4591. DirInfo(mock.Anything, input.Frame, input.Body).
  4592. Return(output, tt.serviceError)
  4593. h := Handler{
  4594. LocateService: svc,
  4595. RouteLogger: middleware.RouteLogger{
  4596. Logger: slog.Default(),
  4597. },
  4598. }
  4599. responseWriter := newMockResponseWriter(t)
  4600. if tt.serviceError == nil {
  4601. responseWriter.EXPECT().
  4602. SendSNAC(output.Frame, output.Body).
  4603. Return(tt.responseError)
  4604. }
  4605. buf := &bytes.Buffer{}
  4606. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4607. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4608. if tt.expectedError != nil {
  4609. assert.ErrorIs(t, err, tt.expectedError)
  4610. } else {
  4611. assert.NoError(t, err)
  4612. }
  4613. })
  4614. }
  4615. }
  4616. func TestHandler_LocateRightsQuery(t *testing.T) {
  4617. tests := []struct {
  4618. name string
  4619. inputBody struct{}
  4620. responseError error
  4621. expectedError error
  4622. }{
  4623. {
  4624. name: "success",
  4625. inputBody: struct{}{},
  4626. },
  4627. {
  4628. name: "response writer error",
  4629. inputBody: struct{}{},
  4630. responseError: assert.AnError,
  4631. expectedError: assert.AnError,
  4632. },
  4633. }
  4634. for _, tt := range tests {
  4635. t.Run(tt.name, func(t *testing.T) {
  4636. input := wire.SNACMessage{
  4637. Frame: wire.SNACFrame{
  4638. FoodGroup: wire.Locate,
  4639. SubGroup: wire.LocateRightsQuery,
  4640. },
  4641. Body: tt.inputBody,
  4642. }
  4643. output := wire.SNACMessage{
  4644. Frame: wire.SNACFrame{
  4645. FoodGroup: wire.Locate,
  4646. SubGroup: wire.LocateRightsReply,
  4647. },
  4648. Body: wire.SNAC_0x02_0x03_LocateRightsReply{
  4649. TLVRestBlock: wire.TLVRestBlock{
  4650. TLVList: wire.TLVList{
  4651. wire.NewTLVBE(0x01, uint16(1000)),
  4652. },
  4653. },
  4654. },
  4655. }
  4656. svc := newMockLocateService(t)
  4657. svc.EXPECT().
  4658. RightsQuery(mock.Anything, input.Frame).
  4659. Return(output)
  4660. h := Handler{
  4661. LocateService: svc,
  4662. RouteLogger: middleware.RouteLogger{
  4663. Logger: slog.Default(),
  4664. },
  4665. }
  4666. responseWriter := newMockResponseWriter(t)
  4667. responseWriter.EXPECT().
  4668. SendSNAC(output.Frame, output.Body).
  4669. Return(tt.responseError)
  4670. buf := &bytes.Buffer{}
  4671. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4672. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4673. if tt.expectedError != nil {
  4674. assert.ErrorIs(t, err, tt.expectedError)
  4675. } else {
  4676. assert.NoError(t, err)
  4677. }
  4678. })
  4679. }
  4680. }
  4681. func TestHandler_LocateSetDirInfo(t *testing.T) {
  4682. tests := []struct {
  4683. name string
  4684. inputBody wire.SNAC_0x02_0x09_LocateSetDirInfo
  4685. serviceError error
  4686. responseError error
  4687. expectedError error
  4688. }{
  4689. {
  4690. name: "success",
  4691. inputBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  4692. TLVRestBlock: wire.TLVRestBlock{
  4693. TLVList: wire.TLVList{
  4694. {
  4695. Tag: 0x01,
  4696. Value: []byte{1, 2, 3, 4},
  4697. },
  4698. },
  4699. },
  4700. },
  4701. },
  4702. {
  4703. name: "service error",
  4704. inputBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  4705. TLVRestBlock: wire.TLVRestBlock{
  4706. TLVList: wire.TLVList{
  4707. {
  4708. Tag: 0x01,
  4709. Value: []byte{1, 2, 3, 4},
  4710. },
  4711. },
  4712. },
  4713. },
  4714. serviceError: assert.AnError,
  4715. expectedError: assert.AnError,
  4716. },
  4717. {
  4718. name: "response writer error",
  4719. inputBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  4720. TLVRestBlock: wire.TLVRestBlock{
  4721. TLVList: wire.TLVList{
  4722. {
  4723. Tag: 0x01,
  4724. Value: []byte{1, 2, 3, 4},
  4725. },
  4726. },
  4727. },
  4728. },
  4729. responseError: assert.AnError,
  4730. expectedError: assert.AnError,
  4731. },
  4732. }
  4733. for _, tt := range tests {
  4734. t.Run(tt.name, func(t *testing.T) {
  4735. input := wire.SNACMessage{
  4736. Frame: wire.SNACFrame{
  4737. FoodGroup: wire.Locate,
  4738. SubGroup: wire.LocateSetDirInfo,
  4739. },
  4740. Body: tt.inputBody,
  4741. }
  4742. output := wire.SNACMessage{
  4743. Frame: wire.SNACFrame{
  4744. FoodGroup: wire.Locate,
  4745. SubGroup: wire.LocateSetDirReply,
  4746. },
  4747. Body: wire.SNAC_0x02_0x0A_LocateSetDirReply{
  4748. Result: 1,
  4749. },
  4750. }
  4751. svc := newMockLocateService(t)
  4752. svc.EXPECT().
  4753. SetDirInfo(mock.Anything, mock.Anything, input.Frame, input.Body).
  4754. Return(output, tt.serviceError)
  4755. h := Handler{
  4756. LocateService: svc,
  4757. RouteLogger: middleware.RouteLogger{
  4758. Logger: slog.Default(),
  4759. },
  4760. }
  4761. responseWriter := newMockResponseWriter(t)
  4762. if tt.serviceError == nil {
  4763. responseWriter.EXPECT().
  4764. SendSNAC(output.Frame, output.Body).
  4765. Return(tt.responseError)
  4766. }
  4767. buf := &bytes.Buffer{}
  4768. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4769. err := h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{})
  4770. if tt.expectedError != nil {
  4771. assert.ErrorIs(t, err, tt.expectedError)
  4772. } else {
  4773. assert.NoError(t, err)
  4774. }
  4775. })
  4776. }
  4777. }
  4778. func TestHandler_LocateSetInfo(t *testing.T) {
  4779. input := wire.SNACMessage{
  4780. Frame: wire.SNACFrame{
  4781. FoodGroup: wire.Locate,
  4782. SubGroup: wire.LocateSetInfo,
  4783. },
  4784. Body: wire.SNAC_0x02_0x04_LocateSetInfo{
  4785. TLVRestBlock: wire.TLVRestBlock{
  4786. TLVList: wire.TLVList{
  4787. {
  4788. Tag: 0x01,
  4789. Value: []byte{1, 2, 3, 4},
  4790. },
  4791. },
  4792. },
  4793. },
  4794. }
  4795. svc := newMockLocateService(t)
  4796. svc.EXPECT().
  4797. SetInfo(mock.Anything, mock.Anything, input.Body).
  4798. Return(nil)
  4799. h := Handler{
  4800. LocateService: svc,
  4801. RouteLogger: middleware.RouteLogger{
  4802. Logger: slog.Default(),
  4803. },
  4804. }
  4805. responseWriter := newMockResponseWriter(t)
  4806. buf := &bytes.Buffer{}
  4807. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4808. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  4809. }
  4810. func TestHandler_LocateSetKeywordInfo(t *testing.T) {
  4811. input := wire.SNACMessage{
  4812. Frame: wire.SNACFrame{
  4813. FoodGroup: wire.Locate,
  4814. SubGroup: wire.LocateSetKeywordInfo,
  4815. },
  4816. Body: wire.SNAC_0x02_0x0F_LocateSetKeywordInfo{
  4817. TLVRestBlock: wire.TLVRestBlock{
  4818. TLVList: wire.TLVList{
  4819. {
  4820. Tag: 0x01,
  4821. Value: []byte{1, 2, 3, 4},
  4822. },
  4823. },
  4824. },
  4825. },
  4826. }
  4827. output := wire.SNACMessage{
  4828. Frame: wire.SNACFrame{
  4829. FoodGroup: wire.Locate,
  4830. SubGroup: wire.LocateSetKeywordReply,
  4831. },
  4832. Body: wire.SNAC_0x02_0x10_LocateSetKeywordReply{
  4833. Unknown: 1,
  4834. },
  4835. }
  4836. svc := newMockLocateService(t)
  4837. svc.EXPECT().
  4838. SetKeywordInfo(mock.Anything, mock.Anything, input.Frame, input.Body).
  4839. Return(output, nil)
  4840. h := Handler{
  4841. LocateService: svc,
  4842. RouteLogger: middleware.RouteLogger{
  4843. Logger: slog.Default(),
  4844. },
  4845. }
  4846. responseWriter := newMockResponseWriter(t)
  4847. responseWriter.EXPECT().
  4848. SendSNAC(output.Frame, output.Body).
  4849. Return(nil)
  4850. buf := &bytes.Buffer{}
  4851. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4852. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  4853. }
  4854. func TestHandler_LocateUserInfoQuery(t *testing.T) {
  4855. input := wire.SNACMessage{
  4856. Frame: wire.SNACFrame{
  4857. FoodGroup: wire.Locate,
  4858. SubGroup: wire.LocateUserInfoQuery,
  4859. },
  4860. Body: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  4861. Type: 1,
  4862. },
  4863. }
  4864. output := wire.SNACMessage{
  4865. Frame: wire.SNACFrame{
  4866. FoodGroup: wire.Locate,
  4867. SubGroup: wire.LocateUserInfoReply,
  4868. },
  4869. Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  4870. TLVUserInfo: wire.TLVUserInfo{
  4871. ScreenName: "screen-name",
  4872. },
  4873. LocateInfo: wire.TLVRestBlock{
  4874. TLVList: wire.TLVList{
  4875. {
  4876. Tag: 0x01,
  4877. Value: []byte{1, 2, 3, 4},
  4878. },
  4879. },
  4880. },
  4881. },
  4882. }
  4883. svc := newMockLocateService(t)
  4884. svc.EXPECT().
  4885. UserInfoQuery(mock.Anything, mock.Anything, input.Frame, input.Body).
  4886. Return(output, nil)
  4887. h := Handler{
  4888. LocateService: svc,
  4889. RouteLogger: middleware.RouteLogger{
  4890. Logger: slog.Default(),
  4891. },
  4892. }
  4893. responseWriter := newMockResponseWriter(t)
  4894. responseWriter.EXPECT().
  4895. SendSNAC(output.Frame, output.Body).
  4896. Return(nil)
  4897. buf := &bytes.Buffer{}
  4898. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4899. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  4900. }
  4901. func TestHandler_LocateUserInfoQuery2(t *testing.T) {
  4902. input := wire.SNACMessage{
  4903. Frame: wire.SNACFrame{
  4904. FoodGroup: wire.Locate,
  4905. SubGroup: wire.LocateUserInfoQuery2,
  4906. },
  4907. Body: wire.SNAC_0x02_0x15_LocateUserInfoQuery2{
  4908. Type2: 1,
  4909. },
  4910. }
  4911. output := wire.SNACMessage{
  4912. Frame: wire.SNACFrame{
  4913. FoodGroup: wire.Locate,
  4914. SubGroup: wire.LocateUserInfoReply,
  4915. },
  4916. Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  4917. TLVUserInfo: wire.TLVUserInfo{
  4918. ScreenName: "screen-name",
  4919. },
  4920. LocateInfo: wire.TLVRestBlock{
  4921. TLVList: wire.TLVList{
  4922. {
  4923. Tag: 0x01,
  4924. Value: []byte{1, 2, 3, 4},
  4925. },
  4926. },
  4927. },
  4928. },
  4929. }
  4930. svc := newMockLocateService(t)
  4931. svc.EXPECT().
  4932. UserInfoQuery(mock.Anything, mock.Anything, input.Frame, wire.SNAC_0x02_0x05_LocateUserInfoQuery{Type: 1}).
  4933. Return(output, nil)
  4934. h := Handler{
  4935. LocateService: svc,
  4936. RouteLogger: middleware.RouteLogger{
  4937. Logger: slog.Default(),
  4938. },
  4939. }
  4940. responseWriter := newMockResponseWriter(t)
  4941. responseWriter.EXPECT().
  4942. SendSNAC(output.Frame, output.Body).
  4943. Return(nil)
  4944. buf := &bytes.Buffer{}
  4945. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4946. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, responseWriter, config.Listener{}))
  4947. }
  4948. func TestHandler_StatsReportEvents(t *testing.T) {
  4949. input := wire.SNACMessage{
  4950. Frame: wire.SNACFrame{
  4951. FoodGroup: wire.Stats,
  4952. SubGroup: wire.StatsReportEvents,
  4953. },
  4954. Body: wire.SNAC_0x0B_0x03_StatsReportEvents{},
  4955. }
  4956. output := wire.SNACMessage{
  4957. Frame: wire.SNACFrame{
  4958. FoodGroup: wire.Stats,
  4959. SubGroup: wire.StatsReportAck,
  4960. },
  4961. Body: wire.SNAC_0x0B_0x04_StatsReportAck{},
  4962. }
  4963. svc := newMockStatsService(t)
  4964. svc.EXPECT().
  4965. ReportEvents(mock.Anything, input.Frame, input.Body).
  4966. Return(output)
  4967. h := Handler{
  4968. StatsService: svc,
  4969. RouteLogger: middleware.RouteLogger{
  4970. Logger: slog.Default(),
  4971. },
  4972. }
  4973. ss := newMockResponseWriter(t)
  4974. ss.EXPECT().
  4975. SendSNAC(output.Frame, output.Body).
  4976. Return(nil)
  4977. buf := &bytes.Buffer{}
  4978. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  4979. assert.NoError(t, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, buf, ss, config.Listener{}))
  4980. }
  4981. func TestHandler_RouteNotFound(t *testing.T) {
  4982. input := wire.SNACMessage{
  4983. Frame: wire.SNACFrame{
  4984. FoodGroup: wire.Stats,
  4985. SubGroup: math.MaxUint16,
  4986. },
  4987. Body: wire.SNAC_0x0B_0x03_StatsReportEvents{},
  4988. }
  4989. h := Handler{
  4990. RouteLogger: middleware.RouteLogger{
  4991. Logger: slog.Default(),
  4992. },
  4993. }
  4994. assert.ErrorIs(t, ErrRouteNotFound, h.Handle(context.TODO(), wire.BOS, nil, input.Frame, nil, nil, config.Listener{}))
  4995. }