Line data Source code
1 : /*
2 : * Purpose: Test dbsafestr()
3 : * Functions: dbsafestr
4 : */
5 :
6 : #include "common.h"
7 :
8 : #ifndef DBNTWIN32
9 :
10 : static int failed = 0;
11 :
12 : /* unsafestr must contain one quote of each type */
13 : static const char *unsafestr = "This is a string with ' and \" in it.";
14 :
15 : /* safestr must be at least strlen(unsafestr) + 3 */
16 : static char safestr[100];
17 :
18 : int
19 10 : main(int argc TDS_UNUSED, char **argv)
20 : {
21 : int len;
22 : RETCODE ret;
23 :
24 10 : set_malloc_options();
25 :
26 10 : printf("Starting %s\n", argv[0]);
27 :
28 10 : dbinit();
29 :
30 :
31 10 : len = strlen(unsafestr);
32 10 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len, DBSINGLE);
33 10 : if (ret != FAIL)
34 0 : failed++;
35 10 : printf("short buffer, single\n%s\n", safestr);
36 : /* plus one for termination and one for the quote */
37 10 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len + 2, DBSINGLE);
38 10 : if (ret != SUCCEED)
39 0 : failed++;
40 10 : if (strlen(safestr) != len + 1)
41 0 : failed++;
42 10 : printf("single quote\n%s\n", safestr);
43 10 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len + 2, DBDOUBLE);
44 10 : if (ret != SUCCEED)
45 0 : failed++;
46 10 : if (strlen(safestr) != len + 1)
47 0 : failed++;
48 10 : printf("double quote\n%s\n", safestr);
49 10 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len + 2, DBBOTH);
50 10 : if (ret != FAIL)
51 0 : failed++;
52 10 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len + 3, DBBOTH);
53 10 : if (ret != SUCCEED)
54 0 : failed++;
55 10 : if (strlen(safestr) != len + 2)
56 0 : failed++;
57 10 : printf("both quotes\n%s\n", safestr);
58 :
59 10 : dbexit();
60 :
61 10 : printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
62 10 : return failed ? 1 : 0;
63 : }
64 : #else
65 : int main(void)
66 : {
67 : fprintf(stderr, "Not supported by MS DBLib\n");
68 : return 0;
69 : }
70 : #endif
|