@ -22,6 +22,10 @@ static bool check_fail(void) {
struct command * cmd ;
struct command_result {
} ;
static struct command_result cmd_failed ;
struct command_result * command_fail ( struct command * cmd ,
int code , const char * fmt , . . . )
{
@ -30,7 +34,7 @@ struct command_result *command_fail(struct command *cmd,
va_start ( ap , fmt ) ;
fail_msg = tal_vfmt ( cmd , fmt , ap ) ;
va_end ( ap ) ;
return NULL ;
return & cmd_failed ;
}
/* AUTOGENERATED MOCKS START */
@ -144,8 +148,8 @@ static void stest(const struct json *j, struct sanity *b)
u64 * ival ;
double * dval ;
if ( ! param ( cmd , j - > buffer , j - > toks ,
p_req ( " u64 " , json_tok _u64, & ival ) ,
p_req ( " double " , json_tok _double, & dval ) , NULL ) ) {
p_req ( " u64 " , param _u64, & ival ) ,
p_req ( " double " , param _double, & dval ) , NULL ) ) {
assert ( check_fail ( ) ) ;
assert ( b - > failed = = true ) ;
if ( ! strstr ( fail_msg , b - > fail_str ) ) {
@ -182,7 +186,7 @@ static void tok_tok(void)
struct json * j = json_parse ( cmd , " { 'satoshi', '546' } " ) ;
assert ( param ( cmd , j - > buffer , j - > toks ,
p_req ( " satoshi " , json_tok _tok, & tok ) , NULL ) ) ;
p_req ( " satoshi " , param _tok, & tok ) , NULL ) ) ;
assert ( tok ) ;
assert ( json_to_number ( j - > buffer , tok , & n ) ) ;
assert ( n = = 546 ) ;
@ -194,7 +198,7 @@ static void tok_tok(void)
struct json * j = json_parse ( cmd , " {} " ) ;
assert ( param ( cmd , j - > buffer , j - > toks ,
p_opt ( " satoshi " , json_tok _tok, & tok ) , NULL ) ) ;
p_opt ( " satoshi " , param _tok, & tok ) , NULL ) ) ;
/* make sure it *is* NULL */
assert ( tok = = NULL ) ;
@ -211,8 +215,8 @@ static void dup_names(void)
u64 * i ;
double * d ;
assert ( ! param ( cmd , j - > buffer , j - > toks ,
p_req ( " u64 " , json_tok _u64, & i ) ,
p_req ( " double " , json_tok _double, & d ) , NULL ) ) ;
p_req ( " u64 " , param _u64, & i ) ,
p_req ( " double " , param _double, & d ) , NULL ) ) ;
}
static void null_params ( void )
@ -223,13 +227,13 @@ static void null_params(void)
json_parse ( cmd , " [ '10', '11', '12', '13', '14', '15', '16'] " ) ;
assert ( param ( cmd , j - > buffer , j - > toks ,
p_req ( " 0 " , json_tok _u64, & intptrs [ 0 ] ) ,
p_req ( " 1 " , json_tok _u64, & intptrs [ 1 ] ) ,
p_req ( " 2 " , json_tok _u64, & intptrs [ 2 ] ) ,
p_req ( " 3 " , json_tok _u64, & intptrs [ 3 ] ) ,
p_opt_def ( " 4 " , json_tok _u64, & intptrs [ 4 ] , 999 ) ,
p_opt ( " 5 " , json_tok _u64, & intptrs [ 5 ] ) ,
p_opt ( " 6 " , json_tok _u64, & intptrs [ 6 ] ) ,
p_req ( " 0 " , param _u64, & intptrs [ 0 ] ) ,
p_req ( " 1 " , param _u64, & intptrs [ 1 ] ) ,
p_req ( " 2 " , param _u64, & intptrs [ 2 ] ) ,
p_req ( " 3 " , param _u64, & intptrs [ 3 ] ) ,
p_opt_def ( " 4 " , param _u64, & intptrs [ 4 ] , 999 ) ,
p_opt ( " 5 " , param _u64, & intptrs [ 5 ] ) ,
p_opt ( " 6 " , param _u64, & intptrs [ 6 ] ) ,
NULL ) ) ;
for ( int i = 0 ; i < tal_count ( intptrs ) ; + + i ) {
assert ( intptrs [ i ] ) ;
@ -239,13 +243,13 @@ static void null_params(void)
/* missing at end */
j = json_parse ( cmd , " [ '10', '11', '12', '13', '14'] " ) ;
assert ( param ( cmd , j - > buffer , j - > toks ,
p_req ( " 0 " , json_tok _u64, & intptrs [ 0 ] ) ,
p_req ( " 1 " , json_tok _u64, & intptrs [ 1 ] ) ,
p_req ( " 2 " , json_tok _u64, & intptrs [ 2 ] ) ,
p_req ( " 3 " , json_tok _u64, & intptrs [ 3 ] ) ,
p_opt ( " 4 " , json_tok _u64, & intptrs [ 4 ] ) ,
p_opt ( " 5 " , json_tok _u64, & intptrs [ 5 ] ) ,
p_opt_def ( " 6 " , json_tok _u64, & intptrs [ 6 ] , 888 ) ,
p_req ( " 0 " , param _u64, & intptrs [ 0 ] ) ,
p_req ( " 1 " , param _u64, & intptrs [ 1 ] ) ,
p_req ( " 2 " , param _u64, & intptrs [ 2 ] ) ,
p_req ( " 3 " , param _u64, & intptrs [ 3 ] ) ,
p_opt ( " 4 " , param _u64, & intptrs [ 4 ] ) ,
p_opt ( " 5 " , param _u64, & intptrs [ 5 ] ) ,
p_opt_def ( " 6 " , param _u64, & intptrs [ 6 ] , 888 ) ,
NULL ) ) ;
assert ( * intptrs [ 0 ] = = 10 ) ;
assert ( * intptrs [ 1 ] = = 11 ) ;
@ -279,30 +283,30 @@ static void bad_programmer(void)
/* check for repeated names */
assert ( ! param ( cmd , j - > buffer , j - > toks ,
p_req ( " repeat " , json_tok _u64, & ival ) ,
p_req ( " double " , json_tok _double, & dval ) ,
p_req ( " repeat " , json_tok _u64, & ival2 ) , NULL ) ) ;
p_req ( " repeat " , param _u64, & ival ) ,
p_req ( " double " , param _double, & dval ) ,
p_req ( " repeat " , param _u64, & ival2 ) , NULL ) ) ;
assert ( check_fail ( ) ) ;
assert ( strstr ( fail_msg , " developer error " ) ) ;
assert ( ! param ( cmd , j - > buffer , j - > toks ,
p_req ( " repeat " , json_tok _u64, & ival ) ,
p_req ( " double " , json_tok _double, & dval ) ,
p_req ( " repeat " , json_tok _u64, & ival ) , NULL ) ) ;
p_req ( " repeat " , param _u64, & ival ) ,
p_req ( " double " , param _double, & dval ) ,
p_req ( " repeat " , param _u64, & ival ) , NULL ) ) ;
assert ( check_fail ( ) ) ;
assert ( strstr ( fail_msg , " developer error " ) ) ;
assert ( ! param ( cmd , j - > buffer , j - > toks ,
p_req ( " u64 " , json_tok _u64, & ival ) ,
p_req ( " repeat " , json_tok _double, & dval ) ,
p_req ( " repeat " , json_tok _double, & dval ) , NULL ) ) ;
p_req ( " u64 " , param _u64, & ival ) ,
p_req ( " repeat " , param _double, & dval ) ,
p_req ( " repeat " , param _double, & dval ) , NULL ) ) ;
assert ( check_fail ( ) ) ;
assert ( strstr ( fail_msg , " developer error " ) ) ;
/* check for repeated arguments */
assert ( ! param ( cmd , j - > buffer , j - > toks ,
p_req ( " u64 " , json_tok _u64, & ival ) ,
p_req ( " repeated-arg " , json_tok _u64, & ival ) , NULL ) ) ;
p_req ( " u64 " , param _u64, & ival ) ,
p_req ( " repeated-arg " , param _u64, & ival ) , NULL ) ) ;
assert ( check_fail ( ) ) ;
assert ( strstr ( fail_msg , " developer error " ) ) ;
@ -316,10 +320,10 @@ static void bad_programmer(void)
unsigned int * msatoshi ;
double * riskfactor ;
assert ( ! param ( cmd , j - > buffer , j - > toks ,
p_req ( " u64 " , json_tok _u64, & ival ) ,
p_req ( " double " , json_tok _double, & dval ) ,
p_opt_def ( " msatoshi " , json_tok _number, & msatoshi , 100 ) ,
p_req ( " riskfactor " , json_tok _double, & riskfactor ) , NULL ) ) ;
p_req ( " u64 " , param _u64, & ival ) ,
p_req ( " double " , param _double, & dval ) ,
p_opt_def ( " msatoshi " , param _number, & msatoshi , 100 ) ,
p_req ( " riskfactor " , param _double, & riskfactor ) , NULL ) ) ;
assert ( * msatoshi ) ;
assert ( * msatoshi = = 100 ) ;
assert ( check_fail ( ) ) ;
@ -340,8 +344,8 @@ static void add_members(struct param **params,
tal_append_fmt ( obj , " \" %i \" : %i " , i , i ) ;
tal_append_fmt ( arr , " %i " , i ) ;
param_add ( params , name , true ,
typesafe_cb_preargs ( bool , void * * ,
json_tok _number,
typesafe_cb_preargs ( struct command_result * , void * * ,
param _number,
& ints [ i ] ,
struct command * ,
const char * ,
@ -368,7 +372,7 @@ static void five_hundred_params(void)
/* first test object version */
struct json * j = json_parse ( params , obj ) ;
assert ( param_arr ( cmd , j - > buffer , j - > toks , params , false ) ) ;
assert ( param_arr ( cmd , j - > buffer , j - > toks , params , false ) = = NULL ) ;
for ( int i = 0 ; i < tal_count ( ints ) ; + + i ) {
assert ( ints [ i ] ) ;
assert ( * ints [ i ] = = i ) ;
@ -377,7 +381,7 @@ static void five_hundred_params(void)
/* now test array */
j = json_parse ( params , arr ) ;
assert ( param_arr ( cmd , j - > buffer , j - > toks , params , false ) ) ;
assert ( param_arr ( cmd , j - > buffer , j - > toks , params , false ) = = NULL ) ;
for ( int i = 0 ; i < tal_count ( ints ) ; + + i ) {
assert ( * ints [ i ] = = i ) ;
}
@ -394,10 +398,10 @@ static void sendpay(void)
unsigned * cltv ;
if ( ! param ( cmd , j - > buffer , j - > toks ,
p_req ( " route " , json_tok _tok, & routetok ) ,
p_req ( " cltv " , json_tok _number, & cltv ) ,
p_opt ( " note " , json_tok _tok, & note ) ,
p_opt ( " msatoshi " , json_tok _u64, & msatoshi ) ,
p_req ( " route " , param _tok, & routetok ) ,
p_req ( " cltv " , param _number, & cltv ) ,
p_opt ( " note " , param _tok, & note ) ,
p_opt ( " msatoshi " , param _u64, & msatoshi ) ,
NULL ) )
assert ( false ) ;
@ -417,10 +421,10 @@ static void sendpay_nulltok(void)
unsigned * cltv ;
if ( ! param ( cmd , j - > buffer , j - > toks ,
p_req ( " route " , json_tok _tok, & routetok ) ,
p_req ( " cltv " , json_tok _number, & cltv ) ,
p_opt ( " note " , json_tok _tok, & note ) ,
p_opt ( " msatoshi " , json_tok _u64, & msatoshi ) ,
p_req ( " route " , param _tok, & routetok ) ,
p_req ( " cltv " , param _number, & cltv ) ,
p_opt ( " note " , param _tok, & note ) ,
p_opt ( " msatoshi " , param _u64, & msatoshi ) ,
NULL ) )
assert ( false ) ;
@ -439,11 +443,11 @@ static void advanced(void)
const jsmntok_t * tok ;
assert ( param ( cmd , j - > buffer , j - > toks ,
p_req ( " description " , json_tok _label, & label ) ,
p_req ( " msat " , json_tok _msat, & msat ) ,
p_req ( " tok " , json_tok _tok, & tok ) ,
p_opt ( " msat_opt1 " , json_tok _msat, & msat_opt1 ) ,
p_opt ( " msat_opt2 " , json_tok _msat, & msat_opt2 ) ,
p_req ( " description " , param _label, & label ) ,
p_req ( " msat " , param _msat, & msat ) ,
p_req ( " tok " , param _tok, & tok ) ,
p_opt ( " msat_opt1 " , param _msat, & msat_opt1 ) ,
p_opt ( " msat_opt2 " , param _msat, & msat_opt2 ) ,
NULL ) ) ;
assert ( label ! = NULL ) ;
assert ( streq ( label - > s , " lightning " ) ) ;
@ -457,8 +461,8 @@ static void advanced(void)
struct json * j = json_parse ( cmd , " [ 3 'foo' ] " ) ;
struct json_escaped * label , * foo ;
assert ( param ( cmd , j - > buffer , j - > toks ,
p_req ( " label " , json_tok _label, & label ) ,
p_opt ( " foo " , json_tok _label, & foo ) ,
p_req ( " label " , param _label, & label ) ,
p_opt ( " foo " , param _label, & foo ) ,
NULL ) ) ;
assert ( streq ( label - > s , " 3 " ) ) ;
assert ( streq ( foo - > s , " foo " ) ) ;
@ -468,8 +472,8 @@ static void advanced(void)
u64 * msat2 ;
struct json * j = json_parse ( cmd , " [ 3 ] " ) ;
assert ( param ( cmd , j - > buffer , j - > toks ,
p_opt_def ( " msat " , json_tok _msat, & msat , 23 ) ,
p_opt_def ( " msat2 " , json_tok _msat, & msat2 , 53 ) ,
p_opt_def ( " msat " , param _msat, & msat , 23 ) ,
p_opt_def ( " msat2 " , param _msat, & msat2 , 53 ) ,
NULL ) ) ;
assert ( * msat = = 3 ) ;
assert ( msat2 ) ;
@ -483,7 +487,7 @@ static void advanced_fail(void)
struct json * j = json_parse ( cmd , " [ 'anyx' ] " ) ;
u64 * msat ;
assert ( ! param ( cmd , j - > buffer , j - > toks ,
p_req ( " msat " , json_tok _msat, & msat ) ,
p_req ( " msat " , param _msat, & msat ) ,
NULL ) ) ;
assert ( check_fail ( ) ) ;
assert ( strstr ( fail_msg , " 'msat' should be a positive "
@ -495,31 +499,31 @@ static void advanced_fail(void)
{ \
struct json * j = json_parse ( cmd , json_ ) ; \
T * v ; \
bool ret = cb ( cmd , " name " , j - > buffer , j - > toks + 1 , & v ) ; \
assert ( ret = = pass ) ; \
if ( ret ) { \
struct command_result * ret = cb ( cmd , " name " , j - > buffer , j - > toks + 1 , & v ) ; \
assert ( ( ret = = NULL ) = = pass ) ; \
if ( ret = = NULL ) { \
assert ( v ) ; \
assert ( * v = = value ) ; \
} \
}
static void json_tok _tests( void )
static void param _tests( void )
{
test_cb ( json_tok _bool, bool , " [ true ] " , true , true ) ;
test_cb ( json_tok _bool, bool , " [ false ] " , false , true ) ;
test_cb ( json_tok _bool, bool , " [ tru ] " , false , false ) ;
test_cb ( json_tok _bool, bool , " [ 1 ] " , false , false ) ;
test_cb ( json_tok _percent, double , " [ -0.01 ] " , 0 , false ) ;
test_cb ( json_tok _percent, double , " [ 0.00 ] " , 0 , true ) ;
test_cb ( json_tok _percent, double , " [ 1 ] " , 1 , true ) ;
test_cb ( json_tok _percent, double , " [ 1.1 ] " , 1.1 , true ) ;
test_cb ( json_tok _percent, double , " [ 1.01 ] " , 1.01 , true ) ;
test_cb ( json_tok _percent, double , " [ 99.99 ] " , 99.99 , true ) ;
test_cb ( json_tok _percent, double , " [ 100.0 ] " , 100 , true ) ;
test_cb ( json_tok _percent, double , " [ 100.001 ] " , 100.001 , true ) ;
test_cb ( json_tok _percent, double , " [ 1000 ] " , 1000 , true ) ;
test_cb ( json_tok _percent, double , " [ 'wow' ] " , 0 , false ) ;
test_cb ( param _bool, bool , " [ true ] " , true , true ) ;
test_cb ( param _bool, bool , " [ false ] " , false , true ) ;
test_cb ( param _bool, bool , " [ tru ] " , false , false ) ;
test_cb ( param _bool, bool , " [ 1 ] " , false , false ) ;
test_cb ( param _percent, double , " [ -0.01 ] " , 0 , false ) ;
test_cb ( param _percent, double , " [ 0.00 ] " , 0 , true ) ;
test_cb ( param _percent, double , " [ 1 ] " , 1 , true ) ;
test_cb ( param _percent, double , " [ 1.1 ] " , 1.1 , true ) ;
test_cb ( param _percent, double , " [ 1.01 ] " , 1.01 , true ) ;
test_cb ( param _percent, double , " [ 99.99 ] " , 99.99 , true ) ;
test_cb ( param _percent, double , " [ 100.0 ] " , 100 , true ) ;
test_cb ( param _percent, double , " [ 100.001 ] " , 100.001 , true ) ;
test_cb ( param _percent, double , " [ 1000 ] " , 1000 , true ) ;
test_cb ( param _percent, double , " [ 'wow' ] " , 0 , false ) ;
}
static void test_invoice ( struct command * cmd ,
@ -536,12 +540,12 @@ static void test_invoice(struct command *cmd,
assert ( cmd - > mode = = CMD_USAGE ) ;
if ( ! param ( cmd , buffer , params ,
p_req ( " msatoshi " , json_tok _msat, & msatoshi_val ) ,
p_req ( " label " , json_tok _label, & label_val ) ,
p_req ( " description " , json_tok _escaped_string, & desc_val ) ,
p_opt ( " expiry " , json_tok _u64, & expiry ) ,
p_opt ( " fallbacks " , json_tok _array, & fallbacks ) ,
p_opt ( " preimage " , json_tok _tok, & preimagetok ) , NULL ) )
p_req ( " msatoshi " , param _msat, & msatoshi_val ) ,
p_req ( " label " , param _label, & label_val ) ,
p_req ( " description " , param _escaped_string, & desc_val ) ,
p_opt ( " expiry " , param _u64, & expiry ) ,
p_opt ( " fallbacks " , param _array, & fallbacks ) ,
p_opt ( " preimage " , param _tok, & preimagetok ) , NULL ) )
return ;
/* should not be here since we are in the mode of CMD_USAGE
@ -583,7 +587,7 @@ int main(void)
sendpay_nulltok ( ) ;
advanced ( ) ;
advanced_fail ( ) ;
json_tok _tests( ) ;
param _tests( ) ;
usage ( ) ;
tal_free ( tmpctx ) ;