Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief tpimport()/tpexport() function tests - client
0003  *
0004  * @file atmiclt56_view.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 <ubf.h>
0042 #include <ndebug.h>
0043 #include <test.fd.h>
0044 #include <ndrstandard.h>
0045 #include <nstopwatch.h>
0046 #include <fcntl.h>
0047 #include <unistd.h>
0048 #include <nstdutil.h>
0049 #include <ubfutil.h>
0050 #include <atmi_int.h>
0051 #include "test56.h"
0052 #include <extest.h>
0053 #include <exbase64.h>
0054 /*---------------------------Externs------------------------------------*/
0055 /*---------------------------Macros-------------------------------------*/
0056 /*---------------------------Enums--------------------------------------*/
0057 /*---------------------------Typedefs-----------------------------------*/
0058 /*---------------------------Globals------------------------------------*/
0059 /*---------------------------Statics------------------------------------*/
0060 /*---------------------------Prototypes---------------------------------*/
0061 /**
0062  * 
0063  * @return 
0064  */
0065 expublic int test_impexp_view(void)
0066 {
0067     int ret = EXSUCCEED;
0068     long rsplen, olen;
0069     char json_view_in_b64[CARR_BUFFSIZE_B64+1];
0070     char json_view_out_b64[CARR_BUFFSIZE_B64+1];
0071     size_t len_b64;
0072     int i;
0073     char *json_view_in = 
0074         "{"
0075             "\"buftype\":\"VIEW\","
0076             "\"version\":1,"
0077             "\"subtype\":\"UBTESTVIEW2\","
0078             "\"data\":"
0079             "{"
0080                 "\"UBTESTVIEW2\":"
0081                 "{"
0082                     "\"tshort1\":1,"
0083                     "\"tlong1\":2,"
0084                     "\"tchar1\":\"A\","
0085                     "\"tfloat1\":1,"
0086                     "\"tdouble1\":21,"
0087                     "\"tstring1\":\"ABC\","
0088                     "\"tcarray1\":\"SEVMTE8AAAAAAA==\""
0089                 "}"
0090             "}"
0091         "}";
0092     char json_view_out[1024];
0093     struct UBTESTVIEW2 *v;
0094     char *istrtemp=NULL;
0095     size_t bufsz = 0;
0096 
0097     NDRX_LOG(log_info, "JSON VIEW IN: [%s]", json_view_in);
0098     v = (struct UBTESTVIEW2 *)tpalloc("VIEW", "UBTESTVIEW2", sizeof(struct UBTESTVIEW2));
0099     if (NULL == v)
0100     {
0101         NDRX_LOG(log_error, "Failed to allocate VIEW %ld bytes: %s",
0102                  NDRX_MSGSIZEMAX, tpstrerror(tperrno));
0103         EXFAIL_OUT(ret);
0104     }
0105     for (i=0; i<10000; i++)
0106     {
0107         rsplen=0L;
0108         if ( EXFAIL == tpimport(json_view_in, 
0109                                 (long)strlen(json_view_in), 
0110                                 (char **)&v, 
0111                                 &rsplen, 
0112                                 0L) )
0113         {
0114             NDRX_LOG(log_error, "TESTERROR: Failed to import VIEW!!!!");
0115             EXFAIL_OUT(ret);
0116         }
0117         if (1 != v->tshort1)
0118         {
0119             NDRX_LOG(log_error, "TESTERROR: tshort1 got %ld expected 5555", 
0120                  v->tshort1);
0121             EXFAIL_OUT(ret);
0122         }
0123         if (2!=v->tlong1)
0124         {
0125             NDRX_LOG(log_error, "TESTERROR: tlong1 got %hd expected 2", 
0126                      v->tlong1);
0127             EXFAIL_OUT(ret);
0128         }
0129         if ('A'!=v->tchar1)
0130         {
0131             NDRX_LOG(log_error, "TESTERROR: tchar1 got %c expected A", 
0132                      v->tchar1);
0133             EXFAIL_OUT(ret);
0134         }
0135         if (fabs(v->tfloat1 - 1.0f) > 0.1)
0136         {
0137             NDRX_LOG(log_error, "TESTERROR: tfloat1 got %f expected 1", 
0138                      v->tfloat1);
0139             EXFAIL_OUT(ret);
0140         }
0141         if ((v->tdouble1 - 21.0f) > 0.1)
0142         {
0143             NDRX_LOG(log_error, "TESTERROR: tdouble1 got %lf expected 21", 
0144                      v->tdouble1);
0145             EXFAIL_OUT(ret);
0146         }
0147         if (0!=strcmp(v->tstring1, "ABC"))
0148         {
0149             NDRX_LOG(log_error, "TESTERROR: tstring1 got [%s] expected ABC", 
0150                      v->tdouble1);
0151             EXFAIL_OUT(ret);
0152         }
0153 
0154         memset(json_view_out, 0, sizeof(json_view_out));
0155         olen = sizeof(json_view_out);
0156 
0157         if ( EXFAIL == tpexport((char*)v, 
0158                                 0L, 
0159                                 json_view_out, 
0160                                 &olen, 
0161                                 0L) )
0162         {
0163                 NDRX_LOG(log_error, "TESTERROR: Failed to export JSON VIEW!!!!");
0164                 EXFAIL_OUT(ret);
0165         }
0166         NDRX_LOG(log_debug, 
0167                 "VIEW exported. Return json_view_out=[%s] olen=[%ld]", 
0168                 json_view_out, olen);
0169 
0170         if (0!=strcmp(json_view_in, json_view_out))
0171         {
0172             NDRX_LOG(log_error, 
0173                  "TESTERROR: Exported VIEW not equal to incoming VIEW ");
0174             EXFAIL_OUT(ret);
0175         }
0176     }
0177 
0178     /* testing with base64 flag*/
0179     NDRX_LOG(log_debug, "convert to b64");
0180     len_b64 = sizeof(json_view_in_b64);
0181     if (NULL==ndrx_base64_encode((unsigned char *)json_view_in, strlen(json_view_in), 
0182             &len_b64, json_view_in_b64))
0183     {
0184             NDRX_LOG(log_error, "Failed to convert to b64!");
0185             EXFAIL_OUT(ret);
0186     }
0187 
0188     for (i=0; i<10000; i++)
0189     {
0190         rsplen=0L;
0191         if ( EXFAIL == tpimport(json_view_in_b64, 
0192                                 (long)strlen(json_view_in_b64), 
0193                                 (char **)&v, 
0194                                 &rsplen, 
0195                                 TPEX_STRING) )
0196         {
0197             NDRX_LOG(log_error, "TESTERROR: Failed to import VIEW!!!!");
0198             EXFAIL_OUT(ret);
0199         }
0200         if (1 != v->tshort1)
0201         {
0202             NDRX_LOG(log_error, "TESTERROR: tshort1 got %ld expected 5555", 
0203                  v->tshort1);
0204             EXFAIL_OUT(ret);
0205         }
0206         if (2!=v->tlong1)
0207         {
0208             NDRX_LOG(log_error, "TESTERROR: tlong1 got %hd expected 2", 
0209                      v->tlong1);
0210             EXFAIL_OUT(ret);
0211         }
0212         if ('A'!=v->tchar1)
0213         {
0214             NDRX_LOG(log_error, "TESTERROR: tchar1 got %c expected A", 
0215                      v->tchar1);
0216             EXFAIL_OUT(ret);
0217         }
0218         if (fabs(v->tfloat1 - 1.0f) > 0.1)
0219         {
0220             NDRX_LOG(log_error, "TESTERROR: tfloat1 got %f expected 1", 
0221                      v->tfloat1);
0222             EXFAIL_OUT(ret);
0223         }
0224         if ((v->tdouble1 - 21.0f) > 0.1)
0225         {
0226             NDRX_LOG(log_error, "TESTERROR: tdouble1 got %lf expected 21", 
0227                      v->tdouble1);
0228             EXFAIL_OUT(ret);
0229         }
0230         if (0!=strcmp(v->tstring1, "ABC"))
0231         {
0232             NDRX_LOG(log_error, "TESTERROR: tstring1 got [%s] expected ABC", 
0233                      v->tdouble1);
0234             EXFAIL_OUT(ret);
0235         }
0236 
0237         memset(json_view_out_b64, 0, sizeof(json_view_out_b64));
0238         olen = sizeof(json_view_out_b64);
0239 
0240         if ( EXFAIL == tpexport((char*)v, 
0241                                 0L, 
0242                                 json_view_out_b64, 
0243                                 &olen, 
0244                                 TPEX_STRING) )
0245         {
0246                 NDRX_LOG(log_error, "TESTERROR: Failed to export JSON VIEW!!!!");
0247                 EXFAIL_OUT(ret);
0248         }
0249 
0250         /* decode from b64 to check returned data */
0251         bufsz = strlen(json_view_out_b64);
0252         if (NULL==(istrtemp = NDRX_MALLOC(bufsz)))
0253         {
0254             NDRX_LOG(log_error, "Failed to allocate %ld bytes", strlen(json_view_out_b64));
0255             EXFAIL_OUT(ret);
0256         }
0257 
0258         if (NULL==ndrx_base64_decode(json_view_out_b64, strlen(json_view_out_b64), 
0259                 &bufsz, istrtemp))
0260         {
0261             NDRX_LOG(log_error, "Failed to decode CARRAY");
0262             EXFAIL_OUT(ret);
0263         }
0264         istrtemp[bufsz]=0;
0265 
0266         if (0!=strcmp(json_view_in, istrtemp))
0267         {
0268             NDRX_LOG(log_error, 
0269                  "TESTERROR: Exported VIEW not equal to incoming VIEW ");
0270             EXFAIL_OUT(ret);
0271         }
0272         if (NULL!=istrtemp)
0273         {
0274             NDRX_FREE(istrtemp);
0275             istrtemp=NULL;
0276         }
0277    }
0278 
0279 out:
0280 
0281     if (NULL!=istrtemp)
0282     {
0283         NDRX_FREE(istrtemp);
0284         istrtemp=NULL;
0285     }
0286 
0287 return ret;
0288 }
0289 /* vim: set ts=4 sw=4 et smartindent: */