| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313 |
- package toc
- import (
- "context"
- "log/slog"
- "net"
- "sync"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/mock"
- "github.com/mk6i/open-oscar-server/state"
- "github.com/mk6i/open-oscar-server/wire"
- )
- func TestOSCARProxy_RecvBOS_ChatIn(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // me is the TOC user session
- me *state.SessionInstance
- // chatID is the chat ID
- chatID int
- // givenMsg is the incoming SNAC
- givenMsg wire.SNACMessage
- // wantCmd is the expected TOC response
- wantCmd string
- }{
- {
- name: "send chat message - plain CHAT_IN",
- me: newTestSession("me"),
- chatID: 0,
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVSenderInformation, wire.TLVUserInfo{
- ScreenName: "them",
- }),
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText, "<p>hello world!</p>"),
- },
- }),
- },
- },
- },
- },
- wantCmd: "CHAT_IN:0:them:F:<p>hello world!</p>",
- },
- {
- name: "send chat message - TOC2 encoded CHAT_IN_ENC",
- me: newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(true) }),
- chatID: 0,
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVSenderInformation, wire.TLVUserInfo{
- ScreenName: "them",
- }),
- wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ChatTLVMessageInfoText, "<p>hello world!</p>"),
- },
- }),
- },
- },
- },
- },
- wantCmd: "CHAT_IN_ENC:0:them:F:A:en:<p>hello world!</p>",
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- svc := OSCARProxy{
- Logger: slog.Default(),
- }
- ch := make(chan []string)
- wg := &sync.WaitGroup{}
- wg.Add(1)
- go func() {
- defer wg.Done()
- svc.RecvChat(ctx, tc.me, tc.chatID, ch)
- }()
- status := tc.me.RelayMessageToInstance(tc.givenMsg)
- assert.Equal(t, state.SessSendOK, status)
- gotCmd := <-ch
- assert.Equal(t, tc.wantCmd, gotCmd[0])
- cancel()
- wg.Wait()
- })
- }
- }
- func TestOSCARProxy_RecvBOS_ChatUpdateBuddyArrived(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // me is the TOC user session
- me *state.SessionInstance
- // chatID is the chat ID
- chatID int
- // givenMsg is the incoming SNAC
- givenMsg wire.SNACMessage
- // wantCmd is the expected TOC response
- wantCmd []string
- }{
- {
- name: "send chat participant arrival",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x0E_0x03_ChatUsersJoined{
- Users: []wire.TLVUserInfo{
- {ScreenName: "user1"},
- {ScreenName: "user2"},
- },
- },
- },
- wantCmd: []string{"CHAT_UPDATE_BUDDY:0:T:user1:user2"},
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- svc := OSCARProxy{
- Logger: slog.Default(),
- }
- ch := make(chan []string)
- wg := &sync.WaitGroup{}
- wg.Add(1)
- go func() {
- defer wg.Done()
- svc.RecvChat(ctx, tc.me, tc.chatID, ch)
- }()
- status := tc.me.RelayMessageToInstance(tc.givenMsg)
- assert.Equal(t, state.SessSendOK, status)
- gotCmd := <-ch
- assert.Equal(t, tc.wantCmd[0], gotCmd[0])
- cancel()
- wg.Wait()
- })
- }
- }
- func TestOSCARProxy_RecvBOS_ChatUpdateBuddyLeft(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // me is the TOC user session
- me *state.SessionInstance
- // chatID is the chat ID
- chatID int
- // givenMsg is the incoming SNAC
- givenMsg wire.SNACMessage
- // wantCmd is the expected TOC response
- wantCmd []string
- }{
- {
- name: "send chat participant departure",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x0E_0x04_ChatUsersLeft{
- Users: []wire.TLVUserInfo{
- {ScreenName: "user1"},
- {ScreenName: "user2"},
- },
- },
- },
- wantCmd: []string{"CHAT_UPDATE_BUDDY:0:F:user1:user2"},
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- svc := OSCARProxy{
- Logger: slog.Default(),
- }
- ch := make(chan []string)
- wg := &sync.WaitGroup{}
- wg.Add(1)
- go func() {
- defer wg.Done()
- svc.RecvChat(ctx, tc.me, tc.chatID, ch)
- }()
- status := tc.me.RelayMessageToInstance(tc.givenMsg)
- assert.Equal(t, state.SessSendOK, status)
- gotCmd := <-ch
- assert.Equal(t, tc.wantCmd[0], gotCmd[0])
- cancel()
- wg.Wait()
- })
- }
- }
- func TestOSCARProxy_RecvBOS_Eviled(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // me is the TOC user session
- me *state.SessionInstance
- // givenMsg is the incoming SNAC
- givenMsg wire.SNACMessage
- // chatRegistry is the chat registry for the current session
- chatRegistry *ChatRegistry
- // wantCmd is the expected TOC response
- wantCmd []string
- }{
- {
- name: "anonymous warning - 10%",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x01_0x10_OServiceEvilNotification{
- NewEvil: 100,
- },
- },
- wantCmd: []string{"EVILED:10:"},
- },
- {
- name: "normal warning - 10%",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x01_0x10_OServiceEvilNotification{
- NewEvil: 100,
- Snitcher: &struct {
- wire.TLVUserInfo
- }{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "them",
- },
- },
- },
- },
- wantCmd: []string{"EVILED:10:them"},
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- svc := testOSCARProxy(t)
- ch := make(chan []string)
- wg := &sync.WaitGroup{}
- wg.Add(1)
- go func() {
- defer wg.Done()
- err := svc.RecvBOS(ctx, tc.me, NewChatRegistry(), ch)
- assert.NoError(t, err)
- }()
- status := tc.me.RelayMessageToInstance(tc.givenMsg)
- assert.Equal(t, state.SessSendOK, status)
- gotCmd := <-ch
- assert.Equal(t, tc.wantCmd[0], gotCmd[0])
- cancel()
- wg.Wait()
- })
- }
- }
- func TestOSCARProxy_RecvBOS_IMIn(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // me is the TOC user session
- me *state.SessionInstance
- // givenMsg is the incoming SNAC
- givenMsg wire.SNACMessage
- // wantCmd is the expected TOC response
- wantCmd []string
- }{
- {
- name: "send IM",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x04_0x07_ICBMChannelMsgToClient{
- ChannelID: wire.ICBMChannelIM,
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "them",
- },
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
- {
- ID: 0x5,
- Version: 0x1,
- Payload: []uint8{0x1, 0x1, 0x2},
- },
- {
- ID: 0x1,
- Version: 0x1,
- Payload: []uint8{
- 0x0, 0x0, // charset
- 0x0, 0x0, // lang
- 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
- },
- },
- }),
- },
- },
- },
- },
- wantCmd: []string{"IM_IN:them:F:hello world!"},
- },
- {
- name: "send IM - auto-response",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x04_0x07_ICBMChannelMsgToClient{
- ChannelID: wire.ICBMChannelIM,
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "them",
- },
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ICBMTLVAutoResponse, []byte{}),
- wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
- {
- ID: 0x5,
- Version: 0x1,
- Payload: []uint8{0x1, 0x1, 0x2},
- },
- {
- ID: 0x1,
- Version: 0x1,
- Payload: []uint8{
- 0x0, 0x0, // charset
- 0x0, 0x0, // lang
- 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
- },
- },
- }),
- },
- },
- },
- },
- wantCmd: []string{"IM_IN:them:T:hello world!"},
- },
- {
- name: "send IM - TOC2 (IM_IN2)",
- me: newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(false) }),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x04_0x07_ICBMChannelMsgToClient{
- ChannelID: wire.ICBMChannelIM,
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "them",
- },
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
- {ID: 0x5, Version: 0x1, Payload: []uint8{0x1, 0x1, 0x2}},
- {ID: 0x1, Version: 0x1, Payload: []uint8{0x0, 0x0, 0x0, 0x0, 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'}},
- }),
- },
- },
- },
- },
- wantCmd: []string{"IM_IN2:them:F:F:hello world!"},
- },
- {
- name: "send IM - TOC2 with encoded messaging (IM_IN_ENC2)",
- me: newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(true) }),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x04_0x07_ICBMChannelMsgToClient{
- ChannelID: wire.ICBMChannelIM,
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "them",
- TLVBlock: wire.TLVBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagOSCARFree),
- },
- },
- },
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
- {ID: 0x5, Version: 0x1, Payload: []uint8{0x1, 0x1, 0x2}},
- {ID: 0x1, Version: 0x1, Payload: []uint8{0x0, 0x0, 0x0, 0x0, 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'}},
- }),
- },
- },
- },
- },
- wantCmd: []string{"IM_IN_ENC2:them:F:F:T: O :F:L:en:hello world!"},
- },
- {
- name: "send IM - TOC2 encoded messaging missing OServiceUserInfoUserFlags returns empty",
- me: newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(true) }),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x04_0x07_ICBMChannelMsgToClient{
- ChannelID: wire.ICBMChannelIM,
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "them",
- // no TLVList / OServiceUserInfoUserFlags
- },
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ICBMTLVAOLIMData, []wire.ICBMCh1Fragment{
- {ID: 0x5, Version: 0x1, Payload: []uint8{0x1, 0x1, 0x2}},
- {ID: 0x1, Version: 0x1, Payload: []uint8{0x0, 0x0, 0x0, 0x0, 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'}},
- }),
- },
- },
- },
- },
- wantCmd: []string{"IM_IN_ENC2:them:F:F:T: :F:L:en:hello world!"},
- },
- {
- name: "send chat invitation",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x04_0x07_ICBMChannelMsgToClient{
- ChannelID: wire.ICBMChannelRendezvous,
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "them",
- },
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ICBMTLVData, []wire.ICBMCh2Fragment{
- {
- Capability: wire.CapChat,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ICBMRdvTLVTagsInvitation, "join my chat!"),
- wire.NewTLVBE(wire.ICBMRdvTLVTagsSvcData, wire.ICBMRoomInfo{
- Cookie: "a-b-the room",
- }),
- },
- },
- },
- }),
- },
- },
- },
- },
- wantCmd: []string{"CHAT_INVITE:the room:0:them:join my chat!"},
- },
- {
- name: "receive file transfer rendezvous IM",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x04_0x07_ICBMChannelMsgToClient{
- ChannelID: wire.ICBMChannelRendezvous,
- TLVUserInfo: newTestSession("them").Session().TLVUserInfo(),
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ICBMTLVWantEvents, []byte{}),
- wire.NewTLVBE(wire.ICBMTLVData, wire.ICBMCh2Fragment{
- Cookie: [8]byte{'h', 'a', 'h', 'a', 'h', 'a', 'h', 'a'},
- Type: wire.ICBMRdvMessagePropose,
- Capability: wire.CapFileTransfer,
- TLVRestBlock: wire.TLVRestBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.ICBMRdvTLVTagsSeqNum, uint16(1)),
- wire.NewTLVBE(wire.ICBMRdvTLVTagsPort, uint16(4000)),
- wire.NewTLVBE(wire.ICBMRdvTLVTagsRdvIP, net.ParseIP("129.168.0.1").To4()),
- wire.NewTLVBE(wire.ICBMRdvTLVTagsRequesterIP, net.ParseIP("129.168.0.2").To4()),
- wire.NewTLVBE(wire.ICBMRdvTLVTagsVerifiedIP, net.ParseIP("129.168.0.3").To4()),
- wire.NewTLVBE(wire.ICBMRdvTLVTagsSvcData, []byte{'l', 'o', 'l'}),
- },
- },
- }),
- },
- },
- },
- },
- wantCmd: []string{"RVOUS_PROPOSE:them:09461343-4C7F-11D1-8222-444553540000:aGFoYWhhaGE=:1:129.168.0.1:129.168.0.2:129.168.0.3:4000:10001:bG9s"},
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- svc := testOSCARProxy(t)
- ch := make(chan []string)
- wg := &sync.WaitGroup{}
- wg.Add(1)
- go func() {
- defer wg.Done()
- err := svc.RecvBOS(ctx, tc.me, NewChatRegistry(), ch)
- assert.NoError(t, err)
- }()
- status := tc.me.RelayMessageToInstance(tc.givenMsg)
- assert.Equal(t, state.SessSendOK, status)
- gotCmd := <-ch
- assert.Equal(t, tc.wantCmd[0], gotCmd[0])
- cancel()
- wg.Wait()
- })
- }
- }
- func TestOSCARProxy_RecvBOS_UpdateBuddyArrival(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // me is the TOC user session
- me *state.SessionInstance
- // givenMsg is the incoming SNAC
- givenMsg wire.SNACMessage
- // wantCmd is the expected TOC response
- wantCmd []string
- }{
- {
- name: "send buddy arrival - buddy online",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x03_0x0B_BuddyArrived{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "me",
- WarningLevel: 0,
- TLVBlock: wire.TLVBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
- wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
- wire.NewTLVBE(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagOSCARFree),
- },
- },
- },
- },
- },
- wantCmd: []string{"UPDATE_BUDDY:me:T:0:1234:5678: O "},
- },
- {
- name: "send buddy arrival - buddy warned 10%",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x03_0x0B_BuddyArrived{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "me",
- WarningLevel: 100,
- TLVBlock: wire.TLVBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
- wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
- wire.NewTLVBE(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagOSCARFree),
- },
- },
- },
- },
- },
- wantCmd: []string{"UPDATE_BUDDY:me:T:10:1234:5678: O "},
- },
- {
- name: "send buddy arrival - buddy away",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x03_0x0B_BuddyArrived{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "me",
- WarningLevel: 0,
- TLVBlock: wire.TLVBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
- wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
- wire.NewTLVBE(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagOSCARFree|wire.OServiceUserFlagUnavailable),
- },
- },
- },
- },
- },
- wantCmd: []string{"UPDATE_BUDDY:me:T:0:1234:5678: OU"},
- },
- {
- name: "send buddy arrival - user class AOL (userClassString uc[0])",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x03_0x0B_BuddyArrived{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "me",
- WarningLevel: 0,
- TLVBlock: wire.TLVBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
- wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
- wire.NewTLVBE(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagAOL),
- },
- },
- },
- },
- },
- wantCmd: []string{"UPDATE_BUDDY:me:T:0:1234:5678:A "},
- },
- {
- name: "send buddy arrival - user class Administrator (userClassString uc[1])",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x03_0x0B_BuddyArrived{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "me",
- WarningLevel: 0,
- TLVBlock: wire.TLVBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
- wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
- wire.NewTLVBE(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagAdministrator),
- },
- },
- },
- },
- },
- wantCmd: []string{"UPDATE_BUDDY:me:T:0:1234:5678: A "},
- },
- {
- name: "send buddy arrival - user class Wireless (userClassString uc[1])",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x03_0x0B_BuddyArrived{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "me",
- WarningLevel: 0,
- TLVBlock: wire.TLVBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
- wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
- wire.NewTLVBE(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagWireless),
- },
- },
- },
- },
- },
- wantCmd: []string{"UPDATE_BUDDY:me:T:0:1234:5678: C "},
- },
- {
- name: "send buddy arrival - user class Unconfirmed (userClassString uc[1])",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x03_0x0B_BuddyArrived{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "me",
- WarningLevel: 0,
- TLVBlock: wire.TLVBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
- wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
- wire.NewTLVBE(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagUnconfirmed),
- },
- },
- },
- },
- },
- wantCmd: []string{"UPDATE_BUDDY:me:T:0:1234:5678: U "},
- },
- {
- name: "send buddy arrival - TOC2 no caps (userInfoToBuddyCaps returns empty when no OServiceUserInfoOscarCaps TLV)",
- me: newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(false) }),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x03_0x0B_BuddyArrived{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "buddy",
- WarningLevel: 0,
- TLVBlock: wire.TLVBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
- wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
- wire.NewTLVBE(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagOSCARFree),
- },
- },
- },
- },
- },
- wantCmd: []string{"UPDATE_BUDDY2:buddy:T:0:1234:5678: O :"},
- },
- {
- name: "send buddy arrival - TOC2 caps length not divisible by 16 (userInfoToBuddyCaps returns empty)",
- me: newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(false) }),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x03_0x0B_BuddyArrived{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "buddy",
- WarningLevel: 0,
- TLVBlock: wire.TLVBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
- wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
- wire.NewTLVBE(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagOSCARFree),
- // Invalid: 8 bytes, not divisible by 16
- wire.NewTLVBE(wire.OServiceUserInfoOscarCaps, []byte{0x55, 0x0e, 0x84, 0x00, 0xe2, 0x9b, 0x41, 0xd4}),
- },
- },
- },
- },
- },
- wantCmd: []string{"UPDATE_BUDDY2:buddy:T:0:1234:5678: O :"},
- },
- {
- name: "send buddy arrival - TOC2 with one capability (userInfoToBuddyCaps formats caps as UUIDs)",
- me: newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(false) }),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x03_0x0B_BuddyArrived{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "buddy",
- WarningLevel: 0,
- TLVBlock: wire.TLVBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
- wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
- wire.NewTLVBE(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagOSCARFree),
- // One 16-byte cap: UUID 550e8400-e29b-41d4-a716-446655440000
- wire.NewTLVBE(wire.OServiceUserInfoOscarCaps, []byte{
- 0x55, 0x0e, 0x84, 0x00, 0xe2, 0x9b, 0x41, 0xd4,
- 0xa7, 0x16, 0x44, 0x66, 0x55, 0x44, 0x00, 0x00,
- }),
- },
- },
- },
- },
- },
- wantCmd: []string{"UPDATE_BUDDY2:buddy:T:0:1234:5678: O :", "BUDDY_CAPS2:buddy:550e8400-e29b-41d4-a716-446655440000"},
- },
- {
- name: "send buddy arrival - TOC2 with two capabilities",
- me: newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(false) }),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x03_0x0B_BuddyArrived{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "buddy",
- WarningLevel: 0,
- TLVBlock: wire.TLVBlock{
- TLVList: wire.TLVList{
- wire.NewTLVBE(wire.OServiceUserInfoSignonTOD, uint32(1234)),
- wire.NewTLVBE(wire.OServiceUserInfoIdleTime, uint16(5678)),
- wire.NewTLVBE(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagOSCARFree),
- // Two 16-byte caps
- wire.NewTLVBE(wire.OServiceUserInfoOscarCaps, []byte{
- 0x55, 0x0e, 0x84, 0x00, 0xe2, 0x9b, 0x41, 0xd4, 0xa7, 0x16, 0x44, 0x66, 0x55, 0x44, 0x00, 0x00,
- 0x74, 0x8f, 0x24, 0x20, 0x62, 0x87, 0x11, 0xd1, 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00,
- }),
- },
- },
- },
- },
- },
- wantCmd: []string{"UPDATE_BUDDY2:buddy:T:0:1234:5678: O :", "BUDDY_CAPS2:buddy:550e8400-e29b-41d4-a716-446655440000,748f2420-6287-11d1-8222-444553540000"},
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- svc := testOSCARProxy(t)
- ch := make(chan []string)
- wg := &sync.WaitGroup{}
- wg.Add(1)
- go func() {
- defer wg.Done()
- err := svc.RecvBOS(ctx, tc.me, NewChatRegistry(), ch)
- assert.NoError(t, err)
- }()
- status := tc.me.RelayMessageToInstance(tc.givenMsg)
- assert.Equal(t, state.SessSendOK, status)
- gotCmd := <-ch
- assert.Len(t, gotCmd, len(tc.wantCmd))
- for i, want := range tc.wantCmd {
- assert.Equal(t, want, gotCmd[i])
- }
- cancel()
- wg.Wait()
- })
- }
- }
- func TestOSCARProxy_RecvBOS_UpdateBuddyDeparted(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // me is the TOC user session
- me *state.SessionInstance
- // givenMsg is the incoming SNAC
- givenMsg wire.SNACMessage
- // wantCmd is the expected TOC response
- wantCmd []string
- }{
- {
- name: "send buddy departure TOC1",
- me: newTestSession("me"),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x03_0x0C_BuddyDeparted{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "me",
- },
- },
- },
- wantCmd: []string{"UPDATE_BUDDY:me:F:0:0:0: "},
- },
- {
- name: "send buddy departure TOC2",
- me: newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(true) }),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x03_0x0C_BuddyDeparted{
- TLVUserInfo: wire.TLVUserInfo{
- ScreenName: "me",
- },
- },
- },
- wantCmd: []string{"UPDATE_BUDDY2:me:F:0:0:0: :"},
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- svc := testOSCARProxy(t)
- ch := make(chan []string)
- wg := &sync.WaitGroup{}
- wg.Add(1)
- go func() {
- defer wg.Done()
- err := svc.RecvBOS(ctx, tc.me, NewChatRegistry(), ch)
- assert.NoError(t, err)
- }()
- status := tc.me.RelayMessageToInstance(tc.givenMsg)
- assert.Equal(t, state.SessSendOK, status)
- gotCmd := <-ch
- assert.Equal(t, tc.wantCmd[0], gotCmd[0])
- cancel()
- wg.Wait()
- })
- }
- }
- func TestOSCARProxy_RecvBOS_ClientEvent(t *testing.T) {
- cases := []struct {
- name string
- me *state.SessionInstance
- givenMsg wire.SNACMessage
- wantCmd []string
- }{
- {
- name: "TOC2 client receives CLIENT_EVENT2 (typing event)",
- me: newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(false) }),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x04_0x14_ICBMClientEvent{
- ScreenName: "buddy",
- Event: 1, // typing
- },
- },
- wantCmd: []string{"CLIENT_EVENT2:buddy:1"},
- },
- {
- name: "TOC2 client receives CLIENT_EVENT2 event 0 (idle)",
- me: newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(false) }),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x04_0x14_ICBMClientEvent{
- ScreenName: "alice",
- Event: 0,
- },
- },
- wantCmd: []string{"CLIENT_EVENT2:alice:0"},
- },
- {
- name: "TOC2 client receives CLIENT_EVENT2 event 2 (entered text)",
- me: newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(false) }),
- givenMsg: wire.SNACMessage{
- Body: wire.SNAC_0x04_0x14_ICBMClientEvent{
- ScreenName: "bob",
- Event: 2,
- },
- },
- wantCmd: []string{"CLIENT_EVENT2:bob:2"},
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- svc := testOSCARProxy(t)
- ch := make(chan []string)
- wg := &sync.WaitGroup{}
- wg.Add(1)
- go func() {
- defer wg.Done()
- err := svc.RecvBOS(ctx, tc.me, NewChatRegistry(), ch)
- assert.NoError(t, err)
- }()
- status := tc.me.RelayMessageToInstance(tc.givenMsg)
- assert.Equal(t, state.SessSendOK, status)
- gotCmd := <-ch
- assert.Equal(t, tc.wantCmd, gotCmd)
- cancel()
- wg.Wait()
- })
- }
- }
- func TestOSCARProxy_RecvBOS_Signout(t *testing.T) {
- }
- func TestOSCARProxy_RecvBOS_Inserted2(t *testing.T) {
- buddyWithAlias := wire.FeedbagItem{
- ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "alice",
- TLVLBlock: wire.TLVLBlock{},
- }
- buddyWithAlias.Append(wire.NewTLVBE(wire.FeedbagAttributesAlias, []byte("Alice N.")))
- cases := []struct {
- name string
- snac wire.SNAC_0x13_0x09_FeedbagUpdateItem
- wantCmd []string
- mockParams mockParams
- }{
- {
- name: "one buddy - group name from feedbag",
- snac: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "alice"},
- },
- },
- wantCmd: []string{"INSERTED2:b::alice:Work"},
- mockParams: mockParams{
- feedBagParams: feedBagParams{
- feedbagParams: feedbagParams{
- {screenName: state.NewIdentScreenName("me"), results: []wire.FeedbagItem{{ItemID: 1, ClassID: wire.FeedbagClassIdGroup, GroupID: 100, Name: "Work"}}, err: nil},
- },
- },
- },
- },
- {
- name: "one buddy with alias",
- snac: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
- Items: []wire.FeedbagItem{buddyWithAlias},
- },
- wantCmd: []string{"INSERTED2:b:Alice N.:alice:Work"},
- mockParams: mockParams{
- feedBagParams: feedBagParams{
- feedbagParams: feedbagParams{
- {screenName: state.NewIdentScreenName("me"), results: []wire.FeedbagItem{{ItemID: 1, ClassID: wire.FeedbagClassIdGroup, GroupID: 100, Name: "Work"}}, err: nil},
- },
- },
- },
- },
- {
- name: "one buddy - unknown group ID uses Buddies",
- snac: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 999, Name: "bob"},
- },
- },
- wantCmd: []string{"INSERTED2:b::bob:Buddies"},
- mockParams: mockParams{
- feedBagParams: feedBagParams{
- feedbagParams: feedbagParams{
- {screenName: state.NewIdentScreenName("me"), results: []wire.FeedbagItem{}, err: nil},
- },
- },
- },
- },
- {
- name: "two buddies same group",
- snac: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "alice"},
- {ItemID: 2, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "bob"},
- },
- },
- wantCmd: []string{"INSERTED2:b::alice:Friends", "INSERTED2:b::bob:Friends"},
- mockParams: mockParams{
- feedBagParams: feedBagParams{
- feedbagParams: feedbagParams{
- {screenName: state.NewIdentScreenName("me"), results: []wire.FeedbagItem{{ItemID: 1, ClassID: wire.FeedbagClassIdGroup, GroupID: 100, Name: "Friends"}}, err: nil},
- },
- },
- },
- },
- {
- name: "group added",
- snac: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIdGroup, GroupID: 100, Name: "NewGroup"},
- },
- },
- wantCmd: []string{"INSERTED2:g:NewGroup"},
- mockParams: mockParams{},
- },
- {
- name: "permit and deny added",
- snac: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIDPermit, GroupID: 0, Name: "alice"},
- {ItemID: 2, ClassID: wire.FeedbagClassIDDeny, GroupID: 0, Name: "bob"},
- },
- },
- wantCmd: []string{"INSERTED2:p:alice", "INSERTED2:d:bob"},
- mockParams: mockParams{},
- },
- {
- name: "all four types - group buddy deny permit",
- snac: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIdGroup, GroupID: 200, Name: "NewGroup"},
- {ItemID: 2, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "alice"},
- {ItemID: 3, ClassID: wire.FeedbagClassIDDeny, GroupID: 0, Name: "blockedUser"},
- {ItemID: 4, ClassID: wire.FeedbagClassIDPermit, GroupID: 0, Name: "allowedUser"},
- },
- },
- wantCmd: []string{"INSERTED2:g:NewGroup", "INSERTED2:b::alice:Buddies", "INSERTED2:d:blockedUser", "INSERTED2:p:allowedUser"},
- mockParams: mockParams{
- feedBagParams: feedBagParams{
- feedbagParams: feedbagParams{
- {screenName: state.NewIdentScreenName("me"), results: []wire.FeedbagItem{{ItemID: 1, ClassID: wire.FeedbagClassIdGroup, GroupID: 100, Name: "Buddies"}}, err: nil},
- },
- },
- },
- },
- {
- name: "feedbag lookup fails",
- snac: wire.SNAC_0x13_0x09_FeedbagUpdateItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "alice"},
- },
- },
- wantCmd: nil,
- mockParams: mockParams{
- feedBagParams: feedBagParams{
- feedbagParams: feedbagParams{
- {screenName: state.NewIdentScreenName("me"), results: nil, err: assert.AnError},
- },
- },
- },
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- fbMgr := newMockFeedbagManager(t)
- for _, params := range tc.mockParams.feedBagParams.feedbagParams {
- fbMgr.EXPECT().
- Feedbag(mock.Anything, params.screenName).
- Return(params.results, params.err)
- }
- svc := testOSCARProxy(t)
- svc.FeedbagManager = fbMgr
- me := newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(true) })
- ch := make(chan []string)
- wg := &sync.WaitGroup{}
- wg.Add(1)
- go func() {
- defer wg.Done()
- err := svc.RecvBOS(ctx, me, NewChatRegistry(), ch)
- assert.NoError(t, err)
- }()
- status := me.RelayMessageToInstance(wire.SNACMessage{Body: tc.snac})
- assert.Equal(t, state.SessSendOK, status)
- gotCmd := <-ch
- assert.Equal(t, tc.wantCmd, gotCmd)
- cancel()
- wg.Wait()
- })
- }
- }
- func TestOSCARProxy_RecvBOS_Deleted2(t *testing.T) {
- cases := []struct {
- name string
- snac wire.SNAC_0x13_0x0A_FeedbagDeleteItem
- wantCmd []string
- mockParams mockParams
- }{
- {
- name: "one buddy removed",
- snac: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "alice"},
- },
- },
- wantCmd: []string{"DELETED2:b:alice:Buddies"},
- mockParams: mockParams{
- feedBagParams: feedBagParams{
- feedbagParams: feedbagParams{
- {screenName: state.NewIdentScreenName("me"), results: []wire.FeedbagItem{{ItemID: 1, ClassID: wire.FeedbagClassIdGroup, GroupID: 100, Name: "Buddies"}}, err: nil},
- },
- },
- },
- },
- {
- name: "two buddies removed",
- snac: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "alice"},
- {ItemID: 2, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "bob"},
- },
- },
- wantCmd: []string{"DELETED2:b:alice:Friends", "DELETED2:b:bob:Friends"},
- mockParams: mockParams{
- feedBagParams: feedBagParams{
- feedbagParams: feedbagParams{
- {screenName: state.NewIdentScreenName("me"), results: []wire.FeedbagItem{{ItemID: 1, ClassID: wire.FeedbagClassIdGroup, GroupID: 100, Name: "Friends"}}, err: nil},
- },
- },
- },
- },
- {
- name: "one buddy - unknown group ID uses Buddies",
- snac: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 999, Name: "alice"},
- },
- },
- wantCmd: []string{"DELETED2:b:alice:Buddies"},
- mockParams: mockParams{
- feedBagParams: feedBagParams{
- feedbagParams: feedbagParams{
- {screenName: state.NewIdentScreenName("me"), results: []wire.FeedbagItem{}, err: nil},
- },
- },
- },
- },
- {
- name: "permit and deny removed",
- snac: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIDPermit, GroupID: 0, Name: "alice"},
- {ItemID: 2, ClassID: wire.FeedbagClassIDDeny, GroupID: 0, Name: "bob"},
- },
- },
- wantCmd: []string{"DELETED2:p:alice", "DELETED2:d:bob"},
- mockParams: mockParams{},
- },
- {
- name: "group deleted",
- snac: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIdGroup, GroupID: 100, Name: "Work"},
- },
- },
- wantCmd: []string{"DELETED2:g:Work"},
- mockParams: mockParams{},
- },
- {
- name: "mix of buddy permit and deny emitted",
- snac: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIDPermit, GroupID: 0, Name: "permitUser"},
- {ItemID: 2, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "buddyUser"},
- },
- },
- wantCmd: []string{"DELETED2:p:permitUser", "DELETED2:b:buddyUser:Work"},
- mockParams: mockParams{
- feedBagParams: feedBagParams{
- feedbagParams: feedbagParams{
- {screenName: state.NewIdentScreenName("me"), results: []wire.FeedbagItem{{ItemID: 1, ClassID: wire.FeedbagClassIdGroup, GroupID: 100, Name: "Work"}}, err: nil},
- },
- },
- },
- },
- {
- name: "all four types - group buddy deny permit",
- snac: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIdGroup, GroupID: 200, Name: "OldGroup"},
- {ItemID: 2, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "alice"},
- {ItemID: 3, ClassID: wire.FeedbagClassIDDeny, GroupID: 0, Name: "blockedUser"},
- {ItemID: 4, ClassID: wire.FeedbagClassIDPermit, GroupID: 0, Name: "allowedUser"},
- },
- },
- wantCmd: []string{"DELETED2:g:OldGroup", "DELETED2:b:alice:Buddies", "DELETED2:d:blockedUser", "DELETED2:p:allowedUser"},
- mockParams: mockParams{
- feedBagParams: feedBagParams{
- feedbagParams: feedbagParams{
- {screenName: state.NewIdentScreenName("me"), results: []wire.FeedbagItem{{ItemID: 1, ClassID: wire.FeedbagClassIdGroup, GroupID: 100, Name: "Buddies"}}, err: nil},
- },
- },
- },
- },
- {
- name: "empty items",
- snac: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{Items: nil},
- wantCmd: nil,
- mockParams: mockParams{},
- },
- {
- name: "feedbag lookup fails",
- snac: wire.SNAC_0x13_0x0A_FeedbagDeleteItem{
- Items: []wire.FeedbagItem{
- {ItemID: 1, ClassID: wire.FeedbagClassIdBuddy, GroupID: 100, Name: "alice"},
- },
- },
- wantCmd: nil,
- mockParams: mockParams{
- feedBagParams: feedBagParams{
- feedbagParams: feedbagParams{
- {screenName: state.NewIdentScreenName("me"), results: nil, err: assert.AnError},
- },
- },
- },
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- fbMgr := newMockFeedbagManager(t)
- for _, params := range tc.mockParams.feedBagParams.feedbagParams {
- fbMgr.EXPECT().
- Feedbag(mock.Anything, params.screenName).
- Return(params.results, params.err)
- }
- svc := testOSCARProxy(t)
- svc.FeedbagManager = fbMgr
- me := newTestSession("me", func(i *state.SessionInstance) { i.SetTOC2(true) })
- ch := make(chan []string)
- wg := &sync.WaitGroup{}
- wg.Add(1)
- go func() {
- defer wg.Done()
- err := svc.RecvBOS(ctx, me, NewChatRegistry(), ch)
- assert.NoError(t, err)
- }()
- status := me.RelayMessageToInstance(wire.SNACMessage{Body: tc.snac})
- assert.Equal(t, state.SessSendOK, status)
- gotCmd := <-ch
- assert.Equal(t, tc.wantCmd, gotCmd)
- cancel()
- wg.Wait()
- })
- }
- }
- func testOSCARProxy(t *testing.T) OSCARProxy {
- buddyService := newMockBuddyService(t)
- buddyService.EXPECT().
- BroadcastBuddyDeparted(mock.Anything, mock.Anything).
- Maybe().
- Return(nil)
- buddyListRegistry := newMockBuddyListRegistry(t)
- buddyListRegistry.EXPECT().
- UnregisterBuddyList(mock.Anything, mock.Anything).
- Maybe().
- Return(nil)
- authService := newMockAuthService(t)
- authService.EXPECT().
- Signout(mock.Anything, mock.Anything).
- Maybe()
- authService.EXPECT().
- SignoutChat(mock.Anything, mock.Anything).
- Maybe()
- return OSCARProxy{
- AuthService: authService,
- BuddyListRegistry: buddyListRegistry,
- BuddyService: buddyService,
- Logger: slog.Default(),
- }
- }
|