summaryrefslogtreecommitdiff
path: root/src/regress/lib
diff options
context:
space:
mode:
authortb <>2023-08-15 19:14:42 +0000
committertb <>2023-08-15 19:14:42 +0000
commit6618681458eebd4bc0a35af823662e1e71b5a2ae (patch)
treecfccd24976c1a951284b17cf27de1bb9cabf62e7 /src/regress/lib
parent84dfc864c5efeb353464243ed5316968c95b3245 (diff)
downloadopenbsd-6618681458eebd4bc0a35af823662e1e71b5a2ae.tar.gz
openbsd-6618681458eebd4bc0a35af823662e1e71b5a2ae.tar.bz2
openbsd-6618681458eebd4bc0a35af823662e1e71b5a2ae.zip
Add some regress coverage for various ASN1_STRING types to codify some
quirks and invariants.
Diffstat (limited to 'src/regress/lib')
-rw-r--r--src/regress/lib/libcrypto/asn1/asn1basic.c230
1 files changed, 229 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/asn1/asn1basic.c b/src/regress/lib/libcrypto/asn1/asn1basic.c
index 1703ba964b..3ab1151e15 100644
--- a/src/regress/lib/libcrypto/asn1/asn1basic.c
+++ b/src/regress/lib/libcrypto/asn1/asn1basic.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: asn1basic.c,v 1.13 2022/11/26 16:08:56 tb Exp $ */ 1/* $OpenBSD: asn1basic.c,v 1.14 2023/08/15 19:14:42 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 * 4 *
@@ -750,6 +750,233 @@ asn1_integer_test(void)
750 return failed; 750 return failed;
751} 751}
752 752
753static const struct asn1_string_new_test {
754 const char *name;
755 ASN1_STRING *(*new)(void);
756 void (*free)(ASN1_STRING *);
757 int type;
758 long flags;
759} asn1_string_new_tests[] = {
760 {
761 .name = "ASN1_STRING",
762 .new = ASN1_STRING_new,
763 .free = ASN1_STRING_free,
764 .type = V_ASN1_OCTET_STRING,
765 },
766 {
767 .name = "ASN1_OCTET_STRING",
768 .new = ASN1_OCTET_STRING_new,
769 .free = ASN1_OCTET_STRING_free,
770 .type = V_ASN1_OCTET_STRING,
771 },
772 {
773 .name = "ASN1_BIT_STRING",
774 .new = ASN1_BIT_STRING_new,
775 .free = ASN1_BIT_STRING_free,
776 .type = V_ASN1_BIT_STRING,
777 },
778 {
779 .name = "ASN1_INTEGER",
780 .new = ASN1_INTEGER_new,
781 .free = ASN1_INTEGER_free,
782 .type = V_ASN1_INTEGER,
783 },
784 {
785 .name = "ASN1_ENUMERATED",
786 .new = ASN1_ENUMERATED_new,
787 .free = ASN1_ENUMERATED_free,
788 .type = V_ASN1_ENUMERATED,
789 },
790 {
791 .name = "ASN1_UTF8STRING",
792 .new = ASN1_UTF8STRING_new,
793 .free = ASN1_UTF8STRING_free,
794 .type = V_ASN1_UTF8STRING,
795 },
796 {
797 .name = "ASN1_IA5STRING",
798 .new = ASN1_IA5STRING_new,
799 .free = ASN1_IA5STRING_free,
800 .type = V_ASN1_IA5STRING,
801 },
802 {
803 .name = "ASN1_UNIVERSALSTRING",
804 .new = ASN1_UNIVERSALSTRING_new,
805 .free = ASN1_UNIVERSALSTRING_free,
806 .type = V_ASN1_UNIVERSALSTRING,
807 },
808 {
809 .name = "ASN1_BMPSTRING",
810 .new = ASN1_BMPSTRING_new,
811 .free = ASN1_BMPSTRING_free,
812 .type = V_ASN1_BMPSTRING,
813 },
814 {
815 .name = "ASN1_GENERALSTRING",
816 .new = ASN1_GENERALSTRING_new,
817 .free = ASN1_GENERALSTRING_free,
818 .type = V_ASN1_GENERALSTRING,
819 },
820 {
821 .name = "ASN1_T61STRING",
822 .new = ASN1_T61STRING_new,
823 .free = ASN1_T61STRING_free,
824 .type = V_ASN1_T61STRING,
825 },
826 {
827 .name = "ASN1_VISIBLESTRING",
828 .new = ASN1_VISIBLESTRING_new,
829 .free = ASN1_VISIBLESTRING_free,
830 .type = V_ASN1_VISIBLESTRING,
831 },
832 {
833 .name = "ASN1_PRINTABLESTRING",
834 .new = ASN1_PRINTABLESTRING_new,
835 .free = ASN1_PRINTABLESTRING_free,
836 .type = V_ASN1_PRINTABLESTRING,
837 },
838 {
839 .name = "ASN1_PRINTABLE",
840 .new = ASN1_PRINTABLE_new,
841 .free = ASN1_PRINTABLE_free,
842 .type = V_ASN1_UNDEF,
843 .flags = ASN1_STRING_FLAG_MSTRING,
844 },
845 {
846 .name = "DIRECTORYSTRING",
847 .new = DIRECTORYSTRING_new,
848 .free = DIRECTORYSTRING_free,
849 .type = V_ASN1_UNDEF,
850 .flags = ASN1_STRING_FLAG_MSTRING,
851 },
852 {
853 .name = "DISPLAYTEXT",
854 .new = DISPLAYTEXT_new,
855 .free = DISPLAYTEXT_free,
856 .type = V_ASN1_UNDEF,
857 .flags = ASN1_STRING_FLAG_MSTRING,
858 },
859 {
860 .name = "ASN1_GENERALIZEDTIME",
861 .new = ASN1_GENERALIZEDTIME_new,
862 .free = ASN1_GENERALIZEDTIME_free,
863 .type = V_ASN1_GENERALIZEDTIME,
864 },
865 {
866 .name = "ASN1_UTCTIME",
867 .new = ASN1_UTCTIME_new,
868 .free = ASN1_UTCTIME_free,
869 .type = V_ASN1_UTCTIME,
870 },
871 {
872 .name = "ASN1_TIME",
873 .new = ASN1_TIME_new,
874 .free = ASN1_TIME_free,
875 .type = V_ASN1_UNDEF,
876 .flags = ASN1_STRING_FLAG_MSTRING,
877 },
878};
879
880#define N_ASN1_STRING_NEW_TESTS \
881 (sizeof(asn1_string_new_tests) / sizeof(asn1_string_new_tests[0]))
882
883static int
884asn1_string_new_test(void)
885{
886 size_t i;
887 ASN1_STRING *astr = NULL;
888 int failed = 1;
889
890 for (i = 0; i < N_ASN1_STRING_NEW_TESTS; i++) {
891 const struct asn1_string_new_test *asnt = &asn1_string_new_tests[i];
892
893 if ((astr = asnt->new()) == NULL) {
894 fprintf(stderr, "%s_new() failed\n", asnt->name);
895 goto err;
896 }
897 if (ASN1_STRING_type(astr) != asnt->type) {
898 fprintf(stderr, "%s type: want %d, got %d\n",
899 asnt->name, asnt->type, ASN1_STRING_type(astr));
900 goto err;
901 }
902 if (ASN1_STRING_data(astr) != NULL) {
903 fprintf(stderr, "%s data != NULL\n", asnt->name);
904 goto err;
905 }
906 if (ASN1_STRING_get0_data(astr) != NULL) {
907 fprintf(stderr, "%s data != NULL\n", asnt->name);
908 goto err;
909 }
910 if (ASN1_STRING_length(astr) != 0) {
911 fprintf(stderr, "%s length %d != 0\n", asnt->name,
912 ASN1_STRING_length(astr));
913 goto err;
914 }
915 ASN1_STRING_length_set(astr, 20);
916 if (ASN1_STRING_length(astr) != 20) {
917 fprintf(stderr, "%s length %d != 20\n", asnt->name,
918 ASN1_STRING_length(astr));
919 goto err;
920 }
921 astr->flags |= ASN1_STRING_FLAG_NDEF;
922 if (astr->flags != (asnt->flags | ASN1_STRING_FLAG_NDEF)) {
923 fprintf(stderr, "%s flags: %lx\n", asnt->name,
924 astr->flags);
925 goto err;
926 }
927 /* ASN1_STRING_set0() clears ASN1_STRING_FLAG_NDEF. */
928 ASN1_STRING_set0(astr, NULL, 0);
929 if (astr->flags != asnt->flags) {
930 fprintf(stderr, "%s flags: %lx != %lx\n", asnt->name,
931 astr->flags, asnt->flags);
932 goto err;
933 }
934 asnt->free(astr);
935 astr = NULL;
936
937 if ((astr = ASN1_STRING_type_new(asnt->type)) == NULL) {
938 fprintf(stderr, "ASN1_STRING_type_new(%s) failed\n",
939 asnt->name);
940 goto err;
941 }
942 if (ASN1_STRING_type(astr) != asnt->type) {
943 fprintf(stderr, "%s type: want %d, got %d\n",
944 asnt->name, asnt->type, ASN1_STRING_type(astr));
945 goto err;
946 }
947 if (ASN1_STRING_data(astr) != NULL) {
948 fprintf(stderr, "%s data != NULL\n", asnt->name);
949 goto err;
950 }
951 /* ASN1_STRING_type_new() does not set flags. */
952 if (astr->flags != 0) {
953 fprintf(stderr, "%s flags %lx\n", asnt->name,
954 astr->flags);
955 goto err;
956 }
957 asnt->free(astr);
958 astr = NULL;
959
960 }
961
962 failed = 0;
963
964 err:
965 ASN1_STRING_free(astr);
966
967 return failed;
968}
969
970static int
971asn1_string_test(void)
972{
973 int failed = 0;
974
975 failed |= asn1_string_new_test();
976
977 return failed;
978}
979
753int 980int
754main(int argc, char **argv) 981main(int argc, char **argv)
755{ 982{
@@ -758,6 +985,7 @@ main(int argc, char **argv)
758 failed |= asn1_bit_string_test(); 985 failed |= asn1_bit_string_test();
759 failed |= asn1_boolean_test(); 986 failed |= asn1_boolean_test();
760 failed |= asn1_integer_test(); 987 failed |= asn1_integer_test();
988 failed |= asn1_string_test();
761 989
762 return (failed); 990 return (failed);
763} 991}