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

Generated by: LCOV version 1.13