cmd_client_test.go 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127
  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: 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: 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_Evil(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. // wantMsg is the expected TOC response
  1418. wantMsg string
  1419. // mockParams is the list of params sent to mocks that satisfy this
  1420. // method's dependencies
  1421. mockParams mockParams
  1422. }{
  1423. {
  1424. name: "successfully warn normally",
  1425. me: newTestSession("me"),
  1426. givenCmd: []byte(`toc_evil them norm`),
  1427. mockParams: mockParams{
  1428. icbmParams: icbmParams{
  1429. evilRequestParams: evilRequestParams{
  1430. {
  1431. me: state.NewIdentScreenName("me"),
  1432. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1433. SendAs: 0,
  1434. ScreenName: "them",
  1435. },
  1436. msg: wire.SNACMessage{
  1437. Body: wire.SNAC_0x04_0x09_ICBMEvilReply{},
  1438. },
  1439. },
  1440. },
  1441. },
  1442. },
  1443. },
  1444. {
  1445. name: "successfully warn anonymously",
  1446. me: newTestSession("me"),
  1447. givenCmd: []byte(`toc_evil them anon`),
  1448. mockParams: mockParams{
  1449. icbmParams: icbmParams{
  1450. evilRequestParams: evilRequestParams{
  1451. {
  1452. me: state.NewIdentScreenName("me"),
  1453. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1454. SendAs: 1,
  1455. ScreenName: "them",
  1456. },
  1457. msg: wire.SNACMessage{
  1458. Body: wire.SNAC_0x04_0x09_ICBMEvilReply{},
  1459. },
  1460. },
  1461. },
  1462. },
  1463. },
  1464. },
  1465. {
  1466. name: "warn, receive error from ICBM service",
  1467. me: newTestSession("me"),
  1468. givenCmd: []byte(`toc_evil them anon`),
  1469. mockParams: mockParams{
  1470. icbmParams: icbmParams{
  1471. evilRequestParams: evilRequestParams{
  1472. {
  1473. me: state.NewIdentScreenName("me"),
  1474. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1475. SendAs: 1,
  1476. ScreenName: "them",
  1477. },
  1478. err: io.EOF,
  1479. },
  1480. },
  1481. },
  1482. },
  1483. wantMsg: cmdInternalSvcErr,
  1484. },
  1485. {
  1486. name: "warn, receive snac err",
  1487. me: newTestSession("me"),
  1488. givenCmd: []byte(`toc_evil them anon`),
  1489. mockParams: mockParams{
  1490. icbmParams: icbmParams{
  1491. evilRequestParams: evilRequestParams{
  1492. {
  1493. me: state.NewIdentScreenName("me"),
  1494. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1495. SendAs: 1,
  1496. ScreenName: "them",
  1497. },
  1498. msg: wire.SNACMessage{
  1499. Body: wire.SNACError{},
  1500. },
  1501. },
  1502. },
  1503. },
  1504. },
  1505. },
  1506. {
  1507. name: "warn, ICBM svc returns unexpected snac type",
  1508. me: newTestSession("me"),
  1509. givenCmd: []byte(`toc_evil them anon`),
  1510. mockParams: mockParams{
  1511. icbmParams: icbmParams{
  1512. evilRequestParams: evilRequestParams{
  1513. {
  1514. me: state.NewIdentScreenName("me"),
  1515. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1516. SendAs: 1,
  1517. ScreenName: "them",
  1518. },
  1519. msg: wire.SNACMessage{
  1520. Body: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{},
  1521. },
  1522. },
  1523. },
  1524. },
  1525. },
  1526. wantMsg: cmdInternalSvcErr,
  1527. },
  1528. {
  1529. name: "warn with incorrect type",
  1530. me: newTestSession("me"),
  1531. givenCmd: []byte(`toc_evil them blah`),
  1532. wantMsg: cmdInternalSvcErr,
  1533. },
  1534. {
  1535. name: "bad command",
  1536. givenCmd: []byte(`toc_evil`),
  1537. wantMsg: cmdInternalSvcErr,
  1538. },
  1539. }
  1540. for _, tc := range cases {
  1541. t.Run(tc.name, func(t *testing.T) {
  1542. ctx := context.Background()
  1543. icbmSvc := newMockICBMService(t)
  1544. for _, params := range tc.mockParams.evilRequestParams {
  1545. icbmSvc.EXPECT().
  1546. EvilRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  1547. Return(params.msg, params.err)
  1548. }
  1549. svc := OSCARProxy{
  1550. Logger: slog.Default(),
  1551. ICBMService: icbmSvc,
  1552. }
  1553. msg := svc.Evil(ctx, tc.me, tc.givenCmd)
  1554. assert.Equal(t, tc.wantMsg, msg)
  1555. })
  1556. }
  1557. }
  1558. func TestOSCARProxy_ChangePassword(t *testing.T) {
  1559. cases := []struct {
  1560. // name is the unit test name
  1561. name string
  1562. // me is the TOC user session
  1563. me *state.Session
  1564. // givenCmd is the TOC command
  1565. givenCmd []byte
  1566. // wantMsg is the expected TOC response
  1567. wantMsg string
  1568. // mockParams is the list of params sent to mocks that satisfy this
  1569. // method's dependencies
  1570. mockParams mockParams
  1571. }{
  1572. {
  1573. name: "successfully change password",
  1574. me: newTestSession("me"),
  1575. givenCmd: []byte("toc_change_passwd oldpass newpass"),
  1576. mockParams: mockParams{
  1577. adminParams: adminParams{
  1578. infoChangeRequestParams: infoChangeRequestParams{
  1579. {
  1580. me: state.NewIdentScreenName("me"),
  1581. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1582. TLVRestBlock: wire.TLVRestBlock{
  1583. TLVList: wire.TLVList{
  1584. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1585. wire.NewTLVBE(wire.AdminTLVNewPassword, "newpass"),
  1586. },
  1587. },
  1588. },
  1589. msg: wire.SNACMessage{
  1590. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  1591. Permissions: wire.AdminInfoPermissionsReadWrite,
  1592. TLVBlock: wire.TLVBlock{
  1593. TLVList: wire.TLVList{
  1594. wire.NewTLVBE(wire.AdminTLVNewPassword, []byte{}),
  1595. },
  1596. },
  1597. },
  1598. },
  1599. },
  1600. },
  1601. },
  1602. },
  1603. wantMsg: "ADMIN_PASSWD_STATUS:0",
  1604. },
  1605. {
  1606. name: "change password - invalid password length",
  1607. me: newTestSession("me"),
  1608. givenCmd: []byte("toc_change_passwd oldpass np"),
  1609. mockParams: mockParams{
  1610. adminParams: adminParams{
  1611. infoChangeRequestParams: infoChangeRequestParams{
  1612. {
  1613. me: state.NewIdentScreenName("me"),
  1614. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1615. TLVRestBlock: wire.TLVRestBlock{
  1616. TLVList: wire.TLVList{
  1617. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1618. wire.NewTLVBE(wire.AdminTLVNewPassword, "np"),
  1619. },
  1620. },
  1621. },
  1622. msg: wire.SNACMessage{
  1623. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  1624. Permissions: wire.AdminInfoPermissionsReadWrite,
  1625. TLVBlock: wire.TLVBlock{
  1626. TLVList: wire.TLVList{
  1627. wire.NewTLVBE(wire.AdminTLVNewPassword, []byte{}),
  1628. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorInvalidPasswordLength),
  1629. },
  1630. },
  1631. },
  1632. },
  1633. },
  1634. },
  1635. },
  1636. },
  1637. wantMsg: "ERROR:911",
  1638. },
  1639. {
  1640. name: "change password - incorrect password",
  1641. me: newTestSession("me"),
  1642. givenCmd: []byte("toc_change_passwd oldpass baddpass"),
  1643. mockParams: mockParams{
  1644. adminParams: adminParams{
  1645. infoChangeRequestParams: infoChangeRequestParams{
  1646. {
  1647. me: state.NewIdentScreenName("me"),
  1648. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1649. TLVRestBlock: wire.TLVRestBlock{
  1650. TLVList: wire.TLVList{
  1651. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1652. wire.NewTLVBE(wire.AdminTLVNewPassword, "baddpass"),
  1653. },
  1654. },
  1655. },
  1656. msg: wire.SNACMessage{
  1657. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  1658. Permissions: wire.AdminInfoPermissionsReadWrite,
  1659. TLVBlock: wire.TLVBlock{
  1660. TLVList: wire.TLVList{
  1661. wire.NewTLVBE(wire.AdminTLVNewPassword, []byte{}),
  1662. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorValidatePassword),
  1663. },
  1664. },
  1665. },
  1666. },
  1667. },
  1668. },
  1669. },
  1670. },
  1671. wantMsg: "ERROR:912",
  1672. },
  1673. {
  1674. name: "change password - catch-all error response",
  1675. me: newTestSession("me"),
  1676. givenCmd: []byte("toc_change_passwd oldpass baddpass"),
  1677. mockParams: mockParams{
  1678. adminParams: adminParams{
  1679. infoChangeRequestParams: infoChangeRequestParams{
  1680. {
  1681. me: state.NewIdentScreenName("me"),
  1682. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1683. TLVRestBlock: wire.TLVRestBlock{
  1684. TLVList: wire.TLVList{
  1685. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1686. wire.NewTLVBE(wire.AdminTLVNewPassword, "baddpass"),
  1687. },
  1688. },
  1689. },
  1690. msg: wire.SNACMessage{
  1691. Body: wire.SNAC_0x07_0x05_AdminChangeReply{
  1692. Permissions: wire.AdminInfoPermissionsReadWrite,
  1693. TLVBlock: wire.TLVBlock{
  1694. TLVList: wire.TLVList{
  1695. wire.NewTLVBE(wire.AdminTLVNewPassword, []byte{}),
  1696. wire.NewTLVBE(wire.AdminTLVErrorCode, wire.AdminInfoErrorAllOtherErrors),
  1697. },
  1698. },
  1699. },
  1700. },
  1701. },
  1702. },
  1703. },
  1704. },
  1705. wantMsg: "ERROR:913",
  1706. },
  1707. {
  1708. name: "change password - runtime error from admin svc",
  1709. me: newTestSession("me"),
  1710. givenCmd: []byte("toc_change_passwd oldpass baddpass"),
  1711. mockParams: mockParams{
  1712. adminParams: adminParams{
  1713. infoChangeRequestParams: infoChangeRequestParams{
  1714. {
  1715. me: state.NewIdentScreenName("me"),
  1716. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1717. TLVRestBlock: wire.TLVRestBlock{
  1718. TLVList: wire.TLVList{
  1719. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1720. wire.NewTLVBE(wire.AdminTLVNewPassword, "baddpass"),
  1721. },
  1722. },
  1723. },
  1724. err: io.EOF,
  1725. },
  1726. },
  1727. },
  1728. },
  1729. wantMsg: cmdInternalSvcErr,
  1730. },
  1731. {
  1732. name: "change password - unexpected response from admin svc",
  1733. me: newTestSession("me"),
  1734. givenCmd: []byte("toc_change_passwd oldpass baddpass"),
  1735. mockParams: mockParams{
  1736. adminParams: adminParams{
  1737. infoChangeRequestParams: infoChangeRequestParams{
  1738. {
  1739. me: state.NewIdentScreenName("me"),
  1740. inBody: wire.SNAC_0x07_0x04_AdminInfoChangeRequest{
  1741. TLVRestBlock: wire.TLVRestBlock{
  1742. TLVList: wire.TLVList{
  1743. wire.NewTLVBE(wire.AdminTLVOldPassword, "oldpass"),
  1744. wire.NewTLVBE(wire.AdminTLVNewPassword, "baddpass"),
  1745. },
  1746. },
  1747. },
  1748. msg: wire.SNACMessage{
  1749. Body: wire.SNACError{},
  1750. },
  1751. },
  1752. },
  1753. },
  1754. },
  1755. wantMsg: cmdInternalSvcErr,
  1756. },
  1757. {
  1758. name: "bad command",
  1759. givenCmd: []byte(`toc_change_passwd`),
  1760. wantMsg: cmdInternalSvcErr,
  1761. },
  1762. }
  1763. for _, tc := range cases {
  1764. t.Run(tc.name, func(t *testing.T) {
  1765. ctx := context.Background()
  1766. adminSvc := newMockAdminService(t)
  1767. for _, params := range tc.mockParams.infoChangeRequestParams {
  1768. adminSvc.EXPECT().
  1769. InfoChangeRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  1770. Return(params.msg, params.err)
  1771. }
  1772. svc := OSCARProxy{
  1773. Logger: slog.Default(),
  1774. AdminService: adminSvc,
  1775. }
  1776. msg := svc.ChangePassword(ctx, tc.me, tc.givenCmd)
  1777. assert.Equal(t, tc.wantMsg, msg)
  1778. })
  1779. }
  1780. }
  1781. func TestOSCARProxy_GetDirSearchURL(t *testing.T) {
  1782. cases := []struct {
  1783. // name is the unit test name
  1784. name string
  1785. // me is the TOC user session
  1786. me *state.Session
  1787. // givenCmd is the TOC command
  1788. givenCmd []byte
  1789. // wantMsg is the expected TOC response
  1790. wantMsg string
  1791. // mockParams is the list of params sent to mocks that satisfy this
  1792. // method's dependencies
  1793. mockParams mockParams
  1794. }{
  1795. {
  1796. name: "successfully request user info",
  1797. me: newTestSession("me"),
  1798. givenCmd: []byte(`toc_dir_search "first name":"middle name":"last name":"maiden name":"city":"state":"country":"email"`),
  1799. mockParams: mockParams{
  1800. cookieBakerParams: cookieBakerParams{
  1801. issueParams: issueParams{
  1802. {
  1803. data: []byte("me"),
  1804. returnData: []byte("monster"),
  1805. },
  1806. },
  1807. },
  1808. },
  1809. 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",
  1810. },
  1811. {
  1812. name: "successfully request user info by keywords",
  1813. me: newTestSession("me"),
  1814. givenCmd: []byte(`toc_dir_search ::::::::::"searchkw"`),
  1815. mockParams: mockParams{
  1816. cookieBakerParams: cookieBakerParams{
  1817. issueParams: issueParams{
  1818. {
  1819. data: []byte("me"),
  1820. returnData: []byte("monster"),
  1821. },
  1822. },
  1823. },
  1824. },
  1825. wantMsg: "GOTO_URL:search results:dir_search?cookie=6d6f6e73746572&keyword=searchkw",
  1826. },
  1827. {
  1828. name: "request user info with too many params",
  1829. me: newTestSession("me"),
  1830. givenCmd: []byte(`toc_dir_search ::::::::::::::::::::"searchkw"`),
  1831. wantMsg: cmdInternalSvcErr,
  1832. },
  1833. {
  1834. name: "request user info, get cookie issue error",
  1835. me: newTestSession("me"),
  1836. givenCmd: []byte(`toc_dir_search them`),
  1837. mockParams: mockParams{
  1838. cookieBakerParams: cookieBakerParams{
  1839. issueParams: issueParams{
  1840. {
  1841. data: []byte("me"),
  1842. returnErr: io.EOF,
  1843. },
  1844. },
  1845. },
  1846. },
  1847. wantMsg: cmdInternalSvcErr,
  1848. },
  1849. {
  1850. name: "bad command",
  1851. givenCmd: []byte(`toc_dir_search`),
  1852. wantMsg: cmdInternalSvcErr,
  1853. },
  1854. }
  1855. for _, tc := range cases {
  1856. t.Run(tc.name, func(t *testing.T) {
  1857. ctx := context.Background()
  1858. cookieBaker := newMockCookieBaker(t)
  1859. for _, params := range tc.mockParams.issueParams {
  1860. cookieBaker.EXPECT().
  1861. Issue(params.data).
  1862. Return(params.returnData, params.returnErr)
  1863. }
  1864. svc := OSCARProxy{
  1865. Logger: slog.Default(),
  1866. CookieBaker: cookieBaker,
  1867. }
  1868. msg := svc.GetDirSearchURL(ctx, tc.me, tc.givenCmd)
  1869. assert.Equal(t, tc.wantMsg, msg)
  1870. })
  1871. }
  1872. }
  1873. func TestOSCARProxy_GetDirURL(t *testing.T) {
  1874. cases := []struct {
  1875. // name is the unit test name
  1876. name string
  1877. // me is the TOC user session
  1878. me *state.Session
  1879. // givenCmd is the TOC command
  1880. givenCmd []byte
  1881. // wantMsg is the expected TOC response
  1882. wantMsg string
  1883. // mockParams is the list of params sent to mocks that satisfy this
  1884. // method's dependencies
  1885. mockParams mockParams
  1886. }{
  1887. {
  1888. name: "successfully request user dir info",
  1889. me: newTestSession("me"),
  1890. givenCmd: []byte(`toc_get_dir them`),
  1891. mockParams: mockParams{
  1892. cookieBakerParams: cookieBakerParams{
  1893. issueParams: issueParams{
  1894. {
  1895. data: []byte("me"),
  1896. returnData: []byte("monster"),
  1897. },
  1898. },
  1899. },
  1900. },
  1901. wantMsg: "GOTO_URL:directory info:dir_info?cookie=6d6f6e73746572&user=them",
  1902. },
  1903. {
  1904. name: "request user info, get cookie issue error",
  1905. me: newTestSession("me"),
  1906. givenCmd: []byte(`toc_get_dir them`),
  1907. mockParams: mockParams{
  1908. cookieBakerParams: cookieBakerParams{
  1909. issueParams: issueParams{
  1910. {
  1911. data: []byte("me"),
  1912. returnErr: io.EOF,
  1913. },
  1914. },
  1915. },
  1916. },
  1917. wantMsg: cmdInternalSvcErr,
  1918. },
  1919. {
  1920. name: "bad command",
  1921. givenCmd: []byte(`toc_get_dir`),
  1922. wantMsg: cmdInternalSvcErr,
  1923. },
  1924. }
  1925. for _, tc := range cases {
  1926. t.Run(tc.name, func(t *testing.T) {
  1927. ctx := context.Background()
  1928. cookieBaker := newMockCookieBaker(t)
  1929. for _, params := range tc.mockParams.issueParams {
  1930. cookieBaker.EXPECT().
  1931. Issue(params.data).
  1932. Return(params.returnData, params.returnErr)
  1933. }
  1934. svc := OSCARProxy{
  1935. Logger: slog.Default(),
  1936. CookieBaker: cookieBaker,
  1937. }
  1938. msg := svc.GetDirURL(ctx, tc.me, tc.givenCmd)
  1939. assert.Equal(t, tc.wantMsg, msg)
  1940. })
  1941. }
  1942. }
  1943. func TestOSCARProxy_GetInfoURL(t *testing.T) {
  1944. cases := []struct {
  1945. // name is the unit test name
  1946. name string
  1947. // me is the TOC user session
  1948. me *state.Session
  1949. // givenCmd is the TOC command
  1950. givenCmd []byte
  1951. // wantMsg is the expected TOC response
  1952. wantMsg string
  1953. // mockParams is the list of params sent to mocks that satisfy this
  1954. // method's dependencies
  1955. mockParams mockParams
  1956. }{
  1957. {
  1958. name: "successfully request user info",
  1959. me: newTestSession("me"),
  1960. givenCmd: []byte(`toc_get_info them`),
  1961. mockParams: mockParams{
  1962. cookieBakerParams: cookieBakerParams{
  1963. issueParams: issueParams{
  1964. {
  1965. data: []byte("me"),
  1966. returnData: []byte("monster"),
  1967. },
  1968. },
  1969. },
  1970. },
  1971. wantMsg: "GOTO_URL:profile:info?cookie=6d6f6e73746572&from=me&user=them",
  1972. },
  1973. {
  1974. name: "request user info, get cookie issue error",
  1975. me: newTestSession("me"),
  1976. givenCmd: []byte(`toc_get_info them`),
  1977. mockParams: mockParams{
  1978. cookieBakerParams: cookieBakerParams{
  1979. issueParams: issueParams{
  1980. {
  1981. data: []byte("me"),
  1982. returnErr: io.EOF,
  1983. },
  1984. },
  1985. },
  1986. },
  1987. wantMsg: cmdInternalSvcErr,
  1988. },
  1989. {
  1990. name: "bad command",
  1991. givenCmd: []byte(`toc_get_info`),
  1992. wantMsg: cmdInternalSvcErr,
  1993. },
  1994. }
  1995. for _, tc := range cases {
  1996. t.Run(tc.name, func(t *testing.T) {
  1997. ctx := context.Background()
  1998. cookieBaker := newMockCookieBaker(t)
  1999. for _, params := range tc.mockParams.issueParams {
  2000. cookieBaker.EXPECT().
  2001. Issue(params.data).
  2002. Return(params.returnData, params.returnErr)
  2003. }
  2004. svc := OSCARProxy{
  2005. Logger: slog.Default(),
  2006. CookieBaker: cookieBaker,
  2007. }
  2008. msg := svc.GetInfoURL(ctx, tc.me, tc.givenCmd)
  2009. assert.Equal(t, tc.wantMsg, msg)
  2010. })
  2011. }
  2012. }
  2013. func TestOSCARProxy_GetStatus(t *testing.T) {
  2014. cases := []struct {
  2015. // name is the unit test name
  2016. name string
  2017. // me is the TOC user session
  2018. me *state.Session
  2019. // givenCmd is the TOC command
  2020. givenCmd []byte
  2021. // wantMsg is the expected TOC response
  2022. wantMsg string
  2023. // mockParams is the list of params sent to mocks that satisfy this
  2024. // method's dependencies
  2025. mockParams mockParams
  2026. }{
  2027. {
  2028. name: "successfully request status",
  2029. me: newTestSession("me"),
  2030. givenCmd: []byte("toc_get_status them"),
  2031. mockParams: mockParams{
  2032. locateParams: locateParams{
  2033. userInfoQueryParams: userInfoQueryParams{
  2034. {
  2035. me: state.NewIdentScreenName("me"),
  2036. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2037. ScreenName: "them",
  2038. },
  2039. msg: wire.SNACMessage{
  2040. Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  2041. TLVUserInfo: wire.TLVUserInfo{
  2042. ScreenName: "them",
  2043. WarningLevel: 0,
  2044. TLVBlock: wire.TLVBlock{
  2045. TLVList: wire.TLVList{
  2046. wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
  2047. wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
  2048. },
  2049. },
  2050. },
  2051. },
  2052. },
  2053. },
  2054. },
  2055. },
  2056. },
  2057. wantMsg: "UPDATE_BUDDY:them:T:0:1234:5678: O ",
  2058. },
  2059. {
  2060. name: "request status, receive err from locate svc",
  2061. me: newTestSession("me"),
  2062. givenCmd: []byte("toc_get_status them"),
  2063. mockParams: mockParams{
  2064. locateParams: locateParams{
  2065. userInfoQueryParams: userInfoQueryParams{
  2066. {
  2067. me: state.NewIdentScreenName("me"),
  2068. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2069. ScreenName: "them",
  2070. },
  2071. err: io.EOF,
  2072. },
  2073. },
  2074. },
  2075. },
  2076. wantMsg: cmdInternalSvcErr,
  2077. },
  2078. {
  2079. name: "request status, user not online",
  2080. me: newTestSession("me"),
  2081. givenCmd: []byte("toc_get_status them"),
  2082. mockParams: mockParams{
  2083. locateParams: locateParams{
  2084. userInfoQueryParams: userInfoQueryParams{
  2085. {
  2086. me: state.NewIdentScreenName("me"),
  2087. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2088. ScreenName: "them",
  2089. },
  2090. msg: wire.SNACMessage{
  2091. Body: wire.SNACError{
  2092. Code: wire.ErrorCodeNotLoggedOn,
  2093. },
  2094. },
  2095. },
  2096. },
  2097. },
  2098. },
  2099. wantMsg: "ERROR:901:them",
  2100. },
  2101. {
  2102. name: "request status, receive unexpected error code",
  2103. me: newTestSession("me"),
  2104. givenCmd: []byte("toc_get_status them"),
  2105. mockParams: mockParams{
  2106. locateParams: locateParams{
  2107. userInfoQueryParams: userInfoQueryParams{
  2108. {
  2109. me: state.NewIdentScreenName("me"),
  2110. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2111. ScreenName: "them",
  2112. },
  2113. msg: wire.SNACMessage{
  2114. Body: wire.SNACError{
  2115. Code: wire.ErrorCodeInvalidSnac,
  2116. },
  2117. },
  2118. },
  2119. },
  2120. },
  2121. },
  2122. wantMsg: cmdInternalSvcErr,
  2123. },
  2124. {
  2125. name: "request status, unexpected response from locate svc",
  2126. me: newTestSession("me"),
  2127. givenCmd: []byte("toc_get_status them"),
  2128. mockParams: mockParams{
  2129. locateParams: locateParams{
  2130. userInfoQueryParams: userInfoQueryParams{
  2131. {
  2132. me: state.NewIdentScreenName("me"),
  2133. inBody: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  2134. ScreenName: "them",
  2135. },
  2136. msg: wire.SNACMessage{
  2137. Body: wire.SNAC_0x0E_0x04_ChatUsersLeft{},
  2138. },
  2139. },
  2140. },
  2141. },
  2142. },
  2143. wantMsg: cmdInternalSvcErr,
  2144. },
  2145. {
  2146. name: "bad command",
  2147. givenCmd: []byte(`toc_get_status`),
  2148. wantMsg: cmdInternalSvcErr,
  2149. },
  2150. }
  2151. for _, tc := range cases {
  2152. t.Run(tc.name, func(t *testing.T) {
  2153. ctx := context.Background()
  2154. locateSvc := newMockLocateService(t)
  2155. for _, params := range tc.mockParams.userInfoQueryParams {
  2156. locateSvc.EXPECT().
  2157. UserInfoQuery(mock.Anything, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  2158. Return(params.msg, params.err)
  2159. }
  2160. svc := OSCARProxy{
  2161. Logger: slog.Default(),
  2162. LocateService: locateSvc,
  2163. }
  2164. msg := svc.GetStatus(ctx, tc.me, tc.givenCmd)
  2165. assert.Equal(t, tc.wantMsg, msg)
  2166. })
  2167. }
  2168. }
  2169. func TestOSCARProxy_InitDone(t *testing.T) {
  2170. cases := []struct {
  2171. // name is the unit test name
  2172. name string
  2173. // me is the TOC user session
  2174. me *state.Session
  2175. // givenCmd is the TOC command
  2176. givenCmd []byte
  2177. // wantMsg is the expected TOC response
  2178. wantMsg string
  2179. // mockParams is the list of params sent to mocks that satisfy this
  2180. // method's dependencies
  2181. mockParams mockParams
  2182. }{
  2183. {
  2184. name: "successfully initialize connection",
  2185. me: newTestSession("me"),
  2186. givenCmd: []byte(`toc_init_done`),
  2187. mockParams: mockParams{
  2188. oServiceBOSParams: oServiceParams{
  2189. clientOnlineParams: clientOnlineParams{
  2190. {
  2191. me: state.NewIdentScreenName("me"),
  2192. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  2193. },
  2194. },
  2195. },
  2196. },
  2197. },
  2198. {
  2199. name: "initialize connection, receive err from BOS oservice svc",
  2200. me: newTestSession("me"),
  2201. givenCmd: []byte(`toc_init_done`),
  2202. mockParams: mockParams{
  2203. oServiceBOSParams: oServiceParams{
  2204. clientOnlineParams: clientOnlineParams{
  2205. {
  2206. me: state.NewIdentScreenName("me"),
  2207. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  2208. err: io.EOF,
  2209. },
  2210. },
  2211. },
  2212. },
  2213. wantMsg: cmdInternalSvcErr,
  2214. },
  2215. {
  2216. name: "bad command",
  2217. givenCmd: []byte(`toc_init_done_diff`),
  2218. wantMsg: cmdInternalSvcErr,
  2219. },
  2220. }
  2221. for _, tc := range cases {
  2222. t.Run(tc.name, func(t *testing.T) {
  2223. ctx := context.Background()
  2224. oSvc := newMockOServiceService(t)
  2225. for _, params := range tc.mockParams.oServiceBOSParams.clientOnlineParams {
  2226. oSvc.EXPECT().
  2227. ClientOnline(ctx, params.body, matchSession(params.me)).
  2228. Return(params.err)
  2229. }
  2230. svc := OSCARProxy{
  2231. Logger: slog.Default(),
  2232. OServiceServiceBOS: oSvc,
  2233. }
  2234. msg := svc.InitDone(ctx, tc.me, tc.givenCmd)
  2235. assert.Equal(t, tc.wantMsg, msg)
  2236. })
  2237. }
  2238. }
  2239. func TestOSCARProxy_RemoveBuddy(t *testing.T) {
  2240. cases := []struct {
  2241. // name is the unit test name
  2242. name string
  2243. // me is the TOC user session
  2244. me *state.Session
  2245. // givenCmd is the TOC command
  2246. givenCmd []byte
  2247. // wantMsg is the expected TOC response
  2248. wantMsg string
  2249. // mockParams is the list of params sent to mocks that satisfy this
  2250. // method's dependencies
  2251. mockParams mockParams
  2252. }{
  2253. {
  2254. name: "successfully remove buddies",
  2255. me: newTestSession("me"),
  2256. givenCmd: []byte("toc_remove_buddy friend1 friend2 friend3"),
  2257. mockParams: mockParams{
  2258. buddyParams: buddyParams{
  2259. delBuddiesParams: delBuddiesParams{
  2260. {
  2261. me: state.NewIdentScreenName("me"),
  2262. inBody: wire.SNAC_0x03_0x05_BuddyDelBuddies{
  2263. Buddies: []struct {
  2264. ScreenName string `oscar:"len_prefix=uint8"`
  2265. }{
  2266. {ScreenName: "friend1"},
  2267. {ScreenName: "friend2"},
  2268. {ScreenName: "friend3"},
  2269. },
  2270. },
  2271. },
  2272. },
  2273. },
  2274. },
  2275. },
  2276. {
  2277. name: "remove buddies, receive error from buddy service",
  2278. me: newTestSession("me"),
  2279. givenCmd: []byte("toc_remove_buddy friend1"),
  2280. mockParams: mockParams{
  2281. buddyParams: buddyParams{
  2282. delBuddiesParams: delBuddiesParams{
  2283. {
  2284. me: state.NewIdentScreenName("me"),
  2285. inBody: wire.SNAC_0x03_0x05_BuddyDelBuddies{
  2286. Buddies: []struct {
  2287. ScreenName string `oscar:"len_prefix=uint8"`
  2288. }{
  2289. {ScreenName: "friend1"},
  2290. },
  2291. },
  2292. err: io.EOF,
  2293. },
  2294. },
  2295. },
  2296. },
  2297. wantMsg: cmdInternalSvcErr,
  2298. },
  2299. {
  2300. name: "bad command",
  2301. givenCmd: []byte(`toc_remove_buddy_bad`),
  2302. wantMsg: cmdInternalSvcErr,
  2303. },
  2304. }
  2305. for _, tc := range cases {
  2306. t.Run(tc.name, func(t *testing.T) {
  2307. ctx := context.Background()
  2308. buddySvc := newMockBuddyService(t)
  2309. for _, params := range tc.mockParams.delBuddiesParams {
  2310. buddySvc.EXPECT().
  2311. DelBuddies(ctx, matchSession(params.me), params.inBody).
  2312. Return(params.err)
  2313. }
  2314. svc := OSCARProxy{
  2315. Logger: slog.Default(),
  2316. BuddyService: buddySvc,
  2317. }
  2318. msg := svc.RemoveBuddy(ctx, tc.me, tc.givenCmd)
  2319. assert.Equal(t, tc.wantMsg, msg)
  2320. })
  2321. }
  2322. }
  2323. func TestOSCARProxy_SendIM(t *testing.T) {
  2324. cases := []struct {
  2325. // name is the unit test name
  2326. name string
  2327. // me is the TOC user session
  2328. me *state.Session
  2329. // givenCmd is the TOC command
  2330. givenCmd []byte
  2331. // wantMsg is the expected TOC response
  2332. wantMsg string
  2333. // mockParams is the list of params sent to mocks that satisfy this
  2334. // method's dependencies
  2335. mockParams mockParams
  2336. }{
  2337. {
  2338. name: "successfully send instant message",
  2339. me: newTestSession("me"),
  2340. givenCmd: []byte(`toc_send_im chattingChuck "hello world!"`),
  2341. mockParams: mockParams{
  2342. icbmParams: icbmParams{
  2343. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  2344. {
  2345. sender: state.NewIdentScreenName("me"),
  2346. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2347. ChannelID: wire.ICBMChannelIM,
  2348. ScreenName: "chattingChuck",
  2349. TLVRestBlock: wire.TLVRestBlock{
  2350. TLVList: wire.TLVList{
  2351. wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
  2352. {
  2353. ID: 5,
  2354. Version: 1,
  2355. Payload: []byte{1, 1, 2},
  2356. },
  2357. {
  2358. ID: 1,
  2359. Version: 1,
  2360. Payload: []byte{
  2361. 0x00, 0x00,
  2362. 0x00, 0x00,
  2363. 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
  2364. },
  2365. },
  2366. }),
  2367. },
  2368. },
  2369. },
  2370. },
  2371. },
  2372. },
  2373. },
  2374. },
  2375. {
  2376. name: "successfully auto-reply send instant message",
  2377. me: newTestSession("me"),
  2378. givenCmd: []byte(`toc_send_im chattingChuck "hello world!" auto`),
  2379. mockParams: mockParams{
  2380. icbmParams: icbmParams{
  2381. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  2382. {
  2383. sender: state.NewIdentScreenName("me"),
  2384. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2385. ChannelID: wire.ICBMChannelIM,
  2386. ScreenName: "chattingChuck",
  2387. TLVRestBlock: wire.TLVRestBlock{
  2388. TLVList: wire.TLVList{
  2389. wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
  2390. {
  2391. ID: 5,
  2392. Version: 1,
  2393. Payload: []byte{1, 1, 2},
  2394. },
  2395. {
  2396. ID: 1,
  2397. Version: 1,
  2398. Payload: []byte{
  2399. 0x00, 0x00,
  2400. 0x00, 0x00,
  2401. 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
  2402. },
  2403. },
  2404. }),
  2405. wire.NewTLVBE(wire.ICBMTLVAutoResponse, []byte{}),
  2406. },
  2407. },
  2408. },
  2409. },
  2410. },
  2411. },
  2412. },
  2413. },
  2414. {
  2415. name: "send instant message, receive error from ICBM service",
  2416. me: newTestSession("me"),
  2417. givenCmd: []byte(`toc_send_im chattingChuck "hello world!"`),
  2418. mockParams: mockParams{
  2419. icbmParams: icbmParams{
  2420. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  2421. {
  2422. sender: state.NewIdentScreenName("me"),
  2423. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  2424. ChannelID: wire.ICBMChannelIM,
  2425. ScreenName: "chattingChuck",
  2426. TLVRestBlock: wire.TLVRestBlock{
  2427. TLVList: wire.TLVList{
  2428. wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
  2429. {
  2430. ID: 5,
  2431. Version: 1,
  2432. Payload: []byte{1, 1, 2},
  2433. },
  2434. {
  2435. ID: 1,
  2436. Version: 1,
  2437. Payload: []byte{
  2438. 0x00, 0x00,
  2439. 0x00, 0x00,
  2440. 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
  2441. },
  2442. },
  2443. }),
  2444. },
  2445. },
  2446. },
  2447. err: io.EOF,
  2448. },
  2449. },
  2450. },
  2451. },
  2452. wantMsg: cmdInternalSvcErr,
  2453. },
  2454. {
  2455. name: "bad command",
  2456. givenCmd: []byte(`toc_send_im`),
  2457. wantMsg: cmdInternalSvcErr,
  2458. },
  2459. }
  2460. for _, tc := range cases {
  2461. t.Run(tc.name, func(t *testing.T) {
  2462. ctx := context.Background()
  2463. icbmSvc := newMockICBMService(t)
  2464. for _, params := range tc.mockParams.channelMsgToHostParamsICBM {
  2465. icbmSvc.EXPECT().
  2466. ChannelMsgToHost(ctx, matchSession(params.sender), params.inFrame, params.inBody).
  2467. Return(params.result, params.err)
  2468. }
  2469. svc := OSCARProxy{
  2470. Logger: slog.Default(),
  2471. ICBMService: icbmSvc,
  2472. }
  2473. msg := svc.SendIM(ctx, tc.me, tc.givenCmd)
  2474. assert.Equal(t, tc.wantMsg, msg)
  2475. })
  2476. }
  2477. }
  2478. func TestOSCARProxy_SetAway(t *testing.T) {
  2479. cases := []struct {
  2480. // name is the unit test name
  2481. name string
  2482. // me is the TOC user session
  2483. me *state.Session
  2484. // givenCmd is the TOC command
  2485. givenCmd []byte
  2486. // wantMsg is the expected TOC response
  2487. wantMsg string
  2488. // mockParams is the list of params sent to mocks that satisfy this
  2489. // method's dependencies
  2490. mockParams mockParams
  2491. }{
  2492. {
  2493. name: "successfully set away with message",
  2494. me: newTestSession("me"),
  2495. givenCmd: []byte(`toc_set_away "I'm away from my computer right now."`),
  2496. mockParams: mockParams{
  2497. locateParams: locateParams{
  2498. setInfoParams: setInfoParams{
  2499. {
  2500. me: state.NewIdentScreenName("me"),
  2501. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2502. TLVRestBlock: wire.TLVRestBlock{
  2503. TLVList: wire.TLVList{
  2504. wire.NewTLVBE(wire.LocateTLVTagsInfoUnavailableData, "I'm away from my computer right now."),
  2505. },
  2506. },
  2507. },
  2508. },
  2509. },
  2510. },
  2511. },
  2512. },
  2513. {
  2514. name: "successfully set away without message",
  2515. me: newTestSession("me"),
  2516. givenCmd: []byte(`toc_set_away`),
  2517. mockParams: mockParams{
  2518. locateParams: locateParams{
  2519. setInfoParams: setInfoParams{
  2520. {
  2521. me: state.NewIdentScreenName("me"),
  2522. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2523. TLVRestBlock: wire.TLVRestBlock{
  2524. TLVList: wire.TLVList{
  2525. wire.NewTLVBE(wire.LocateTLVTagsInfoUnavailableData, ""),
  2526. },
  2527. },
  2528. },
  2529. },
  2530. },
  2531. },
  2532. },
  2533. },
  2534. {
  2535. name: "set away message, receive error from locate service",
  2536. me: newTestSession("me"),
  2537. givenCmd: []byte(`toc_set_away "I'm away from my computer right now."`),
  2538. mockParams: mockParams{
  2539. locateParams: locateParams{
  2540. setInfoParams: setInfoParams{
  2541. {
  2542. me: state.NewIdentScreenName("me"),
  2543. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2544. TLVRestBlock: wire.TLVRestBlock{
  2545. TLVList: wire.TLVList{
  2546. wire.NewTLVBE(wire.LocateTLVTagsInfoUnavailableData, "I'm away from my computer right now."),
  2547. },
  2548. },
  2549. },
  2550. err: io.EOF,
  2551. },
  2552. },
  2553. },
  2554. },
  2555. wantMsg: cmdInternalSvcErr,
  2556. },
  2557. {
  2558. name: "bad command",
  2559. givenCmd: []byte(`toc_set_away_bad`),
  2560. wantMsg: cmdInternalSvcErr,
  2561. },
  2562. }
  2563. for _, tc := range cases {
  2564. t.Run(tc.name, func(t *testing.T) {
  2565. ctx := context.Background()
  2566. locateSvc := newMockLocateService(t)
  2567. for _, params := range tc.mockParams.setInfoParams {
  2568. locateSvc.EXPECT().
  2569. SetInfo(ctx, matchSession(params.me), params.inBody).
  2570. Return(params.err)
  2571. }
  2572. svc := OSCARProxy{
  2573. Logger: slog.Default(),
  2574. LocateService: locateSvc,
  2575. }
  2576. msg := svc.SetAway(ctx, tc.me, tc.givenCmd)
  2577. assert.Equal(t, tc.wantMsg, msg)
  2578. })
  2579. }
  2580. }
  2581. func TestOSCARProxy_SetCaps(t *testing.T) {
  2582. cases := []struct {
  2583. // name is the unit test name
  2584. name string
  2585. // me is the TOC user session
  2586. me *state.Session
  2587. // givenCmd is the TOC command
  2588. givenCmd []byte
  2589. // wantMsg is the expected TOC response
  2590. wantMsg string
  2591. // mockParams is the list of params sent to mocks that satisfy this
  2592. // method's dependencies
  2593. mockParams mockParams
  2594. }{
  2595. {
  2596. name: "successfully set capabilities",
  2597. me: newTestSession("me"),
  2598. givenCmd: []byte(`toc_set_caps 09460000-4C7F-11D1-8222-444553540000 09460001-4C7F-11D1-8222-444553540000`),
  2599. mockParams: mockParams{
  2600. locateParams: locateParams{
  2601. setInfoParams: setInfoParams{
  2602. {
  2603. me: state.NewIdentScreenName("me"),
  2604. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2605. TLVRestBlock: wire.TLVRestBlock{
  2606. TLVList: wire.TLVList{
  2607. wire.NewTLVBE(wire.LocateTLVTagsInfoCapabilities, []uuid.UUID{
  2608. uuid.MustParse("09460000-4C7F-11D1-8222-444553540000"),
  2609. uuid.MustParse("09460001-4C7F-11D1-8222-444553540000"),
  2610. capChat,
  2611. }),
  2612. },
  2613. },
  2614. },
  2615. },
  2616. },
  2617. },
  2618. },
  2619. },
  2620. {
  2621. name: "set capability, receive error from locate service",
  2622. me: newTestSession("me"),
  2623. givenCmd: []byte(`toc_set_caps 09460000-4C7F-11D1-8222-444553540000`),
  2624. mockParams: mockParams{
  2625. locateParams: locateParams{
  2626. setInfoParams: setInfoParams{
  2627. {
  2628. me: state.NewIdentScreenName("me"),
  2629. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2630. TLVRestBlock: wire.TLVRestBlock{
  2631. TLVList: wire.TLVList{
  2632. wire.NewTLVBE(wire.LocateTLVTagsInfoCapabilities, []uuid.UUID{
  2633. uuid.MustParse("09460000-4C7F-11D1-8222-444553540000"),
  2634. capChat,
  2635. }),
  2636. },
  2637. },
  2638. },
  2639. err: io.EOF,
  2640. },
  2641. },
  2642. },
  2643. },
  2644. wantMsg: cmdInternalSvcErr,
  2645. },
  2646. {
  2647. name: "set malformed capability UUID",
  2648. me: newTestSession("me"),
  2649. givenCmd: []byte(`toc_set_caps 09460000-`),
  2650. wantMsg: cmdInternalSvcErr,
  2651. },
  2652. {
  2653. name: "bad command",
  2654. givenCmd: []byte(`toc_set_caps_bad`),
  2655. wantMsg: cmdInternalSvcErr,
  2656. },
  2657. }
  2658. for _, tc := range cases {
  2659. t.Run(tc.name, func(t *testing.T) {
  2660. ctx := context.Background()
  2661. locateSvc := newMockLocateService(t)
  2662. for _, params := range tc.mockParams.setInfoParams {
  2663. locateSvc.EXPECT().
  2664. SetInfo(ctx, matchSession(params.me), params.inBody).
  2665. Return(params.err)
  2666. }
  2667. svc := OSCARProxy{
  2668. Logger: slog.Default(),
  2669. LocateService: locateSvc,
  2670. }
  2671. msg := svc.SetCaps(ctx, tc.me, tc.givenCmd)
  2672. assert.Equal(t, tc.wantMsg, msg)
  2673. })
  2674. }
  2675. }
  2676. func TestOSCARProxy_SetConfig(t *testing.T) {
  2677. cases := []struct {
  2678. // name is the unit test name
  2679. name string
  2680. // me is the TOC user session
  2681. me *state.Session
  2682. // givenCmd is the TOC command
  2683. givenCmd []byte
  2684. // wantMsg is the expected TOC response
  2685. wantMsg string
  2686. // mockParams is the list of params sent to mocks that satisfy this
  2687. // method's dependencies
  2688. mockParams mockParams
  2689. }{
  2690. {
  2691. name: "successfully set permit all config",
  2692. me: newTestSession("me"),
  2693. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\nb friend2\n}\n"),
  2694. mockParams: mockParams{
  2695. permitDenyParams: permitDenyParams{
  2696. // confusingly, setting "permit all" mode requires adding a
  2697. // deny list entry
  2698. addDenyListEntriesParams: addDenyListEntriesParams{
  2699. {
  2700. me: state.NewIdentScreenName("me"),
  2701. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2702. Users: []struct {
  2703. ScreenName string `oscar:"len_prefix=uint8"`
  2704. }{
  2705. {ScreenName: "me"},
  2706. },
  2707. },
  2708. },
  2709. },
  2710. },
  2711. buddyParams: buddyParams{
  2712. addBuddiesParams: addBuddiesParams{
  2713. {
  2714. me: state.NewIdentScreenName("me"),
  2715. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2716. Buddies: []struct {
  2717. ScreenName string `oscar:"len_prefix=uint8"`
  2718. }{
  2719. {ScreenName: "friend1"},
  2720. {ScreenName: "friend2"},
  2721. },
  2722. },
  2723. },
  2724. },
  2725. },
  2726. tocConfigParams: tocConfigParams{
  2727. setTOCConfigParams: setTOCConfigParams{
  2728. {
  2729. user: state.NewIdentScreenName("me"),
  2730. config: "m 1\ng Buddies\nb friend1\nb friend2",
  2731. },
  2732. },
  2733. },
  2734. },
  2735. },
  2736. {
  2737. name: "set permit all config, receive err from config store svc",
  2738. me: newTestSession("me"),
  2739. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\n}\n"),
  2740. mockParams: mockParams{
  2741. permitDenyParams: permitDenyParams{
  2742. // confusingly, setting "permit all" mode requires adding a
  2743. // deny list entry
  2744. addDenyListEntriesParams: addDenyListEntriesParams{
  2745. {
  2746. me: state.NewIdentScreenName("me"),
  2747. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2748. Users: []struct {
  2749. ScreenName string `oscar:"len_prefix=uint8"`
  2750. }{
  2751. {ScreenName: "me"},
  2752. },
  2753. },
  2754. },
  2755. },
  2756. },
  2757. buddyParams: buddyParams{
  2758. addBuddiesParams: addBuddiesParams{
  2759. {
  2760. me: state.NewIdentScreenName("me"),
  2761. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2762. Buddies: []struct {
  2763. ScreenName string `oscar:"len_prefix=uint8"`
  2764. }{
  2765. {ScreenName: "friend1"},
  2766. },
  2767. },
  2768. },
  2769. },
  2770. },
  2771. tocConfigParams: tocConfigParams{
  2772. setTOCConfigParams: setTOCConfigParams{
  2773. {
  2774. user: state.NewIdentScreenName("me"),
  2775. config: "m 1\ng Buddies\nb friend1",
  2776. err: io.EOF,
  2777. },
  2778. },
  2779. },
  2780. },
  2781. wantMsg: cmdInternalSvcErr,
  2782. },
  2783. {
  2784. name: "set permit all config, receive err from buddy svc",
  2785. me: newTestSession("me"),
  2786. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\n}\n"),
  2787. mockParams: mockParams{
  2788. permitDenyParams: permitDenyParams{
  2789. // confusingly, setting "permit all" mode requires adding a
  2790. // deny list entry
  2791. addDenyListEntriesParams: addDenyListEntriesParams{
  2792. {
  2793. me: state.NewIdentScreenName("me"),
  2794. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2795. Users: []struct {
  2796. ScreenName string `oscar:"len_prefix=uint8"`
  2797. }{
  2798. {ScreenName: "me"},
  2799. },
  2800. },
  2801. },
  2802. },
  2803. },
  2804. buddyParams: buddyParams{
  2805. addBuddiesParams: addBuddiesParams{
  2806. {
  2807. me: state.NewIdentScreenName("me"),
  2808. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2809. Buddies: []struct {
  2810. ScreenName string `oscar:"len_prefix=uint8"`
  2811. }{
  2812. {ScreenName: "friend1"},
  2813. },
  2814. },
  2815. err: io.EOF,
  2816. },
  2817. },
  2818. },
  2819. },
  2820. wantMsg: cmdInternalSvcErr,
  2821. },
  2822. {
  2823. name: "set permit all config, receive err from permit-deny svc",
  2824. me: newTestSession("me"),
  2825. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\nb friend2\n}\n"),
  2826. mockParams: mockParams{
  2827. permitDenyParams: permitDenyParams{
  2828. // confusingly, setting "permit all" mode requires adding a
  2829. // deny list entry
  2830. addDenyListEntriesParams: addDenyListEntriesParams{
  2831. {
  2832. me: state.NewIdentScreenName("me"),
  2833. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2834. Users: []struct {
  2835. ScreenName string `oscar:"len_prefix=uint8"`
  2836. }{
  2837. {ScreenName: "me"},
  2838. },
  2839. },
  2840. err: io.EOF,
  2841. },
  2842. },
  2843. },
  2844. },
  2845. wantMsg: cmdInternalSvcErr,
  2846. },
  2847. {
  2848. name: "successfully set deny all config",
  2849. me: newTestSession("me"),
  2850. givenCmd: []byte("toc_set_config {m 2\ng Buddies\nb friend1\nb friend2\n}\n"),
  2851. mockParams: mockParams{
  2852. permitDenyParams: permitDenyParams{
  2853. // confusingly, setting "deny all" mode requires adding a
  2854. // permit list entry
  2855. addPermListEntriesParams: addPermListEntriesParams{
  2856. {
  2857. me: state.NewIdentScreenName("me"),
  2858. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  2859. Users: []struct {
  2860. ScreenName string `oscar:"len_prefix=uint8"`
  2861. }{
  2862. {ScreenName: "me"},
  2863. },
  2864. },
  2865. },
  2866. },
  2867. },
  2868. buddyParams: buddyParams{
  2869. addBuddiesParams: addBuddiesParams{
  2870. {
  2871. me: state.NewIdentScreenName("me"),
  2872. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2873. Buddies: []struct {
  2874. ScreenName string `oscar:"len_prefix=uint8"`
  2875. }{
  2876. {ScreenName: "friend1"},
  2877. {ScreenName: "friend2"},
  2878. },
  2879. },
  2880. },
  2881. },
  2882. },
  2883. tocConfigParams: tocConfigParams{
  2884. setTOCConfigParams: setTOCConfigParams{
  2885. {
  2886. user: state.NewIdentScreenName("me"),
  2887. config: "m 2\ng Buddies\nb friend1\nb friend2",
  2888. },
  2889. },
  2890. },
  2891. },
  2892. },
  2893. {
  2894. name: "set deny all config, receive err from permit-deny svc",
  2895. me: newTestSession("me"),
  2896. givenCmd: []byte("toc_set_config {m 2\ng Buddies\nb friend1\nb friend2\n}\n"),
  2897. mockParams: mockParams{
  2898. permitDenyParams: permitDenyParams{
  2899. // confusingly, setting "deny all" mode requires adding a
  2900. // permit list entry
  2901. addPermListEntriesParams: addPermListEntriesParams{
  2902. {
  2903. me: state.NewIdentScreenName("me"),
  2904. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  2905. Users: []struct {
  2906. ScreenName string `oscar:"len_prefix=uint8"`
  2907. }{
  2908. {ScreenName: "me"},
  2909. },
  2910. },
  2911. err: io.EOF,
  2912. },
  2913. },
  2914. },
  2915. },
  2916. wantMsg: cmdInternalSvcErr,
  2917. },
  2918. {
  2919. name: "successfully set permit some config",
  2920. me: newTestSession("me"),
  2921. givenCmd: []byte("toc_set_config {m 3\np friend3\np friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  2922. mockParams: mockParams{
  2923. permitDenyParams: permitDenyParams{
  2924. addPermListEntriesParams: addPermListEntriesParams{
  2925. {
  2926. me: state.NewIdentScreenName("me"),
  2927. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  2928. Users: []struct {
  2929. ScreenName string `oscar:"len_prefix=uint8"`
  2930. }{
  2931. {ScreenName: "friend3"},
  2932. {ScreenName: "friend4"},
  2933. },
  2934. },
  2935. },
  2936. },
  2937. },
  2938. buddyParams: buddyParams{
  2939. addBuddiesParams: addBuddiesParams{
  2940. {
  2941. me: state.NewIdentScreenName("me"),
  2942. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2943. Buddies: []struct {
  2944. ScreenName string `oscar:"len_prefix=uint8"`
  2945. }{
  2946. {ScreenName: "friend1"},
  2947. {ScreenName: "friend2"},
  2948. },
  2949. },
  2950. },
  2951. },
  2952. },
  2953. tocConfigParams: tocConfigParams{
  2954. setTOCConfigParams: setTOCConfigParams{
  2955. {
  2956. user: state.NewIdentScreenName("me"),
  2957. config: "m 3\np friend3\np friend4\n\ng Buddies\nb friend1\nb friend2",
  2958. },
  2959. },
  2960. },
  2961. },
  2962. },
  2963. {
  2964. name: "set permit some config, receive err from permit-deny svc",
  2965. me: newTestSession("me"),
  2966. givenCmd: []byte("toc_set_config {m 3\np friend3\np friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  2967. mockParams: mockParams{
  2968. permitDenyParams: permitDenyParams{
  2969. addPermListEntriesParams: addPermListEntriesParams{
  2970. {
  2971. me: state.NewIdentScreenName("me"),
  2972. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  2973. Users: []struct {
  2974. ScreenName string `oscar:"len_prefix=uint8"`
  2975. }{
  2976. {ScreenName: "friend3"},
  2977. {ScreenName: "friend4"},
  2978. },
  2979. },
  2980. err: io.EOF,
  2981. },
  2982. },
  2983. },
  2984. },
  2985. wantMsg: cmdInternalSvcErr,
  2986. },
  2987. {
  2988. name: "successfully set deny some config",
  2989. me: newTestSession("me"),
  2990. givenCmd: []byte("toc_set_config {m 4\nd friend3\nd friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  2991. mockParams: mockParams{
  2992. permitDenyParams: permitDenyParams{
  2993. addDenyListEntriesParams: addDenyListEntriesParams{
  2994. {
  2995. me: state.NewIdentScreenName("me"),
  2996. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2997. Users: []struct {
  2998. ScreenName string `oscar:"len_prefix=uint8"`
  2999. }{
  3000. {ScreenName: "friend3"},
  3001. {ScreenName: "friend4"},
  3002. },
  3003. },
  3004. },
  3005. },
  3006. },
  3007. buddyParams: buddyParams{
  3008. addBuddiesParams: addBuddiesParams{
  3009. {
  3010. me: state.NewIdentScreenName("me"),
  3011. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  3012. Buddies: []struct {
  3013. ScreenName string `oscar:"len_prefix=uint8"`
  3014. }{
  3015. {ScreenName: "friend1"},
  3016. {ScreenName: "friend2"},
  3017. },
  3018. },
  3019. },
  3020. },
  3021. },
  3022. tocConfigParams: tocConfigParams{
  3023. setTOCConfigParams: setTOCConfigParams{
  3024. {
  3025. user: state.NewIdentScreenName("me"),
  3026. config: "m 4\nd friend3\nd friend4\n\ng Buddies\nb friend1\nb friend2",
  3027. },
  3028. },
  3029. },
  3030. },
  3031. },
  3032. {
  3033. name: "set deny some config, receive err from permit-deny svc",
  3034. me: newTestSession("me"),
  3035. givenCmd: []byte("toc_set_config {m 4\nd friend3\nd friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  3036. mockParams: mockParams{
  3037. permitDenyParams: permitDenyParams{
  3038. addDenyListEntriesParams: addDenyListEntriesParams{
  3039. {
  3040. me: state.NewIdentScreenName("me"),
  3041. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  3042. Users: []struct {
  3043. ScreenName string `oscar:"len_prefix=uint8"`
  3044. }{
  3045. {ScreenName: "friend3"},
  3046. {ScreenName: "friend4"},
  3047. },
  3048. },
  3049. err: io.EOF,
  3050. },
  3051. },
  3052. },
  3053. },
  3054. wantMsg: cmdInternalSvcErr,
  3055. },
  3056. {
  3057. name: "set unknown PD mode",
  3058. me: newTestSession("me"),
  3059. givenCmd: []byte("toc_set_config {m 5\nd friend3\nd friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  3060. wantMsg: cmdInternalSvcErr,
  3061. },
  3062. {
  3063. name: "bad command",
  3064. givenCmd: []byte(`toc_chat_leave`),
  3065. wantMsg: cmdInternalSvcErr,
  3066. },
  3067. }
  3068. for _, tc := range cases {
  3069. t.Run(tc.name, func(t *testing.T) {
  3070. ctx := context.Background()
  3071. pdSvc := newMockPermitDenyService(t)
  3072. for _, params := range tc.mockParams.addDenyListEntriesParams {
  3073. pdSvc.EXPECT().
  3074. AddDenyListEntries(ctx, matchSession(params.me), params.body).
  3075. Return(params.err)
  3076. }
  3077. for _, params := range tc.mockParams.addPermListEntriesParams {
  3078. pdSvc.EXPECT().
  3079. AddPermListEntries(ctx, matchSession(params.me), params.body).
  3080. Return(params.err)
  3081. }
  3082. buddySvc := newMockBuddyService(t)
  3083. for _, params := range tc.mockParams.addBuddiesParams {
  3084. buddySvc.EXPECT().
  3085. AddBuddies(ctx, matchSession(params.me), params.inBody).
  3086. Return(params.err)
  3087. }
  3088. tocConfigSvc := newMockTOCConfigStore(t)
  3089. for _, params := range tc.mockParams.setTOCConfigParams {
  3090. tocConfigSvc.EXPECT().
  3091. SetTOCConfig(params.user, params.config).
  3092. Return(params.err)
  3093. }
  3094. svc := OSCARProxy{
  3095. BuddyService: buddySvc,
  3096. Logger: slog.Default(),
  3097. PermitDenyService: pdSvc,
  3098. TOCConfigStore: tocConfigSvc,
  3099. }
  3100. msg := svc.SetConfig(ctx, tc.me, tc.givenCmd)
  3101. assert.Equal(t, tc.wantMsg, msg)
  3102. })
  3103. }
  3104. }
  3105. func TestOSCARProxy_SetDir(t *testing.T) {
  3106. cases := []struct {
  3107. // name is the unit test name
  3108. name string
  3109. // me is the TOC user session
  3110. me *state.Session
  3111. // givenCmd is the TOC command
  3112. givenCmd []byte
  3113. // wantMsg is the expected TOC response
  3114. wantMsg string
  3115. // mockParams is the list of params sent to mocks that satisfy this
  3116. // method's dependencies
  3117. mockParams mockParams
  3118. }{
  3119. {
  3120. name: "successfully set directory info with quoted fields",
  3121. me: newTestSession("me"),
  3122. givenCmd: []byte(`toc_set_dir "first name":"middle name":"last name":"maiden name":"city":"state":"country":"email":"allow web searches"`),
  3123. mockParams: mockParams{
  3124. locateParams: locateParams{
  3125. setDirInfoParams: setDirInfoParams{
  3126. {
  3127. me: state.NewIdentScreenName("me"),
  3128. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  3129. TLVRestBlock: wire.TLVRestBlock{
  3130. TLVList: wire.TLVList{
  3131. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  3132. wire.NewTLVBE(wire.ODirTLVMiddleName, "middle name"),
  3133. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  3134. wire.NewTLVBE(wire.ODirTLVMaidenName, "maiden name"),
  3135. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  3136. wire.NewTLVBE(wire.ODirTLVState, "state"),
  3137. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  3138. },
  3139. },
  3140. },
  3141. },
  3142. },
  3143. },
  3144. },
  3145. },
  3146. {
  3147. name: "successfully set directory info with some blank fields",
  3148. me: newTestSession("me"),
  3149. givenCmd: []byte(`toc_set_dir "first name"::"last name"::"city":"state":"country":"email":"allow web searches"`),
  3150. mockParams: mockParams{
  3151. locateParams: locateParams{
  3152. setDirInfoParams: setDirInfoParams{
  3153. {
  3154. me: state.NewIdentScreenName("me"),
  3155. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  3156. TLVRestBlock: wire.TLVRestBlock{
  3157. TLVList: wire.TLVList{
  3158. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  3159. wire.NewTLVBE(wire.ODirTLVMiddleName, ""),
  3160. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  3161. wire.NewTLVBE(wire.ODirTLVMaidenName, ""),
  3162. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  3163. wire.NewTLVBE(wire.ODirTLVState, "state"),
  3164. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  3165. },
  3166. },
  3167. },
  3168. },
  3169. },
  3170. },
  3171. },
  3172. },
  3173. {
  3174. name: "successfully set directory info with last two fields absent",
  3175. me: newTestSession("me"),
  3176. givenCmd: []byte(`toc_set_dir "first name"::"last name"::"city":"state":"country"`),
  3177. mockParams: mockParams{
  3178. locateParams: locateParams{
  3179. setDirInfoParams: setDirInfoParams{
  3180. {
  3181. me: state.NewIdentScreenName("me"),
  3182. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  3183. TLVRestBlock: wire.TLVRestBlock{
  3184. TLVList: wire.TLVList{
  3185. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  3186. wire.NewTLVBE(wire.ODirTLVMiddleName, ""),
  3187. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  3188. wire.NewTLVBE(wire.ODirTLVMaidenName, ""),
  3189. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  3190. wire.NewTLVBE(wire.ODirTLVState, "state"),
  3191. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  3192. },
  3193. },
  3194. },
  3195. },
  3196. },
  3197. },
  3198. },
  3199. },
  3200. {
  3201. name: "set directory info, receive error from locate svc",
  3202. me: newTestSession("me"),
  3203. givenCmd: []byte(`toc_set_dir "first name":"middle name":"last name":"maiden name":"city":"state":"country":"email":"allow web searches"`),
  3204. mockParams: mockParams{
  3205. locateParams: locateParams{
  3206. setDirInfoParams: setDirInfoParams{
  3207. {
  3208. me: state.NewIdentScreenName("me"),
  3209. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  3210. TLVRestBlock: wire.TLVRestBlock{
  3211. TLVList: wire.TLVList{
  3212. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  3213. wire.NewTLVBE(wire.ODirTLVMiddleName, "middle name"),
  3214. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  3215. wire.NewTLVBE(wire.ODirTLVMaidenName, "maiden name"),
  3216. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  3217. wire.NewTLVBE(wire.ODirTLVState, "state"),
  3218. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  3219. },
  3220. },
  3221. },
  3222. err: io.EOF,
  3223. },
  3224. },
  3225. },
  3226. },
  3227. wantMsg: cmdInternalSvcErr,
  3228. },
  3229. {
  3230. name: "set directory with too many fields present",
  3231. me: newTestSession("me"),
  3232. givenCmd: []byte(`toc_set_dir "first name"::"last name"::"city":"state":"country":"email":"allow web searches":"extra":"extra"`),
  3233. wantMsg: cmdInternalSvcErr,
  3234. },
  3235. {
  3236. name: "bad command",
  3237. givenCmd: []byte(`toc_set_dir`),
  3238. wantMsg: cmdInternalSvcErr,
  3239. },
  3240. }
  3241. for _, tc := range cases {
  3242. t.Run(tc.name, func(t *testing.T) {
  3243. ctx := context.Background()
  3244. locateSvc := newMockLocateService(t)
  3245. for _, params := range tc.mockParams.setDirInfoParams {
  3246. locateSvc.EXPECT().
  3247. SetDirInfo(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  3248. Return(params.msg, params.err)
  3249. }
  3250. svc := OSCARProxy{
  3251. Logger: slog.Default(),
  3252. LocateService: locateSvc,
  3253. }
  3254. msg := svc.SetDir(ctx, tc.me, tc.givenCmd)
  3255. assert.Equal(t, tc.wantMsg, msg)
  3256. })
  3257. }
  3258. }
  3259. func TestOSCARProxy_SetIdle(t *testing.T) {
  3260. cases := []struct {
  3261. // name is the unit test name
  3262. name string
  3263. // me is the TOC user session
  3264. me *state.Session
  3265. // givenCmd is the TOC command
  3266. givenCmd []byte
  3267. // wantMsg is the expected TOC response
  3268. wantMsg string
  3269. // mockParams is the list of params sent to mocks that satisfy this
  3270. // method's dependencies
  3271. mockParams mockParams
  3272. }{
  3273. {
  3274. name: "successfully set idle status",
  3275. me: newTestSession("me"),
  3276. givenCmd: []byte(`toc_set_idle 10`),
  3277. mockParams: mockParams{
  3278. oServiceBOSParams: oServiceParams{
  3279. idleNotificationParams: idleNotificationParams{
  3280. {
  3281. me: state.NewIdentScreenName("me"),
  3282. bodyIn: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  3283. IdleTime: uint32(10),
  3284. },
  3285. },
  3286. },
  3287. },
  3288. },
  3289. },
  3290. {
  3291. name: "set idle status, receive err from BOS oservice svc",
  3292. me: newTestSession("me"),
  3293. givenCmd: []byte(`toc_set_idle 10`),
  3294. mockParams: mockParams{
  3295. oServiceBOSParams: oServiceParams{
  3296. idleNotificationParams: idleNotificationParams{
  3297. {
  3298. me: state.NewIdentScreenName("me"),
  3299. bodyIn: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  3300. IdleTime: uint32(10),
  3301. },
  3302. err: io.EOF,
  3303. },
  3304. },
  3305. },
  3306. },
  3307. wantMsg: cmdInternalSvcErr,
  3308. },
  3309. {
  3310. name: "bad secs param",
  3311. givenCmd: []byte(`toc_set_idle zero`),
  3312. wantMsg: cmdInternalSvcErr,
  3313. },
  3314. {
  3315. name: "bad command",
  3316. givenCmd: []byte(`toc_set_idle`),
  3317. wantMsg: cmdInternalSvcErr,
  3318. },
  3319. }
  3320. for _, tc := range cases {
  3321. t.Run(tc.name, func(t *testing.T) {
  3322. ctx := context.Background()
  3323. oServiceSvc := newMockOServiceService(t)
  3324. for _, params := range tc.mockParams.oServiceBOSParams.idleNotificationParams {
  3325. oServiceSvc.EXPECT().
  3326. IdleNotification(ctx, matchSession(params.me), params.bodyIn).
  3327. Return(params.err)
  3328. }
  3329. svc := OSCARProxy{
  3330. Logger: slog.Default(),
  3331. OServiceServiceBOS: oServiceSvc,
  3332. }
  3333. msg := svc.SetIdle(ctx, tc.me, tc.givenCmd)
  3334. assert.Equal(t, tc.wantMsg, msg)
  3335. })
  3336. }
  3337. }
  3338. func TestOSCARProxy_SetInfo(t *testing.T) {
  3339. cases := []struct {
  3340. // name is the unit test name
  3341. name string
  3342. // me is the TOC user session
  3343. me *state.Session
  3344. // givenCmd is the TOC command
  3345. givenCmd []byte
  3346. // wantMsg is the expected TOC response
  3347. wantMsg string
  3348. // mockParams is the list of params sent to mocks that satisfy this
  3349. // method's dependencies
  3350. mockParams mockParams
  3351. }{
  3352. {
  3353. name: "successfully set profile",
  3354. me: newTestSession("me"),
  3355. givenCmd: []byte(`toc_set_info "my profile!"`),
  3356. mockParams: mockParams{
  3357. locateParams: locateParams{
  3358. setInfoParams: setInfoParams{
  3359. {
  3360. me: state.NewIdentScreenName("me"),
  3361. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  3362. TLVRestBlock: wire.TLVRestBlock{
  3363. TLVList: wire.TLVList{
  3364. wire.NewTLVBE(wire.LocateTLVTagsInfoSigData, "my profile!"),
  3365. },
  3366. },
  3367. },
  3368. },
  3369. },
  3370. },
  3371. },
  3372. },
  3373. {
  3374. name: "set profile, receive error from locate svc",
  3375. me: newTestSession("me"),
  3376. givenCmd: []byte(`toc_set_info "my profile!"`),
  3377. mockParams: mockParams{
  3378. locateParams: locateParams{
  3379. setInfoParams: setInfoParams{
  3380. {
  3381. me: state.NewIdentScreenName("me"),
  3382. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  3383. TLVRestBlock: wire.TLVRestBlock{
  3384. TLVList: wire.TLVList{
  3385. wire.NewTLVBE(wire.LocateTLVTagsInfoSigData, "my profile!"),
  3386. },
  3387. },
  3388. },
  3389. err: io.EOF,
  3390. },
  3391. },
  3392. },
  3393. },
  3394. wantMsg: cmdInternalSvcErr,
  3395. },
  3396. {
  3397. name: "bad command",
  3398. givenCmd: []byte(`toc_set_info`),
  3399. wantMsg: cmdInternalSvcErr,
  3400. },
  3401. }
  3402. for _, tc := range cases {
  3403. t.Run(tc.name, func(t *testing.T) {
  3404. ctx := context.Background()
  3405. locateSvc := newMockLocateService(t)
  3406. for _, params := range tc.mockParams.setInfoParams {
  3407. locateSvc.EXPECT().
  3408. SetInfo(ctx, matchSession(params.me), params.inBody).
  3409. Return(params.err)
  3410. }
  3411. svc := OSCARProxy{
  3412. Logger: slog.Default(),
  3413. LocateService: locateSvc,
  3414. }
  3415. msg := svc.SetInfo(ctx, tc.me, tc.givenCmd)
  3416. assert.Equal(t, tc.wantMsg, msg)
  3417. })
  3418. }
  3419. }
  3420. func TestOSCARProxy_Signon(t *testing.T) {
  3421. roastedPass := wire.RoastTOCPassword([]byte("thepass"))
  3422. cases := []struct {
  3423. // name is the unit test name
  3424. name string
  3425. // me is the TOC user session
  3426. me *state.Session
  3427. // givenCmd is the TOC command
  3428. givenCmd []byte
  3429. // wantMsg is the expected TOC response
  3430. wantMsg []string
  3431. // mockParams is the list of params sent to mocks that satisfy this
  3432. // method's dependencies
  3433. mockParams mockParams
  3434. }{
  3435. {
  3436. name: "successfully login",
  3437. me: newTestSession("me", func(session *state.Session) {
  3438. session.SetCaps([][16]byte{capChat})
  3439. }),
  3440. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3441. mockParams: mockParams{
  3442. authParams: authParams{
  3443. flapLoginParams: flapLoginParams{
  3444. {
  3445. frame: wire.FLAPSignonFrame{
  3446. TLVRestBlock: wire.TLVRestBlock{
  3447. TLVList: wire.TLVList{
  3448. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3449. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3450. },
  3451. },
  3452. },
  3453. newUserFn: state.NewStubUser,
  3454. tlv: wire.TLVRestBlock{
  3455. TLVList: wire.TLVList{
  3456. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3457. },
  3458. },
  3459. },
  3460. },
  3461. registerBOSSessionParams: registerBOSSessionParams{
  3462. {
  3463. authCookie: []byte("thecookie"),
  3464. sess: newTestSession("me"),
  3465. },
  3466. },
  3467. },
  3468. buddyListRegistryParams: buddyListRegistryParams{
  3469. registerBuddyListParams: registerBuddyListParams{
  3470. {
  3471. user: state.NewIdentScreenName("me"),
  3472. },
  3473. },
  3474. },
  3475. tocConfigParams: tocConfigParams{
  3476. userParams: userParams{
  3477. {
  3478. screenName: state.NewIdentScreenName("me"),
  3479. returnedUser: &state.User{
  3480. TOCConfig: "my-toc-config",
  3481. },
  3482. },
  3483. },
  3484. },
  3485. },
  3486. wantMsg: []string{"SIGN_ON:TOC1.0", "CONFIG:my-toc-config"},
  3487. },
  3488. {
  3489. name: "login, receive error from auth svc FLAP login",
  3490. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3491. mockParams: mockParams{
  3492. authParams: authParams{
  3493. flapLoginParams: flapLoginParams{
  3494. {
  3495. frame: wire.FLAPSignonFrame{
  3496. TLVRestBlock: wire.TLVRestBlock{
  3497. TLVList: wire.TLVList{
  3498. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3499. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3500. },
  3501. },
  3502. },
  3503. newUserFn: state.NewStubUser,
  3504. err: io.EOF,
  3505. },
  3506. },
  3507. },
  3508. },
  3509. wantMsg: []string{string(cmdInternalSvcErr)},
  3510. },
  3511. {
  3512. name: "login, receive error from auth svc registration",
  3513. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3514. mockParams: mockParams{
  3515. authParams: authParams{
  3516. flapLoginParams: flapLoginParams{
  3517. {
  3518. frame: wire.FLAPSignonFrame{
  3519. TLVRestBlock: wire.TLVRestBlock{
  3520. TLVList: wire.TLVList{
  3521. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3522. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3523. },
  3524. },
  3525. },
  3526. newUserFn: state.NewStubUser,
  3527. tlv: wire.TLVRestBlock{
  3528. TLVList: wire.TLVList{
  3529. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3530. },
  3531. },
  3532. },
  3533. },
  3534. registerBOSSessionParams: registerBOSSessionParams{
  3535. {
  3536. authCookie: []byte("thecookie"),
  3537. err: io.EOF,
  3538. },
  3539. },
  3540. },
  3541. },
  3542. wantMsg: []string{string(cmdInternalSvcErr)},
  3543. },
  3544. {
  3545. name: "login, receive error from buddy list registry",
  3546. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3547. mockParams: mockParams{
  3548. authParams: authParams{
  3549. flapLoginParams: flapLoginParams{
  3550. {
  3551. frame: wire.FLAPSignonFrame{
  3552. TLVRestBlock: wire.TLVRestBlock{
  3553. TLVList: wire.TLVList{
  3554. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3555. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3556. },
  3557. },
  3558. },
  3559. newUserFn: state.NewStubUser,
  3560. tlv: wire.TLVRestBlock{
  3561. TLVList: wire.TLVList{
  3562. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3563. },
  3564. },
  3565. },
  3566. },
  3567. registerBOSSessionParams: registerBOSSessionParams{
  3568. {
  3569. authCookie: []byte("thecookie"),
  3570. sess: newTestSession("me"),
  3571. },
  3572. },
  3573. },
  3574. buddyListRegistryParams: buddyListRegistryParams{
  3575. registerBuddyListParams: registerBuddyListParams{
  3576. {
  3577. user: state.NewIdentScreenName("me"),
  3578. err: io.EOF,
  3579. },
  3580. },
  3581. },
  3582. },
  3583. wantMsg: []string{string(cmdInternalSvcErr)},
  3584. },
  3585. {
  3586. name: "login, receive error from TOC config store",
  3587. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3588. mockParams: mockParams{
  3589. authParams: authParams{
  3590. flapLoginParams: flapLoginParams{
  3591. {
  3592. frame: wire.FLAPSignonFrame{
  3593. TLVRestBlock: wire.TLVRestBlock{
  3594. TLVList: wire.TLVList{
  3595. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3596. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3597. },
  3598. },
  3599. },
  3600. newUserFn: state.NewStubUser,
  3601. tlv: wire.TLVRestBlock{
  3602. TLVList: wire.TLVList{
  3603. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3604. },
  3605. },
  3606. },
  3607. },
  3608. registerBOSSessionParams: registerBOSSessionParams{
  3609. {
  3610. authCookie: []byte("thecookie"),
  3611. sess: newTestSession("me"),
  3612. },
  3613. },
  3614. },
  3615. buddyListRegistryParams: buddyListRegistryParams{
  3616. registerBuddyListParams: registerBuddyListParams{
  3617. {
  3618. user: state.NewIdentScreenName("me"),
  3619. },
  3620. },
  3621. },
  3622. tocConfigParams: tocConfigParams{
  3623. userParams: userParams{
  3624. {
  3625. screenName: state.NewIdentScreenName("me"),
  3626. err: io.EOF,
  3627. },
  3628. },
  3629. },
  3630. },
  3631. wantMsg: []string{string(cmdInternalSvcErr)},
  3632. },
  3633. {
  3634. name: "login, user not found after login",
  3635. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3636. mockParams: mockParams{
  3637. authParams: authParams{
  3638. flapLoginParams: flapLoginParams{
  3639. {
  3640. frame: wire.FLAPSignonFrame{
  3641. TLVRestBlock: wire.TLVRestBlock{
  3642. TLVList: wire.TLVList{
  3643. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3644. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3645. },
  3646. },
  3647. },
  3648. newUserFn: state.NewStubUser,
  3649. tlv: wire.TLVRestBlock{
  3650. TLVList: wire.TLVList{
  3651. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  3652. },
  3653. },
  3654. },
  3655. },
  3656. registerBOSSessionParams: registerBOSSessionParams{
  3657. {
  3658. authCookie: []byte("thecookie"),
  3659. sess: newTestSession("me"),
  3660. },
  3661. },
  3662. },
  3663. buddyListRegistryParams: buddyListRegistryParams{
  3664. registerBuddyListParams: registerBuddyListParams{
  3665. {
  3666. user: state.NewIdentScreenName("me"),
  3667. },
  3668. },
  3669. },
  3670. tocConfigParams: tocConfigParams{
  3671. userParams: userParams{
  3672. {
  3673. screenName: state.NewIdentScreenName("me"),
  3674. returnedUser: nil,
  3675. },
  3676. },
  3677. },
  3678. },
  3679. wantMsg: []string{string(cmdInternalSvcErr)},
  3680. },
  3681. {
  3682. name: "login with bad credentials",
  3683. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  3684. mockParams: mockParams{
  3685. authParams: authParams{
  3686. flapLoginParams: flapLoginParams{
  3687. {
  3688. frame: wire.FLAPSignonFrame{
  3689. TLVRestBlock: wire.TLVRestBlock{
  3690. TLVList: wire.TLVList{
  3691. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3692. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3693. },
  3694. },
  3695. },
  3696. newUserFn: state.NewStubUser,
  3697. tlv: wire.TLVRestBlock{
  3698. TLVList: wire.TLVList{
  3699. wire.NewTLVBE(wire.LoginTLVTagsErrorSubcode, wire.LoginErrInvalidUsernameOrPassword),
  3700. },
  3701. },
  3702. },
  3703. },
  3704. },
  3705. },
  3706. wantMsg: []string{"ERROR:980"},
  3707. },
  3708. {
  3709. name: "bad command",
  3710. givenCmd: []byte(`toc_init_done_diff`),
  3711. wantMsg: []string{string(cmdInternalSvcErr)},
  3712. },
  3713. }
  3714. for _, tc := range cases {
  3715. t.Run(tc.name, func(t *testing.T) {
  3716. ctx := context.Background()
  3717. authSvc := newMockAuthService(t)
  3718. for _, params := range tc.mockParams.flapLoginParams {
  3719. authSvc.EXPECT().
  3720. FLAPLogin(params.frame, mock.Anything).
  3721. Return(params.tlv, params.err)
  3722. }
  3723. for _, params := range tc.mockParams.registerBOSSessionParams {
  3724. authSvc.EXPECT().
  3725. RegisterBOSSession(ctx, params.authCookie).
  3726. Return(params.sess, params.err)
  3727. }
  3728. buddyRegistry := newMockBuddyListRegistry(t)
  3729. for _, params := range tc.mockParams.registerBuddyListParams {
  3730. buddyRegistry.EXPECT().
  3731. RegisterBuddyList(params.user).
  3732. Return(params.err)
  3733. }
  3734. tocCfg := newMockTOCConfigStore(t)
  3735. for _, params := range tc.mockParams.userParams {
  3736. tocCfg.EXPECT().
  3737. User(params.screenName).
  3738. Return(params.returnedUser, params.err)
  3739. }
  3740. svc := OSCARProxy{
  3741. AuthService: authSvc,
  3742. BuddyListRegistry: buddyRegistry,
  3743. Logger: slog.Default(),
  3744. TOCConfigStore: tocCfg,
  3745. }
  3746. sess, msg := svc.Signon(ctx, tc.givenCmd)
  3747. assert.Equal(t, tc.wantMsg, msg)
  3748. if tc.me == nil {
  3749. assert.Nil(t, sess)
  3750. } else if assert.NotNil(t, sess) {
  3751. assert.Equal(t, tc.me.IdentScreenName(), sess.IdentScreenName())
  3752. assert.Equal(t, tc.me.Caps(), sess.Caps())
  3753. }
  3754. })
  3755. }
  3756. }
  3757. func TestOSCARProxy_Signout(t *testing.T) {
  3758. cases := []struct {
  3759. // name is the unit test name
  3760. name string
  3761. // me is the TOC user session
  3762. me *state.Session
  3763. // mockParams is the list of params sent to mocks that satisfy this
  3764. // method's dependencies
  3765. mockParams mockParams
  3766. }{
  3767. {
  3768. name: "successfully sign out",
  3769. me: newTestSession("me"),
  3770. mockParams: mockParams{
  3771. buddyParams: buddyParams{
  3772. broadcastBuddyDepartedParams: broadcastBuddyDepartedParams{
  3773. {
  3774. me: state.NewIdentScreenName("me"),
  3775. },
  3776. },
  3777. },
  3778. buddyListRegistryParams: buddyListRegistryParams{
  3779. unregisterBuddyListParams: unregisterBuddyListParams{
  3780. {
  3781. user: state.NewIdentScreenName("me"),
  3782. },
  3783. },
  3784. },
  3785. authParams: authParams{
  3786. signoutParams: signoutParams{
  3787. {
  3788. me: state.NewIdentScreenName("me"),
  3789. },
  3790. },
  3791. },
  3792. },
  3793. },
  3794. {
  3795. name: "sign out, receive error from buddy service",
  3796. me: newTestSession("me"),
  3797. mockParams: mockParams{
  3798. buddyParams: buddyParams{
  3799. broadcastBuddyDepartedParams: broadcastBuddyDepartedParams{
  3800. {
  3801. me: state.NewIdentScreenName("me"),
  3802. err: io.EOF,
  3803. },
  3804. },
  3805. },
  3806. buddyListRegistryParams: buddyListRegistryParams{
  3807. unregisterBuddyListParams: unregisterBuddyListParams{
  3808. {
  3809. user: state.NewIdentScreenName("me"),
  3810. },
  3811. },
  3812. },
  3813. authParams: authParams{
  3814. signoutParams: signoutParams{
  3815. {
  3816. me: state.NewIdentScreenName("me"),
  3817. },
  3818. },
  3819. },
  3820. },
  3821. },
  3822. {
  3823. name: "sign out, receive error from buddy list registry",
  3824. me: newTestSession("me"),
  3825. mockParams: mockParams{
  3826. buddyParams: buddyParams{
  3827. broadcastBuddyDepartedParams: broadcastBuddyDepartedParams{
  3828. {
  3829. me: state.NewIdentScreenName("me"),
  3830. },
  3831. },
  3832. },
  3833. buddyListRegistryParams: buddyListRegistryParams{
  3834. unregisterBuddyListParams: unregisterBuddyListParams{
  3835. {
  3836. user: state.NewIdentScreenName("me"),
  3837. err: io.EOF,
  3838. },
  3839. },
  3840. },
  3841. authParams: authParams{
  3842. signoutParams: signoutParams{
  3843. {
  3844. me: state.NewIdentScreenName("me"),
  3845. },
  3846. },
  3847. },
  3848. },
  3849. },
  3850. }
  3851. for _, tc := range cases {
  3852. t.Run(tc.name, func(t *testing.T) {
  3853. ctx := context.Background()
  3854. buddySvc := newMockBuddyService(t)
  3855. for _, params := range tc.mockParams.broadcastBuddyDepartedParams {
  3856. buddySvc.EXPECT().
  3857. BroadcastBuddyDeparted(ctx, matchSession(params.me)).
  3858. Return(params.err)
  3859. }
  3860. buddyListSvc := newMockBuddyListRegistry(t)
  3861. for _, params := range tc.mockParams.unregisterBuddyListParams {
  3862. buddyListSvc.EXPECT().
  3863. UnregisterBuddyList(params.user).
  3864. Return(params.err)
  3865. }
  3866. authSvc := newMockAuthService(t)
  3867. for _, params := range tc.mockParams.signoutParams {
  3868. authSvc.EXPECT().Signout(ctx, matchSession(params.me))
  3869. }
  3870. svc := OSCARProxy{
  3871. AuthService: authSvc,
  3872. BuddyListRegistry: buddyListSvc,
  3873. BuddyService: buddySvc,
  3874. Logger: slog.Default(),
  3875. }
  3876. svc.Signout(ctx, tc.me)
  3877. })
  3878. }
  3879. }
  3880. func Test_parseArgs(t *testing.T) {
  3881. type testCase struct {
  3882. name string
  3883. givenPayload string
  3884. givenCmd string
  3885. givenArgs []*string
  3886. wantVarArgs []string
  3887. wantArgs []string
  3888. wantErrMsg string
  3889. }
  3890. tests := []testCase{
  3891. {
  3892. name: "no positional args or varargs",
  3893. givenPayload: `toc_chat_invite`,
  3894. givenCmd: "toc_chat_invite",
  3895. givenArgs: nil,
  3896. wantVarArgs: []string{},
  3897. },
  3898. {
  3899. name: "positional args with varargs",
  3900. givenPayload: `toc_chat_invite 1234 "Join me!" user1 user2 user3`,
  3901. givenCmd: "toc_chat_invite",
  3902. givenArgs: []*string{new(string), new(string)},
  3903. wantVarArgs: []string{"user1", "user2", "user3"},
  3904. wantArgs: []string{"1234", "Join me!"},
  3905. },
  3906. {
  3907. name: "nil positional argument placeholders should get skipped",
  3908. givenPayload: `toc_chat_invite 1234 "Join me!" user1 user2 user3`,
  3909. givenCmd: "toc_chat_invite",
  3910. givenArgs: []*string{nil, nil}, // still 2 placeholders, both nil
  3911. wantVarArgs: []string{"user1", "user2", "user3"},
  3912. wantArgs: []string{"", ""},
  3913. },
  3914. {
  3915. name: "positional args with no varargs",
  3916. givenPayload: `toc_chat_invite 1234 "Join me!"`,
  3917. givenCmd: "toc_chat_invite",
  3918. givenArgs: []*string{new(string), new(string)}, // roomID + msg
  3919. wantVarArgs: []string{},
  3920. wantArgs: []string{"1234", "Join me!"},
  3921. },
  3922. {
  3923. name: "varargs only",
  3924. givenPayload: `toc_chat_invite user1 user2 user3`,
  3925. givenCmd: "toc_chat_invite",
  3926. givenArgs: nil,
  3927. wantVarArgs: []string{"user1", "user2", "user3"},
  3928. },
  3929. {
  3930. name: "command mismatch",
  3931. givenPayload: `toc_chat_invite user1 user2 user3`,
  3932. givenCmd: "toc_chat_accept",
  3933. givenArgs: nil,
  3934. wantVarArgs: []string{},
  3935. wantErrMsg: "mismatch",
  3936. },
  3937. {
  3938. name: "too many positional arg placeholders",
  3939. givenPayload: `toc_chat_invite`,
  3940. givenCmd: "toc_chat_invite",
  3941. givenArgs: []*string{new(string), new(string)},
  3942. wantVarArgs: []string{},
  3943. wantErrMsg: "command contains fewer arguments than expected",
  3944. },
  3945. {
  3946. name: "CSV parser error",
  3947. givenPayload: ``,
  3948. givenCmd: "toc_chat_invite",
  3949. givenArgs: []*string{nil},
  3950. wantVarArgs: []string{},
  3951. wantErrMsg: "CSV reader error",
  3952. },
  3953. }
  3954. for _, tt := range tests {
  3955. t.Run(tt.name, func(t *testing.T) {
  3956. varArgs, err := parseArgs([]byte(tt.givenPayload), tt.givenCmd, tt.givenArgs...)
  3957. if tt.wantErrMsg != "" {
  3958. assert.ErrorContains(t, err, tt.wantErrMsg)
  3959. return
  3960. }
  3961. assert.NoError(t, err)
  3962. // verify the placeholder pointers got populated
  3963. for i, want := range tt.wantArgs {
  3964. if want == "" {
  3965. assert.Nil(t, tt.givenArgs[i])
  3966. } else {
  3967. got := *tt.givenArgs[i]
  3968. assert.Equal(t, want, got)
  3969. }
  3970. }
  3971. // verify we have the same varargs
  3972. assert.Equal(t, tt.wantVarArgs, varArgs)
  3973. assert.Equal(t, len(tt.wantArgs), len(tt.givenArgs))
  3974. })
  3975. }
  3976. }