meowlib/proto_test.go

38 lines
1.0 KiB
Go
Raw Normal View History

2022-09-06 09:30:45 +02:00
package meowlib_test
2022-01-15 22:19:29 +01:00
import (
"log"
2022-12-19 12:15:10 +01:00
"os"
2022-01-15 22:19:29 +01:00
"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)
}
2022-12-19 12:15:10 +01:00
if err := os.WriteFile("test", out, 0644); err != nil {
2022-01-15 22:19:29 +01:00
log.Fatalln("Failed to write address book:", err)
}
2022-12-19 12:15:10 +01:00
in, err := os.ReadFile("test")
2022-01-15 22:19:29 +01:00
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.")
}