cmd_client_test.go 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425
  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_ChatAccept(t *testing.T) {
  267. fnNewChatNavParams := func(err error) chatNavParams {
  268. ret := chatNavParams{
  269. requestRoomInfoParams: requestRoomInfoParams{
  270. {
  271. inBody: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  272. Cookie: "the-cookie",
  273. Exchange: 4,
  274. InstanceNumber: 0,
  275. },
  276. },
  277. },
  278. }
  279. if err != nil {
  280. ret.requestRoomInfoParams[0].err = err
  281. } else {
  282. ret.requestRoomInfoParams[0].msg = wire.SNACMessage{
  283. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  284. TLVRestBlock: wire.TLVRestBlock{
  285. TLVList: wire.TLVList{
  286. wire.NewTLVBE(wire.ChatNavTLVRoomInfo, wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  287. Cookie: "the-cookie",
  288. TLVBlock: wire.TLVBlock{
  289. TLVList: wire.TLVList{
  290. wire.NewTLVBE(wire.ChatRoomTLVRoomName, "cool room"),
  291. },
  292. },
  293. }),
  294. },
  295. },
  296. },
  297. }
  298. }
  299. return ret
  300. }
  301. fnNewOServiceBOSParams := func(err error) oServiceParams {
  302. ret := oServiceParams{
  303. serviceRequestParams: serviceRequestParams{
  304. {
  305. me: state.NewIdentScreenName("me"),
  306. bodyIn: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  307. FoodGroup: wire.Chat,
  308. TLVRestBlock: wire.TLVRestBlock{
  309. TLVList: wire.TLVList{
  310. wire.NewTLVBE(0x01, wire.SNAC_0x01_0x04_TLVRoomInfo{
  311. Cookie: "the-cookie",
  312. }),
  313. },
  314. },
  315. },
  316. },
  317. },
  318. }
  319. if err != nil {
  320. ret.serviceRequestParams[0].err = err
  321. } else {
  322. ret.serviceRequestParams[0].msg = wire.SNACMessage{
  323. Body: wire.SNAC_0x01_0x05_OServiceServiceResponse{
  324. TLVRestBlock: wire.TLVRestBlock{
  325. TLVList: wire.TLVList{
  326. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, "chat-auth-cookie"),
  327. },
  328. },
  329. },
  330. }
  331. }
  332. return ret
  333. }
  334. fnNewAuthParams := func(err error) authParams {
  335. ret := authParams{
  336. registerChatSessionParams: registerChatSessionParams{
  337. {
  338. authCookie: []byte("chat-auth-cookie"),
  339. },
  340. },
  341. }
  342. if err != nil {
  343. ret.registerChatSessionParams[0].err = err
  344. } else {
  345. ret.registerChatSessionParams[0].sess = newTestSession("me-chat")
  346. }
  347. return ret
  348. }
  349. fnNewOServiceChatParams := func(err error) oServiceParams {
  350. ret := oServiceParams{
  351. clientOnlineParams: clientOnlineParams{
  352. {
  353. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  354. me: state.NewIdentScreenName("me-chat"),
  355. },
  356. },
  357. }
  358. if err != nil {
  359. ret.clientOnlineParams[0].err = err
  360. }
  361. return ret
  362. }
  363. cases := []struct {
  364. // name is the unit test name
  365. name string
  366. // me is the TOC user session
  367. me *state.Session
  368. // givenCmd is the TOC command
  369. givenCmd []byte
  370. // givenChatRegistry is the chat registry passed to the function
  371. givenChatRegistry *ChatRegistry
  372. // wantMsg is the expected TOC response
  373. wantMsg string
  374. // wantChatID is the expected chat ID
  375. wantChatID int
  376. // mockParams is the list of params sent to mocks that satisfy this
  377. // method's dependencies
  378. mockParams mockParams
  379. }{
  380. {
  381. name: "successfully accept chat",
  382. me: newTestSession("me"),
  383. givenCmd: []byte(`toc_chat_accept 0`),
  384. givenChatRegistry: func() *ChatRegistry {
  385. reg := NewChatRegistry()
  386. reg.Add(wire.ICBMRoomInfo{
  387. Cookie: "the-cookie",
  388. Exchange: 4,
  389. Instance: 0,
  390. })
  391. return reg
  392. }(),
  393. mockParams: mockParams{
  394. chatNavParams: fnNewChatNavParams(nil),
  395. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  396. authParams: fnNewAuthParams(nil),
  397. oServiceChatParams: fnNewOServiceChatParams(nil),
  398. },
  399. wantMsg: "CHAT_JOIN:0:cool room",
  400. },
  401. {
  402. name: "accept chat, receive error from chat oservice svc",
  403. me: newTestSession("me"),
  404. givenCmd: []byte(`toc_chat_accept 0`),
  405. givenChatRegistry: func() *ChatRegistry {
  406. reg := NewChatRegistry()
  407. reg.Add(wire.ICBMRoomInfo{
  408. Cookie: "the-cookie",
  409. Exchange: 4,
  410. Instance: 0,
  411. })
  412. return reg
  413. }(),
  414. mockParams: mockParams{
  415. chatNavParams: fnNewChatNavParams(nil),
  416. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  417. authParams: fnNewAuthParams(nil),
  418. oServiceChatParams: fnNewOServiceChatParams(io.EOF),
  419. },
  420. wantMsg: cmdInternalSvcErr,
  421. },
  422. {
  423. name: "accept chat, receive error from auth svc",
  424. me: newTestSession("me"),
  425. givenCmd: []byte(`toc_chat_accept 0`),
  426. givenChatRegistry: func() *ChatRegistry {
  427. reg := NewChatRegistry()
  428. reg.Add(wire.ICBMRoomInfo{
  429. Cookie: "the-cookie",
  430. Exchange: 4,
  431. Instance: 0,
  432. })
  433. return reg
  434. }(),
  435. mockParams: mockParams{
  436. chatNavParams: fnNewChatNavParams(nil),
  437. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  438. authParams: fnNewAuthParams(io.EOF),
  439. },
  440. wantMsg: cmdInternalSvcErr,
  441. },
  442. {
  443. name: "accept chat, receive error from BOS oservice svc",
  444. me: newTestSession("me"),
  445. givenCmd: []byte(`toc_chat_accept 0`),
  446. givenChatRegistry: func() *ChatRegistry {
  447. reg := NewChatRegistry()
  448. reg.Add(wire.ICBMRoomInfo{
  449. Cookie: "the-cookie",
  450. Exchange: 4,
  451. Instance: 0,
  452. })
  453. return reg
  454. }(),
  455. mockParams: mockParams{
  456. chatNavParams: fnNewChatNavParams(nil),
  457. oServiceBOSParams: fnNewOServiceBOSParams(io.EOF),
  458. },
  459. wantMsg: cmdInternalSvcErr,
  460. },
  461. {
  462. name: "accept chat, receive error from chat nav svc",
  463. me: newTestSession("me"),
  464. givenCmd: []byte(`toc_chat_accept 0`),
  465. givenChatRegistry: func() *ChatRegistry {
  466. reg := NewChatRegistry()
  467. reg.Add(wire.ICBMRoomInfo{
  468. Cookie: "the-cookie",
  469. Exchange: 4,
  470. Instance: 0,
  471. })
  472. return reg
  473. }(),
  474. mockParams: mockParams{
  475. chatNavParams: fnNewChatNavParams(io.EOF),
  476. },
  477. wantMsg: cmdInternalSvcErr,
  478. },
  479. {
  480. name: "chat doesn't exist",
  481. givenCmd: []byte(`toc_chat_accept 0`),
  482. givenChatRegistry: NewChatRegistry(),
  483. wantMsg: cmdInternalSvcErr,
  484. },
  485. {
  486. name: "bad command",
  487. givenCmd: []byte(`toc_chat_accept`),
  488. wantMsg: cmdInternalSvcErr,
  489. },
  490. {
  491. name: "bad exchange number",
  492. givenCmd: []byte(`toc_chat_accept four`),
  493. wantMsg: cmdInternalSvcErr,
  494. },
  495. }
  496. for _, tc := range cases {
  497. t.Run(tc.name, func(t *testing.T) {
  498. ctx := context.Background()
  499. chatNavSvc := newMockChatNavService(t)
  500. for _, params := range tc.mockParams.requestRoomInfoParams {
  501. chatNavSvc.EXPECT().
  502. RequestRoomInfo(ctx, wire.SNACFrame{}, params.inBody).
  503. Return(params.msg, params.err)
  504. }
  505. bosOServiceSvc := newMockOServiceService(t)
  506. for _, params := range tc.mockParams.oServiceBOSParams.serviceRequestParams {
  507. bosOServiceSvc.EXPECT().
  508. ServiceRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.bodyIn).
  509. Return(params.msg, params.err)
  510. }
  511. chatOServiceSvc := newMockOServiceService(t)
  512. for _, params := range tc.mockParams.oServiceChatParams.clientOnlineParams {
  513. chatOServiceSvc.EXPECT().
  514. ClientOnline(ctx, params.body, matchSession(params.me)).
  515. Return(params.err)
  516. }
  517. authSvc := newMockAuthService(t)
  518. for _, params := range tc.mockParams.authParams.registerChatSessionParams {
  519. authSvc.EXPECT().
  520. RegisterChatSession(ctx, params.authCookie).
  521. Return(params.sess, params.err)
  522. }
  523. svc := OSCARProxy{
  524. AuthService: authSvc,
  525. ChatNavService: chatNavSvc,
  526. Logger: slog.Default(),
  527. OServiceServiceBOS: bosOServiceSvc,
  528. OServiceServiceChat: chatOServiceSvc,
  529. }
  530. chatID, msg := svc.ChatAccept(ctx, tc.me, tc.givenChatRegistry, tc.givenCmd)
  531. assert.Equal(t, tc.wantMsg, msg)
  532. assert.Equal(t, tc.wantChatID, chatID)
  533. })
  534. }
  535. }
  536. func TestOSCARProxy_ChatInvite(t *testing.T) {
  537. cases := []struct {
  538. // name is the unit test name
  539. name string
  540. // me is the TOC user session
  541. me *state.Session
  542. // givenCmd is the TOC command
  543. givenCmd []byte
  544. // givenChatRegistry is the chat registry passed to the function
  545. givenChatRegistry *ChatRegistry
  546. // wantMsg is the expected TOC response
  547. wantMsg string
  548. // mockParams is the list of params sent to mocks that satisfy this
  549. // method's dependencies
  550. mockParams mockParams
  551. }{
  552. {
  553. name: "successfully send chat invitation",
  554. me: newTestSession("me"),
  555. givenCmd: []byte(`toc_chat_invite 0 "join my chat!" friend1`),
  556. givenChatRegistry: func() *ChatRegistry {
  557. reg := NewChatRegistry()
  558. reg.Add(wire.ICBMRoomInfo{
  559. Exchange: 4,
  560. Cookie: "the-cookie",
  561. Instance: 0,
  562. })
  563. return reg
  564. }(),
  565. mockParams: mockParams{
  566. icbmParams: icbmParams{
  567. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  568. {
  569. sender: state.NewIdentScreenName("me"),
  570. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  571. ChannelID: wire.ICBMChannelRendezvous,
  572. ScreenName: "friend1",
  573. TLVRestBlock: wire.TLVRestBlock{
  574. TLVList: wire.TLVList{
  575. wire.NewTLVBE(0x05, wire.ICBMCh2Fragment{
  576. Type: 0,
  577. Capability: capChat,
  578. TLVRestBlock: wire.TLVRestBlock{
  579. TLVList: wire.TLVList{
  580. wire.NewTLVBE(10, uint16(1)),
  581. wire.NewTLVBE(12, "join my chat!"),
  582. wire.NewTLVBE(13, "us-ascii"),
  583. wire.NewTLVBE(14, "en"),
  584. wire.NewTLVBE(10001, wire.ICBMRoomInfo{
  585. Exchange: 4,
  586. Cookie: "the-cookie",
  587. Instance: 0,
  588. }),
  589. },
  590. },
  591. }),
  592. },
  593. },
  594. },
  595. },
  596. },
  597. },
  598. },
  599. },
  600. {
  601. name: "send chat invitation, receive error from ICBM svc",
  602. me: newTestSession("me"),
  603. givenCmd: []byte(`toc_chat_invite 0 "join my chat!" friend1`),
  604. givenChatRegistry: func() *ChatRegistry {
  605. reg := NewChatRegistry()
  606. reg.Add(wire.ICBMRoomInfo{
  607. Exchange: 4,
  608. Cookie: "the-cookie",
  609. Instance: 0,
  610. })
  611. return reg
  612. }(),
  613. mockParams: mockParams{
  614. icbmParams: icbmParams{
  615. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  616. {
  617. sender: state.NewIdentScreenName("me"),
  618. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  619. ChannelID: wire.ICBMChannelRendezvous,
  620. ScreenName: "friend1",
  621. TLVRestBlock: wire.TLVRestBlock{
  622. TLVList: wire.TLVList{
  623. wire.NewTLVBE(0x05, wire.ICBMCh2Fragment{
  624. Type: 0,
  625. Capability: capChat,
  626. TLVRestBlock: wire.TLVRestBlock{
  627. TLVList: wire.TLVList{
  628. wire.NewTLVBE(10, uint16(1)),
  629. wire.NewTLVBE(12, "join my chat!"),
  630. wire.NewTLVBE(13, "us-ascii"),
  631. wire.NewTLVBE(14, "en"),
  632. wire.NewTLVBE(10001, wire.ICBMRoomInfo{
  633. Exchange: 4,
  634. Cookie: "the-cookie",
  635. Instance: 0,
  636. }),
  637. },
  638. },
  639. }),
  640. },
  641. },
  642. },
  643. err: io.EOF,
  644. },
  645. },
  646. },
  647. },
  648. wantMsg: cmdInternalSvcErr,
  649. },
  650. {
  651. name: "send chat invitation to non-existent room",
  652. me: newTestSession("me"),
  653. givenCmd: []byte(`toc_chat_invite 0 "join my chat!" friend1`),
  654. givenChatRegistry: NewChatRegistry(),
  655. wantMsg: cmdInternalSvcErr,
  656. },
  657. {
  658. name: "bad chat room ID",
  659. givenCmd: []byte(`toc_chat_invite zero "join my chat!" friend1`),
  660. wantMsg: cmdInternalSvcErr,
  661. },
  662. {
  663. name: "bad command",
  664. givenCmd: []byte(`toc_chat_invite`),
  665. wantMsg: cmdInternalSvcErr,
  666. },
  667. }
  668. for _, tc := range cases {
  669. t.Run(tc.name, func(t *testing.T) {
  670. ctx := context.Background()
  671. icbmSvc := newMockICBMService(t)
  672. for _, params := range tc.mockParams.channelMsgToHostParamsICBM {
  673. icbmSvc.EXPECT().
  674. ChannelMsgToHost(ctx, matchSession(params.sender), wire.SNACFrame{}, params.inBody).
  675. Return(nil, params.err)
  676. }
  677. svc := OSCARProxy{
  678. Logger: slog.Default(),
  679. ICBMService: icbmSvc,
  680. }
  681. msg := svc.ChatInvite(ctx, tc.me, tc.givenChatRegistry, tc.givenCmd)
  682. assert.Equal(t, tc.wantMsg, msg)
  683. })
  684. }
  685. }
  686. func TestOSCARProxy_ChatJoin(t *testing.T) {
  687. fnNewChatNavParams := func(err error) chatNavParams {
  688. ret := chatNavParams{
  689. createRoomParams: createRoomParams{
  690. {
  691. me: state.NewIdentScreenName("me"),
  692. inBody: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  693. Exchange: 4,
  694. Cookie: "create",
  695. TLVBlock: wire.TLVBlock{
  696. TLVList: wire.TLVList{
  697. wire.NewTLVBE(wire.ChatRoomTLVRoomName, "cool room"),
  698. },
  699. },
  700. },
  701. },
  702. },
  703. }
  704. if err != nil {
  705. ret.createRoomParams[0].err = err
  706. } else {
  707. ret.createRoomParams[0].msg = wire.SNACMessage{
  708. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{
  709. TLVRestBlock: wire.TLVRestBlock{
  710. TLVList: wire.TLVList{
  711. wire.NewTLVBE(wire.ChatNavTLVRoomInfo, wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  712. Cookie: "the-cookie",
  713. }),
  714. },
  715. },
  716. },
  717. }
  718. }
  719. return ret
  720. }
  721. fnNewOServiceBOSParams := func(err error) oServiceParams {
  722. ret := oServiceParams{
  723. serviceRequestParams: serviceRequestParams{
  724. {
  725. me: state.NewIdentScreenName("me"),
  726. bodyIn: wire.SNAC_0x01_0x04_OServiceServiceRequest{
  727. FoodGroup: wire.Chat,
  728. TLVRestBlock: wire.TLVRestBlock{
  729. TLVList: wire.TLVList{
  730. wire.NewTLVBE(0x01, wire.SNAC_0x01_0x04_TLVRoomInfo{
  731. Cookie: "the-cookie",
  732. }),
  733. },
  734. },
  735. },
  736. },
  737. },
  738. }
  739. if err != nil {
  740. ret.serviceRequestParams[0].err = err
  741. } else {
  742. ret.serviceRequestParams[0].msg = wire.SNACMessage{
  743. Body: wire.SNAC_0x01_0x05_OServiceServiceResponse{
  744. TLVRestBlock: wire.TLVRestBlock{
  745. TLVList: wire.TLVList{
  746. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, "chat-auth-cookie"),
  747. },
  748. },
  749. },
  750. }
  751. }
  752. return ret
  753. }
  754. fnNewAuthParams := func(err error) authParams {
  755. ret := authParams{
  756. registerChatSessionParams: registerChatSessionParams{
  757. {
  758. authCookie: []byte("chat-auth-cookie"),
  759. },
  760. },
  761. }
  762. if err != nil {
  763. ret.registerChatSessionParams[0].err = err
  764. } else {
  765. ret.registerChatSessionParams[0].sess = newTestSession("me-chat")
  766. }
  767. return ret
  768. }
  769. fnNewOServiceChatParams := func(err error) oServiceParams {
  770. ret := oServiceParams{
  771. clientOnlineParams: clientOnlineParams{
  772. {
  773. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  774. me: state.NewIdentScreenName("me-chat"),
  775. },
  776. },
  777. }
  778. if err != nil {
  779. ret.clientOnlineParams[0].err = err
  780. }
  781. return ret
  782. }
  783. cases := []struct {
  784. // name is the unit test name
  785. name string
  786. // me is the TOC user session
  787. me *state.Session
  788. // givenCmd is the TOC command
  789. givenCmd []byte
  790. // givenChatRegistry is the chat registry passed to the function
  791. givenChatRegistry *ChatRegistry
  792. // wantMsg is the expected TOC response
  793. wantMsg string
  794. // wantChatID is the expected chat ID
  795. wantChatID int
  796. // mockParams is the list of params sent to mocks that satisfy this
  797. // method's dependencies
  798. mockParams mockParams
  799. }{
  800. {
  801. name: "successfully join chat",
  802. me: newTestSession("me"),
  803. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  804. givenChatRegistry: NewChatRegistry(),
  805. mockParams: mockParams{
  806. chatNavParams: fnNewChatNavParams(nil),
  807. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  808. authParams: fnNewAuthParams(nil),
  809. oServiceChatParams: fnNewOServiceChatParams(nil),
  810. },
  811. wantMsg: "CHAT_JOIN:0:cool room",
  812. },
  813. {
  814. name: "join chat, receive error from chat oservice svc",
  815. me: newTestSession("me"),
  816. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  817. givenChatRegistry: NewChatRegistry(),
  818. mockParams: mockParams{
  819. chatNavParams: fnNewChatNavParams(nil),
  820. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  821. authParams: fnNewAuthParams(nil),
  822. oServiceChatParams: fnNewOServiceChatParams(io.EOF),
  823. },
  824. wantMsg: cmdInternalSvcErr,
  825. },
  826. {
  827. name: "join chat, receive error from auth svc",
  828. me: newTestSession("me"),
  829. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  830. givenChatRegistry: NewChatRegistry(),
  831. mockParams: mockParams{
  832. chatNavParams: fnNewChatNavParams(nil),
  833. oServiceBOSParams: fnNewOServiceBOSParams(nil),
  834. authParams: fnNewAuthParams(io.EOF),
  835. },
  836. wantMsg: cmdInternalSvcErr,
  837. },
  838. {
  839. name: "join chat, receive error from BOS oservice svc",
  840. me: newTestSession("me"),
  841. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  842. givenChatRegistry: NewChatRegistry(),
  843. mockParams: mockParams{
  844. chatNavParams: fnNewChatNavParams(nil),
  845. oServiceBOSParams: fnNewOServiceBOSParams(io.EOF),
  846. },
  847. wantMsg: cmdInternalSvcErr,
  848. },
  849. {
  850. name: "join chat, receive error from chat nav svc",
  851. me: newTestSession("me"),
  852. givenCmd: []byte(`toc_chat_join 4 "cool room"`),
  853. givenChatRegistry: NewChatRegistry(),
  854. mockParams: mockParams{
  855. chatNavParams: fnNewChatNavParams(io.EOF),
  856. },
  857. wantMsg: cmdInternalSvcErr,
  858. },
  859. {
  860. name: "bad command",
  861. givenCmd: []byte(`toc_chat_join`),
  862. wantMsg: cmdInternalSvcErr,
  863. },
  864. {
  865. name: "bad exchange number",
  866. givenCmd: []byte(`toc_chat_join four "cool room"`),
  867. wantMsg: cmdInternalSvcErr,
  868. },
  869. }
  870. for _, tc := range cases {
  871. t.Run(tc.name, func(t *testing.T) {
  872. ctx := context.Background()
  873. chatNavSvc := newMockChatNavService(t)
  874. for _, params := range tc.mockParams.createRoomParams {
  875. chatNavSvc.EXPECT().
  876. CreateRoom(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  877. Return(params.msg, params.err)
  878. }
  879. bosOServiceSvc := newMockOServiceService(t)
  880. for _, params := range tc.mockParams.oServiceBOSParams.serviceRequestParams {
  881. bosOServiceSvc.EXPECT().
  882. ServiceRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.bodyIn).
  883. Return(params.msg, params.err)
  884. }
  885. chatOServiceSvc := newMockOServiceService(t)
  886. for _, params := range tc.mockParams.oServiceChatParams.clientOnlineParams {
  887. chatOServiceSvc.EXPECT().
  888. ClientOnline(ctx, params.body, matchSession(params.me)).
  889. Return(params.err)
  890. }
  891. authSvc := newMockAuthService(t)
  892. for _, params := range tc.mockParams.authParams.registerChatSessionParams {
  893. authSvc.EXPECT().
  894. RegisterChatSession(ctx, params.authCookie).
  895. Return(params.sess, params.err)
  896. }
  897. svc := OSCARProxy{
  898. AuthService: authSvc,
  899. ChatNavService: chatNavSvc,
  900. Logger: slog.Default(),
  901. OServiceServiceBOS: bosOServiceSvc,
  902. OServiceServiceChat: chatOServiceSvc,
  903. }
  904. chatID, msg := svc.ChatJoin(ctx, tc.me, tc.givenChatRegistry, tc.givenCmd)
  905. assert.Equal(t, tc.wantMsg, msg)
  906. assert.Equal(t, tc.wantChatID, chatID)
  907. })
  908. }
  909. }
  910. func TestOSCARProxy_ChatLeave(t *testing.T) {
  911. cases := []struct {
  912. // name is the unit test name
  913. name string
  914. // me is the TOC user session
  915. me *state.Session
  916. // givenCmd is the TOC command
  917. givenCmd []byte
  918. // givenChatRegistry is the chat registry passed to the function
  919. givenChatRegistry *ChatRegistry
  920. // wantMsg is the expected TOC response
  921. wantMsg string
  922. // mockParams is the list of params sent to mocks that satisfy this
  923. // method's dependencies
  924. mockParams mockParams
  925. }{
  926. {
  927. name: "successfully leave chat",
  928. me: newTestSession("me"),
  929. givenCmd: []byte(`toc_chat_leave 0`),
  930. givenChatRegistry: func() *ChatRegistry {
  931. reg := NewChatRegistry()
  932. reg.RegisterSess(0, newTestSession("me"))
  933. return reg
  934. }(),
  935. mockParams: mockParams{
  936. authParams: authParams{
  937. signoutChatParams: signoutChatParams{
  938. {
  939. me: state.NewIdentScreenName("me"),
  940. },
  941. },
  942. },
  943. },
  944. wantMsg: "CHAT_LEFT:0",
  945. },
  946. {
  947. name: "chat room ID with invalid format",
  948. givenCmd: []byte(`toc_chat_leave zero`),
  949. wantMsg: cmdInternalSvcErr,
  950. },
  951. {
  952. name: "missing chat session",
  953. givenCmd: []byte(`toc_chat_leave 0`),
  954. givenChatRegistry: NewChatRegistry(),
  955. wantMsg: cmdInternalSvcErr,
  956. },
  957. {
  958. name: "bad command",
  959. givenCmd: []byte(`toc_chat_leave`),
  960. wantMsg: cmdInternalSvcErr,
  961. },
  962. }
  963. for _, tc := range cases {
  964. t.Run(tc.name, func(t *testing.T) {
  965. ctx := context.Background()
  966. authSvc := newMockAuthService(t)
  967. for _, params := range tc.mockParams.signoutChatParams {
  968. authSvc.EXPECT().SignoutChat(ctx, matchSession(params.me))
  969. }
  970. svc := OSCARProxy{
  971. Logger: slog.Default(),
  972. AuthService: authSvc,
  973. }
  974. msg := svc.ChatLeave(ctx, tc.givenChatRegistry, tc.givenCmd)
  975. assert.Equal(t, tc.wantMsg, msg)
  976. })
  977. }
  978. }
  979. func TestOSCARProxy_ChatSend(t *testing.T) {
  980. cases := []struct {
  981. // name is the unit test name
  982. name string
  983. // me is the TOC user session
  984. me *state.Session
  985. // givenCmd is the TOC command
  986. givenCmd []byte
  987. // givenChatRegistry is the chat registry passed to the function
  988. givenChatRegistry *ChatRegistry
  989. // wantMsg is the expected TOC response
  990. wantMsg string
  991. // mockParams is the list of params sent to mocks that satisfy this
  992. // method's dependencies
  993. mockParams mockParams
  994. }{
  995. {
  996. name: "successfully send chat message",
  997. me: newTestSession("me"),
  998. givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
  999. givenChatRegistry: func() *ChatRegistry {
  1000. reg := NewChatRegistry()
  1001. reg.RegisterSess(0, newTestSession("me"))
  1002. return reg
  1003. }(),
  1004. mockParams: mockParams{
  1005. chatParams: chatParams{
  1006. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1007. {
  1008. sender: state.NewIdentScreenName("me"),
  1009. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1010. Channel: wire.ICBMChannelMIME,
  1011. TLVRestBlock: wire.TLVRestBlock{
  1012. TLVList: wire.TLVList{
  1013. wire.NewTLVBE(wire.ChatTLVEnableReflectionFlag, uint8(1)),
  1014. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1015. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  1016. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1017. TLVList: wire.TLVList{
  1018. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1019. },
  1020. }),
  1021. },
  1022. },
  1023. },
  1024. },
  1025. },
  1026. },
  1027. },
  1028. wantMsg: "CHAT_IN:0:me:F:Hello world!",
  1029. },
  1030. {
  1031. name: "send chat message, receive error from chat svc",
  1032. me: newTestSession("me"),
  1033. givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
  1034. givenChatRegistry: func() *ChatRegistry {
  1035. reg := NewChatRegistry()
  1036. reg.RegisterSess(0, newTestSession("me"))
  1037. return reg
  1038. }(),
  1039. mockParams: mockParams{
  1040. chatParams: chatParams{
  1041. channelMsgToHostParamsChat: channelMsgToHostParamsChat{
  1042. {
  1043. sender: state.NewIdentScreenName("me"),
  1044. inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  1045. Channel: wire.ICBMChannelMIME,
  1046. TLVRestBlock: wire.TLVRestBlock{
  1047. TLVList: wire.TLVList{
  1048. wire.NewTLVBE(wire.ChatTLVEnableReflectionFlag, uint8(1)),
  1049. wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
  1050. wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
  1051. wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
  1052. TLVList: wire.TLVList{
  1053. wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
  1054. },
  1055. }),
  1056. },
  1057. },
  1058. },
  1059. err: io.EOF,
  1060. },
  1061. },
  1062. },
  1063. },
  1064. wantMsg: cmdInternalSvcErr,
  1065. },
  1066. {
  1067. name: "chat room ID with invalid format",
  1068. givenCmd: []byte(`toc_chat_send zero "Hello world!"`),
  1069. wantMsg: cmdInternalSvcErr,
  1070. },
  1071. {
  1072. name: "missing chat session",
  1073. givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
  1074. givenChatRegistry: NewChatRegistry(),
  1075. wantMsg: cmdInternalSvcErr,
  1076. },
  1077. {
  1078. name: "bad command",
  1079. givenCmd: []byte(`toc_chat_send`),
  1080. wantMsg: cmdInternalSvcErr,
  1081. },
  1082. }
  1083. for _, tc := range cases {
  1084. t.Run(tc.name, func(t *testing.T) {
  1085. ctx := context.Background()
  1086. chatSvc := newMockChatService(t)
  1087. for _, params := range tc.mockParams.channelMsgToHostParamsChat {
  1088. chatSvc.EXPECT().
  1089. ChannelMsgToHost(ctx, matchSession(params.sender), wire.SNACFrame{}, params.inBody).
  1090. Return(params.result, params.err)
  1091. }
  1092. svc := OSCARProxy{
  1093. Logger: slog.Default(),
  1094. ChatService: chatSvc,
  1095. }
  1096. msg := svc.ChatSend(ctx, tc.givenChatRegistry, tc.givenCmd)
  1097. assert.Equal(t, tc.wantMsg, msg)
  1098. })
  1099. }
  1100. }
  1101. func TestOSCARProxy_Evil(t *testing.T) {
  1102. cases := []struct {
  1103. // name is the unit test name
  1104. name string
  1105. // me is the TOC user session
  1106. me *state.Session
  1107. // givenCmd is the TOC command
  1108. givenCmd []byte
  1109. // wantMsg is the expected TOC response
  1110. wantMsg string
  1111. // mockParams is the list of params sent to mocks that satisfy this
  1112. // method's dependencies
  1113. mockParams mockParams
  1114. }{
  1115. {
  1116. name: "successfully warn normally",
  1117. me: newTestSession("me"),
  1118. givenCmd: []byte(`toc_evil them norm`),
  1119. mockParams: mockParams{
  1120. icbmParams: icbmParams{
  1121. evilRequestParams: evilRequestParams{
  1122. {
  1123. me: state.NewIdentScreenName("me"),
  1124. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1125. SendAs: 0,
  1126. ScreenName: "them",
  1127. },
  1128. msg: wire.SNACMessage{
  1129. Body: wire.SNAC_0x04_0x09_ICBMEvilReply{},
  1130. },
  1131. },
  1132. },
  1133. },
  1134. },
  1135. },
  1136. {
  1137. name: "successfully warn anonymously",
  1138. me: newTestSession("me"),
  1139. givenCmd: []byte(`toc_evil them anon`),
  1140. mockParams: mockParams{
  1141. icbmParams: icbmParams{
  1142. evilRequestParams: evilRequestParams{
  1143. {
  1144. me: state.NewIdentScreenName("me"),
  1145. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1146. SendAs: 1,
  1147. ScreenName: "them",
  1148. },
  1149. msg: wire.SNACMessage{
  1150. Body: wire.SNAC_0x04_0x09_ICBMEvilReply{},
  1151. },
  1152. },
  1153. },
  1154. },
  1155. },
  1156. },
  1157. {
  1158. name: "warn, receive error from ICBM service",
  1159. me: newTestSession("me"),
  1160. givenCmd: []byte(`toc_evil them anon`),
  1161. mockParams: mockParams{
  1162. icbmParams: icbmParams{
  1163. evilRequestParams: evilRequestParams{
  1164. {
  1165. me: state.NewIdentScreenName("me"),
  1166. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1167. SendAs: 1,
  1168. ScreenName: "them",
  1169. },
  1170. err: io.EOF,
  1171. },
  1172. },
  1173. },
  1174. },
  1175. wantMsg: cmdInternalSvcErr,
  1176. },
  1177. {
  1178. name: "warn, receive snac err",
  1179. me: newTestSession("me"),
  1180. givenCmd: []byte(`toc_evil them anon`),
  1181. mockParams: mockParams{
  1182. icbmParams: icbmParams{
  1183. evilRequestParams: evilRequestParams{
  1184. {
  1185. me: state.NewIdentScreenName("me"),
  1186. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1187. SendAs: 1,
  1188. ScreenName: "them",
  1189. },
  1190. msg: wire.SNACMessage{
  1191. Body: wire.SNACError{},
  1192. },
  1193. },
  1194. },
  1195. },
  1196. },
  1197. },
  1198. {
  1199. name: "warn, ICBM svc returns unexpected snac type",
  1200. me: newTestSession("me"),
  1201. givenCmd: []byte(`toc_evil them anon`),
  1202. mockParams: mockParams{
  1203. icbmParams: icbmParams{
  1204. evilRequestParams: evilRequestParams{
  1205. {
  1206. me: state.NewIdentScreenName("me"),
  1207. inBody: wire.SNAC_0x04_0x08_ICBMEvilRequest{
  1208. SendAs: 1,
  1209. ScreenName: "them",
  1210. },
  1211. msg: wire.SNACMessage{
  1212. Body: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{},
  1213. },
  1214. },
  1215. },
  1216. },
  1217. },
  1218. wantMsg: cmdInternalSvcErr,
  1219. },
  1220. {
  1221. name: "warn with incorrect type",
  1222. me: newTestSession("me"),
  1223. givenCmd: []byte(`toc_evil them blah`),
  1224. wantMsg: cmdInternalSvcErr,
  1225. },
  1226. {
  1227. name: "bad command",
  1228. givenCmd: []byte(`toc_evil`),
  1229. wantMsg: cmdInternalSvcErr,
  1230. },
  1231. }
  1232. for _, tc := range cases {
  1233. t.Run(tc.name, func(t *testing.T) {
  1234. ctx := context.Background()
  1235. icbmSvc := newMockICBMService(t)
  1236. for _, params := range tc.mockParams.evilRequestParams {
  1237. icbmSvc.EXPECT().
  1238. EvilRequest(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  1239. Return(params.msg, params.err)
  1240. }
  1241. svc := OSCARProxy{
  1242. Logger: slog.Default(),
  1243. ICBMService: icbmSvc,
  1244. }
  1245. msg := svc.Evil(ctx, tc.me, tc.givenCmd)
  1246. assert.Equal(t, tc.wantMsg, msg)
  1247. })
  1248. }
  1249. }
  1250. func TestOSCARProxy_GetDirSearchURL(t *testing.T) {
  1251. cases := []struct {
  1252. // name is the unit test name
  1253. name string
  1254. // me is the TOC user session
  1255. me *state.Session
  1256. // givenCmd is the TOC command
  1257. givenCmd []byte
  1258. // wantMsg is the expected TOC response
  1259. wantMsg string
  1260. // mockParams is the list of params sent to mocks that satisfy this
  1261. // method's dependencies
  1262. mockParams mockParams
  1263. }{
  1264. {
  1265. name: "successfully request user info",
  1266. me: newTestSession("me"),
  1267. givenCmd: []byte(`toc_dir_search "first name":"middle name":"last name":"maiden name":"city":"state":"country":"email"`),
  1268. mockParams: mockParams{
  1269. cookieBakerParams: cookieBakerParams{
  1270. issueParams: issueParams{
  1271. {
  1272. data: []byte("me"),
  1273. returnData: []byte("monster"),
  1274. },
  1275. },
  1276. },
  1277. },
  1278. 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",
  1279. },
  1280. {
  1281. name: "successfully request user info by keywords",
  1282. me: newTestSession("me"),
  1283. givenCmd: []byte(`toc_dir_search ::::::::::"searchkw"`),
  1284. mockParams: mockParams{
  1285. cookieBakerParams: cookieBakerParams{
  1286. issueParams: issueParams{
  1287. {
  1288. data: []byte("me"),
  1289. returnData: []byte("monster"),
  1290. },
  1291. },
  1292. },
  1293. },
  1294. wantMsg: "GOTO_URL:search results:dir_search?cookie=6d6f6e73746572&keyword=searchkw",
  1295. },
  1296. {
  1297. name: "request user info with too many params",
  1298. me: newTestSession("me"),
  1299. givenCmd: []byte(`toc_dir_search ::::::::::::::::::::"searchkw"`),
  1300. wantMsg: cmdInternalSvcErr,
  1301. },
  1302. {
  1303. name: "request user info, get cookie issue error",
  1304. me: newTestSession("me"),
  1305. givenCmd: []byte(`toc_dir_search them`),
  1306. mockParams: mockParams{
  1307. cookieBakerParams: cookieBakerParams{
  1308. issueParams: issueParams{
  1309. {
  1310. data: []byte("me"),
  1311. returnErr: io.EOF,
  1312. },
  1313. },
  1314. },
  1315. },
  1316. wantMsg: cmdInternalSvcErr,
  1317. },
  1318. {
  1319. name: "bad command",
  1320. givenCmd: []byte(`toc_dir_search`),
  1321. wantMsg: cmdInternalSvcErr,
  1322. },
  1323. }
  1324. for _, tc := range cases {
  1325. t.Run(tc.name, func(t *testing.T) {
  1326. ctx := context.Background()
  1327. cookieBaker := newMockCookieBaker(t)
  1328. for _, params := range tc.mockParams.issueParams {
  1329. cookieBaker.EXPECT().
  1330. Issue(params.data).
  1331. Return(params.returnData, params.returnErr)
  1332. }
  1333. svc := OSCARProxy{
  1334. Logger: slog.Default(),
  1335. CookieBaker: cookieBaker,
  1336. }
  1337. msg := svc.GetDirSearchURL(ctx, tc.me, tc.givenCmd)
  1338. assert.Equal(t, tc.wantMsg, msg)
  1339. })
  1340. }
  1341. }
  1342. func TestOSCARProxy_GetDirURL(t *testing.T) {
  1343. cases := []struct {
  1344. // name is the unit test name
  1345. name string
  1346. // me is the TOC user session
  1347. me *state.Session
  1348. // givenCmd is the TOC command
  1349. givenCmd []byte
  1350. // wantMsg is the expected TOC response
  1351. wantMsg string
  1352. // mockParams is the list of params sent to mocks that satisfy this
  1353. // method's dependencies
  1354. mockParams mockParams
  1355. }{
  1356. {
  1357. name: "successfully request user dir info",
  1358. me: newTestSession("me"),
  1359. givenCmd: []byte(`toc_get_dir them`),
  1360. mockParams: mockParams{
  1361. cookieBakerParams: cookieBakerParams{
  1362. issueParams: issueParams{
  1363. {
  1364. data: []byte("me"),
  1365. returnData: []byte("monster"),
  1366. },
  1367. },
  1368. },
  1369. },
  1370. wantMsg: "GOTO_URL:directory info:dir_info?cookie=6d6f6e73746572&user=them",
  1371. },
  1372. {
  1373. name: "request user info, get cookie issue error",
  1374. me: newTestSession("me"),
  1375. givenCmd: []byte(`toc_get_dir them`),
  1376. mockParams: mockParams{
  1377. cookieBakerParams: cookieBakerParams{
  1378. issueParams: issueParams{
  1379. {
  1380. data: []byte("me"),
  1381. returnErr: io.EOF,
  1382. },
  1383. },
  1384. },
  1385. },
  1386. wantMsg: cmdInternalSvcErr,
  1387. },
  1388. {
  1389. name: "bad command",
  1390. givenCmd: []byte(`toc_get_dir`),
  1391. wantMsg: cmdInternalSvcErr,
  1392. },
  1393. }
  1394. for _, tc := range cases {
  1395. t.Run(tc.name, func(t *testing.T) {
  1396. ctx := context.Background()
  1397. cookieBaker := newMockCookieBaker(t)
  1398. for _, params := range tc.mockParams.issueParams {
  1399. cookieBaker.EXPECT().
  1400. Issue(params.data).
  1401. Return(params.returnData, params.returnErr)
  1402. }
  1403. svc := OSCARProxy{
  1404. Logger: slog.Default(),
  1405. CookieBaker: cookieBaker,
  1406. }
  1407. msg := svc.GetDirURL(ctx, tc.me, tc.givenCmd)
  1408. assert.Equal(t, tc.wantMsg, msg)
  1409. })
  1410. }
  1411. }
  1412. func TestOSCARProxy_GetInfoURL(t *testing.T) {
  1413. cases := []struct {
  1414. // name is the unit test name
  1415. name string
  1416. // me is the TOC user session
  1417. me *state.Session
  1418. // givenCmd is the TOC command
  1419. givenCmd []byte
  1420. // wantMsg is the expected TOC response
  1421. wantMsg string
  1422. // mockParams is the list of params sent to mocks that satisfy this
  1423. // method's dependencies
  1424. mockParams mockParams
  1425. }{
  1426. {
  1427. name: "successfully request user info",
  1428. me: newTestSession("me"),
  1429. givenCmd: []byte(`toc_get_info them`),
  1430. mockParams: mockParams{
  1431. cookieBakerParams: cookieBakerParams{
  1432. issueParams: issueParams{
  1433. {
  1434. data: []byte("me"),
  1435. returnData: []byte("monster"),
  1436. },
  1437. },
  1438. },
  1439. },
  1440. wantMsg: "GOTO_URL:profile:info?cookie=6d6f6e73746572&from=me&user=them",
  1441. },
  1442. {
  1443. name: "request user info, get cookie issue error",
  1444. me: newTestSession("me"),
  1445. givenCmd: []byte(`toc_get_info them`),
  1446. mockParams: mockParams{
  1447. cookieBakerParams: cookieBakerParams{
  1448. issueParams: issueParams{
  1449. {
  1450. data: []byte("me"),
  1451. returnErr: io.EOF,
  1452. },
  1453. },
  1454. },
  1455. },
  1456. wantMsg: cmdInternalSvcErr,
  1457. },
  1458. {
  1459. name: "bad command",
  1460. givenCmd: []byte(`toc_get_info`),
  1461. wantMsg: cmdInternalSvcErr,
  1462. },
  1463. }
  1464. for _, tc := range cases {
  1465. t.Run(tc.name, func(t *testing.T) {
  1466. ctx := context.Background()
  1467. cookieBaker := newMockCookieBaker(t)
  1468. for _, params := range tc.mockParams.issueParams {
  1469. cookieBaker.EXPECT().
  1470. Issue(params.data).
  1471. Return(params.returnData, params.returnErr)
  1472. }
  1473. svc := OSCARProxy{
  1474. Logger: slog.Default(),
  1475. CookieBaker: cookieBaker,
  1476. }
  1477. msg := svc.GetInfoURL(ctx, tc.me, tc.givenCmd)
  1478. assert.Equal(t, tc.wantMsg, msg)
  1479. })
  1480. }
  1481. }
  1482. func TestOSCARProxy_InitDone(t *testing.T) {
  1483. cases := []struct {
  1484. // name is the unit test name
  1485. name string
  1486. // me is the TOC user session
  1487. me *state.Session
  1488. // givenCmd is the TOC command
  1489. givenCmd []byte
  1490. // wantMsg is the expected TOC response
  1491. wantMsg string
  1492. // mockParams is the list of params sent to mocks that satisfy this
  1493. // method's dependencies
  1494. mockParams mockParams
  1495. }{
  1496. {
  1497. name: "successfully initialize connection",
  1498. me: newTestSession("me"),
  1499. givenCmd: []byte(`toc_init_done`),
  1500. mockParams: mockParams{
  1501. oServiceBOSParams: oServiceParams{
  1502. clientOnlineParams: clientOnlineParams{
  1503. {
  1504. me: state.NewIdentScreenName("me"),
  1505. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  1506. },
  1507. },
  1508. },
  1509. },
  1510. },
  1511. {
  1512. name: "initialize connection, receive err from BOS oservice svc",
  1513. me: newTestSession("me"),
  1514. givenCmd: []byte(`toc_init_done`),
  1515. mockParams: mockParams{
  1516. oServiceBOSParams: oServiceParams{
  1517. clientOnlineParams: clientOnlineParams{
  1518. {
  1519. me: state.NewIdentScreenName("me"),
  1520. body: wire.SNAC_0x01_0x02_OServiceClientOnline{},
  1521. err: io.EOF,
  1522. },
  1523. },
  1524. },
  1525. },
  1526. wantMsg: cmdInternalSvcErr,
  1527. },
  1528. {
  1529. name: "bad command",
  1530. givenCmd: []byte(`toc_init_done_diff`),
  1531. wantMsg: cmdInternalSvcErr,
  1532. },
  1533. }
  1534. for _, tc := range cases {
  1535. t.Run(tc.name, func(t *testing.T) {
  1536. ctx := context.Background()
  1537. oSvc := newMockOServiceService(t)
  1538. for _, params := range tc.mockParams.oServiceBOSParams.clientOnlineParams {
  1539. oSvc.EXPECT().
  1540. ClientOnline(ctx, params.body, matchSession(params.me)).
  1541. Return(params.err)
  1542. }
  1543. svc := OSCARProxy{
  1544. Logger: slog.Default(),
  1545. OServiceServiceBOS: oSvc,
  1546. }
  1547. msg := svc.InitDone(ctx, tc.me, tc.givenCmd)
  1548. assert.Equal(t, tc.wantMsg, msg)
  1549. })
  1550. }
  1551. }
  1552. func TestOSCARProxy_RemoveBuddy(t *testing.T) {
  1553. cases := []struct {
  1554. // name is the unit test name
  1555. name string
  1556. // me is the TOC user session
  1557. me *state.Session
  1558. // givenCmd is the TOC command
  1559. givenCmd []byte
  1560. // wantMsg is the expected TOC response
  1561. wantMsg string
  1562. // mockParams is the list of params sent to mocks that satisfy this
  1563. // method's dependencies
  1564. mockParams mockParams
  1565. }{
  1566. {
  1567. name: "successfully remove buddies",
  1568. me: newTestSession("me"),
  1569. givenCmd: []byte("toc_remove_buddy friend1 friend2 friend3"),
  1570. mockParams: mockParams{
  1571. buddyParams: buddyParams{
  1572. delBuddiesParams: delBuddiesParams{
  1573. {
  1574. me: state.NewIdentScreenName("me"),
  1575. inBody: wire.SNAC_0x03_0x05_BuddyDelBuddies{
  1576. Buddies: []struct {
  1577. ScreenName string `oscar:"len_prefix=uint8"`
  1578. }{
  1579. {ScreenName: "friend1"},
  1580. {ScreenName: "friend2"},
  1581. {ScreenName: "friend3"},
  1582. },
  1583. },
  1584. },
  1585. },
  1586. },
  1587. },
  1588. },
  1589. {
  1590. name: "remove buddies, receive error from buddy service",
  1591. me: newTestSession("me"),
  1592. givenCmd: []byte("toc_remove_buddy friend1"),
  1593. mockParams: mockParams{
  1594. buddyParams: buddyParams{
  1595. delBuddiesParams: delBuddiesParams{
  1596. {
  1597. me: state.NewIdentScreenName("me"),
  1598. inBody: wire.SNAC_0x03_0x05_BuddyDelBuddies{
  1599. Buddies: []struct {
  1600. ScreenName string `oscar:"len_prefix=uint8"`
  1601. }{
  1602. {ScreenName: "friend1"},
  1603. },
  1604. },
  1605. err: io.EOF,
  1606. },
  1607. },
  1608. },
  1609. },
  1610. wantMsg: cmdInternalSvcErr,
  1611. },
  1612. {
  1613. name: "bad command",
  1614. givenCmd: []byte(`toc_remove_buddy_bad`),
  1615. wantMsg: cmdInternalSvcErr,
  1616. },
  1617. }
  1618. for _, tc := range cases {
  1619. t.Run(tc.name, func(t *testing.T) {
  1620. ctx := context.Background()
  1621. buddySvc := newMockBuddyService(t)
  1622. for _, params := range tc.mockParams.delBuddiesParams {
  1623. buddySvc.EXPECT().
  1624. DelBuddies(ctx, matchSession(params.me), params.inBody).
  1625. Return(params.err)
  1626. }
  1627. svc := OSCARProxy{
  1628. Logger: slog.Default(),
  1629. BuddyService: buddySvc,
  1630. }
  1631. msg := svc.RemoveBuddy(ctx, tc.me, tc.givenCmd)
  1632. assert.Equal(t, tc.wantMsg, msg)
  1633. })
  1634. }
  1635. }
  1636. func TestOSCARProxy_SendIM(t *testing.T) {
  1637. cases := []struct {
  1638. // name is the unit test name
  1639. name string
  1640. // me is the TOC user session
  1641. me *state.Session
  1642. // givenCmd is the TOC command
  1643. givenCmd []byte
  1644. // wantMsg is the expected TOC response
  1645. wantMsg string
  1646. // mockParams is the list of params sent to mocks that satisfy this
  1647. // method's dependencies
  1648. mockParams mockParams
  1649. }{
  1650. {
  1651. name: "successfully send instant message",
  1652. me: newTestSession("me"),
  1653. givenCmd: []byte(`toc_send_im chattingChuck "hello world!"`),
  1654. mockParams: mockParams{
  1655. icbmParams: icbmParams{
  1656. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  1657. {
  1658. sender: state.NewIdentScreenName("me"),
  1659. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  1660. ChannelID: wire.ICBMChannelIM,
  1661. ScreenName: "chattingChuck",
  1662. TLVRestBlock: wire.TLVRestBlock{
  1663. TLVList: wire.TLVList{
  1664. wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
  1665. {
  1666. ID: 5,
  1667. Version: 1,
  1668. Payload: []byte{1, 1, 2},
  1669. },
  1670. {
  1671. ID: 1,
  1672. Version: 1,
  1673. Payload: []byte{
  1674. 0x00, 0x00,
  1675. 0x00, 0x00,
  1676. 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
  1677. },
  1678. },
  1679. }),
  1680. },
  1681. },
  1682. },
  1683. },
  1684. },
  1685. },
  1686. },
  1687. },
  1688. {
  1689. name: "successfully auto-reply send instant message",
  1690. me: newTestSession("me"),
  1691. givenCmd: []byte(`toc_send_im chattingChuck "hello world!" auto`),
  1692. mockParams: mockParams{
  1693. icbmParams: icbmParams{
  1694. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  1695. {
  1696. sender: state.NewIdentScreenName("me"),
  1697. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  1698. ChannelID: wire.ICBMChannelIM,
  1699. ScreenName: "chattingChuck",
  1700. TLVRestBlock: wire.TLVRestBlock{
  1701. TLVList: wire.TLVList{
  1702. wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
  1703. {
  1704. ID: 5,
  1705. Version: 1,
  1706. Payload: []byte{1, 1, 2},
  1707. },
  1708. {
  1709. ID: 1,
  1710. Version: 1,
  1711. Payload: []byte{
  1712. 0x00, 0x00,
  1713. 0x00, 0x00,
  1714. 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
  1715. },
  1716. },
  1717. }),
  1718. wire.NewTLVBE(wire.ICBMTLVAutoResponse, []byte{}),
  1719. },
  1720. },
  1721. },
  1722. },
  1723. },
  1724. },
  1725. },
  1726. },
  1727. {
  1728. name: "send instant message, receive error from ICBM service",
  1729. me: newTestSession("me"),
  1730. givenCmd: []byte(`toc_send_im chattingChuck "hello world!"`),
  1731. mockParams: mockParams{
  1732. icbmParams: icbmParams{
  1733. channelMsgToHostParamsICBM: channelMsgToHostParamsICBM{
  1734. {
  1735. sender: state.NewIdentScreenName("me"),
  1736. inBody: wire.SNAC_0x04_0x06_ICBMChannelMsgToHost{
  1737. ChannelID: wire.ICBMChannelIM,
  1738. ScreenName: "chattingChuck",
  1739. TLVRestBlock: wire.TLVRestBlock{
  1740. TLVList: wire.TLVList{
  1741. wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
  1742. {
  1743. ID: 5,
  1744. Version: 1,
  1745. Payload: []byte{1, 1, 2},
  1746. },
  1747. {
  1748. ID: 1,
  1749. Version: 1,
  1750. Payload: []byte{
  1751. 0x00, 0x00,
  1752. 0x00, 0x00,
  1753. 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
  1754. },
  1755. },
  1756. }),
  1757. },
  1758. },
  1759. },
  1760. err: io.EOF,
  1761. },
  1762. },
  1763. },
  1764. },
  1765. wantMsg: cmdInternalSvcErr,
  1766. },
  1767. {
  1768. name: "bad command",
  1769. givenCmd: []byte(`toc_send_im`),
  1770. wantMsg: cmdInternalSvcErr,
  1771. },
  1772. }
  1773. for _, tc := range cases {
  1774. t.Run(tc.name, func(t *testing.T) {
  1775. ctx := context.Background()
  1776. icbmSvc := newMockICBMService(t)
  1777. for _, params := range tc.mockParams.channelMsgToHostParamsICBM {
  1778. icbmSvc.EXPECT().
  1779. ChannelMsgToHost(ctx, matchSession(params.sender), params.inFrame, params.inBody).
  1780. Return(params.result, params.err)
  1781. }
  1782. svc := OSCARProxy{
  1783. Logger: slog.Default(),
  1784. ICBMService: icbmSvc,
  1785. }
  1786. msg := svc.SendIM(ctx, tc.me, tc.givenCmd)
  1787. assert.Equal(t, tc.wantMsg, msg)
  1788. })
  1789. }
  1790. }
  1791. func TestOSCARProxy_SetAway(t *testing.T) {
  1792. cases := []struct {
  1793. // name is the unit test name
  1794. name string
  1795. // me is the TOC user session
  1796. me *state.Session
  1797. // givenCmd is the TOC command
  1798. givenCmd []byte
  1799. // wantMsg is the expected TOC response
  1800. wantMsg string
  1801. // mockParams is the list of params sent to mocks that satisfy this
  1802. // method's dependencies
  1803. mockParams mockParams
  1804. }{
  1805. {
  1806. name: "successfully set away with message",
  1807. me: newTestSession("me"),
  1808. givenCmd: []byte(`toc_set_away "I'm away from my computer right now."`),
  1809. mockParams: mockParams{
  1810. locateParams: locateParams{
  1811. setInfoParams: setInfoParams{
  1812. {
  1813. me: state.NewIdentScreenName("me"),
  1814. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  1815. TLVRestBlock: wire.TLVRestBlock{
  1816. TLVList: wire.TLVList{
  1817. wire.NewTLVBE(wire.LocateTLVTagsInfoUnavailableData, "I'm away from my computer right now."),
  1818. },
  1819. },
  1820. },
  1821. },
  1822. },
  1823. },
  1824. },
  1825. },
  1826. {
  1827. name: "successfully set away without message",
  1828. me: newTestSession("me"),
  1829. givenCmd: []byte(`toc_set_away`),
  1830. mockParams: mockParams{
  1831. locateParams: locateParams{
  1832. setInfoParams: setInfoParams{
  1833. {
  1834. me: state.NewIdentScreenName("me"),
  1835. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  1836. TLVRestBlock: wire.TLVRestBlock{
  1837. TLVList: wire.TLVList{
  1838. wire.NewTLVBE(wire.LocateTLVTagsInfoUnavailableData, ""),
  1839. },
  1840. },
  1841. },
  1842. },
  1843. },
  1844. },
  1845. },
  1846. },
  1847. {
  1848. name: "set away message, receive error from locate service",
  1849. me: newTestSession("me"),
  1850. givenCmd: []byte(`toc_set_away "I'm away from my computer right now."`),
  1851. mockParams: mockParams{
  1852. locateParams: locateParams{
  1853. setInfoParams: setInfoParams{
  1854. {
  1855. me: state.NewIdentScreenName("me"),
  1856. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  1857. TLVRestBlock: wire.TLVRestBlock{
  1858. TLVList: wire.TLVList{
  1859. wire.NewTLVBE(wire.LocateTLVTagsInfoUnavailableData, "I'm away from my computer right now."),
  1860. },
  1861. },
  1862. },
  1863. err: io.EOF,
  1864. },
  1865. },
  1866. },
  1867. },
  1868. wantMsg: cmdInternalSvcErr,
  1869. },
  1870. {
  1871. name: "bad command",
  1872. givenCmd: []byte(`toc_set_away_bad`),
  1873. wantMsg: cmdInternalSvcErr,
  1874. },
  1875. }
  1876. for _, tc := range cases {
  1877. t.Run(tc.name, func(t *testing.T) {
  1878. ctx := context.Background()
  1879. locateSvc := newMockLocateService(t)
  1880. for _, params := range tc.mockParams.setInfoParams {
  1881. locateSvc.EXPECT().
  1882. SetInfo(ctx, matchSession(params.me), params.inBody).
  1883. Return(params.err)
  1884. }
  1885. svc := OSCARProxy{
  1886. Logger: slog.Default(),
  1887. LocateService: locateSvc,
  1888. }
  1889. msg := svc.SetAway(ctx, tc.me, tc.givenCmd)
  1890. assert.Equal(t, tc.wantMsg, msg)
  1891. })
  1892. }
  1893. }
  1894. func TestOSCARProxy_SetCaps(t *testing.T) {
  1895. cases := []struct {
  1896. // name is the unit test name
  1897. name string
  1898. // me is the TOC user session
  1899. me *state.Session
  1900. // givenCmd is the TOC command
  1901. givenCmd []byte
  1902. // wantMsg is the expected TOC response
  1903. wantMsg string
  1904. // mockParams is the list of params sent to mocks that satisfy this
  1905. // method's dependencies
  1906. mockParams mockParams
  1907. }{
  1908. {
  1909. name: "successfully set capabilities",
  1910. me: newTestSession("me"),
  1911. givenCmd: []byte(`toc_set_caps 09460000-4C7F-11D1-8222-444553540000 09460001-4C7F-11D1-8222-444553540000`),
  1912. mockParams: mockParams{
  1913. locateParams: locateParams{
  1914. setInfoParams: setInfoParams{
  1915. {
  1916. me: state.NewIdentScreenName("me"),
  1917. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  1918. TLVRestBlock: wire.TLVRestBlock{
  1919. TLVList: wire.TLVList{
  1920. wire.NewTLVBE(wire.LocateTLVTagsInfoCapabilities, []uuid.UUID{
  1921. uuid.MustParse("09460000-4C7F-11D1-8222-444553540000"),
  1922. uuid.MustParse("09460001-4C7F-11D1-8222-444553540000"),
  1923. capChat,
  1924. }),
  1925. },
  1926. },
  1927. },
  1928. },
  1929. },
  1930. },
  1931. },
  1932. },
  1933. {
  1934. name: "set capability, receive error from locate service",
  1935. me: newTestSession("me"),
  1936. givenCmd: []byte(`toc_set_caps 09460000-4C7F-11D1-8222-444553540000`),
  1937. mockParams: mockParams{
  1938. locateParams: locateParams{
  1939. setInfoParams: setInfoParams{
  1940. {
  1941. me: state.NewIdentScreenName("me"),
  1942. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  1943. TLVRestBlock: wire.TLVRestBlock{
  1944. TLVList: wire.TLVList{
  1945. wire.NewTLVBE(wire.LocateTLVTagsInfoCapabilities, []uuid.UUID{
  1946. uuid.MustParse("09460000-4C7F-11D1-8222-444553540000"),
  1947. capChat,
  1948. }),
  1949. },
  1950. },
  1951. },
  1952. err: io.EOF,
  1953. },
  1954. },
  1955. },
  1956. },
  1957. wantMsg: cmdInternalSvcErr,
  1958. },
  1959. {
  1960. name: "set malformed capability UUID",
  1961. me: newTestSession("me"),
  1962. givenCmd: []byte(`toc_set_caps 09460000-`),
  1963. wantMsg: cmdInternalSvcErr,
  1964. },
  1965. {
  1966. name: "bad command",
  1967. givenCmd: []byte(`toc_set_caps_bad`),
  1968. wantMsg: cmdInternalSvcErr,
  1969. },
  1970. }
  1971. for _, tc := range cases {
  1972. t.Run(tc.name, func(t *testing.T) {
  1973. ctx := context.Background()
  1974. locateSvc := newMockLocateService(t)
  1975. for _, params := range tc.mockParams.setInfoParams {
  1976. locateSvc.EXPECT().
  1977. SetInfo(ctx, matchSession(params.me), params.inBody).
  1978. Return(params.err)
  1979. }
  1980. svc := OSCARProxy{
  1981. Logger: slog.Default(),
  1982. LocateService: locateSvc,
  1983. }
  1984. msg := svc.SetCaps(ctx, tc.me, tc.givenCmd)
  1985. assert.Equal(t, tc.wantMsg, msg)
  1986. })
  1987. }
  1988. }
  1989. func TestOSCARProxy_SetConfig(t *testing.T) {
  1990. cases := []struct {
  1991. // name is the unit test name
  1992. name string
  1993. // me is the TOC user session
  1994. me *state.Session
  1995. // givenCmd is the TOC command
  1996. givenCmd []byte
  1997. // wantMsg is the expected TOC response
  1998. wantMsg string
  1999. // mockParams is the list of params sent to mocks that satisfy this
  2000. // method's dependencies
  2001. mockParams mockParams
  2002. }{
  2003. {
  2004. name: "successfully set permit all config",
  2005. me: newTestSession("me"),
  2006. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\nb friend2\n}\n"),
  2007. mockParams: mockParams{
  2008. permitDenyParams: permitDenyParams{
  2009. // confusingly, setting "permit all" mode requires adding a
  2010. // deny list entry
  2011. addDenyListEntriesParams: addDenyListEntriesParams{
  2012. {
  2013. me: state.NewIdentScreenName("me"),
  2014. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2015. Users: []struct {
  2016. ScreenName string `oscar:"len_prefix=uint8"`
  2017. }{
  2018. {ScreenName: "me"},
  2019. },
  2020. },
  2021. },
  2022. },
  2023. },
  2024. buddyParams: buddyParams{
  2025. addBuddiesParams: addBuddiesParams{
  2026. {
  2027. me: state.NewIdentScreenName("me"),
  2028. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2029. Buddies: []struct {
  2030. ScreenName string `oscar:"len_prefix=uint8"`
  2031. }{
  2032. {ScreenName: "friend1"},
  2033. {ScreenName: "friend2"},
  2034. },
  2035. },
  2036. },
  2037. },
  2038. },
  2039. tocConfigParams: tocConfigParams{
  2040. setTOCConfigParams: setTOCConfigParams{
  2041. {
  2042. user: state.NewIdentScreenName("me"),
  2043. config: "m 1\ng Buddies\nb friend1\nb friend2",
  2044. },
  2045. },
  2046. },
  2047. },
  2048. },
  2049. {
  2050. name: "set permit all config, receive err from config store svc",
  2051. me: newTestSession("me"),
  2052. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\n}\n"),
  2053. mockParams: mockParams{
  2054. permitDenyParams: permitDenyParams{
  2055. // confusingly, setting "permit all" mode requires adding a
  2056. // deny list entry
  2057. addDenyListEntriesParams: addDenyListEntriesParams{
  2058. {
  2059. me: state.NewIdentScreenName("me"),
  2060. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2061. Users: []struct {
  2062. ScreenName string `oscar:"len_prefix=uint8"`
  2063. }{
  2064. {ScreenName: "me"},
  2065. },
  2066. },
  2067. },
  2068. },
  2069. },
  2070. buddyParams: buddyParams{
  2071. addBuddiesParams: addBuddiesParams{
  2072. {
  2073. me: state.NewIdentScreenName("me"),
  2074. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2075. Buddies: []struct {
  2076. ScreenName string `oscar:"len_prefix=uint8"`
  2077. }{
  2078. {ScreenName: "friend1"},
  2079. },
  2080. },
  2081. },
  2082. },
  2083. },
  2084. tocConfigParams: tocConfigParams{
  2085. setTOCConfigParams: setTOCConfigParams{
  2086. {
  2087. user: state.NewIdentScreenName("me"),
  2088. config: "m 1\ng Buddies\nb friend1",
  2089. err: io.EOF,
  2090. },
  2091. },
  2092. },
  2093. },
  2094. wantMsg: cmdInternalSvcErr,
  2095. },
  2096. {
  2097. name: "set permit all config, receive err from buddy svc",
  2098. me: newTestSession("me"),
  2099. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\n}\n"),
  2100. mockParams: mockParams{
  2101. permitDenyParams: permitDenyParams{
  2102. // confusingly, setting "permit all" mode requires adding a
  2103. // deny list entry
  2104. addDenyListEntriesParams: addDenyListEntriesParams{
  2105. {
  2106. me: state.NewIdentScreenName("me"),
  2107. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2108. Users: []struct {
  2109. ScreenName string `oscar:"len_prefix=uint8"`
  2110. }{
  2111. {ScreenName: "me"},
  2112. },
  2113. },
  2114. },
  2115. },
  2116. },
  2117. buddyParams: buddyParams{
  2118. addBuddiesParams: addBuddiesParams{
  2119. {
  2120. me: state.NewIdentScreenName("me"),
  2121. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2122. Buddies: []struct {
  2123. ScreenName string `oscar:"len_prefix=uint8"`
  2124. }{
  2125. {ScreenName: "friend1"},
  2126. },
  2127. },
  2128. err: io.EOF,
  2129. },
  2130. },
  2131. },
  2132. },
  2133. wantMsg: cmdInternalSvcErr,
  2134. },
  2135. {
  2136. name: "set permit all config, receive err from permit-deny svc",
  2137. me: newTestSession("me"),
  2138. givenCmd: []byte("toc_set_config {m 1\ng Buddies\nb friend1\nb friend2\n}\n"),
  2139. mockParams: mockParams{
  2140. permitDenyParams: permitDenyParams{
  2141. // confusingly, setting "permit all" mode requires adding a
  2142. // deny list entry
  2143. addDenyListEntriesParams: addDenyListEntriesParams{
  2144. {
  2145. me: state.NewIdentScreenName("me"),
  2146. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2147. Users: []struct {
  2148. ScreenName string `oscar:"len_prefix=uint8"`
  2149. }{
  2150. {ScreenName: "me"},
  2151. },
  2152. },
  2153. err: io.EOF,
  2154. },
  2155. },
  2156. },
  2157. },
  2158. wantMsg: cmdInternalSvcErr,
  2159. },
  2160. {
  2161. name: "successfully set deny all config",
  2162. me: newTestSession("me"),
  2163. givenCmd: []byte("toc_set_config {m 2\ng Buddies\nb friend1\nb friend2\n}\n"),
  2164. mockParams: mockParams{
  2165. permitDenyParams: permitDenyParams{
  2166. // confusingly, setting "deny all" mode requires adding a
  2167. // permit list entry
  2168. addPermListEntriesParams: addPermListEntriesParams{
  2169. {
  2170. me: state.NewIdentScreenName("me"),
  2171. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  2172. Users: []struct {
  2173. ScreenName string `oscar:"len_prefix=uint8"`
  2174. }{
  2175. {ScreenName: "me"},
  2176. },
  2177. },
  2178. },
  2179. },
  2180. },
  2181. buddyParams: buddyParams{
  2182. addBuddiesParams: addBuddiesParams{
  2183. {
  2184. me: state.NewIdentScreenName("me"),
  2185. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2186. Buddies: []struct {
  2187. ScreenName string `oscar:"len_prefix=uint8"`
  2188. }{
  2189. {ScreenName: "friend1"},
  2190. {ScreenName: "friend2"},
  2191. },
  2192. },
  2193. },
  2194. },
  2195. },
  2196. tocConfigParams: tocConfigParams{
  2197. setTOCConfigParams: setTOCConfigParams{
  2198. {
  2199. user: state.NewIdentScreenName("me"),
  2200. config: "m 2\ng Buddies\nb friend1\nb friend2",
  2201. },
  2202. },
  2203. },
  2204. },
  2205. },
  2206. {
  2207. name: "set deny all config, receive err from permit-deny svc",
  2208. me: newTestSession("me"),
  2209. givenCmd: []byte("toc_set_config {m 2\ng Buddies\nb friend1\nb friend2\n}\n"),
  2210. mockParams: mockParams{
  2211. permitDenyParams: permitDenyParams{
  2212. // confusingly, setting "deny all" mode requires adding a
  2213. // permit list entry
  2214. addPermListEntriesParams: addPermListEntriesParams{
  2215. {
  2216. me: state.NewIdentScreenName("me"),
  2217. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  2218. Users: []struct {
  2219. ScreenName string `oscar:"len_prefix=uint8"`
  2220. }{
  2221. {ScreenName: "me"},
  2222. },
  2223. },
  2224. err: io.EOF,
  2225. },
  2226. },
  2227. },
  2228. },
  2229. wantMsg: cmdInternalSvcErr,
  2230. },
  2231. {
  2232. name: "successfully set permit some config",
  2233. me: newTestSession("me"),
  2234. givenCmd: []byte("toc_set_config {m 3\np friend3\np friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  2235. mockParams: mockParams{
  2236. permitDenyParams: permitDenyParams{
  2237. addPermListEntriesParams: addPermListEntriesParams{
  2238. {
  2239. me: state.NewIdentScreenName("me"),
  2240. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  2241. Users: []struct {
  2242. ScreenName string `oscar:"len_prefix=uint8"`
  2243. }{
  2244. {ScreenName: "friend3"},
  2245. {ScreenName: "friend4"},
  2246. },
  2247. },
  2248. },
  2249. },
  2250. },
  2251. buddyParams: buddyParams{
  2252. addBuddiesParams: addBuddiesParams{
  2253. {
  2254. me: state.NewIdentScreenName("me"),
  2255. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2256. Buddies: []struct {
  2257. ScreenName string `oscar:"len_prefix=uint8"`
  2258. }{
  2259. {ScreenName: "friend1"},
  2260. {ScreenName: "friend2"},
  2261. },
  2262. },
  2263. },
  2264. },
  2265. },
  2266. tocConfigParams: tocConfigParams{
  2267. setTOCConfigParams: setTOCConfigParams{
  2268. {
  2269. user: state.NewIdentScreenName("me"),
  2270. config: "m 3\np friend3\np friend4\n\ng Buddies\nb friend1\nb friend2",
  2271. },
  2272. },
  2273. },
  2274. },
  2275. },
  2276. {
  2277. name: "set permit some config, receive err from permit-deny svc",
  2278. me: newTestSession("me"),
  2279. givenCmd: []byte("toc_set_config {m 3\np friend3\np friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  2280. mockParams: mockParams{
  2281. permitDenyParams: permitDenyParams{
  2282. addPermListEntriesParams: addPermListEntriesParams{
  2283. {
  2284. me: state.NewIdentScreenName("me"),
  2285. body: wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries{
  2286. Users: []struct {
  2287. ScreenName string `oscar:"len_prefix=uint8"`
  2288. }{
  2289. {ScreenName: "friend3"},
  2290. {ScreenName: "friend4"},
  2291. },
  2292. },
  2293. err: io.EOF,
  2294. },
  2295. },
  2296. },
  2297. },
  2298. wantMsg: cmdInternalSvcErr,
  2299. },
  2300. {
  2301. name: "successfully set deny some config",
  2302. me: newTestSession("me"),
  2303. givenCmd: []byte("toc_set_config {m 4\nd friend3\nd friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  2304. mockParams: mockParams{
  2305. permitDenyParams: permitDenyParams{
  2306. addDenyListEntriesParams: addDenyListEntriesParams{
  2307. {
  2308. me: state.NewIdentScreenName("me"),
  2309. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2310. Users: []struct {
  2311. ScreenName string `oscar:"len_prefix=uint8"`
  2312. }{
  2313. {ScreenName: "friend3"},
  2314. {ScreenName: "friend4"},
  2315. },
  2316. },
  2317. },
  2318. },
  2319. },
  2320. buddyParams: buddyParams{
  2321. addBuddiesParams: addBuddiesParams{
  2322. {
  2323. me: state.NewIdentScreenName("me"),
  2324. inBody: wire.SNAC_0x03_0x04_BuddyAddBuddies{
  2325. Buddies: []struct {
  2326. ScreenName string `oscar:"len_prefix=uint8"`
  2327. }{
  2328. {ScreenName: "friend1"},
  2329. {ScreenName: "friend2"},
  2330. },
  2331. },
  2332. },
  2333. },
  2334. },
  2335. tocConfigParams: tocConfigParams{
  2336. setTOCConfigParams: setTOCConfigParams{
  2337. {
  2338. user: state.NewIdentScreenName("me"),
  2339. config: "m 4\nd friend3\nd friend4\n\ng Buddies\nb friend1\nb friend2",
  2340. },
  2341. },
  2342. },
  2343. },
  2344. },
  2345. {
  2346. name: "set deny some config, receive err from permit-deny svc",
  2347. me: newTestSession("me"),
  2348. givenCmd: []byte("toc_set_config {m 4\nd friend3\nd friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  2349. mockParams: mockParams{
  2350. permitDenyParams: permitDenyParams{
  2351. addDenyListEntriesParams: addDenyListEntriesParams{
  2352. {
  2353. me: state.NewIdentScreenName("me"),
  2354. body: wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries{
  2355. Users: []struct {
  2356. ScreenName string `oscar:"len_prefix=uint8"`
  2357. }{
  2358. {ScreenName: "friend3"},
  2359. {ScreenName: "friend4"},
  2360. },
  2361. },
  2362. err: io.EOF,
  2363. },
  2364. },
  2365. },
  2366. },
  2367. wantMsg: cmdInternalSvcErr,
  2368. },
  2369. {
  2370. name: "set unknown PD mode",
  2371. me: newTestSession("me"),
  2372. givenCmd: []byte("toc_set_config {m 5\nd friend3\nd friend4\n\ng Buddies\nb friend1\nb friend2\n}\n"),
  2373. wantMsg: cmdInternalSvcErr,
  2374. },
  2375. {
  2376. name: "bad command",
  2377. givenCmd: []byte(`toc_chat_leave`),
  2378. wantMsg: cmdInternalSvcErr,
  2379. },
  2380. }
  2381. for _, tc := range cases {
  2382. t.Run(tc.name, func(t *testing.T) {
  2383. ctx := context.Background()
  2384. pdSvc := newMockPermitDenyService(t)
  2385. for _, params := range tc.mockParams.addDenyListEntriesParams {
  2386. pdSvc.EXPECT().
  2387. AddDenyListEntries(ctx, matchSession(params.me), params.body).
  2388. Return(params.err)
  2389. }
  2390. for _, params := range tc.mockParams.addPermListEntriesParams {
  2391. pdSvc.EXPECT().
  2392. AddPermListEntries(ctx, matchSession(params.me), params.body).
  2393. Return(params.err)
  2394. }
  2395. buddySvc := newMockBuddyService(t)
  2396. for _, params := range tc.mockParams.addBuddiesParams {
  2397. buddySvc.EXPECT().
  2398. AddBuddies(ctx, matchSession(params.me), params.inBody).
  2399. Return(params.err)
  2400. }
  2401. tocConfigSvc := newMockTOCConfigStore(t)
  2402. for _, params := range tc.mockParams.setTOCConfigParams {
  2403. tocConfigSvc.EXPECT().
  2404. SetTOCConfig(params.user, params.config).
  2405. Return(params.err)
  2406. }
  2407. svc := OSCARProxy{
  2408. BuddyService: buddySvc,
  2409. Logger: slog.Default(),
  2410. PermitDenyService: pdSvc,
  2411. TOCConfigStore: tocConfigSvc,
  2412. }
  2413. msg := svc.SetConfig(ctx, tc.me, tc.givenCmd)
  2414. assert.Equal(t, tc.wantMsg, msg)
  2415. })
  2416. }
  2417. }
  2418. func TestOSCARProxy_SetDir(t *testing.T) {
  2419. cases := []struct {
  2420. // name is the unit test name
  2421. name string
  2422. // me is the TOC user session
  2423. me *state.Session
  2424. // givenCmd is the TOC command
  2425. givenCmd []byte
  2426. // wantMsg is the expected TOC response
  2427. wantMsg string
  2428. // mockParams is the list of params sent to mocks that satisfy this
  2429. // method's dependencies
  2430. mockParams mockParams
  2431. }{
  2432. {
  2433. name: "successfully set directory info with quoted fields",
  2434. me: newTestSession("me"),
  2435. givenCmd: []byte(`toc_set_dir "first name":"middle name":"last name":"maiden name":"city":"state":"country":"email":"allow web searches"`),
  2436. mockParams: mockParams{
  2437. locateParams: locateParams{
  2438. setDirInfoParams: setDirInfoParams{
  2439. {
  2440. me: state.NewIdentScreenName("me"),
  2441. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  2442. TLVRestBlock: wire.TLVRestBlock{
  2443. TLVList: wire.TLVList{
  2444. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  2445. wire.NewTLVBE(wire.ODirTLVMiddleName, "middle name"),
  2446. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  2447. wire.NewTLVBE(wire.ODirTLVMaidenName, "maiden name"),
  2448. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  2449. wire.NewTLVBE(wire.ODirTLVState, "state"),
  2450. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  2451. },
  2452. },
  2453. },
  2454. },
  2455. },
  2456. },
  2457. },
  2458. },
  2459. {
  2460. name: "successfully set directory info with some blank fields",
  2461. me: newTestSession("me"),
  2462. givenCmd: []byte(`toc_set_dir "first name"::"last name"::"city":"state":"country":"email":"allow web searches"`),
  2463. mockParams: mockParams{
  2464. locateParams: locateParams{
  2465. setDirInfoParams: setDirInfoParams{
  2466. {
  2467. me: state.NewIdentScreenName("me"),
  2468. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  2469. TLVRestBlock: wire.TLVRestBlock{
  2470. TLVList: wire.TLVList{
  2471. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  2472. wire.NewTLVBE(wire.ODirTLVMiddleName, ""),
  2473. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  2474. wire.NewTLVBE(wire.ODirTLVMaidenName, ""),
  2475. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  2476. wire.NewTLVBE(wire.ODirTLVState, "state"),
  2477. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  2478. },
  2479. },
  2480. },
  2481. },
  2482. },
  2483. },
  2484. },
  2485. },
  2486. {
  2487. name: "successfully set directory info with last two fields absent",
  2488. me: newTestSession("me"),
  2489. givenCmd: []byte(`toc_set_dir "first name"::"last name"::"city":"state":"country"`),
  2490. mockParams: mockParams{
  2491. locateParams: locateParams{
  2492. setDirInfoParams: setDirInfoParams{
  2493. {
  2494. me: state.NewIdentScreenName("me"),
  2495. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  2496. TLVRestBlock: wire.TLVRestBlock{
  2497. TLVList: wire.TLVList{
  2498. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  2499. wire.NewTLVBE(wire.ODirTLVMiddleName, ""),
  2500. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  2501. wire.NewTLVBE(wire.ODirTLVMaidenName, ""),
  2502. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  2503. wire.NewTLVBE(wire.ODirTLVState, "state"),
  2504. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  2505. },
  2506. },
  2507. },
  2508. },
  2509. },
  2510. },
  2511. },
  2512. },
  2513. {
  2514. name: "set directory info, receive error from locate svc",
  2515. me: newTestSession("me"),
  2516. givenCmd: []byte(`toc_set_dir "first name":"middle name":"last name":"maiden name":"city":"state":"country":"email":"allow web searches"`),
  2517. mockParams: mockParams{
  2518. locateParams: locateParams{
  2519. setDirInfoParams: setDirInfoParams{
  2520. {
  2521. me: state.NewIdentScreenName("me"),
  2522. inBody: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  2523. TLVRestBlock: wire.TLVRestBlock{
  2524. TLVList: wire.TLVList{
  2525. wire.NewTLVBE(wire.ODirTLVFirstName, "first name"),
  2526. wire.NewTLVBE(wire.ODirTLVMiddleName, "middle name"),
  2527. wire.NewTLVBE(wire.ODirTLVLastName, "last name"),
  2528. wire.NewTLVBE(wire.ODirTLVMaidenName, "maiden name"),
  2529. wire.NewTLVBE(wire.ODirTLVCountry, "country"),
  2530. wire.NewTLVBE(wire.ODirTLVState, "state"),
  2531. wire.NewTLVBE(wire.ODirTLVCity, "city"),
  2532. },
  2533. },
  2534. },
  2535. err: io.EOF,
  2536. },
  2537. },
  2538. },
  2539. },
  2540. wantMsg: cmdInternalSvcErr,
  2541. },
  2542. {
  2543. name: "set directory with too many fields present",
  2544. me: newTestSession("me"),
  2545. givenCmd: []byte(`toc_set_dir "first name"::"last name"::"city":"state":"country":"email":"allow web searches":"extra":"extra"`),
  2546. wantMsg: cmdInternalSvcErr,
  2547. },
  2548. {
  2549. name: "bad command",
  2550. givenCmd: []byte(`toc_set_dir`),
  2551. wantMsg: cmdInternalSvcErr,
  2552. },
  2553. }
  2554. for _, tc := range cases {
  2555. t.Run(tc.name, func(t *testing.T) {
  2556. ctx := context.Background()
  2557. locateSvc := newMockLocateService(t)
  2558. for _, params := range tc.mockParams.setDirInfoParams {
  2559. locateSvc.EXPECT().
  2560. SetDirInfo(ctx, matchSession(params.me), wire.SNACFrame{}, params.inBody).
  2561. Return(params.msg, params.err)
  2562. }
  2563. svc := OSCARProxy{
  2564. Logger: slog.Default(),
  2565. LocateService: locateSvc,
  2566. }
  2567. msg := svc.SetDir(ctx, tc.me, tc.givenCmd)
  2568. assert.Equal(t, tc.wantMsg, msg)
  2569. })
  2570. }
  2571. }
  2572. func TestOSCARProxy_SetIdle(t *testing.T) {
  2573. cases := []struct {
  2574. // name is the unit test name
  2575. name string
  2576. // me is the TOC user session
  2577. me *state.Session
  2578. // givenCmd is the TOC command
  2579. givenCmd []byte
  2580. // wantMsg is the expected TOC response
  2581. wantMsg string
  2582. // mockParams is the list of params sent to mocks that satisfy this
  2583. // method's dependencies
  2584. mockParams mockParams
  2585. }{
  2586. {
  2587. name: "successfully set idle status",
  2588. me: newTestSession("me"),
  2589. givenCmd: []byte(`toc_set_idle 10`),
  2590. mockParams: mockParams{
  2591. oServiceBOSParams: oServiceParams{
  2592. idleNotificationParams: idleNotificationParams{
  2593. {
  2594. me: state.NewIdentScreenName("me"),
  2595. bodyIn: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  2596. IdleTime: uint32(10),
  2597. },
  2598. },
  2599. },
  2600. },
  2601. },
  2602. },
  2603. {
  2604. name: "set idle status, receive err from BOS oservice svc",
  2605. me: newTestSession("me"),
  2606. givenCmd: []byte(`toc_set_idle 10`),
  2607. mockParams: mockParams{
  2608. oServiceBOSParams: oServiceParams{
  2609. idleNotificationParams: idleNotificationParams{
  2610. {
  2611. me: state.NewIdentScreenName("me"),
  2612. bodyIn: wire.SNAC_0x01_0x11_OServiceIdleNotification{
  2613. IdleTime: uint32(10),
  2614. },
  2615. err: io.EOF,
  2616. },
  2617. },
  2618. },
  2619. },
  2620. wantMsg: cmdInternalSvcErr,
  2621. },
  2622. {
  2623. name: "bad secs param",
  2624. givenCmd: []byte(`toc_set_idle zero`),
  2625. wantMsg: cmdInternalSvcErr,
  2626. },
  2627. {
  2628. name: "bad command",
  2629. givenCmd: []byte(`toc_set_idle`),
  2630. wantMsg: cmdInternalSvcErr,
  2631. },
  2632. }
  2633. for _, tc := range cases {
  2634. t.Run(tc.name, func(t *testing.T) {
  2635. ctx := context.Background()
  2636. oServiceSvc := newMockOServiceService(t)
  2637. for _, params := range tc.mockParams.oServiceBOSParams.idleNotificationParams {
  2638. oServiceSvc.EXPECT().
  2639. IdleNotification(ctx, matchSession(params.me), params.bodyIn).
  2640. Return(params.err)
  2641. }
  2642. svc := OSCARProxy{
  2643. Logger: slog.Default(),
  2644. OServiceServiceBOS: oServiceSvc,
  2645. }
  2646. msg := svc.SetIdle(ctx, tc.me, tc.givenCmd)
  2647. assert.Equal(t, tc.wantMsg, msg)
  2648. })
  2649. }
  2650. }
  2651. func TestOSCARProxy_SetInfo(t *testing.T) {
  2652. cases := []struct {
  2653. // name is the unit test name
  2654. name string
  2655. // me is the TOC user session
  2656. me *state.Session
  2657. // givenCmd is the TOC command
  2658. givenCmd []byte
  2659. // wantMsg is the expected TOC response
  2660. wantMsg string
  2661. // mockParams is the list of params sent to mocks that satisfy this
  2662. // method's dependencies
  2663. mockParams mockParams
  2664. }{
  2665. {
  2666. name: "successfully set profile",
  2667. me: newTestSession("me"),
  2668. givenCmd: []byte(`toc_set_info "my profile!"`),
  2669. mockParams: mockParams{
  2670. locateParams: locateParams{
  2671. setInfoParams: setInfoParams{
  2672. {
  2673. me: state.NewIdentScreenName("me"),
  2674. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2675. TLVRestBlock: wire.TLVRestBlock{
  2676. TLVList: wire.TLVList{
  2677. wire.NewTLVBE(wire.LocateTLVTagsInfoSigData, "my profile!"),
  2678. },
  2679. },
  2680. },
  2681. },
  2682. },
  2683. },
  2684. },
  2685. },
  2686. {
  2687. name: "set profile, receive error from locate svc",
  2688. me: newTestSession("me"),
  2689. givenCmd: []byte(`toc_set_info "my profile!"`),
  2690. mockParams: mockParams{
  2691. locateParams: locateParams{
  2692. setInfoParams: setInfoParams{
  2693. {
  2694. me: state.NewIdentScreenName("me"),
  2695. inBody: wire.SNAC_0x02_0x04_LocateSetInfo{
  2696. TLVRestBlock: wire.TLVRestBlock{
  2697. TLVList: wire.TLVList{
  2698. wire.NewTLVBE(wire.LocateTLVTagsInfoSigData, "my profile!"),
  2699. },
  2700. },
  2701. },
  2702. err: io.EOF,
  2703. },
  2704. },
  2705. },
  2706. },
  2707. wantMsg: cmdInternalSvcErr,
  2708. },
  2709. {
  2710. name: "bad command",
  2711. givenCmd: []byte(`toc_set_info`),
  2712. wantMsg: cmdInternalSvcErr,
  2713. },
  2714. }
  2715. for _, tc := range cases {
  2716. t.Run(tc.name, func(t *testing.T) {
  2717. ctx := context.Background()
  2718. locateSvc := newMockLocateService(t)
  2719. for _, params := range tc.mockParams.setInfoParams {
  2720. locateSvc.EXPECT().
  2721. SetInfo(ctx, matchSession(params.me), params.inBody).
  2722. Return(params.err)
  2723. }
  2724. svc := OSCARProxy{
  2725. Logger: slog.Default(),
  2726. LocateService: locateSvc,
  2727. }
  2728. msg := svc.SetInfo(ctx, tc.me, tc.givenCmd)
  2729. assert.Equal(t, tc.wantMsg, msg)
  2730. })
  2731. }
  2732. }
  2733. func TestOSCARProxy_Signon(t *testing.T) {
  2734. roastedPass := wire.RoastTOCPassword([]byte("thepass"))
  2735. cases := []struct {
  2736. // name is the unit test name
  2737. name string
  2738. // me is the TOC user session
  2739. me *state.Session
  2740. // givenCmd is the TOC command
  2741. givenCmd []byte
  2742. // wantMsg is the expected TOC response
  2743. wantMsg []string
  2744. // mockParams is the list of params sent to mocks that satisfy this
  2745. // method's dependencies
  2746. mockParams mockParams
  2747. }{
  2748. {
  2749. name: "successfully login",
  2750. me: newTestSession("me", func(session *state.Session) {
  2751. session.SetCaps([][16]byte{capChat})
  2752. }),
  2753. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  2754. mockParams: mockParams{
  2755. authParams: authParams{
  2756. flapLoginParams: flapLoginParams{
  2757. {
  2758. frame: wire.FLAPSignonFrame{
  2759. TLVRestBlock: wire.TLVRestBlock{
  2760. TLVList: wire.TLVList{
  2761. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  2762. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  2763. },
  2764. },
  2765. },
  2766. newUserFn: state.NewStubUser,
  2767. tlv: wire.TLVRestBlock{
  2768. TLVList: wire.TLVList{
  2769. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  2770. },
  2771. },
  2772. },
  2773. },
  2774. registerBOSSessionParams: registerBOSSessionParams{
  2775. {
  2776. authCookie: []byte("thecookie"),
  2777. sess: newTestSession("me"),
  2778. },
  2779. },
  2780. },
  2781. buddyListRegistryParams: buddyListRegistryParams{
  2782. registerBuddyListParams: registerBuddyListParams{
  2783. {
  2784. user: state.NewIdentScreenName("me"),
  2785. },
  2786. },
  2787. },
  2788. tocConfigParams: tocConfigParams{
  2789. userParams: userParams{
  2790. {
  2791. screenName: state.NewIdentScreenName("me"),
  2792. returnedUser: &state.User{
  2793. TOCConfig: "my-toc-config",
  2794. },
  2795. },
  2796. },
  2797. },
  2798. },
  2799. wantMsg: []string{"SIGN_ON:TOC1.0", "CONFIG:my-toc-config"},
  2800. },
  2801. {
  2802. name: "login, receive error from auth svc FLAP login",
  2803. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  2804. mockParams: mockParams{
  2805. authParams: authParams{
  2806. flapLoginParams: flapLoginParams{
  2807. {
  2808. frame: wire.FLAPSignonFrame{
  2809. TLVRestBlock: wire.TLVRestBlock{
  2810. TLVList: wire.TLVList{
  2811. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  2812. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  2813. },
  2814. },
  2815. },
  2816. newUserFn: state.NewStubUser,
  2817. err: io.EOF,
  2818. },
  2819. },
  2820. },
  2821. },
  2822. wantMsg: []string{string(cmdInternalSvcErr)},
  2823. },
  2824. {
  2825. name: "login, receive error from auth svc registration",
  2826. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  2827. mockParams: mockParams{
  2828. authParams: authParams{
  2829. flapLoginParams: flapLoginParams{
  2830. {
  2831. frame: wire.FLAPSignonFrame{
  2832. TLVRestBlock: wire.TLVRestBlock{
  2833. TLVList: wire.TLVList{
  2834. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  2835. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  2836. },
  2837. },
  2838. },
  2839. newUserFn: state.NewStubUser,
  2840. tlv: wire.TLVRestBlock{
  2841. TLVList: wire.TLVList{
  2842. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  2843. },
  2844. },
  2845. },
  2846. },
  2847. registerBOSSessionParams: registerBOSSessionParams{
  2848. {
  2849. authCookie: []byte("thecookie"),
  2850. err: io.EOF,
  2851. },
  2852. },
  2853. },
  2854. },
  2855. wantMsg: []string{string(cmdInternalSvcErr)},
  2856. },
  2857. {
  2858. name: "login, receive error from buddy list registry",
  2859. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  2860. mockParams: mockParams{
  2861. authParams: authParams{
  2862. flapLoginParams: flapLoginParams{
  2863. {
  2864. frame: wire.FLAPSignonFrame{
  2865. TLVRestBlock: wire.TLVRestBlock{
  2866. TLVList: wire.TLVList{
  2867. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  2868. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  2869. },
  2870. },
  2871. },
  2872. newUserFn: state.NewStubUser,
  2873. tlv: wire.TLVRestBlock{
  2874. TLVList: wire.TLVList{
  2875. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  2876. },
  2877. },
  2878. },
  2879. },
  2880. registerBOSSessionParams: registerBOSSessionParams{
  2881. {
  2882. authCookie: []byte("thecookie"),
  2883. sess: newTestSession("me"),
  2884. },
  2885. },
  2886. },
  2887. buddyListRegistryParams: buddyListRegistryParams{
  2888. registerBuddyListParams: registerBuddyListParams{
  2889. {
  2890. user: state.NewIdentScreenName("me"),
  2891. err: io.EOF,
  2892. },
  2893. },
  2894. },
  2895. },
  2896. wantMsg: []string{string(cmdInternalSvcErr)},
  2897. },
  2898. {
  2899. name: "login, receive error from TOC config store",
  2900. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  2901. mockParams: mockParams{
  2902. authParams: authParams{
  2903. flapLoginParams: flapLoginParams{
  2904. {
  2905. frame: wire.FLAPSignonFrame{
  2906. TLVRestBlock: wire.TLVRestBlock{
  2907. TLVList: wire.TLVList{
  2908. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  2909. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  2910. },
  2911. },
  2912. },
  2913. newUserFn: state.NewStubUser,
  2914. tlv: wire.TLVRestBlock{
  2915. TLVList: wire.TLVList{
  2916. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  2917. },
  2918. },
  2919. },
  2920. },
  2921. registerBOSSessionParams: registerBOSSessionParams{
  2922. {
  2923. authCookie: []byte("thecookie"),
  2924. sess: newTestSession("me"),
  2925. },
  2926. },
  2927. },
  2928. buddyListRegistryParams: buddyListRegistryParams{
  2929. registerBuddyListParams: registerBuddyListParams{
  2930. {
  2931. user: state.NewIdentScreenName("me"),
  2932. },
  2933. },
  2934. },
  2935. tocConfigParams: tocConfigParams{
  2936. userParams: userParams{
  2937. {
  2938. screenName: state.NewIdentScreenName("me"),
  2939. err: io.EOF,
  2940. },
  2941. },
  2942. },
  2943. },
  2944. wantMsg: []string{string(cmdInternalSvcErr)},
  2945. },
  2946. {
  2947. name: "login, user not found after login",
  2948. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  2949. mockParams: mockParams{
  2950. authParams: authParams{
  2951. flapLoginParams: flapLoginParams{
  2952. {
  2953. frame: wire.FLAPSignonFrame{
  2954. TLVRestBlock: wire.TLVRestBlock{
  2955. TLVList: wire.TLVList{
  2956. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  2957. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  2958. },
  2959. },
  2960. },
  2961. newUserFn: state.NewStubUser,
  2962. tlv: wire.TLVRestBlock{
  2963. TLVList: wire.TLVList{
  2964. wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte("thecookie")),
  2965. },
  2966. },
  2967. },
  2968. },
  2969. registerBOSSessionParams: registerBOSSessionParams{
  2970. {
  2971. authCookie: []byte("thecookie"),
  2972. sess: newTestSession("me"),
  2973. },
  2974. },
  2975. },
  2976. buddyListRegistryParams: buddyListRegistryParams{
  2977. registerBuddyListParams: registerBuddyListParams{
  2978. {
  2979. user: state.NewIdentScreenName("me"),
  2980. },
  2981. },
  2982. },
  2983. tocConfigParams: tocConfigParams{
  2984. userParams: userParams{
  2985. {
  2986. screenName: state.NewIdentScreenName("me"),
  2987. returnedUser: nil,
  2988. },
  2989. },
  2990. },
  2991. },
  2992. wantMsg: []string{string(cmdInternalSvcErr)},
  2993. },
  2994. {
  2995. name: "login with bad credentials",
  2996. givenCmd: []byte(`toc_signon "" "" me "xx` + hex.EncodeToString(roastedPass) + `"`),
  2997. mockParams: mockParams{
  2998. authParams: authParams{
  2999. flapLoginParams: flapLoginParams{
  3000. {
  3001. frame: wire.FLAPSignonFrame{
  3002. TLVRestBlock: wire.TLVRestBlock{
  3003. TLVList: wire.TLVList{
  3004. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  3005. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, roastedPass),
  3006. },
  3007. },
  3008. },
  3009. newUserFn: state.NewStubUser,
  3010. tlv: wire.TLVRestBlock{
  3011. TLVList: wire.TLVList{
  3012. wire.NewTLVBE(wire.LoginTLVTagsErrorSubcode, wire.LoginErrInvalidUsernameOrPassword),
  3013. },
  3014. },
  3015. },
  3016. },
  3017. },
  3018. },
  3019. wantMsg: []string{"ERROR:980"},
  3020. },
  3021. {
  3022. name: "bad command",
  3023. givenCmd: []byte(`toc_init_done_diff`),
  3024. wantMsg: []string{string(cmdInternalSvcErr)},
  3025. },
  3026. }
  3027. for _, tc := range cases {
  3028. t.Run(tc.name, func(t *testing.T) {
  3029. ctx := context.Background()
  3030. authSvc := newMockAuthService(t)
  3031. for _, params := range tc.mockParams.flapLoginParams {
  3032. authSvc.EXPECT().
  3033. FLAPLogin(params.frame, mock.Anything).
  3034. Return(params.tlv, params.err)
  3035. }
  3036. for _, params := range tc.mockParams.registerBOSSessionParams {
  3037. authSvc.EXPECT().
  3038. RegisterBOSSession(ctx, params.authCookie).
  3039. Return(params.sess, params.err)
  3040. }
  3041. buddyRegistry := newMockBuddyListRegistry(t)
  3042. for _, params := range tc.mockParams.registerBuddyListParams {
  3043. buddyRegistry.EXPECT().
  3044. RegisterBuddyList(params.user).
  3045. Return(params.err)
  3046. }
  3047. tocCfg := newMockTOCConfigStore(t)
  3048. for _, params := range tc.mockParams.userParams {
  3049. tocCfg.EXPECT().
  3050. User(params.screenName).
  3051. Return(params.returnedUser, params.err)
  3052. }
  3053. svc := OSCARProxy{
  3054. AuthService: authSvc,
  3055. BuddyListRegistry: buddyRegistry,
  3056. Logger: slog.Default(),
  3057. TOCConfigStore: tocCfg,
  3058. }
  3059. sess, msg := svc.Signon(ctx, tc.givenCmd)
  3060. assert.Equal(t, tc.wantMsg, msg)
  3061. if tc.me == nil {
  3062. assert.Nil(t, sess)
  3063. } else if assert.NotNil(t, sess) {
  3064. assert.Equal(t, tc.me.IdentScreenName(), sess.IdentScreenName())
  3065. assert.Equal(t, tc.me.Caps(), sess.Caps())
  3066. }
  3067. })
  3068. }
  3069. }
  3070. func TestOSCARProxy_Signout(t *testing.T) {
  3071. cases := []struct {
  3072. // name is the unit test name
  3073. name string
  3074. // me is the TOC user session
  3075. me *state.Session
  3076. // mockParams is the list of params sent to mocks that satisfy this
  3077. // method's dependencies
  3078. mockParams mockParams
  3079. }{
  3080. {
  3081. name: "successfully sign out",
  3082. me: newTestSession("me"),
  3083. mockParams: mockParams{
  3084. buddyParams: buddyParams{
  3085. broadcastBuddyDepartedParams: broadcastBuddyDepartedParams{
  3086. {
  3087. me: state.NewIdentScreenName("me"),
  3088. },
  3089. },
  3090. },
  3091. buddyListRegistryParams: buddyListRegistryParams{
  3092. unregisterBuddyListParams: unregisterBuddyListParams{
  3093. {
  3094. user: state.NewIdentScreenName("me"),
  3095. },
  3096. },
  3097. },
  3098. authParams: authParams{
  3099. signoutParams: signoutParams{
  3100. {
  3101. me: state.NewIdentScreenName("me"),
  3102. },
  3103. },
  3104. },
  3105. },
  3106. },
  3107. {
  3108. name: "sign out, receive error from buddy service",
  3109. me: newTestSession("me"),
  3110. mockParams: mockParams{
  3111. buddyParams: buddyParams{
  3112. broadcastBuddyDepartedParams: broadcastBuddyDepartedParams{
  3113. {
  3114. me: state.NewIdentScreenName("me"),
  3115. err: io.EOF,
  3116. },
  3117. },
  3118. },
  3119. buddyListRegistryParams: buddyListRegistryParams{
  3120. unregisterBuddyListParams: unregisterBuddyListParams{
  3121. {
  3122. user: state.NewIdentScreenName("me"),
  3123. },
  3124. },
  3125. },
  3126. authParams: authParams{
  3127. signoutParams: signoutParams{
  3128. {
  3129. me: state.NewIdentScreenName("me"),
  3130. },
  3131. },
  3132. },
  3133. },
  3134. },
  3135. {
  3136. name: "sign out, receive error from buddy list registry",
  3137. me: newTestSession("me"),
  3138. mockParams: mockParams{
  3139. buddyParams: buddyParams{
  3140. broadcastBuddyDepartedParams: broadcastBuddyDepartedParams{
  3141. {
  3142. me: state.NewIdentScreenName("me"),
  3143. },
  3144. },
  3145. },
  3146. buddyListRegistryParams: buddyListRegistryParams{
  3147. unregisterBuddyListParams: unregisterBuddyListParams{
  3148. {
  3149. user: state.NewIdentScreenName("me"),
  3150. err: io.EOF,
  3151. },
  3152. },
  3153. },
  3154. authParams: authParams{
  3155. signoutParams: signoutParams{
  3156. {
  3157. me: state.NewIdentScreenName("me"),
  3158. },
  3159. },
  3160. },
  3161. },
  3162. },
  3163. }
  3164. for _, tc := range cases {
  3165. t.Run(tc.name, func(t *testing.T) {
  3166. ctx := context.Background()
  3167. buddySvc := newMockBuddyService(t)
  3168. for _, params := range tc.mockParams.broadcastBuddyDepartedParams {
  3169. buddySvc.EXPECT().
  3170. BroadcastBuddyDeparted(ctx, matchSession(params.me)).
  3171. Return(params.err)
  3172. }
  3173. buddyListSvc := newMockBuddyListRegistry(t)
  3174. for _, params := range tc.mockParams.unregisterBuddyListParams {
  3175. buddyListSvc.EXPECT().
  3176. UnregisterBuddyList(params.user).
  3177. Return(params.err)
  3178. }
  3179. authSvc := newMockAuthService(t)
  3180. for _, params := range tc.mockParams.signoutParams {
  3181. authSvc.EXPECT().Signout(ctx, matchSession(params.me))
  3182. }
  3183. svc := OSCARProxy{
  3184. AuthService: authSvc,
  3185. BuddyListRegistry: buddyListSvc,
  3186. BuddyService: buddySvc,
  3187. Logger: slog.Default(),
  3188. }
  3189. svc.Signout(ctx, tc.me)
  3190. })
  3191. }
  3192. }
  3193. func Test_parseArgs(t *testing.T) {
  3194. type testCase struct {
  3195. name string
  3196. givenPayload string
  3197. givenCmd string
  3198. givenArgs []*string
  3199. wantVarArgs []string
  3200. wantArgs []string
  3201. wantErrMsg string
  3202. }
  3203. tests := []testCase{
  3204. {
  3205. name: "no positional args or varargs",
  3206. givenPayload: `toc_chat_invite`,
  3207. givenCmd: "toc_chat_invite",
  3208. givenArgs: nil,
  3209. wantVarArgs: nil,
  3210. },
  3211. {
  3212. name: "positional args with varargs",
  3213. givenPayload: `toc_chat_invite 1234 "Join me!" user1 user2 user3`,
  3214. givenCmd: "toc_chat_invite",
  3215. givenArgs: []*string{new(string), new(string)},
  3216. wantVarArgs: []string{"user1", "user2", "user3"},
  3217. wantArgs: []string{"1234", "Join me!"},
  3218. },
  3219. {
  3220. name: "nil positional argument placeholders should get skipped",
  3221. givenPayload: `toc_chat_invite 1234 "Join me!" user1 user2 user3`,
  3222. givenCmd: "toc_chat_invite",
  3223. givenArgs: []*string{nil, nil}, // still 2 placeholders, both nil
  3224. wantVarArgs: []string{"user1", "user2", "user3"},
  3225. wantArgs: []string{"", ""},
  3226. },
  3227. {
  3228. name: "positional args with no varargs",
  3229. givenPayload: `toc_chat_invite 1234 "Join me!"`,
  3230. givenCmd: "toc_chat_invite",
  3231. givenArgs: []*string{new(string), new(string)}, // roomID + msg
  3232. wantVarArgs: nil,
  3233. wantArgs: []string{"1234", "Join me!"},
  3234. },
  3235. {
  3236. name: "varargs only",
  3237. givenPayload: `toc_chat_invite user1 user2 user3`,
  3238. givenCmd: "toc_chat_invite",
  3239. givenArgs: nil,
  3240. wantVarArgs: []string{"user1", "user2", "user3"},
  3241. },
  3242. {
  3243. name: "command mismatch",
  3244. givenPayload: `toc_chat_invite user1 user2 user3`,
  3245. givenCmd: "toc_chat_accept",
  3246. givenArgs: nil,
  3247. wantVarArgs: nil,
  3248. wantErrMsg: "mismatch",
  3249. },
  3250. {
  3251. name: "too many positional arg placeholders",
  3252. givenPayload: `toc_chat_invite`,
  3253. givenCmd: "toc_chat_invite",
  3254. givenArgs: []*string{new(string), new(string)},
  3255. wantVarArgs: nil,
  3256. wantErrMsg: "command contains fewer arguments than expected",
  3257. },
  3258. {
  3259. name: "CSV parser error",
  3260. givenPayload: ``,
  3261. givenCmd: "toc_chat_invite",
  3262. givenArgs: []*string{nil},
  3263. wantVarArgs: nil,
  3264. wantErrMsg: "CSV reader error",
  3265. },
  3266. }
  3267. for _, tt := range tests {
  3268. t.Run(tt.name, func(t *testing.T) {
  3269. varArgs, err := parseArgs([]byte(tt.givenPayload), tt.givenCmd, tt.givenArgs...)
  3270. if tt.wantErrMsg != "" {
  3271. assert.ErrorContains(t, err, tt.wantErrMsg)
  3272. return
  3273. }
  3274. assert.NoError(t, err)
  3275. // verify the placeholder pointers got populated
  3276. for i, want := range tt.wantArgs {
  3277. if want == "" {
  3278. assert.Nil(t, tt.givenArgs[i])
  3279. } else {
  3280. got := *tt.givenArgs[i]
  3281. assert.Equal(t, want, got)
  3282. }
  3283. }
  3284. // verify we have the same varargs
  3285. assert.Equal(t, tt.wantVarArgs, varArgs)
  3286. assert.Equal(t, len(tt.wantArgs), len(tt.givenArgs))
  3287. })
  3288. }
  3289. }