4 # This file aims to be a comprehensive test suite for the persona subsystem.
6 # 1. persona_mgr - create, destroy, lookup personas
7 # 2. persona_spawn - spawn processes into personas with a variety of options
8 # The script relies heavily on the particular output of these tools, so if you
9 # are modifying / extending those tools, this file also need to be updated to
10 # properly capture the new output. Specifically, the get_persona_info function
11 # needs to be maintained / updated.
13 # NOTE: the function get_persona_info() also needs to be kept up to date with
14 # the types of personas found in bsd/sys/persona.h
16 PERSONA_MGR
="${PWD}/persona_mgr"
17 PERSONA_SPAWN
="${PWD}/persona_spawn"
18 PERSONA_SPAWN_UNENTITLED
="${PWD}/persona_spawn_unentitled"
20 TEST_DEFAULT_PERSONA
=0
22 if [ ! -d "$TMPDIR" ]; then
23 echo "Couldn't find temp directory '$TMPDIR': check permissions/environment?"
27 if [ ! -e "${PERSONA_MGR}" ] || [ ! -x "${PERSONA_MGR}" ]; then
28 echo "Can't find '${PERSONA_MGR}': skipping test"
31 if [ ! -e "${PERSONA_SPAWN}" ] || [ ! -x "${PERSONA_SPAWN}" ]; then
32 echo "Can't find '${PERSONA_SPAWN}': skipping test"
36 function check_for_persona_support
() {
38 ${PERSONA_MGR} support
|| errno
=$?
39 if [ $errno -eq 78 ]; then
40 echo "Persona subsystem is not supported - skipping tests"
45 check_for_persona_support
50 # exit the script with an error code that corresponds to the line number
51 # from which this function was invoked. Because we want to exit with a
52 # non-zero exit code, we use: 1 + (254 % line).
57 if [ -z "$line" ]; then
58 line
=${BASH_LINENO[0]}
60 echo "[$line] ERROR: $msg" 1>&2
61 exit $((1 + $line % 254))
64 ## check_return [message_on_failure]
66 # Check the return value of the previous command or script line. If the
67 # value of '$?' is not 0, then call bail() with an appropriate message.
69 function check_return
() {
73 if [ -z "$line" ]; then
74 line
=${BASH_LINENO[0]}
77 if [ $err -ne 0 ]; then
78 bail
"e=$err: $msg" $line
84 ## expect_failure [message_on_success]
86 # Check the return value of the previous command or script line. If the
87 # value of '$?' is 0 (success), then call bail() with a message saying
88 # that we expected this previous command/line to fail.
90 function expect_failure
() {
94 if [ -z "$line" ]; then
95 line
=${BASH_LINENO[0]}
97 if [ $err -eq 0 ]; then
98 bail
"found success, expected failure: $msg" $line
101 echo "EXPECT: failure: $msg"
105 ## test_num [debug_info] [number]
107 # Check that a variable value is a number, bail() on error.
109 function test_num
() {
113 if [ -z "$line" ]; then
114 line
=${BASH_LINENO[0]}
116 if [ -z "$num" ]; then
117 bail
"invalid (NULL) $type" $line
119 [ "$num" -eq "$num" ] 2>/dev
/null
120 if [ $?
-ne 0 ]; then
121 bail
"invalid $type: $num" $line
127 ## global variables used to return values to callers
136 ## get_persona_info {persona_id} {persona_login}
138 # Lookup persona info for the given ID/login. At least one of the ID/login
139 # parameters must be valid
140 function get_persona_info
() {
142 local pna_login
=${2:- }
144 if [ -z "$line" ]; then
145 line
=${BASH_LINENO[0]}
148 local largs
="-u ${pna_id}"
149 if [ "${pna_login}" != " " ]; then
150 largs
+=" -l ${pna_login}"
161 local file="${TMPDIR}/plookup"
163 ${PERSONA_MGR} lookup
${largs} > "${file}"
164 check_return
"persona lookup of: ${largs}" $line
166 _ID
=$(cat "${file}" | grep "+id: " | head -1 | sed 's/.*+id:[ ]*\([0-9][0-9]*\).
*/\
1/')
167 test_num "Persona ID lookup:${largs}" "$_ID"
169 local type=$(cat "${file}" | grep "+type: " | head -1 | sed 's/.*+type:[ ]*\([0-9][0-9]*\).*/\1/')
170 test_num
"+type lookup:${largs}" "$type"
172 ## NOTE: keep in sync with bsd/sys/persona.h types!
174 if [ $type -eq 1 ]; then
176 elif [ $type -eq 2 ]; then
178 elif [ $type -eq 3 ]; then
180 elif [ $type -eq 4 ]; then
186 _LOGIN
=$(cat "${file}" | grep "+login: " | head -1 | sed 's/.*+login:[ ]*"\([^"]*\)".*/\1/')
187 if [ -z "$_LOGIN" ]; then
188 bail "invalid login
for pna_id
:$_ID: '$_LOGIN'" $line
191 # these are always the same
194 _GID=$(cat "${file}" | grep "+gid: " | head -1 | sed 's/.*+gid:[ ]*\([0-9][0-9]*\).*/\1/')
195 test_num "GID lookup
:${largs}" "$_GID"
197 _NGROUPS=$(cat "${file}" | grep "ngroups: " | head -1 | sed 's/.*ngroups:[ ]*\([0-9][0-9]*\)[ ][ ]*{.*}.*/\1/')
198 test_num "NGROUPS lookup
:${largs}" "$_NGROUPS"
200 _GROUPS=( $(cat "${file}" | grep "ngroups: " | head -1 | sed 's/.*ngroups:[ ]*[0-9][0-9]*[ ][ ]*{[ ]*\([^ ].*\)[ ][ ]*}.*/\1/') )
201 if [ $_NGROUPS -gt 0 ]; then
202 if [ -z "${_GROUPS}" ]; then
203 bail "lookup
:${largs}: missing
$_NGROUPS groups
" $line
205 if [ ${#_GROUPS[@]} -ne $_NGROUPS ]; then
206 bail "lookup
:${largs} wrong number of groups
${#_GROUPS[@]} != $_NGROUPS" $line
211 ## validate_child_info [output_file] [persona_id] {uid} {gid} {groups}
213 # Parse the output of the 'persona_spawn' command and validate that
214 # the new child process is in the correct persona with the correct
215 # process attributes.
217 function validate_child_info() {
224 if [ -z "$line" ]; then
225 line=${BASH_LINENO[0]}
229 # get the child's PID
230 local cpid="$(cat "$file" | grep "Child: PID:" | sed 's/.*Child: PID:\([0-9][0-9]*\).
*/\
1/')"
231 test_num "Child PID" "$cpid" $line
233 # validate the child's persona
234 l
=( $(cat "$file" | grep "Child: Persona:" | sed 's/.*Child: Persona: \([0-9][0-9]*\) (err
:\
([0-9][0-9]*\
))/\
1 \
2/') )
235 if [ ${#l[@]} -ne 2 ]; then
236 bail "Invalid Child[$cpid] Persona line" $line
238 test_num "Child Persona ID" "${l[0]}" $line
239 test_num "kpersona_info retval" "${l[1]}" $line
241 if [ ${l[0]} -ne $pna_id ]; then
242 bail "Child[$cpid] persona:${l[0]} != specified persona:$pna_id" $line
245 # Validate the UID/GID
246 l=( $(cat "$file" | grep "Child: UID:" | sed 's/.*UID:\([0-9][0-9]*\), GID:\([0-9][0-9]*\).*/\1 \2/') )
247 if [ ${#l[@]} -ne 2 ]; then
248 bail
"Invalid Child[$cpid] UID/GID output" $line
250 if [ $uid -ge 0 ]; then
251 if [ $uid -ne ${l[0]} ]; then
252 bail
"Child[$cpid] UID:${l[0]} != specified UID:$uid" $line
255 if [ $gid -ge 0 ]; then
256 if [ $gid -ne ${l[1]} ]; then
257 bail
"Child[$cpid] GID:${l[1]} != specified GID:$gid" $line
261 # TODO: validate / verify groups?
267 ## spawn_child [persona_id] {uid} {gid} {group_spec}
269 # Create a child process that is spawn'd into the persona given by
270 # the first argument (pna_id). The new process can have its UID, GID,
271 # and group membership properties overridden.
273 function spawn_child
() {
279 if [ -z "$line" ]; then
280 line
=${BASH_LINENO[0]}
283 local file="child.${pna_id}"
284 local spawn_args
="-I $pna_id"
285 if [ $uid -ge 0 ]; then
286 spawn_args
+=" -u $uid"
289 if [ $gid -ge 0 ]; then
290 spawn_args
+=" -g $gid"
293 if [ "$groups" != " " ]; then
294 spawn_args
+=" -G $groups"
299 ${PERSONA_SPAWN} -v $spawn_args ${PERSONA_SPAWN} child
-v -E > "${TMPDIR}/$file"
300 check_return
"child info: $file" $line
302 # Grab the specified persona's info so we can
303 # verify the child's info against it.
304 # This function puts data into global variables, e.g. _ID, _GID, etc.
305 get_persona_info
${pna_id} " " $line
306 if [ $uid -lt 0 ]; then
309 if [ $gid -lt 0 ]; then
312 if [ "$groups" == " " ]; then
313 # convert a bash array into a comma-separated list for validation
314 local _g
="${_GROUPS[@]}"
318 validate_child_info
"${TMPDIR}/$file" "$pna_id" "$uid" "$gid" "$groups" $line
320 ## validate that the first child spawned into a persona *cannot* spawn
321 ## into a different persona...
322 if [ $uid -eq 0 ]; then
323 ${PERSONA_SPAWN} -v $spawn_args ${PERSONA_SPAWN_UNENTITLED} child
-v -E -R spawn
-v $spawn_args -I ${TEST_DEFAULT_PERSONA} /bin
/echo "This is running in the system persona"
324 expect_failure
"Spawned child that re-execs into non-default persona" $line
329 ## get_created_id [output_file]
331 # Parse the output of the 'persona_mgr' command to determine the ID
332 # of the newly created persona.
334 function get_created_id
() {
336 local o
=$(cat "$file" | grep "Created persona" | sed 's/.*Created persona \([0-9][0-9]*\):/\
1/')
341 ## create_persona [login_name] [persona_type] {persona_id} {gid} {group_spec}
343 # Create a new persona with given parameters.
345 # Returns: the newly created persona ID via the global variable, $_ID
347 function create_persona() {
350 local pna_id=${3:--1}
354 if [ -z "$line" ]; then
355 line=${BASH_LINENO[0]}
358 if [ -z "$name" -o -z "$type" ]; then
359 bail "Invalid arguments to create_persona '$name' '$type'" $line
362 local file="persona.at${line}"
363 # persona ID of '-1' is auto-assigned
364 local spawn_args="-v -l $name -i $pna_id"
365 if [ $pna_id -eq -1 ]; then
371 spawn_args+=" -t $type"
374 if [ $gid -ge 0 ]; then
375 spawn_args+=" -g $gid"
378 if [ "$groups" != " " ]; then
379 spawn_args+=" -G $groups"
384 ${PERSONA_MGR} create ${spawn_args} > "${TMPDIR}/${file}"
385 check_return "persona creation: ${file}" $line
386 # test output should include persona creation output for later debugging
387 cat "${TMPDIR}/${file}"
389 # validate the output of the persona_mgr tool (what we think we created)
390 _ID=`get_created_id "${TMPDIR}/${file}"`
391 test_num "persona_id for $file" "$_ID" $line
392 if [ ${pna_id} -gt 0 ]; then
393 if [ $_ID -ne ${pna_id} ]; then
394 bail "Created persona doesn't have expected ID
$_ID != ${pna_id}" $line
398 # validate the entire persona information (what a kpersona_lookup says we created)
399 # This function puts data into global variables, e.g. _ID, _LOGIN, _GID, etc.
400 echo "VALIDATE
: ${file}"
401 get_persona_info ${pna_id} "$name" $line
402 if [ "$name" != "$_LOGIN" ]; then
403 bail "${file}: unexpected login
'$_LOGIN' != '$name'" $line
405 if [ "$type" != "$_TYPE" ]; then
406 bail "${file}: unexpected
type '$_TYPE' != '$type'" $line
408 if [ ${pna_id} -gt 0 ]; then
409 if [ ${pna_id} -ne $_ID ]; then
410 bail "${file}: unexpected ID
'$_ID' != '${pna_id}'" $line
413 if [ $gid -ge 0 ]; then
414 if [ $gid -ne $_GID ]; then
415 bail "${file}: unexpected GID
'$_GID' != '$gid'" $line
418 if [ "$groups" != " " ]; then
419 local _g="${_GROUPS[@]}"
420 if [ "${_g// /,}" != "$groups" ]; then
421 bail "${file}: unexpected groups
'${_g// /,}' != '$groups'" $line
428 ## destroy_persona [persona_id]
430 # Destroy the given persona.
432 function destroy_persona() {
435 if [ -z "$line" ]; then
436 line=${BASH_LINENO[0]}
439 echo "DESTROY
: ${pna_id}"
440 ${PERSONA_MGR} destroy -v -i ${pna_id}
441 check_return "destruction of
${pna_id}" $line
449 echo "Running persona tests
[$LINENO] ($TMPDIR)"
452 ## Test Group 0: basic creation + spawn tests
455 create_persona "test_default_persona
" "guest
" 9999
456 TEST_DEFAULT_PERSONA=$_ID
458 # default group, specific ID
459 create_persona "test0_1
" "guest
" 1001
463 spawn_child $P0ID 1100
465 spawn_child $P0ID -1 1101
466 spawn_child $P0ID 1100 1101
467 spawn_child $P0ID 1100 1101 1000,2000,3000
468 spawn_child $P0ID 1100 -1 1000,2000,3000
469 spawn_child $P0ID -1 -1 1000,2000,3000
470 destroy_persona $P0ID
472 # specific ID, non-default group
473 create_persona "test0_2
" "guest
" 1002 2000
476 spawn_child $P0ID 1100
478 spawn_child $P0ID -1 1101
479 spawn_child $P0ID 1100 1101
480 spawn_child $P0ID 1100 1101 1000,2000,3000
481 spawn_child $P0ID 1100 -1 1000,2000,3000
482 spawn_child $P0ID -1 -1 1000,2000,3000
483 destroy_persona $P0ID
485 # non-default set of groups
486 create_persona "test0_3
" "guest
" 1003 2000 2000,3000,4000
489 spawn_child $P0ID 1100
491 spawn_child $P0ID -1 1101
492 spawn_child $P0ID 1100 1101
493 spawn_child $P0ID 1100 1101 1111,2222,3333
494 spawn_child $P0ID 1100 -1 1111,2222,3333
495 spawn_child $P0ID -1 -1 1111,2222,3333
496 destroy_persona $P0ID
500 ## Test Group 1: persona creation / re-creation
503 # Create 3 personas with auto-assigned IDs
504 create_persona "test1_1
" "guest
"
506 create_persona "test1_2
" "managed
"
508 create_persona "test1_3
" "priv
"
510 create_persona "test1_4
" "guest
"
513 D1=$(($P2ID - $P1ID))
514 D2=$(($P3ID - $P2ID))
515 D3=$(($P4ID - $P3ID))
516 if [ $D1 -ne $D2 -o $D1 -ne $D3 -o $D2 -ne $D3 ]; then
517 bail "inconsistent automatic Persona ID increment
: $D1,$D2,$D3 ($P1ID,$P2ID,$P3ID,$P4ID)"
520 # make sure we can't re-allocate the same name / ID
521 ${PERSONA_MGR} create -v -l test1_1 -t guest -i -1 && expect_failure "re
-create same name
:test1_1
type:guest
"
522 ${PERSONA_MGR} create -v -l test1_1 -t managed -i -1 && expect_failure "re
-create same name
:test1_1
type:managed
"
523 ${PERSONA_MGR} create -v -l test1_1_new -t managed -i $P1ID && expect_failure "re
-create $P1ID with new name
:test1_1_new
type:managed
"
526 ## Test Group 2: auto-assigned ID tricks
529 # Notice the difference in IDs, then try to create a persona by
530 # specifying an ID that will match the next auto-assigned ID
532 P5ID_REQ=$(($P4ID + $D2))
533 create_persona "test2_1
" "guest
" ${P5ID_REQ}
535 if [ ! $P5ID -eq ${P5ID_REQ} ]; then
536 bail "test2_1
: ${P5ID_REQ} != $P5ID"
539 # try to create a persona with auto-assigned ID
540 # (resulting persona should have ID != P5ID)
541 create_persona "test2_2
" "guest
"
543 if [ $P6ID -eq $P5ID ]; then
544 bail "created duplicate persona IDs
: $P6ID == $P5ID"
548 ## Test Group 3: persona destruction
551 destroy_persona $P1ID
552 destroy_persona $P2ID
553 destroy_persona $P3ID
554 destroy_persona $P4ID
555 destroy_persona $P5ID
556 destroy_persona $P6ID
558 # try to re-destroy the personas
560 ${PERSONA_MGR} destroy -v -i $P1ID && expect_failure "re
-destroy (1/2) $P1ID"
561 ${PERSONA_MGR} destroy -v -i $P1ID && expect_failure "re
-destroy (2/2) $P1ID"
562 ${PERSONA_MGR} destroy -v -i $P2ID && expect_failure "re
-destroy $P2ID"
563 ${PERSONA_MGR} destroy -v -i $P3ID && expect_failure "re
-destroy $P3ID"
564 ${PERSONA_MGR} destroy -v -i $P4ID && expect_failure "re
-destroy $P4ID"
565 ${PERSONA_MGR} destroy -v -i $P5ID && expect_failure "re
-destroy $P5ID"
566 ${PERSONA_MGR} destroy -v -i $P6ID && expect_failure "re
-destroy $P6ID"
568 destroy_persona ${TEST_DEFAULT_PERSONA}
574 echo "${0##/}: SUCCESS
"