handler_test.go 129 KB

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