Skip to content

Commit

Permalink
Test for nonexistent tokens in p11-kit commands
Browse files Browse the repository at this point in the history
Signed-off-by: Zoltan Fridrich <[email protected]>
  • Loading branch information
ZoltanFridrich committed Sep 26, 2023
1 parent 202c1c1 commit 5b13c85
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
4 changes: 2 additions & 2 deletions common/test-init.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ assert_fail()

assert_contains()
{
if ! grep -qF $2 $1; then
if ! grep -qF "$2" "$1"; then
assert_fail "$1 does not contain $2"
fi
}

assert_not_contains()
{
if grep -qF $2 $1; then
if grep -qF "$2" "$1"; then
assert_fail "$1 contains $2"
fi
}
Expand Down
35 changes: 34 additions & 1 deletion p11-kit/test-objects.sh
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,38 @@ EOF
fi
}

test_generate_keypair_nonexistent_token() {
cat > list.exp <<EOF
EOF

if "$abs_top_builddir"/p11-kit/p11-kit-testable generate-keypair --type=mock "pkcs11:token=NONEXISTENT" > list.out 2> err.out; then
assert_fail "expected to fail: p11-kit generate-keypair"
fi

: ${DIFF=diff}
if ! ${DIFF} list.exp list.out > list.diff; then
sed 's/^/# /' list.diff
assert_fail "output contains wrong result"
fi
assert_contains err.out "no matching token"
}

test_delete_nonexistent_token() {
cat > list.exp <<EOF
EOF

if "$abs_top_builddir"/p11-kit/p11-kit-testable delete-object "pkcs11:token=NONEXISTENT" > list.out 2> err.out; then
assert_fail "expected to fail: p11-kit delete-object"
fi

: ${DIFF=diff}
if ! ${DIFF} list.exp list.out > list.diff; then
sed 's/^/# /' list.diff
assert_fail "output contains wrong result"
fi
assert_contains err.out "no matching object"
}

run test_list_all test_list_with_type test_list_exact test_list_nonexistent \
test_export_cert test_export_pubkey test_generate_keypair
test_export_cert test_export_pubkey test_generate_keypair test_generate_keypair_nonexistent_token \
test_delete_nonexistent_token
19 changes: 18 additions & 1 deletion p11-kit/test-profiles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,21 @@ EOF
fi
}

run test_list_profiles
test_add_profile_nonexistent_token() {
cat > list.exp <<EOF
EOF

if "$abs_top_builddir"/p11-kit/p11-kit-testable add-profile --profile=baseline-provider "pkcs11:token=NONEXISTENT" > list.out 2> err.out; then
assert_fail "expected to fail: p11-kit add-profile"
fi

: ${DIFF=diff}
if ! ${DIFF} list.exp list.out > list.diff; then
sed 's/^/# /' list.diff
assert_fail "output contains wrong results"
fi
assert_contains err.out "no matching token"
}

run test_list_profiles \
test_add_profile_nonexistent_token

0 comments on commit 5b13c85

Please sign in to comment.