LCOV - code coverage report
Current view: top level - src/dblib/unittests - null.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 70 91 76.9 %
Date: 2024-03-23 09:12:27 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /* 
       2             :  * Purpose: Test NULL behavior in order to fix problems with PHP and NULLs
       3             :  * PHP use dbdata to get data
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : 
       8             : #if HAVE_UNISTD_H
       9             : #include <unistd.h>
      10             : #endif /* HAVE_UNISTD_H */
      11             : 
      12             : #ifndef DBNTWIN32
      13             : 
      14             : static DBPROCESS *dbproc = NULL;
      15             : static int failed = 0;
      16             : 
      17             : static int
      18           4 : ignore_msg_handler(DBPROCESS * dbproc, DBINT msgno, int state, int severity, char *text, char *server, char *proc, int line)
      19             : {
      20           4 :         return 0;
      21             : }
      22             : 
      23             : static int
      24           4 : ignore_err_handler(DBPROCESS * dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
      25             : {
      26           4 :         return INT_CANCEL;
      27             : }
      28             : 
      29             : static void
      30         232 : query(const char *query)
      31             : {
      32         232 :         printf("query: %s\n", query);
      33         232 :         dbcmd(dbproc, (char *) query);
      34         232 :         dbsqlexec(dbproc);
      35         232 :         while (dbresults(dbproc) == SUCCEED) {
      36             :                 /* nop */
      37             :         }
      38         232 : }
      39             : 
      40             : static void
      41         152 : test0(int n, int expected)
      42             : {
      43             :         static const char sql[] = "select c from #null where n = %d";
      44             : 
      45         152 :         printf(sql, n);
      46         152 :         printf(" ... ");
      47             :         
      48         152 :         dbfcmd(dbproc, sql, n);
      49             : 
      50         152 :         dbsqlexec(dbproc);
      51             : 
      52         152 :         if (dbresults(dbproc) != SUCCEED || dbnextrow(dbproc) != REG_ROW) {
      53           0 :                 printf("\nExpected a row.\n");
      54           0 :                 failed = 1;
      55           0 :                 dbcancel(dbproc);
      56           0 :                 return;
      57             :         }
      58             : 
      59         152 :         printf("got %p and length %d\n", dbdata(dbproc, 1), dbdatlen(dbproc, 1));
      60             : 
      61         152 :         if (dbdatlen(dbproc, 1) != (expected < 0? 0 : expected)) {
      62           0 :                 fprintf(stderr, "Error: n=%d: dbdatlen returned %d, expected %d\n", 
      63             :                                 n, dbdatlen(dbproc, 1), expected < 0? 0 : expected);
      64           0 :                 dbcancel(dbproc);
      65           0 :                 failed = 1;
      66             :         }
      67             : 
      68         152 :         if (dbdata(dbproc, 1) != NULL && expected  < 0) {
      69           0 :                 fprintf(stderr, "Error: n=%d: dbdata returned %p, expected NULL and length %d\n", 
      70             :                                 n, dbdata(dbproc, 1), expected);
      71           0 :                 dbcancel(dbproc);
      72           0 :                 failed = 1;
      73             :         }
      74             : 
      75         152 :         if (dbdata(dbproc, 1) == NULL && expected  > 0) {
      76           0 :                 fprintf(stderr, "Error: n=%d: dbdata returned %p, expected non-NULL and length %d\n", 
      77             :                                 n, dbdata(dbproc, 1), expected);
      78           0 :                 dbcancel(dbproc);
      79           0 :                 failed = 1;
      80             :         }
      81             : 
      82         152 :         if (dbnextrow(dbproc) != NO_MORE_ROWS) {
      83           0 :                 fprintf(stderr, "Error: Only one row expected (cancelling remaining results)\n");
      84           0 :                 dbcancel(dbproc);
      85           0 :                 failed = 1;
      86             :         }
      87             :         
      88             : 
      89             : 
      90         152 :         while (dbresults(dbproc) == SUCCEED) {
      91             :                 /* nop */
      92             :         }
      93             : }
      94             : 
      95             : 
      96             : static void
      97          42 : test(const char *type, int give_err)
      98             : {
      99             :         RETCODE ret;
     100             : 
     101          42 :         query("if object_id('#null') is not NULL drop table #null");
     102             : 
     103          42 :         dberrhandle(ignore_err_handler);
     104          42 :         dbmsghandle(ignore_msg_handler);
     105             : 
     106          42 :         printf("create table #null (n int, c %s NULL)\n", type);
     107          42 :         dbfcmd(dbproc, "create table #null (n int, c %s NULL)", type);
     108          42 :         dbsqlexec(dbproc);
     109             : 
     110          42 :         ret = dbresults(dbproc);
     111             : 
     112          42 :         dberrhandle(syb_err_handler);
     113          42 :         dbmsghandle(syb_msg_handler);
     114             : 
     115          42 :         if (ret != SUCCEED) {
     116           4 :                 dbcancel(dbproc);
     117           4 :                 if (!give_err)
     118             :                         return;
     119           0 :                 printf("Was expecting a result set.\n");
     120           0 :                 failed = 1;
     121           0 :                 return;
     122             :         }
     123             : 
     124          38 :         query("insert into #null values(1, '')");
     125          38 :         query("insert into #null values(2, NULL)");
     126          38 :         query("insert into #null values(3, ' ')");
     127          38 :         query("insert into #null values(4, 'a')");
     128             : 
     129          38 :         test0(1, DBTDS_5_0 < DBTDS(dbproc)?  0 : 1);
     130          38 :         test0(2, DBTDS_5_0 < DBTDS(dbproc)? -1 : 0);
     131          38 :         test0(3, 1);
     132          38 :         test0(4, 1);
     133             : 
     134          38 :         query("drop table #null");
     135             : }
     136             : 
     137             : int
     138           8 : main(int argc, char **argv)
     139             : {
     140             :         LOGINREC *login;
     141             : 
     142           8 :         read_login_info(argc, argv);
     143             : 
     144           8 :         printf("Starting %s\n", argv[0]);
     145             : 
     146           8 :         dbinit();
     147             : 
     148           8 :         dberrhandle(syb_err_handler);
     149           8 :         dbmsghandle(syb_msg_handler);
     150             : 
     151           8 :         printf("About to logon\n");
     152             : 
     153           8 :         login = dblogin();
     154           8 :         DBSETLPWD(login, PASSWORD);
     155           8 :         DBSETLUSER(login, USER);
     156           8 :         DBSETLAPP(login, "thread");
     157             : 
     158           8 :         printf("About to open \"%s\"\n", SERVER);
     159             : 
     160           8 :         dbproc = dbopen(login, SERVER);
     161           8 :         if (!dbproc) {
     162           0 :                 fprintf(stderr, "Unable to connect to %s\n", SERVER);
     163           0 :                 return 1;
     164             :         }
     165             : 
     166           8 :         dbloginfree(login);
     167             : 
     168           8 :         if (strlen(DATABASE))
     169           8 :                 dbuse(dbproc, DATABASE);
     170             : 
     171           8 :         test("VARCHAR(10)", 1);
     172           8 :         test("TEXT", 1);
     173             : 
     174           8 :         test("NVARCHAR(10)", 0);
     175           8 :         if (DBTDS_5_0 < DBTDS(dbproc)) {
     176           6 :                 test("NTEXT", 0);
     177           6 :                 test("VARCHAR(MAX)", 0);
     178           6 :                 test("NVARCHAR(MAX)", 0);
     179             :         }
     180             : 
     181           8 :         dbexit();
     182             : 
     183           8 :         return failed ? 1 : 0;
     184             : }
     185             : #else
     186             : int main(void)
     187             : {
     188             :         fprintf(stderr, "Not supported by MS DBLib\n");
     189             :         return 0;
     190             : }
     191             : #endif
     192             : 

Generated by: LCOV version 1.13