File: route_linux_test.go

package info (click to toggle)
golang-github-vishvananda-netlink 0.0~git20160306.0.4fdf23c-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 476 kB
  • sloc: makefile: 24
file content (43 lines) | stat: -rw-r--r-- 856 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package nl

import (
	"bytes"
	"crypto/rand"
	"encoding/binary"
	"syscall"
	"testing"
)

func (msg *RtMsg) write(b []byte) {
	native := NativeEndian()
	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 {
	len := syscall.SizeofRtMsg
	b := make([]byte, len)
	msg.write(b)
	return b
}

func deserializeRtMsgSafe(b []byte) *RtMsg {
	var msg = RtMsg{}
	binary.Read(bytes.NewReader(b[0:syscall.SizeofRtMsg]), NativeEndian(), &msg)
	return &msg
}

func TestRtMsgDeserializeSerialize(t *testing.T) {
	var orig = make([]byte, syscall.SizeofRtMsg)
	rand.Read(orig)
	safemsg := deserializeRtMsgSafe(orig)
	msg := DeserializeRtMsg(orig)
	testDeserializeSerialize(t, orig, safemsg, msg)
}