Pull request: 4465 fix ifaces resp

Merge in DNS/adguard-home from 4465-bad-ifaces-resp to master

Closes #4465.

Squashed commit of the following:

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

    aghnet: fix get_addresses
This commit is contained in:
Eugene Burkov 2022-04-06 19:36:13 +03:00
parent e9e0b7c4f9
commit 8bb95469d9
2 changed files with 11 additions and 5 deletions

View File

@ -87,8 +87,8 @@ type NetInterface struct {
MTU int `json:"mtu"` MTU int `json:"mtu"`
} }
// MarshalText implements the json.Marshaler interface for NetInterface. // MarshalJSON implements the json.Marshaler interface for NetInterface.
func (iface NetInterface) MarshalText() ([]byte, error) { func (iface NetInterface) MarshalJSON() ([]byte, error) {
type netInterface NetInterface type netInterface NetInterface
return json.Marshal(&struct { return json.Marshal(&struct {
HardwareAddr string `json:"hardware_address"` HardwareAddr string `json:"hardware_address"`

View File

@ -1,6 +1,8 @@
package aghnet package aghnet
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"io/fs" "io/fs"
"net" "net"
@ -311,14 +313,14 @@ func TestIsAddrInUse(t *testing.T) {
}) })
} }
func TestNetInterface_MarshalText(t *testing.T) { func TestNetInterface_MarshalJSON(t *testing.T) {
const want = `{` + const want = `{` +
`"hardware_address":"aa:bb:cc:dd:ee:ff",` + `"hardware_address":"aa:bb:cc:dd:ee:ff",` +
`"flags":"up|multicast",` + `"flags":"up|multicast",` +
`"ip_addresses":["1.2.3.4","aaaa::1"],` + `"ip_addresses":["1.2.3.4","aaaa::1"],` +
`"name":"iface0",` + `"name":"iface0",` +
`"mtu":1500` + `"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} 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) mask4, mask6 := net.CIDRMask(24, netutil.IPv4BitLen), net.CIDRMask(8, netutil.IPv6BitLen)
@ -338,5 +340,9 @@ func TestNetInterface_MarshalText(t *testing.T) {
MTU: 1500, 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())
} }