aboutsummaryrefslogtreecommitdiff
path: root/src/lib/rpc/unit-test/lib/helpers.exp
blob: c4b76aa0ce2fb36188e1bf35a408098688a34dba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
if {[info commands exp_version] != {}} {
	set exp_version_4 [regexp {^4} [exp_version]]
} else {
	set exp_version_4 [regexp {^4} [expect_version]]
}

# Backward compatibility until we're using expect 5 everywhere
if {$exp_version_4} {
	global wait_error_index wait_errno_index wait_status_index
	set wait_error_index 0
	set wait_errno_index 1
	set wait_status_index 1
} else {
	set wait_error_index 2
	set wait_errno_index 3
	set wait_status_index 3
}
				

proc kinit {princ pass lifetime} {
	global kinit
	global wait_error_index wait_errno_index wait_status_index

	spawn -noecho $kinit -5 -l $lifetime $princ
	expect {
		-re "Password for $princ.*: " { send "$pass\n"; expect eof }
		timeout { perror "Timeout waiting for kinit"; close }
		eof
	}

	set ret [wait]
	if {[lindex $ret $wait_error_index] == -1} {
	 	perror \
	    "wait(kinit $princ) returned error [lindex $ret $wait_errno_index]"
	} else {
		if {[lindex $ret $wait_status_index] != 0} {
			perror \
		    "kinit $princ failed with [lindex $ret $wait_status_index]"
		}
	}
}

proc flush_server {} {
	global server_id
	global expect_out

	verbose "flushing server output" 1

	while {1} {
		set timeout 5

		expect {
			-i $server_id
			-re "^.+$" {
			    verbose "server output: $expect_out(buffer)"
			}
			timeout { break }
		}
	}
}

proc start_client {testname ccname user password lifetime count
		  {target ""}} {
	global env CLIENT PROT hostname spawn_id verbose

	if {$target == ""} {
		set target "server@$hostname"
	}

	set env(KRB5CCNAME) FILE:/tmp/krb5cc_rpc_test_$ccname
	kinit $user $password $lifetime

	if {$verbose > 0} {
		spawn $CLIENT -a 1 -s 1 -m 1 $PROT $hostname $target $count
	} else {
		spawn $CLIENT $PROT $hostname $target $count
	}

	verbose "$testname: client $ccname started"

	unset env(KRB5CCNAME)
}

proc eof_client {testname ccname id status} {
	verbose "$testname: eof'ing for client $ccname" 1

	expect {
		-i $id
		eof { verbose $expect_out(buffer) 1 }
		timeout { 
			fail "$testname: timeout waiting for client $ccname to exit"
		}
	}
	wait_client $testname $ccname $id $status
}


proc wait_client {testname ccname id status} {
	global env
	global kill
	global kdestroy
	global wait_error_index wait_errno_index wait_status_index

	verbose "$testname: waiting for client $ccname" 1

	set ret [wait -i $id]
	if {[lindex $ret $wait_error_index] == -1} {
		fail \
       "$testname: wait $ccname returned error [lindex $ret $wait_errno_index]"
	} else {
		if {[lindex $ret $wait_status_index] == $status} {
			pass "$testname: client $ccname"
		} else {
			fail "$testname: client $ccname: unexpected return status [lindex $ret $wait_status_index], should be $status."
		}
	}

	set env(KRB5CCNAME) FILE:/tmp/krb5cc_rpc_test_$ccname
 	if {[catch "exec $kdestroy -5"] != 0} {
 		perror "$testname: cannot destroy client $ccname ccache"
 	}

	unset env(KRB5CCNAME)
}