Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief tpimport()/tpexport() function tests - client
0003  *
0004  * @file atmiclt56_string.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 #include <math.h>
0039 
0040 #include <atmi.h>
0041 #include <atmi_int.h>
0042 #include <ndebug.h>
0043 #include <ndrstandard.h>
0044 #include <nstopwatch.h>
0045 #include <fcntl.h>
0046 #include <unistd.h>
0047 #include <nstdutil.h>
0048 #include "test56.h"
0049 #include <exbase64.h>
0050 /*---------------------------Externs------------------------------------*/
0051 /*---------------------------Macros-------------------------------------*/
0052 /*---------------------------Enums--------------------------------------*/
0053 /*---------------------------Typedefs-----------------------------------*/
0054 /*---------------------------Globals------------------------------------*/
0055 /*---------------------------Statics------------------------------------*/
0056 /*---------------------------Prototypes---------------------------------*/
0057 
0058 /**
0059  * test case to import/export STRING
0060  * @return EXSUCCEED/EXFAIL
0061  */
0062 expublic int test_impexp_string()
0063 {
0064     int ret = EXSUCCEED;
0065     int i;
0066     long rsplen,olen;
0067     char *obuf;
0068     char json_string_in_b64[CARR_BUFFSIZE_B64+1];
0069     char json_string_out_b64[CARR_BUFFSIZE_B64+1];
0070     size_t len_b64;
0071     char *json_string_in = 
0072         "{"
0073             "\"buftype\":\"STRING\","
0074             "\"version\":1,"
0075             "\"data\":\"HELLO WORLD\""
0076         "}";
0077     char json_string_out[1024];
0078     char *istrtemp=NULL;
0079     size_t bufsz=0;
0080 
0081     NDRX_LOG(log_info, "JSON STRING IN: [%s]", json_string_in);
0082 
0083     if (NULL==(obuf = tpalloc("STRING", NULL, 256)))
0084     {
0085         NDRX_LOG(log_error, "TESTERROR: failed to alloc STRING buffer!");
0086         EXFAIL_OUT(ret);
0087     }
0088 
0089     for (i=0; i<10000; i++)
0090     {
0091         rsplen=0L;
0092         if ( EXFAIL == tpimport(json_string_in, 
0093                                 (long)strlen(json_string_in), 
0094                                 (char **)&obuf, 
0095                                 &rsplen, 
0096                                 0L) )
0097         {
0098             NDRX_LOG(log_error, "TESTERROR: Failed to import JSON STRING!!!!");
0099             EXFAIL_OUT(ret);
0100         }
0101         NDRX_LOG(log_debug, 
0102                  "JSON STRING imported. Return obuf=[%s] rsplen=[%ld]", 
0103                  obuf, rsplen);
0104 
0105         /* Check if imported sting match with original string */
0106         if ( 0!=strcmp("HELLO WORLD", obuf) )
0107         {
0108             NDRX_LOG(log_error, 
0109                  "TESTERROR: imported string not equal to obuf string ");
0110             EXFAIL_OUT(ret);
0111         }
0112 
0113         memset(json_string_out, 0, sizeof(json_string_out));
0114         olen = sizeof(json_string_out);
0115         if ( EXFAIL == tpexport(obuf, 
0116                                 (long)strlen(obuf), 
0117                                 json_string_out, 
0118                                 &olen, 
0119                                 0L) )
0120         {
0121             NDRX_LOG(log_error, "TESTERROR: Failed to export JSON STRING!!!!");
0122             EXFAIL_OUT(ret);
0123         }
0124         NDRX_LOG(log_debug, 
0125                  "JSON STRING exported. Return json_string_out=[%s] olen=[%ld]", 
0126                  json_string_out, olen);
0127 
0128         if (0!=strcmp(json_string_in, json_string_out))
0129         {
0130             NDRX_LOG(log_error, 
0131                  "TESTERROR: Exported JSON not equal to incoming string ");
0132             EXFAIL_OUT(ret);
0133         }
0134     }
0135 
0136     /* testing with base64 flag*/
0137     NDRX_LOG(log_debug, "convert to b64");
0138     len_b64 = sizeof(json_string_in_b64);
0139     if (NULL==ndrx_base64_encode((unsigned char *)json_string_in, strlen(json_string_in), 
0140             &len_b64, json_string_in_b64))
0141     {
0142             NDRX_LOG(log_error, "Failed to convert to b64!");
0143             EXFAIL_OUT(ret);
0144     }
0145 
0146     for (i=0; i<10000; i++)
0147     {
0148         rsplen=0L;
0149         if ( EXFAIL == tpimport(json_string_in_b64, 
0150                                 (long)strlen(json_string_in_b64), 
0151                                 (char **)&obuf, 
0152                                 &rsplen, 
0153                                 TPEX_STRING) )
0154         {
0155             NDRX_LOG(log_error, "TESTERROR: Failed to import JSON STRING!!!!");
0156             EXFAIL_OUT(ret);
0157         }
0158         NDRX_LOG(log_debug, 
0159                  "JSON STRING imported. Return obuf=[%s] rsplen=[%ld]", 
0160                  obuf, rsplen);
0161 
0162         /* Check if imported sting match with original string */
0163         if ( 0!=strcmp("HELLO WORLD", obuf) )
0164         {
0165             NDRX_LOG(log_error, 
0166                  "TESTERROR: imported string not equal to obuf string ");
0167             EXFAIL_OUT(ret);
0168         }
0169 
0170         memset(json_string_out_b64, 0, sizeof(json_string_out_b64));
0171         olen = sizeof(json_string_out_b64);
0172         if ( EXFAIL == tpexport(obuf, 
0173                                 (long)strlen(obuf), 
0174                                 json_string_out_b64, 
0175                                 &olen, 
0176                                 TPEX_STRING) )
0177         {
0178             NDRX_LOG(log_error, "TESTERROR: Failed to export JSON STRING!!!!");
0179             EXFAIL_OUT(ret);
0180         }
0181         NDRX_LOG(log_debug, 
0182                  "JSON STRING exported. Return json_string_out=[%s] olen=[%ld]", 
0183                  json_string_out_b64, olen);
0184 
0185         /* decode from b64 to check returned data */
0186         bufsz = strlen(json_string_out_b64);
0187         if (NULL==(istrtemp = NDRX_MALLOC(bufsz)))
0188         {
0189             NDRX_LOG(log_error, "Failed to allocate %ld bytes", strlen(json_string_out_b64));
0190             EXFAIL_OUT(ret);
0191         }
0192 
0193         if (NULL==ndrx_base64_decode(json_string_out_b64, strlen(json_string_out_b64), &bufsz, istrtemp))
0194         {
0195             NDRX_LOG(log_error, "Failed to decode CARRAY");
0196             EXFAIL_OUT(ret);
0197         }
0198         istrtemp[bufsz]=0;
0199 
0200 
0201         
0202         if (0!=strcmp(json_string_in, istrtemp))
0203         {
0204             NDRX_LOG(log_error, "TESTERROR: \n"
0205                 " in = [%s]\n"
0206                 "out = [%s]",
0207                 json_string_in_b64,
0208                 json_string_out_b64);
0209 
0210             NDRX_LOG(log_error, "TESTERROR: \n"
0211                 " in = [%s]\n"
0212                 "out = [%s]",
0213                 json_string_in,
0214                 istrtemp);
0215             
0216             NDRX_LOG(log_error, 
0217                  "TESTERROR: Exported JSON not equal to incoming string ");
0218             EXFAIL_OUT(ret);
0219         }
0220         if (NULL!=istrtemp)
0221         {
0222             NDRX_FREE(istrtemp);
0223             istrtemp=NULL;
0224         }
0225     }
0226 
0227 out:
0228 
0229     if (NULL!=istrtemp)
0230     {
0231         NDRX_FREE(istrtemp);
0232         istrtemp=NULL;
0233     }
0234 
0235     return ret;
0236 }
0237 /* vim: set ts=4 sw=4 et smartindent: */