Skip to content

Added test workflow for PR and upgraded dependencies #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
5 commits merged into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Build and Test Workflow

on:
pull_request:
branches: [ "master" ]

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
max-parallel: 30
matrix:
version:
- 'oldstable'
- 'stable'
steps:
- name: Checkout Code
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- name: Setup Go
uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a
with:
go-version: ${{ matrix.version }}
- name: Test Go
run: go test -v ./...
- name: Build Go
run: go build ./...
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/hashicorp/go-sockaddr

go 1.19
go 1.23

require (
github.com/hashicorp/errwrap v1.1.0
Expand All @@ -27,6 +27,6 @@ require (
github.com/posener/complete v1.1.1 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/crypto v0.32.0 // indirect

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also change the go stanza at the top of the file to 1.23?

golang.org/x/sys v0.29.0 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
5 changes: 4 additions & 1 deletion ifaddrs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,10 @@ func TestGetIfAddrs(t *testing.T) {
t.Fatalf("No loopback interfaces found, loInt nil")
}

if val := sockaddr.IfAddrAttr(*loInt, "flags"); !(val == "up|loopback|multicast" || val == "up|loopback") {
if val := sockaddr.IfAddrAttr(*loInt, "flags"); !(val == "up|loopback|multicast" ||
val == "up|loopback" ||
val == "up|loopback|multicast|running" ||
val == "up|loopback|running") {
t.Fatalf("expected different flags from loopback: %q", val)
}

Expand Down
226 changes: 172 additions & 54 deletions template/template_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,169 @@
package template_test

import (
"net"
"testing"

sockaddr "github.com/hashicorp/go-sockaddr"
socktmpl "github.com/hashicorp/go-sockaddr/template"
)

func TestSockAddr_Parse(t *testing.T) {

interfaceList := []struct {
SockAddress string
Interface net.Interface
}{
{
SockAddress: "127.0.0.1/8",
Interface: net.Interface{
Index: 1,
MTU: 16384,
Name: "lo0",
HardwareAddr: []byte{},
Flags: net.FlagUp | net.FlagLoopback | net.FlagMulticast,
},
},
{
SockAddress: "::1/128",
Interface: net.Interface{
Index: 1,
MTU: 16384,
Name: "lo0",
HardwareAddr: []byte{},
Flags: net.FlagUp | net.FlagLoopback | net.FlagMulticast,
},
},
{
SockAddress: "fe80::1/64",
Interface: net.Interface{
Index: 1,
MTU: 16384,
Name: "lo0",
HardwareAddr: []byte{},
Flags: net.FlagUp | net.FlagLoopback | net.FlagMulticast,
},
},
{
SockAddress: "fe80::603e:5fff:fe48:75ff/64",
Interface: net.Interface{
Index: 14,
MTU: 1500,
Name: "ap1",
HardwareAddr: []byte{0x62, 0x3e, 0x5f, 0x48, 0x75, 0xff},
Flags: net.FlagUp | net.FlagBroadcast | net.FlagMulticast,
},
},
{
SockAddress: "fe80::2b:112f:ce21:7b6f/64",
Interface: net.Interface{
Index: 15,
MTU: 1500,
Name: "en0",
HardwareAddr: []byte{0x60, 0x3e, 0x5f, 0x48, 0x75, 0xff},
Flags: net.FlagUp | net.FlagBroadcast | net.FlagMulticast,
},
},
{
SockAddress: "2406:7400:63:ef5:1415:8bc3:fa5e:2578/64",
Interface: net.Interface{
Index: 15,
MTU: 1500,
Name: "en0",
HardwareAddr: []byte{0x60, 0x3e, 0x5f, 0x48, 0x75, 0xff},
Flags: net.FlagUp | net.FlagBroadcast | net.FlagMulticast,
},
},
{
SockAddress: "2406:7400:63:ef5:3c37:71a6:e3b4:b565/64",
Interface: net.Interface{
Index: 15,
MTU: 1500,
Name: "en0",
HardwareAddr: []byte{0x60, 0x3e, 0x5f, 0x48, 0x75, 0xff},
Flags: net.FlagUp | net.FlagBroadcast | net.FlagMulticast,
},
},
{
SockAddress: "192.168.0.102/24",
Interface: net.Interface{
Index: 15,
MTU: 1500,
Name: "en0",
HardwareAddr: []byte{0x60, 0x3e, 0x5f, 0x48, 0x75, 0xff},
Flags: net.FlagUp | net.FlagBroadcast | net.FlagMulticast,
},
},
{
SockAddress: "fe80::3871:85ff:fed8:aadc/64",
Interface: net.Interface{
Index: 16,
MTU: 1500,
Name: "awdl0",
HardwareAddr: []byte{0x3a, 0x71, 0x85, 0xd8, 0xaa, 0xdc},
Flags: net.FlagUp | net.FlagBroadcast | net.FlagMulticast,
},
},
{
SockAddress: "fe80::3871:85ff:fed8:aadc/64",
Interface: net.Interface{
Index: 17,
MTU: 1500,
Name: "llw0",
HardwareAddr: []byte{0x3a, 0x71, 0x85, 0xd8, 0xaa, 0xdc},
Flags: net.FlagUp | net.FlagBroadcast | net.FlagMulticast,
},
},
{
SockAddress: "fe80::83f9:d7fb:f204:cee5/64",
Interface: net.Interface{
Index: 18,
MTU: 1500,
Name: "utun0",
HardwareAddr: nil,
Flags: net.FlagUp | net.FlagPointToPoint | net.FlagMulticast,
},
},
{
SockAddress: "fe80::2023:8d30:551e:18d/64",
Interface: net.Interface{
Index: 19,
MTU: 1380,
Name: "utun1",
HardwareAddr: nil,
Flags: net.FlagUp | net.FlagPointToPoint | net.FlagMulticast,
},
},
{
SockAddress: "fe80::f9c1:463:96e6:fa8a/64",
Interface: net.Interface{
Index: 20,
MTU: 2000,
Name: "utun2",
HardwareAddr: nil,
Flags: net.FlagUp | net.FlagPointToPoint | net.FlagMulticast,
},
},
{
SockAddress: "fe80::ce81:b1c:bd2c:69e/64",
Interface: net.Interface{
Index: 21,
MTU: 1000,
Name: "utun3",
HardwareAddr: nil,
Flags: net.FlagUp | net.FlagPointToPoint | net.FlagMulticast,
},
},
}
inputList := []sockaddr.IfAddr{}
for i := range interfaceList {
sockAddr, _ := sockaddr.NewIPAddr(interfaceList[i].SockAddress)
inputList = append(inputList, sockaddr.IfAddr{
SockAddr: sockAddr,
Interface: interfaceList[i].Interface,
})
}

tests := []struct {
name string
input string
Expand All @@ -16,7 +173,7 @@ func TestSockAddr_Parse(t *testing.T) {
}{
{
name: `basic include "name"`,
input: `{{GetAllInterfaces | include "name" "lo0" | printf "%v"}}`,
input: `{{. | include "name" "lo0" | printf "%v"}}`,
output: `[127.0.0.1/8 {1 16384 lo0 up|loopback|multicast} ::1 {1 16384 lo0 up|loopback|multicast} fe80::1/64 {1 16384 lo0 up|loopback|multicast}]`,
},
{
Expand All @@ -25,14 +182,9 @@ func TestSockAddr_Parse(t *testing.T) {
output: ``,
fail: true,
},
{
name: "GetDefaultInterface",
input: `{{GetDefaultInterfaces | include "type" "IPv4" | attr "name" }}`,
output: `en0`,
},
{
name: `include "name" regexp`,
input: `{{GetAllInterfaces | include "name" "^(en|lo)0$" | exclude "name" "^en0$" | sort "type" | sort "address" | join "address" " " }}`,
input: `{{. | include "name" "^(en|lo)0$" | exclude "name" "^en0$" | sort "type" | sort "address" | join "address" " " }}`,
output: `127.0.0.1 ::1 fe80::1`,
},
{
Expand Down Expand Up @@ -167,95 +319,61 @@ func TestSockAddr_Parse(t *testing.T) {
input: `{{. | include "name" "lo0" | include "flag" "up" | sort "-type,+address" | attr "address" }}`,
output: `::1`,
},
{
// NOTE(sean@): This is the HashiCorp default in 2016.
// Indented for effect. Using "true" as the output
// instead of printing the correct $rfc*Addrs values.
name: "HashiCorpDefault2016",
input: `
{{- with $addr := GetAllInterfaces | include "type" "IP" | include "rfc" "1918|6598" | sort "address" | attr "address" -}}

{{- if ($addr | len) gt 0 -}}
{{- print "true" -}}{{/* print $addr*/ -}}
{{- end -}}
{{- end -}}`,
output: `true`,
},
{
name: "math address +",
input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "+2" | sort "+type,+address" | join "address" " " }}`,
input: `{{. | include "name" "^lo0$" | include "type" "IP" | math "address" "+2" | sort "+type,+address" | join "address" " " }}`,
output: `127.0.0.3 ::3 fe80::3`,
},
{
name: "math address + overflow",
input: `|{{- with $ifAddrs := GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "+16777217" | sort "+type,+address" -}}
{{- range $ifAddrs -}}
{{- attr "address" . }} -- {{ attr "network" . }}/{{ attr "size" . }}|{{ end -}}
{{- end -}}`,
input: `|{{- with $ifAddrs := . | include "name" "^lo0$" | include "type" "IP" | math "address" "+16777217" | sort "+type,+address" -}}
{{- range $ifAddrs -}}
{{- attr "address" . }} -- {{ attr "network" . }}/{{ attr "size" . }}|{{ end -}}
{{- end -}}`,
output: `|128.0.0.2 -- 128.0.0.0/16777216|::100:2 -- ::100:2/1|fe80::100:2 -- fe80::/18446744073709551616|`,
},
{
name: "math address + overflow+wrap",
input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "+4294967294" | sort "+type,+address" | join "address" " " }}`,
input: `{{. | include "name" "^lo0$" | include "type" "IP" | math "address" "+4294967294" | sort "+type,+address" | join "address" " " }}`,
output: `126.255.255.255 ::ffff:ffff fe80::ffff:ffff`,
},
{
name: "math address -",
input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "-256" | sort "+type,+address" | join "address" " " }}`,
input: `{{. | include "name" "^lo0$" | include "type" "IP" | math "address" "-256" | sort "+type,+address" | join "address" " " }}`,
output: `126.255.255.1 fe7f:ffff:ffff:ffff:ffff:ffff:ffff:ff01 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff01`,
},
{
name: "math address - underflow",
input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "-4278190082" | sort "+type,+address" | join "address" " " }}`,
input: `{{. | include "name" "^lo0$" | include "type" "IP" | math "address" "-4278190082" | sort "+type,+address" | join "address" " " }}`,
output: `127.255.255.255 fe7f:ffff:ffff:ffff:ffff:ffff:ff:ffff ffff:ffff:ffff:ffff:ffff:ffff:ff:ffff`,
},
{
// Note to readers: lo0's link-local address (::1) address has a mask of
// /128 which means its value never changes and this is expected. lo0's
// site-local address has a /64 address and is expected to change.
name: "math network",
input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "network" "+2" | sort "+type,+address" | join "address" " " }}`,
input: `{{. | include "name" "^lo0$" | include "type" "IP" | math "network" "+2" | sort "+type,+address" | join "address" " " }}`,
output: `127.0.0.2 ::1 fe80::2`,
},
{
// Assume an IPv4 input of 127.0.0.1. With a value of 0xff00ff01, we wrap once on purpose.
name: "math network + wrap",
input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "network" "+4278255368" | sort "+type,+address" | join "address" " " }}`,
input: `{{. | include "name" "^lo0$" | include "type" "IP" | math "network" "+4278255368" | sort "+type,+address" | join "address" " " }}`,
output: `127.0.255.8 ::1 fe80::ff00:ff08`,
},
{
name: "math network -",
input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "network" "-2" | sort "+type,+address" | join "address" " " }}`,
input: `{{. | include "name" "^lo0$" | include "type" "IP" | math "network" "-2" | sort "+type,+address" | join "address" " " }}`,
output: `127.255.255.254 ::1 fe80::ffff:ffff:ffff:fffe`,
},
{
// Assume an IPv4 input of 127.0.0.1. With a value of 0xff000008 it
// should wrap and underflow by 8. Assume an IPv6 input of ::1. With a
// value of -0xff000008 the value underflows and wraps.
name: "math network - underflow+wrap",
input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | sort "+type,+address" | math "network" "-4278190088" | join "address" " " }}`,
input: `{{. | include "name" "^lo0$" | include "type" "IP" | sort "+type,+address" | math "network" "-4278190088" | join "address" " " }}`,
output: `127.255.255.248 ::1 fe80::ffff:ffff:ff:fff8`,
},
{
// Assume the private IPs available on the host are: 10.1.2.3
// fe80::1025:f732:1001:203
name: "GetPrivateIPs",
input: `{{GetPrivateIPs}}`,
output: `10.1.2.3 fe80::1025:f732:1001:203`,
},
{
// Assume the public IPs available on the host are: 1.2.3.4 6.7.8.9
name: "GetPublicIPs",
input: `{{GetPublicIPs}}`,
output: `1.2.3.4 6.7.8.9`,
},
{
// Assume the private IPs on this host are just the IPv4 addresses:
// 10.1.2.3 and 172.16.4.6
name: "GetInterfaceIPs",
input: `{{GetInterfaceIPs "en0"}}`,
output: `10.1.2.3 and 172.16.4.6`,
},
}

for i, test := range tests {
Expand All @@ -265,7 +383,7 @@ func TestSockAddr_Parse(t *testing.T) {
}
t.Run(test.name, func(t *testing.T) {
t.Parallel()
out, err := socktmpl.Parse(test.input)
out, err := socktmpl.ParseIfAddrs(test.input, inputList)
if err != nil && !test.fail {
t.Fatalf("%q: bad: %v", test.name, err)
}
Expand Down