diff --git a/address.go b/address.go index a8d274e..b0f5322 100644 --- a/address.go +++ b/address.go @@ -213,14 +213,19 @@ func (a *AddressAttributes) decode(ad *netlink.AttributeDecoder) error { func (a *AddressAttributes) encode(ae *netlink.AttributeEncoder) error { ae.Uint16(unix.IFA_UNSPEC, 0) ae.Bytes(unix.IFA_ADDRESS, a.Address) - ae.Bytes(unix.IFA_BROADCAST, a.Broadcast) - ae.Bytes(unix.IFA_ANYCAST, a.Anycast) - ae.Bytes(unix.IFA_MULTICAST, a.Multicast) - ae.Uint32(unix.IFA_FLAGS, a.Flags) - if a.Local != nil { ae.Bytes(unix.IFA_LOCAL, a.Local) } + if a.Broadcast != nil { + ae.Bytes(unix.IFA_BROADCAST, a.Broadcast) + } + if a.Anycast != nil { + ae.Bytes(unix.IFA_ANYCAST, a.Anycast) + } + if a.Multicast != nil { + ae.Bytes(unix.IFA_MULTICAST, a.Multicast) + } + ae.Uint32(unix.IFA_FLAGS, a.Flags) return nil } diff --git a/address_test.go b/address_test.go index abce58b..5f8dbc0 100644 --- a/address_test.go +++ b/address_test.go @@ -26,9 +26,8 @@ func TestAddressMessageMarshalBinary(t *testing.T) { b: []byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00, - 0x04, 0x00, 0x05, 0x00, 0x04, 0x00, 0x07, 0x00, - 0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x01, 0x00, 0x08, 0x00, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, }, }, { @@ -43,9 +42,8 @@ func TestAddressMessageMarshalBinary(t *testing.T) { b: []byte{ 0x02, 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00, - 0x04, 0x00, 0x05, 0x00, 0x04, 0x00, 0x07, 0x00, - 0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x01, 0x00, 0x08, 0x00, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, }, }, { @@ -63,10 +61,25 @@ func TestAddressMessageMarshalBinary(t *testing.T) { 0x0a, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x05, 0x00, 0x04, 0x00, 0x07, 0x00, 0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, }, }, + { + name: "no broadcast ", + m: &AddressMessage{ + Attributes: AddressAttributes{ + Address: []byte{0, 0, 0, 0, 0, 0}, + Label: "lo", + }, + }, + b: []byte{ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x0a, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, + }, + }, } for _, tt := range tests { @@ -81,7 +94,7 @@ func TestAddressMessageMarshalBinary(t *testing.T) { } if want, got := tt.b, b; !bytes.Equal(want, got) { - t.Fatalf("unexpected Message bytes:\n- want: [%# x]\n- got: [%# x]", want, got) + t.Fatalf("unexpected Message bytes:\n- want: [%s]\n- got: [%s]", bPrint(want), bPrint(got)) } }) } diff --git a/conn_test.go b/conn_test.go index 0f87742..c675874 100644 --- a/conn_test.go +++ b/conn_test.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "reflect" + "strings" "testing" "time" @@ -221,3 +222,7 @@ func mustMarshal(m encoding.BinaryMarshaler) []byte { return b } + +func bPrint(b []byte) string { + return strings.ReplaceAll(fmt.Sprintf("%# x", b), " ", ", ") +}