summaryrefslogtreecommitdiffstats
path: root/dirmngr/t-ldap-misc.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 16:14:06 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 16:14:06 +0000
commiteee068778cb28ecf3c14e1bf843a95547d72c42d (patch)
tree0e07b30ddc5ea579d682d5dbe57998200d1c9ab7 /dirmngr/t-ldap-misc.c
parentInitial commit. (diff)
downloadgnupg2-eee068778cb28ecf3c14e1bf843a95547d72c42d.tar.xz
gnupg2-eee068778cb28ecf3c14e1bf843a95547d72c42d.zip
Adding upstream version 2.2.40.upstream/2.2.40upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dirmngr/t-ldap-misc.c')
-rw-r--r--dirmngr/t-ldap-misc.c158
1 files changed, 158 insertions, 0 deletions
diff --git a/dirmngr/t-ldap-misc.c b/dirmngr/t-ldap-misc.c
new file mode 100644
index 0000000..afba102
--- /dev/null
+++ b/dirmngr/t-ldap-misc.c
@@ -0,0 +1,158 @@
+/* t-ldap-parse-uri.c - Tests for ldap-parse-uri.c and ldap-misc.c
+ * Copyright (C) 2015 g10 Code GmbH
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <gpg-error.h>
+
+#include "../common/util.h"
+#include "t-support.h"
+#include "ldap-misc.h"
+
+
+static void
+test_ldap_parse_extfilter (void)
+{
+ struct {
+ const char *string;
+ const char *base;
+ const char *filter;
+ int scope;
+ gpg_err_code_t ec;
+ } tests[] =
+ {
+ { "^CN=foo, OU=My Users&(objectClasses=*)",
+ "CN=foo, OU=My Users", "(objectClasses=*)",
+ -1 },
+ { "^CN=foo, OU=My Users&base&(objectClasses=*)",
+ "CN=foo, OU=My Users", "(objectClasses=*)",
+ LDAP_SCOPE_BASE },
+ { "^CN=foo, OU=My Users&one&(objectClasses=*)",
+ "CN=foo, OU=My Users", "(objectClasses=*)",
+ LDAP_SCOPE_ONELEVEL },
+ { "^CN=foo, OU=My Users&sub&(objectClasses=*)",
+ "CN=foo, OU=My Users", "(objectClasses=*)",
+ LDAP_SCOPE_SUBTREE },
+ /* { "^CN=foo, OU=My Users&children&(objectClasses=*)", */
+ /* "CN=foo, OU=My Users", "(objectClasses=*)", */
+ /* LDAP_SCOPE_CHILDREN }, */
+ { "^CN=foo, OU=My Users&",
+ "CN=foo, OU=My Users", NULL,
+ -1 },
+ { "^CN=foo, OU=My Users&sub&",
+ "CN=foo, OU=My Users", NULL,
+ LDAP_SCOPE_SUBTREE },
+ /* { "^&children&(objectClasses=*)", */
+ /* "", "(objectClasses=*)", */
+ /* LDAP_SCOPE_CHILDREN }, */
+ { "^CN=foo, OU=My &&Users&base&(objectClasses=*)",
+ "CN=foo, OU=My &Users", "(objectClasses=*)",
+ LDAP_SCOPE_BASE },
+ { "^CN=foo, OU=My Users&&&base&(objectClasses=*)",
+ "CN=foo, OU=My Users&", "(objectClasses=*)",
+ LDAP_SCOPE_BASE },
+ { "^CN=foo, OU=My Users",
+ NULL, NULL,
+ LDAP_SCOPE_BASE, GPG_ERR_SYNTAX },
+ { "^CN=foo, OU=My Users&base(objectClasses=*)",
+ NULL, NULL,
+ LDAP_SCOPE_BASE, GPG_ERR_SYNTAX },
+ { "^CN=foo, OU=My Users&base&objectClasses=*)",
+ NULL, NULL,
+ LDAP_SCOPE_BASE, GPG_ERR_SYNTAX },
+ { "^CN=foo, OU=My Users&base&(objectClasses=*",
+ NULL, NULL,
+ LDAP_SCOPE_BASE, GPG_ERR_SYNTAX }
+ };
+ int idx;
+ gpg_error_t err;
+ int errcount = 0;
+ char *base, *filter;
+ int scope;
+
+ for (idx= 0; idx < DIM (tests); idx++)
+ {
+ scope = -1;
+ err = ldap_parse_extfilter (tests[idx].string, 1, &base, &scope, &filter);
+ if (err && tests[idx].ec)
+ {
+ if (gpg_err_code (err) != tests[idx].ec)
+ {
+ fprintf (stderr, "%s: test %d failed: wrong error code %d\n",
+ __func__, idx, err);
+ errcount++;
+ }
+ continue;
+ }
+ if (err)
+ {
+ fprintf (stderr, "%s: test %d failed: %s\n",
+ __func__, idx, gpg_strerror (err));
+ errcount++;
+ continue;
+ }
+ if (tests[idx].ec)
+ {
+ fprintf (stderr, "%s: test %d failed: error not detected\n",
+ __func__, idx);
+ errcount++;
+ continue;
+ }
+ if ((!tests[idx].base ^ !base)
+ || (tests[idx].base && strcmp (tests[idx].base, base)))
+ {
+ fprintf (stderr, "%s: test %d failed: base mismatch ('%s')\n",
+ __func__, idx, base? base : "(null");
+ errcount++;
+ }
+ if ((!tests[idx].filter ^ !filter)
+ || (tests[idx].filter && strcmp (tests[idx].filter, filter)))
+ {
+ fprintf (stderr, "%s: test %d failed: filter mismatch ('%s')\n",
+ __func__, idx, filter? filter : "(null");
+ errcount++;
+ }
+ if (tests[idx].scope != scope)
+ {
+ fprintf (stderr, "%s: test %d failed: scope mismatch (%d)\n",
+ __func__, idx, scope);
+ errcount++;
+ }
+ xfree (base);
+ xfree (filter);
+ }
+ if (errcount)
+ exit (1);
+}
+
+
+
+
+int
+main (int argc, char **argv)
+{
+ (void)argc;
+ (void)argv;
+
+ test_ldap_parse_extfilter ();
+
+ return 0;
+}