LCOV - code coverage report
Current view: top level - src/ctlib/unittests - long_binary.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 54 58 93.1 %
Date: 2025-07-02 09:40:27 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /* Test we can insert long binary into database.
       2             :  */
       3             : #include "common.h"
       4             : 
       5             : #include <freetds/macros.h>
       6             : 
       7             : static const CS_INT unused = CS_UNUSED, nullterm = CS_NULLTERM;
       8             : static CS_INT result_len = -1;
       9             : 
      10             : static CS_RETCODE
      11           0 : csmsg_callback(CS_CONTEXT *ctx TDS_UNUSED, CS_CLIENTMSG * emsgp)
      12             : {
      13           0 :         printf("message from csmsg_callback(): %s\n", emsgp->msgstring);
      14           0 :         return CS_SUCCEED;
      15             : }
      16             : 
      17             : static int
      18          54 : fetch_results(CS_COMMAND * command)
      19             : {
      20             :         CS_INT result_type, int_result, copied, rows_read;
      21          54 :         CS_SMALLINT ind = 0;
      22             :         CS_DATAFMT datafmt;
      23          54 :         int result = 0;
      24             : 
      25          54 :         memset(&datafmt, 0, sizeof(datafmt));
      26          54 :         datafmt.datatype = CS_INT_TYPE;
      27          54 :         datafmt.count = 1;
      28             : 
      29          54 :         check_call(ct_results, (command, &result_type));
      30             :         do {
      31         142 :                 if (result_type == CS_ROW_RESULT) {
      32           6 :                         check_call(ct_bind, (command, 1, &datafmt, &int_result, &copied, &ind));
      33           6 :                         check_call(ct_fetch, (command, CS_UNUSED, CS_UNUSED, CS_UNUSED, &rows_read));
      34           6 :                         printf("received %d bytes\n", (int) int_result);
      35           6 :                         result_len = int_result;
      36             :                 }
      37         142 :                 if (result_type == CS_CMD_FAIL) {
      38           4 :                         result = 1;
      39             :                 }
      40         142 :         } while (ct_results(command, &result_type) == CS_SUCCEED);
      41          54 :         return result;
      42             : }
      43             : 
      44             : static int
      45          48 : execute_sql(CS_COMMAND * command, const char *sql)
      46             : {
      47          48 :         printf("executing sql: %s\n", sql);
      48          48 :         check_call(ct_command, (command, CS_LANG_CMD, sql, nullterm, unused));
      49          48 :         check_call(ct_send, (command));
      50          48 :         return fetch_results(command);
      51             : }
      52             : 
      53          10 : TEST_MAIN()
      54             : {
      55          10 :         int verbose = 0;
      56             :         CS_COMMAND *command;
      57             :         CS_CONNECTION *connection;
      58             :         CS_CONTEXT *context;
      59             :         unsigned char buffer[65536];
      60          10 :         CS_INT buffer_len = 8192;
      61          10 :         CS_SMALLINT ind = 0;
      62             :         CS_DATAFMT datafmt;
      63             :         CS_INT ret;
      64             :         int i;
      65             : 
      66          10 :         printf("-- begin --\n");
      67             : 
      68          10 :         check_call(try_ctlogin, (&context, &connection, &command, verbose));
      69          10 :         check_call(cs_config, (context, CS_SET, CS_MESSAGE_CB, (CS_VOID *) csmsg_callback, unused, 0));
      70             : 
      71          10 :         execute_sql(command, "if object_id('mps_table') is not null drop table mps_table");
      72          10 :         execute_sql(command, "if object_id('mps_rpc') is not null drop procedure mps_rpc");
      73             :         /* if this query fails probably we are using wrong database vendor or version */
      74          10 :         ret = execute_sql(command, "create procedure mps_rpc (@varbinary_param varbinary(max)) as "
      75             :                     "insert mps_table values (@varbinary_param) " "select len(varbinary_data) from mps_table");
      76          10 :         if (ret != 0) {
      77           4 :                 try_ctlogout(context, connection, command, verbose);
      78           4 :                 return 0;
      79             :         }
      80           6 :         execute_sql(command, "create table mps_table (varbinary_data varbinary(max))");
      81             : 
      82           6 :         if (argc > 1)
      83           0 :                 buffer_len = atoi(argv[1]);
      84           6 :         if (buffer_len < 0 || buffer_len > sizeof(buffer))
      85             :                 return 1;
      86             : 
      87           6 :         printf("sending %d bytes\n", buffer_len);
      88             : 
      89       49158 :         for (i = 0; i < buffer_len; i++)
      90       49152 :                 buffer[i] = (rand() % 16);
      91             : 
      92           6 :         memset(&datafmt, 0, sizeof(datafmt));
      93           6 :         strcpy(datafmt.name, "@varbinary_param");
      94           6 :         datafmt.namelen = nullterm;
      95           6 :         datafmt.datatype = CS_IMAGE_TYPE;
      96           6 :         datafmt.status = CS_INPUTVALUE;
      97             : 
      98           6 :         check_call(ct_command, (command, CS_RPC_CMD, "mps_rpc", nullterm, unused));
      99           6 :         check_call(ct_setparam, (command, &datafmt, buffer, &buffer_len, &ind));
     100           6 :         check_call(ct_send, (command));
     101             : 
     102           6 :         fetch_results(command);
     103             : 
     104           6 :         execute_sql(command, "drop table mps_table");
     105           6 :         execute_sql(command, "drop procedure mps_rpc");
     106           6 :         try_ctlogout(context, connection, command, verbose);
     107             : 
     108           6 :         printf("-- end --\n");
     109           6 :         return (result_len == buffer_len) ? 0 : 1;
     110             : }

Generated by: LCOV version 1.13