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 10 : TEST_MAIN()
19 : {
20 : int len;
21 : RETCODE ret;
22 :
23 10 : set_malloc_options();
24 :
25 10 : printf("Starting %s\n", argv[0]);
26 :
27 10 : dbinit();
28 :
29 :
30 10 : len = sizeof(unsafestr) - 1;
31 10 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len, DBSINGLE);
32 10 : if (ret != FAIL)
33 0 : failed++;
34 10 : printf("short buffer, single\n%s\n", safestr);
35 : /* plus one for termination and one for the quote */
36 10 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len + 2, DBSINGLE);
37 10 : if (ret != SUCCEED)
38 0 : failed++;
39 10 : if (strlen(safestr) != len + 1)
40 0 : failed++;
41 10 : printf("single quote\n%s\n", safestr);
42 10 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len + 2, DBDOUBLE);
43 10 : if (ret != SUCCEED)
44 0 : failed++;
45 10 : if (strlen(safestr) != len + 1)
46 0 : failed++;
47 10 : printf("double quote\n%s\n", safestr);
48 10 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len + 2, DBBOTH);
49 10 : if (ret != FAIL)
50 0 : failed++;
51 10 : ret = dbsafestr(NULL, unsafestr, -1, safestr, len + 3, DBBOTH);
52 10 : if (ret != SUCCEED)
53 0 : failed++;
54 10 : if (strlen(safestr) != len + 2)
55 0 : failed++;
56 10 : printf("both quotes\n%s\n", safestr);
57 :
58 10 : dbexit();
59 :
60 10 : printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
61 10 : return failed ? 1 : 0;
62 : }
63 : #else
64 : TEST_MAIN()
65 : {
66 : fprintf(stderr, "Not supported by MS DBLib\n");
67 : return 0;
68 : }
69 : #endif
|