2015-03-04 13:11:31 -05:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2015-03-04 14:37:16 -05:00
|
|
|
"strings"
|
2015-03-04 13:11:31 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetupIPForwarding(t *testing.T) {
|
|
|
|
// Read current setting and ensure the original value gets restored
|
|
|
|
procSetting := readCurrentIPForwardingSetting(t)
|
|
|
|
defer reconcileIPForwardingSetting(t, procSetting)
|
|
|
|
|
|
|
|
// Disable IP Forwarding if enabled
|
|
|
|
if bytes.Compare(procSetting, []byte("1\n")) == 0 {
|
|
|
|
writeIPForwardingSetting(t, []byte{'0', '\n'})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create test interface with ip forwarding setting enabled
|
2015-03-04 16:25:43 -05:00
|
|
|
br := &bridgeInterface{
|
2015-03-04 13:11:31 -05:00
|
|
|
Config: &Configuration{
|
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
EnableIPForwarding: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set IP Forwarding
|
2015-03-04 16:25:43 -05:00
|
|
|
if err := setupIPForwarding(br); err != nil {
|
2015-03-04 13:11:31 -05:00
|
|
|
t.Fatalf("Failed to setup IP forwarding: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read new setting
|
|
|
|
procSetting = readCurrentIPForwardingSetting(t)
|
|
|
|
if bytes.Compare(procSetting, []byte("1\n")) != 0 {
|
|
|
|
t.Fatalf("Failed to effectively setup IP forwarding")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnexpectedSetupIPForwarding(t *testing.T) {
|
|
|
|
// Read current setting and ensure the original value gets restored
|
|
|
|
procSetting := readCurrentIPForwardingSetting(t)
|
|
|
|
defer reconcileIPForwardingSetting(t, procSetting)
|
|
|
|
|
|
|
|
// Create test interface without ip forwarding setting enabled
|
2015-03-04 16:25:43 -05:00
|
|
|
br := &bridgeInterface{
|
2015-03-04 13:11:31 -05:00
|
|
|
Config: &Configuration{
|
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
EnableIPForwarding: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt Set IP Forwarding
|
2015-03-04 16:25:43 -05:00
|
|
|
if err := setupIPForwarding(br); err == nil {
|
2015-03-04 14:37:16 -05:00
|
|
|
t.Fatal("Setup IP forwarding was expected to fail")
|
|
|
|
} else if !strings.Contains(err.Error(), "Unexpected request") {
|
|
|
|
t.Fatalf("Setup IP forwarding failed with unexpected error: %v", err)
|
2015-03-04 13:11:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readCurrentIPForwardingSetting(t *testing.T) []byte {
|
2015-03-04 16:25:43 -05:00
|
|
|
procSetting, err := ioutil.ReadFile(ipv4ForwardConf)
|
2015-03-04 13:11:31 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't execute test: Failed to read current IP forwarding setting: %v", err)
|
|
|
|
}
|
|
|
|
return procSetting
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeIPForwardingSetting(t *testing.T, chars []byte) {
|
2015-03-04 16:25:43 -05:00
|
|
|
err := ioutil.WriteFile(ipv4ForwardConf, chars, ipv4ForwardConfPerm)
|
2015-03-04 13:11:31 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't execute or cleanup after test: Failed to reset IP forwarding: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func reconcileIPForwardingSetting(t *testing.T, original []byte) {
|
|
|
|
current := readCurrentIPForwardingSetting(t)
|
|
|
|
if bytes.Compare(original, current) != 0 {
|
|
|
|
writeIPForwardingSetting(t, original)
|
|
|
|
}
|
|
|
|
}
|