diff options
author | tb <> | 2023-08-15 21:05:44 +0000 |
---|---|---|
committer | tb <> | 2023-08-15 21:05:44 +0000 |
commit | 1d62005d9bd6b94c568280794de8179344169f7b (patch) | |
tree | 30520d4ef4c4b6a21e729dbab0fe4b8b9ac1233b | |
parent | 6618681458eebd4bc0a35af823662e1e71b5a2ae (diff) | |
download | openbsd-1d62005d9bd6b94c568280794de8179344169f7b.tar.gz openbsd-1d62005d9bd6b94c568280794de8179344169f7b.tar.bz2 openbsd-1d62005d9bd6b94c568280794de8179344169f7b.zip |
Add regress coverage for ASN1_STRING_cmp()
-rw-r--r-- | src/regress/lib/libcrypto/asn1/asn1basic.c | 148 |
1 files changed, 147 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/asn1/asn1basic.c b/src/regress/lib/libcrypto/asn1/asn1basic.c index 3ab1151e15..5bcb9009a2 100644 --- a/src/regress/lib/libcrypto/asn1/asn1basic.c +++ b/src/regress/lib/libcrypto/asn1/asn1basic.c | |||
@@ -1,6 +1,7 @@ | |||
1 | /* $OpenBSD: asn1basic.c,v 1.14 2023/08/15 19:14:42 tb Exp $ */ | 1 | /* $OpenBSD: asn1basic.c,v 1.15 2023/08/15 21:05:44 tb Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org> |
4 | * Copyright (c) 2023 Theo Buehler <tb@openbsd.org> | ||
4 | * | 5 | * |
5 | * Permission to use, copy, modify, and distribute this software for any | 6 | * Permission to use, copy, modify, and distribute this software for any |
6 | * purpose with or without fee is hereby granted, provided that the above | 7 | * purpose with or without fee is hereby granted, provided that the above |
@@ -967,12 +968,157 @@ asn1_string_new_test(void) | |||
967 | return failed; | 968 | return failed; |
968 | } | 969 | } |
969 | 970 | ||
971 | static char *comparison_str = "mystring"; | ||
972 | |||
973 | static int | ||
974 | asn1_string_cmp_test(void) | ||
975 | { | ||
976 | ASN1_STRING *a = NULL, *b = NULL; | ||
977 | int got, want; | ||
978 | int failed = 1; | ||
979 | |||
980 | if ((got = ASN1_STRING_cmp(NULL, NULL)) != -1) { | ||
981 | fprintf(stderr, "ASN1_STRING_cmp(NULL, NULL): %d != -1\n", got); | ||
982 | goto err; | ||
983 | } | ||
984 | |||
985 | if ((a = ASN1_STRING_new()) == NULL) { | ||
986 | fprintf(stderr, "a = ASN1_STRING_new() failed\n"); | ||
987 | goto err; | ||
988 | } | ||
989 | if ((b = ASN1_STRING_type_new(V_ASN1_UTF8STRING)) == NULL) { | ||
990 | fprintf(stderr, "b = ASN1_STRING_type_new() failed\n"); | ||
991 | goto err; | ||
992 | } | ||
993 | |||
994 | if ((got = ASN1_STRING_cmp(a, NULL)) != -1) { | ||
995 | fprintf(stderr, "ASN1_STRING_cmp(a, NULL): %d != -1\n", got); | ||
996 | goto err; | ||
997 | } | ||
998 | if ((got = ASN1_STRING_cmp(NULL, a)) != -1) { | ||
999 | fprintf(stderr, "ASN1_STRING_cmp(NULL, a): %d != -1\n", got); | ||
1000 | goto err; | ||
1001 | } | ||
1002 | |||
1003 | if (ASN1_STRING_cmp(a, b) >= 0) { | ||
1004 | fprintf(stderr, "V_ASN1_OCTET_STRING >= V_ASN1_UTF8STRING\n"); | ||
1005 | goto err; | ||
1006 | } | ||
1007 | want = V_ASN1_UTF8STRING - V_ASN1_OCTET_STRING; | ||
1008 | if ((got = ASN1_STRING_cmp(b, a)) != want) { | ||
1009 | fprintf(stderr, "comparison of octet with utf8 string:" | ||
1010 | "want %d, got %d\n", want, got); | ||
1011 | goto err; | ||
1012 | } | ||
1013 | |||
1014 | ASN1_STRING_set0(a, comparison_str, strlen(comparison_str)); | ||
1015 | ASN1_STRING_set0(b, comparison_str, strlen(comparison_str)); | ||
1016 | |||
1017 | /* Ensure any data set on a or b isn't freed/zeroed. */ | ||
1018 | a->flags |= ASN1_STRING_FLAG_NDEF; | ||
1019 | b->flags |= ASN1_STRING_FLAG_NDEF; | ||
1020 | |||
1021 | if ((got = ASN1_STRING_cmp(b, a)) != want) { | ||
1022 | fprintf(stderr, "comparison of octet with utf8 string:" | ||
1023 | "want %d, got %d\n", want, got); | ||
1024 | goto err; | ||
1025 | } | ||
1026 | |||
1027 | b->type = V_ASN1_OCTET_STRING; | ||
1028 | |||
1029 | if ((got = ASN1_STRING_cmp(a, b)) != 0) { | ||
1030 | fprintf(stderr, "same string on both. want 0, got %d\n", got); | ||
1031 | goto err; | ||
1032 | } | ||
1033 | |||
1034 | if (!ASN1_STRING_set(b, "myString", -1)) { | ||
1035 | fprintf(stderr, "ASN1_STRING_set(b) failed\n"); | ||
1036 | goto err; | ||
1037 | } | ||
1038 | |||
1039 | if ((got = ASN1_STRING_cmp(a, b)) <= 0) { | ||
1040 | fprintf(stderr, "capitalized letter compares larger: got %d\n", | ||
1041 | got); | ||
1042 | goto err; | ||
1043 | } | ||
1044 | if ((got = ASN1_STRING_cmp(b, a)) >= 0) { | ||
1045 | fprintf(stderr, "capitalized letter is larger 2: %d\n", got); | ||
1046 | goto err; | ||
1047 | } | ||
1048 | |||
1049 | ASN1_STRING_length_set(b, 2); | ||
1050 | |||
1051 | want = strlen(comparison_str) - 2; | ||
1052 | |||
1053 | if ((got = ASN1_STRING_cmp(a, b)) != want) { | ||
1054 | fprintf(stderr, "comparison of a with truncated b: " | ||
1055 | "want %d, got %d\n", want, got); | ||
1056 | goto err; | ||
1057 | } | ||
1058 | |||
1059 | want = -want; | ||
1060 | |||
1061 | if ((got = ASN1_STRING_cmp(b, a)) != want) { | ||
1062 | fprintf(stderr, "comparison of truncated b with a: " | ||
1063 | "want %d, got %d\n", want, got); | ||
1064 | goto err; | ||
1065 | } | ||
1066 | |||
1067 | ASN1_STRING_length_set(a, 2); | ||
1068 | |||
1069 | if ((got = ASN1_STRING_cmp(a, b)) != 0) { | ||
1070 | fprintf(stderr, "both truncated compared to %d\n", got); | ||
1071 | goto err; | ||
1072 | } | ||
1073 | |||
1074 | ASN1_STRING_length_set(a, strlen(comparison_str)); | ||
1075 | |||
1076 | ASN1_STRING_set0(b, NULL, 0); | ||
1077 | |||
1078 | want = strlen(comparison_str); | ||
1079 | if ((got = ASN1_STRING_cmp(a, b)) != want) { | ||
1080 | fprintf(stderr, "comparison of a with zeroed b: " | ||
1081 | "want %d, got %d\n", want, got); | ||
1082 | goto err; | ||
1083 | } | ||
1084 | |||
1085 | ASN1_STRING_set0(b, "", 0); | ||
1086 | b->flags |= ASN1_STRING_FLAG_NDEF; | ||
1087 | |||
1088 | if ((got = ASN1_STRING_cmp(a, b)) != want) { | ||
1089 | fprintf(stderr, "comparison of a with zero-length b: " | ||
1090 | "want %d, got %d\n", want, got); | ||
1091 | goto err; | ||
1092 | } | ||
1093 | |||
1094 | ASN1_STRING_set0(a, NULL, 0); | ||
1095 | if ((got = ASN1_STRING_cmp(a, b)) != 0) { | ||
1096 | fprintf(stderr, "comparison of zeroed a with zero-length b: " | ||
1097 | "want 0, got %d\n", got); | ||
1098 | goto err; | ||
1099 | } | ||
1100 | if ((got = ASN1_STRING_cmp(b, a)) != 0) { | ||
1101 | fprintf(stderr, "comparison of zero-length b with zeroed a: " | ||
1102 | "want 0, got %d\n", got); | ||
1103 | goto err; | ||
1104 | } | ||
1105 | |||
1106 | failed = 0; | ||
1107 | |||
1108 | err: | ||
1109 | ASN1_STRING_free(a); | ||
1110 | ASN1_STRING_free(b); | ||
1111 | |||
1112 | return failed; | ||
1113 | } | ||
1114 | |||
970 | static int | 1115 | static int |
971 | asn1_string_test(void) | 1116 | asn1_string_test(void) |
972 | { | 1117 | { |
973 | int failed = 0; | 1118 | int failed = 0; |
974 | 1119 | ||
975 | failed |= asn1_string_new_test(); | 1120 | failed |= asn1_string_new_test(); |
1121 | failed |= asn1_string_cmp_test(); | ||
976 | 1122 | ||
977 | return failed; | 1123 | return failed; |
978 | } | 1124 | } |