load_lib lib.t
api_exit
api_start

test "randkey-principal 1"
proc test1 {} {
    global test
    if {! (( ! [principal_exists "$test/a"]) ||
	   [delete_principal "$test/a"])} {
	    error_and_restart "$test: couldn't create principal \"$test/a\""
	    return
    }
    if {! [create_principal_pol "$test/a" once-a-min]} {
	error_and_restart "$test: creating principal"
	return
    }
    
    if {! [cmd [format {
	kadm5_init "%s/a" "%s/a" $KADM5_ADMIN_SERVICE null \
		$KADM5_STRUCT_VERSION $KADM5_API_VERSION_3 \
		server_handle
    } $test $test]]} {
	perror "$test: unexpected failure in init"
	return
    }
    one_line_fail_test [format {
	kadm5_randkey_principal $server_handle "%s/a" keys num_keys
    } $test] "PASS_TOOSOON"
    if { ! [cmd {kadm5_destroy $server_handle}]} {
	perror "$test: unexpected failure in destroy"
	return
    }
}
if {$RPC} { test1 } 

test "randkey-principal 3"
proc test3 {} {
    global test
    if {! (( ! [principal_exists "$test/a"]) ||
	   [delete_principal "$test/a"])} {
	    error_and_restart "$test: couldn't create principal \"$test/a\""
	    return
    }
    if {! [create_principal_pol "$test/a" once-a-min]} {
	error_and_restart "$test: creating principal"
	return
    }
    
    if {! [cmd [format {
	kadm5_init "%s/a" "%s/a" $KADM5_CHANGEPW_SERVICE null \
		$KADM5_STRUCT_VERSION $KADM5_API_VERSION_3 \
		server_handle
    } $test $test]]} {
	perror "$test: unexpected failure in init"
	return
    }
    one_line_fail_test [format {
	kadm5_randkey_principal $server_handle "%s/a" keys num_keys
    } $test] "PASS_TOOSOON"
    if { ! [cmd {kadm5_destroy $server_handle}]} {
	perror "$test: unexpected failure in destroy"
	return
    }
}
if ${RPC} { test3 } 

test "randkey-principal 13"
proc test13 {} {
    global test
    if {! ((  [principal_exists "$test/a"]) ||
	   [create_principal "$test/a"])} {
	    error_and_restart "$test: couldn't create principal \"$test/a\""
	    return
    }
    if {! [cmd {
	kadm5_init admin admin $KADM5_ADMIN_SERVICE null \
		$KADM5_STRUCT_VERSION $KADM5_API_VERSION_3 \
		server_handle
    }]} {
	perror "$test: unexpected failure in init"
	return
    }
    if {! [cmd [format {
	kadm5_modify_principal $server_handle [princ_w_pol "%s/a" \
		once-a-min] KADM5_POLICY
    } $test]]} {
	perror "$test: failed modify"
	return
    }
    one_line_succeed_test [format {
	kadm5_randkey_principal $server_handle "%s/a" keys num_keys
    } $test]
    if { ! [cmd {kadm5_destroy $server_handle}]} {
	perror "$test: unexpected failure in destroy"
	return
    }
}
test13

test "randkey-principal 15"
proc test15 {} {
    global test
    if {! (( ! [principal_exists "$test/a"]) ||
	   [delete_principal "$test/a"])} {
	    error_and_restart "$test: couldn't create principal \"$test/a\""
	    return
    }
    if {! [create_principal_pol "$test/a" once-a-min]} {
	error_and_restart "$test: creating principal"
	return
    }
    
    if {! [cmd {
	kadm5_init admin admin $KADM5_CHANGEPW_SERVICE null \
		$KADM5_STRUCT_VERSION $KADM5_API_VERSION_3 \
		server_handle
    }]} {
	perror "$test: unexpected failure in init"
	return
    }
    one_line_fail_test [format {
	kadm5_randkey_principal $server_handle "%s/a" keys num_keys
    } $test] "AUTH_CHANGEPW"
    if { ! [cmd {kadm5_destroy $server_handle}]} {
	perror "$test: unexpected failure in destroy"
	return
    }
}
if { $RPC } { test15 }

test "randkey-principal 28"
proc test28 {} {
    global test
    if {! ((  [principal_exists "$test/a"]) ||
	   [create_principal "$test/a"])} {
	    error_and_restart "$test: couldn't create principal \"$test/a\""
	    return
    }
    if {! [cmd {
	kadm5_init admin admin $KADM5_ADMIN_SERVICE null \
		$KADM5_STRUCT_VERSION $KADM5_API_VERSION_3 \
		server_handle
    }]} {
	perror "$test: unexpected failure in init"
	return
    }
    one_line_succeed_test [format {
	kadm5_randkey_principal $server_handle "%s/a" keys num_keys
    } $test]
    if { ! [cmd {kadm5_destroy $server_handle}]} {
	perror "$test: unexpected failure in destroy"
	return
    }
}
test28

test "randkey-principal 28.25"
proc test2825 {} {
    global test
    if {! ((  [principal_exists "$test/a"]) ||
	   [create_principal "$test/a"])} {
	    error_and_restart "$test: couldn't create principal \"$test/a\""
	    return
    }
    if {! [cmd {
	kadm5_init admin admin $KADM5_CHANGEPW_SERVICE null \
		$KADM5_STRUCT_VERSION $KADM5_API_VERSION_3 \
		server_handle
    }]} {
	perror "$test: unexpected failure in init"
	return
    }
    one_line_fail_test [format {
	kadm5_randkey_principal $server_handle "%s/a" keys num_keys
    } $test] "AUTH"
    if { ! [cmd {kadm5_destroy $server_handle}]} {
	perror "$test: unexpected failure in destroy"
	return
    }
}
if {$RPC} { test2825 }

test "randkey-principal 28.5"
proc test285 {} {
    global test
    if {! ((  [principal_exists "$test/a"]) ||
	   [create_principal "$test/a"])} {
	    error_and_restart "$test: couldn't create principal \"$test/a\""
	    return
    }
    if {! [cmd {
	kadm5_init admin/modify admin $KADM5_ADMIN_SERVICE null \
		$KADM5_STRUCT_VERSION $KADM5_API_VERSION_3 \
		server_handle
    }]} {
	perror "$test: unexpected failure in init"
	return
    }
    one_line_succeed_test [format {
	kadm5_randkey_principal $server_handle "%s/a" keys num_keys
    } $test]
    if { ! [cmd {kadm5_destroy $server_handle}]} {
	perror "$test: unexpected failure in destroy"
	return
    }
}
test285

test "randkey-principal 30"
proc test30 {} {
    global test
    if {! (( ! [principal_exists "$test/a"]) ||
	   [delete_principal "$test/a"])} {
	    error_and_restart "$test: couldn't delete principal \"$test/a\""
	    return
    }
    if {! [create_principal "$test/a"]} {
	error_and_restart "$test: creating principal"
	return
    }
    if {! [cmd [format {
	kadm5_init "%s/a" "%s/a" $KADM5_CHANGEPW_SERVICE null \
		$KADM5_STRUCT_VERSION $KADM5_API_VERSION_3 \
		server_handle
    } $test $test]]} {
	perror "$test: unexpected failure in init"
	return
    }
    one_line_succeed_test [format {
	kadm5_randkey_principal $server_handle "%s/a" keys num_keys
    } $test]
    if { ! [cmd {kadm5_destroy $server_handle}]} {
	perror "$test: unexpected failure in destroy"
	return
    }
}
test30

test "randkey-principal 31"
proc test31 {} {
    global test
    if {! (( ! [principal_exists "$test/a"]) ||
	   [delete_principal "$test/a"])} {
	    error_and_restart "$test: couldn't create principal \"$test/a\""
	    return
    }
    if {! [create_principal "$test/a"]} {
	error_and_restart "$test: creating principal"
	return
    }
    
    if {! [cmd [format {
	kadm5_init "%s/a" "%s/a" $KADM5_ADMIN_SERVICE null \
		$KADM5_STRUCT_VERSION $KADM5_API_VERSION_3 \
		server_handle
    } $test $test]]} {
	perror "$test: unexpected failure in init"
	return
    }
    one_line_succeed_test [format {
	kadm5_randkey_principal $server_handle "%s/a" keys num_keys
    } $test]
    if { ! [cmd {kadm5_destroy $server_handle}]} {
	perror "$test: unexpected failure in destroy"
	return
    }
}
test31

test "randkey-principal 33"
proc test33 {} {
    global test
    if {! ((  [principal_exists "$test/a"]) ||
	   [create_principal "$test/a"])} {
	    error_and_restart "$test: couldn't create principal \"$test/a\""
	    return
    }
    if { ! [cmd {
	kadm5_init admin admin $KADM5_ADMIN_SERVICE null \
		$KADM5_STRUCT_VERSION $KADM5_API_VERSION_3 \
		server_handle
    }]} {
	perror "$test: unexpected failure in init"
	return
    }
    one_line_fail_test [format {
	kadm5_randkey_principal null "%s/a" keys num_keys
    } $test] "BAD_SERVER_HANDLE"
    if { ! [cmd {kadm5_destroy $server_handle}]} {
	perror "$test: unexpected failure in destroy"
	return
    }
}

test33

return ""
