LCOV - code coverage report
Current view: top level - src/ctlib/unittests - t0008.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 44 76 57.9 %
Date: 2024-03-23 09:12:27 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include <config.h>
       2             : 
       3             : #include <stdio.h>
       4             : #include <cspublic.h>
       5             : #include <ctpublic.h>
       6             : #include "common.h"
       7             : 
       8             : /*
       9             :  * ct_send SQL |select name = @@servername|
      10             :  * ct_bind variable
      11             :  * ct_fetch and print results
      12             :  */
      13             : int
      14           8 : main(int argc, char **argv)
      15             : {
      16           8 :         int verbose = 1;
      17             :         CS_CONTEXT *ctx;
      18             :         CS_CONNECTION *conn;
      19             :         CS_COMMAND *cmd;
      20             :         CS_RETCODE ret;
      21             :         CS_DATAFMT srcfmt;
      22           8 :         CS_INT src = 32768;
      23             :         CS_DATAFMT dstfmt;
      24             :         CS_SMALLINT dst;
      25             : 
      26           8 :         printf("%s: Testing context callbacks\n", __FILE__);
      27           8 :         srcfmt.datatype = CS_INT_TYPE;
      28           8 :         srcfmt.maxlength = sizeof(CS_INT);
      29           8 :         srcfmt.locale = NULL;
      30             : #if 0
      31             :         dstfmt.datatype = CS_SMALLINT_TYPE;
      32             : #else
      33           8 :         dstfmt.datatype = CS_DATETIME_TYPE;
      34             : #endif
      35           8 :         dstfmt.maxlength = sizeof(CS_SMALLINT);
      36           8 :         dstfmt.locale = NULL;
      37             : 
      38             :         if (verbose) {
      39           8 :                 printf("Trying clientmsg_cb with context\n");
      40             :         }
      41           8 :         if (cs_ctx_alloc(CS_VERSION_100, &ctx) != CS_SUCCEED) {
      42           0 :                 fprintf(stderr, "cs_ctx_alloc() failed\n");
      43             :         }
      44           8 :         if (ct_init(ctx, CS_VERSION_100) != CS_SUCCEED) {
      45           0 :                 fprintf(stderr, "ct_init() failed\n");
      46             :         }
      47             : 
      48           8 :         if (ct_callback(ctx, NULL, CS_SET, CS_CLIENTMSG_CB, (CS_VOID*) clientmsg_cb)
      49             :             != CS_SUCCEED) {
      50           0 :                 fprintf(stderr, "ct_callback() failed\n");
      51           0 :                 return 1;
      52             :         }
      53           8 :         clientmsg_cb_invoked = 0;
      54           8 :         if (cs_convert(ctx, &srcfmt, &src, &dstfmt, &dst, NULL) == CS_SUCCEED) {
      55           0 :                 fprintf(stderr, "cs_convert() succeeded when failure was expected\n");
      56           0 :                 return 1;
      57             :         }
      58           8 :         if (clientmsg_cb_invoked != 0) {
      59           0 :                 fprintf(stderr, "clientmsg_cb was invoked!\n");
      60           0 :                 return 1;
      61             :         }
      62             : 
      63             :         if (verbose) {
      64           8 :                 printf("Trying cslibmsg_cb\n");
      65             :         }
      66           8 :         if (cs_config(ctx, CS_SET, CS_MESSAGE_CB, (CS_VOID*) cslibmsg_cb, CS_UNUSED, NULL)
      67             :             != CS_SUCCEED) {
      68           0 :                 fprintf(stderr, "cs_config() failed\n");
      69           0 :                 return 1;
      70             :         }
      71           8 :         cslibmsg_cb_invoked = 0;
      72           8 :         if (cs_convert(ctx, &srcfmt, &src, &dstfmt, &dst, NULL) == CS_SUCCEED) {
      73           0 :                 fprintf(stderr, "cs_convert() succeeded when failure was expected\n");
      74           0 :                 return 1;
      75             :         }
      76           8 :         if (cslibmsg_cb_invoked == 0) {
      77           0 :                 fprintf(stderr, "cslibmsg_cb was not invoked!\n");
      78           0 :                 return 1;
      79             :         }
      80             : 
      81           8 :         if (ct_exit(ctx, CS_UNUSED) != CS_SUCCEED) {
      82           0 :                 fprintf(stderr, "ct_exit() failed\n");
      83             :         }
      84           8 :         if (cs_ctx_drop(ctx) != CS_SUCCEED) {
      85           0 :                 fprintf(stderr, "cx_ctx_drop() failed\n");
      86             :         }
      87             : 
      88             :         if (verbose) {
      89           8 :                 printf("Trying login\n");
      90             :         }
      91           8 :         ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
      92           8 :         if (ret != CS_SUCCEED) {
      93           0 :                 fprintf(stderr, "Login failed\n");
      94           0 :                 return 1;
      95             :         }
      96             : 
      97             :         if (verbose) {
      98           8 :                 printf("Trying clientmsg_cb with connection\n");
      99             :         }
     100           8 :         ret = ct_callback(NULL, conn, CS_SET, CS_CLIENTMSG_CB, (CS_VOID *) clientmsg_cb);
     101           8 :         if (ret != CS_SUCCEED) {
     102           0 :                 fprintf(stderr, "ct_callback() failed\n");
     103           0 :                 return 1;
     104             :         }
     105           8 :         clientmsg_cb_invoked = 0;
     106           8 :         ret = run_command(cmd, ".");
     107           8 :         if (ret != CS_SUCCEED) {
     108           0 :                 fprintf(stderr, "run_command() failed\n");
     109           0 :                 return 1;
     110             :         }
     111           8 :         if (clientmsg_cb_invoked) {
     112           0 :                 fprintf(stderr, "clientmsg_cb was invoked!\n");
     113           0 :                 return 1;
     114             :         }
     115             : 
     116             :         if (verbose) {
     117           8 :                 printf("Trying servermsg_cb with connection\n");
     118             :         }
     119           8 :         ret = ct_callback(NULL, conn, CS_SET, CS_SERVERMSG_CB, (CS_VOID *) servermsg_cb);
     120           8 :         if (ret != CS_SUCCEED) {
     121           0 :                 fprintf(stderr, "ct_callback() failed\n");
     122           0 :                 return 1;
     123             :         }
     124           8 :         servermsg_cb_invoked = 0;
     125             : #if 0
     126             :         ret = run_command(cmd, "raiserror 99999 'This is a test'");
     127             :         ret = run_command(cmd, "raiserror('This is a test', 17, 1)");
     128             : #else
     129           8 :         ret = run_command(cmd, "print 'This is a test'");
     130             : #endif
     131           8 :         if (ret != CS_SUCCEED) {
     132           0 :                 fprintf(stderr, "run_command() failed\n");
     133           0 :                 return 1;
     134             :         }
     135           8 :         if (servermsg_cb_invoked == 0) {
     136           0 :                 fprintf(stderr, "servermsg_cb was not invoked!\n");
     137           0 :                 return 1;
     138             :         }
     139             : 
     140             :         if (verbose) {
     141           8 :                 printf("Trying logout\n");
     142             :         }
     143           8 :         ret = try_ctlogout(ctx, conn, cmd, verbose);
     144           8 :         if (ret != CS_SUCCEED) {
     145           0 :                 fprintf(stderr, "Logout failed\n");
     146           0 :                 return 1;
     147             :         }
     148             : 
     149             :         return 0;
     150             : }

Generated by: LCOV version 1.13