ソースを参照

Merge pull request #39 from squeed/fix-error

fix error type-casting (revive PR from sanbornm)
Luca Bruno 7 年 前
コミット
a6bee28589
2 ファイル変更43 行追加1 行削除
  1. 6 1
      iptables/iptables.go
  2. 37 0
      iptables/iptables_test.go

+ 6 - 1
iptables/iptables.go

@@ -322,7 +322,12 @@ func (ipt *IPTables) runWithOutput(args []string, stdout io.Writer) error {
 	}
 
 	if err := cmd.Run(); err != nil {
-		return &Error{*(err.(*exec.ExitError)), cmd, stderr.String()}
+		switch e := err.(type) {
+		case *exec.ExitError:
+			return &Error{*e, cmd, stderr.String()}
+		default:
+			return err
+		}
 	}
 
 	return nil

+ 37 - 0
iptables/iptables_test.go

@@ -18,6 +18,7 @@ import (
 	"crypto/rand"
 	"fmt"
 	"math/big"
+	"os"
 	"reflect"
 	"testing"
 )
@@ -305,3 +306,39 @@ func runRulesTests(t *testing.T, ipt *IPTables) {
 		t.Fatalf("Failed to delete test chain: %v", err)
 	}
 }
+
+// TestError checks that we're OK when iptables fails to execute
+func TestError(t *testing.T) {
+	ipt, err := New()
+	if err != nil {
+		t.Fatalf("failed to init: %v", err)
+	}
+
+	chain := randChain(t)
+	_, err = ipt.List("filter", chain)
+	if err == nil {
+		t.Fatalf("no error with invalid params")
+	}
+	switch e := err.(type) {
+	case *Error:
+		// OK
+	default:
+		t.Fatalf("expected type iptables.Error, got %t", e)
+	}
+
+	// Now set an invalid binary path
+	ipt.path = "/does-not-exist"
+
+	_, err = ipt.ListChains("filter")
+
+	if err == nil {
+		t.Fatalf("no error with invalid ipt binary")
+	}
+
+	switch e := err.(type) {
+	case *os.PathError:
+		// OK
+	default:
+		t.Fatalf("expected type os.PathError, got %t", e)
+	}
+}