2014-09-19 02:04:48 +00:00
|
|
|
package nl
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
|
|
|
"testing"
|
2017-10-20 20:38:07 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2014-09-01 03:27:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (msg *RtMsg) write(b []byte) {
|
2014-09-19 02:04:48 +00:00
|
|
|
native := NativeEndian()
|
2014-09-01 03:27:34 +00:00
|
|
|
b[0] = msg.Family
|
|
|
|
b[1] = msg.Dst_len
|
|
|
|
b[2] = msg.Src_len
|
|
|
|
b[3] = msg.Tos
|
|
|
|
b[4] = msg.Table
|
|
|
|
b[5] = msg.Protocol
|
|
|
|
b[6] = msg.Scope
|
|
|
|
b[7] = msg.Type
|
|
|
|
native.PutUint32(b[8:12], msg.Flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *RtMsg) serializeSafe() []byte {
|
2017-10-20 20:38:07 +00:00
|
|
|
len := unix.SizeofRtMsg
|
2014-09-01 03:27:34 +00:00
|
|
|
b := make([]byte, len)
|
|
|
|
msg.write(b)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func deserializeRtMsgSafe(b []byte) *RtMsg {
|
|
|
|
var msg = RtMsg{}
|
2017-10-20 20:38:07 +00:00
|
|
|
binary.Read(bytes.NewReader(b[0:unix.SizeofRtMsg]), NativeEndian(), &msg)
|
2014-09-01 03:27:34 +00:00
|
|
|
return &msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRtMsgDeserializeSerialize(t *testing.T) {
|
2017-10-20 20:38:07 +00:00
|
|
|
var orig = make([]byte, unix.SizeofRtMsg)
|
2014-09-01 03:27:34 +00:00
|
|
|
rand.Read(orig)
|
|
|
|
safemsg := deserializeRtMsgSafe(orig)
|
|
|
|
msg := DeserializeRtMsg(orig)
|
|
|
|
testDeserializeSerialize(t, orig, safemsg, msg)
|
|
|
|
}
|