Complete refactor using protobuff
This commit is contained in:
36
proto_test.go
Normal file
36
proto_test.go
Normal file
@ -0,0 +1,36 @@
|
||||
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.")
|
||||
|
||||
}
|
Reference in New Issue
Block a user