~ubuntu-branches/ubuntu/oneiric/likewise-open/oneiric

« back to all changes in this revision

Viewing changes to krb5/src/lib/kadm5/unit-test/lib/lib.t

  • Committer: Bazaar Package Importer
  • Author(s): Scott Salley
  • Date: 2010-11-22 12:06:00 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20101122120600-8lba1fpceot71wlb
Tags: 6.0.0.53010-1
Likewise Open 6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
global timeout
 
2
set timeout 60
 
3
 
 
4
set lib_pid 0
 
5
 
 
6
#
 
7
# The functions in this library used to be responsible for bazillions
 
8
# of wasted api_starts.  Now, they all just use their own library
 
9
# handle so they are not interrupted when the main tests call init or
 
10
# destroy.  They have to keep track of when the api exists and
 
11
# restarts, though, since the lib_handle needs to be re-opened in that
 
12
# case.
 
13
#
 
14
proc lib_start_api {} {
 
15
    global spawn_id lib_pid test
 
16
 
 
17
    if {! [api_isrunning $lib_pid]} {
 
18
        api_exit
 
19
        set lib_pid [api_start]
 
20
        if {! [cmd {
 
21
            ovsec_kadm_init admin admin $OVSEC_KADM_ADMIN_SERVICE null \
 
22
                    $OVSEC_KADM_STRUCT_VERSION $OVSEC_KADM_API_VERSION_1 \
 
23
                    lib_handle
 
24
        }]} {
 
25
            error "$test: unexpected failure in init"
 
26
            return
 
27
        }
 
28
        verbose "+++ restarted api ($lib_pid) for lib"
 
29
    } else {
 
30
        verbose "+++ api $lib_pid already running for lib"
 
31
    }   
 
32
}
 
33
 
 
34
proc cmd {command} {
 
35
    global prompt
 
36
    global spawn_id
 
37
    global test
 
38
 
 
39
    send "[string trim $command]\n"
 
40
    expect {
 
41
        -re "OK .*$prompt$" { return 1 }
 
42
        -re "ERROR .*$prompt$" { return 0 }
 
43
        "wrong # args" { error "$test: wrong number args"; return 0 }
 
44
        timeout { fail "$test: timeout"; return 0 }
 
45
        eof { fail "$test: eof"; api_exit; lib_start_api; return 0 }
 
46
    }
 
47
}
 
48
 
 
49
proc tcl_cmd {command} {
 
50
    global prompt spawn_id test
 
51
 
 
52
    send "[string trim $command]\n"
 
53
    expect {
 
54
        -re "$prompt$" { return 1}
 
55
        "wrong # args" { error "$test: wrong number args"; return 0 }
 
56
        timeout { error_and_restart "timeout" }
 
57
        eof { api_exit; lib_start_api; return 0 }
 
58
    }
 
59
}
 
60
 
 
61
proc one_line_succeed_test {command} {
 
62
    global prompt
 
63
    global spawn_id
 
64
    global test
 
65
 
 
66
    send "[string trim $command]\n"
 
67
    expect {
 
68
        -re "OK .*$prompt$"             { pass "$test"; return 1 }
 
69
        -re "ERROR .*$prompt$" { 
 
70
                fail "$test: $expect_out(buffer)"; return 0
 
71
        }
 
72
        "wrong # args" { error "$test: wrong number args"; return 0 }
 
73
        timeout                         { fail "$test: timeout"; return 0 }
 
74
        eof                             { fail "$test: eof"; api_exit; lib_start_api; return 0 }
 
75
    }
 
76
}
 
77
 
 
78
proc one_line_fail_test {command code} {
 
79
    global prompt
 
80
    global spawn_id
 
81
    global test
 
82
 
 
83
    send "[string trim $command]\n"
 
84
    expect {
 
85
        -re "ERROR .*$code.*$prompt$"   { pass "$test"; return 1 }
 
86
        -re "ERROR .*$prompt$"  { fail "$test: bad failure"; return 0 }
 
87
        -re "OK .*$prompt$"             { fail "$test: bad success"; return 0 }
 
88
        "wrong # args" { error "$test: wrong number args"; return 0 }
 
89
        timeout                         { fail "$test: timeout"; return 0 }
 
90
        eof                             { fail "$test: eof"; api_exit; lib_start_api; return 0 }
 
91
    }
 
92
}
 
93
 
 
94
proc one_line_fail_test_nochk {command} {
 
95
    global prompt
 
96
    global spawn_id
 
97
    global test
 
98
 
 
99
    send "[string trim $command]\n"
 
100
    expect {
 
101
        -re "ERROR .*$prompt$"  { pass "$test:"; return 1 }
 
102
        -re "OK .*$prompt$"             { fail "$test: bad success"; return 0 }
 
103
        "wrong # args" { error "$test: wrong number args"; return 0 }
 
104
        timeout                         { fail "$test: timeout"; return 0 }
 
105
        eof                             { fail "$test: eof"; api_exit; lib_start_api; return 0 }
 
106
    }
 
107
}
 
108
 
 
109
proc resync {} {
 
110
    global prompt spawn_id test
 
111
 
 
112
    expect {
 
113
        -re "$prompt$"  {}
 
114
        "wrong # args" { error "$test: wrong number args"; return 0 }
 
115
        eof { api_exit; lib_start_api }
 
116
    }
 
117
}
 
118
 
 
119
proc create_principal {name} {
 
120
    lib_start_api
 
121
 
 
122
    set ret [cmd [format {
 
123
        ovsec_kadm_create_principal $lib_handle [simple_principal \
 
124
                "%s"] {OVSEC_KADM_PRINCIPAL} "%s"
 
125
    } $name $name]]
 
126
 
 
127
    return $ret
 
128
}
 
129
 
 
130
proc create_policy {name} {
 
131
    lib_start_api
 
132
 
 
133
    set ret [cmd [format {
 
134
            ovsec_kadm_create_policy $lib_handle [simple_policy "%s"] \
 
135
                    {OVSEC_KADM_POLICY}
 
136
        } $name $name]]
 
137
 
 
138
    return $ret
 
139
}
 
140
 
 
141
proc create_principal_pol {name policy} {
 
142
    lib_start_api
 
143
 
 
144
    set ret [cmd [format {
 
145
            ovsec_kadm_create_principal $lib_handle [princ_w_pol "%s" \
 
146
                    "%s"] {OVSEC_KADM_PRINCIPAL OVSEC_KADM_POLICY} "%s"
 
147
    } $name $policy $name]]
 
148
 
 
149
    return $ret
 
150
}
 
151
 
 
152
proc delete_principal {name} {
 
153
    lib_start_api
 
154
 
 
155
    set ret [cmd [format {
 
156
            ovsec_kadm_delete_principal $lib_handle "%s"
 
157
    } $name]]
 
158
 
 
159
    return $ret
 
160
}
 
161
 
 
162
proc delete_policy {name} {
 
163
    lib_start_api
 
164
 
 
165
    set ret [cmd [format {ovsec_kadm_delete_policy $lib_handle "%s"} $name]]
 
166
 
 
167
    return $ret
 
168
}
 
169
 
 
170
proc principal_exists {name} {
 
171
#    puts stdout "Starting principal_exists."
 
172
 
 
173
    lib_start_api
 
174
 
 
175
    set ret [cmd [format {
 
176
        ovsec_kadm_get_principal $lib_handle "%s" principal
 
177
    } $name]]
 
178
 
 
179
#   puts stdout "Finishing principal_exists."
 
180
 
 
181
    return $ret
 
182
}
 
183
 
 
184
proc policy_exists {name} {
 
185
    lib_start_api
 
186
 
 
187
#    puts stdout "Starting policy_exists."
 
188
 
 
189
    set ret [cmd [format {
 
190
            ovsec_kadm_get_policy $lib_handle "%s" policy
 
191
        } $name]]
 
192
 
 
193
#    puts stdout "Finishing policy_exists."
 
194
 
 
195
    return $ret
 
196
}
 
197
 
 
198
proc error_and_restart {error} {
 
199
    api_exit
 
200
    api_start
 
201
    perror $error
 
202
}
 
203
 
 
204
proc test {name} {
 
205
   global test verbose
 
206
 
 
207
   set test $name
 
208
   if {$verbose >= 1} {
 
209
        puts stdout "At $test"
 
210
   }
 
211
}
 
212
 
 
213
proc begin_dump {} {
 
214
    global TOP
 
215
    global RPC
 
216
    
 
217
    if { ! $RPC } {
 
218
#       exec $env(SIMPLE_DUMP) > /tmp/dump.before
 
219
    }
 
220
}
 
221
 
 
222
proc end_dump_compare {name} {
 
223
    global  file
 
224
    global  TOP
 
225
    global  RPC
 
226
 
 
227
    if { ! $RPC } { 
 
228
#       set file $TOP/admin/lib/unit-test/diff-files/$name
 
229
#       exec $env(SIMPLE_DUMP) > /tmp/dump.after
 
230
#       exec $env(COMPARE_DUMP) /tmp/dump.before /tmp/dump.after $file
 
231
    }
 
232
}
 
233
 
 
234
proc kinit { princ pass {opts ""} } {
 
235
        global env;
 
236
        global KINIT
 
237
 
 
238
        eval spawn $KINIT -5 $opts $princ
 
239
        expect {
 
240
                -re {Password for .*: $}
 
241
                    {send "$pass\n"}
 
242
                timeout {puts "Timeout waiting for prompt" ; close }
 
243
        }
 
244
 
 
245
        # this necessary so close(1) in the child will not sleep waiting for
 
246
        # the parent, which is us, to read pending data.
 
247
 
 
248
        expect {
 
249
                "when initializing cache" { error "kinit failed: $expect_out(buffer)" }
 
250
                eof {}
 
251
        }
 
252
        wait
 
253
}
 
254
 
 
255
proc kdestroy {} {
 
256
        global KDESTROY
 
257
        global errorCode errorInfo
 
258
        global env
 
259
 
 
260
        if {[info exists errorCode]} {
 
261
                set saveErrorCode $errorCode
 
262
        }
 
263
        if {[info exists errorInfo]} {
 
264
                set saveErrorInfo $errorInfo
 
265
        }
 
266
        catch "system $KDESTROY -5 2>/dev/null"
 
267
        if {[info exists saveErrorCode]} {
 
268
                set errorCode $saveErrorCode
 
269
        } elseif {[info exists errorCode]} {
 
270
                unset errorCode
 
271
        }
 
272
        if {[info exists saveErrorInfo]} {
 
273
                set errorInfo $saveErrorInfo
 
274
        } elseif {[info exists errorInfo]} {
 
275
                unset errorInfo
 
276
        }
 
277
}
 
278
 
 
279
proc create_principal_with_keysalts {name keysalts} {
 
280
    global kadmin_local
 
281
 
 
282
    spawn $kadmin_local -e "$keysalts"
 
283
    expect {
 
284
        "kadmin.local:" {}
 
285
        default { error "waiting for kadmin.local prompt"; return 1}
 
286
    }
 
287
    send "ank -pw \"$name\" \"$name\"\n"
 
288
    expect {
 
289
        -re "Principal \"$name.*\" created." {}
 
290
        "kadmin.local:" {
 
291
            error "expecting principal created message"; 
 
292
            return 1
 
293
        }
 
294
        default { error "waiting for principal created message"; return 1 }
 
295
    }
 
296
    expect {
 
297
        "kadmin.local:" {}
 
298
        default { error "waiting for kadmin.local prompt"; return 1 }
 
299
    }
 
300
    close
 
301
    wait
 
302
    return 0
 
303
}
 
304
 
 
305