cmd_client_test.go 118 KB

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