2019-06-02 00:50:54 +02:00
|
|
|
package threema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/o3ma/o3"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-06-28 03:03:38 +02:00
|
|
|
"gosrc.io/xmpp/stanza"
|
2019-06-02 00:50:54 +02:00
|
|
|
)
|
|
|
|
|
2019-08-30 17:28:21 +02:00
|
|
|
const threemaFromID = "87654321"
|
2019-06-02 00:50:54 +02:00
|
|
|
|
2019-08-30 17:28:21 +02:00
|
|
|
var threemaFromIDByte o3.IDString
|
2019-06-02 00:50:54 +02:00
|
|
|
|
|
|
|
func init() {
|
2019-08-30 17:28:21 +02:00
|
|
|
threemaFromIDByte = o3.NewIDString(threemaFromID)
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func createDummyAccount() Account {
|
|
|
|
a := Account{
|
|
|
|
deliveredMSG: make(map[uint64]string),
|
|
|
|
readedMSG: make(map[uint64]string),
|
2019-10-03 23:44:42 +02:00
|
|
|
xmpp: make(chan<- stanza.Packet),
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
2019-08-30 17:28:21 +02:00
|
|
|
a.TID = make([]byte, len(threemaFromIDByte))
|
|
|
|
copy(a.TID, threemaFromIDByte[:])
|
2019-06-02 00:50:54 +02:00
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2019-06-06 21:12:21 +02:00
|
|
|
/*
|
2019-06-02 10:41:19 +02:00
|
|
|
func TestReceive(t *testing.T) {
|
2019-06-02 00:50:54 +02:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
a := createDummyAccount()
|
|
|
|
|
2019-06-02 10:41:19 +02:00
|
|
|
// nothing to receiving
|
2019-06-06 21:12:21 +02:00
|
|
|
p, err := a.receiving(nil)
|
2019-06-02 00:50:54 +02:00
|
|
|
assert.Nil(p)
|
2019-06-02 21:39:21 +02:00
|
|
|
assert.Error(err)
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
2019-06-06 21:12:21 +02:00
|
|
|
*/
|
2019-06-02 10:41:19 +02:00
|
|
|
func TestReceiveText(t *testing.T) {
|
2019-06-02 00:50:54 +02:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
a := createDummyAccount()
|
|
|
|
|
2019-06-02 10:41:19 +02:00
|
|
|
// receiving text
|
2019-09-06 05:30:56 +02:00
|
|
|
txtMsg := &o3.TextMessage{
|
2019-08-30 17:28:21 +02:00
|
|
|
MessageHeader: &o3.MessageHeader{
|
|
|
|
Sender: threemaFromIDByte,
|
|
|
|
Recipient: o3.NewIDString("12345678"),
|
2019-06-02 00:50:54 +02:00
|
|
|
},
|
2019-08-30 17:28:21 +02:00
|
|
|
Body: "Oojoh0Ah",
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
2019-06-06 21:12:21 +02:00
|
|
|
p, err := a.receiving(txtMsg)
|
2019-06-02 21:39:21 +02:00
|
|
|
assert.NoError(err)
|
2019-08-30 17:28:21 +02:00
|
|
|
assert.NotNil(p)
|
2019-06-28 03:03:38 +02:00
|
|
|
xMSG, ok := p.(stanza.Message)
|
2019-06-02 00:50:54 +02:00
|
|
|
assert.True(ok)
|
|
|
|
assert.Equal("Oojoh0Ah", xMSG.Body)
|
2019-06-02 21:39:21 +02:00
|
|
|
}
|
2019-06-02 00:50:54 +02:00
|
|
|
|
2019-06-02 23:49:26 +02:00
|
|
|
func TestReceiveAudio(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
a := createDummyAccount()
|
|
|
|
a.threema = &Threema{}
|
|
|
|
|
2019-09-09 13:27:06 +02:00
|
|
|
dataMsg := o3.AudioMessage{
|
2019-08-30 17:28:21 +02:00
|
|
|
MessageHeader: &o3.MessageHeader{
|
|
|
|
Sender: threemaFromIDByte,
|
2019-06-02 23:49:26 +02:00
|
|
|
},
|
2019-08-30 17:28:21 +02:00
|
|
|
}
|
2019-06-06 21:12:21 +02:00
|
|
|
_, err := a.receiving(dataMsg)
|
2019-06-02 23:49:26 +02:00
|
|
|
assert.Error(err)
|
|
|
|
|
|
|
|
a.threema.httpUploadPath = "/tmp"
|
2019-09-09 13:27:06 +02:00
|
|
|
dataMsg = o3.AudioMessage{
|
|
|
|
MessageHeader: &o3.MessageHeader{
|
|
|
|
Sender: threemaFromIDByte,
|
|
|
|
},
|
|
|
|
}
|
2019-06-06 21:12:21 +02:00
|
|
|
_, err = a.receiving(dataMsg)
|
2019-06-02 23:49:26 +02:00
|
|
|
assert.Error(err)
|
|
|
|
}
|
2019-06-02 21:39:21 +02:00
|
|
|
func TestReceiveImage(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
a := createDummyAccount()
|
|
|
|
a.threema = &Threema{}
|
|
|
|
|
2019-08-30 17:28:21 +02:00
|
|
|
// receiving image
|
|
|
|
dataMsg := o3.ImageMessage{
|
|
|
|
MessageHeader: &o3.MessageHeader{
|
|
|
|
Sender: threemaFromIDByte,
|
2019-06-02 00:50:54 +02:00
|
|
|
},
|
2019-08-30 17:28:21 +02:00
|
|
|
}
|
|
|
|
_, err := a.receiving(dataMsg)
|
2019-06-02 21:39:21 +02:00
|
|
|
assert.Error(err)
|
|
|
|
|
|
|
|
a.threema.httpUploadPath = "/tmp"
|
2019-09-09 13:27:06 +02:00
|
|
|
dataMsg = o3.ImageMessage{
|
|
|
|
MessageHeader: &o3.MessageHeader{
|
|
|
|
Sender: threemaFromIDByte,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dataMsg = o3.ImageMessage{
|
2019-08-30 17:28:21 +02:00
|
|
|
MessageHeader: &o3.MessageHeader{
|
|
|
|
Sender: threemaFromIDByte,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err = a.receiving(dataMsg)
|
2019-06-02 21:39:21 +02:00
|
|
|
assert.Error(err)
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
|
|
|
|
2019-06-02 10:41:19 +02:00
|
|
|
func TestReceiveDeliveryReceipt(t *testing.T) {
|
2019-06-02 00:50:54 +02:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
a := createDummyAccount()
|
|
|
|
|
2019-06-02 10:41:19 +02:00
|
|
|
// receiving delivered
|
2019-06-02 00:50:54 +02:00
|
|
|
msgID := o3.NewMsgID()
|
|
|
|
a.deliveredMSG[msgID] = "im4aeseeh1IbaQui"
|
|
|
|
a.readedMSG[msgID] = "im4aeseeh1IbaQui"
|
|
|
|
|
2019-09-06 05:30:56 +02:00
|
|
|
drm := &o3.DeliveryReceiptMessage{
|
2019-08-30 17:28:21 +02:00
|
|
|
MessageHeader: &o3.MessageHeader{
|
|
|
|
Sender: threemaFromIDByte,
|
|
|
|
},
|
|
|
|
Status: o3.MSGDELIVERED,
|
|
|
|
MessageID: msgID,
|
|
|
|
}
|
2019-06-06 21:12:21 +02:00
|
|
|
p, err := a.receiving(drm)
|
2019-06-02 21:39:21 +02:00
|
|
|
assert.NoError(err)
|
2019-08-30 17:28:21 +02:00
|
|
|
assert.NotNil(p)
|
2019-06-28 03:03:38 +02:00
|
|
|
xMSG, ok := p.(stanza.Message)
|
2019-06-02 00:50:54 +02:00
|
|
|
assert.True(ok)
|
2019-06-28 03:03:38 +02:00
|
|
|
rr := xMSG.Extensions[0].(stanza.ReceiptReceived)
|
2019-06-05 04:30:00 +02:00
|
|
|
assert.Equal("im4aeseeh1IbaQui", rr.ID)
|
2019-06-02 00:50:54 +02:00
|
|
|
|
2019-06-02 10:41:19 +02:00
|
|
|
// receiving delivered -> not in cache
|
2019-06-06 21:12:21 +02:00
|
|
|
p, err = a.receiving(drm)
|
2019-06-02 21:39:21 +02:00
|
|
|
assert.NoError(err)
|
2019-06-02 00:50:54 +02:00
|
|
|
assert.Nil(p)
|
|
|
|
|
2019-06-02 10:41:19 +02:00
|
|
|
// receiving readed
|
2019-09-06 05:30:56 +02:00
|
|
|
drm = &o3.DeliveryReceiptMessage{
|
2019-08-30 17:28:21 +02:00
|
|
|
MessageHeader: &o3.MessageHeader{
|
|
|
|
Sender: threemaFromIDByte,
|
|
|
|
},
|
|
|
|
MessageID: msgID,
|
|
|
|
Status: o3.MSGREAD,
|
|
|
|
}
|
2019-06-06 21:12:21 +02:00
|
|
|
p, err = a.receiving(drm)
|
2019-06-02 21:39:21 +02:00
|
|
|
assert.NoError(err)
|
2019-08-30 17:28:21 +02:00
|
|
|
assert.NotNil(p)
|
2019-06-28 03:03:38 +02:00
|
|
|
xMSG, ok = p.(stanza.Message)
|
2019-06-02 00:50:54 +02:00
|
|
|
assert.True(ok)
|
2019-06-28 03:03:38 +02:00
|
|
|
cmdd := xMSG.Extensions[0].(stanza.MarkDisplayed)
|
2019-06-05 04:30:00 +02:00
|
|
|
assert.Equal("im4aeseeh1IbaQui", cmdd.ID)
|
2019-06-02 00:50:54 +02:00
|
|
|
|
2019-06-02 10:41:19 +02:00
|
|
|
// receiving delivered -> not in cache
|
2019-06-06 21:12:21 +02:00
|
|
|
p, err = a.receiving(drm)
|
2019-06-02 21:39:21 +02:00
|
|
|
assert.NoError(err)
|
2019-06-02 00:50:54 +02:00
|
|
|
assert.Nil(p)
|
|
|
|
}
|
2019-06-06 22:33:24 +02:00
|
|
|
func TestReceiveTyping(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
a := createDummyAccount()
|
|
|
|
|
|
|
|
// receiving inactive
|
2019-09-06 05:30:56 +02:00
|
|
|
tnm := &o3.TypingNotificationMessage{
|
2019-08-30 17:28:21 +02:00
|
|
|
MessageHeader: &o3.MessageHeader{
|
|
|
|
Sender: threemaFromIDByte,
|
|
|
|
},
|
|
|
|
}
|
2019-06-06 22:33:24 +02:00
|
|
|
p, err := a.receiving(tnm)
|
2019-08-30 17:28:21 +02:00
|
|
|
assert.NotNil(p)
|
2019-06-06 22:33:24 +02:00
|
|
|
assert.NoError(err)
|
2019-06-28 03:03:38 +02:00
|
|
|
xMSG, ok := p.(stanza.Message)
|
2019-06-06 22:33:24 +02:00
|
|
|
assert.True(ok)
|
2019-06-28 03:03:38 +02:00
|
|
|
assert.IsType(stanza.StateInactive{}, xMSG.Extensions[0])
|
2019-06-06 22:33:24 +02:00
|
|
|
|
|
|
|
// receiving composing
|
2019-09-06 05:30:56 +02:00
|
|
|
tnm = &o3.TypingNotificationMessage{
|
2019-08-30 17:28:21 +02:00
|
|
|
MessageHeader: &o3.MessageHeader{
|
|
|
|
Sender: threemaFromIDByte,
|
|
|
|
},
|
|
|
|
OnOff: 0x1,
|
|
|
|
}
|
2019-06-06 22:33:24 +02:00
|
|
|
p, err = a.receiving(tnm)
|
2019-08-30 17:28:21 +02:00
|
|
|
assert.NotNil(p)
|
2019-06-06 22:33:24 +02:00
|
|
|
assert.NoError(err)
|
2019-06-28 03:03:38 +02:00
|
|
|
xMSG, ok = p.(stanza.Message)
|
2019-06-06 22:33:24 +02:00
|
|
|
assert.True(ok)
|
2019-06-28 03:03:38 +02:00
|
|
|
assert.IsType(stanza.StateComposing{}, xMSG.Extensions[0])
|
2019-06-06 22:33:24 +02:00
|
|
|
}
|