2022-09-06 09:30:45 +02:00
|
|
|
package meowlib_test
|
2022-01-15 22:19:29 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-06 09:30:45 +02:00
|
|
|
"forge.redroom.link/yves/meowlib"
|
2022-01-15 22:19:29 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServerMessageSerialization(t *testing.T) {
|
2022-09-06 09:30:45 +02:00
|
|
|
var msg meowlib.PackedServerMessage
|
2022-01-15 22:19:29 +01:00
|
|
|
msg.From = "toto"
|
|
|
|
msg.Payload = []byte("mon texte")
|
2022-09-06 09:30:45 +02:00
|
|
|
msg.Signature = []byte("moi")
|
2022-01-15 22:19:29 +01:00
|
|
|
out, err := proto.Marshal(&msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed to encode address book:", err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile("test", out, 0644); err != nil {
|
|
|
|
log.Fatalln("Failed to write address book:", err)
|
|
|
|
}
|
|
|
|
in, err := ioutil.ReadFile("test")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Error reading file:", err)
|
|
|
|
}
|
2022-09-06 09:30:45 +02:00
|
|
|
rmsg := &meowlib.PackedServerMessage{}
|
2022-01-15 22:19:29 +01:00
|
|
|
if err := proto.Unmarshal(in, rmsg); err != nil {
|
|
|
|
log.Fatalln("Failed to parse address book:", err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, msg.From, rmsg.From, "The two words should be the same.")
|
|
|
|
assert.Equal(t, msg.Payload, rmsg.Payload, "The two words should be the same.")
|
|
|
|
assert.Equal(t, msg.Signature, rmsg.Signature, "The two words should be the same.")
|
|
|
|
|
|
|
|
}
|