Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief tpimport()/tpexport() function tests - client
0003  *
0004  * @file atmiclt56_ubf.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 <ubf.h>
0043 #include <ndebug.h>
0044 #include <test.fd.h>
0045 #include <ndrstandard.h>
0046 #include <nstopwatch.h>
0047 #include <fcntl.h>
0048 #include <unistd.h>
0049 #include <nstdutil.h>
0050 #include <ubfutil.h>
0051 #include "test56.h"
0052 #include <exbase64.h>
0053 /*---------------------------Externs------------------------------------*/
0054 /*---------------------------Macros-------------------------------------*/
0055 /*---------------------------Enums--------------------------------------*/
0056 /*---------------------------Typedefs-----------------------------------*/
0057 /*---------------------------Globals------------------------------------*/
0058 /*---------------------------Statics------------------------------------*/
0059 /*---------------------------Prototypes---------------------------------*/
0060 
0061 /**
0062  * 
0063  * @return 
0064  */
0065 expublic int test_impexp_ubf(void)
0066 {
0067     int ret = EXSUCCEED;
0068     int i;
0069     long rsplen,olen;
0070     char *obuf;
0071     char json_ubf_in_b64[CARR_BUFFSIZE_B64+1];
0072     char json_ubf_out_b64[CARR_BUFFSIZE_B64+1];
0073     size_t len_b64;
0074     char *json_ubf_in = 
0075         "{"
0076             "\"buftype\":\"UBF\","
0077             "\"version\":1,"
0078             "\"data\":"
0079             "{"
0080                 "\"T_SHORT_FLD\":1765,"
0081                 "\"T_LONG_FLD\":[3333111,2],"
0082                 "\"T_CHAR_FLD\":\"A\","
0083                 "\"T_FLOAT_FLD\":1,"
0084                 "\"T_DOUBLE_FLD\":[1111.220000,333,444],"
0085                 "\"T_STRING_FLD\":\"HELLO WORLD\","
0086                 "\"T_CARRAY_FLD\":\"AAECA0hFTExPIEJJTkFSWQQFAA==\""
0087             "}"
0088         "}";
0089     char json_ubf_out[1024];
0090     char *istrtemp=NULL;
0091     size_t bufsz;
0092     long blen;
0093 
0094     NDRX_LOG(log_info, "JSON UBF IN: [%s]", json_ubf_in);
0095 
0096     if (NULL == (obuf = tpalloc("UBF", NULL, NDRX_MSGSIZEMAX)))
0097     {
0098         NDRX_LOG(log_error, "Failed to allocate UBFH %ld bytes: %s",
0099                  NDRX_MSGSIZEMAX, tpstrerror(tperrno));
0100         EXFAIL_OUT(ret);
0101     }
0102 
0103     for (i=0; i<10000; i++)
0104     {
0105         rsplen=0L;
0106         if ( EXFAIL == tpimport(json_ubf_in, 
0107                                 (long)strlen(json_ubf_in), 
0108                                 (char **)&obuf, 
0109                                 &rsplen, 
0110                                 0L) )
0111         {
0112             NDRX_LOG(log_error, "TESTERROR: Failed to import JSON UBF!!!!");
0113             EXFAIL_OUT(ret);
0114         }
0115         ndrx_debug_dump_UBF(log_debug, "JSON UBF imported. Return obuf", (UBFH *)obuf);
0116 
0117         if (EXFAIL==(blen=tptypes(obuf, NULL, NULL)))
0118         {
0119             NDRX_LOG(log_error, "TESTERROR: tptypes failed: %s", tpstrerror(tperrno));
0120             EXFAIL_OUT(ret);
0121         }
0122 
0123         if (rsplen!=blen)
0124         {
0125             NDRX_LOG(log_error, "TESTERROR: buffer imported len=%ld tptypes=%ld",
0126                 rsplen, blen);
0127             EXFAIL_OUT(ret);
0128         }
0129 
0130         memset(json_ubf_out, 0, sizeof(json_ubf_out));
0131         olen = sizeof(json_ubf_out);
0132         if ( EXFAIL == tpexport(obuf, 
0133                                 (long)strlen(obuf), 
0134                                 json_ubf_out, 
0135                                 &olen, 
0136                                 0L) )
0137         {
0138             NDRX_LOG(log_error, "TESTERROR: Failed to export JSON UBF!!!!");
0139             EXFAIL_OUT(ret);
0140         }
0141         NDRX_LOG(log_debug, 
0142                  "JSON UBF exported. Return json_ubf_out=[%s] olen=[%ld]", 
0143                  json_ubf_out, olen);
0144 
0145         if (0!=strcmp(json_ubf_in, json_ubf_out))
0146         {
0147             NDRX_LOG(log_error, 
0148                  "TESTERROR: Exported UBF not equal to incoming string ");
0149             EXFAIL_OUT(ret);
0150         }
0151     }
0152 
0153     /* testing with base64 flag*/
0154     NDRX_LOG(log_debug, "convert to b64");
0155     
0156     len_b64 = sizeof(json_ubf_in_b64);
0157     if (NULL==ndrx_base64_encode((unsigned char *)json_ubf_in, strlen(json_ubf_in), 
0158             &len_b64, json_ubf_in_b64))
0159     {
0160             NDRX_LOG(log_error, "Failed to convert to b64!");
0161             EXFAIL_OUT(ret);
0162     }
0163 
0164     for (i=0; i<10000; i++)
0165     {
0166         rsplen=0L;
0167         if ( EXFAIL == tpimport(json_ubf_in_b64, 
0168                                 (long)strlen(json_ubf_in_b64), 
0169                                 (char **)&obuf, 
0170                                 &rsplen, 
0171                                 TPEX_STRING) )
0172         {
0173             NDRX_LOG(log_error, "TESTERROR: Failed to import JSON UBF!!!!");
0174             EXFAIL_OUT(ret);
0175         }
0176         ndrx_debug_dump_UBF(log_debug, "JSON UBF imported. Return obuf", (UBFH *)obuf);
0177 
0178         /* TODO Check imported field values */
0179 
0180         memset(json_ubf_out_b64, 0, sizeof(json_ubf_out_b64));
0181         olen = sizeof(json_ubf_out_b64);
0182         if ( EXFAIL == tpexport(obuf, 
0183                                 (long)strlen(obuf), 
0184                                 json_ubf_out_b64, 
0185                                 &olen, 
0186                                 TPEX_STRING) )
0187         {
0188             NDRX_LOG(log_error, "TESTERROR: Failed to export JSON UBF!!!!");
0189             EXFAIL_OUT(ret);
0190         }
0191         NDRX_LOG(log_debug, 
0192                  "JSON UBF exported. Return json_ubf_out=[%s] olen=[%ld]", 
0193                  json_ubf_out_b64, olen);
0194 
0195         /* decode from b64 to check returned data */
0196         bufsz = strlen(json_ubf_out_b64);
0197         if (NULL==(istrtemp = NDRX_MALLOC(bufsz)))
0198         {
0199             NDRX_LOG(log_error, "Failed to allocate %ld bytes", strlen(json_ubf_out_b64));
0200             EXFAIL_OUT(ret);
0201         }
0202 
0203         if (NULL==ndrx_base64_decode(json_ubf_out_b64, strlen(json_ubf_out_b64), &bufsz, istrtemp))
0204         {
0205             NDRX_LOG(log_error, "Failed to decode CARRAY");
0206             EXFAIL_OUT(ret);
0207         }
0208         istrtemp[bufsz]=0;
0209 
0210         if (0!=strcmp(json_ubf_in, istrtemp))
0211         {
0212             NDRX_LOG(log_error, 
0213                  "TESTERROR: Exported UBF not equal to incoming string ");
0214             EXFAIL_OUT(ret);
0215         }
0216         if (NULL!=istrtemp)
0217         {
0218             NDRX_FREE(istrtemp);
0219             istrtemp=NULL;
0220         }
0221     }
0222 
0223 out:
0224 
0225     if (NULL!=istrtemp)
0226     {
0227         NDRX_FREE(istrtemp);
0228         istrtemp=NULL;
0229     }
0230 
0231     return ret;
0232 }
0233 /* vim: set ts=4 sw=4 et smartindent: */