38 lines
1.0 KiB
Go
38 lines
1.0 KiB
Go
package meowlib_test
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"testing"
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
"github.com/stretchr/testify/assert"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
func TestServerMessageSerialization(t *testing.T) {
|
|
var msg meowlib.PackedServerMessage
|
|
msg.From = "toto"
|
|
msg.Payload = []byte("mon texte")
|
|
msg.Signature = []byte("moi")
|
|
out, err := proto.Marshal(&msg)
|
|
if err != nil {
|
|
log.Fatalln("Failed to encode address book:", err)
|
|
}
|
|
if err := os.WriteFile("test", out, 0644); err != nil {
|
|
log.Fatalln("Failed to write address book:", err)
|
|
}
|
|
in, err := os.ReadFile("test")
|
|
if err != nil {
|
|
log.Fatalln("Error reading file:", err)
|
|
}
|
|
rmsg := &meowlib.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.")
|
|
|
|
}
|