Skip to content

Commit

Permalink
Pull request: 4465 fix ifaces resp
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from 4465-bad-ifaces-resp to master

Closes AdguardTeam#4465.

Squashed commit of the following:

commit cc44252
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Apr 6 19:21:40 2022 +0300

    aghnet: fix get_addresses
  • Loading branch information
EugeneOne1 committed Apr 6, 2022
1 parent e9e0b7c commit 8bb9546
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions internal/aghnet/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ type NetInterface struct {
MTU int `json:"mtu"`
}

// MarshalText implements the json.Marshaler interface for NetInterface.
func (iface NetInterface) MarshalText() ([]byte, error) {
// MarshalJSON implements the json.Marshaler interface for NetInterface.
func (iface NetInterface) MarshalJSON() ([]byte, error) {
type netInterface NetInterface
return json.Marshal(&struct {
HardwareAddr string `json:"hardware_address"`
Expand Down
12 changes: 9 additions & 3 deletions internal/aghnet/net_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package aghnet

import (
"bytes"
"encoding/json"
"fmt"
"io/fs"
"net"
Expand Down Expand Up @@ -311,14 +313,14 @@ func TestIsAddrInUse(t *testing.T) {
})
}

func TestNetInterface_MarshalText(t *testing.T) {
func TestNetInterface_MarshalJSON(t *testing.T) {
const want = `{` +
`"hardware_address":"aa:bb:cc:dd:ee:ff",` +
`"flags":"up|multicast",` +
`"ip_addresses":["1.2.3.4","aaaa::1"],` +
`"name":"iface0",` +
`"mtu":1500` +
`}`
`}` + "\n"

ip4, ip6 := net.IP{1, 2, 3, 4}, net.IP{0xAA, 0xAA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
mask4, mask6 := net.CIDRMask(24, netutil.IPv4BitLen), net.CIDRMask(8, netutil.IPv6BitLen)
Expand All @@ -338,5 +340,9 @@ func TestNetInterface_MarshalText(t *testing.T) {
MTU: 1500,
}

testutil.AssertMarshalText(t, want, iface)
b := &bytes.Buffer{}
err := json.NewEncoder(b).Encode(iface)
require.NoError(t, err)

assert.Equal(t, want, b.String())
}

0 comments on commit 8bb9546

Please sign in to comment.