LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0016.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 226 273 82.8 %
Date: 2026-11-25 04:57:11 Functions: 9 10 90.0 %

          Line data    Source code
       1             : /* 
       2             :  * Purpose: Test bcp in and out, and specifically bcp_colfmt()
       3             :  * Functions: bcp_colfmt bcp_columns bcp_exec bcp_init 
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : 
       8             : #include <freetds/bool.h>
       9             : #include <freetds/replacements.h>
      10             : #include <freetds/utils.h>
      11             : 
      12             : static bool failed = false;
      13             : 
      14             : static void
      15           0 : failure(const char *fmt, ...)
      16             : {
      17             :         va_list ap;
      18             : 
      19           0 :         failed = true;
      20             : 
      21           0 :         va_start(ap, fmt);
      22           0 :         vfprintf(stderr, fmt, ap);
      23           0 :         va_end(ap);
      24           0 : }
      25             : 
      26             : #define INFILE_NAME "t0016"
      27             : #define TABLE_NAME "#dblib0016"
      28             : 
      29             : static void test_file(const char *fn);
      30             : static bool compare_files(const char *fn1, const char *fn2);
      31             : static unsigned count_file_rows(FILE * f);
      32             : static size_t fgets_raw(char *s, int len, FILE * f);
      33             : static DBPROCESS *dbproc;
      34             : 
      35             : /* error expected as a warning (data copied anyway), 0 if none */
      36             : static int expected_warning = 0;
      37             : static unsigned num_warnings = 0;
      38             : 
      39             : /* custom error handler for clean exit */
      40             : static int
      41          40 : t0016_syb_err_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
      42             : {
      43             :         int retval;
      44             : 
      45          40 :         if (expected_warning && dberr == expected_warning) {
      46          40 :                 printf("OK: expected warning %d (%s) arrived\n", dberr, dberrstr);
      47          40 :                 ++num_warnings;
      48          40 :                 return INT_CANCEL;
      49             :         }
      50             : 
      51           0 :         retval = syb_err_logmsg(dbproc, severity, dberr, oserr, dberrstr, oserrstr);
      52             : 
      53           0 :         if (retval != 0)
      54             :                 return retval;
      55             : 
      56           0 :         failed = true;
      57           0 :         return INT_CANCEL;
      58             : }
      59             : 
      60          10 : TEST_MAIN()
      61             : {
      62             :         LOGINREC *login;
      63             :         char in_file[30];
      64          10 :         unsigned int n = 1, end = 100;
      65          10 :         unsigned int n_pass = 0, n_fail = 0;
      66             : 
      67          10 :         setbuf(stdout, NULL);
      68          10 :         setbuf(stderr, NULL);
      69             : 
      70          10 :         set_malloc_options();
      71             : 
      72          10 :         read_login_info(argc, argv);
      73          10 :         printf("Starting %s\n", argv[0]);
      74          10 :         dbsetversion(DBVERSION_100);
      75          10 :         dbinit();
      76             : 
      77          10 :         dberrhandle(syb_err_handler);
      78          10 :         dbmsghandle(syb_msg_handler);
      79             : 
      80          10 :         printf("About to logon\n");
      81             : 
      82          10 :         login = dblogin();
      83          10 :         BCP_SETL(login, TRUE);
      84          10 :         DBSETLPWD(login, PASSWORD);
      85          10 :         DBSETLUSER(login, USER);
      86          10 :         DBSETLAPP(login, "t0016");
      87          10 :         DBSETLCHARSET(login, "utf8");
      88             : 
      89          10 :         dbproc = dbopen(login, SERVER);
      90          10 :         if (strlen(DATABASE)) {
      91          10 :                 dbuse(dbproc, DATABASE);
      92             :         }
      93          10 :         dbloginfree(login);
      94          10 :         printf("After logon\n");
      95             : 
      96             :         /* First testcase already had SQL opened by read_login_info() */
      97          10 :         strcpy(in_file, INFILE_NAME);
      98             : 
      99             :         /* Developer option to focus on specific tests */
     100             :         {
     101          10 :                 char const *var = getenv("TDST0016_BEGIN");
     102             : 
     103          10 :                 if (var)
     104           0 :                         n = atoi(var);
     105          10 :                 var = getenv("TDST0016_END");
     106          10 :                 if (var)
     107           0 :                         end = atoi(var);
     108             :         }
     109             : 
     110             :         /* Execute all testcases (carry on even if one fails) */
     111          10 :         if (n < 1)
     112           0 :                 n = 1;
     113         230 :         for (; n <= end; ++n) {
     114         240 :                 failed = FALSE;
     115         240 :                 test_file(in_file);
     116         240 :                 failed ? ++n_fail : ++n_pass;
     117             : 
     118         240 :                 sprintf(in_file, "%s_%d", INFILE_NAME, n);
     119         240 :                 if (sql_reopen(in_file) != SUCCEED)
     120             :                         break;
     121             :         }
     122          10 :         printf("===== End of t0016 subtests =====\n");
     123             : 
     124          10 :         dbclose(dbproc);
     125          10 :         dbexit();
     126             : 
     127          10 :         printf("dblib t0016: %d pass, %d fail.\n", n_pass, n_fail);
     128          10 :         printf("=================================\n");
     129          10 :         return n_fail ? 1 : 0;
     130             : }
     131             : 
     132             : static bool got_error = false;
     133             : 
     134             : static int
     135          56 : ignore_msg_handler(DBPROCESS *dbproc TDS_UNUSED, DBINT msgno TDS_UNUSED, int state TDS_UNUSED, int severity TDS_UNUSED,
     136             :                    char *text TDS_UNUSED, char *server TDS_UNUSED, char *proc TDS_UNUSED, int line TDS_UNUSED)
     137             : {
     138          56 :         got_error = true;
     139          56 :         return 0;
     140             : }
     141             : 
     142             : static int
     143          56 : ignore_err_handler(DBPROCESS *dbproc TDS_UNUSED, int severity TDS_UNUSED, int dberr TDS_UNUSED,
     144             :                    int oserr TDS_UNUSED, char *dberrstr TDS_UNUSED, char *oserrstr TDS_UNUSED)
     145             : {
     146          56 :         got_error = true;
     147          56 :         return INT_CANCEL;
     148             : }
     149             : 
     150             : /* T0016 data files are all tab-delimited char columns, but we support omitting
     151             :  * columns in order to simulate testing of BCP with a format file that omits
     152             :  * columns (feature supported by ASE, but not MSSQL)
     153             :  */
     154             : static void
     155         352 : format_columns(int table_cols, char *col_list)
     156             : {
     157         352 :         const char *seps = ", ";
     158         352 :         int colnum = 1;         /* bcp_colfmt uses 1-based indexing */
     159             :         RETCODE ret;
     160         352 :         const char *tok = NULL;
     161             :         int num_cols;
     162             : 
     163             :         /* Count number of columns wanted */
     164         354 :         if (col_list && col_list[0]) {
     165             :                 char *dup;
     166             : 
     167           2 :                 printf("Custom columns: %s\n", col_list);
     168           2 :                 num_cols = 0;
     169           2 :                 dup = xstrdup(col_list);
     170           4 :                 for (tok = strtok(dup, seps); tok; tok = strtok(NULL, seps))
     171           2 :                         ++num_cols;
     172           2 :                 free(dup);
     173             : 
     174           2 :                 tok = strtok(col_list, seps);
     175             :         } else
     176             :                 num_cols = table_cols;
     177             : 
     178         352 :         if (num_cols == 0) {
     179           0 :                 failure("PARAM cols contained no valid items.\n");
     180           0 :                 return;
     181             :         }
     182             : 
     183         352 :         ret = bcp_columns(dbproc, num_cols);
     184         352 :         if (ret != SUCCEED) {
     185           0 :                 failure("return from bcp_columns = %d\n", ret);
     186           0 :                 return;
     187             :         }
     188             : 
     189        1852 :         for (colnum = 1; colnum <= num_cols; ++colnum) {
     190             :                 int host_column;
     191             : 
     192        1852 :                 if (tok) {
     193           2 :                         host_column = atoi(tok);
     194           2 :                         if (host_column < 1 || host_column > table_cols)
     195           0 :                                 failure("PARAM cols %d out of range (1-%d)\n", host_column, table_cols);
     196           2 :                         tok = strtok(NULL, seps);
     197             :                 } else
     198             :                         host_column = colnum;
     199             : 
     200        1852 :                 ret = bcp_colfmt(dbproc, host_column, SYBCHAR, 0, -1,
     201             :                                  (BYTE *) (colnum == num_cols ? "\n" : "\t"), sizeof(char), colnum);
     202             : 
     203        1852 :                 if (ret == FAIL)
     204           0 :                         failure("return from bcp_colfmt(%d,%d) = FAIL\n", host_column, colnum);
     205             :         }
     206             : }
     207             : 
     208             : static char line1[1024 * 16];
     209             : static char line2[1024 * 16];
     210             : 
     211             : static void
     212         240 : test_file(const char *fn)
     213             : {
     214             :         RETCODE ret;
     215         240 :         int num_cols = 0;
     216         240 :         const char *out_file = "t0016.out";
     217         240 :         const char *err_file = "t0016.err";
     218             :         DBINT rows_copied;
     219         240 :         unsigned num_rows = 2;
     220             :         char table_name[64];
     221             :         char hints[64];
     222         240 :         char colin[64] = { 0 };
     223         240 :         char colout[64] = { 0 };
     224         240 :         bool fmt_file_exists = false;
     225             : 
     226             :         FILE *input_file;
     227             : 
     228             :         char sql_file[256];
     229             :         char in_file[256];
     230             :         char exp_file[256];
     231             :         char fmt_file[256];
     232             : 
     233         240 :         snprintf(sql_file, sizeof(sql_file), "%s/%s.sql", FREETDS_SRCDIR, fn);
     234         240 :         snprintf(in_file, sizeof(in_file), "%s/%s.in", FREETDS_SRCDIR, fn);
     235         240 :         snprintf(exp_file, sizeof(exp_file), "%s/%s.exp", FREETDS_SRCDIR, fn);
     236         240 :         snprintf(fmt_file, sizeof(fmt_file), "%s/%s.fmt", FREETDS_SRCDIR, fn);
     237             : 
     238         240 :         strlcpy(table_name, TABLE_NAME, sizeof(table_name));
     239         240 :         hints[0] = 0;
     240         240 :         expected_warning = 0;
     241         240 :         num_warnings = 0;
     242             : 
     243         240 :         printf("===== %s =====\n", fn);
     244         240 :         input_file = fopen(fmt_file, "r");
     245         240 :         if (input_file) {
     246          20 :                 fmt_file_exists = true;
     247          20 :                 fclose(input_file);
     248             :         }
     249             : 
     250         240 :         input_file = fopen(in_file, "r");
     251         240 :         if (!input_file) {
     252           0 :                 sprintf(in_file, "%s.in", fn);
     253           0 :                 sprintf(exp_file, "%s.exp", fn);
     254           0 :                 input_file = fopen(in_file, "rb");
     255             :         }
     256         240 :         if (!input_file) {
     257           0 :                 fprintf(stderr, "could not open %s\n", in_file);
     258           0 :                 exit(1);
     259             :         }
     260         240 :         num_rows = count_file_rows(input_file);
     261         240 :         fclose(input_file);
     262             : 
     263         240 :         input_file = fopen(exp_file, "r");
     264         240 :         if (!input_file)
     265         180 :                 strcpy(exp_file, in_file);
     266             :         else
     267          60 :                 fclose(input_file);
     268             : 
     269         240 :         input_file = fopen(sql_file, "r");
     270         240 :         assert(input_file);
     271             :         for (;;) {
     272             :                 const char *param;
     273         300 :                 size_t len = fgets_raw(line1, sizeof(line1), input_file);
     274             : 
     275         300 :                 if (len && line1[len - 1] == '\n')
     276         300 :                         line1[len - 1] = '\0';
     277         300 :                 if (len == 0 || strncmp(line1, "-- PARAM:", 9) != 0)
     278             :                         break;
     279          60 :                 param = line1 + 9;
     280          60 :                 if (strncmp(param, "table ", 6) == 0) {
     281          10 :                         param += 6;
     282          10 :                         strlcpy(table_name, param, sizeof(table_name));
     283          50 :                 } else if (strncmp(param, "hints ", 6) == 0) {
     284          20 :                         param += 6;
     285          20 :                         strlcpy(hints, param, sizeof(hints));
     286          30 :                 } else if (strncmp(param, "colin ", 6) == 0) {
     287          10 :                         param += 6;
     288          10 :                         strlcpy(colin, param, sizeof(colin));
     289          20 :                 } else if (strncmp(param, "colout ", 7) == 0) {
     290           0 :                         param += 7;
     291           0 :                         strlcpy(colout, param, sizeof(colout));
     292          20 :                 } else if (strncmp(param, "warning ", 8) == 0) {
     293          20 :                         param += 8;
     294          20 :                         expected_warning = atoi(param);
     295             :                 } else {
     296           0 :                         fprintf(stderr, "invalid parameter: %s\n", param);
     297           0 :                         exit(1);
     298             :                 }
     299             :         }
     300         240 :         fclose(input_file);
     301             : 
     302         240 :         dberrhandle(ignore_err_handler);
     303         240 :         dbmsghandle(ignore_msg_handler);
     304             : 
     305         240 :         printf("Creating table '%s'\n", table_name);
     306         240 :         got_error = false;
     307         240 :         sql_cmd(dbproc);
     308         240 :         dbsqlexec(dbproc);
     309         740 :         while (dbresults(dbproc) != NO_MORE_RESULTS)
     310         260 :                 continue;
     311             : 
     312         240 :         dberrhandle(t0016_syb_err_handler);
     313         240 :         dbmsghandle(syb_msg_handler);
     314             : 
     315         240 :         if (got_error) {
     316          44 :                 printf("Skipping %s due to table creation failure.\n", fn);
     317          44 :                 return;
     318             :         }
     319             : 
     320         196 :         ret = sql_cmd(dbproc);
     321         196 :         printf("return from dbcmd = %d\n", ret);
     322             : 
     323         196 :         ret = dbsqlexec(dbproc);
     324         196 :         printf("return from dbsqlexec = %d\n", ret);
     325             : 
     326         196 :         if (dbresults(dbproc) != FAIL) {
     327         196 :                 num_cols = dbnumcols(dbproc);
     328         196 :                 printf("Number of columns = %d\n", num_cols);
     329             : 
     330         392 :                 while (dbnextrow(dbproc) != NO_MORE_ROWS)
     331           0 :                         continue;
     332             :         }
     333             : 
     334             :         /* BCP in */
     335         196 :         printf("bcp_init with in_file as '%s'\n", in_file);
     336         196 :         ret = bcp_init(dbproc, table_name, in_file, (char *) err_file, DB_IN);
     337         196 :         if (ret != SUCCEED)
     338           0 :                 failure("bcp_init failed\n");
     339             : 
     340         196 :         if (hints[0])
     341          12 :                 bcp_options(dbproc, BCPHINTS, (BYTE *) hints, (int) strlen(hints));
     342             : 
     343         196 :         printf("return from bcp_init = %d\n", ret);
     344             : 
     345             :         /* Check for a format file; if not, then format all columns as SYBCHAR tab/newline delimit */
     346         196 :         if (fmt_file_exists && SUCCEED == bcp_readfmt(dbproc, fmt_file))
     347          20 :                 printf("Loaded %s\n", fmt_file);
     348             :         else
     349         176 :                 format_columns(num_cols, colin);
     350             : 
     351         196 :         ret = bcp_exec(dbproc, &rows_copied);
     352         196 :         if (ret != SUCCEED || rows_copied != num_rows)
     353           0 :                 failure("bcp_exec failed\n");
     354             : 
     355         196 :         printf("%d rows copied in\n", rows_copied);
     356             : 
     357         196 :         if (expected_warning && !num_warnings)
     358           0 :                 failure("expected warning %d not received\n", expected_warning);
     359         196 :         expected_warning = 0;
     360             : 
     361             :         /* BCP out */
     362             : 
     363         196 :         rows_copied = 0;
     364         196 :         ret = bcp_init(dbproc, table_name, (char *) out_file, (char *) err_file, DB_OUT);
     365         196 :         if (ret != SUCCEED)
     366           0 :                 failure("bcp_init failed\n");
     367             : 
     368         196 :         printf("select\n");
     369         196 :         sql_cmd(dbproc);
     370         196 :         dbsqlexec(dbproc);
     371             : 
     372         196 :         if (dbresults(dbproc) != FAIL) {
     373         196 :                 num_cols = dbnumcols(dbproc);
     374         422 :                 while (dbnextrow(dbproc) != NO_MORE_ROWS)
     375          30 :                         continue;
     376             :         }
     377             : 
     378         196 :         if (fmt_file_exists && SUCCEED == bcp_readfmt(dbproc, fmt_file))
     379          20 :                 printf("Loaded %s\n", fmt_file);
     380             :         else
     381         176 :                 format_columns(num_cols, colout);
     382             : 
     383         196 :         ret = bcp_exec(dbproc, &rows_copied);
     384         196 :         if (ret != SUCCEED || rows_copied != num_rows)
     385           0 :                 failure("bcp_exec failed\n");
     386             : 
     387         196 :         printf("%d rows copied out\n", rows_copied);
     388             : 
     389         196 :         printf("Dropping table '%s'\n", table_name);
     390         196 :         sql_cmd(dbproc);
     391         196 :         dbsqlexec(dbproc);
     392         592 :         while (dbresults(dbproc) != NO_MORE_RESULTS)
     393         200 :                 continue;
     394             : 
     395         196 :         if (failed)
     396             :                 return;
     397             : 
     398         196 :         if (compare_files(exp_file, out_file))
     399         196 :                 printf("Input and output files are equal\n");
     400             :         else
     401           0 :                 failed = true;
     402             : }
     403             : 
     404             : static size_t
     405        2434 : fgets_raw(char *s, int len, FILE *f)
     406             : {
     407        2434 :         char *p = s;
     408             : 
     409      784114 :         while (len > 1) {
     410      781680 :                 int c = getc(f);
     411             : 
     412      781680 :                 if (c == EOF) {
     413         632 :                         if (ferror(f))
     414             :                                 return 0;
     415             :                         break;
     416             :                 }
     417      781048 :                 *p++ = c;
     418      781048 :                 --len;
     419      781048 :                 if (c == '\n')
     420             :                         break;
     421             :         }
     422        2434 :         if (len > 0)
     423        2434 :                 *p = 0;
     424        2434 :         return p - s;
     425             : }
     426             : 
     427             : static bool
     428         196 : compare_files(const char *fn1, const char *fn2)
     429             : {
     430         196 :         bool equal = true;
     431             :         FILE *f1, *f2;
     432             :         size_t s1, s2;
     433             : 
     434             :         /* check input and output should be the same */
     435         196 :         f1 = fopen(fn1, "r");
     436         196 :         f2 = fopen(fn2, "r");
     437         196 :         if (f1 != NULL && f2 != NULL) {
     438             :                 int line = 1;
     439             : 
     440         466 :                 for (;; ++line) {
     441        1128 :                         s1 = fgets_raw(line1, sizeof(line1), f1);
     442         662 :                         s2 = fgets_raw(line2, sizeof(line2), f2);
     443             : 
     444             :                         /* EOF or error of one */
     445         662 :                         if (!!s1 != !!s2) {
     446           0 :                                 equal = false;
     447           0 :                                 failure("error reading a file or EOF of a file\n");
     448           0 :                                 break;
     449             :                         }
     450             : 
     451             :                         /* EOF or error of both */
     452         662 :                         if (!s1) {
     453         196 :                                 if (feof(f1) && feof(f2))
     454             :                                         break;
     455           0 :                                 equal = false;
     456           0 :                                 failure("error reading a file\n");
     457           0 :                                 break;
     458             :                         }
     459             : 
     460         466 :                         if (s1 != s2 || memcmp(line1, line2, s1) != 0) {
     461           0 :                                 equal = false;
     462           0 :                                 failure("File different at line %d\n"
     463             :                                         " %s: %s"
     464           0 :                                         " output: %s%s", line, fn1[strlen(fn1) - 1] == 'p' ? "expect" : " input", line1, line2,
     465             :                                         /* don't double-newline if expected output had one already */
     466           0 :                                         (line2[0] && line2[strlen(line2) - 1] == '\n') ? "" : "\n");
     467             : 
     468             :                         }
     469             :                 }
     470             :         } else {
     471           0 :                 equal = false;
     472           0 :                 failure("error opening files\n");
     473             :         }
     474         196 :         if (f1)
     475         196 :                 fclose(f1);
     476         196 :         if (f2)
     477         196 :                 fclose(f2);
     478             : 
     479         196 :         return equal;
     480             : }
     481             : 
     482             : static unsigned
     483         240 : count_file_rows(FILE *f)
     484             : {
     485             :         size_t s;
     486         240 :         unsigned rows = 1;
     487         240 :         char last = '\n';
     488             : 
     489         240 :         assert(f);
     490             : 
     491         810 :         while ((s = fgets_raw(line1, sizeof(line1), f)) != 0) {
     492         570 :                 last = line1[s - 1];
     493         570 :                 if (last == '\n')
     494         570 :                         ++rows;
     495             :         }
     496         240 :         if (last == '\n')
     497         240 :                 --rows;
     498         240 :         assert(!ferror(f));
     499         240 :         return rows;
     500             : }

Generated by: LCOV version 1.13