netlink/nl/addr_linux_test.go

70 lines
1.6 KiB
Go
Raw Normal View History

package nl
2014-09-01 03:27:34 +00:00
import (
"bytes"
"crypto/rand"
"encoding/binary"
"testing"
"golang.org/x/sys/unix"
2014-09-01 03:27:34 +00:00
)
func (msg *IfAddrmsg) write(b []byte) {
native := NativeEndian()
2014-09-01 03:27:34 +00:00
b[0] = msg.Family
b[1] = msg.Prefixlen
b[2] = msg.Flags
b[3] = msg.Scope
native.PutUint32(b[4:8], msg.Index)
}
func (msg *IfAddrmsg) serializeSafe() []byte {
len := unix.SizeofIfAddrmsg
2014-09-01 03:27:34 +00:00
b := make([]byte, len)
msg.write(b)
return b
}
func deserializeIfAddrmsgSafe(b []byte) *IfAddrmsg {
var msg = IfAddrmsg{}
binary.Read(bytes.NewReader(b[0:unix.SizeofIfAddrmsg]), NativeEndian(), &msg)
2014-09-01 03:27:34 +00:00
return &msg
}
func TestIfAddrmsgDeserializeSerialize(t *testing.T) {
var orig = make([]byte, unix.SizeofIfAddrmsg)
2014-09-01 03:27:34 +00:00
rand.Read(orig)
safemsg := deserializeIfAddrmsgSafe(orig)
msg := DeserializeIfAddrmsg(orig)
testDeserializeSerialize(t, orig, safemsg, msg)
}
func (msg *IfaCacheInfo) write(b []byte) {
native := NativeEndian()
native.PutUint32(b[0:4], uint32(msg.IfaPrefered))
native.PutUint32(b[4:8], uint32(msg.IfaValid))
native.PutUint32(b[8:12], uint32(msg.Cstamp))
native.PutUint32(b[12:16], uint32(msg.Tstamp))
}
func (msg *IfaCacheInfo) serializeSafe() []byte {
length := SizeofIfaCacheInfo
b := make([]byte, length)
msg.write(b)
return b
}
func deserializeIfaCacheInfoSafe(b []byte) *IfaCacheInfo {
var msg = IfaCacheInfo{}
binary.Read(bytes.NewReader(b[0:SizeofIfaCacheInfo]), NativeEndian(), &msg)
return &msg
}
func TestIfaCacheInfoDeserializeSerialize(t *testing.T) {
var orig = make([]byte, SizeofIfaCacheInfo)
rand.Read(orig)
safemsg := deserializeIfaCacheInfoSafe(orig)
msg := DeserializeIfaCacheInfo(orig)
testDeserializeSerialize(t, orig, safemsg, msg)
}