handler_test.go 139 KB

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