Add code to test for allowable types in json rpc id field along with test code for the new function.

This commit is contained in:
John C. Vernaleo 2013-05-31 10:59:46 -04:00
parent 7b4aeb2353
commit 4ab8ca5eef
2 changed files with 42 additions and 0 deletions

View file

@ -723,3 +723,22 @@ func RpcCommand(user string, password string, server string, message []byte) (Re
} }
return result, err return result, err
} }
// IsValidIdType checks that the Id field (which can go in any of the json
// messages is valid. json rpc 1.0 allows any (json) type, but we still need
// to prevent values that cannot be marshalled from going in. json rpc 2.0
// (which bitcoind follows for some parts) only allows string, number, or null,
// so we restrict to that list. Ths is only necessary if you manually marshal
// a message. The normal btcjson functions only use string ids.
func IsValidIdType(id interface{}) bool {
switch id.(type) {
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64,
string,
nil:
return true
default:
return false
}
}

View file

@ -243,3 +243,26 @@ func TestRpcReply(t *testing.T) {
} }
return return
} }
var idtests = []struct {
testId []interface{}
pass bool
}{
{[]interface{}{"string test"}, true},
{[]interface{}{1}, true},
{[]interface{}{1.0}, true},
{[]interface{}{nil}, true},
{[]interface{}{make(chan int)}, false},
}
// TestIsValidIdType tests that IsValidIdType allows (and disallows the correct
// types.
func TestIsValidIdType(t *testing.T) {
for _, tt := range idtests {
res := btcjson.IsValidIdType(tt.testId[0])
if res != tt.pass {
t.Errorf("Incorrect type result %v.", tt)
}
}
return
}