LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0001.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 49 72 68.1 %
Date: 2026-11-07 23:58:45 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* 
       2             :  * Purpose: Log in, create a table, insert a few rows, select them, and log out.   
       3             :  * Functions: dbbind dbcmd dbcolname dberrhandle dbisopt dbmsghandle dbnextrow dbnumcols dbopen dbresults dbsetlogintime dbsqlexec dbuse 
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : 
       8             : #include <freetds/bool.h>
       9             : 
      10          10 : TEST_MAIN()
      11             : {
      12          10 :         bool failed = false;
      13          10 :         const int rows_to_add = 50;
      14             :         LOGINREC *login;
      15             :         DBPROCESS *dbproc;
      16             :         int i;
      17             :         char teststr[1024];
      18             :         DBINT testint, erc;
      19             : 
      20          10 :         set_malloc_options();
      21             : 
      22          10 :         read_login_info(argc, argv);
      23             : 
      24          10 :         printf("Starting %s\n", argv[0]);
      25             : 
      26             :         /* Fortify_EnterScope(); */
      27          10 :         dbinit();
      28             : 
      29          10 :         dberrhandle(syb_err_handler);
      30          10 :         dbmsghandle(syb_msg_handler);
      31             : 
      32          10 :         printf("About to logon as \"%s\"\n", USER);
      33             : 
      34          10 :         login = dblogin();
      35          10 :         DBSETLPWD(login, PASSWORD);
      36          10 :         DBSETLUSER(login, USER);
      37          10 :         DBSETLAPP(login, "t0001");
      38             : 
      39          10 :         printf("About to open \"%s\"\n", SERVER);
      40             : 
      41          10 :         dbproc = dbopen(login, SERVER);
      42          10 :         if (!dbproc) {
      43           0 :                 fprintf(stderr, "Unable to connect to %s\n", SERVER);
      44           0 :                 return 1;
      45             :         }
      46          10 :         dbloginfree(login);
      47             : 
      48          10 :         printf("Using database \"%s\"\n", DATABASE);
      49          10 :         if (strlen(DATABASE)) {
      50          10 :                 erc = dbuse(dbproc, DATABASE);
      51          10 :                 assert(erc == SUCCEED);
      52             :         }
      53             : #ifdef DBQUOTEDIDENT
      54          10 :         printf("QUOTED_IDENTIFIER is %s\n", (dbisopt(dbproc, DBQUOTEDIDENT, NULL)) ? "ON" : "OFF");
      55             : #endif
      56          10 :         sql_cmd(dbproc);
      57          10 :         dbsqlexec(dbproc);
      58          10 :         while (dbresults(dbproc) == SUCCEED) {
      59             :                 /* nop */
      60             :         }
      61             : 
      62         500 :         for (i = 0; i < rows_to_add && sql_cmd(dbproc) == SUCCEED; i++) {
      63         500 :                 dbsqlexec(dbproc);
      64         500 :                 while (dbresults(dbproc) == SUCCEED) {
      65             :                         /* nop */
      66             :                 }
      67             :         }
      68             : 
      69          10 :         sql_cmd(dbproc);
      70          10 :         dbsqlexec(dbproc);
      71             : 
      72          10 :         if (dbresults(dbproc) != SUCCEED) {
      73           0 :                 failed = true;
      74           0 :                 fprintf(stderr, "error: expected a result set, none returned.\n");
      75           0 :                 exit(1);
      76             :         }
      77             : 
      78          20 :         for (i = 1; i <= dbnumcols(dbproc); i++)
      79          20 :                 printf("col %d is %s\n", i, dbcolname(dbproc, i));
      80             : 
      81          10 :         if (SUCCEED != dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint)) {
      82           0 :                 failed = true;
      83           0 :                 fprintf(stderr, "Had problem with bind\n");
      84           0 :                 abort();
      85             :         }
      86          10 :         if (SUCCEED != dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr)) {
      87           0 :                 failed = true;
      88           0 :                 fprintf(stderr, "Had problem with bind\n");
      89           0 :                 abort();
      90             :         }
      91             : 
      92         500 :         for (i = 0; i < rows_to_add; i++) {
      93             :                 char expected[1024];
      94             : 
      95         500 :                 sprintf(expected, "row %03d", i);
      96             : 
      97         500 :                 memset(teststr, 'x', sizeof(teststr));
      98         500 :                 teststr[0] = 0;
      99         500 :                 teststr[sizeof(teststr) - 1] = 0;
     100         500 :                 if (REG_ROW != dbnextrow(dbproc)) {
     101           0 :                         failed = true;
     102           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
     103           0 :                         exit(1);
     104             :                 }
     105         500 :                 if (testint != i) {
     106           0 :                         failed = true;
     107           0 :                         fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", i, (int) testint);
     108           0 :                         abort();
     109             :                 }
     110         500 :                 if (0 != strncmp(teststr, expected, strlen(expected))) {
     111           0 :                         failed = true;
     112           0 :                         printf("Failed.  Expected s to be |%s|, was |%s|\n", expected, teststr);
     113           0 :                         abort();
     114             :                 }
     115         500 :                 printf("Read a row of data -> %d |%s|\n", (int) testint, teststr);
     116             :         }
     117             : 
     118          10 :         if (dbnextrow(dbproc) != NO_MORE_ROWS) {
     119           0 :                 failed = true;
     120           0 :                 fprintf(stderr, "Was expecting no more rows\n");
     121           0 :                 exit(1);
     122             :         }
     123             : 
     124          10 :         dbexit();
     125             : 
     126          10 :         printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
     127          10 :         return failed ? 1 : 0;
     128             : }

Generated by: LCOV version 1.13