Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief tpimport()/tpexport() function tests - client
0003  *
0004  * @file atmiclt56_embbuf.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 <extest.h>
0053 #include <exbase64.h>
0054 #include <exassert.h>
0055 /*---------------------------Externs------------------------------------*/
0056 /*---------------------------Macros-------------------------------------*/
0057 /*---------------------------Enums--------------------------------------*/
0058 /*---------------------------Typedefs-----------------------------------*/
0059 /*---------------------------Globals------------------------------------*/
0060 /*---------------------------Statics------------------------------------*/
0061 /*---------------------------Prototypes---------------------------------*/
0062 
0063 /**
0064  * Test one loop
0065  * @return 
0066  */
0067 exprivate int test_impexp_testemb_int(void)
0068 {
0069     int ret = EXSUCCEED;
0070     
0071     UBFH *p_ub1=NULL;
0072     UBFH *p_ub2=NULL;
0073     UBFH *p_ub3=NULL;
0074     struct UBTESTVIEW2 *v=NULL;
0075     struct UBTESTVIEW2 *v2=NULL;
0076     struct UBTESTVIEW2 *v3=NULL;
0077     
0078     UBFH *p_ub4=NULL;
0079     UBFH *p_ub5=NULL;
0080     
0081     BVIEWFLD vf;
0082     char json_ubf_out[56000];
0083     long olen;
0084     long rsplen;
0085     char vtyp[16];
0086     char vsubtyp[16];
0087     
0088     p_ub1 = (UBFH *)tpalloc("UBF", 0, 56000); 
0089     NDRX_ASSERT_TP_OUT((NULL!=p_ub1), "Failed to alloc p_ub1");
0090     
0091     p_ub2 = (UBFH *)tpalloc("UBF", 0, 56000); 
0092     NDRX_ASSERT_TP_OUT((NULL!=p_ub2), "Failed to alloc p_ub2");
0093     
0094     p_ub3 = (UBFH *)tpalloc("UBF", 0, 56000); 
0095     NDRX_ASSERT_TP_OUT((NULL!=p_ub3), "Failed to alloc p_ub3");
0096     
0097     
0098     v = (struct UBTESTVIEW2 *)tpalloc("VIEW", "UBTESTVIEW2", sizeof(struct UBTESTVIEW2));
0099     
0100     
0101     memset(v, 0, sizeof(*v));
0102     
0103     NDRX_STRCPY_SAFE(v->tstring1, "HELLO");
0104     v->tchar1='A';
0105     v->tshort1=77;
0106     v->tlong1=123123;
0107     v->tchar1='X';
0108     v->tfloat1=99;
0109     v->tdouble1=99999;
0110     v->tcarray1[0]=0; /* well seems like we have a bug. default empty view value is ' ' space */
0111     v->tcarray1[1]=1;
0112     v->tcarray1[2]=2;
0113     
0114     NDRX_ASSERT_TP_OUT((NULL!=p_ub2), "Failed to alloc UBTESTVIEW2");
0115     
0116     /* Load some fields in sub-buffer  */
0117     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bchg(p_ub2, T_STRING_FLD, 3, "HELLO STRING", 0)), 
0118             "Failed to set T_STRING to buf2");
0119     
0120     NDRX_ASSERT_UBF_OUT((EXSUCCEED==CBchg(p_ub2, T_LONG_FLD, 10, "777", 0, BFLD_STRING)), 
0121             "Failed to set T_LONG_FLD to buf2");
0122     
0123     vf.data = (char *)v;
0124     vf.vflags=0;
0125     NDRX_STRCPY_SAFE(vf.vname, "UBTESTVIEW2");
0126     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bchg(p_ub2, T_VIEW_FLD, 5, (char *)&vf, 0)), 
0127             "Failed to set T_VIEW_FLD[5]");
0128     
0129     /* test non array */
0130     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bchg(p_ub2, T_VIEW_2_FLD, 0, (char *)&vf, 0)), 
0131             "Failed to set T_VIEW_2_FLD[0]");
0132     
0133     /* Load some UBF... */
0134     NDRX_ASSERT_UBF_OUT((EXSUCCEED==CBchg(p_ub3, T_STRING_FLD, 5, "HELLO 3/5", 0, BFLD_STRING)), 
0135             "Failed to set T_STRING_FLD to buf3");
0136     
0137     /* at third level */
0138     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bchg(p_ub2, T_UBF_FLD, 2, (char *)p_ub3, 0)), 
0139             "Failed to set T_UBF_FLD[2]");
0140     
0141     /* Load buffer into main p_ub1 */
0142     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bchg(p_ub1, T_UBF_FLD, 3, (char *)p_ub2, 0)), 
0143             "Failed to set T_UBF_FLD[3]");
0144     
0145     /* test non array ... */
0146     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bchg(p_ub1, T_UBF_2_FLD, 0, (char *)p_ub2, 0)), 
0147             "Failed to set T_UBF_2_FLD[0]");
0148     
0149     /* unload some pointers: */
0150     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bchg(p_ub1, T_PTR_FLD, 0, (char *)&p_ub2, 0)), 
0151             "Failed to set T_PTR_FLD[0]");
0152     
0153     /* some view too... */
0154     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bchg(p_ub1, T_PTR_FLD, 1, (char *)&v, 0)), 
0155             "Failed to set T_PTR_FLD[1]");
0156     
0157     /* test non array ... */
0158     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bchg(p_ub1, T_PTR_2_FLD, 0, (char *)&v, 0)), 
0159             "Failed to set T_PTR_2_FLD[0]");
0160     
0161     memset(json_ubf_out, 0, sizeof(json_ubf_out));
0162     olen = sizeof(json_ubf_out);
0163     
0164     if ( EXFAIL == tpexport((char *)p_ub1, 
0165                             0,
0166                             json_ubf_out, 
0167                             &olen, 
0168                             0L) )
0169     {
0170         NDRX_LOG(log_error, "TESTERROR: Failed to export JSON UBF!!!!");
0171         EXFAIL_OUT(ret);
0172     }
0173     
0174     NDRX_LOG(log_debug, 
0175              "JSON UBF exported. Return json_ubf_out=[%s] olen=[%ld]", 
0176              json_ubf_out, olen);
0177     
0178     
0179     /* load the buffer */
0180     if ( EXFAIL == tpimport(json_ubf_out, 
0181                                 (long)strlen(json_ubf_out), 
0182                                 (char **)&p_ub4, 
0183                                 &rsplen, 
0184                                 0L) )
0185     {
0186         NDRX_LOG(log_error, "TESTERROR: Failed to import JSON UBF/EMB!!!!");
0187         EXFAIL_OUT(ret);
0188     }
0189     
0190     /* read the pointer and delete them... */
0191     
0192     /* Read the pointer: */
0193     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bget(p_ub4, T_PTR_FLD, 0, (char *)&p_ub5, 0)), 
0194             "Failed to set T_PTR_FLD[0]");
0195     
0196     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bget(p_ub4, T_PTR_FLD, 1, (char *)&v2, NULL)), 
0197             "Failed to set T_PTR_FLD[1]");
0198     
0199     /* test non array ... */
0200     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bget(p_ub4, T_PTR_2_FLD, 0, (char *)&v3, NULL)), 
0201             "Failed to set T_PTR_2_FLD[0]");
0202     
0203     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bdel(p_ub4, T_PTR_FLD, 1)), "Failed to delete p_ub4 T_PTR_FLD[1]");
0204     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bdel(p_ub4, T_PTR_FLD, 0)), "Failed to delete p_ub4 T_PTR_FLD[0]");
0205     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bdel(p_ub4, T_PTR_2_FLD, 0)), "Failed to delete p_ub4 T_PTR_2_FLD[0]");
0206     
0207     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bdel(p_ub1, T_PTR_FLD, 1)), "Failed to delete p_ub1 T_PTR_FLD[1]");
0208     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bdel(p_ub1, T_PTR_FLD, 0)), "Failed to delete p_ub1 T_PTR_FLD[0]");
0209     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bdel(p_ub1, T_PTR_2_FLD, 0)), "Failed to delete p_ub1 T_PTR_2_FLD[0]");
0210     
0211     
0212     /* run compare: */
0213     NDRX_ASSERT_UBF_OUT(Bcmp(p_ub1, p_ub4)==0, "Failed to compare p_ub1 vs p_ub4");
0214     NDRX_ASSERT_UBF_OUT(Bcmp(p_ub2, p_ub5)==0, "Failed to compare p_ub2 vs p_ub5");
0215     
0216     
0217     /* check that that v2 has view type */
0218     
0219     if (EXFAIL==tptypes((char *)v2, vtyp, vsubtyp))
0220     {
0221         NDRX_LOG(log_error, "TESTERROR: got error for tptypes on v2 %s", tpstrerror(tperrno));
0222         EXFAIL_OUT(ret);
0223     }
0224     
0225     NDRX_ASSERT_VAL_OUT(0==strcmp(vtyp, "VIEW"), "Expected VIEW");
0226     NDRX_ASSERT_VAL_OUT(0==strcmp(vsubtyp, "UBTESTVIEW2"), "Expected UBTESTVIEW2");
0227     
0228     
0229     /* check that that v3 has view type */
0230     
0231     if (EXFAIL==tptypes((char *)v3, vtyp, vsubtyp))
0232     {
0233         NDRX_LOG(log_error, "TESTERROR: got error for tptypes on v3 %s", tpstrerror(tperrno));
0234         EXFAIL_OUT(ret);
0235     }
0236     
0237     NDRX_ASSERT_VAL_OUT(0==strcmp(vtyp, "VIEW"), "Expected VIEW");
0238     NDRX_ASSERT_VAL_OUT(0==strcmp(vsubtyp, "UBTESTVIEW2"), "Expected UBTESTVIEW2");
0239     
0240     NDRX_ASSERT_UBF_OUT(Bvcmp((char *)v, "UBTESTVIEW2", (char *)v2, "UBTESTVIEW2")==0, 
0241             "Failed to compare v vs v2");
0242     NDRX_ASSERT_UBF_OUT(Bvcmp((char *)v, "UBTESTVIEW2", (char *)v3, "UBTESTVIEW2")==0, 
0243             "Failed to compare v vs v3");
0244     
0245 out:
0246    
0247     if (NULL!=p_ub1)
0248     {
0249         tpfree((char *)p_ub1);
0250     }
0251 
0252     if (NULL!=p_ub2)
0253     {
0254         tpfree((char *)p_ub2);
0255     }
0256 
0257     if (NULL!=p_ub3)
0258     {
0259         tpfree((char *)p_ub3);
0260     }
0261 
0262     if (NULL!=p_ub4)
0263     {
0264         tpfree((char *)p_ub4);
0265     }
0266 
0267     if (NULL!=p_ub5)
0268     {
0269         tpfree((char *)p_ub5);
0270     }
0271 
0272     if (NULL!=v)
0273     {
0274         tpfree((char *)v);
0275     }
0276 
0277     if (NULL!=v2)
0278     {
0279         tpfree((char *)v2);
0280     }
0281 
0282     if (NULL!=v3)
0283     {
0284         tpfree((char *)v3);
0285     }
0286              
0287     return ret;
0288 }
0289 
0290 /**
0291  * Test embedded buffer build/parse for UBF
0292  * @return EXSUCCEED/EXFAIL
0293  */
0294 expublic int test_impexp_testemb(void)
0295 {
0296     int ret=EXSUCCEED;
0297     
0298     int i;
0299     for (i=0; i<10000; i++)
0300     {
0301         
0302         if (EXSUCCEED!=test_impexp_testemb_int())
0303         {
0304             NDRX_LOG(log_error, "TESTERROR at %d loop", i);
0305             EXFAIL_OUT(ret);
0306         }
0307         
0308     }
0309 out:
0310     return ret;
0311 }
0312 
0313 /* vim: set ts=4 sw=4 et smartindent: */