unit1604.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at http://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "curlcheck.h"
00023 
00024 #include "tool_cfgable.h"
00025 #include "tool_doswin.h"
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 #include "memdebug.h" /* LAST include file */
00032 
00033 static CURLcode unit_setup(void)
00034 {
00035   return CURLE_OK;
00036 }
00037 
00038 static void unit_stop(void)
00039 {
00040 
00041 }
00042 
00043 #if defined(MSDOS) || defined(WIN32)
00044 
00045 static char *getflagstr(int flags)
00046 {
00047   char *buf = malloc(256);
00048   fail_unless(buf, "out of memory");
00049   snprintf(buf, 256, "%s,%s,%s,%s",
00050     ((flags & SANITIZE_ALLOW_COLONS) ? "SANITIZE_ALLOW_COLONS" : ""),
00051     ((flags & SANITIZE_ALLOW_PATH) ? "SANITIZE_ALLOW_PATH" : ""),
00052     ((flags & SANITIZE_ALLOW_RESERVED) ? "SANITIZE_ALLOW_RESERVED" : ""),
00053     ((flags & SANITIZE_ALLOW_TRUNCATE) ? "SANITIZE_ALLOW_TRUNCATE" : ""));
00054   return buf;
00055 }
00056 
00057 static char *getcurlcodestr(int cc)
00058 {
00059   char *buf = malloc(256);
00060   fail_unless(buf, "out of memory");
00061   snprintf(buf, 256, "%s (%d)",
00062     (cc == SANITIZE_ERR_OK ? "SANITIZE_ERR_OK" :
00063      cc == SANITIZE_ERR_BAD_ARGUMENT ? "SANITIZE_ERR_BAD_ARGUMENT" :
00064      cc == SANITIZE_ERR_INVALID_PATH ? "SANITIZE_ERR_INVALID_PATH" :
00065      cc == SANITIZE_ERR_OUT_OF_MEMORY ? "SANITIZE_ERR_OUT_OF_MEMORY" :
00066      "unexpected error code - add name"),
00067     cc);
00068   return buf;
00069 }
00070 
00071 struct data {
00072   const char *input;
00073   int flags;
00074   const char *expected_output;
00075   CURLcode expected_result;
00076 };
00077 
00078 UNITTEST_START
00079 
00080 { /* START sanitize_file_name */
00081   struct data data[] = {
00082     { "", 0,
00083       "", SANITIZE_ERR_OK
00084     },
00085     { "normal filename", 0,
00086       "normal filename", SANITIZE_ERR_OK
00087     },
00088     { "control\tchar", 0,
00089       "control_char", SANITIZE_ERR_OK
00090     },
00091     { "banned*char", 0,
00092       "banned_char", SANITIZE_ERR_OK
00093     },
00094     { "f:foo", 0,
00095       "f_foo", SANITIZE_ERR_OK
00096     },
00097     { "f:foo", SANITIZE_ALLOW_COLONS,
00098       "f:foo", SANITIZE_ERR_OK
00099     },
00100     { "f:foo", SANITIZE_ALLOW_PATH,
00101       "f:foo", SANITIZE_ERR_OK
00102     },
00103     { "f:\\foo", 0,
00104       "f__foo", SANITIZE_ERR_OK
00105     },
00106     { "f:\\foo", SANITIZE_ALLOW_PATH,
00107       "f:\\foo", SANITIZE_ERR_OK
00108     },
00109     { "f:/foo", 0,
00110       "f__foo", SANITIZE_ERR_OK
00111     },
00112     { "f:/foo", SANITIZE_ALLOW_PATH,
00113       "f:/foo", SANITIZE_ERR_OK
00114     },
00115 #ifndef MSDOS
00116     { "\\\\?\\C:\\foo", SANITIZE_ALLOW_PATH,
00117       "\\\\?\\C:\\foo", SANITIZE_ERR_OK
00118     },
00119     { "\\\\?\\C:\\foo", 0,
00120       "____C__foo", SANITIZE_ERR_OK
00121     },
00122 #endif
00123     { "foo:bar", 0,
00124       "foo_bar", SANITIZE_ERR_OK
00125     },
00126     { "foo|<>/bar\\\":?*baz", 0,
00127       "foo____bar_____baz", SANITIZE_ERR_OK
00128     },
00129     { "f:foo::$DATA", 0,
00130       "f_foo__$DATA", SANITIZE_ERR_OK
00131     },
00132     { "con . air", 0,
00133       "con _ air", SANITIZE_ERR_OK
00134     },
00135     { "con.air", 0,
00136       "con_air", SANITIZE_ERR_OK
00137     },
00138     { "con:/x", 0,
00139       "con__x", SANITIZE_ERR_OK
00140     },
00141     { "file . . . .  ..  .", 0,
00142       "file", SANITIZE_ERR_OK
00143     },
00144     { "foo . . ? . . ", 0,
00145       "foo . . _", SANITIZE_ERR_OK
00146     },
00147     { "com1", 0,
00148       "_com1", SANITIZE_ERR_OK
00149     },
00150     { "com1", SANITIZE_ALLOW_RESERVED,
00151       "com1", SANITIZE_ERR_OK
00152     },
00153     { "f:\\com1", 0,
00154       "f__com1", SANITIZE_ERR_OK
00155     },
00156     { "f:\\com1", SANITIZE_ALLOW_PATH,
00157       "f:\\_com1", SANITIZE_ERR_OK
00158     },
00159     { "f:\\com1", SANITIZE_ALLOW_RESERVED,
00160       "f__com1", SANITIZE_ERR_OK
00161     },
00162     { "f:\\com1", SANITIZE_ALLOW_RESERVED | SANITIZE_ALLOW_COLONS,
00163       "f:_com1", SANITIZE_ERR_OK
00164     },
00165     { "f:\\com1", SANITIZE_ALLOW_RESERVED | SANITIZE_ALLOW_PATH,
00166       "f:\\com1", SANITIZE_ERR_OK
00167     },
00168     { "com1:\\com1", SANITIZE_ALLOW_PATH,
00169       "_com1:\\_com1", SANITIZE_ERR_OK
00170     },
00171     { "com1:\\com1", SANITIZE_ALLOW_RESERVED | SANITIZE_ALLOW_PATH,
00172       "com1:\\com1", SANITIZE_ERR_OK
00173     },
00174     { "com1:\\com1", SANITIZE_ALLOW_RESERVED,
00175       "com1__com1", SANITIZE_ERR_OK
00176     },
00177 #ifndef MSDOS
00178     { "\\com1", SANITIZE_ALLOW_PATH,
00179       "\\_com1", SANITIZE_ERR_OK
00180     },
00181     { "\\\\com1", SANITIZE_ALLOW_PATH,
00182       "\\\\com1", SANITIZE_ERR_OK
00183     },
00184     { "\\\\?\\C:\\com1", SANITIZE_ALLOW_PATH,
00185       "\\\\?\\C:\\com1", SANITIZE_ERR_OK
00186     },
00187 #endif
00188     { "CoM1", 0,
00189       "_CoM1", SANITIZE_ERR_OK
00190     },
00191     { "CoM1", SANITIZE_ALLOW_RESERVED,
00192       "CoM1", SANITIZE_ERR_OK
00193     },
00194     { "COM56", 0,
00195       "COM56", SANITIZE_ERR_OK
00196     },
00197     /* At the moment we expect a maximum path length of 259. I assume MSDOS
00198        has variable max path lengths depending on compiler that are shorter
00199        so currently these "good" truncate tests won't run on MSDOS */
00200 #ifndef MSDOS
00201     { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
00202       "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
00203       "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
00204       "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
00205       "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
00206       "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
00207         SANITIZE_ALLOW_TRUNCATE,
00208       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
00209       "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
00210       "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
00211       "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
00212       "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
00213       "FFFFF", SANITIZE_ERR_OK
00214     },
00215     { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
00216       "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
00217       "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
00218       "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
00219       "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
00220       "FFF\\FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
00221         SANITIZE_ALLOW_TRUNCATE | SANITIZE_ALLOW_PATH,
00222       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
00223       "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
00224       "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
00225       "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
00226       "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
00227       "FFF\\FFFFF", SANITIZE_ERR_OK
00228     },
00229     { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
00230       "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
00231       "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
00232       "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
00233       "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
00234       "FFF\\FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
00235         SANITIZE_ALLOW_TRUNCATE,
00236       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
00237       "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
00238       "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
00239       "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
00240       "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
00241       "FFF_F", SANITIZE_ERR_OK
00242     },
00243 #endif /* !MSDOS */
00244     { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
00245       "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
00246       "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
00247       "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
00248       "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
00249       "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
00250         0,
00251       NULL, SANITIZE_ERR_INVALID_PATH
00252     },
00253     { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
00254       "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
00255       "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
00256       "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
00257       "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
00258       "FFFF\\FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
00259         SANITIZE_ALLOW_TRUNCATE,
00260       NULL, SANITIZE_ERR_INVALID_PATH
00261     },
00262     { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
00263       "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
00264       "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
00265       "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
00266       "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
00267       "FFFFFFFFFFFFFFFFFFFFFFFFF\\FFFFFFFFFFFFFFFFFFFFFFFF",
00268         SANITIZE_ALLOW_TRUNCATE | SANITIZE_ALLOW_PATH,
00269       NULL, SANITIZE_ERR_INVALID_PATH
00270     },
00271     { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
00272       "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
00273       "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
00274       "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
00275       "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
00276       "FFF\\FFFFFFFFFFFFFFFFFFFFF:FFFFFFFFFFFFFFFFFFFFFFFF",
00277         SANITIZE_ALLOW_TRUNCATE | SANITIZE_ALLOW_PATH,
00278       NULL, SANITIZE_ERR_INVALID_PATH
00279     },
00280     { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
00281       "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
00282       "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
00283       "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
00284       "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
00285       "FF\\F:FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
00286         SANITIZE_ALLOW_TRUNCATE | SANITIZE_ALLOW_PATH,
00287       NULL, SANITIZE_ERR_INVALID_PATH
00288     },
00289     { NULL, 0,
00290       NULL, SANITIZE_ERR_BAD_ARGUMENT
00291     },
00292   };
00293 
00294   size_t i;
00295 
00296   for(i = 0; i < sizeof data / sizeof data[0]; ++i) {
00297     char *output = NULL;
00298     char *flagstr = NULL;
00299     char *received_ccstr = NULL;
00300     char *expected_ccstr = NULL;
00301 
00302     CURLcode res = sanitize_file_name(&output, data[i].input, data[i].flags);
00303 
00304     if(res == data[i].expected_result &&
00305        ((!output && !data[i].expected_output) ||
00306         (output && data[i].expected_output &&
00307          !strcmp(output, data[i].expected_output)))) { /* OK */
00308       free(output);
00309       continue;
00310     }
00311 
00312     flagstr = getflagstr(data[i].flags);
00313     received_ccstr = getcurlcodestr(res);
00314     expected_ccstr = getcurlcodestr(data[i].expected_result);
00315 
00316     unitfail++;
00317     fprintf(stderr, "\n"
00318             "%s:%d sanitize_file_name failed.\n"
00319             "input: %s\n"
00320             "flags: %s\n"
00321             "output: %s\n"
00322             "result: %s\n"
00323             "expected output: %s\n"
00324             "expected result: %s\n",
00325             __FILE__, __LINE__,
00326             data[i].input,
00327             flagstr,
00328             (output ? output : "(null)"),
00329             received_ccstr,
00330             (data[i].expected_output ? data[i].expected_output : "(null)"),
00331             expected_ccstr);
00332 
00333     free(output);
00334     free(flagstr);
00335     free(received_ccstr);
00336     free(expected_ccstr);
00337   }
00338 } /* END sanitize_file_name */
00339 
00340 #else
00341 UNITTEST_START
00342 
00343 {
00344   fprintf(stderr, "Skipped test not for this platform\n");
00345 }
00346 #endif /* MSDOS || WIN32 */
00347 
00348 UNITTEST_STOP


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07