krb5 commit: Tighten up error checking in t_keytab.c
Greg Hudson
ghudson at MIT.EDU
Sat Jun 2 14:08:14 EDT 2012
https://github.com/krb5/krb5/commit/a5c693ee32900b252490a75b18ed40088eb6fec0
commit a5c693ee32900b252490a75b18ed40088eb6fec0
Author: Greg Hudson <ghudson at mit.edu>
Date: Sat Jun 2 13:54:12 2012 -0400
Tighten up error checking in t_keytab.c
When checking for specific error codes, using CHECK() meant that we
wouldn't properly fail if we got error code 0. Define and use a
CHECK_ERR() to test for a specific error code, and define CHECK() in
terms of it.
src/lib/krb5/keytab/t_keytab.c | 45 ++++++++++++++-------------------------
1 files changed, 16 insertions(+), 29 deletions(-)
diff --git a/src/lib/krb5/keytab/t_keytab.c b/src/lib/krb5/keytab/t_keytab.c
index 6e87076..6b64d52 100644
--- a/src/lib/krb5/keytab/t_keytab.c
+++ b/src/lib/krb5/keytab/t_keytab.c
@@ -40,13 +40,14 @@ extern const krb5_kt_ops krb5_ktf_writable_ops;
#define KRB5_OK 0
-#define CHECK(kret,msg) \
- if (kret != KRB5_OK) { \
+#define CHECK_ERR(kret,err,msg) \
+ if (kret != err) { \
com_err(msg, kret, ""); \
fflush(stderr); \
exit(1); \
} else if(debug) printf("%s went ok\n", msg);
+#define CHECK(kret,msg) CHECK_ERR(kret, 0, msg)
#define CHECK_STR(str,msg) \
if (str == 0) { \
@@ -66,9 +67,7 @@ test_misc(krb5_context context)
fprintf(stderr, "Testing miscellaneous error conditions\n");
kret = krb5_kt_resolve(context, "unknown_method_ep:/tmp/name", &ktid);
- if (kret != KRB5_KT_UNKNOWN_TYPE) {
- CHECK(kret, "resolve unknown type");
- }
+ CHECK_ERR(kret, KRB5_KT_UNKNOWN_TYPE, "resolve unknown type");
/* Test length limits on krb5_kt_default_name */
kret = krb5_kt_default_name(context, defname, sizeof(defname));
@@ -82,9 +81,7 @@ test_misc(krb5_context context)
}
kret = krb5_kt_default_name(context, name, strlen(defname));
free(name);
- if (kret != KRB5_CONFIG_NOTENUFSPACE) {
- CHECK(kret, "krb5_kt_default_name limited");
- }
+ CHECK_ERR(kret, KRB5_CONFIG_NOTENUFSPACE, "krb5_kt_default_name limited");
}
static void
@@ -117,25 +114,22 @@ kt_test(krb5_context context, const char *name)
*/
p = malloc(strlen(buf));
kret = krb5_kt_get_name(context, kt, p, 1);
- if(kret != KRB5_KT_NAME_TOOLONG) {
- CHECK(kret, "get_name - size 1");
- }
+ CHECK_ERR(kret, KRB5_KT_NAME_TOOLONG, "get_name - size 1");
kret = krb5_kt_get_name(context, kt, p, strlen(buf));
- if(kret != KRB5_KT_NAME_TOOLONG) {
- CHECK(kret, "get_name");
- }
+ CHECK_ERR(kret, KRB5_KT_NAME_TOOLONG, "get_name");
free(p);
/* Try to lookup unknown principal - when keytab does not exist*/
kret = krb5_parse_name(context, "test/test2 at TEST.MIT.EDU", &princ);
CHECK(kret, "parsing principal");
-
+ /* This will return ENOENT for FILE because the file doesn't exist,
+ * so accept that or KRB5_KT_NOTFOUND. */
kret = krb5_kt_get_entry(context, kt, princ, 0, 0, &kent);
- if((kret != KRB5_KT_NOTFOUND) && (kret != ENOENT)) {
- CHECK(kret, "Getting non-existant entry");
+ if (kret != ENOENT) {
+ CHECK_ERR(kret, KRB5_KT_NOTFOUND, "Getting non-existent entry");
}
@@ -199,9 +193,7 @@ kt_test(krb5_context context, const char *name)
cnt++;
krb5_free_keytab_entry_contents(context, &kent);
}
- if (kret != KRB5_KT_END) {
- CHECK(kret, "getting next entry");
- }
+ CHECK_ERR(kret, KRB5_KT_END, "getting next entry");
if(cnt != 3) {
fprintf(stderr, "Mismatch in number of entries in keytab");
@@ -219,9 +211,7 @@ kt_test(krb5_context context, const char *name)
kret = krb5_kt_get_entry(context, kt, princ, 0, 0, &kent);
- if((kret != KRB5_KT_NOTFOUND)) {
- CHECK(kret, "Getting non-existant entry");
- }
+ CHECK_ERR(kret, KRB5_KT_NOTFOUND, "Getting non-existant entry");
krb5_free_principal(context, princ);
@@ -339,9 +329,8 @@ kt_test(krb5_context context, const char *name)
/* Try to lookup specified enctype and kvno - that does not exist*/
kret = krb5_kt_get_entry(context, kt, princ, 3, 1, &kent);
- if(kret != KRB5_KT_KVNONOTFOUND) {
- CHECK(kret, "looking up specific principal, kvno, enctype");
- }
+ CHECK_ERR(kret, KRB5_KT_KVNONOTFOUND,
+ "looking up specific principal, kvno, enctype");
krb5_free_principal(context, princ);
@@ -436,9 +425,7 @@ main(void)
/* All keytab types are registered by default -- test for
redundant error */
kret = krb5_kt_register(context, &krb5_ktf_writable_ops);
- if(kret && kret != KRB5_KT_TYPE_EXISTS) {
- CHECK(kret, "register ktf_writable");
- }
+ CHECK_ERR(kret, KRB5_KT_TYPE_EXISTS, "register ktf_writable");
test_misc(context);
do_test(context, "WRFILE:", FALSE);
More information about the cvs-krb5
mailing list