handler_test.go 139 KB

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