meowlib/proto_test.go
2022-01-15 22:19:29 +01:00

37 lines
995 B
Go

package meowlib
import (
"io/ioutil"
"log"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
)
func TestServerMessageSerialization(t *testing.T) {
var msg PackedServerMessage
msg.From = "toto"
msg.Payload = []byte("mon texte")
msg.Signature = "moi"
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)
}
rmsg := &PackedServerMessage{}
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.")
}