cmd_client_test.go 122 KB

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