load_lib lib.t
api_exit
api_start

test "get-principal 100"
proc test100 {} {
    global test prompt

    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 {
	kadm5_get_principal $server_handle testuser p \
		{KADM5_PRINCIPAL_NORMAL_MASK}
    }]} {
	perror "$test: unexpected failure in get_principal"
    }
    send "lindex \$p 16\n"
    expect {
	-re "(\[0-9\]+)\n$prompt" { set num_keys $expect_out(1,string) }
	timeout {
	    error_and_restart "$test: timeout getting num_keys"
	    return
	}
	eof {
	    error_and_restart "$test: eof getting num_keys"
	    return
	}
    }
    send "lindex \$p 17\n"
    expect {
	-re "(\[0-9\]+)\n$prompt" { set num_tl $expect_out(1,string) }
	timeout {
	    error_and_restart "$test: timeout getting num_tl"
	    return
	}
	eof {
	    error_and_restart "$test: eof getting num_tl"
	    return
	}
    }
    send "lindex \$p 18\n"
    expect {
	-re "({.*})\n$prompt" {set key_data $expect_out(1,string) }
	-re "\n$prompt" { set key_data {} }
	timeout {
	    error_and_restart "$test: timeout getting key_data"
	    return
	}
	eof {
	    error_and_restart "$test: eof getting key_data"
	    return
	}
    }
    send "lindex \$p 19\n"
    expect {
	-re "({.*})\n$prompt" {set tl_data $expect_out(1,string) }
	-re "\n$prompt" { set tl_data {} }
	timeout {
	    error_and_restart "$test: timeout getting tl_data"
	    return
	}
	eof {
	    error_and_restart "$test: eof getting tl_data"
	    return
	}
    }
    
    set failed 0
    if {$num_keys != 0} {
	fail "$test: num_keys $num_keys should be 0"
	set failed 1
    }
    if {$num_tl != 0} {
	fail "$test: num_tl $num_tl should be 0"
	set failed 1
    }
    if {$key_data != {}} {
	fail "$test: key_data $key_data should be {}"
	set failed 1
    }
    if {$tl_data != "{}"} {
	fail "$test: tl_data $tl_data should be empty"
	set failed 1
    }
    if {$failed == 0} {
	pass "$test"
    }

    if { ! [cmd {kadm5_destroy $server_handle}]} {
	perror "$test: unexpected failure in destroy"
	return
    }
}
test100

proc test101_102 {rpc} {
    global test prompt

    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 {
	kadm5_get_principal $server_handle testuser p \
		{KADM5_PRINCIPAL_NORMAL_MASK KADM5_KEY_DATA}
    }]} {
	perror "$test: unexpected failure in get_principal"
    }
    send "lindex \$p 16\n"
    expect {
	-re "(\[0-9\]+)\n$prompt" { set num_keys $expect_out(1,string) }
	timeout {
	    error_and_restart "$test: timeout getting num_keys"
	    return
	}
	eof {
	    error_and_restart "$test: eof getting num_keys"
	    return
	}
    }
    send "lindex \$p 18\n"
    expect {
	-re "({.*})\n$prompt" {set key_data $expect_out(1,string) }
	-re "\n$prompt" { set key_data {} }
	timeout {
	    error_and_restart "$test: timeout getting key_data"
	    return
	}
	eof {
	    error_and_restart "$test: eof getting key_data"
	    return
	}
    }

    set failed 0
    if {$num_keys != 5} {
	fail "$test: num_keys $num_keys should be 5"
	set failed 1
    }
    for {set i 0} {$i < $num_keys} {incr i} {
	set key "[lindex [lindex $key_data $i] 2]"
	if {($rpc && [string compare $key ""] != 0) ||
	    ((! $rpc) && [string compare $key ""] == 0)} {
	    fail "$test: key_data $key is wrong"
	    set failed 1
	    
	}
    }
    if {$failed == 0} { pass "$test" }
    
    if { ! [cmd {kadm5_destroy $server_handle}]} {
	perror "$test: unexpected failure in destroy"
	return
    }
}
test "get-principal 101" 
if {$RPC} {test101_102 $RPC}
test "get-principal 102" 
if {! $RPC} {test101_102 $RPC}

test "get-principal 103"
proc test103 {} {
    global test prompt

    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 \
		"{%s/a} 0 0 0 0 {%s/a} 0 0 0 0 null 0 0 0 0 0 0 1 {} {{999 6 foobar}}" \
		{KADM5_TL_DATA}
    } $test $test]]} {
	fail "$test: cannot set TL_DATA"
	return
    }

    if {! [cmd [format {
	kadm5_get_principal $server_handle {%s/a} p \
		{KADM5_PRINCIPAL_NORMAL_MASK KADM5_TL_DATA}
    } $test]]} {
	perror "$test: unexpected failure in get_principal"
    }
    send "lindex \$p 17\n"
    expect {
	-re "(\[0-9\]+)\n$prompt" { set num_tl $expect_out(1,string) }
	timeout {
	    error_and_restart "$test: timeout getting num_tl"
	    return
	}
	eof {
	    error_and_restart "$test: eof getting num_tl"
	    return
	}
    }
    send "lindex \$p 19\n"
    expect {
	-re "({.*})\n$prompt" {set tl_data $expect_out(1,string) }
	-re "\n$prompt" { set tl_data {} }
	timeout {
	    error_and_restart "$test: timeout getting tl_data"
	    return
	}
	eof {
	    error_and_restart "$test: eof getting tl_data"
	    return
	}
    }
    
    if {$num_tl == 0} {
	fail "$test: num_tl $num_tl should not be 0"
    } elseif {$tl_data == "{{999 6 foobar}}"} {
	pass "$test"
    } else {
	fail "$test: tl_data $tl_data should be {{999 6 foobar}}"
    }

    if { ! [cmd {kadm5_destroy $server_handle}]} {
	perror "$test: unexpected failure in destroy"
	return
    }
}
test103

return ""




