Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief tpimport()/tpexport() function tests - client
0003  *
0004  * @file atmiclt56_carray.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 <ubfutil.h>
0049 #include <exbase64.h>
0050 #include "test56.h"
0051 /*---------------------------Externs------------------------------------*/
0052 /*---------------------------Macros-------------------------------------*/
0053 /*---------------------------Enums--------------------------------------*/
0054 /*---------------------------Typedefs-----------------------------------*/
0055 /*---------------------------Globals------------------------------------*/
0056 /*---------------------------Statics------------------------------------*/
0057 /*---------------------------Prototypes---------------------------------*/
0058 
0059 /**
0060  * test case to import/export STRING
0061  * @return EXSUCCEED/EXFAIL
0062  */
0063 int test_impexp_carray(void)
0064 {
0065     int ret = EXSUCCEED;
0066     int i;
0067     long rsplen,olen,ilen;
0068     char *obuf;
0069     char buf_bin[CARR_BUFFSIZE+1];
0070     char json_carray_in_b64[CARR_BUFFSIZE_B64+1];
0071     char json_carray_out_b64[CARR_BUFFSIZE_B64+1];
0072     size_t st_len, len_b64;
0073     char *json_carray_in = 
0074         "{"
0075             "\"buftype\":\"CARRAY\","
0076             "\"version\":1,"
0077             "\"data\":\"SEVMTE8gV09STEQgQ0FSUkFZ\""
0078         "}";
0079     char json_carray_out[1024];
0080     char *istrtemp=NULL;
0081     size_t bufsz = 0;
0082 
0083     NDRX_LOG(log_debug, "JSON CARRAY IN: [%s]", json_carray_in);
0084 
0085     if (NULL==(obuf = tpalloc("CARRAY", NULL, 128)))
0086     {
0087         NDRX_LOG(log_error, "TESTERROR: failed to alloc CARRAY buffer!");
0088         EXFAIL_OUT(ret);
0089     }
0090 
0091     for (i=0; i<10000; i++)
0092     {
0093         rsplen=0L;
0094         if ( EXFAIL == tpimport(json_carray_in, 
0095                                 (long)strlen(json_carray_in), 
0096                                 (char **)&obuf, 
0097                                 &rsplen, 
0098                                 0L) )
0099         {
0100             NDRX_LOG(log_error, "TESTERROR: Failed to import JSON CARRAY!!!!");
0101             EXFAIL_OUT(ret);
0102         }
0103 
0104         NDRX_DUMP(log_debug, "Imported CARRAY", obuf, rsplen);
0105 
0106         memset(json_carray_out, 0, sizeof(json_carray_out));
0107         olen = sizeof(json_carray_out);
0108         ilen = rsplen;
0109         if ( EXFAIL == tpexport(obuf, 
0110                                 ilen, 
0111                                 json_carray_out, 
0112                                 &olen, 
0113                                 0L) )
0114         {
0115             NDRX_LOG(log_error, "TESTERROR: Failed to export JSON CARRAY!!!!");
0116             EXFAIL_OUT(ret);
0117         }
0118 
0119         NDRX_LOG(log_debug, 
0120                  "CARRAY exported. Return json_carray_out=[%s] olen=[%ld]", 
0121                  json_carray_out, olen);
0122 
0123         if (0!=strcmp(json_carray_in, json_carray_out))
0124         {
0125             
0126             NDRX_LOG(log_error, 
0127                  "TESTERROR: Exported JSON not equal to incoming carray");
0128             EXFAIL_OUT(ret);
0129         }
0130     }
0131 
0132     /* testing with base64 flag*/
0133     NDRX_LOG(log_debug, "convert to b64");
0134     len_b64 = sizeof(json_carray_in_b64);
0135     if (NULL==ndrx_base64_encode((unsigned char *)json_carray_in, strlen(json_carray_in), 
0136             &len_b64, json_carray_in_b64))
0137     {
0138             NDRX_LOG(log_error, "Failed to convert to b64!");
0139             EXFAIL_OUT(ret);
0140     }
0141     
0142     for (i=0; i<10000; i++)
0143     {
0144         rsplen=0L;
0145         if ( EXFAIL == tpimport(json_carray_in_b64, 
0146                                 (long)len_b64, 
0147                                 (char **)&obuf, 
0148                                 &rsplen, 
0149                                 TPEX_STRING) )
0150         {
0151             NDRX_LOG(log_error, "TESTERROR: Failed to import JSON CARRAY!!!!");
0152             EXFAIL_OUT(ret);
0153         }
0154 
0155         NDRX_DUMP(log_debug, "Imported CARRAY [%s][%ld]", obuf, rsplen);
0156 
0157         memset(json_carray_out_b64, 0, sizeof(json_carray_out_b64));
0158         olen = sizeof(json_carray_out_b64);
0159         ilen = rsplen;
0160         if ( EXFAIL == tpexport(obuf, 
0161                                 ilen, 
0162                                 json_carray_out_b64, 
0163                                 &olen, 
0164                                 TPEX_STRING) )
0165         {
0166             NDRX_LOG(log_error, "TESTERROR: Failed to export JSON CARRAY!!!!");
0167             EXFAIL_OUT(ret);
0168         }
0169 
0170         NDRX_LOG(log_debug, 
0171                  "CARRAY exported. Return json_carray_out_b64=[%s] olen=[%ld]", 
0172                  json_carray_out_b64, olen);
0173 
0174         /* decode from b64 to check returned data */
0175         bufsz = strlen(json_carray_out_b64);
0176         if (NULL==(istrtemp = NDRX_MALLOC(bufsz)))
0177         {
0178             NDRX_LOG(log_error, "Failed to allocate %ld bytes", strlen(json_carray_out_b64));
0179             EXFAIL_OUT(ret);
0180         }
0181 
0182         if (NULL==ndrx_base64_decode(json_carray_out_b64, strlen(json_carray_out_b64), 
0183                 &bufsz, istrtemp))
0184         {
0185             NDRX_LOG(log_error, "Failed to decode CARRAY");
0186             EXFAIL_OUT(ret);
0187         }
0188         istrtemp[bufsz]=0;
0189 
0190         if (0!=strcmp(json_carray_in, istrtemp))
0191         {
0192             NDRX_LOG(log_error, 
0193                 "TESTERROR: \n"
0194                 "json_carray_in=[%s]\n"
0195                 "      istrtemp=[%s]", 
0196                 json_carray_in, istrtemp);
0197             NDRX_LOG(log_error, 
0198                  "TESTERROR: Exported JSON not equal to incoming carray");
0199             EXFAIL_OUT(ret);
0200         }
0201         if (NULL!=istrtemp)
0202         {
0203             NDRX_FREE(istrtemp);
0204             istrtemp=NULL;
0205         }
0206     }
0207 
0208 out:
0209 
0210     if (NULL!=istrtemp)
0211     {
0212         NDRX_FREE(istrtemp);
0213         istrtemp=NULL;
0214     }
0215 
0216     return ret;
0217 }
0218 /* vim: set ts=4 sw=4 et smartindent: */