handler_test.go 132 KB

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