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 {
	ovsec_kadm_init "%s/a" "%s/a" $OVSEC_KADM_ADMIN_SERVICE null \
		$OVSEC_KADM_STRUCT_VERSION $OVSEC_KADM_API_VERSION_1 \
		server_handle
    } $test $test]]} {
	error "$test: unexpected failure in init"
	return
    }
    one_line_fail_test [format {
	ovsec_kadm_randkey_principal $server_handle "%s/a" key
    } $test] "PASS_TOOSOON"
    if { ! [cmd {ovsec_kadm_destroy $server_handle}]} {
	error "$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 {
	ovsec_kadm_init "%s/a" "%s/a" $OVSEC_KADM_CHANGEPW_SERVICE null \
		$OVSEC_KADM_STRUCT_VERSION $OVSEC_KADM_API_VERSION_1 \
		server_handle
    } $test $test]]} {
	error "$test: unexpected failure in init"
	return
    }
    one_line_fail_test [format {
	ovsec_kadm_randkey_principal $server_handle "%s/a" key
    } $test] "PASS_TOOSOON"
    if { ! [cmd {ovsec_kadm_destroy $server_handle}]} {
	error "$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 {
	ovsec_kadm_init admin admin $OVSEC_KADM_ADMIN_SERVICE null \
		$OVSEC_KADM_STRUCT_VERSION $OVSEC_KADM_API_VERSION_1 \
		server_handle
    }]} {
	error "$test: unexpected failure in init"
	return
    }
    if {! [cmd [format {
	ovsec_kadm_modify_principal $server_handle [princ_w_pol "%s/a" \
		once-a-min] OVSEC_KADM_POLICY
    } $test]]} {
	error "$test: failed modify"
	return
    }
    one_line_succeed_test [format {
	ovsec_kadm_randkey_principal $server_handle "%s/a" key
    } $test]
    if { ! [cmd {ovsec_kadm_destroy $server_handle}]} {
	error "$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 {
	ovsec_kadm_init admin admin $OVSEC_KADM_CHANGEPW_SERVICE null \
		$OVSEC_KADM_STRUCT_VERSION $OVSEC_KADM_API_VERSION_1 \
		server_handle
    }]} {
	error "$test: unexpected failure in init"
	return
    }
    one_line_fail_test [format {
	ovsec_kadm_randkey_principal $server_handle "%s/a" key
    } $test] "AUTH_CHANGEPW"
    if { ! [cmd {ovsec_kadm_destroy $server_handle}]} {
	error "$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 {
	ovsec_kadm_init admin admin $OVSEC_KADM_ADMIN_SERVICE null \
		$OVSEC_KADM_STRUCT_VERSION $OVSEC_KADM_API_VERSION_1 \
		server_handle
    }]} {
	error "$test: unexpected failure in init"
	return
    }
    one_line_succeed_test [format {
	ovsec_kadm_randkey_principal $server_handle "%s/a" key
    } $test]
    if { ! [cmd {ovsec_kadm_destroy $server_handle}]} {
	error "$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 {
	ovsec_kadm_init admin admin $OVSEC_KADM_CHANGEPW_SERVICE null \
		$OVSEC_KADM_STRUCT_VERSION $OVSEC_KADM_API_VERSION_1 \
		server_handle
    }]} {
	error "$test: unexpected failure in init"
	return
    }
    one_line_fail_test [format {
	ovsec_kadm_randkey_principal $server_handle "%s/a" key
    } $test] "AUTH"
    if { ! [cmd {ovsec_kadm_destroy $server_handle}]} {
	error "$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 {
	ovsec_kadm_init admin/modify admin $OVSEC_KADM_ADMIN_SERVICE null \
		$OVSEC_KADM_STRUCT_VERSION $OVSEC_KADM_API_VERSION_1 \
		server_handle
    }]} {
	error "$test: unexpected failure in init"
	return
    }
    one_line_succeed_test [format {
	ovsec_kadm_randkey_principal $server_handle "%s/a" key
    } $test]
    if { ! [cmd {ovsec_kadm_destroy $server_handle}]} {
	error "$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 {
	ovsec_kadm_init "%s/a" "%s/a" $OVSEC_KADM_CHANGEPW_SERVICE null \
		$OVSEC_KADM_STRUCT_VERSION $OVSEC_KADM_API_VERSION_1 \
		server_handle
    } $test $test]]} {
	error "$test: unexpected failure in init"
	return
    }
    one_line_succeed_test [format {
	ovsec_kadm_randkey_principal $server_handle "%s/a" key
    } $test]
    if { ! [cmd {ovsec_kadm_destroy $server_handle}]} {
	error "$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 {
	ovsec_kadm_init "%s/a" "%s/a" $OVSEC_KADM_ADMIN_SERVICE null \
		$OVSEC_KADM_STRUCT_VERSION $OVSEC_KADM_API_VERSION_1 \
		server_handle
    } $test $test]]} {
	error "$test: unexpected failure in init"
	return
    }
    one_line_succeed_test [format {
	ovsec_kadm_randkey_principal $server_handle "%s/a" key
    } $test]
    if { ! [cmd {ovsec_kadm_destroy $server_handle}]} {
	error "$test: unexpected failure in destroy"
	return
    }
}
test31

test "randkey-principal 32"
proc test32 {} {
    global test

    if { ! [cmd {
	ovsec_kadm_init admin admin $OVSEC_KADM_ADMIN_SERVICE null \
		$OVSEC_KADM_STRUCT_VERSION $OVSEC_KADM_API_VERSION_1 \
		server_handle
    }]} {
	error "$test: unexpected failure in init"
	return
    }
    one_line_fail_test {
	ovsec_kadm_randkey_principal $server_handle kadmin/history key
    } "PROTECT"
    if { ! [cmd {ovsec_kadm_destroy $server_handle}]} {
	error "$test: unexpected failure in destroy"
	return
    }
}
test32

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 {
	ovsec_kadm_init admin admin $OVSEC_KADM_ADMIN_SERVICE null \
		$OVSEC_KADM_STRUCT_VERSION $OVSEC_KADM_API_VERSION_1 \
		server_handle
    }]} {
	error "$test: unexpected failure in init"
	return
    }
    one_line_fail_test [format {
	ovsec_kadm_randkey_principal null "%s/a" key
    } $test] "BAD_SERVER_HANDLE"
    if { ! [cmd {ovsec_kadm_destroy $server_handle}]} {
	error "$test: unexpected failure in destroy"
	return
    }
}

test33

return ""
