2015-03-04 10:11:31 -08:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"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-04-15 05:25:42 +00:00
|
|
|
config := &Configuration{
|
|
|
|
EnableIPForwarding: true}
|
2015-03-04 10:11:31 -08:00
|
|
|
|
|
|
|
// Set IP Forwarding
|
2015-05-06 05:51:26 +00:00
|
|
|
if err := setupIPForwarding(config); err != nil {
|
2015-03-04 10:11:31 -08: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-04-15 05:25:42 +00:00
|
|
|
config := &Configuration{
|
|
|
|
EnableIPForwarding: false}
|
2015-03-04 10:11:31 -08:00
|
|
|
|
|
|
|
// Attempt Set IP Forwarding
|
2015-05-06 05:51:26 +00:00
|
|
|
err := setupIPForwarding(config)
|
2015-04-17 02:47:12 +00:00
|
|
|
if err == nil {
|
2015-03-04 11:37:16 -08:00
|
|
|
t.Fatal("Setup IP forwarding was expected to fail")
|
2015-04-17 02:47:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
if err != ErrIPFwdCfg {
|
2015-03-04 11:37:16 -08:00
|
|
|
t.Fatalf("Setup IP forwarding failed with unexpected error: %v", err)
|
2015-03-04 10:11:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readCurrentIPForwardingSetting(t *testing.T) []byte {
|
2015-03-04 13:25:43 -08:00
|
|
|
procSetting, err := ioutil.ReadFile(ipv4ForwardConf)
|
2015-03-04 10:11:31 -08: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 13:25:43 -08:00
|
|
|
err := ioutil.WriteFile(ipv4ForwardConf, chars, ipv4ForwardConfPerm)
|
2015-03-04 10:11:31 -08: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)
|
|
|
|
}
|
|
|
|
}
|