cmd_client_test.go 124 KB

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