[TEST] refactory component threema sending

This commit is contained in:
Martin/Geno 2019-06-02 10:41:19 +02:00
parent ee53a32ace
commit e8360e18c1
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
10 changed files with 117 additions and 217 deletions

View File

@ -12,7 +12,7 @@ func (c *Config) receiver() {
log.WithField("type", c.Type).Panicf("connection closed%s", err) log.WithField("type", c.Type).Panicf("connection closed%s", err)
return return
} }
p, back := c.receive(packet) p, back := c.receiving(packet)
if p == nil { if p == nil {
continue continue
} }
@ -24,7 +24,7 @@ func (c *Config) receiver() {
} }
} }
func (c *Config) receive(packet xmpp.Packet) (xmpp.Packet, bool) { func (c *Config) receiving(packet xmpp.Packet) (xmpp.Packet, bool) {
logger := log.WithField("type", c.Type) logger := log.WithField("type", c.Type)
switch p := packet.(type) { switch p := packet.(type) {

View File

@ -13,20 +13,20 @@ func TestReceive(t *testing.T) {
c := Config{Host: "example.org", Type: "monkeyservice"} c := Config{Host: "example.org", Type: "monkeyservice"}
// ignoring packet // ignoring packet
p, back := c.receive(xmpp.Handshake{}) p, _ := c.receiving(xmpp.Handshake{})
assert.Nil(p) assert.Nil(p)
// receive presence // receive presence
p, back = c.receive(xmpp.Presence{}) p, _ = c.receiving(xmpp.Presence{})
assert.Nil(p) assert.Nil(p)
// message // message
p, back = c.receive(xmpp.Message{}) p, back := c.receiving(xmpp.Message{})
assert.False(back) assert.False(back)
assert.NotNil(p) assert.NotNil(p)
// unsupported iq // unsupported iq
p, back = c.receive(xmpp.IQ{Payload: []xmpp.IQPayload{ p, back = c.receiving(xmpp.IQ{Payload: []xmpp.IQPayload{
&xmpp.Err{}, &xmpp.Err{},
}}) }})
assert.True(back) assert.True(back)
@ -36,7 +36,7 @@ func TestReceive(t *testing.T) {
assert.Equal("feature-not-implemented", iq.Error.Reason) assert.Equal("feature-not-implemented", iq.Error.Reason)
// iq disco info // iq disco info
p, back = c.receive(xmpp.IQ{ p, back = c.receiving(xmpp.IQ{
PacketAttrs: xmpp.PacketAttrs{Type: "get"}, PacketAttrs: xmpp.PacketAttrs{Type: "get"},
Payload: []xmpp.IQPayload{ Payload: []xmpp.IQPayload{
&xmpp.DiscoInfo{}, &xmpp.DiscoInfo{},
@ -50,7 +50,7 @@ func TestReceive(t *testing.T) {
assert.Equal("monkeyservice", dinfo.Identity.Name) assert.Equal("monkeyservice", dinfo.Identity.Name)
// iq disco items // iq disco items
p, back = c.receive(xmpp.IQ{ p, back = c.receiving(xmpp.IQ{
PacketAttrs: xmpp.PacketAttrs{Type: "get"}, PacketAttrs: xmpp.PacketAttrs{Type: "get"},
Payload: []xmpp.IQPayload{ Payload: []xmpp.IQPayload{
&xmpp.DiscoItems{}, &xmpp.DiscoItems{},

View File

@ -7,13 +7,13 @@ import (
func (c *Config) sender(packets chan xmpp.Packet) { func (c *Config) sender(packets chan xmpp.Packet) {
for packet := range packets { for packet := range packets {
if p := c.send(packet); p != nil { if p := c.sending(packet); p != nil {
c.xmpp.Send(p) c.xmpp.Send(p)
} }
} }
} }
func (c *Config) send(packet xmpp.Packet) xmpp.Packet { func (c *Config) sending(packet xmpp.Packet) xmpp.Packet {
logger := log.WithField("type", c.Type) logger := log.WithField("type", c.Type)
switch p := packet.(type) { switch p := packet.(type) {
case xmpp.Message: case xmpp.Message:

View File

@ -13,17 +13,17 @@ func TestSend(t *testing.T) {
c := Config{Host: "example.org"} c := Config{Host: "example.org"}
// ignoring packet // ignoring packet
p := c.send(xmpp.IQ{}) p := c.sending(xmpp.IQ{})
assert.Nil(p) assert.Nil(p)
// send by component host // send by component host
p = c.send(xmpp.Message{}) p = c.sending(xmpp.Message{})
assert.NotNil(p) assert.NotNil(p)
msg := p.(xmpp.Message) msg := p.(xmpp.Message)
assert.Equal("example.org", msg.PacketAttrs.From) assert.Equal("example.org", msg.PacketAttrs.From)
// send by a user of component // send by a user of component
p = c.send(xmpp.Message{PacketAttrs: xmpp.PacketAttrs{From: "threemaid"}}) p = c.sending(xmpp.Message{PacketAttrs: xmpp.PacketAttrs{From: "threemaid"}})
assert.NotNil(p) assert.NotNil(p)
msg = p.(xmpp.Message) msg = p.(xmpp.Message)
assert.Equal("threemaid@example.org", msg.PacketAttrs.From) assert.Equal("threemaid@example.org", msg.PacketAttrs.From)

View File

@ -14,7 +14,7 @@ type Account struct {
models.AccountThreema models.AccountThreema
Session o3.SessionContext Session o3.SessionContext
send chan<- o3.Message send chan<- o3.Message
recieve <-chan o3.ReceivedMsg receive <-chan o3.ReceivedMsg
deliveredMSG map[uint64]string deliveredMSG map[uint64]string
readedMSG map[uint64]string readedMSG map[uint64]string
} }
@ -45,7 +45,7 @@ func (t *Threema) getAccount(jid *models.JID) (*Account, error) {
a := &Account{AccountThreema: account} a := &Account{AccountThreema: account}
a.Session = o3.NewSessionContext(tid) a.Session = o3.NewSessionContext(tid)
a.send, a.recieve, err = a.Session.Run() a.send, a.receive, err = a.Session.Run()
if err != nil { if err != nil {
return nil, err return nil, err
@ -55,7 +55,7 @@ func (t *Threema) getAccount(jid *models.JID) (*Account, error) {
a.deliveredMSG = make(map[uint64]string) a.deliveredMSG = make(map[uint64]string)
a.readedMSG = make(map[uint64]string) a.readedMSG = make(map[uint64]string)
go a.reciever(t.out) go a.receiver(t.out)
t.accountJID[jid.String()] = a t.accountJID[jid.String()] = a
return a, nil return a, nil

View File

@ -55,7 +55,7 @@ func (t *Threema) send(packet xmpp.Packet) xmpp.Packet {
if to.IsDomain() { if to.IsDomain() {
if from == nil { if from == nil {
log.Warn("recieve message without sender") log.Warn("receive message without sender")
return nil return nil
} }
msg := xmpp.NewMessage("chat", "", from.String(), "", "en") msg := xmpp.NewMessage("chat", "", from.String(), "", "en")

View File

@ -1,62 +0,0 @@
package threema
import (
"strconv"
"github.com/bdlm/log"
"github.com/o3ma/o3"
"gosrc.io/xmpp"
)
func (a *Account) reciever(out chan<- xmpp.Packet) {
for receivedMessage := range a.recieve {
if p := a.handle(receivedMessage); p != nil {
out <- p
}
}
}
func (a *Account) handle(receivedMessage o3.ReceivedMsg) xmpp.Packet {
if receivedMessage.Err != nil {
log.Warnf("Error Receiving Message: %s\n", receivedMessage.Err)
return nil
}
switch msg := receivedMessage.Msg.(type) {
case o3.TextMessage:
sender := msg.Sender().String()
if string(a.TID) == sender {
return nil
}
xMSG := xmpp.NewMessage("chat", sender, a.XMPP.String(), strconv.FormatUint(msg.ID(), 10), "en")
xMSG.Body = msg.Text()
xMSG.Extensions = append(xMSG.Extensions, xmpp.ReceiptRequest{})
xMSG.Extensions = append(xMSG.Extensions, xmpp.ChatMarkerMarkable{})
return xMSG
case o3.DeliveryReceiptMessage:
msgID := msg.MsgID()
xMSG := xmpp.NewMessage("chat", msg.Sender().String(), a.XMPP.String(), "", "en")
if msg.Status() == o3.MSGDELIVERED {
if id, ok := a.deliveredMSG[msgID]; ok {
xMSG.Extensions = append(xMSG.Extensions, xmpp.ReceiptReceived{Id: id})
xMSG.Extensions = append(xMSG.Extensions, xmpp.ChatMarkerReceived{Id: id})
delete(a.deliveredMSG, msgID)
} else {
log.Warnf("found not id in cache to announce received on xmpp side")
}
}
if msg.Status() == o3.MSGREAD {
if id, ok := a.readedMSG[msgID]; ok {
xMSG.Extensions = append(xMSG.Extensions, xmpp.ChatMarkerDisplayed{Id: id})
delete(a.readedMSG, msgID)
} else {
log.Warnf("found not id in cache to announce readed on xmpp side")
}
}
if len(xMSG.Extensions) > 0 {
return xMSG
}
}
return nil
}

View File

@ -1,131 +0,0 @@
package threema
import (
"errors"
"testing"
"github.com/o3ma/o3"
"github.com/stretchr/testify/assert"
"gosrc.io/xmpp"
)
const threemaID = "87654321"
var threemaIDByte o3.IDString
func init() {
threemaIDByte = o3.NewIDString(threemaID)
}
func createDummyAccount() Account {
a := Account{
deliveredMSG: make(map[uint64]string),
readedMSG: make(map[uint64]string),
}
a.TID = make([]byte, len(threemaIDByte))
copy(a.TID, threemaIDByte[:])
return a
}
func TestRecieve(t *testing.T) {
assert := assert.New(t)
a := createDummyAccount()
// handle/skip error
p := a.handle(o3.ReceivedMsg{
Err: errors.New("dummy"),
})
assert.Nil(p)
// nothing to handle
p = a.handle(o3.ReceivedMsg{})
assert.Nil(p)
}
func TestRecieveText(t *testing.T) {
assert := assert.New(t)
a := createDummyAccount()
// handle text
session := o3.SessionContext{
ID: o3.ThreemaID{
ID: o3.NewIDString("12345678"),
Nick: o3.NewPubNick("user"),
},
}
txtMsg, err := o3.NewTextMessage(&session, threemaID, "Oojoh0Ah")
assert.NoError(err)
p := a.handle(o3.ReceivedMsg{
Msg: txtMsg,
})
xMSG, ok := p.(xmpp.Message)
assert.True(ok)
assert.Equal("Oojoh0Ah", xMSG.Body)
// handle/skip text to own id
session = o3.SessionContext{
ID: o3.ThreemaID{
ID: threemaIDByte,
Nick: o3.NewPubNick("user"),
},
}
txtMsg, err = o3.NewTextMessage(&session, threemaID, "Aesh8shu")
assert.NoError(err)
p = a.handle(o3.ReceivedMsg{
Msg: txtMsg,
})
assert.Nil(p)
}
func TestRecieveDeliveryReceipt(t *testing.T) {
assert := assert.New(t)
a := createDummyAccount()
// handle delivered
session := o3.SessionContext{
ID: o3.ThreemaID{
ID: o3.NewIDString("12345678"),
Nick: o3.NewPubNick("user"),
},
}
msgID := o3.NewMsgID()
a.deliveredMSG[msgID] = "im4aeseeh1IbaQui"
a.readedMSG[msgID] = "im4aeseeh1IbaQui"
drm, err := o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGDELIVERED)
assert.NoError(err)
p := a.handle(o3.ReceivedMsg{
Msg: drm,
})
xMSG, ok := p.(xmpp.Message)
assert.True(ok)
rr := xMSG.Extensions[0].(xmpp.ReceiptReceived)
assert.Equal("im4aeseeh1IbaQui", rr.Id)
// handle delivered -> not in cache
p = a.handle(o3.ReceivedMsg{
Msg: drm,
})
assert.Nil(p)
// handle readed
drm, err = o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGREAD)
assert.NoError(err)
p = a.handle(o3.ReceivedMsg{
Msg: drm,
})
xMSG, ok = p.(xmpp.Message)
assert.True(ok)
cmdd := xMSG.Extensions[0].(xmpp.ChatMarkerDisplayed)
assert.Equal("im4aeseeh1IbaQui", cmdd.Id)
// handle delivered -> not in cache
p = a.handle(o3.ReceivedMsg{
Msg: drm,
})
assert.Nil(p)
}

View File

@ -9,6 +9,17 @@ import (
) )
func (a *Account) Send(to string, msg xmpp.Message) error { func (a *Account) Send(to string, msg xmpp.Message) error {
m, err := a.sending(to, msg)
if err != nil {
return err
}
if m != nil {
a.send <- m
}
return nil
}
func (a *Account) sending(to string, msg xmpp.Message) (o3.Message, error) {
// handle delivered / readed
msgID := "" msgID := ""
readed := false readed := false
for _, el := range msg.Extensions { for _, el := range msg.Extensions {
@ -25,7 +36,7 @@ func (a *Account) Send(to string, msg xmpp.Message) error {
if msgID != "" { if msgID != "" {
id, err := strconv.ParseUint(msgID, 10, 64) id, err := strconv.ParseUint(msgID, 10, 64)
if err != nil { if err != nil {
return err return nil, err
} }
msgType := o3.MSGDELIVERED msgType := o3.MSGDELIVERED
if readed { if readed {
@ -33,23 +44,22 @@ func (a *Account) Send(to string, msg xmpp.Message) error {
} }
drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType) drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType)
if err != nil { if err != nil {
return err return nil, err
} }
a.send <- drm
log.WithFields(map[string]interface{}{ log.WithFields(map[string]interface{}{
"tid": to, "tid": to,
"msg_id": id, "msg_id": id,
"type": msgType, "type": msgType,
}).Debug("update status of threema message") }).Debug("update status of threema message")
return nil return drm, nil
} }
// send text message
msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body) msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body)
if err != nil { if err != nil {
return err return nil, err
} }
a.deliveredMSG[msg3.ID()] = msg.Id a.deliveredMSG[msg3.ID()] = msg.Id
a.readedMSG[msg3.ID()] = msg.Id a.readedMSG[msg3.ID()] = msg.Id
a.send <- msg3 return msg3, nil
return nil
} }

View File

@ -0,0 +1,83 @@
package threema
import (
"testing"
"github.com/o3ma/o3"
"github.com/stretchr/testify/assert"
"gosrc.io/xmpp"
)
func TestAccountSend(t *testing.T) {
assert := assert.New(t)
send := make(chan o3.Message)
a := Account{
Session: o3.NewSessionContext(o3.ThreemaID{ID: o3.NewIDString("43218765")}),
send: send,
deliveredMSG: make(map[uint64]string),
readedMSG: make(map[uint64]string),
}
go func() {
a.Send("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Body: "ohz8kai0ohNgohth",
})
}()
p := <-send
assert.NotNil(p)
msg := p.(o3.TextMessage)
assert.Contains(msg.Text(), "ohz8kai0ohNgohth")
// test error
err := a.Send("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{
&xmpp.ReceiptReceived{Id: "blub"},
},
})
assert.Error(err)
}
func TestAccountSendingDeliviery(t *testing.T) {
assert := assert.New(t)
a := Account{
Session: o3.NewSessionContext(o3.ThreemaID{ID: o3.NewIDString("43218765")}),
}
// test error - threema send only int ids
msg, err := a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{
&xmpp.ReceiptReceived{Id: "blub"},
},
})
assert.Error(err)
assert.Nil(msg)
// test delivered
msg, err = a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{
&xmpp.ChatMarkerReceived{Id: "3"},
},
})
assert.NoError(err)
drm, ok := msg.(o3.DeliveryReceiptMessage)
assert.True(ok)
assert.Equal(o3.MSGDELIVERED, drm.Status())
// test read
msg, err = a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{
&xmpp.ChatMarkerDisplayed{Id: "5"},
},
})
assert.NoError(err)
drm, ok = msg.(o3.DeliveryReceiptMessage)
assert.True(ok)
assert.Equal(o3.MSGREAD, drm.Status())
}