1 : /*
2 : * Purpose: Test dbsafestr()
3 : * Functions: dbsafestr
4 : */
5 :
6 : #if HAVE_CONFIG_H
7 : #include <config.h>
8 : #endif /* HAVE_CONFIG_H */
9 :
10 : #include <stdio.h>
11 :
12 : #if HAVE_STDLIB_H
13 : #include <stdlib.h>
14 : #endif /* HAVE_STDLIB_H */
15 :
16 : #if HAVE_STRING_H
17 : #include <string.h>
18 : #endif /* HAVE_STRING_H */
19 :
20 : #include <sqlfront.h>
21 : #include <sqldb.h>
22 :
23 : #include "common.h"
24 :
25 : static char software_version[] = "$Id: t0021.c,v 1.11 2005/04/19 03:51:04 jklowden Exp $";
26 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
27 :
28 :
29 :
30 : int failed = 0;
31 :
32 : /* unsafestr must contain one quote of each type */
33 : const char *unsafestr = "This is a string with ' and \" in it.";
34 :
35 : /* safestr must be at least strlen(unsafestr) + 3 */
36 : char safestr[100];
37 :
38 : int
39 : main(int argc, char **argv)
40 2 : {
41 : int len;
42 : RETCODE ret;
43 :
44 2 : set_malloc_options();
45 :
46 2 : fprintf(stdout, "Start\n");
47 :
48 : /* Fortify_EnterScope(); */
49 2 : dbinit();
50 :
51 :
52 2 : len = strlen(unsafestr);
53 2 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len, DBSINGLE);
54 2 : if (ret != FAIL)
55 0 : failed++;
56 2 : fprintf(stdout, "short buffer, single\n%s\n", safestr);
57 : /* plus one for termination and one for the quote */
58 2 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len + 2, DBSINGLE);
59 2 : if (ret != SUCCEED)
60 0 : failed++;
61 2 : if (strlen(safestr) != len + 1)
62 0 : failed++;
63 2 : fprintf(stdout, "single quote\n%s\n", safestr);
64 2 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len + 2, DBDOUBLE);
65 2 : if (ret != SUCCEED)
66 0 : failed++;
67 2 : if (strlen(safestr) != len + 1)
68 0 : failed++;
69 2 : fprintf(stdout, "double quote\n%s\n", safestr);
70 2 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len + 3, DBBOTH);
71 2 : if (ret != SUCCEED)
72 0 : failed++;
73 2 : if (strlen(safestr) != len + 2)
74 0 : failed++;
75 2 : fprintf(stdout, "both quotes\n%s\n", safestr);
76 :
77 2 : dbexit();
78 :
79 2 : fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
80 2 : return failed ? 1 : 0;
81 : }
|