Fazlul Shahriar avatar Fazlul Shahriar committed 8965d52

fix read RPC (thanks Markus Sonderegger)

Comments (0)

Files changed (2)

 // is missing or incomplete, getKey is called in an attempt
 // to obtain missing information.
 func (rpc *RPC) callNeedKey(getKey GetKeyFunc, verb string, arg []byte) (string, []byte, error) {
-	status, b, err := rpc.Call(verb, arg)
-	if err != nil {
-		return status, b, err
-	}
-	switch status {
-	case "neekkey", "badkey":
-		if getKey == nil {
+	for {
+		status, b, err := rpc.Call(verb, arg)
+		if err != nil {
 			return status, b, err
 		}
-		if err := getKey(string(b)); err != nil {
+		switch status {
+		default:
 			return status, b, err
+		case "needkey", "badkey":
+			if getKey == nil {
+				return status, b, errors.New("key not found")
+			}
+			if err := getKey(string(b)); err != nil {
+				return status, b, err
+			}
 		}
 	}
-	return status, b, err
+	panic("not reached")
 }
 
 // GetUserPassword returns the username and password for the key
 	"testing"
 )
 
-func TestGetUserPassword(t *testing.T) {
+func testUserPass(t *testing.T, getUP func(*control, string, string) (string, string, error)) {
 	user, pass := "gopher", "gorocks"
 	params := "dom=testing.golang.org proto=pass role=client"
 	key := params + fmt.Sprintf(" user=%s !password=%s", user, pass)
 		t.Fatalf("open factotum/ctl: %v", err)
 	}
 	defer ctl.Close()
-	if err := ctl.AddKey(key); err != nil {
-		t.Fatalf("AddKey failed: %v\n", err)
-	}
 
-	user1, pass1, err := GetUserPassword(nil, params)
+	user1, pass1, err := getUP(ctl, params, key)
 	if err != nil {
 		t.Errorf("GetUserPassword failed: %v\n", err)
 	}
 		t.Errorf("DeleteKey failed: %v\n", err)
 	}
 }
+
+func TestGetUserPassword(t *testing.T) {
+	testUserPass(t, func(ctl *control, params, key string) (string, string, error) {
+		if err := ctl.AddKey(key); err != nil {
+			t.Fatalf("AddKey failed: %v\n", err)
+		}
+		return GetUserPassword(nil, params)
+	})
+}
+
+func TestGetUserPassword1(t *testing.T) {
+	testUserPass(t, func(ctl *control, params, key string) (string, string, error) {
+		return GetUserPassword(func(string) error {
+			return ctl.AddKey(key)
+		}, params)
+	})
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.