2018-06-28 13:39:48 +02:00
|
|
|
// Copyright 2018 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-05-27 17:30:42 +02:00
|
|
|
package integration_test
|
|
|
|
|
|
|
|
import (
|
2018-06-08 15:41:33 +02:00
|
|
|
"fmt"
|
2018-05-27 17:30:42 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-07-09 08:54:04 +02:00
|
|
|
"github.com/rtr7/router7/internal/netconfig"
|
|
|
|
|
2018-05-27 17:30:42 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2018-08-08 23:15:21 +02:00
|
|
|
"github.com/google/nftables"
|
2018-05-27 17:30:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const goldenInterfaces = `
|
|
|
|
{
|
|
|
|
"interfaces":[
|
|
|
|
{
|
|
|
|
"hardware_addr": "02:73:53:00:ca:fe",
|
2018-06-03 20:35:41 +02:00
|
|
|
"name": "uplink0"
|
2018-06-03 20:47:11 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"hardware_addr": "02:73:53:00:b0:0c",
|
2018-06-04 08:30:57 +02:00
|
|
|
"spoof_hardware_addr": "02:73:53:00:b0:aa",
|
2018-06-03 20:47:11 +02:00
|
|
|
"name": "lan0",
|
|
|
|
"addr": "192.168.42.1/24"
|
2018-05-27 17:30:42 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2018-06-05 08:51:51 +02:00
|
|
|
const goldenPortForwardings = `
|
|
|
|
{
|
|
|
|
"forwardings":[
|
|
|
|
{
|
2018-06-14 22:25:39 +02:00
|
|
|
"port": "8080",
|
2018-06-05 08:51:51 +02:00
|
|
|
"dest_addr": "192.168.42.23",
|
2018-06-14 22:25:39 +02:00
|
|
|
"dest_port": "9999"
|
2018-06-05 08:51:51 +02:00
|
|
|
},
|
|
|
|
{
|
2018-06-14 22:25:39 +02:00
|
|
|
"port": "8040-8060",
|
2018-06-05 08:51:51 +02:00
|
|
|
"dest_addr": "192.168.42.99",
|
2018-06-14 22:25:39 +02:00
|
|
|
"dest_port": "8040-8060"
|
2018-06-14 21:05:43 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"proto": "udp",
|
2018-06-14 22:25:39 +02:00
|
|
|
"port": "53",
|
2018-06-14 21:05:43 +02:00
|
|
|
"dest_addr": "192.168.42.99",
|
2018-06-14 22:25:39 +02:00
|
|
|
"dest_port": "53"
|
2018-06-05 08:51:51 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2018-05-27 17:30:42 +02:00
|
|
|
const goldenDhcp4 = `
|
|
|
|
{
|
|
|
|
"valid_until":"2018-05-18T23:46:04.429895261+02:00",
|
|
|
|
"client_ip":"85.195.207.62",
|
|
|
|
"subnet_mask":"255.255.255.128",
|
|
|
|
"router":"85.195.207.1",
|
|
|
|
"dns":[
|
|
|
|
"77.109.128.2",
|
|
|
|
"213.144.129.20"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const goldenDhcp6 = `
|
|
|
|
{
|
|
|
|
"valid_until":"0001-01-01T00:00:00Z",
|
|
|
|
"prefixes":[
|
|
|
|
{"IP":"2a02:168:4a00::","Mask":"////////AAAAAAAAAAAAAA=="}
|
|
|
|
],
|
|
|
|
"dns":[
|
|
|
|
"2001:1620:2777:1::10",
|
|
|
|
"2001:1620:2777:2::20"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestNetconfig(t *testing.T) {
|
|
|
|
if os.Getenv("HELPER_PROCESS") == "1" {
|
|
|
|
tmp, err := ioutil.TempDir("", "router7")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
for _, golden := range []struct {
|
|
|
|
filename, content string
|
|
|
|
}{
|
|
|
|
{"dhcp4/wire/lease.json", goldenDhcp4},
|
|
|
|
{"dhcp6/wire/lease.json", goldenDhcp6},
|
|
|
|
{"interfaces.json", goldenInterfaces},
|
2018-06-05 08:51:51 +02:00
|
|
|
{"portforwardings.json", goldenPortForwardings},
|
2018-05-27 17:30:42 +02:00
|
|
|
} {
|
|
|
|
if err := os.MkdirAll(filepath.Join(tmp, filepath.Dir(golden.filename)), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(tmp, golden.filename), []byte(golden.content), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-03 20:47:11 +02:00
|
|
|
if err := os.MkdirAll(filepath.Join(tmp, "root", "etc"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-06-22 17:59:23 +02:00
|
|
|
if err := os.MkdirAll(filepath.Join(tmp, "root", "tmp"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:15:21 +02:00
|
|
|
netconfig.DefaultCounterObj = &nftables.CounterObj{Packets: 23, Bytes: 42}
|
2018-06-03 20:47:11 +02:00
|
|
|
if err := netconfig.Apply(tmp, filepath.Join(tmp, "root")); err != nil {
|
2018-06-14 08:27:08 +02:00
|
|
|
t.Fatalf("netconfig.Apply: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply twice to ensure the absence of errors when dealing with
|
|
|
|
// already-configured interfaces, addresses, routes, … (and ensure
|
|
|
|
// nftables rules are replaced, not appendend to).
|
2018-08-08 23:15:21 +02:00
|
|
|
netconfig.DefaultCounterObj = &nftables.CounterObj{Packets: 0, Bytes: 0}
|
2018-06-14 08:27:08 +02:00
|
|
|
if err := netconfig.Apply(tmp, filepath.Join(tmp, "root")); err != nil {
|
2018-05-27 17:30:42 +02:00
|
|
|
t.Fatalf("netconfig.Apply: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-06-22 17:59:23 +02:00
|
|
|
b, err := ioutil.ReadFile(filepath.Join(tmp, "root", "tmp", "resolv.conf"))
|
2018-06-03 20:47:11 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if got, want := strings.TrimSpace(string(b)), "nameserver 192.168.42.1"; got != want {
|
2018-06-22 17:59:23 +02:00
|
|
|
t.Errorf("/tmp/resolv.conf: got %q, want %q", got, want)
|
2018-06-03 20:47:11 +02:00
|
|
|
}
|
|
|
|
|
2018-05-27 17:30:42 +02:00
|
|
|
return
|
|
|
|
}
|
2018-06-24 12:06:40 +02:00
|
|
|
const ns = "ns3" // name of the network namespace to use for this test
|
2018-05-27 17:30:42 +02:00
|
|
|
|
2018-07-14 15:29:34 +02:00
|
|
|
add := exec.Command("ip", "netns", "add", ns)
|
|
|
|
add.Stderr = os.Stderr
|
|
|
|
if err := add.Run(); err != nil {
|
|
|
|
t.Fatalf("%v: %v", add.Args, err)
|
2018-05-27 17:30:42 +02:00
|
|
|
}
|
|
|
|
defer exec.Command("ip", "netns", "delete", ns).Run()
|
|
|
|
|
|
|
|
nsSetup := []*exec.Cmd{
|
|
|
|
exec.Command("ip", "netns", "exec", ns, "ip", "link", "add", "dummy0", "type", "dummy"),
|
2018-06-03 20:35:41 +02:00
|
|
|
exec.Command("ip", "netns", "exec", ns, "ip", "link", "add", "lan0", "type", "dummy"),
|
2018-05-27 17:30:42 +02:00
|
|
|
exec.Command("ip", "netns", "exec", ns, "ip", "link", "set", "dummy0", "address", "02:73:53:00:ca:fe"),
|
2018-06-03 20:47:11 +02:00
|
|
|
exec.Command("ip", "netns", "exec", ns, "ip", "link", "set", "lan0", "address", "02:73:53:00:b0:0c"),
|
2018-05-27 17:30:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, cmd := range nsSetup {
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Fatalf("%v: %v", cmd.Args, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("ip", "netns", "exec", ns, os.Args[0], "-test.run=^TestNetconfig$")
|
|
|
|
cmd.Env = append(os.Environ(), "HELPER_PROCESS=1")
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-06-04 08:30:57 +02:00
|
|
|
link, err := exec.Command("ip", "netns", "exec", ns, "ip", "link", "show", "dev", "lan0").Output()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(link), "link/ether 02:73:53:00:b0:aa") {
|
|
|
|
t.Errorf("lan0 MAC address is not 02:73:53:00:b0:aa")
|
|
|
|
}
|
|
|
|
|
2018-06-03 20:35:41 +02:00
|
|
|
addrs, err := exec.Command("ip", "netns", "exec", ns, "ip", "address", "show", "dev", "uplink0").Output()
|
2018-05-27 17:30:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-06-03 20:35:41 +02:00
|
|
|
addrRe := regexp.MustCompile(`(?m)^\s*inet 85.195.207.62/25 brd 85.195.207.127 scope global uplink0$`)
|
2018-05-27 17:30:42 +02:00
|
|
|
if !addrRe.MatchString(string(addrs)) {
|
|
|
|
t.Fatalf("regexp %s does not match %s", addrRe, string(addrs))
|
|
|
|
}
|
2018-06-03 20:35:41 +02:00
|
|
|
|
|
|
|
addrsLan, err := exec.Command("ip", "netns", "exec", ns, "ip", "address", "show", "dev", "lan0").Output()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-05-28 09:53:54 +02:00
|
|
|
addr6Re := regexp.MustCompile(`(?m)^\s*inet6 2a02:168:4a00::1/64 scope global\s*$`)
|
2018-06-03 20:35:41 +02:00
|
|
|
if !addr6Re.MatchString(string(addrsLan)) {
|
|
|
|
t.Fatalf("regexp %s does not match %s", addr6Re, string(addrsLan))
|
2018-05-27 17:30:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
wantRoutes := []string{
|
|
|
|
"default via 85.195.207.1 proto dhcp src 85.195.207.62 ",
|
|
|
|
"85.195.207.0/25 proto kernel scope link src 85.195.207.62 ",
|
|
|
|
"85.195.207.1 proto dhcp scope link src 85.195.207.62",
|
|
|
|
}
|
|
|
|
|
2018-06-08 15:41:33 +02:00
|
|
|
routes, err := ipLines("netns", "exec", ns, "ip", "route", "show", "dev", "uplink0")
|
2018-05-27 17:30:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(routes, wantRoutes); diff != "" {
|
|
|
|
t.Fatalf("routes: diff (-got +want):\n%s", diff)
|
|
|
|
}
|
|
|
|
|
2018-06-08 15:41:33 +02:00
|
|
|
rules, err := ipLines("netns", "exec", ns, "nft", "list", "ruleset")
|
2018-05-27 17:30:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for n, rule := range rules {
|
|
|
|
t.Logf("rule %d: %s", n, rule)
|
|
|
|
}
|
2018-06-05 08:48:30 +02:00
|
|
|
if len(rules) < 2 {
|
2018-06-04 10:01:09 +02:00
|
|
|
t.Fatalf("nftables rules not found")
|
|
|
|
}
|
|
|
|
wantRules := []string{
|
|
|
|
`table ip nat {`,
|
|
|
|
` chain prerouting {`,
|
|
|
|
` type nat hook prerouting priority 0; policy accept;`,
|
2018-06-14 21:05:43 +02:00
|
|
|
` iifname "uplink0" udp dport domain dnat to 192.168.42.99:domain`,
|
2018-06-14 22:25:39 +02:00
|
|
|
` iifname "uplink0" tcp dport 8040-8060 dnat to 192.168.42.99:8040-8060`,
|
2018-06-05 08:51:51 +02:00
|
|
|
` iifname "uplink0" tcp dport http-alt dnat to 192.168.42.23:9999`,
|
2018-06-04 10:01:09 +02:00
|
|
|
` }`,
|
|
|
|
``,
|
|
|
|
` chain postrouting {`,
|
|
|
|
` type nat hook postrouting priority 100; policy accept;`,
|
2018-06-05 08:48:30 +02:00
|
|
|
` oifname "uplink0" masquerade`,
|
2018-06-04 10:01:09 +02:00
|
|
|
` }`,
|
|
|
|
`}`,
|
2018-06-23 19:56:34 +02:00
|
|
|
`table ip filter {`,
|
2018-08-08 23:15:21 +02:00
|
|
|
` counter fwded {`,
|
|
|
|
` packets 23 bytes 42`,
|
|
|
|
` }`,
|
|
|
|
``,
|
2018-06-23 19:56:34 +02:00
|
|
|
` chain forward {`,
|
|
|
|
` type filter hook forward priority 0; policy accept;`,
|
2018-08-08 23:15:21 +02:00
|
|
|
` counter name "fwded"`,
|
2018-06-23 19:56:34 +02:00
|
|
|
` }`,
|
|
|
|
`}`,
|
|
|
|
`table ip6 filter {`,
|
2018-08-08 23:15:21 +02:00
|
|
|
` counter fwded {`,
|
|
|
|
` packets 23 bytes 42`,
|
|
|
|
` }`,
|
|
|
|
``,
|
2018-06-23 19:56:34 +02:00
|
|
|
` chain forward {`,
|
|
|
|
` type filter hook forward priority 0; policy accept;`,
|
2018-08-08 23:15:21 +02:00
|
|
|
` counter name "fwded"`,
|
2018-06-23 19:56:34 +02:00
|
|
|
` }`,
|
|
|
|
`}`,
|
2018-06-04 10:01:09 +02:00
|
|
|
}
|
2018-06-08 15:41:33 +02:00
|
|
|
opts := []cmp.Option{
|
|
|
|
cmp.Transformer("formatting", func(line string) string {
|
|
|
|
return strings.TrimSpace(strings.Replace(line, "dnat to", "dnat", -1))
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(rules, wantRules, opts...); diff != "" {
|
2018-06-04 10:01:09 +02:00
|
|
|
t.Fatalf("unexpected nftables rules: diff (-got +want):\n%s", diff)
|
2018-05-27 17:30:42 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-08 15:41:33 +02:00
|
|
|
|
|
|
|
func ipLines(args ...string) ([]string, error) {
|
|
|
|
cmd := exec.Command("ip", args...)
|
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %v", cmd.Args, err)
|
|
|
|
}
|
|
|
|
outstr := string(out)
|
|
|
|
for strings.Contains(outstr, " ") {
|
|
|
|
outstr = strings.Replace(outstr, " ", " ", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Split(strings.TrimSpace(outstr), "\n"), nil
|
|
|
|
}
|