aboutsummaryrefslogtreecommitdiff
path: root/test/unit/disposition.c
diff options
context:
space:
mode:
authorRodrigo Arias Mallo <rodarima@gmail.com>2025-03-28 19:32:28 +0100
committerRodrigo Arias Mallo <rodarima@gmail.com>2025-05-01 00:56:42 +0200
commit71ca5d9e294ce7cbb6d6b68d68d55a860b810a3c (patch)
tree9d849ed619c62dff0036e2ad42c709bc849a38c0 /test/unit/disposition.c
parent3eeb7842f51e431f0ae2160bed2bdca8e53e5c52 (diff)
Add content disposition unit test
Diffstat (limited to 'test/unit/disposition.c')
-rw-r--r--test/unit/disposition.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/test/unit/disposition.c b/test/unit/disposition.c
new file mode 100644
index 00000000..a2f5fc46
--- /dev/null
+++ b/test/unit/disposition.c
@@ -0,0 +1,88 @@
+/*
+ * File: disposition.c
+ *
+ * Copyright 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
+ *
+ * This program 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.
+ *
+ * This program 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "dlib/dlib.h"
+#include "src/misc.h"
+
+#include <stdlib.h>
+
+struct testcase {
+ const char *disposition;
+ const char *type;
+ const char *filename;
+};
+
+struct testcase cases[] = {
+ /* See http://test.greenbytes.de/tech/tc2231/ */
+ { "attachment; filename=\"\\\"quoting\\\" tested.html\"", "attachment", "_\"quoting_\" tested.html" },
+ //{ "attachment; filename=\"\\\"quoting\\\" tested.html\"", "attachment", "quoting" }, /* <-- Should be this */
+ { "attachment; filename=\"/foo.html\"", "attachment", "_foo.html" },
+ { "attachment; filename=/foo", "attachment", "_foo" },
+ { "attachment; filename=./../foo", "attachment", "_.._foo" },
+ { "attachment; filename=", "attachment", NULL },
+ { "attachment; filename= ", "attachment", NULL },
+ { "attachment; filename=\"", "attachment", NULL },
+ { "attachment; filename=\"foo", "attachment", NULL },
+ { "attachment; filename=~/foo", "attachment", "~_foo" },
+};
+
+static int equal(const char *a, const char *b)
+{
+ if (a == NULL && b == NULL)
+ return 1;
+
+ if (a == NULL || b == NULL)
+ return 0;
+
+ return strcmp(a, b) == 0;
+}
+
+int main(void)
+{
+ char dummy[] = "dummy";
+ int ncases = sizeof(cases) / sizeof(cases[0]);
+ int rc = 0;
+
+ for (int i = 0; i < ncases; i++) {
+ struct testcase *t = &cases[i];
+ char *type = dummy;
+ char *filename = dummy;
+ a_Misc_parse_content_disposition(t->disposition, &type, &filename);
+ if (!equal(type, t->type)) {
+ fprintf(stderr, "test %d failed, type mismatch:\n", i);
+ fprintf(stderr, " Content-Dispsition: %s\n", t->disposition);
+ fprintf(stderr, " Expected type: %s\n", t->type);
+ fprintf(stderr, " Computed type: %s\n", type);
+ rc = 1;
+ }
+
+ if (!equal(filename, t->filename)) {
+ fprintf(stderr, "test %d failed, filename mismatch:\n", i);
+ fprintf(stderr, " Content-Dispsition: %s\n", t->disposition);
+ fprintf(stderr, " Expected filename: %s\n", t->filename);
+ fprintf(stderr, " Computed filename: %s\n", filename);
+ rc = 1;
+ }
+
+ dFree(type);
+ dFree(filename);
+ }
+
+ return rc;
+}