LCOV - code coverage report
Current view: top level - src/dblib/unittests - dbsafestr.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 25 33 75.8 %
Date: 2025-01-18 12:13:41 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.13