Change address message to ignore empty ip's (#74)

This will not marshal the following attributes when empty:
- Local (was already ignored)
- Broadcast
- Anycast
- Multicast

Fixes: #73

Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
This commit is contained in:
Jeroen Simonetti 2020-03-19 15:28:06 +01:00 committed by GitHub
parent 7a753270ee
commit 31febcbb33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 13 deletions

View File

@ -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
}

View File

@ -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))
}
})
}

View File

@ -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), " ", ", ")
}