cmd_client_test.go 121 KB

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