aboutsummaryrefslogtreecommitdiff
path: root/test/unit/disposition.c
blob: a2f5fc46608d3f596177df4a6dfb5369c6a3ee73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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;
}