66 lines
2.3 KiB
Go
66 lines
2.3 KiB
Go
package isphere
|
|
|
|
import (
|
|
"context"
|
|
"crypto/cipher"
|
|
"crypto/des"
|
|
"encoding/base64"
|
|
"testing"
|
|
)
|
|
|
|
func TestEncryptedPacketLogSourceReturnsNormalizedMessages(t *testing.T) {
|
|
plaintext := `--------------------------------------------------------------------------------------------------------------------------------------------
|
|
2026/7/7 15:30:07
|
|
<message id="msg-1" from="sender@imopenfire1-lanzhou/imp_pc_4.1.2.6842" to="receiver@imopenfire1-lanzhou" type="chat">
|
|
<body>redacted</body>
|
|
<received xmlns="urn:xmpp:receipts" id="receipt-1" type="1" stamp="" />
|
|
<subject>FILE_TRANSFER_CANCEL</subject>
|
|
<isphere xmlns="isphere.im" type="1001" sendtime="1783423807000" version="1" />
|
|
<readed />
|
|
</message>`
|
|
source := EncryptedPacketLogSource{Lines: []string{encryptPacketLineForTest(t, plaintext)}}
|
|
|
|
got, err := source.ReceiveMessages(context.Background(), ReceiveMessagesQuery{Limit: 10})
|
|
if err != nil {
|
|
t.Fatalf("ReceiveMessages returned error: %v", err)
|
|
}
|
|
if len(got.Messages) != 1 {
|
|
t.Fatalf("message count = %d, want 1", len(got.Messages))
|
|
}
|
|
msg := got.Messages[0]
|
|
if msg.ID != "msg-1" || msg.Text != "redacted" || msg.Subject != "FILE_TRANSFER_CANCEL" {
|
|
t.Fatalf("unexpected message content: %+v", msg)
|
|
}
|
|
if msg.ConversationID != "sender@imopenfire1-lanzhou|receiver@imopenfire1-lanzhou" {
|
|
t.Fatalf("conversation id = %q", msg.ConversationID)
|
|
}
|
|
if msg.SenderID != "sender@imopenfire1-lanzhou" || msg.ReceiverID != "receiver@imopenfire1-lanzhou" {
|
|
t.Fatalf("sender/receiver = %q/%q", msg.SenderID, msg.ReceiverID)
|
|
}
|
|
if msg.Timestamp != "1783423807000" || msg.ReceiptID != "receipt-1" || !msg.Read {
|
|
t.Fatalf("metadata = %+v", msg)
|
|
}
|
|
}
|
|
|
|
func encryptPacketLineForTest(t *testing.T, plaintext string) string {
|
|
t.Helper()
|
|
block, err := des.NewCipher([]byte("hyhccdtm"))
|
|
if err != nil {
|
|
t.Fatalf("NewCipher: %v", err)
|
|
}
|
|
plain := padPacketLineForTest([]byte(plaintext), des.BlockSize)
|
|
ciphertext := make([]byte, len(plain))
|
|
iv := []byte{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}
|
|
cipher.NewCBCEncrypter(block, iv).CryptBlocks(ciphertext, plain)
|
|
return base64.StdEncoding.EncodeToString(ciphertext)
|
|
}
|
|
|
|
func padPacketLineForTest(data []byte, blockSize int) []byte {
|
|
pad := blockSize - len(data)%blockSize
|
|
out := append([]byte(nil), data...)
|
|
for i := 0; i < pad; i++ {
|
|
out = append(out, byte(pad))
|
|
}
|
|
return out
|
|
}
|