cmd_client_test.go 122 KB

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