cmd_client_test.go 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273
  1. package toc
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "io"
  6. "log/slog"
  7. "testing"
  8. "github.com/google/uuid"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/mock"
  11. "golang.org/x/sync/errgroup"
  12. "github.com/mk6i/retro-aim-server/state"
  13. "github.com/mk6i/retro-aim-server/wire"
  14. )
  15. func TestOSCARProxy_RecvClientCmd_AddBuddy(t *testing.T) {
  16. cases := []struct {
  17. // name is the unit test name
  18. name string
  19. // me is the TOC user session
  20. me *state.Session
  21. // givenCmd is the TOC command
  22. givenCmd []byte
  23. // wantMsg is the expected TOC response
  24. wantMsg string
  25. // mockParams is the list of params sent to mocks that satisfy this
  26. // method's dependencies
  27. mockParams mockParams
  28. }{
  29. {
  30. name: "successfully add buddies",
  31. me: newTestSession("me"),
  32. givenCmd: []byte("toc_add_buddy friend1 friend2 friend3"),
  33. mockParams: mockParams{
  34. buddyParams: buddyParams{
  35. addBuddiesParams: addBuddiesParams{
  36. {
  37. me: state.NewIdentScreenName("me"),
  38. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  39. Buddies: []struct {
  40. ScreenName string `oscar:"len_prefix=uint8"`
  41. }{
  42. {ScreenName: "friend1"},
  43. {ScreenName: "friend2"},
  44. {ScreenName: "friend3"},
  45. },
  46. },
  47. },
  48. },
  49. },
  50. },
  51. },
  52. {
  53. name: "add buddies with empty list",
  54. me: newTestSession("me"),
  55. givenCmd: []byte("toc_add_buddy"),
  56. mockParams: mockParams{
  57. buddyParams: buddyParams{
  58. addBuddiesParams: addBuddiesParams{
  59. {
  60. me: state.NewIdentScreenName("me"),
  61. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{},
  62. },
  63. },
  64. },
  65. },
  66. },
  67. {
  68. name: "add buddies, receive error from buddy service",
  69. me: newTestSession("me"),
  70. givenCmd: []byte("toc_add_buddy friend1"),
  71. mockParams: mockParams{
  72. buddyParams: buddyParams{
  73. addBuddiesParams: addBuddiesParams{
  74. {
  75. me: state.NewIdentScreenName("me"),
  76. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  77. Buddies: []struct {
  78. ScreenName string `oscar:"len_prefix=uint8"`
  79. }{
  80. {ScreenName: "friend1"},
  81. },
  82. },
  83. err: io.EOF,
  84. },
  85. },
  86. },
  87. },
  88. wantMsg: cmdInternalSvcErr,
  89. },
  90. }
  91. for _, tc := range cases {
  92. t.Run(tc.name, func(t *testing.T) {
  93. ctx := context.Background()
  94. buddySvc := newMockBuddyService(t)
  95. for _, params := range tc.mockParams.addBuddiesParams {
  96. buddySvc.EXPECT().
  97. AddBuddies(ctx, matchSession(params.me), params.inBody).
  98. Return(params.err)
  99. }
  100. svc := OSCARProxy{
  101. Logger: slog.Default(),
  102. BuddyService: buddySvc,
  103. }
  104. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  105. assert.Equal(t, tc.wantMsg, msg)
  106. })
  107. }
  108. }
  109. func TestOSCARProxy_RecvClientCmd_AddPermit(t *testing.T) {
  110. cases := []struct {
  111. // name is the unit test name
  112. name string
  113. // me is the TOC user session
  114. me *state.Session
  115. // givenCmd is the TOC command
  116. givenCmd []byte
  117. // wantMsg is the expected TOC response
  118. wantMsg string
  119. // mockParams is the list of params sent to mocks that satisfy this
  120. // method's dependencies
  121. mockParams mockParams
  122. }{
  123. {
  124. name: "successfully permit buddies",
  125. me: newTestSession("me"),
  126. givenCmd: []byte("toc_add_permit friend1 friend2 friend3"),
  127. mockParams: mockParams{
  128. permitDenyParams: permitDenyParams{
  129. addPermListEntriesParams: addPermListEntriesParams{
  130. {
  131. me: state.NewIdentScreenName("me"),
  132. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  133. Users: []struct {
  134. ScreenName string `oscar:"len_prefix=uint8"`
  135. }{
  136. {ScreenName: "friend1"},
  137. {ScreenName: "friend2"},
  138. {ScreenName: "friend3"},
  139. },
  140. },
  141. },
  142. },
  143. },
  144. },
  145. },
  146. {
  147. name: "permit buddies, receive error from buddy service",
  148. me: newTestSession("me"),
  149. givenCmd: []byte("toc_add_permit friend1"),
  150. mockParams: mockParams{
  151. permitDenyParams: permitDenyParams{
  152. addPermListEntriesParams: addPermListEntriesParams{
  153. {
  154. me: state.NewIdentScreenName("me"),
  155. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  156. Users: []struct {
  157. ScreenName string `oscar:"len_prefix=uint8"`
  158. }{
  159. {ScreenName: "friend1"},
  160. },
  161. },
  162. err: io.EOF,
  163. },
  164. },
  165. },
  166. },
  167. wantMsg: cmdInternalSvcErr,
  168. },
  169. {
  170. name: "permit buddies with empty list",
  171. me: newTestSession("me"),
  172. givenCmd: []byte("toc_add_permit"),
  173. mockParams: mockParams{
  174. permitDenyParams: permitDenyParams{
  175. addPermListEntriesParams: addPermListEntriesParams{
  176. {
  177. me: state.NewIdentScreenName("me"),
  178. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{},
  179. },
  180. },
  181. },
  182. },
  183. },
  184. }
  185. for _, tc := range cases {
  186. t.Run(tc.name, func(t *testing.T) {
  187. ctx := context.Background()
  188. pdSvc := newMockPermitDenyService(t)
  189. for _, params := range tc.mockParams.addPermListEntriesParams {
  190. pdSvc.EXPECT().
  191. AddPermListEntries(ctx, matchSession(params.me), params.body).
  192. Return(params.err)
  193. }
  194. svc := OSCARProxy{
  195. Logger: slog.Default(),
  196. PermitDenyService: pdSvc,
  197. }
  198. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  199. assert.Equal(t, tc.wantMsg, msg)
  200. })
  201. }
  202. }
  203. func TestOSCARProxy_RecvClientCmd_AddDeny(t *testing.T) {
  204. cases := []struct {
  205. // name is the unit test name
  206. name string
  207. // me is the TOC user session
  208. me *state.Session
  209. // givenCmd is the TOC command
  210. givenCmd []byte
  211. // wantMsg is the expected TOC response
  212. wantMsg string
  213. // mockParams is the list of params sent to mocks that satisfy this
  214. // method's dependencies
  215. mockParams mockParams
  216. }{
  217. {
  218. name: "successfully deny buddies",
  219. me: newTestSession("me"),
  220. givenCmd: []byte("toc_add_deny friend1 friend2 friend3"),
  221. mockParams: mockParams{
  222. permitDenyParams: permitDenyParams{
  223. addDenyListEntriesParams: addDenyListEntriesParams{
  224. {
  225. me: state.NewIdentScreenName("me"),
  226. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  227. Users: []struct {
  228. ScreenName string `oscar:"len_prefix=uint8"`
  229. }{
  230. {ScreenName: "friend1"},
  231. {ScreenName: "friend2"},
  232. {ScreenName: "friend3"},
  233. },
  234. },
  235. },
  236. },
  237. },
  238. },
  239. },
  240. {
  241. name: "deny buddies, receive error from buddy service",
  242. me: newTestSession("me"),
  243. givenCmd: []byte("toc_add_deny friend1"),
  244. mockParams: mockParams{
  245. permitDenyParams: permitDenyParams{
  246. addDenyListEntriesParams: addDenyListEntriesParams{
  247. {
  248. me: state.NewIdentScreenName("me"),
  249. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  250. Users: []struct {
  251. ScreenName string `oscar:"len_prefix=uint8"`
  252. }{
  253. {ScreenName: "friend1"},
  254. },
  255. },
  256. err: io.EOF,
  257. },
  258. },
  259. },
  260. },
  261. wantMsg: cmdInternalSvcErr,
  262. },
  263. {
  264. name: "deny buddies with empty list",
  265. me: newTestSession("me"),
  266. givenCmd: []byte("toc_add_deny"),
  267. mockParams: mockParams{
  268. permitDenyParams: permitDenyParams{
  269. addDenyListEntriesParams: addDenyListEntriesParams{
  270. {
  271. me: state.NewIdentScreenName("me"),
  272. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{},
  273. },
  274. },
  275. },
  276. },
  277. },
  278. }
  279. for _, tc := range cases {
  280. t.Run(tc.name, func(t *testing.T) {
  281. ctx := context.Background()
  282. pdSvc := newMockPermitDenyService(t)
  283. for _, params := range tc.mockParams.addDenyListEntriesParams {
  284. pdSvc.EXPECT().
  285. AddDenyListEntries(ctx, matchSession(params.me), params.body).
  286. Return(params.err)
  287. }
  288. svc := OSCARProxy{
  289. Logger: slog.Default(),
  290. PermitDenyService: pdSvc,
  291. }
  292. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  293. assert.Equal(t, tc.wantMsg, msg)
  294. })
  295. }
  296. }
  297. func TestOSCARProxy_RecvClientCmd_FormatNickname(t *testing.T) {
  298. cases := []struct {
  299. // name is the unit test name
  300. name string
  301. // me is the TOC user session
  302. me *state.Session
  303. // givenCmd is the TOC command
  304. givenCmd []byte
  305. // wantMsg is the expected TOC response
  306. wantMsg string
  307. // mockParams is the list of params sent to mocks that satisfy this
  308. // method's dependencies
  309. mockParams mockParams
  310. }{
  311. {
  312. name: "successfully change screen name format",
  313. me: newTestSession("myScreenName"),
  314. givenCmd: []byte("toc_format_nickname mYsCrEeNnAmE"),
  315. mockParams: mockParams{
  316. adminParams: adminParams{
  317. infoChangeRequestParams: infoChangeRequestParams{
  318. {
  319. me: state.NewIdentScreenName("myScreenName"),
  320. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  321. TLVRestBlock: wire.TLVRestBlock{
  322. TLVList: wire.TLVList{
  323. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "mYsCrEeNnAmE"),
  324. },
  325. },
  326. },
  327. msg: wire.SNACMessage{
  328. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  329. Permissions: wire.AdminInfoPermissionsReadWrite,
  330. TLVBlock: wire.TLVBlock{
  331. TLVList: wire.TLVList{
  332. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "mYsCrEeNnAmE"),
  333. },
  334. },
  335. },
  336. },
  337. },
  338. },
  339. },
  340. },
  341. wantMsg: "ADMIN_NICK_STATUS:0",
  342. },
  343. {
  344. name: "format nickname - invalid length",
  345. me: newTestSession("sn"),
  346. givenCmd: []byte("toc_format_nickname sN"),
  347. mockParams: mockParams{
  348. adminParams: adminParams{
  349. infoChangeRequestParams: infoChangeRequestParams{
  350. {
  351. me: state.NewIdentScreenName("sn"),
  352. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  353. TLVRestBlock: wire.TLVRestBlock{
  354. TLVList: wire.TLVList{
  355. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "sN"),
  356. },
  357. },
  358. },
  359. msg: wire.SNACMessage{
  360. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  361. Permissions: wire.AdminInfoPermissionsReadWrite,
  362. TLVBlock: wire.TLVBlock{
  363. TLVList: wire.TLVList{
  364. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorInvalidNickNameLength),
  365. wire.NewTLVBE(wire.AdminTLVUrl, ""),
  366. },
  367. },
  368. },
  369. },
  370. },
  371. },
  372. },
  373. },
  374. wantMsg: "ERROR:911",
  375. },
  376. {
  377. name: "format nickname - invalid screen name",
  378. me: newTestSession("sn"),
  379. givenCmd: []byte("toc_format_nickname sN"),
  380. mockParams: mockParams{
  381. adminParams: adminParams{
  382. infoChangeRequestParams: infoChangeRequestParams{
  383. {
  384. me: state.NewIdentScreenName("sn"),
  385. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  386. TLVRestBlock: wire.TLVRestBlock{
  387. TLVList: wire.TLVList{
  388. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "sN"),
  389. },
  390. },
  391. },
  392. msg: wire.SNACMessage{
  393. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  394. Permissions: wire.AdminInfoPermissionsReadWrite,
  395. TLVBlock: wire.TLVBlock{
  396. TLVList: wire.TLVList{
  397. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorInvalidNickName),
  398. wire.NewTLVBE(wire.AdminTLVUrl, ""),
  399. },
  400. },
  401. },
  402. },
  403. },
  404. },
  405. },
  406. },
  407. wantMsg: "ERROR:911",
  408. },
  409. {
  410. name: "format nickname - catch-all error",
  411. me: newTestSession("sn"),
  412. givenCmd: []byte("toc_format_nickname sN"),
  413. mockParams: mockParams{
  414. adminParams: adminParams{
  415. infoChangeRequestParams: infoChangeRequestParams{
  416. {
  417. me: state.NewIdentScreenName("sn"),
  418. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  419. TLVRestBlock: wire.TLVRestBlock{
  420. TLVList: wire.TLVList{
  421. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "sN"),
  422. },
  423. },
  424. },
  425. msg: wire.SNACMessage{
  426. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  427. Permissions: wire.AdminInfoPermissionsReadWrite,
  428. TLVBlock: wire.TLVBlock{
  429. TLVList: wire.TLVList{
  430. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorValidateNickName),
  431. wire.NewTLVBE(wire.AdminTLVUrl, ""),
  432. },
  433. },
  434. },
  435. },
  436. },
  437. },
  438. },
  439. },
  440. wantMsg: "ERROR:913",
  441. },
  442. {
  443. name: "format nickname - runtime error from admin svc",
  444. me: newTestSession("myScreenName"),
  445. givenCmd: []byte("toc_format_nickname mYsCrEeNnAmE"),
  446. mockParams: mockParams{
  447. adminParams: adminParams{
  448. infoChangeRequestParams: infoChangeRequestParams{
  449. {
  450. me: state.NewIdentScreenName("myScreenName"),
  451. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  452. TLVRestBlock: wire.TLVRestBlock{
  453. TLVList: wire.TLVList{
  454. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "mYsCrEeNnAmE"),
  455. },
  456. },
  457. },
  458. err: io.EOF,
  459. },
  460. },
  461. },
  462. },
  463. wantMsg: cmdInternalSvcErr,
  464. },
  465. {
  466. name: "change password - unexpected response from admin svc",
  467. me: newTestSession("myScreenName"),
  468. givenCmd: []byte("toc_format_nickname mYsCrEeNnAmE"),
  469. mockParams: mockParams{
  470. adminParams: adminParams{
  471. infoChangeRequestParams: infoChangeRequestParams{
  472. {
  473. me: state.NewIdentScreenName("myScreenName"),
  474. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  475. TLVRestBlock: wire.TLVRestBlock{
  476. TLVList: wire.TLVList{
  477. wire.NewTLVBE(wire.AdminTLVScreenNameFormatted, "mYsCrEeNnAmE"),
  478. },
  479. },
  480. },
  481. msg: wire.SNACMessage{
  482. Body: wire.SNACError{},
  483. },
  484. },
  485. },
  486. },
  487. },
  488. wantMsg: cmdInternalSvcErr,
  489. },
  490. {
  491. name: "bad command",
  492. givenCmd: []byte(`toc_format_nickname`),
  493. wantMsg: cmdInternalSvcErr,
  494. },
  495. }
  496. for _, tc := range cases {
  497. t.Run(tc.name, func(t *testing.T) {
  498. ctx := context.Background()
  499. adminSvc := newMockAdminService(t)
  500. for _, params := range tc.mockParams.infoChangeRequestParams {
  501. adminSvc.EXPECT().
  502. InfoChangeRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  503. Return(params.msg, params.err)
  504. }
  505. svc := OSCARProxy{
  506. Logger: slog.Default(),
  507. AdminService: adminSvc,
  508. }
  509. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  510. assert.Equal(t, tc.wantMsg, msg)
  511. })
  512. }
  513. }
  514. func TestOSCARProxy_RecvClientCmd_ChatAccept(t *testing.T) {
  515. fnNewChatNavParams := func(err error) chatNavParams {
  516. ret := chatNavParams{
  517. requestRoomInfoParams: requestRoomInfoParams{
  518. {
  519. inBody: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  520. Cookie: "the-cookie",
  521. Exchange: 4,
  522. InstanceNumber: 0,
  523. },
  524. },
  525. },
  526. }
  527. if err != nil {
  528. ret.requestRoomInfoParams[0].err = err
  529. } else {
  530. ret.requestRoomInfoParams[0].msg = wire.SNACMessage{
  531. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  532. TLVRestBlock: wire.TLVRestBlock{
  533. TLVList: wire.TLVList{
  534. wire.NewTLVBE(wire.ChatNavTLVRoomInfo, wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  535. Cookie: "the-cookie",
  536. TLVBlock: wire.TLVBlock{
  537. TLVList: wire.TLVList{
  538. wire.NewTLVBE(wire.ChatRoomTLVRoomName, "cool room"),
  539. },
  540. },
  541. }),
  542. },
  543. },
  544. },
  545. }
  546. }
  547. return ret
  548. }
  549. fnNewOServiceBOSParams := func(err error) oServiceParams {
  550. ret := oServiceParams{
  551. serviceRequestParams: serviceRequestParams{
  552. {
  553. me: state.NewIdentScreenName("me"),
  554. bodyIn: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  555. FoodGroup: wire.Chat,
  556. TLVRestBlock: wire.TLVRestBlock{
  557. TLVList: wire.TLVList{
  558. wire.NewTLVBE(0x01, wire.SNAC_0x01_0x04_TLVRoomInfo{
  559. Cookie: "the-cookie",
  560. }),
  561. },
  562. },
  563. },
  564. },
  565. },
  566. }
  567. if err != nil {
  568. ret.serviceRequestParams[0].err = err
  569. } else {
  570. ret.serviceRequestParams[0].msg = wire.SNACMessage{
  571. Body: wire.SNAC_0x01_0x05_OServiceServiceResponse{
  572. TLVRestBlock: wire.TLVRestBlock{
  573. TLVList: wire.TLVList{
  574. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, "chat-auth-cookie"),
  575. },
  576. },
  577. },
  578. }
  579. }
  580. return ret
  581. }
  582. fnNewAuthParams := func(err error) authParams {
  583. ret := authParams{
  584. registerChatSessionParams: registerChatSessionParams{
  585. {
  586. authCookie: []byte("chat-auth-cookie"),
  587. },
  588. },
  589. }
  590. if err != nil {
  591. ret.registerChatSessionParams[0].err = err
  592. } else {
  593. ret.registerChatSessionParams[0].sess = newTestSession("me-chat")
  594. }
  595. return ret
  596. }
  597. fnNewOServiceChatParams := func(err error) oServiceParams {
  598. ret := oServiceParams{
  599. clientOnlineParams: clientOnlineParams{
  600. {
  601. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  602. me: state.NewIdentScreenName("me-chat"),
  603. },
  604. },
  605. }
  606. if err != nil {
  607. ret.clientOnlineParams[0].err = err
  608. }
  609. return ret
  610. }
  611. cases := []struct {
  612. // name is the unit test name
  613. name string
  614. // me is the TOC user session
  615. me *state.Session
  616. // givenCmd is the TOC command
  617. givenCmd []byte
  618. // givenChatRegistry is the chat registry passed to the function
  619. givenChatRegistry *ChatRegistry
  620. // wantMsg is the expected TOC response
  621. wantMsg string
  622. // expectChatSession indicates whether a chat session should be present
  623. // in the chat registry
  624. expectChatSession bool
  625. // mockParams is the list of params sent to mocks that satisfy this
  626. // method's dependencies
  627. mockParams mockParams
  628. }{
  629. {
  630. name: "successfully accept chat",
  631. me: newTestSession("me"),
  632. givenCmd: []byte(`toc_chat_accept 0`),
  633. givenChatRegistry: func() *ChatRegistry {
  634. reg := NewChatRegistry()
  635. reg.Add(wire.ICBMRoomInfo{
  636. Cookie: "the-cookie",
  637. Exchange: 4,
  638. Instance: 0,
  639. })
  640. return reg
  641. }(),
  642. mockParams: mockParams{
  643. chatNavParams: fnNewChatNavParams(nil),
  644. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  645. authParams: fnNewAuthParams(nil),
  646. oServiceChatParams: fnNewOServiceChatParams(nil),
  647. },
  648. wantMsg: "CHAT_JOIN:0:cool room",
  649. expectChatSession: true,
  650. },
  651. {
  652. name: "accept chat, receive error from chat oservice svc",
  653. me: newTestSession("me"),
  654. givenCmd: []byte(`toc_chat_accept 0`),
  655. givenChatRegistry: func() *ChatRegistry {
  656. reg := NewChatRegistry()
  657. reg.Add(wire.ICBMRoomInfo{
  658. Cookie: "the-cookie",
  659. Exchange: 4,
  660. Instance: 0,
  661. })
  662. return reg
  663. }(),
  664. mockParams: mockParams{
  665. chatNavParams: fnNewChatNavParams(nil),
  666. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  667. authParams: fnNewAuthParams(nil),
  668. oServiceChatParams: fnNewOServiceChatParams(io.EOF),
  669. },
  670. wantMsg: cmdInternalSvcErr,
  671. expectChatSession: false,
  672. },
  673. {
  674. name: "accept chat, receive error from auth svc",
  675. me: newTestSession("me"),
  676. givenCmd: []byte(`toc_chat_accept 0`),
  677. givenChatRegistry: func() *ChatRegistry {
  678. reg := NewChatRegistry()
  679. reg.Add(wire.ICBMRoomInfo{
  680. Cookie: "the-cookie",
  681. Exchange: 4,
  682. Instance: 0,
  683. })
  684. return reg
  685. }(),
  686. mockParams: mockParams{
  687. chatNavParams: fnNewChatNavParams(nil),
  688. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  689. authParams: fnNewAuthParams(io.EOF),
  690. },
  691. wantMsg: cmdInternalSvcErr,
  692. expectChatSession: false,
  693. },
  694. {
  695. name: "accept chat, receive error from BOS oservice svc",
  696. me: newTestSession("me"),
  697. givenCmd: []byte(`toc_chat_accept 0`),
  698. givenChatRegistry: func() *ChatRegistry {
  699. reg := NewChatRegistry()
  700. reg.Add(wire.ICBMRoomInfo{
  701. Cookie: "the-cookie",
  702. Exchange: 4,
  703. Instance: 0,
  704. })
  705. return reg
  706. }(),
  707. mockParams: mockParams{
  708. chatNavParams: fnNewChatNavParams(nil),
  709. oServiceBOSParams: fnNewOServiceBOSParams(io.EOF),
  710. },
  711. wantMsg: cmdInternalSvcErr,
  712. expectChatSession: false,
  713. },
  714. {
  715. name: "accept chat, receive error from chat nav svc",
  716. me: newTestSession("me"),
  717. givenCmd: []byte(`toc_chat_accept 0`),
  718. givenChatRegistry: func() *ChatRegistry {
  719. reg := NewChatRegistry()
  720. reg.Add(wire.ICBMRoomInfo{
  721. Cookie: "the-cookie",
  722. Exchange: 4,
  723. Instance: 0,
  724. })
  725. return reg
  726. }(),
  727. mockParams: mockParams{
  728. chatNavParams: fnNewChatNavParams(io.EOF),
  729. },
  730. wantMsg: cmdInternalSvcErr,
  731. expectChatSession: false,
  732. },
  733. {
  734. name: "bad command",
  735. me: newTestSession("me"),
  736. givenCmd: []byte(`toc_chat_accept`),
  737. givenChatRegistry: NewChatRegistry(),
  738. wantMsg: cmdInternalSvcErr,
  739. expectChatSession: false,
  740. },
  741. {
  742. name: "bad exchange number",
  743. me: newTestSession("me"),
  744. givenCmd: []byte(`toc_chat_accept four`),
  745. givenChatRegistry: NewChatRegistry(),
  746. wantMsg: cmdInternalSvcErr,
  747. expectChatSession: false,
  748. },
  749. }
  750. for _, tc := range cases {
  751. t.Run(tc.name, func(t *testing.T) {
  752. ctx, cancel := context.WithCancel(context.Background())
  753. cancel()
  754. chatNavSvc := newMockChatNavService(t)
  755. for _, params := range tc.mockParams.requestRoomInfoParams {
  756. chatNavSvc.EXPECT().
  757. RequestRoomInfo(ctx, wire.SNACFrame{}, params.inBody).
  758. Return(params.msg, params.err)
  759. }
  760. bosOServiceSvc := newMockOServiceService(t)
  761. for _, params := range tc.mockParams.oServiceBOSParams.serviceRequestParams {
  762. bosOServiceSvc.EXPECT().
  763. ServiceRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.bodyIn).
  764. Return(params.msg, params.err)
  765. }
  766. chatOServiceSvc := newMockOServiceService(t)
  767. for _, params := range tc.mockParams.oServiceChatParams.clientOnlineParams {
  768. chatOServiceSvc.EXPECT().
  769. ClientOnline(ctx, params.body, matchSession(params.me)).
  770. Return(params.err)
  771. }
  772. authSvc := newMockAuthService(t)
  773. for _, params := range tc.mockParams.authParams.registerChatSessionParams {
  774. authSvc.EXPECT().
  775. RegisterChatSession(ctx, params.authCookie).
  776. Return(params.sess, params.err)
  777. }
  778. svc := OSCARProxy{
  779. AuthService: authSvc,
  780. ChatNavService: chatNavSvc,
  781. Logger: slog.Default(),
  782. OServiceServiceBOS: bosOServiceSvc,
  783. OServiceServiceChat: chatOServiceSvc,
  784. }
  785. g := &errgroup.Group{}
  786. tc.me.Close()
  787. msg := svc.RecvClientCmd(ctx, tc.me, tc.givenChatRegistry, tc.givenCmd, nil, g.Go)
  788. assert.NoError(t, g.Wait())
  789. assert.Equal(t, tc.wantMsg, msg)
  790. assert.Equal(t, tc.expectChatSession, len(tc.givenChatRegistry.Sessions()) == 1)
  791. })
  792. }
  793. }
  794. func TestOSCARProxy_RecvClientCmd_ChatInvite(t *testing.T) {
  795. cases := []struct {
  796. // name is the unit test name
  797. name string
  798. // me is the TOC user session
  799. me *state.Session
  800. // givenCmd is the TOC command
  801. givenCmd []byte
  802. // givenChatRegistry is the chat registry passed to the function
  803. givenChatRegistry *ChatRegistry
  804. // wantMsg is the expected TOC response
  805. wantMsg string
  806. // mockParams is the list of params sent to mocks that satisfy this
  807. // method's dependencies
  808. mockParams mockParams
  809. }{
  810. {
  811. name: "successfully send chat invitation",
  812. me: newTestSession("me"),
  813. givenCmd: []byte(`toc_chat_invite 0 "join my chat!" friend1`),
  814. givenChatRegistry: func() *ChatRegistry {
  815. reg := NewChatRegistry()
  816. reg.Add(wire.ICBMRoomInfo{
  817. Exchange: 4,
  818. Cookie: "the-cookie",
  819. Instance: 0,
  820. })
  821. return reg
  822. }(),
  823. mockParams: mockParams{
  824. icbmParams: icbmParams{
  825. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  826. {
  827. sender: state.NewIdentScreenName("me"),
  828. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  829. ChannelID: wire.ICBMChannelRendezvous,
  830. ScreenName: "friend1",
  831. TLVRestBlock: wire.TLVRestBlock{
  832. TLVList: wire.TLVList{
  833. wire.NewTLVBE(0x05, wire.ICBMCh2Fragment{
  834. Type: 0,
  835. Capability: wire.CapChat,
  836. TLVRestBlock: wire.TLVRestBlock{
  837. TLVList: wire.TLVList{
  838. wire.NewTLVBE(10, uint16(1)),
  839. wire.NewTLVBE(12, "join my chat!"),
  840. wire.NewTLVBE(13, "us-ascii"),
  841. wire.NewTLVBE(14, "en"),
  842. wire.NewTLVBE(10001, wire.ICBMRoomInfo{
  843. Exchange: 4,
  844. Cookie: "the-cookie",
  845. Instance: 0,
  846. }),
  847. },
  848. },
  849. }),
  850. },
  851. },
  852. },
  853. },
  854. },
  855. },
  856. },
  857. },
  858. {
  859. name: "send chat invitation, receive error from ICBM svc",
  860. me: newTestSession("me"),
  861. givenCmd: []byte(`toc_chat_invite 0 "join my chat!" friend1`),
  862. givenChatRegistry: func() *ChatRegistry {
  863. reg := NewChatRegistry()
  864. reg.Add(wire.ICBMRoomInfo{
  865. Exchange: 4,
  866. Cookie: "the-cookie",
  867. Instance: 0,
  868. })
  869. return reg
  870. }(),
  871. mockParams: mockParams{
  872. icbmParams: icbmParams{
  873. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  874. {
  875. sender: state.NewIdentScreenName("me"),
  876. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  877. ChannelID: wire.ICBMChannelRendezvous,
  878. ScreenName: "friend1",
  879. TLVRestBlock: wire.TLVRestBlock{
  880. TLVList: wire.TLVList{
  881. wire.NewTLVBE(0x05, wire.ICBMCh2Fragment{
  882. Type: 0,
  883. Capability: wire.CapChat,
  884. TLVRestBlock: wire.TLVRestBlock{
  885. TLVList: wire.TLVList{
  886. wire.NewTLVBE(10, uint16(1)),
  887. wire.NewTLVBE(12, "join my chat!"),
  888. wire.NewTLVBE(13, "us-ascii"),
  889. wire.NewTLVBE(14, "en"),
  890. wire.NewTLVBE(10001, wire.ICBMRoomInfo{
  891. Exchange: 4,
  892. Cookie: "the-cookie",
  893. Instance: 0,
  894. }),
  895. },
  896. },
  897. }),
  898. },
  899. },
  900. },
  901. err: io.EOF,
  902. },
  903. },
  904. },
  905. },
  906. wantMsg: cmdInternalSvcErr,
  907. },
  908. {
  909. name: "send chat invitation to non-existent room",
  910. me: newTestSession("me"),
  911. givenCmd: []byte(`toc_chat_invite 0 "join my chat!" friend1`),
  912. givenChatRegistry: NewChatRegistry(),
  913. wantMsg: cmdInternalSvcErr,
  914. },
  915. {
  916. name: "bad chat room ID",
  917. givenCmd: []byte(`toc_chat_invite zero "join my chat!" friend1`),
  918. wantMsg: cmdInternalSvcErr,
  919. },
  920. {
  921. name: "bad command",
  922. givenCmd: []byte(`toc_chat_invite`),
  923. wantMsg: cmdInternalSvcErr,
  924. },
  925. }
  926. for _, tc := range cases {
  927. t.Run(tc.name, func(t *testing.T) {
  928. ctx := context.Background()
  929. icbmSvc := newMockICBMService(t)
  930. for _, params := range tc.mockParams.channelMsgToHostParamsICBM {
  931. icbmSvc.EXPECT().
  932. ChannelMsgToHost(ctx, matchSession(params.sender), wire.SNACFrame{}, params.inBody).
  933. Return(nil, params.err)
  934. }
  935. svc := OSCARProxy{
  936. Logger: slog.Default(),
  937. ICBMService: icbmSvc,
  938. }
  939. msg := svc.RecvClientCmd(ctx, tc.me, tc.givenChatRegistry, tc.givenCmd, nil, nil)
  940. assert.Equal(t, tc.wantMsg, msg)
  941. })
  942. }
  943. }
  944. func TestOSCARProxy_RecvClientCmd_ChatJoin(t *testing.T) {
  945. fnNewChatNavParams := func(err error) chatNavParams {
  946. ret := chatNavParams{
  947. createRoomParams: createRoomParams{
  948. {
  949. me: state.NewIdentScreenName("me"),
  950. inBody: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  951. Exchange: 4,
  952. Cookie: "create",
  953. TLVBlock: wire.TLVBlock{
  954. TLVList: wire.TLVList{
  955. wire.NewTLVBE(wire.ChatRoomTLVRoomName, "cool room"),
  956. },
  957. },
  958. },
  959. },
  960. },
  961. }
  962. if err != nil {
  963. ret.createRoomParams[0].err = err
  964. } else {
  965. ret.createRoomParams[0].msg = wire.SNACMessage{
  966. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  967. TLVRestBlock: wire.TLVRestBlock{
  968. TLVList: wire.TLVList{
  969. wire.NewTLVBE(wire.ChatNavTLVRoomInfo, wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  970. Cookie: "the-cookie",
  971. }),
  972. },
  973. },
  974. },
  975. }
  976. }
  977. return ret
  978. }
  979. fnNewOServiceBOSParams := func(err error) oServiceParams {
  980. ret := oServiceParams{
  981. serviceRequestParams: serviceRequestParams{
  982. {
  983. me: state.NewIdentScreenName("me"),
  984. bodyIn: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  985. FoodGroup: wire.Chat,
  986. TLVRestBlock: wire.TLVRestBlock{
  987. TLVList: wire.TLVList{
  988. wire.NewTLVBE(0x01, wire.SNAC_0x01_0x04_TLVRoomInfo{
  989. Cookie: "the-cookie",
  990. }),
  991. },
  992. },
  993. },
  994. },
  995. },
  996. }
  997. if err != nil {
  998. ret.serviceRequestParams[0].err = err
  999. } else {
  1000. ret.serviceRequestParams[0].msg = wire.SNACMessage{
  1001. Body: wire.SNAC_0x01_0x05_OServiceServiceResponse{
  1002. TLVRestBlock: wire.TLVRestBlock{
  1003. TLVList: wire.TLVList{
  1004. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, "chat-auth-cookie"),
  1005. },
  1006. },
  1007. },
  1008. }
  1009. }
  1010. return ret
  1011. }
  1012. fnNewAuthParams := func(err error) authParams {
  1013. ret := authParams{
  1014. registerChatSessionParams: registerChatSessionParams{
  1015. {
  1016. authCookie: []byte("chat-auth-cookie"),
  1017. },
  1018. },
  1019. }
  1020. if err != nil {
  1021. ret.registerChatSessionParams[0].err = err
  1022. } else {
  1023. ret.registerChatSessionParams[0].sess = newTestSession("me-chat")
  1024. }
  1025. return ret
  1026. }
  1027. fnNewOServiceChatParams := func(err error) oServiceParams {
  1028. ret := oServiceParams{
  1029. clientOnlineParams: clientOnlineParams{
  1030. {
  1031. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  1032. me: state.NewIdentScreenName("me-chat"),
  1033. },
  1034. },
  1035. }
  1036. if err != nil {
  1037. ret.clientOnlineParams[0].err = err
  1038. }
  1039. return ret
  1040. }
  1041. cases := []struct {
  1042. // name is the unit test name
  1043. name string
  1044. // me is the TOC user session
  1045. me *state.Session
  1046. // givenCmd is the TOC command
  1047. givenCmd []byte
  1048. // givenChatRegistry is the chat registry passed to the function
  1049. givenChatRegistry *ChatRegistry
  1050. // wantMsg is the expected TOC response
  1051. wantMsg string
  1052. // expectChatSession indicates whether a chat session should be present
  1053. // in the chat registry
  1054. expectChatSession bool
  1055. // mockParams is the list of params sent to mocks that satisfy this
  1056. // method's dependencies
  1057. mockParams mockParams
  1058. }{
  1059. {
  1060. name: "successfully join chat",
  1061. me: newTestSession("me"),
  1062. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  1063. givenChatRegistry: NewChatRegistry(),
  1064. mockParams: mockParams{
  1065. chatNavParams: fnNewChatNavParams(nil),
  1066. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  1067. authParams: fnNewAuthParams(nil),
  1068. oServiceChatParams: fnNewOServiceChatParams(nil),
  1069. },
  1070. wantMsg: "CHAT_JOIN:0:cool room",
  1071. expectChatSession: true,
  1072. },
  1073. {
  1074. name: "join chat, receive error from chat oservice svc",
  1075. me: newTestSession("me"),
  1076. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  1077. givenChatRegistry: NewChatRegistry(),
  1078. mockParams: mockParams{
  1079. chatNavParams: fnNewChatNavParams(nil),
  1080. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  1081. authParams: fnNewAuthParams(nil),
  1082. oServiceChatParams: fnNewOServiceChatParams(io.EOF),
  1083. },
  1084. wantMsg: cmdInternalSvcErr,
  1085. expectChatSession: false,
  1086. },
  1087. {
  1088. name: "join chat, receive error from auth svc",
  1089. me: newTestSession("me"),
  1090. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  1091. givenChatRegistry: NewChatRegistry(),
  1092. mockParams: mockParams{
  1093. chatNavParams: fnNewChatNavParams(nil),
  1094. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  1095. authParams: fnNewAuthParams(io.EOF),
  1096. },
  1097. wantMsg: cmdInternalSvcErr,
  1098. expectChatSession: false,
  1099. },
  1100. {
  1101. name: "join chat, receive error from BOS oservice svc",
  1102. me: newTestSession("me"),
  1103. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  1104. givenChatRegistry: NewChatRegistry(),
  1105. mockParams: mockParams{
  1106. chatNavParams: fnNewChatNavParams(nil),
  1107. oServiceBOSParams: fnNewOServiceBOSParams(io.EOF),
  1108. },
  1109. wantMsg: cmdInternalSvcErr,
  1110. expectChatSession: false,
  1111. },
  1112. {
  1113. name: "join chat, receive error from chat nav svc",
  1114. me: newTestSession("me"),
  1115. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  1116. givenChatRegistry: NewChatRegistry(),
  1117. mockParams: mockParams{
  1118. chatNavParams: fnNewChatNavParams(io.EOF),
  1119. },
  1120. wantMsg: cmdInternalSvcErr,
  1121. expectChatSession: false,
  1122. },
  1123. {
  1124. name: "bad command",
  1125. me: newTestSession("me"),
  1126. givenCmd: []byte(`toc_chat_join`),
  1127. givenChatRegistry: NewChatRegistry(),
  1128. wantMsg: cmdInternalSvcErr,
  1129. expectChatSession: false,
  1130. },
  1131. {
  1132. name: "bad exchange number",
  1133. me: newTestSession("me"),
  1134. givenCmd: []byte(`toc_chat_join four "cool room"`),
  1135. givenChatRegistry: NewChatRegistry(),
  1136. wantMsg: cmdInternalSvcErr,
  1137. expectChatSession: false,
  1138. },
  1139. }
  1140. for _, tc := range cases {
  1141. t.Run(tc.name, func(t *testing.T) {
  1142. ctx, cancel := context.WithCancel(context.Background())
  1143. cancel()
  1144. chatNavSvc := newMockChatNavService(t)
  1145. for _, params := range tc.mockParams.createRoomParams {
  1146. chatNavSvc.EXPECT().
  1147. CreateRoom(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  1148. Return(params.msg, params.err)
  1149. }
  1150. bosOServiceSvc := newMockOServiceService(t)
  1151. for _, params := range tc.mockParams.oServiceBOSParams.serviceRequestParams {
  1152. bosOServiceSvc.EXPECT().
  1153. ServiceRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.bodyIn).
  1154. Return(params.msg, params.err)
  1155. }
  1156. chatOServiceSvc := newMockOServiceService(t)
  1157. for _, params := range tc.mockParams.oServiceChatParams.clientOnlineParams {
  1158. chatOServiceSvc.EXPECT().
  1159. ClientOnline(ctx, params.body, matchSession(params.me)).
  1160. Return(params.err)
  1161. }
  1162. authSvc := newMockAuthService(t)
  1163. for _, params := range tc.mockParams.authParams.registerChatSessionParams {
  1164. authSvc.EXPECT().
  1165. RegisterChatSession(ctx, params.authCookie).
  1166. Return(params.sess, params.err)
  1167. }
  1168. svc := OSCARProxy{
  1169. AuthService: authSvc,
  1170. ChatNavService: chatNavSvc,
  1171. Logger: slog.Default(),
  1172. OServiceServiceBOS: bosOServiceSvc,
  1173. OServiceServiceChat: chatOServiceSvc,
  1174. }
  1175. g := &errgroup.Group{}
  1176. tc.me.Close()
  1177. msg := svc.RecvClientCmd(ctx, tc.me, tc.givenChatRegistry, tc.givenCmd, nil, g.Go)
  1178. assert.NoError(t, g.Wait())
  1179. assert.Equal(t, tc.wantMsg, msg)
  1180. assert.Equal(t, tc.expectChatSession, len(tc.givenChatRegistry.Sessions()) == 1)
  1181. })
  1182. }
  1183. }
  1184. func TestOSCARProxy_RecvClientCmd_ChatLeave(t *testing.T) {
  1185. cases := []struct {
  1186. // name is the unit test name
  1187. name string
  1188. // me is the TOC user session
  1189. me *state.Session
  1190. // givenCmd is the TOC command
  1191. givenCmd []byte
  1192. // givenChatRegistry is the chat registry passed to the function
  1193. givenChatRegistry *ChatRegistry
  1194. // wantMsg is the expected TOC response
  1195. wantMsg string
  1196. // mockParams is the list of params sent to mocks that satisfy this
  1197. // method's dependencies
  1198. mockParams mockParams
  1199. }{
  1200. {
  1201. name: "successfully leave chat",
  1202. me: newTestSession("me"),
  1203. givenCmd: []byte(`toc_chat_leave 0`),
  1204. givenChatRegistry: func() *ChatRegistry {
  1205. reg := NewChatRegistry()
  1206. reg.RegisterSess(0, newTestSession("me"))
  1207. return reg
  1208. }(),
  1209. mockParams: mockParams{
  1210. authParams: authParams{
  1211. signoutChatParams: signoutChatParams{
  1212. {
  1213. me: state.NewIdentScreenName("me"),
  1214. },
  1215. },
  1216. },
  1217. },
  1218. wantMsg: "CHAT_LEFT:0",
  1219. },
  1220. {
  1221. name: "chat room ID with invalid format",
  1222. givenCmd: []byte(`toc_chat_leave zero`),
  1223. wantMsg: cmdInternalSvcErr,
  1224. },
  1225. {
  1226. name: "missing chat session",
  1227. givenCmd: []byte(`toc_chat_leave 0`),
  1228. givenChatRegistry: NewChatRegistry(),
  1229. wantMsg: cmdInternalSvcErr,
  1230. },
  1231. {
  1232. name: "bad command",
  1233. givenCmd: []byte(`toc_chat_leave`),
  1234. wantMsg: cmdInternalSvcErr,
  1235. },
  1236. }
  1237. for _, tc := range cases {
  1238. t.Run(tc.name, func(t *testing.T) {
  1239. ctx := context.Background()
  1240. authSvc := newMockAuthService(t)
  1241. for _, params := range tc.mockParams.signoutChatParams {
  1242. authSvc.EXPECT().SignoutChat(ctx, matchSession(params.me))
  1243. }
  1244. svc := OSCARProxy{
  1245. Logger: slog.Default(),
  1246. AuthService: authSvc,
  1247. }
  1248. msg := svc.RecvClientCmd(ctx, nil, tc.givenChatRegistry, tc.givenCmd, nil, nil)
  1249. assert.Equal(t, tc.wantMsg, msg)
  1250. })
  1251. }
  1252. }
  1253. func TestOSCARProxy_RecvClientCmd_ChatSend(t *testing.T) {
  1254. cases := []struct {
  1255. // name is the unit test name
  1256. name string
  1257. // me is the TOC user session
  1258. me *state.Session
  1259. // givenCmd is the TOC command
  1260. givenCmd []byte
  1261. // givenChatRegistry is the chat registry passed to the function
  1262. givenChatRegistry *ChatRegistry
  1263. // wantMsg is the expected TOC response
  1264. wantMsg string
  1265. // mockParams is the list of params sent to mocks that satisfy this
  1266. // method's dependencies
  1267. mockParams mockParams
  1268. }{
  1269. {
  1270. name: "successfully send chat message",
  1271. me: newTestSession("me"),
  1272. givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
  1273. givenChatRegistry: func() *ChatRegistry {
  1274. reg := NewChatRegistry()
  1275. reg.RegisterSess(0, newTestSession("me"))
  1276. return reg
  1277. }(),
  1278. mockParams: mockParams{
  1279. chatParams: chatParams{
  1280. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1281. {
  1282. sender: state.NewIdentScreenName("me"),
  1283. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1284. Channel: wire.ICBMChannelMIME,
  1285. TLVRestBlock: wire.TLVRestBlock{
  1286. TLVList: wire.TLVList{
  1287. wire.NewTLVBE(wire.ChatTLVEnableReflectionFlag, uint8(1)),
  1288. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1289. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  1290. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1291. TLVList: wire.TLVList{
  1292. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1293. },
  1294. }),
  1295. },
  1296. },
  1297. },
  1298. result: &wire.SNACMessage{
  1299. Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  1300. Channel: wire.ICBMChannelMIME,
  1301. TLVRestBlock: wire.TLVRestBlock{
  1302. TLVList: wire.TLVList{
  1303. wire.NewTLVBE(wire.ChatTLVSenderInformation,
  1304. newTestSession("me").TLVUserInfo()),
  1305. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  1306. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1307. TLVList: wire.TLVList{
  1308. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1309. },
  1310. }),
  1311. },
  1312. },
  1313. },
  1314. },
  1315. },
  1316. },
  1317. },
  1318. },
  1319. wantMsg: "CHAT_IN:0:me:F:Hello world!",
  1320. },
  1321. {
  1322. name: "send chat message, receive error from chat svc",
  1323. me: newTestSession("me"),
  1324. givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
  1325. givenChatRegistry: func() *ChatRegistry {
  1326. reg := NewChatRegistry()
  1327. reg.RegisterSess(0, newTestSession("me"))
  1328. return reg
  1329. }(),
  1330. mockParams: mockParams{
  1331. chatParams: chatParams{
  1332. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1333. {
  1334. sender: state.NewIdentScreenName("me"),
  1335. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1336. Channel: wire.ICBMChannelMIME,
  1337. TLVRestBlock: wire.TLVRestBlock{
  1338. TLVList: wire.TLVList{
  1339. wire.NewTLVBE(wire.ChatTLVEnableReflectionFlag, uint8(1)),
  1340. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1341. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  1342. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1343. TLVList: wire.TLVList{
  1344. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1345. },
  1346. }),
  1347. },
  1348. },
  1349. },
  1350. err: io.EOF,
  1351. },
  1352. },
  1353. },
  1354. },
  1355. wantMsg: cmdInternalSvcErr,
  1356. },
  1357. {
  1358. name: "send chat message, receive nil response from chat svc",
  1359. me: newTestSession("me"),
  1360. givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
  1361. givenChatRegistry: func() *ChatRegistry {
  1362. reg := NewChatRegistry()
  1363. reg.RegisterSess(0, newTestSession("me"))
  1364. return reg
  1365. }(),
  1366. mockParams: mockParams{
  1367. chatParams: chatParams{
  1368. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1369. {
  1370. sender: state.NewIdentScreenName("me"),
  1371. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1372. Channel: wire.ICBMChannelMIME,
  1373. TLVRestBlock: wire.TLVRestBlock{
  1374. TLVList: wire.TLVList{
  1375. wire.NewTLVBE(wire.ChatTLVEnableReflectionFlag, uint8(1)),
  1376. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1377. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  1378. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1379. TLVList: wire.TLVList{
  1380. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1381. },
  1382. }),
  1383. },
  1384. },
  1385. },
  1386. result: nil,
  1387. },
  1388. },
  1389. },
  1390. },
  1391. wantMsg: cmdInternalSvcErr,
  1392. },
  1393. {
  1394. name: "send chat message, receive unexpected response from chat svc",
  1395. me: newTestSession("me"),
  1396. givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
  1397. givenChatRegistry: func() *ChatRegistry {
  1398. reg := NewChatRegistry()
  1399. reg.RegisterSess(0, newTestSession("me"))
  1400. return reg
  1401. }(),
  1402. mockParams: mockParams{
  1403. chatParams: chatParams{
  1404. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1405. {
  1406. sender: state.NewIdentScreenName("me"),
  1407. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1408. Channel: wire.ICBMChannelMIME,
  1409. TLVRestBlock: wire.TLVRestBlock{
  1410. TLVList: wire.TLVList{
  1411. wire.NewTLVBE(wire.ChatTLVEnableReflectionFlag, uint8(1)),
  1412. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1413. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  1414. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1415. TLVList: wire.TLVList{
  1416. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1417. },
  1418. }),
  1419. },
  1420. },
  1421. },
  1422. result: &wire.SNACMessage{
  1423. Body: wire.SNACError{},
  1424. },
  1425. },
  1426. },
  1427. },
  1428. },
  1429. wantMsg: cmdInternalSvcErr,
  1430. },
  1431. {
  1432. name: "chat room ID with invalid format",
  1433. givenCmd: []byte(`toc_chat_send zero "Hello world!"`),
  1434. wantMsg: cmdInternalSvcErr,
  1435. },
  1436. {
  1437. name: "missing chat session",
  1438. givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
  1439. givenChatRegistry: NewChatRegistry(),
  1440. wantMsg: cmdInternalSvcErr,
  1441. },
  1442. {
  1443. name: "bad command",
  1444. givenCmd: []byte(`toc_chat_send`),
  1445. wantMsg: cmdInternalSvcErr,
  1446. },
  1447. }
  1448. for _, tc := range cases {
  1449. t.Run(tc.name, func(t *testing.T) {
  1450. ctx := context.Background()
  1451. chatSvc := newMockChatService(t)
  1452. for _, params := range tc.mockParams.channelMsgToHostParamsChat {
  1453. chatSvc.EXPECT().
  1454. ChannelMsgToHost(ctx, matchSession(params.sender), wire.SNACFrame{}, params.inBody).
  1455. Return(params.result, params.err)
  1456. }
  1457. svc := OSCARProxy{
  1458. Logger: slog.Default(),
  1459. ChatService: chatSvc,
  1460. }
  1461. msg := svc.RecvClientCmd(ctx, nil, tc.givenChatRegistry, tc.givenCmd, nil, nil)
  1462. assert.Equal(t, tc.wantMsg, msg)
  1463. })
  1464. }
  1465. }
  1466. func TestOSCARProxy_RecvClientCmd_ChatWhisper(t *testing.T) {
  1467. cases := []struct {
  1468. // name is the unit test name
  1469. name string
  1470. // me is the TOC user session
  1471. me *state.Session
  1472. // givenCmd is the TOC command
  1473. givenCmd []byte
  1474. // givenChatRegistry is the chat registry passed to the function
  1475. givenChatRegistry *ChatRegistry
  1476. // wantMsg is the expected TOC response
  1477. wantMsg string
  1478. // mockParams is the list of params sent to mocks that satisfy this
  1479. // method's dependencies
  1480. mockParams mockParams
  1481. }{
  1482. {
  1483. name: "successfully send chat whisper",
  1484. me: newTestSession("me"),
  1485. givenCmd: []byte(`toc_chat_whisper 0 them "Hello world!"`),
  1486. givenChatRegistry: func() *ChatRegistry {
  1487. reg := NewChatRegistry()
  1488. reg.RegisterSess(0, newTestSession("me"))
  1489. return reg
  1490. }(),
  1491. mockParams: mockParams{
  1492. chatParams: chatParams{
  1493. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1494. {
  1495. sender: state.NewIdentScreenName("me"),
  1496. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1497. Channel: wire.ICBMChannelMIME,
  1498. TLVRestBlock: wire.TLVRestBlock{
  1499. TLVList: wire.TLVList{
  1500. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1501. wire.NewTLVBE(wire.ChatTLVWhisperToUser, "them"),
  1502. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1503. TLVList: wire.TLVList{
  1504. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1505. },
  1506. }),
  1507. },
  1508. },
  1509. },
  1510. result: nil,
  1511. },
  1512. },
  1513. },
  1514. },
  1515. wantMsg: "",
  1516. },
  1517. {
  1518. name: "send chat whisper, receive error from chat svc",
  1519. me: newTestSession("me"),
  1520. givenCmd: []byte(`toc_chat_whisper 0 them "Hello world!"`),
  1521. givenChatRegistry: func() *ChatRegistry {
  1522. reg := NewChatRegistry()
  1523. reg.RegisterSess(0, newTestSession("me"))
  1524. return reg
  1525. }(),
  1526. mockParams: mockParams{
  1527. chatParams: chatParams{
  1528. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1529. {
  1530. sender: state.NewIdentScreenName("me"),
  1531. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1532. Channel: wire.ICBMChannelMIME,
  1533. TLVRestBlock: wire.TLVRestBlock{
  1534. TLVList: wire.TLVList{
  1535. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1536. wire.NewTLVBE(wire.ChatTLVWhisperToUser, "them"),
  1537. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1538. TLVList: wire.TLVList{
  1539. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1540. },
  1541. }),
  1542. },
  1543. },
  1544. },
  1545. err: io.EOF,
  1546. },
  1547. },
  1548. },
  1549. },
  1550. wantMsg: cmdInternalSvcErr,
  1551. },
  1552. {
  1553. name: "chat room ID with invalid format",
  1554. givenCmd: []byte(`toc_chat_whisper zero them "Hello world!"`),
  1555. wantMsg: cmdInternalSvcErr,
  1556. },
  1557. {
  1558. name: "missing chat session",
  1559. givenCmd: []byte(`toc_chat_whisper 0 them "Hello world!"`),
  1560. givenChatRegistry: NewChatRegistry(),
  1561. wantMsg: cmdInternalSvcErr,
  1562. },
  1563. {
  1564. name: "bad command",
  1565. givenCmd: []byte(`toc_chat_whisper`),
  1566. wantMsg: cmdInternalSvcErr,
  1567. },
  1568. }
  1569. for _, tc := range cases {
  1570. t.Run(tc.name, func(t *testing.T) {
  1571. ctx := context.Background()
  1572. chatSvc := newMockChatService(t)
  1573. for _, params := range tc.mockParams.channelMsgToHostParamsChat {
  1574. chatSvc.EXPECT().
  1575. ChannelMsgToHost(ctx, matchSession(params.sender), wire.SNACFrame{}, params.inBody).
  1576. Return(params.result, params.err)
  1577. }
  1578. svc := OSCARProxy{
  1579. Logger: slog.Default(),
  1580. ChatService: chatSvc,
  1581. }
  1582. msg := svc.RecvClientCmd(ctx, nil, tc.givenChatRegistry, tc.givenCmd, nil, nil)
  1583. assert.Equal(t, tc.wantMsg, msg)
  1584. })
  1585. }
  1586. }
  1587. func TestOSCARProxy_RecvClientCmd_Evil(t *testing.T) {
  1588. cases := []struct {
  1589. // name is the unit test name
  1590. name string
  1591. // me is the TOC user session
  1592. me *state.Session
  1593. // givenCmd is the TOC command
  1594. givenCmd []byte
  1595. // wantMsg is the expected TOC response
  1596. wantMsg string
  1597. // mockParams is the list of params sent to mocks that satisfy this
  1598. // method's dependencies
  1599. mockParams mockParams
  1600. }{
  1601. {
  1602. name: "successfully warn normally",
  1603. me: newTestSession("me"),
  1604. givenCmd: []byte(`toc_evil them norm`),
  1605. mockParams: mockParams{
  1606. icbmParams: icbmParams{
  1607. evilRequestParams: evilRequestParams{
  1608. {
  1609. me: state.NewIdentScreenName("me"),
  1610. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1611. SendAs: 0,
  1612. ScreenName: "them",
  1613. },
  1614. msg: wire.SNACMessage{
  1615. Body: wire.SNAC_0x04_0x09_ICBMEvilReply{},
  1616. },
  1617. },
  1618. },
  1619. },
  1620. },
  1621. },
  1622. {
  1623. name: "successfully warn anonymously",
  1624. me: newTestSession("me"),
  1625. givenCmd: []byte(`toc_evil them anon`),
  1626. mockParams: mockParams{
  1627. icbmParams: icbmParams{
  1628. evilRequestParams: evilRequestParams{
  1629. {
  1630. me: state.NewIdentScreenName("me"),
  1631. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1632. SendAs: 1,
  1633. ScreenName: "them",
  1634. },
  1635. msg: wire.SNACMessage{
  1636. Body: wire.SNAC_0x04_0x09_ICBMEvilReply{},
  1637. },
  1638. },
  1639. },
  1640. },
  1641. },
  1642. },
  1643. {
  1644. name: "warn, receive error from ICBM service",
  1645. me: newTestSession("me"),
  1646. givenCmd: []byte(`toc_evil them anon`),
  1647. mockParams: mockParams{
  1648. icbmParams: icbmParams{
  1649. evilRequestParams: evilRequestParams{
  1650. {
  1651. me: state.NewIdentScreenName("me"),
  1652. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1653. SendAs: 1,
  1654. ScreenName: "them",
  1655. },
  1656. err: io.EOF,
  1657. },
  1658. },
  1659. },
  1660. },
  1661. wantMsg: cmdInternalSvcErr,
  1662. },
  1663. {
  1664. name: "warn, receive snac err",
  1665. me: newTestSession("me"),
  1666. givenCmd: []byte(`toc_evil them anon`),
  1667. mockParams: mockParams{
  1668. icbmParams: icbmParams{
  1669. evilRequestParams: evilRequestParams{
  1670. {
  1671. me: state.NewIdentScreenName("me"),
  1672. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1673. SendAs: 1,
  1674. ScreenName: "them",
  1675. },
  1676. msg: wire.SNACMessage{
  1677. Body: wire.SNACError{},
  1678. },
  1679. },
  1680. },
  1681. },
  1682. },
  1683. },
  1684. {
  1685. name: "warn, ICBM svc returns unexpected snac type",
  1686. me: newTestSession("me"),
  1687. givenCmd: []byte(`toc_evil them anon`),
  1688. mockParams: mockParams{
  1689. icbmParams: icbmParams{
  1690. evilRequestParams: evilRequestParams{
  1691. {
  1692. me: state.NewIdentScreenName("me"),
  1693. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1694. SendAs: 1,
  1695. ScreenName: "them",
  1696. },
  1697. msg: wire.SNACMessage{
  1698. Body: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{},
  1699. },
  1700. },
  1701. },
  1702. },
  1703. },
  1704. wantMsg: cmdInternalSvcErr,
  1705. },
  1706. {
  1707. name: "warn with incorrect type",
  1708. me: newTestSession("me"),
  1709. givenCmd: []byte(`toc_evil them blah`),
  1710. wantMsg: cmdInternalSvcErr,
  1711. },
  1712. {
  1713. name: "bad command",
  1714. givenCmd: []byte(`toc_evil`),
  1715. wantMsg: cmdInternalSvcErr,
  1716. },
  1717. }
  1718. for _, tc := range cases {
  1719. t.Run(tc.name, func(t *testing.T) {
  1720. ctx := context.Background()
  1721. icbmSvc := newMockICBMService(t)
  1722. for _, params := range tc.mockParams.evilRequestParams {
  1723. icbmSvc.EXPECT().
  1724. EvilRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  1725. Return(params.msg, params.err)
  1726. }
  1727. svc := OSCARProxy{
  1728. Logger: slog.Default(),
  1729. ICBMService: icbmSvc,
  1730. }
  1731. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  1732. assert.Equal(t, tc.wantMsg, msg)
  1733. })
  1734. }
  1735. }
  1736. func TestOSCARProxy_RecvClientCmd_ChangePassword(t *testing.T) {
  1737. cases := []struct {
  1738. // name is the unit test name
  1739. name string
  1740. // me is the TOC user session
  1741. me *state.Session
  1742. // givenCmd is the TOC command
  1743. givenCmd []byte
  1744. // wantMsg is the expected TOC response
  1745. wantMsg string
  1746. // mockParams is the list of params sent to mocks that satisfy this
  1747. // method's dependencies
  1748. mockParams mockParams
  1749. }{
  1750. {
  1751. name: "successfully change password",
  1752. me: newTestSession("me"),
  1753. givenCmd: []byte("toc_change_passwd oldpass newpass"),
  1754. mockParams: mockParams{
  1755. adminParams: adminParams{
  1756. infoChangeRequestParams: infoChangeRequestParams{
  1757. {
  1758. me: state.NewIdentScreenName("me"),
  1759. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1760. TLVRestBlock: wire.TLVRestBlock{
  1761. TLVList: wire.TLVList{
  1762. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1763. wire.NewTLVBE(wire.AdminTLVNewPassword, "newpass"),
  1764. },
  1765. },
  1766. },
  1767. msg: wire.SNACMessage{
  1768. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  1769. Permissions: wire.AdminInfoPermissionsReadWrite,
  1770. TLVBlock: wire.TLVBlock{
  1771. TLVList: wire.TLVList{
  1772. wire.NewTLVBE(wire.AdminTLVNewPassword, []byte{}),
  1773. },
  1774. },
  1775. },
  1776. },
  1777. },
  1778. },
  1779. },
  1780. },
  1781. wantMsg: "ADMIN_PASSWD_STATUS:0",
  1782. },
  1783. {
  1784. name: "change password - invalid password length",
  1785. me: newTestSession("me"),
  1786. givenCmd: []byte("toc_change_passwd oldpass np"),
  1787. mockParams: mockParams{
  1788. adminParams: adminParams{
  1789. infoChangeRequestParams: infoChangeRequestParams{
  1790. {
  1791. me: state.NewIdentScreenName("me"),
  1792. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1793. TLVRestBlock: wire.TLVRestBlock{
  1794. TLVList: wire.TLVList{
  1795. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1796. wire.NewTLVBE(wire.AdminTLVNewPassword, "np"),
  1797. },
  1798. },
  1799. },
  1800. msg: wire.SNACMessage{
  1801. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  1802. Permissions: wire.AdminInfoPermissionsReadWrite,
  1803. TLVBlock: wire.TLVBlock{
  1804. TLVList: wire.TLVList{
  1805. wire.NewTLVBE(wire.AdminTLVNewPassword, []byte{}),
  1806. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorInvalidPasswordLength),
  1807. },
  1808. },
  1809. },
  1810. },
  1811. },
  1812. },
  1813. },
  1814. },
  1815. wantMsg: "ERROR:911",
  1816. },
  1817. {
  1818. name: "change password - incorrect password",
  1819. me: newTestSession("me"),
  1820. givenCmd: []byte("toc_change_passwd oldpass baddpass"),
  1821. mockParams: mockParams{
  1822. adminParams: adminParams{
  1823. infoChangeRequestParams: infoChangeRequestParams{
  1824. {
  1825. me: state.NewIdentScreenName("me"),
  1826. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1827. TLVRestBlock: wire.TLVRestBlock{
  1828. TLVList: wire.TLVList{
  1829. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1830. wire.NewTLVBE(wire.AdminTLVNewPassword, "baddpass"),
  1831. },
  1832. },
  1833. },
  1834. msg: wire.SNACMessage{
  1835. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  1836. Permissions: wire.AdminInfoPermissionsReadWrite,
  1837. TLVBlock: wire.TLVBlock{
  1838. TLVList: wire.TLVList{
  1839. wire.NewTLVBE(wire.AdminTLVNewPassword, []byte{}),
  1840. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorValidatePassword),
  1841. },
  1842. },
  1843. },
  1844. },
  1845. },
  1846. },
  1847. },
  1848. },
  1849. wantMsg: "ERROR:912",
  1850. },
  1851. {
  1852. name: "change password - catch-all error response",
  1853. me: newTestSession("me"),
  1854. givenCmd: []byte("toc_change_passwd oldpass baddpass"),
  1855. mockParams: mockParams{
  1856. adminParams: adminParams{
  1857. infoChangeRequestParams: infoChangeRequestParams{
  1858. {
  1859. me: state.NewIdentScreenName("me"),
  1860. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1861. TLVRestBlock: wire.TLVRestBlock{
  1862. TLVList: wire.TLVList{
  1863. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1864. wire.NewTLVBE(wire.AdminTLVNewPassword, "baddpass"),
  1865. },
  1866. },
  1867. },
  1868. msg: wire.SNACMessage{
  1869. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  1870. Permissions: wire.AdminInfoPermissionsReadWrite,
  1871. TLVBlock: wire.TLVBlock{
  1872. TLVList: wire.TLVList{
  1873. wire.NewTLVBE(wire.AdminTLVNewPassword, []byte{}),
  1874. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorAllOtherErrors),
  1875. },
  1876. },
  1877. },
  1878. },
  1879. },
  1880. },
  1881. },
  1882. },
  1883. wantMsg: "ERROR:913",
  1884. },
  1885. {
  1886. name: "change password - runtime error from admin svc",
  1887. me: newTestSession("me"),
  1888. givenCmd: []byte("toc_change_passwd oldpass baddpass"),
  1889. mockParams: mockParams{
  1890. adminParams: adminParams{
  1891. infoChangeRequestParams: infoChangeRequestParams{
  1892. {
  1893. me: state.NewIdentScreenName("me"),
  1894. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1895. TLVRestBlock: wire.TLVRestBlock{
  1896. TLVList: wire.TLVList{
  1897. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1898. wire.NewTLVBE(wire.AdminTLVNewPassword, "baddpass"),
  1899. },
  1900. },
  1901. },
  1902. err: io.EOF,
  1903. },
  1904. },
  1905. },
  1906. },
  1907. wantMsg: cmdInternalSvcErr,
  1908. },
  1909. {
  1910. name: "change password - unexpected response from admin svc",
  1911. me: newTestSession("me"),
  1912. givenCmd: []byte("toc_change_passwd oldpass baddpass"),
  1913. mockParams: mockParams{
  1914. adminParams: adminParams{
  1915. infoChangeRequestParams: infoChangeRequestParams{
  1916. {
  1917. me: state.NewIdentScreenName("me"),
  1918. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1919. TLVRestBlock: wire.TLVRestBlock{
  1920. TLVList: wire.TLVList{
  1921. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1922. wire.NewTLVBE(wire.AdminTLVNewPassword, "baddpass"),
  1923. },
  1924. },
  1925. },
  1926. msg: wire.SNACMessage{
  1927. Body: wire.SNACError{},
  1928. },
  1929. },
  1930. },
  1931. },
  1932. },
  1933. wantMsg: cmdInternalSvcErr,
  1934. },
  1935. {
  1936. name: "bad command",
  1937. givenCmd: []byte(`toc_change_passwd`),
  1938. wantMsg: cmdInternalSvcErr,
  1939. },
  1940. }
  1941. for _, tc := range cases {
  1942. t.Run(tc.name, func(t *testing.T) {
  1943. ctx := context.Background()
  1944. adminSvc := newMockAdminService(t)
  1945. for _, params := range tc.mockParams.infoChangeRequestParams {
  1946. adminSvc.EXPECT().
  1947. InfoChangeRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  1948. Return(params.msg, params.err)
  1949. }
  1950. svc := OSCARProxy{
  1951. Logger: slog.Default(),
  1952. AdminService: adminSvc,
  1953. }
  1954. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  1955. assert.Equal(t, tc.wantMsg, msg)
  1956. })
  1957. }
  1958. }
  1959. func TestOSCARProxy_RecvClientCmd_GetDirSearchURL(t *testing.T) {
  1960. cases := []struct {
  1961. // name is the unit test name
  1962. name string
  1963. // me is the TOC user session
  1964. me *state.Session
  1965. // givenCmd is the TOC command
  1966. givenCmd []byte
  1967. // wantMsg is the expected TOC response
  1968. wantMsg string
  1969. // mockParams is the list of params sent to mocks that satisfy this
  1970. // method's dependencies
  1971. mockParams mockParams
  1972. }{
  1973. {
  1974. name: "successfully request user info",
  1975. me: newTestSession("me"),
  1976. givenCmd: []byte(`toc_dir_search "first name":"middle name":"last name":"maiden name":"city":"state":"country":"email"`),
  1977. mockParams: mockParams{
  1978. cookieBakerParams: cookieBakerParams{
  1979. issueParams: issueParams{
  1980. {
  1981. data: []byte("me"),
  1982. returnData: []byte("monster"),
  1983. },
  1984. },
  1985. },
  1986. },
  1987. wantMsg: "GOTO_URL:search results:dir_search?city=city&cookie=6d6f6e73746572&country=country&email=email&first_name=first+name&last_name=last+name&maiden_name=maiden+name&middle_name=middle+name&state=state",
  1988. },
  1989. {
  1990. name: "successfully request user info by keywords",
  1991. me: newTestSession("me"),
  1992. givenCmd: []byte(`toc_dir_search ::::::::::"searchkw"`),
  1993. mockParams: mockParams{
  1994. cookieBakerParams: cookieBakerParams{
  1995. issueParams: issueParams{
  1996. {
  1997. data: []byte("me"),
  1998. returnData: []byte("monster"),
  1999. },
  2000. },
  2001. },
  2002. },
  2003. wantMsg: "GOTO_URL:search results:dir_search?cookie=6d6f6e73746572&keyword=searchkw",
  2004. },
  2005. {
  2006. name: "request user info with too many params",
  2007. me: newTestSession("me"),
  2008. givenCmd: []byte(`toc_dir_search ::::::::::::::::::::"searchkw"`),
  2009. wantMsg: cmdInternalSvcErr,
  2010. },
  2011. {
  2012. name: "request user info, get cookie issue error",
  2013. me: newTestSession("me"),
  2014. givenCmd: []byte(`toc_dir_search them`),
  2015. mockParams: mockParams{
  2016. cookieBakerParams: cookieBakerParams{
  2017. issueParams: issueParams{
  2018. {
  2019. data: []byte("me"),
  2020. returnErr: io.EOF,
  2021. },
  2022. },
  2023. },
  2024. },
  2025. wantMsg: cmdInternalSvcErr,
  2026. },
  2027. {
  2028. name: "bad command",
  2029. givenCmd: []byte(`toc_dir_search`),
  2030. wantMsg: cmdInternalSvcErr,
  2031. },
  2032. }
  2033. for _, tc := range cases {
  2034. t.Run(tc.name, func(t *testing.T) {
  2035. ctx := context.Background()
  2036. cookieBaker := newMockCookieBaker(t)
  2037. for _, params := range tc.mockParams.issueParams {
  2038. cookieBaker.EXPECT().
  2039. Issue(params.data).
  2040. Return(params.returnData, params.returnErr)
  2041. }
  2042. svc := OSCARProxy{
  2043. Logger: slog.Default(),
  2044. CookieBaker: cookieBaker,
  2045. }
  2046. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  2047. assert.Equal(t, tc.wantMsg, msg)
  2048. })
  2049. }
  2050. }
  2051. func TestOSCARProxy_RecvClientCmd_GetDirURL(t *testing.T) {
  2052. cases := []struct {
  2053. // name is the unit test name
  2054. name string
  2055. // me is the TOC user session
  2056. me *state.Session
  2057. // givenCmd is the TOC command
  2058. givenCmd []byte
  2059. // wantMsg is the expected TOC response
  2060. wantMsg string
  2061. // mockParams is the list of params sent to mocks that satisfy this
  2062. // method's dependencies
  2063. mockParams mockParams
  2064. }{
  2065. {
  2066. name: "successfully request user dir info",
  2067. me: newTestSession("me"),
  2068. givenCmd: []byte(`toc_get_dir them`),
  2069. mockParams: mockParams{
  2070. cookieBakerParams: cookieBakerParams{
  2071. issueParams: issueParams{
  2072. {
  2073. data: []byte("me"),
  2074. returnData: []byte("monster"),
  2075. },
  2076. },
  2077. },
  2078. },
  2079. wantMsg: "GOTO_URL:directory info:dir_info?cookie=6d6f6e73746572&user=them",
  2080. },
  2081. {
  2082. name: "request user info, get cookie issue error",
  2083. me: newTestSession("me"),
  2084. givenCmd: []byte(`toc_get_dir them`),
  2085. mockParams: mockParams{
  2086. cookieBakerParams: cookieBakerParams{
  2087. issueParams: issueParams{
  2088. {
  2089. data: []byte("me"),
  2090. returnErr: io.EOF,
  2091. },
  2092. },
  2093. },
  2094. },
  2095. wantMsg: cmdInternalSvcErr,
  2096. },
  2097. {
  2098. name: "bad command",
  2099. givenCmd: []byte(`toc_get_dir`),
  2100. wantMsg: cmdInternalSvcErr,
  2101. },
  2102. }
  2103. for _, tc := range cases {
  2104. t.Run(tc.name, func(t *testing.T) {
  2105. ctx := context.Background()
  2106. cookieBaker := newMockCookieBaker(t)
  2107. for _, params := range tc.mockParams.issueParams {
  2108. cookieBaker.EXPECT().
  2109. Issue(params.data).
  2110. Return(params.returnData, params.returnErr)
  2111. }
  2112. svc := OSCARProxy{
  2113. Logger: slog.Default(),
  2114. CookieBaker: cookieBaker,
  2115. }
  2116. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  2117. assert.Equal(t, tc.wantMsg, msg)
  2118. })
  2119. }
  2120. }
  2121. func TestOSCARProxy_RecvClientCmd_GetInfoURL(t *testing.T) {
  2122. cases := []struct {
  2123. // name is the unit test name
  2124. name string
  2125. // me is the TOC user session
  2126. me *state.Session
  2127. // givenCmd is the TOC command
  2128. givenCmd []byte
  2129. // wantMsg is the expected TOC response
  2130. wantMsg string
  2131. // mockParams is the list of params sent to mocks that satisfy this
  2132. // method's dependencies
  2133. mockParams mockParams
  2134. }{
  2135. {
  2136. name: "successfully request user info",
  2137. me: newTestSession("me"),
  2138. givenCmd: []byte(`toc_get_info them`),
  2139. mockParams: mockParams{
  2140. cookieBakerParams: cookieBakerParams{
  2141. issueParams: issueParams{
  2142. {
  2143. data: []byte("me"),
  2144. returnData: []byte("monster"),
  2145. },
  2146. },
  2147. },
  2148. },
  2149. wantMsg: "GOTO_URL:profile:info?cookie=6d6f6e73746572&from=me&user=them",
  2150. },
  2151. {
  2152. name: "request user info, get cookie issue error",
  2153. me: newTestSession("me"),
  2154. givenCmd: []byte(`toc_get_info them`),
  2155. mockParams: mockParams{
  2156. cookieBakerParams: cookieBakerParams{
  2157. issueParams: issueParams{
  2158. {
  2159. data: []byte("me"),
  2160. returnErr: io.EOF,
  2161. },
  2162. },
  2163. },
  2164. },
  2165. wantMsg: cmdInternalSvcErr,
  2166. },
  2167. {
  2168. name: "bad command",
  2169. givenCmd: []byte(`toc_get_info`),
  2170. wantMsg: cmdInternalSvcErr,
  2171. },
  2172. }
  2173. for _, tc := range cases {
  2174. t.Run(tc.name, func(t *testing.T) {
  2175. ctx := context.Background()
  2176. cookieBaker := newMockCookieBaker(t)
  2177. for _, params := range tc.mockParams.issueParams {
  2178. cookieBaker.EXPECT().
  2179. Issue(params.data).
  2180. Return(params.returnData, params.returnErr)
  2181. }
  2182. svc := OSCARProxy{
  2183. Logger: slog.Default(),
  2184. CookieBaker: cookieBaker,
  2185. }
  2186. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  2187. assert.Equal(t, tc.wantMsg, msg)
  2188. })
  2189. }
  2190. }
  2191. func TestOSCARProxy_RecvClientCmd_GetStatus(t *testing.T) {
  2192. cases := []struct {
  2193. // name is the unit test name
  2194. name string
  2195. // me is the TOC user session
  2196. me *state.Session
  2197. // givenCmd is the TOC command
  2198. givenCmd []byte
  2199. // wantMsg is the expected TOC response
  2200. wantMsg string
  2201. // mockParams is the list of params sent to mocks that satisfy this
  2202. // method's dependencies
  2203. mockParams mockParams
  2204. }{
  2205. {
  2206. name: "successfully request status",
  2207. me: newTestSession("me"),
  2208. givenCmd: []byte("toc_get_status them"),
  2209. mockParams: mockParams{
  2210. locateParams: locateParams{
  2211. userInfoQueryParams: userInfoQueryParams{
  2212. {
  2213. me: state.NewIdentScreenName("me"),
  2214. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2215. ScreenName: "them",
  2216. },
  2217. msg: wire.SNACMessage{
  2218. Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  2219. TLVUserInfo: wire.TLVUserInfo{
  2220. ScreenName: "them",
  2221. WarningLevel: 0,
  2222. TLVBlock: wire.TLVBlock{
  2223. TLVList: wire.TLVList{
  2224. wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
  2225. wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
  2226. },
  2227. },
  2228. },
  2229. },
  2230. },
  2231. },
  2232. },
  2233. },
  2234. },
  2235. wantMsg: "UPDATE_BUDDY:them:T:0:1234:5678: O ",
  2236. },
  2237. {
  2238. name: "request status, receive err from locate svc",
  2239. me: newTestSession("me"),
  2240. givenCmd: []byte("toc_get_status them"),
  2241. mockParams: mockParams{
  2242. locateParams: locateParams{
  2243. userInfoQueryParams: userInfoQueryParams{
  2244. {
  2245. me: state.NewIdentScreenName("me"),
  2246. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2247. ScreenName: "them",
  2248. },
  2249. err: io.EOF,
  2250. },
  2251. },
  2252. },
  2253. },
  2254. wantMsg: cmdInternalSvcErr,
  2255. },
  2256. {
  2257. name: "request status, user not online",
  2258. me: newTestSession("me"),
  2259. givenCmd: []byte("toc_get_status them"),
  2260. mockParams: mockParams{
  2261. locateParams: locateParams{
  2262. userInfoQueryParams: userInfoQueryParams{
  2263. {
  2264. me: state.NewIdentScreenName("me"),
  2265. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2266. ScreenName: "them",
  2267. },
  2268. msg: wire.SNACMessage{
  2269. Body: wire.SNACError{
  2270. Code: wire.ErrorCodeNotLoggedOn,
  2271. },
  2272. },
  2273. },
  2274. },
  2275. },
  2276. },
  2277. wantMsg: "ERROR:901:them",
  2278. },
  2279. {
  2280. name: "request status, receive unexpected error code",
  2281. me: newTestSession("me"),
  2282. givenCmd: []byte("toc_get_status them"),
  2283. mockParams: mockParams{
  2284. locateParams: locateParams{
  2285. userInfoQueryParams: userInfoQueryParams{
  2286. {
  2287. me: state.NewIdentScreenName("me"),
  2288. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2289. ScreenName: "them",
  2290. },
  2291. msg: wire.SNACMessage{
  2292. Body: wire.SNACError{
  2293. Code: wire.ErrorCodeInvalidSnac,
  2294. },
  2295. },
  2296. },
  2297. },
  2298. },
  2299. },
  2300. wantMsg: cmdInternalSvcErr,
  2301. },
  2302. {
  2303. name: "request status, unexpected response from locate svc",
  2304. me: newTestSession("me"),
  2305. givenCmd: []byte("toc_get_status them"),
  2306. mockParams: mockParams{
  2307. locateParams: locateParams{
  2308. userInfoQueryParams: userInfoQueryParams{
  2309. {
  2310. me: state.NewIdentScreenName("me"),
  2311. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2312. ScreenName: "them",
  2313. },
  2314. msg: wire.SNACMessage{
  2315. Body: wire.SNAC_0x0E_0x04_ChatUsersLeft{},
  2316. },
  2317. },
  2318. },
  2319. },
  2320. },
  2321. wantMsg: cmdInternalSvcErr,
  2322. },
  2323. {
  2324. name: "bad command",
  2325. givenCmd: []byte(`toc_get_status`),
  2326. wantMsg: cmdInternalSvcErr,
  2327. },
  2328. }
  2329. for _, tc := range cases {
  2330. t.Run(tc.name, func(t *testing.T) {
  2331. ctx := context.Background()
  2332. locateSvc := newMockLocateService(t)
  2333. for _, params := range tc.mockParams.userInfoQueryParams {
  2334. locateSvc.EXPECT().
  2335. UserInfoQuery(mock.Anything, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  2336. Return(params.msg, params.err)
  2337. }
  2338. svc := OSCARProxy{
  2339. Logger: slog.Default(),
  2340. LocateService: locateSvc,
  2341. }
  2342. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  2343. assert.Equal(t, tc.wantMsg, msg)
  2344. })
  2345. }
  2346. }
  2347. func TestOSCARProxy_RecvClientCmd_InitDone(t *testing.T) {
  2348. cases := []struct {
  2349. // name is the unit test name
  2350. name string
  2351. // me is the TOC user session
  2352. me *state.Session
  2353. // givenCmd is the TOC command
  2354. givenCmd []byte
  2355. // wantMsg is the expected TOC response
  2356. wantMsg string
  2357. // mockParams is the list of params sent to mocks that satisfy this
  2358. // method's dependencies
  2359. mockParams mockParams
  2360. }{
  2361. {
  2362. name: "successfully initialize connection",
  2363. me: newTestSession("me"),
  2364. givenCmd: []byte(`toc_init_done`),
  2365. mockParams: mockParams{
  2366. oServiceBOSParams: oServiceParams{
  2367. clientOnlineParams: clientOnlineParams{
  2368. {
  2369. me: state.NewIdentScreenName("me"),
  2370. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  2371. },
  2372. },
  2373. },
  2374. },
  2375. },
  2376. {
  2377. name: "initialize connection, receive err from BOS oservice svc",
  2378. me: newTestSession("me"),
  2379. givenCmd: []byte(`toc_init_done`),
  2380. mockParams: mockParams{
  2381. oServiceBOSParams: oServiceParams{
  2382. clientOnlineParams: clientOnlineParams{
  2383. {
  2384. me: state.NewIdentScreenName("me"),
  2385. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  2386. err: io.EOF,
  2387. },
  2388. },
  2389. },
  2390. },
  2391. wantMsg: cmdInternalSvcErr,
  2392. },
  2393. }
  2394. for _, tc := range cases {
  2395. t.Run(tc.name, func(t *testing.T) {
  2396. ctx := context.Background()
  2397. oSvc := newMockOServiceService(t)
  2398. for _, params := range tc.mockParams.oServiceBOSParams.clientOnlineParams {
  2399. oSvc.EXPECT().
  2400. ClientOnline(ctx, params.body, matchSession(params.me)).
  2401. Return(params.err)
  2402. }
  2403. svc := OSCARProxy{
  2404. Logger: slog.Default(),
  2405. OServiceServiceBOS: oSvc,
  2406. }
  2407. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  2408. assert.Equal(t, tc.wantMsg, msg)
  2409. })
  2410. }
  2411. }
  2412. func TestOSCARProxy_RecvClientCmd_RemoveBuddy(t *testing.T) {
  2413. cases := []struct {
  2414. // name is the unit test name
  2415. name string
  2416. // me is the TOC user session
  2417. me *state.Session
  2418. // givenCmd is the TOC command
  2419. givenCmd []byte
  2420. // wantMsg is the expected TOC response
  2421. wantMsg string
  2422. // mockParams is the list of params sent to mocks that satisfy this
  2423. // method's dependencies
  2424. mockParams mockParams
  2425. }{
  2426. {
  2427. name: "successfully remove buddies",
  2428. me: newTestSession("me"),
  2429. givenCmd: []byte("toc_remove_buddy friend1 friend2 friend3"),
  2430. mockParams: mockParams{
  2431. buddyParams: buddyParams{
  2432. delBuddiesParams: delBuddiesParams{
  2433. {
  2434. me: state.NewIdentScreenName("me"),
  2435. inBody: wire.SNAC_0x03_0x05_BuddyDelBuddies{
  2436. Buddies: []struct {
  2437. ScreenName string `oscar:"len_prefix=uint8"`
  2438. }{
  2439. {ScreenName: "friend1"},
  2440. {ScreenName: "friend2"},
  2441. {ScreenName: "friend3"},
  2442. },
  2443. },
  2444. },
  2445. },
  2446. },
  2447. },
  2448. },
  2449. {
  2450. name: "remove buddies with empty list",
  2451. me: newTestSession("me"),
  2452. givenCmd: []byte("toc_remove_buddy"),
  2453. mockParams: mockParams{
  2454. buddyParams: buddyParams{
  2455. delBuddiesParams: delBuddiesParams{
  2456. {
  2457. me: state.NewIdentScreenName("me"),
  2458. inBody: wire.SNAC_0x03_0x05_BuddyDelBuddies{},
  2459. },
  2460. },
  2461. },
  2462. },
  2463. },
  2464. {
  2465. name: "remove buddies, receive error from buddy service",
  2466. me: newTestSession("me"),
  2467. givenCmd: []byte("toc_remove_buddy friend1"),
  2468. mockParams: mockParams{
  2469. buddyParams: buddyParams{
  2470. delBuddiesParams: delBuddiesParams{
  2471. {
  2472. me: state.NewIdentScreenName("me"),
  2473. inBody: wire.SNAC_0x03_0x05_BuddyDelBuddies{
  2474. Buddies: []struct {
  2475. ScreenName string `oscar:"len_prefix=uint8"`
  2476. }{
  2477. {ScreenName: "friend1"},
  2478. },
  2479. },
  2480. err: io.EOF,
  2481. },
  2482. },
  2483. },
  2484. },
  2485. wantMsg: cmdInternalSvcErr,
  2486. },
  2487. }
  2488. for _, tc := range cases {
  2489. t.Run(tc.name, func(t *testing.T) {
  2490. ctx := context.Background()
  2491. buddySvc := newMockBuddyService(t)
  2492. for _, params := range tc.mockParams.delBuddiesParams {
  2493. buddySvc.EXPECT().
  2494. DelBuddies(ctx, matchSession(params.me), params.inBody).
  2495. Return(params.err)
  2496. }
  2497. svc := OSCARProxy{
  2498. Logger: slog.Default(),
  2499. BuddyService: buddySvc,
  2500. }
  2501. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  2502. assert.Equal(t, tc.wantMsg, msg)
  2503. })
  2504. }
  2505. }
  2506. func TestOSCARProxy_RecvClientCmd_RvousAccept(t *testing.T) {
  2507. cases := []struct {
  2508. // name is the unit test name
  2509. name string
  2510. // me is the TOC user session
  2511. me *state.Session
  2512. // givenCmd is the TOC command
  2513. givenCmd []byte
  2514. // wantMsg is the expected TOC response
  2515. wantMsg string
  2516. // mockParams is the list of params sent to mocks that satisfy this
  2517. // method's dependencies
  2518. mockParams mockParams
  2519. }{
  2520. {
  2521. name: "successfully send rendezvous request",
  2522. me: newTestSession("me"),
  2523. givenCmd: []byte(`toc_rvous_accept them aGFoYWhhaGE= 09461343-4C7F-11D1-8222-444553540000`),
  2524. mockParams: mockParams{
  2525. icbmParams: icbmParams{
  2526. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  2527. {
  2528. sender: state.NewIdentScreenName("me"),
  2529. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2530. ChannelID: wire.ICBMChannelRendezvous,
  2531. ScreenName: "them",
  2532. TLVRestBlock: wire.TLVRestBlock{
  2533. TLVList: wire.TLVList{
  2534. wire.NewTLVBE(wire.ICBMTLVData, wire.ICBMCh2Fragment{
  2535. Type: wire.ICBMRdvMessageAccept,
  2536. Cookie: [8]byte{'h', 'a', 'h', 'a', 'h', 'a', 'h', 'a'},
  2537. Capability: wire.CapFileTransfer,
  2538. }),
  2539. },
  2540. },
  2541. },
  2542. },
  2543. },
  2544. },
  2545. },
  2546. },
  2547. {
  2548. name: "send rendezvous request, receive error from ICBM service",
  2549. me: newTestSession("me"),
  2550. givenCmd: []byte(`toc_rvous_accept them aGFoYWhhaGE= 09461343-4C7F-11D1-8222-444553540000`),
  2551. mockParams: mockParams{
  2552. icbmParams: icbmParams{
  2553. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  2554. {
  2555. sender: state.NewIdentScreenName("me"),
  2556. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2557. ChannelID: wire.ICBMChannelRendezvous,
  2558. ScreenName: "them",
  2559. TLVRestBlock: wire.TLVRestBlock{
  2560. TLVList: wire.TLVList{
  2561. wire.NewTLVBE(wire.ICBMTLVData, wire.ICBMCh2Fragment{
  2562. Type: wire.ICBMRdvMessageAccept,
  2563. Cookie: [8]byte{'h', 'a', 'h', 'a', 'h', 'a', 'h', 'a'},
  2564. Capability: wire.CapFileTransfer,
  2565. }),
  2566. },
  2567. },
  2568. },
  2569. err: io.EOF,
  2570. },
  2571. },
  2572. },
  2573. },
  2574. wantMsg: cmdInternalSvcErr,
  2575. },
  2576. {
  2577. name: "bad command",
  2578. givenCmd: []byte(`toc_rvous_accept`),
  2579. wantMsg: cmdInternalSvcErr,
  2580. },
  2581. }
  2582. for _, tc := range cases {
  2583. t.Run(tc.name, func(t *testing.T) {
  2584. ctx := context.Background()
  2585. icbmSvc := newMockICBMService(t)
  2586. for _, params := range tc.mockParams.channelMsgToHostParamsICBM {
  2587. icbmSvc.EXPECT().
  2588. ChannelMsgToHost(ctx, matchSession(params.sender), params.inFrame, params.inBody).
  2589. Return(params.result, params.err)
  2590. }
  2591. svc := OSCARProxy{
  2592. Logger: slog.Default(),
  2593. ICBMService: icbmSvc,
  2594. }
  2595. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  2596. assert.Equal(t, tc.wantMsg, msg)
  2597. })
  2598. }
  2599. }
  2600. func TestOSCARProxy_RecvClientCmd_RvousCancel(t *testing.T) {
  2601. cases := []struct {
  2602. // name is the unit test name
  2603. name string
  2604. // me is the TOC user session
  2605. me *state.Session
  2606. // givenCmd is the TOC command
  2607. givenCmd []byte
  2608. // wantMsg is the expected TOC response
  2609. wantMsg string
  2610. // mockParams is the list of params sent to mocks that satisfy this
  2611. // method's dependencies
  2612. mockParams mockParams
  2613. }{
  2614. {
  2615. name: "successfully send rendezvous cancellation",
  2616. me: newTestSession("me"),
  2617. givenCmd: []byte(`toc_rvous_cancel them aGFoYWhhaGE= 09461343-4C7F-11D1-8222-444553540000`),
  2618. mockParams: mockParams{
  2619. icbmParams: icbmParams{
  2620. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  2621. {
  2622. sender: state.NewIdentScreenName("me"),
  2623. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2624. ChannelID: wire.ICBMChannelRendezvous,
  2625. ScreenName: "them",
  2626. TLVRestBlock: wire.TLVRestBlock{
  2627. TLVList: wire.TLVList{
  2628. wire.NewTLVBE(wire.ICBMTLVData, wire.ICBMCh2Fragment{
  2629. Type: wire.ICBMRdvMessageCancel,
  2630. Cookie: [8]byte{'h', 'a', 'h', 'a', 'h', 'a', 'h', 'a'},
  2631. Capability: wire.CapFileTransfer,
  2632. TLVRestBlock: wire.TLVRestBlock{
  2633. TLVList: wire.TLVList{
  2634. wire.NewTLVBE(wire.ICBMRdvTLVTagsCancelReason, wire.ICBMRdvCancelReasonsUserCancel),
  2635. },
  2636. },
  2637. }),
  2638. },
  2639. },
  2640. },
  2641. },
  2642. },
  2643. },
  2644. },
  2645. },
  2646. {
  2647. name: "send rendezvous cancellation, receive error from ICBM service",
  2648. me: newTestSession("me"),
  2649. givenCmd: []byte(`toc_rvous_cancel them aGFoYWhhaGE= 09461343-4C7F-11D1-8222-444553540000`),
  2650. mockParams: mockParams{
  2651. icbmParams: icbmParams{
  2652. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  2653. {
  2654. sender: state.NewIdentScreenName("me"),
  2655. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2656. ChannelID: wire.ICBMChannelRendezvous,
  2657. ScreenName: "them",
  2658. TLVRestBlock: wire.TLVRestBlock{
  2659. TLVList: wire.TLVList{
  2660. wire.NewTLVBE(wire.ICBMTLVData, wire.ICBMCh2Fragment{
  2661. Type: wire.ICBMRdvMessageCancel,
  2662. Cookie: [8]byte{'h', 'a', 'h', 'a', 'h', 'a', 'h', 'a'},
  2663. Capability: wire.CapFileTransfer,
  2664. TLVRestBlock: wire.TLVRestBlock{
  2665. TLVList: wire.TLVList{
  2666. wire.NewTLVBE(wire.ICBMRdvTLVTagsCancelReason, wire.ICBMRdvCancelReasonsUserCancel),
  2667. },
  2668. },
  2669. }),
  2670. },
  2671. },
  2672. },
  2673. err: io.EOF,
  2674. },
  2675. },
  2676. },
  2677. },
  2678. wantMsg: cmdInternalSvcErr,
  2679. },
  2680. {
  2681. name: "bad command",
  2682. givenCmd: []byte(`toc_rvous_cancel`),
  2683. wantMsg: cmdInternalSvcErr,
  2684. },
  2685. }
  2686. for _, tc := range cases {
  2687. t.Run(tc.name, func(t *testing.T) {
  2688. ctx := context.Background()
  2689. icbmSvc := newMockICBMService(t)
  2690. for _, params := range tc.mockParams.channelMsgToHostParamsICBM {
  2691. icbmSvc.EXPECT().
  2692. ChannelMsgToHost(ctx, matchSession(params.sender), params.inFrame, params.inBody).
  2693. Return(params.result, params.err)
  2694. }
  2695. svc := OSCARProxy{
  2696. Logger: slog.Default(),
  2697. ICBMService: icbmSvc,
  2698. }
  2699. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  2700. assert.Equal(t, tc.wantMsg, msg)
  2701. })
  2702. }
  2703. }
  2704. func TestOSCARProxy_RecvClientCmd_SendIM(t *testing.T) {
  2705. cases := []struct {
  2706. // name is the unit test name
  2707. name string
  2708. // me is the TOC user session
  2709. me *state.Session
  2710. // givenCmd is the TOC command
  2711. givenCmd []byte
  2712. // wantMsg is the expected TOC response
  2713. wantMsg string
  2714. // mockParams is the list of params sent to mocks that satisfy this
  2715. // method's dependencies
  2716. mockParams mockParams
  2717. }{
  2718. {
  2719. name: "successfully send instant message",
  2720. me: newTestSession("me"),
  2721. givenCmd: []byte(`toc_send_im chattingChuck "hello world!"`),
  2722. mockParams: mockParams{
  2723. icbmParams: icbmParams{
  2724. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  2725. {
  2726. sender: state.NewIdentScreenName("me"),
  2727. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2728. ChannelID: wire.ICBMChannelIM,
  2729. ScreenName: "chattingChuck",
  2730. TLVRestBlock: wire.TLVRestBlock{
  2731. TLVList: wire.TLVList{
  2732. wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
  2733. {
  2734. ID: 5,
  2735. Version: 1,
  2736. Payload: []byte{1, 1, 2},
  2737. },
  2738. {
  2739. ID: 1,
  2740. Version: 1,
  2741. Payload: []byte{
  2742. 0x00, 0x00,
  2743. 0x00, 0x00,
  2744. 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
  2745. },
  2746. },
  2747. }),
  2748. },
  2749. },
  2750. },
  2751. },
  2752. },
  2753. },
  2754. },
  2755. },
  2756. {
  2757. name: "successfully auto-reply send instant message",
  2758. me: newTestSession("me"),
  2759. givenCmd: []byte(`toc_send_im chattingChuck "hello world!" auto`),
  2760. mockParams: mockParams{
  2761. icbmParams: icbmParams{
  2762. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  2763. {
  2764. sender: state.NewIdentScreenName("me"),
  2765. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2766. ChannelID: wire.ICBMChannelIM,
  2767. ScreenName: "chattingChuck",
  2768. TLVRestBlock: wire.TLVRestBlock{
  2769. TLVList: wire.TLVList{
  2770. wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
  2771. {
  2772. ID: 5,
  2773. Version: 1,
  2774. Payload: []byte{1, 1, 2},
  2775. },
  2776. {
  2777. ID: 1,
  2778. Version: 1,
  2779. Payload: []byte{
  2780. 0x00, 0x00,
  2781. 0x00, 0x00,
  2782. 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
  2783. },
  2784. },
  2785. }),
  2786. wire.NewTLVBE(wire.ICBMTLVAutoResponse, []byte{}),
  2787. },
  2788. },
  2789. },
  2790. },
  2791. },
  2792. },
  2793. },
  2794. },
  2795. {
  2796. name: "send instant message, receive error from ICBM service",
  2797. me: newTestSession("me"),
  2798. givenCmd: []byte(`toc_send_im chattingChuck "hello world!"`),
  2799. mockParams: mockParams{
  2800. icbmParams: icbmParams{
  2801. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  2802. {
  2803. sender: state.NewIdentScreenName("me"),
  2804. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2805. ChannelID: wire.ICBMChannelIM,
  2806. ScreenName: "chattingChuck",
  2807. TLVRestBlock: wire.TLVRestBlock{
  2808. TLVList: wire.TLVList{
  2809. wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
  2810. {
  2811. ID: 5,
  2812. Version: 1,
  2813. Payload: []byte{1, 1, 2},
  2814. },
  2815. {
  2816. ID: 1,
  2817. Version: 1,
  2818. Payload: []byte{
  2819. 0x00, 0x00,
  2820. 0x00, 0x00,
  2821. 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
  2822. },
  2823. },
  2824. }),
  2825. },
  2826. },
  2827. },
  2828. err: io.EOF,
  2829. },
  2830. },
  2831. },
  2832. },
  2833. wantMsg: cmdInternalSvcErr,
  2834. },
  2835. {
  2836. name: "bad command",
  2837. givenCmd: []byte(`toc_send_im`),
  2838. wantMsg: cmdInternalSvcErr,
  2839. },
  2840. }
  2841. for _, tc := range cases {
  2842. t.Run(tc.name, func(t *testing.T) {
  2843. ctx := context.Background()
  2844. icbmSvc := newMockICBMService(t)
  2845. for _, params := range tc.mockParams.channelMsgToHostParamsICBM {
  2846. icbmSvc.EXPECT().
  2847. ChannelMsgToHost(ctx, matchSession(params.sender), params.inFrame, params.inBody).
  2848. Return(params.result, params.err)
  2849. }
  2850. svc := OSCARProxy{
  2851. Logger: slog.Default(),
  2852. ICBMService: icbmSvc,
  2853. }
  2854. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  2855. assert.Equal(t, tc.wantMsg, msg)
  2856. })
  2857. }
  2858. }
  2859. func TestOSCARProxy_RecvClientCmd_SetAway(t *testing.T) {
  2860. cases := []struct {
  2861. // name is the unit test name
  2862. name string
  2863. // me is the TOC user session
  2864. me *state.Session
  2865. // givenCmd is the TOC command
  2866. givenCmd []byte
  2867. // wantMsg is the expected TOC response
  2868. wantMsg string
  2869. // mockParams is the list of params sent to mocks that satisfy this
  2870. // method's dependencies
  2871. mockParams mockParams
  2872. }{
  2873. {
  2874. name: "successfully set away with message",
  2875. me: newTestSession("me"),
  2876. givenCmd: []byte(`toc_set_away "I'm away from my computer right now."`),
  2877. mockParams: mockParams{
  2878. locateParams: locateParams{
  2879. setInfoParams: setInfoParams{
  2880. {
  2881. me: state.NewIdentScreenName("me"),
  2882. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2883. TLVRestBlock: wire.TLVRestBlock{
  2884. TLVList: wire.TLVList{
  2885. wire.NewTLVBE(wire.LocateTLVTagsInfoUnavailableData, "I'm away from my computer right now."),
  2886. },
  2887. },
  2888. },
  2889. },
  2890. },
  2891. },
  2892. },
  2893. },
  2894. {
  2895. name: "successfully set away without message",
  2896. me: newTestSession("me"),
  2897. givenCmd: []byte(`toc_set_away`),
  2898. mockParams: mockParams{
  2899. locateParams: locateParams{
  2900. setInfoParams: setInfoParams{
  2901. {
  2902. me: state.NewIdentScreenName("me"),
  2903. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2904. TLVRestBlock: wire.TLVRestBlock{
  2905. TLVList: wire.TLVList{
  2906. wire.NewTLVBE(wire.LocateTLVTagsInfoUnavailableData, ""),
  2907. },
  2908. },
  2909. },
  2910. },
  2911. },
  2912. },
  2913. },
  2914. },
  2915. {
  2916. name: "set away message, receive error from locate service",
  2917. me: newTestSession("me"),
  2918. givenCmd: []byte(`toc_set_away "I'm away from my computer right now."`),
  2919. mockParams: mockParams{
  2920. locateParams: locateParams{
  2921. setInfoParams: setInfoParams{
  2922. {
  2923. me: state.NewIdentScreenName("me"),
  2924. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2925. TLVRestBlock: wire.TLVRestBlock{
  2926. TLVList: wire.TLVList{
  2927. wire.NewTLVBE(wire.LocateTLVTagsInfoUnavailableData, "I'm away from my computer right now."),
  2928. },
  2929. },
  2930. },
  2931. err: io.EOF,
  2932. },
  2933. },
  2934. },
  2935. },
  2936. wantMsg: cmdInternalSvcErr,
  2937. },
  2938. }
  2939. for _, tc := range cases {
  2940. t.Run(tc.name, func(t *testing.T) {
  2941. ctx := context.Background()
  2942. locateSvc := newMockLocateService(t)
  2943. for _, params := range tc.mockParams.setInfoParams {
  2944. locateSvc.EXPECT().
  2945. SetInfo(ctx, matchSession(params.me), params.inBody).
  2946. Return(params.err)
  2947. }
  2948. svc := OSCARProxy{
  2949. Logger: slog.Default(),
  2950. LocateService: locateSvc,
  2951. }
  2952. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  2953. assert.Equal(t, tc.wantMsg, msg)
  2954. })
  2955. }
  2956. }
  2957. func TestOSCARProxy_RecvClientCmd_SetCaps(t *testing.T) {
  2958. cases := []struct {
  2959. // name is the unit test name
  2960. name string
  2961. // me is the TOC user session
  2962. me *state.Session
  2963. // givenCmd is the TOC command
  2964. givenCmd []byte
  2965. // wantMsg is the expected TOC response
  2966. wantMsg string
  2967. // mockParams is the list of params sent to mocks that satisfy this
  2968. // method's dependencies
  2969. mockParams mockParams
  2970. }{
  2971. {
  2972. name: "successfully set capabilities",
  2973. me: newTestSession("me"),
  2974. givenCmd: []byte(`toc_set_caps 09460000-4C7F-11D1-8222-444553540000 09460001-4C7F-11D1-8222-444553540000`),
  2975. mockParams: mockParams{
  2976. locateParams: locateParams{
  2977. setInfoParams: setInfoParams{
  2978. {
  2979. me: state.NewIdentScreenName("me"),
  2980. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2981. TLVRestBlock: wire.TLVRestBlock{
  2982. TLVList: wire.TLVList{
  2983. wire.NewTLVBE(wire.LocateTLVTagsInfoCapabilities, []uuid.UUID{
  2984. uuid.MustParse("09460000-4C7F-11D1-8222-444553540000"),
  2985. uuid.MustParse("09460001-4C7F-11D1-8222-444553540000"),
  2986. wire.CapChat,
  2987. }),
  2988. },
  2989. },
  2990. },
  2991. },
  2992. },
  2993. },
  2994. },
  2995. },
  2996. {
  2997. name: "set capabilities with empty list",
  2998. me: newTestSession("me"),
  2999. givenCmd: []byte(`toc_set_caps`),
  3000. mockParams: mockParams{
  3001. locateParams: locateParams{
  3002. setInfoParams: setInfoParams{
  3003. {
  3004. me: state.NewIdentScreenName("me"),
  3005. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  3006. TLVRestBlock: wire.TLVRestBlock{
  3007. TLVList: wire.TLVList{
  3008. wire.NewTLVBE(wire.LocateTLVTagsInfoCapabilities, []uuid.UUID{
  3009. wire.CapChat,
  3010. }),
  3011. },
  3012. },
  3013. },
  3014. },
  3015. },
  3016. },
  3017. },
  3018. },
  3019. {
  3020. name: "set capability, receive error from locate service",
  3021. me: newTestSession("me"),
  3022. givenCmd: []byte(`toc_set_caps 09460000-4C7F-11D1-8222-444553540000`),
  3023. mockParams: mockParams{
  3024. locateParams: locateParams{
  3025. setInfoParams: setInfoParams{
  3026. {
  3027. me: state.NewIdentScreenName("me"),
  3028. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  3029. TLVRestBlock: wire.TLVRestBlock{
  3030. TLVList: wire.TLVList{
  3031. wire.NewTLVBE(wire.LocateTLVTagsInfoCapabilities, []uuid.UUID{
  3032. uuid.MustParse("09460000-4C7F-11D1-8222-444553540000"),
  3033. wire.CapChat,
  3034. }),
  3035. },
  3036. },
  3037. },
  3038. err: io.EOF,
  3039. },
  3040. },
  3041. },
  3042. },
  3043. wantMsg: cmdInternalSvcErr,
  3044. },
  3045. {
  3046. name: "set malformed capability UUID",
  3047. me: newTestSession("me"),
  3048. givenCmd: []byte(`toc_set_caps 09460000-`),
  3049. wantMsg: cmdInternalSvcErr,
  3050. },
  3051. }
  3052. for _, tc := range cases {
  3053. t.Run(tc.name, func(t *testing.T) {
  3054. ctx := context.Background()
  3055. locateSvc := newMockLocateService(t)
  3056. for _, params := range tc.mockParams.setInfoParams {
  3057. locateSvc.EXPECT().
  3058. SetInfo(ctx, matchSession(params.me), params.inBody).
  3059. Return(params.err)
  3060. }
  3061. svc := OSCARProxy{
  3062. Logger: slog.Default(),
  3063. LocateService: locateSvc,
  3064. }
  3065. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  3066. assert.Equal(t, tc.wantMsg, msg)
  3067. })
  3068. }
  3069. }
  3070. func TestOSCARProxy_RecvClientCmd_SetConfig(t *testing.T) {
  3071. cases := []struct {
  3072. // name is the unit test name
  3073. name string
  3074. // me is the TOC user session
  3075. me *state.Session
  3076. // givenCmd is the TOC command
  3077. givenCmd []byte
  3078. // wantMsg is the expected TOC response
  3079. wantMsg string
  3080. // mockParams is the list of params sent to mocks that satisfy this
  3081. // method's dependencies
  3082. mockParams mockParams
  3083. }{
  3084. {
  3085. name: "successfully set permit all config (unquoted)",
  3086. me: newTestSession("me"),
  3087. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\nb friend2\n}\n"),
  3088. mockParams: mockParams{
  3089. tocConfigParams: tocConfigParams{
  3090. setTOCConfigParams: setTOCConfigParams{
  3091. {
  3092. user: state.NewIdentScreenName("me"),
  3093. config: "{m 1\ng Buddies\nb friend1\nb friend2\n}\n",
  3094. },
  3095. },
  3096. },
  3097. },
  3098. },
  3099. {
  3100. name: "successfully set permit all config (double-quoted)",
  3101. me: newTestSession("me"),
  3102. givenCmd: []byte("toc_set_config \"{m 1\ng Buddies\nb friend1\nb friend2\n}\n\""),
  3103. mockParams: mockParams{
  3104. tocConfigParams: tocConfigParams{
  3105. setTOCConfigParams: setTOCConfigParams{
  3106. {
  3107. user: state.NewIdentScreenName("me"),
  3108. config: "{m 1\ng Buddies\nb friend1\nb friend2\n}\n",
  3109. },
  3110. },
  3111. },
  3112. },
  3113. },
  3114. {
  3115. name: "successfully set permit all config (single-quoted)",
  3116. me: newTestSession("me"),
  3117. givenCmd: []byte("toc_set_config '{m 1\ng Buddies\nb friend1\nb friend2\n}\n'"),
  3118. mockParams: mockParams{
  3119. tocConfigParams: tocConfigParams{
  3120. setTOCConfigParams: setTOCConfigParams{
  3121. {
  3122. user: state.NewIdentScreenName("me"),
  3123. config: "{m 1\ng Buddies\nb friend1\nb friend2\n}\n",
  3124. },
  3125. },
  3126. },
  3127. },
  3128. },
  3129. {
  3130. name: "successfully set permit all config (double-quoted with spaces)",
  3131. me: newTestSession("me"),
  3132. givenCmd: []byte("toc_set_config \" {m 1\ng Buddies\nb friend1\nb friend2\n}\n \""),
  3133. mockParams: mockParams{
  3134. tocConfigParams: tocConfigParams{
  3135. setTOCConfigParams: setTOCConfigParams{
  3136. {
  3137. user: state.NewIdentScreenName("me"),
  3138. config: "{m 1\ng Buddies\nb friend1\nb friend2\n}\n",
  3139. },
  3140. },
  3141. },
  3142. },
  3143. },
  3144. {
  3145. name: "set config, receive error from toc config store",
  3146. me: newTestSession("me"),
  3147. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\nb friend2\n}\n"),
  3148. mockParams: mockParams{
  3149. tocConfigParams: tocConfigParams{
  3150. setTOCConfigParams: setTOCConfigParams{
  3151. {
  3152. user: state.NewIdentScreenName("me"),
  3153. config: "{m 1\ng Buddies\nb friend1\nb friend2\n}\n",
  3154. err: io.EOF,
  3155. },
  3156. },
  3157. },
  3158. },
  3159. wantMsg: cmdInternalSvcErr,
  3160. },
  3161. {
  3162. name: "bad command",
  3163. givenCmd: []byte(`toc_set_config`),
  3164. wantMsg: cmdInternalSvcErr,
  3165. },
  3166. }
  3167. for _, tc := range cases {
  3168. t.Run(tc.name, func(t *testing.T) {
  3169. ctx := context.Background()
  3170. pdSvc := newMockPermitDenyService(t)
  3171. for _, params := range tc.mockParams.addDenyListEntriesParams {
  3172. pdSvc.EXPECT().
  3173. AddDenyListEntries(ctx, matchSession(params.me), params.body).
  3174. Return(params.err)
  3175. }
  3176. for _, params := range tc.mockParams.addPermListEntriesParams {
  3177. pdSvc.EXPECT().
  3178. AddPermListEntries(ctx, matchSession(params.me), params.body).
  3179. Return(params.err)
  3180. }
  3181. buddySvc := newMockBuddyService(t)
  3182. for _, params := range tc.mockParams.addBuddiesParams {
  3183. buddySvc.EXPECT().
  3184. AddBuddies(ctx, matchSession(params.me), params.inBody).
  3185. Return(params.err)
  3186. }
  3187. tocConfigSvc := newMockTOCConfigStore(t)
  3188. for _, params := range tc.mockParams.setTOCConfigParams {
  3189. tocConfigSvc.EXPECT().
  3190. SetTOCConfig(params.user, params.config).
  3191. Return(params.err)
  3192. }
  3193. svc := OSCARProxy{
  3194. BuddyService: buddySvc,
  3195. Logger: slog.Default(),
  3196. PermitDenyService: pdSvc,
  3197. TOCConfigStore: tocConfigSvc,
  3198. }
  3199. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  3200. assert.Equal(t, tc.wantMsg, msg)
  3201. })
  3202. }
  3203. }
  3204. func TestOSCARProxy_RecvClientCmd_SetDir(t *testing.T) {
  3205. cases := []struct {
  3206. // name is the unit test name
  3207. name string
  3208. // me is the TOC user session
  3209. me *state.Session
  3210. // givenCmd is the TOC command
  3211. givenCmd []byte
  3212. // wantMsg is the expected TOC response
  3213. wantMsg string
  3214. // mockParams is the list of params sent to mocks that satisfy this
  3215. // method's dependencies
  3216. mockParams mockParams
  3217. }{
  3218. {
  3219. name: "successfully set directory info with quoted fields",
  3220. me: newTestSession("me"),
  3221. givenCmd: []byte(`toc_set_dir "first name":"middle name":"last name":"maiden name":"city":"state":"country":"email":"allow web searches"`),
  3222. mockParams: mockParams{
  3223. locateParams: locateParams{
  3224. setDirInfoParams: setDirInfoParams{
  3225. {
  3226. me: state.NewIdentScreenName("me"),
  3227. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  3228. TLVRestBlock: wire.TLVRestBlock{
  3229. TLVList: wire.TLVList{
  3230. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  3231. wire.NewTLVBE(wire.ODirTLVMiddleName, "middle name"),
  3232. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  3233. wire.NewTLVBE(wire.ODirTLVMaidenName, "maiden name"),
  3234. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  3235. wire.NewTLVBE(wire.ODirTLVState, "state"),
  3236. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  3237. },
  3238. },
  3239. },
  3240. },
  3241. },
  3242. },
  3243. },
  3244. },
  3245. {
  3246. name: "successfully set directory info with some blank fields",
  3247. me: newTestSession("me"),
  3248. givenCmd: []byte(`toc_set_dir "first name"::"last name"::"city":"state":"country":"email":"allow web searches"`),
  3249. mockParams: mockParams{
  3250. locateParams: locateParams{
  3251. setDirInfoParams: setDirInfoParams{
  3252. {
  3253. me: state.NewIdentScreenName("me"),
  3254. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  3255. TLVRestBlock: wire.TLVRestBlock{
  3256. TLVList: wire.TLVList{
  3257. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  3258. wire.NewTLVBE(wire.ODirTLVMiddleName, ""),
  3259. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  3260. wire.NewTLVBE(wire.ODirTLVMaidenName, ""),
  3261. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  3262. wire.NewTLVBE(wire.ODirTLVState, "state"),
  3263. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  3264. },
  3265. },
  3266. },
  3267. },
  3268. },
  3269. },
  3270. },
  3271. },
  3272. {
  3273. name: "successfully set directory info with last two fields absent",
  3274. me: newTestSession("me"),
  3275. givenCmd: []byte(`toc_set_dir "first name"::"last name"::"city":"state":"country"`),
  3276. mockParams: mockParams{
  3277. locateParams: locateParams{
  3278. setDirInfoParams: setDirInfoParams{
  3279. {
  3280. me: state.NewIdentScreenName("me"),
  3281. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  3282. TLVRestBlock: wire.TLVRestBlock{
  3283. TLVList: wire.TLVList{
  3284. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  3285. wire.NewTLVBE(wire.ODirTLVMiddleName, ""),
  3286. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  3287. wire.NewTLVBE(wire.ODirTLVMaidenName, ""),
  3288. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  3289. wire.NewTLVBE(wire.ODirTLVState, "state"),
  3290. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  3291. },
  3292. },
  3293. },
  3294. },
  3295. },
  3296. },
  3297. },
  3298. },
  3299. {
  3300. name: "set directory info, receive error from locate svc",
  3301. me: newTestSession("me"),
  3302. givenCmd: []byte(`toc_set_dir "first name":"middle name":"last name":"maiden name":"city":"state":"country":"email":"allow web searches"`),
  3303. mockParams: mockParams{
  3304. locateParams: locateParams{
  3305. setDirInfoParams: setDirInfoParams{
  3306. {
  3307. me: state.NewIdentScreenName("me"),
  3308. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  3309. TLVRestBlock: wire.TLVRestBlock{
  3310. TLVList: wire.TLVList{
  3311. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  3312. wire.NewTLVBE(wire.ODirTLVMiddleName, "middle name"),
  3313. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  3314. wire.NewTLVBE(wire.ODirTLVMaidenName, "maiden name"),
  3315. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  3316. wire.NewTLVBE(wire.ODirTLVState, "state"),
  3317. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  3318. },
  3319. },
  3320. },
  3321. err: io.EOF,
  3322. },
  3323. },
  3324. },
  3325. },
  3326. wantMsg: cmdInternalSvcErr,
  3327. },
  3328. {
  3329. name: "set directory with too many fields present",
  3330. me: newTestSession("me"),
  3331. givenCmd: []byte(`toc_set_dir "first name"::"last name"::"city":"state":"country":"email":"allow web searches":"extra":"extra"`),
  3332. wantMsg: cmdInternalSvcErr,
  3333. },
  3334. {
  3335. name: "bad command",
  3336. givenCmd: []byte(`toc_set_dir`),
  3337. wantMsg: cmdInternalSvcErr,
  3338. },
  3339. }
  3340. for _, tc := range cases {
  3341. t.Run(tc.name, func(t *testing.T) {
  3342. ctx := context.Background()
  3343. locateSvc := newMockLocateService(t)
  3344. for _, params := range tc.mockParams.setDirInfoParams {
  3345. locateSvc.EXPECT().
  3346. SetDirInfo(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  3347. Return(params.msg, params.err)
  3348. }
  3349. svc := OSCARProxy{
  3350. Logger: slog.Default(),
  3351. LocateService: locateSvc,
  3352. }
  3353. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  3354. assert.Equal(t, tc.wantMsg, msg)
  3355. })
  3356. }
  3357. }
  3358. func TestOSCARProxy_RecvClientCmd_SetIdle(t *testing.T) {
  3359. cases := []struct {
  3360. // name is the unit test name
  3361. name string
  3362. // me is the TOC user session
  3363. me *state.Session
  3364. // givenCmd is the TOC command
  3365. givenCmd []byte
  3366. // wantMsg is the expected TOC response
  3367. wantMsg string
  3368. // mockParams is the list of params sent to mocks that satisfy this
  3369. // method's dependencies
  3370. mockParams mockParams
  3371. }{
  3372. {
  3373. name: "successfully set idle status",
  3374. me: newTestSession("me"),
  3375. givenCmd: []byte(`toc_set_idle 10`),
  3376. mockParams: mockParams{
  3377. oServiceBOSParams: oServiceParams{
  3378. idleNotificationParams: idleNotificationParams{
  3379. {
  3380. me: state.NewIdentScreenName("me"),
  3381. bodyIn: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  3382. IdleTime: uint32(10),
  3383. },
  3384. },
  3385. },
  3386. },
  3387. },
  3388. },
  3389. {
  3390. name: "set idle status, receive err from BOS oservice svc",
  3391. me: newTestSession("me"),
  3392. givenCmd: []byte(`toc_set_idle 10`),
  3393. mockParams: mockParams{
  3394. oServiceBOSParams: oServiceParams{
  3395. idleNotificationParams: idleNotificationParams{
  3396. {
  3397. me: state.NewIdentScreenName("me"),
  3398. bodyIn: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  3399. IdleTime: uint32(10),
  3400. },
  3401. err: io.EOF,
  3402. },
  3403. },
  3404. },
  3405. },
  3406. wantMsg: cmdInternalSvcErr,
  3407. },
  3408. {
  3409. name: "bad secs param",
  3410. givenCmd: []byte(`toc_set_idle zero`),
  3411. wantMsg: cmdInternalSvcErr,
  3412. },
  3413. {
  3414. name: "bad command",
  3415. givenCmd: []byte(`toc_set_idle`),
  3416. wantMsg: cmdInternalSvcErr,
  3417. },
  3418. }
  3419. for _, tc := range cases {
  3420. t.Run(tc.name, func(t *testing.T) {
  3421. ctx := context.Background()
  3422. oServiceSvc := newMockOServiceService(t)
  3423. for _, params := range tc.mockParams.oServiceBOSParams.idleNotificationParams {
  3424. oServiceSvc.EXPECT().
  3425. IdleNotification(ctx, matchSession(params.me), params.bodyIn).
  3426. Return(params.err)
  3427. }
  3428. svc := OSCARProxy{
  3429. Logger: slog.Default(),
  3430. OServiceServiceBOS: oServiceSvc,
  3431. }
  3432. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  3433. assert.Equal(t, tc.wantMsg, msg)
  3434. })
  3435. }
  3436. }
  3437. func TestOSCARProxy_RecvClientCmd_SetInfo(t *testing.T) {
  3438. cases := []struct {
  3439. // name is the unit test name
  3440. name string
  3441. // me is the TOC user session
  3442. me *state.Session
  3443. // givenCmd is the TOC command
  3444. givenCmd []byte
  3445. // wantMsg is the expected TOC response
  3446. wantMsg string
  3447. // mockParams is the list of params sent to mocks that satisfy this
  3448. // method's dependencies
  3449. mockParams mockParams
  3450. }{
  3451. {
  3452. name: "successfully set profile",
  3453. me: newTestSession("me"),
  3454. givenCmd: []byte(`toc_set_info "my profile!"`),
  3455. mockParams: mockParams{
  3456. locateParams: locateParams{
  3457. setInfoParams: setInfoParams{
  3458. {
  3459. me: state.NewIdentScreenName("me"),
  3460. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  3461. TLVRestBlock: wire.TLVRestBlock{
  3462. TLVList: wire.TLVList{
  3463. wire.NewTLVBE(wire.LocateTLVTagsInfoSigData, "my profile!"),
  3464. },
  3465. },
  3466. },
  3467. },
  3468. },
  3469. },
  3470. },
  3471. },
  3472. {
  3473. name: "set profile, receive error from locate svc",
  3474. me: newTestSession("me"),
  3475. givenCmd: []byte(`toc_set_info "my profile!"`),
  3476. mockParams: mockParams{
  3477. locateParams: locateParams{
  3478. setInfoParams: setInfoParams{
  3479. {
  3480. me: state.NewIdentScreenName("me"),
  3481. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  3482. TLVRestBlock: wire.TLVRestBlock{
  3483. TLVList: wire.TLVList{
  3484. wire.NewTLVBE(wire.LocateTLVTagsInfoSigData, "my profile!"),
  3485. },
  3486. },
  3487. },
  3488. err: io.EOF,
  3489. },
  3490. },
  3491. },
  3492. },
  3493. wantMsg: cmdInternalSvcErr,
  3494. },
  3495. {
  3496. name: "bad command",
  3497. givenCmd: []byte(`toc_set_info`),
  3498. wantMsg: cmdInternalSvcErr,
  3499. },
  3500. }
  3501. for _, tc := range cases {
  3502. t.Run(tc.name, func(t *testing.T) {
  3503. ctx := context.Background()
  3504. locateSvc := newMockLocateService(t)
  3505. for _, params := range tc.mockParams.setInfoParams {
  3506. locateSvc.EXPECT().
  3507. SetInfo(ctx, matchSession(params.me), params.inBody).
  3508. Return(params.err)
  3509. }
  3510. svc := OSCARProxy{
  3511. Logger: slog.Default(),
  3512. LocateService: locateSvc,
  3513. }
  3514. msg := svc.RecvClientCmd(ctx, tc.me, nil, tc.givenCmd, nil, nil)
  3515. assert.Equal(t, tc.wantMsg, msg)
  3516. })
  3517. }
  3518. }
  3519. func TestOSCARProxy_Signon(t *testing.T) {
  3520. roastedPass := wire.RoastTOCPassword([]byte("thepass"))
  3521. cases := []struct {
  3522. // name is the unit test name
  3523. name string
  3524. // me is the TOC user session
  3525. me *state.Session
  3526. // givenCmd is the TOC command
  3527. givenCmd []byte
  3528. // wantMsg is the expected TOC response
  3529. wantMsg []string
  3530. // mockParams is the list of params sent to mocks that satisfy this
  3531. // method's dependencies
  3532. mockParams mockParams
  3533. }{
  3534. {
  3535. name: "successfully login",
  3536. me: newTestSession("me", func(session *state.Session) {
  3537. session.SetCaps([][16]byte{wire.CapChat})
  3538. }),
  3539. givenCmd: []byte(`"" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3540. mockParams: mockParams{
  3541. authParams: authParams{
  3542. flapLoginParams: flapLoginParams{
  3543. {
  3544. frame: wire.FLAPSignonFrame{
  3545. TLVRestBlock: wire.TLVRestBlock{
  3546. TLVList: wire.TLVList{
  3547. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3548. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3549. },
  3550. },
  3551. },
  3552. newUserFn: state.NewStubUser,
  3553. tlv: wire.TLVRestBlock{
  3554. TLVList: wire.TLVList{
  3555. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3556. },
  3557. },
  3558. },
  3559. },
  3560. registerBOSSessionParams: registerBOSSessionParams{
  3561. {
  3562. authCookie: []byte("thecookie"),
  3563. sess: newTestSession("me"),
  3564. },
  3565. },
  3566. },
  3567. buddyListRegistryParams: buddyListRegistryParams{
  3568. registerBuddyListParams: registerBuddyListParams{
  3569. {
  3570. user: state.NewIdentScreenName("me"),
  3571. },
  3572. },
  3573. },
  3574. tocConfigParams: tocConfigParams{
  3575. userParams: userParams{
  3576. {
  3577. screenName: state.NewIdentScreenName("me"),
  3578. returnedUser: &state.User{
  3579. TOCConfig: "my-toc-config",
  3580. },
  3581. },
  3582. },
  3583. },
  3584. },
  3585. wantMsg: []string{"SIGN_ON:TOC1.0", "CONFIG:my-toc-config"},
  3586. },
  3587. {
  3588. name: "login, receive error from auth svc FLAP login",
  3589. givenCmd: []byte(`"" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3590. mockParams: mockParams{
  3591. authParams: authParams{
  3592. flapLoginParams: flapLoginParams{
  3593. {
  3594. frame: wire.FLAPSignonFrame{
  3595. TLVRestBlock: wire.TLVRestBlock{
  3596. TLVList: wire.TLVList{
  3597. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3598. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3599. },
  3600. },
  3601. },
  3602. newUserFn: state.NewStubUser,
  3603. err: io.EOF,
  3604. },
  3605. },
  3606. },
  3607. },
  3608. wantMsg: []string{cmdInternalSvcErr},
  3609. },
  3610. {
  3611. name: "login, receive error from auth svc registration",
  3612. givenCmd: []byte(`"" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3613. mockParams: mockParams{
  3614. authParams: authParams{
  3615. flapLoginParams: flapLoginParams{
  3616. {
  3617. frame: wire.FLAPSignonFrame{
  3618. TLVRestBlock: wire.TLVRestBlock{
  3619. TLVList: wire.TLVList{
  3620. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3621. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3622. },
  3623. },
  3624. },
  3625. newUserFn: state.NewStubUser,
  3626. tlv: wire.TLVRestBlock{
  3627. TLVList: wire.TLVList{
  3628. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3629. },
  3630. },
  3631. },
  3632. },
  3633. registerBOSSessionParams: registerBOSSessionParams{
  3634. {
  3635. authCookie: []byte("thecookie"),
  3636. err: io.EOF,
  3637. },
  3638. },
  3639. },
  3640. },
  3641. wantMsg: []string{cmdInternalSvcErr},
  3642. },
  3643. {
  3644. name: "login, receive error from buddy list registry",
  3645. givenCmd: []byte(`"" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3646. mockParams: mockParams{
  3647. authParams: authParams{
  3648. flapLoginParams: flapLoginParams{
  3649. {
  3650. frame: wire.FLAPSignonFrame{
  3651. TLVRestBlock: wire.TLVRestBlock{
  3652. TLVList: wire.TLVList{
  3653. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3654. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3655. },
  3656. },
  3657. },
  3658. newUserFn: state.NewStubUser,
  3659. tlv: wire.TLVRestBlock{
  3660. TLVList: wire.TLVList{
  3661. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3662. },
  3663. },
  3664. },
  3665. },
  3666. registerBOSSessionParams: registerBOSSessionParams{
  3667. {
  3668. authCookie: []byte("thecookie"),
  3669. sess: newTestSession("me"),
  3670. },
  3671. },
  3672. },
  3673. buddyListRegistryParams: buddyListRegistryParams{
  3674. registerBuddyListParams: registerBuddyListParams{
  3675. {
  3676. user: state.NewIdentScreenName("me"),
  3677. err: io.EOF,
  3678. },
  3679. },
  3680. },
  3681. },
  3682. wantMsg: []string{cmdInternalSvcErr},
  3683. },
  3684. {
  3685. name: "login, receive error from TOC config store",
  3686. givenCmd: []byte(`"" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3687. mockParams: mockParams{
  3688. authParams: authParams{
  3689. flapLoginParams: flapLoginParams{
  3690. {
  3691. frame: wire.FLAPSignonFrame{
  3692. TLVRestBlock: wire.TLVRestBlock{
  3693. TLVList: wire.TLVList{
  3694. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3695. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3696. },
  3697. },
  3698. },
  3699. newUserFn: state.NewStubUser,
  3700. tlv: wire.TLVRestBlock{
  3701. TLVList: wire.TLVList{
  3702. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3703. },
  3704. },
  3705. },
  3706. },
  3707. registerBOSSessionParams: registerBOSSessionParams{
  3708. {
  3709. authCookie: []byte("thecookie"),
  3710. sess: newTestSession("me"),
  3711. },
  3712. },
  3713. },
  3714. buddyListRegistryParams: buddyListRegistryParams{
  3715. registerBuddyListParams: registerBuddyListParams{
  3716. {
  3717. user: state.NewIdentScreenName("me"),
  3718. },
  3719. },
  3720. },
  3721. tocConfigParams: tocConfigParams{
  3722. userParams: userParams{
  3723. {
  3724. screenName: state.NewIdentScreenName("me"),
  3725. err: io.EOF,
  3726. },
  3727. },
  3728. },
  3729. },
  3730. wantMsg: []string{cmdInternalSvcErr},
  3731. },
  3732. {
  3733. name: "login, user not found after login",
  3734. givenCmd: []byte(`"" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3735. mockParams: mockParams{
  3736. authParams: authParams{
  3737. flapLoginParams: flapLoginParams{
  3738. {
  3739. frame: wire.FLAPSignonFrame{
  3740. TLVRestBlock: wire.TLVRestBlock{
  3741. TLVList: wire.TLVList{
  3742. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3743. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3744. },
  3745. },
  3746. },
  3747. newUserFn: state.NewStubUser,
  3748. tlv: wire.TLVRestBlock{
  3749. TLVList: wire.TLVList{
  3750. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3751. },
  3752. },
  3753. },
  3754. },
  3755. registerBOSSessionParams: registerBOSSessionParams{
  3756. {
  3757. authCookie: []byte("thecookie"),
  3758. sess: newTestSession("me"),
  3759. },
  3760. },
  3761. },
  3762. buddyListRegistryParams: buddyListRegistryParams{
  3763. registerBuddyListParams: registerBuddyListParams{
  3764. {
  3765. user: state.NewIdentScreenName("me"),
  3766. },
  3767. },
  3768. },
  3769. tocConfigParams: tocConfigParams{
  3770. userParams: userParams{
  3771. {
  3772. screenName: state.NewIdentScreenName("me"),
  3773. returnedUser: nil,
  3774. },
  3775. },
  3776. },
  3777. },
  3778. wantMsg: []string{cmdInternalSvcErr},
  3779. },
  3780. {
  3781. name: "login with bad credentials",
  3782. givenCmd: []byte(`"" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3783. mockParams: mockParams{
  3784. authParams: authParams{
  3785. flapLoginParams: flapLoginParams{
  3786. {
  3787. frame: wire.FLAPSignonFrame{
  3788. TLVRestBlock: wire.TLVRestBlock{
  3789. TLVList: wire.TLVList{
  3790. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3791. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3792. },
  3793. },
  3794. },
  3795. newUserFn: state.NewStubUser,
  3796. tlv: wire.TLVRestBlock{
  3797. TLVList: wire.TLVList{
  3798. wire.NewTLVBE(wire.LoginTLVTagsErrorSubcode, wire.LoginErrInvalidUsernameOrPassword),
  3799. },
  3800. },
  3801. },
  3802. },
  3803. },
  3804. },
  3805. wantMsg: []string{"ERROR:980"},
  3806. },
  3807. {
  3808. name: "bad command",
  3809. givenCmd: []byte(`"" ""`),
  3810. wantMsg: []string{cmdInternalSvcErr},
  3811. },
  3812. }
  3813. for _, tc := range cases {
  3814. t.Run(tc.name, func(t *testing.T) {
  3815. ctx := context.Background()
  3816. authSvc := newMockAuthService(t)
  3817. for _, params := range tc.mockParams.flapLoginParams {
  3818. authSvc.EXPECT().
  3819. FLAPLogin(params.frame, mock.Anything).
  3820. Return(params.tlv, params.err)
  3821. }
  3822. for _, params := range tc.mockParams.registerBOSSessionParams {
  3823. authSvc.EXPECT().
  3824. RegisterBOSSession(ctx, params.authCookie).
  3825. Return(params.sess, params.err)
  3826. }
  3827. buddyRegistry := newMockBuddyListRegistry(t)
  3828. for _, params := range tc.mockParams.registerBuddyListParams {
  3829. buddyRegistry.EXPECT().
  3830. RegisterBuddyList(params.user).
  3831. Return(params.err)
  3832. }
  3833. tocCfg := newMockTOCConfigStore(t)
  3834. for _, params := range tc.mockParams.userParams {
  3835. tocCfg.EXPECT().
  3836. User(params.screenName).
  3837. Return(params.returnedUser, params.err)
  3838. }
  3839. svc := OSCARProxy{
  3840. AuthService: authSvc,
  3841. BuddyListRegistry: buddyRegistry,
  3842. Logger: slog.Default(),
  3843. TOCConfigStore: tocCfg,
  3844. }
  3845. sess, msg := svc.Signon(ctx, tc.givenCmd)
  3846. assert.Equal(t, tc.wantMsg, msg)
  3847. if tc.me == nil {
  3848. assert.Nil(t, sess)
  3849. } else if assert.NotNil(t, sess) {
  3850. assert.Equal(t, tc.me.IdentScreenName(), sess.IdentScreenName())
  3851. assert.Equal(t, tc.me.Caps(), sess.Caps())
  3852. }
  3853. })
  3854. }
  3855. }
  3856. func TestOSCARProxy_Signout(t *testing.T) {
  3857. cases := []struct {
  3858. // name is the unit test name
  3859. name string
  3860. // me is the TOC user session
  3861. me *state.Session
  3862. // givenChatRegistry is the chat registry passed to the function
  3863. chatRegistry *ChatRegistry
  3864. // mockParams is the list of params sent to mocks that satisfy this
  3865. // method's dependencies
  3866. mockParams mockParams
  3867. }{
  3868. {
  3869. name: "successfully sign out",
  3870. me: newTestSession("me"),
  3871. chatRegistry: func() *ChatRegistry {
  3872. cr := NewChatRegistry()
  3873. s1 := state.NewSession()
  3874. s1.SetIdentScreenName(state.NewIdentScreenName("me1"))
  3875. cr.RegisterSess(0, s1)
  3876. s2 := state.NewSession()
  3877. s2.SetIdentScreenName(state.NewIdentScreenName("me2"))
  3878. cr.RegisterSess(1, s2)
  3879. return cr
  3880. }(),
  3881. mockParams: mockParams{
  3882. buddyParams: buddyParams{
  3883. broadcastBuddyDepartedParams: broadcastBuddyDepartedParams{
  3884. {
  3885. me: state.NewIdentScreenName("me"),
  3886. },
  3887. },
  3888. },
  3889. buddyListRegistryParams: buddyListRegistryParams{
  3890. unregisterBuddyListParams: unregisterBuddyListParams{
  3891. {
  3892. user: state.NewIdentScreenName("me"),
  3893. },
  3894. },
  3895. },
  3896. authParams: authParams{
  3897. signoutParams: signoutParams{
  3898. {
  3899. me: state.NewIdentScreenName("me"),
  3900. },
  3901. },
  3902. signoutChatParams: signoutChatParams{
  3903. {
  3904. me: state.NewIdentScreenName("me1"),
  3905. },
  3906. {
  3907. me: state.NewIdentScreenName("me2"),
  3908. },
  3909. },
  3910. },
  3911. },
  3912. },
  3913. {
  3914. name: "sign out, receive error from buddy service",
  3915. me: newTestSession("me"),
  3916. chatRegistry: func() *ChatRegistry {
  3917. return NewChatRegistry()
  3918. }(),
  3919. mockParams: mockParams{
  3920. buddyParams: buddyParams{
  3921. broadcastBuddyDepartedParams: broadcastBuddyDepartedParams{
  3922. {
  3923. me: state.NewIdentScreenName("me"),
  3924. err: io.EOF,
  3925. },
  3926. },
  3927. },
  3928. buddyListRegistryParams: buddyListRegistryParams{
  3929. unregisterBuddyListParams: unregisterBuddyListParams{
  3930. {
  3931. user: state.NewIdentScreenName("me"),
  3932. },
  3933. },
  3934. },
  3935. authParams: authParams{
  3936. signoutParams: signoutParams{
  3937. {
  3938. me: state.NewIdentScreenName("me"),
  3939. },
  3940. },
  3941. },
  3942. },
  3943. },
  3944. {
  3945. name: "sign out, receive error from buddy list registry",
  3946. me: newTestSession("me"),
  3947. chatRegistry: func() *ChatRegistry {
  3948. return NewChatRegistry()
  3949. }(),
  3950. mockParams: mockParams{
  3951. buddyParams: buddyParams{
  3952. broadcastBuddyDepartedParams: broadcastBuddyDepartedParams{
  3953. {
  3954. me: state.NewIdentScreenName("me"),
  3955. },
  3956. },
  3957. },
  3958. buddyListRegistryParams: buddyListRegistryParams{
  3959. unregisterBuddyListParams: unregisterBuddyListParams{
  3960. {
  3961. user: state.NewIdentScreenName("me"),
  3962. err: io.EOF,
  3963. },
  3964. },
  3965. },
  3966. authParams: authParams{
  3967. signoutParams: signoutParams{
  3968. {
  3969. me: state.NewIdentScreenName("me"),
  3970. },
  3971. },
  3972. },
  3973. },
  3974. },
  3975. }
  3976. for _, tc := range cases {
  3977. t.Run(tc.name, func(t *testing.T) {
  3978. ctx := context.Background()
  3979. buddySvc := newMockBuddyService(t)
  3980. for _, params := range tc.mockParams.broadcastBuddyDepartedParams {
  3981. buddySvc.EXPECT().
  3982. BroadcastBuddyDeparted(ctx, matchSession(params.me)).
  3983. Return(params.err)
  3984. }
  3985. buddyListSvc := newMockBuddyListRegistry(t)
  3986. for _, params := range tc.mockParams.unregisterBuddyListParams {
  3987. buddyListSvc.EXPECT().
  3988. UnregisterBuddyList(params.user).
  3989. Return(params.err)
  3990. }
  3991. authSvc := newMockAuthService(t)
  3992. for _, params := range tc.mockParams.signoutParams {
  3993. authSvc.EXPECT().Signout(ctx, matchSession(params.me))
  3994. }
  3995. for _, params := range tc.mockParams.signoutChatParams {
  3996. authSvc.EXPECT().SignoutChat(ctx, matchSession(params.me))
  3997. }
  3998. svc := OSCARProxy{
  3999. AuthService: authSvc,
  4000. BuddyListRegistry: buddyListSvc,
  4001. BuddyService: buddySvc,
  4002. Logger: slog.Default(),
  4003. }
  4004. svc.Signout(ctx, tc.me, tc.chatRegistry)
  4005. })
  4006. }
  4007. }
  4008. func TestOSCARProxy_RecvClientCmd_UnknownCmd(t *testing.T) {
  4009. ctx := context.Background()
  4010. svc := OSCARProxy{
  4011. Logger: slog.Default(),
  4012. }
  4013. cmd := []byte("toc_unknown_cmd")
  4014. msg := svc.RecvClientCmd(ctx, nil, nil, cmd, nil, nil)
  4015. assert.Equal(t, cmdInternalSvcErr, msg)
  4016. }
  4017. func Test_parseArgs(t *testing.T) {
  4018. type testCase struct {
  4019. name string
  4020. givenPayload string
  4021. givenArgs []*string
  4022. wantVarArgs []string
  4023. wantArgs []string
  4024. wantErrMsg string
  4025. }
  4026. tests := []testCase{
  4027. {
  4028. name: "no positional args or varargs",
  4029. givenPayload: ``,
  4030. givenArgs: nil,
  4031. wantVarArgs: []string{},
  4032. },
  4033. {
  4034. name: "positional args with varargs",
  4035. givenPayload: `1234 "Join me!" user1 user2 user3`,
  4036. givenArgs: []*string{new(string), new(string)},
  4037. wantVarArgs: []string{"user1", "user2", "user3"},
  4038. wantArgs: []string{"1234", "Join me!"},
  4039. },
  4040. {
  4041. name: "nil positional argument placeholders should get skipped",
  4042. givenPayload: `1234 "Join me!" user1 user2 user3`,
  4043. givenArgs: []*string{nil, nil}, // still 2 placeholders, both nil
  4044. wantVarArgs: []string{"user1", "user2", "user3"},
  4045. wantArgs: []string{"", ""},
  4046. },
  4047. {
  4048. name: "positional args with no varargs",
  4049. givenPayload: `1234 "Join me!"`,
  4050. givenArgs: []*string{new(string), new(string)}, // roomID + msg
  4051. wantVarArgs: []string{},
  4052. wantArgs: []string{"1234", "Join me!"},
  4053. },
  4054. {
  4055. name: "varargs only",
  4056. givenPayload: `user1 user2 user3`,
  4057. givenArgs: nil,
  4058. wantVarArgs: []string{"user1", "user2", "user3"},
  4059. },
  4060. {
  4061. name: "too many positional arg placeholders",
  4062. givenPayload: `toc_chat_invite`,
  4063. givenArgs: []*string{new(string), new(string)},
  4064. wantVarArgs: []string{},
  4065. wantErrMsg: "command contains fewer arguments than expected",
  4066. },
  4067. {
  4068. name: "CSV parser error",
  4069. givenPayload: ``,
  4070. givenArgs: []*string{nil},
  4071. wantVarArgs: []string{},
  4072. wantErrMsg: "CSV reader error",
  4073. },
  4074. }
  4075. for _, tt := range tests {
  4076. t.Run(tt.name, func(t *testing.T) {
  4077. varArgs, err := parseArgs([]byte(tt.givenPayload), tt.givenArgs...)
  4078. if tt.wantErrMsg != "" {
  4079. assert.ErrorContains(t, err, tt.wantErrMsg)
  4080. return
  4081. }
  4082. assert.NoError(t, err)
  4083. // verify the placeholder pointers got populated
  4084. for i, want := range tt.wantArgs {
  4085. if want == "" {
  4086. assert.Nil(t, tt.givenArgs[i])
  4087. } else {
  4088. got := *tt.givenArgs[i]
  4089. assert.Equal(t, want, got)
  4090. }
  4091. }
  4092. // verify we have the same varargs
  4093. assert.Equal(t, tt.wantVarArgs, varArgs)
  4094. assert.Equal(t, len(tt.wantArgs), len(tt.givenArgs))
  4095. })
  4096. }
  4097. }