2014-09-19 02:04:48 +00:00
|
|
|
package nl
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
2014-09-19 02:04:48 +00:00
|
|
|
"reflect"
|
2014-09-01 03:27:34 +00:00
|
|
|
"testing"
|
2017-09-27 23:53:24 +00:00
|
|
|
"time"
|
2017-10-20 20:38:07 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2014-09-01 03:27:34 +00:00
|
|
|
)
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
type testSerializer interface {
|
|
|
|
serializeSafe() []byte
|
|
|
|
Serialize() []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDeserializeSerialize(t *testing.T, orig []byte, safemsg testSerializer, msg testSerializer) {
|
|
|
|
if !reflect.DeepEqual(safemsg, msg) {
|
|
|
|
t.Fatal("Deserialization failed.\n", safemsg, "\n", msg)
|
|
|
|
}
|
|
|
|
safe := msg.serializeSafe()
|
|
|
|
if !bytes.Equal(safe, orig) {
|
|
|
|
t.Fatal("Safe serialization failed.\n", safe, "\n", orig)
|
|
|
|
}
|
|
|
|
b := msg.Serialize()
|
|
|
|
if !bytes.Equal(b, safe) {
|
|
|
|
t.Fatal("Serialization failed.\n", b, "\n", safe)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
func (msg *IfInfomsg) 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
|
2017-02-19 23:32:03 +00:00
|
|
|
// pad byte is skipped because it is not exported on linux/s390x
|
2014-09-01 03:27:34 +00:00
|
|
|
native.PutUint16(b[2:4], msg.Type)
|
|
|
|
native.PutUint32(b[4:8], uint32(msg.Index))
|
|
|
|
native.PutUint32(b[8:12], msg.Flags)
|
|
|
|
native.PutUint32(b[12:16], msg.Change)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *IfInfomsg) serializeSafe() []byte {
|
2017-10-20 20:38:07 +00:00
|
|
|
length := unix.SizeofIfInfomsg
|
2014-09-01 03:27:34 +00:00
|
|
|
b := make([]byte, length)
|
|
|
|
msg.write(b)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func deserializeIfInfomsgSafe(b []byte) *IfInfomsg {
|
|
|
|
var msg = IfInfomsg{}
|
2017-10-20 20:38:07 +00:00
|
|
|
binary.Read(bytes.NewReader(b[0:unix.SizeofIfInfomsg]), NativeEndian(), &msg)
|
2014-09-01 03:27:34 +00:00
|
|
|
return &msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIfInfomsgDeserializeSerialize(t *testing.T) {
|
2017-10-20 20:38:07 +00:00
|
|
|
var orig = make([]byte, unix.SizeofIfInfomsg)
|
2014-09-01 03:27:34 +00:00
|
|
|
rand.Read(orig)
|
2017-02-19 23:32:03 +00:00
|
|
|
// zero out the pad byte
|
|
|
|
orig[1] = 0
|
2014-09-01 03:27:34 +00:00
|
|
|
safemsg := deserializeIfInfomsgSafe(orig)
|
|
|
|
msg := DeserializeIfInfomsg(orig)
|
|
|
|
testDeserializeSerialize(t, orig, safemsg, msg)
|
|
|
|
}
|
2017-09-27 23:53:24 +00:00
|
|
|
|
|
|
|
func TestIfSocketCloses(t *testing.T) {
|
2017-10-20 20:38:07 +00:00
|
|
|
nlSock, err := Subscribe(unix.NETLINK_ROUTE, unix.RTNLGRP_NEIGH)
|
2017-09-27 23:53:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error on creating the socket: %v", err)
|
|
|
|
}
|
2017-10-20 20:38:07 +00:00
|
|
|
nlSock.SetReceiveTimeout(&unix.Timeval{Sec: 2, Usec: 0})
|
2017-09-27 23:53:24 +00:00
|
|
|
endCh := make(chan error)
|
|
|
|
go func(sk *NetlinkSocket, endCh chan error) {
|
|
|
|
endCh <- nil
|
|
|
|
for {
|
|
|
|
_, err := sk.Receive()
|
|
|
|
// Receive returned because of a timeout and the FD == -1 means that the socket got closed
|
2017-10-20 20:38:07 +00:00
|
|
|
if err == unix.EAGAIN && nlSock.GetFd() == -1 {
|
2017-09-27 23:53:24 +00:00
|
|
|
endCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(nlSock, endCh)
|
|
|
|
|
|
|
|
// first receive nil
|
|
|
|
if msg := <-endCh; msg != nil {
|
|
|
|
t.Fatalf("Expected nil instead got: %v", msg)
|
|
|
|
}
|
|
|
|
// this to guarantee that the receive is invoked before the close
|
|
|
|
time.Sleep(4 * time.Second)
|
|
|
|
|
|
|
|
// Close the socket
|
|
|
|
nlSock.Close()
|
|
|
|
|
|
|
|
// Expect to have an error
|
|
|
|
msg := <-endCh
|
|
|
|
if msg == nil {
|
|
|
|
t.Fatalf("Expected error instead received nil")
|
|
|
|
}
|
|
|
|
}
|