Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief tpimport()/tpexport() function tests - client
0003  *
0004  * @file atmiclt56_json.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 <exbase64.h>
0052 #include "test56.h"
0053 /*---------------------------Externs------------------------------------*/
0054 /*---------------------------Macros-------------------------------------*/
0055 /*---------------------------Enums--------------------------------------*/
0056 /*---------------------------Typedefs-----------------------------------*/
0057 /*---------------------------Globals------------------------------------*/
0058 /*---------------------------Statics------------------------------------*/
0059 /*---------------------------Prototypes---------------------------------*/
0060 
0061 expublic int test_impexp_json()
0062 {
0063     int ret = EXSUCCEED;
0064     char type[16+1]={EXEOS};
0065     char subtype[XATMI_SUBTYPE_LEN]={EXEOS};
0066     long rsplen, olen;
0067     char json_json_in_b64[CARR_BUFFSIZE_B64+1];
0068     char json_json_out_b64[CARR_BUFFSIZE_B64+1];
0069     size_t len_b64;
0070     int i;
0071     char *obuf=NULL;
0072     char *data_test = 
0073                 "{\"T_SHORT_FLD\":1765,"
0074                 "\"T_LONG_FLD\":[3333111,2],"
0075                 "\"T_CHAR_FLD\":\"A\","
0076                 "\"T_FLOAT_FLD\":1,"
0077                 "\"T_DOUBLE_FLD\":[1111.220000,333,444],"
0078                 "\"T_STRING_FLD\":\"HELLO WORLD\","
0079                 "\"T_CARRAY_FLD\":\"AAECA0hFTExPIEJJTkFSWQQFAA==\"}";
0080 
0081     char *json_json_in = 
0082         "{"
0083             "\"buftype\":\"JSON\","
0084             "\"version\":1,"
0085             "\"data\":"
0086                 "{\"T_SHORT_FLD\":1765,"
0087                 "\"T_LONG_FLD\":[3333111,2],"
0088                 "\"T_CHAR_FLD\":\"A\","
0089                 "\"T_FLOAT_FLD\":1,"
0090                 "\"T_DOUBLE_FLD\":[1111.220000,333,444],"
0091                 "\"T_STRING_FLD\":\"HELLO WORLD\","
0092                 "\"T_CARRAY_FLD\":\"AAECA0hFTExPIEJJTkFSWQQFAA==\"}"
0093         "}";
0094 
0095     char json_json_out[1024];
0096     char *istrtemp=NULL;
0097     size_t bufsz = 0;
0098 
0099     NDRX_LOG(log_info, "JSON IN: [%s]", json_json_in);
0100         rsplen=0L;
0101 
0102     for (i=0; i<10000; i++)
0103     {
0104         if ( EXFAIL == tpimport(json_json_in, 
0105                                 (long)strlen(json_json_in), 
0106                                 (char **)&obuf, 
0107                                 &rsplen, 
0108                                 0L) )
0109         {
0110             NDRX_LOG(log_error, "TESTERROR: Failed to import JSON!!!!");
0111             EXFAIL_OUT(ret);
0112         }
0113         NDRX_LOG(log_debug, "JSON imported. Return obuf=[%s] rsplen=[%ld]", 
0114                                                     obuf, rsplen);
0115 
0116         if (0!=strcmp(data_test, obuf))
0117         {
0118             NDRX_LOG(log_error, 
0119                  "TESTERROR: imported JSON not equal to obuf string ");
0120             EXFAIL_OUT(ret);
0121         }
0122 
0123         memset(json_json_out, 0, sizeof(json_json_out));
0124         olen = sizeof(json_json_out);
0125 
0126         if ( EXFAIL == tpexport(obuf, 
0127                                 (long)strlen(obuf),
0128                                 json_json_out, 
0129                                 &olen, 
0130                                 0L) )
0131         {
0132                 NDRX_LOG(log_error, "TESTERROR: Failed to export JSON!!!!");
0133                 EXFAIL_OUT(ret);
0134         }
0135         NDRX_LOG(log_debug, 
0136                 "JSON exported. Return json_json_out=[%s] olen=[%ld]", 
0137                 json_json_out, olen);
0138 
0139         if (0!=strcmp(json_json_in, json_json_out))
0140         {
0141             NDRX_LOG(log_error, 
0142                  "TESTERROR: Exported JSON not equal to incoming string ");
0143             EXFAIL_OUT(ret);
0144         }
0145     }
0146 
0147     /* testing with base64 flag*/
0148     NDRX_LOG(log_debug, "convert to b64");
0149     len_b64 = sizeof(json_json_in_b64);
0150     if (NULL==ndrx_base64_encode((unsigned char *)json_json_in, strlen(json_json_in), 
0151             &len_b64, json_json_in_b64))
0152     {
0153             NDRX_LOG(log_error, "Failed to convert to b64!");
0154             EXFAIL_OUT(ret);
0155     }
0156     for (i=0; i<10000; i++)
0157     {
0158         rsplen=0L;
0159         if ( EXFAIL == tpimport(json_json_in_b64, 
0160                                 (long)strlen(json_json_in_b64), 
0161                                 (char **)&obuf, 
0162                                 &rsplen, 
0163                                 TPEX_STRING) )
0164         {
0165             NDRX_LOG(log_error, "TESTERROR: Failed to import JSON CARRAY!!!!");
0166             EXFAIL_OUT(ret);
0167         }
0168 
0169         NDRX_LOG(log_debug, "JSON imported. Return obuf=[%s] rsplen=[%ld]", 
0170                                                     obuf, rsplen);
0171 
0172         if (0!=strcmp(data_test, obuf))
0173         {
0174             NDRX_LOG(log_error, 
0175                  "TESTERROR: imported JSON not equal to obuf string ");
0176             EXFAIL_OUT(ret);
0177         }
0178 
0179         memset(json_json_out_b64, 0, sizeof(json_json_out_b64));
0180         olen = sizeof(json_json_out_b64);
0181 
0182         if ( EXFAIL == tpexport(obuf, 
0183                                 (long)strlen(obuf),
0184                                 json_json_out_b64, 
0185                                 &olen, 
0186                                 TPEX_STRING) )
0187         {
0188                 NDRX_LOG(log_error, "TESTERROR: Failed to export JSON!!!!");
0189                 EXFAIL_OUT(ret);
0190         }
0191         NDRX_LOG(log_debug, 
0192                 "JSON exported. Return json_json_out_b64=[%s] olen=[%ld]", 
0193                 json_json_out_b64, olen);
0194 
0195         /* decode from b64 to check returned data */
0196         bufsz = strlen(json_json_out_b64);
0197         if (NULL==(istrtemp = NDRX_MALLOC(bufsz)))
0198         {
0199             NDRX_LOG(log_error, "Failed to allocate %ld bytes", strlen(json_json_out_b64));
0200             EXFAIL_OUT(ret);
0201         }
0202 
0203         if (NULL==ndrx_base64_decode(json_json_out_b64, strlen(json_json_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_json_in, istrtemp))
0211         {
0212             NDRX_LOG(log_error, 
0213                  "TESTERROR: Exported JSON not equal to incoming JSON");
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: */