LCOV - code coverage report
Current view: top level - src/dblib/unittests - bcp2.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 94 120 78.3 %
Date: 2026-11-25 04:57:11 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /*
       2             :  * Purpose: Test bcp functions, specifically some NULL behaviour
       3             :  * Functions: bcp_bind bcp_done bcp_init bcp_sendrow
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : 
       8             : #include <assert.h>
       9             : 
      10             : static void
      11           0 : doexit(int value)
      12             : {
      13           0 :         dbexit();            /* always call dbexit before returning to OS */
      14           0 :         exit(value);
      15             : }
      16             : 
      17             : static int
      18          10 : init(DBPROCESS * dbproc, const char *name)
      19             : {
      20             :         RETCODE rc;
      21             : 
      22          10 :         printf("Dropping %s.%s..%s\n", SERVER, DATABASE, name);
      23          10 :         sql_cmd(dbproc);
      24          10 :         dbsqlexec(dbproc);
      25          34 :         while ((rc=dbresults(dbproc)) == SUCCEED)
      26          14 :                 continue;
      27          10 :         if (rc != NO_MORE_RESULTS)
      28             :                 return 1;
      29             : 
      30          10 :         printf("Creating %s.%s..%s\n", SERVER, DATABASE, name);
      31          10 :         sql_cmd(dbproc);
      32             : 
      33          10 :         if (dbsqlexec(dbproc) == FAIL)
      34             :                 return 1;
      35          20 :         while ((rc=dbresults(dbproc)) == SUCCEED)
      36          10 :                 continue;
      37          10 :         if (rc != NO_MORE_RESULTS)
      38             :                 return 1;
      39          10 :         printf("ok\n");
      40          10 :         return 0;
      41             : }
      42             : 
      43          10 : TEST_MAIN()
      44             : {
      45             :         LOGINREC *login;
      46             :         DBPROCESS *dbproc;
      47             :         DBINT msgno;
      48          10 :         int num_rows, expected = 3;
      49             :         static const char table_name[] = "bcp_test";
      50             : 
      51             :         /* Variables for host file data to be copied to database. */
      52             :         char s1[11];
      53             :         char s2[11];
      54             :         char s3[11];
      55          10 :         char long_s1[] = "test short but too long";
      56             : 
      57          10 :         set_malloc_options();
      58             : 
      59          10 :         read_login_info(argc, argv);
      60             : 
      61          10 :         printf("Starting %s\n", argv[0]);
      62             : 
      63          10 :         dbsetversion(DBVERSION_100);
      64          10 :         dbinit();
      65             : 
      66          10 :         dberrhandle(syb_err_handler);
      67          10 :         dbmsghandle(syb_msg_handler);
      68             : 
      69          10 :         printf("About to logon\n");
      70             : 
      71          10 :         login = dblogin();
      72          10 :         DBSETLPWD(login, PASSWORD);
      73          10 :         DBSETLUSER(login, USER);
      74          10 :         DBSETLAPP(login, "bcp2.c unit test");
      75          10 :         BCP_SETL(login, TRUE);
      76             : 
      77          10 :         printf("About to open %s.%s\n", SERVER, DATABASE);
      78             : 
      79          10 :         dbproc = dbopen(login, SERVER);
      80          10 :         if (strlen(DATABASE))
      81          10 :                 dbuse(dbproc, DATABASE);
      82          10 :         dbloginfree(login);
      83             : 
      84          10 :         if (init(dbproc, table_name))
      85           0 :                 doexit(1);
      86             : 
      87             :         /* set up and send the bcp */
      88          10 :         strcpy(s1, "test short");
      89          10 :         strcpy(s2, " ");
      90          10 :         strcpy(s3, "");
      91             : 
      92          10 :         if (bcp_init(dbproc, "bcp_test", NULL, "bcp.errors", DB_IN) != SUCCEED)
      93           0 :                 doexit(1);
      94             : 
      95          10 :         bcp_bind(dbproc, (BYTE *)s1, 0, -1, (BYTE *)"", 1, SYBCHAR, 1);
      96          10 :         bcp_bind(dbproc, (BYTE *)s2, 0, -1, (BYTE *)"", 1, SYBCHAR, 2);
      97          10 :         bcp_bind(dbproc, (BYTE *)s3, 0, -1, (BYTE *)"", 1, SYBCHAR, 3);
      98             : 
      99          10 :         printf("Sending some rows... \n");
     100          10 :         if (bcp_sendrow(dbproc) == FAIL) {
     101           0 :                 fprintf(stderr, "send failed\n");
     102           0 :                 doexit(1);
     103             :         }
     104             : 
     105          10 :         strcpy(s2, "x");
     106          10 :         strcpy(s3, " ");
     107          10 :         if (bcp_sendrow(dbproc) == FAIL) {
     108           0 :                 fprintf(stderr, "send failed\n");
     109           0 :                 doexit(1);
     110             :         }
     111             : 
     112             :         /* data too long are truncated with a warning */
     113          10 :         bcp_bind(dbproc, (BYTE *) long_s1, 0, -1, (BYTE *) "", 1, SYBCHAR, 1);
     114          10 :         strcpy(s2, "y");
     115          10 :         strcpy(s3, "z");
     116          10 :         msgno = 20074;
     117          10 :         dbsetuserdata(dbproc, (BYTE *) & msgno);
     118          10 :         if (bcp_sendrow(dbproc) == FAIL) {
     119           0 :                 fprintf(stderr, "send truncated failed\n");
     120           0 :                 doexit(1);
     121             :         }
     122          10 :         dbsetuserdata(dbproc, NULL);
     123          10 :         if (msgno != 0) {
     124           0 :                 fprintf(stderr, "expected truncation warning not received\n");
     125           0 :                 doexit(1);
     126             :         }
     127          10 :         bcp_bind(dbproc, (BYTE *) s1, 0, -1, (BYTE *) "", 1, SYBCHAR, 1);
     128             : 
     129             :         /* In MSSQL the error is reported during bcp_done but all inserts are ignored */
     130          10 :         if (DBTDS_5_0 == DBTDS(dbproc)) {
     131           2 :                 msgno = 20073;
     132           2 :                 dbsetuserdata(dbproc, (BYTE*) &msgno);
     133           2 :                 strcpy(s2, "");
     134           2 :                 strcpy(s3, "");
     135           2 :                 if (bcp_sendrow(dbproc) != FAIL) {
     136           0 :                         fprintf(stderr, "send NULL succeeded\n");
     137           0 :                         doexit(1);
     138             :                 }
     139           2 :                 dbsetuserdata(dbproc, NULL);
     140             : 
     141             :                 /* end bcp. */
     142           2 :                 if (bcp_done(dbproc) != expected) {
     143           0 :                         fprintf(stderr, "Bulk copy unsuccessful.\n");
     144           0 :                         doexit(1);
     145             :                 }
     146             :         } else {
     147             :                 /* end bcp. */
     148           8 :                 if (bcp_done(dbproc) != expected) {
     149           0 :                         fprintf(stderr, "Bulk copy unsuccessful.\n");
     150           0 :                         doexit(1);
     151             :                 }
     152             : 
     153             :                 /* another insert with error */
     154           8 :                 if (bcp_init(dbproc, "bcp_test", NULL, "bcp.errors", DB_IN) != SUCCEED)
     155           0 :                         doexit(1);
     156             : 
     157           8 :                 bcp_bind(dbproc, (BYTE *)s1, 0, -1, (BYTE *)"", 1, SYBCHAR, 1);
     158           8 :                 bcp_bind(dbproc, (BYTE *)s2, 0, -1, (BYTE *)"", 1, SYBCHAR, 2);
     159           8 :                 bcp_bind(dbproc, (BYTE *)s3, 0, -1, (BYTE *)"", 1, SYBCHAR, 3);
     160             : 
     161           8 :                 strcpy(s2, "");
     162           8 :                 strcpy(s3, "");
     163           8 :                 if (bcp_sendrow(dbproc) == FAIL) {
     164           0 :                         fprintf(stderr, "send failed\n");
     165           0 :                         doexit(1);
     166             :                 }
     167             : 
     168             :                 /* end bcp. */
     169           8 :                 msgno = 515;
     170           8 :                 dbsetuserdata(dbproc, (BYTE*) &msgno);
     171           8 :                 num_rows = bcp_done(dbproc);
     172           8 :                 if (num_rows != -1) {
     173           0 :                         fprintf(stderr, "Bulk copy successful. %d rows returned\n", num_rows);
     174           0 :                         doexit(1);
     175             :                 }
     176           8 :                 dbsetuserdata(dbproc, NULL);
     177             :         }
     178             : 
     179          10 :         printf("done\n");
     180             : 
     181             :         /* check we inserted the expected number of rows row */
     182          10 :         num_rows = 0;
     183          10 :         sql_cmd(dbproc);
     184          10 :         dbsqlexec(dbproc);
     185          30 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
     186          40 :                 while (dbnextrow(dbproc) == REG_ROW)
     187          30 :                         num_rows++;
     188             :         }
     189          10 :         if (num_rows != expected) {
     190           0 :                 fprintf(stderr, "Expected %d row(s), got %d\n", expected, num_rows);
     191           0 :                 doexit(1);
     192             :         }
     193             : 
     194          10 :         printf("Dropping table %s\n", table_name);
     195          10 :         sql_cmd(dbproc);
     196          10 :         dbsqlexec(dbproc);
     197          10 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
     198             :                 /* nop */
     199             :         }
     200          10 :         dbexit();
     201             : 
     202          10 :         printf("%s OK\n", __FILE__);
     203          10 :         return 0;
     204             : }

Generated by: LCOV version 1.13