Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Manual syntax test of tpimport() for embedded buffers
0003  *  at next step export/import is performed, thus export shall match the
0004  *  already tested import format.
0005  *
0006  * @file atmiclt56_embbuf_syntax.c
0007  */
0008 /* -----------------------------------------------------------------------------
0009  * Enduro/X Middleware Platform for Distributed Transaction Processing
0010  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0011  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0012  * This software is released under one of the following licenses:
0013  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0014  * See LICENSE file for full text.
0015  * -----------------------------------------------------------------------------
0016  * AGPL license:
0017  *
0018  * This program is free software; you can redistribute it and/or modify it under
0019  * the terms of the GNU Affero General Public License, version 3 as published
0020  * by the Free Software Foundation;
0021  *
0022  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0023  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0024  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0025  * for more details.
0026  *
0027  * You should have received a copy of the GNU Affero General Public License along 
0028  * with this program; if not, write to the Free Software Foundation, Inc.,
0029  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0030  *
0031  * -----------------------------------------------------------------------------
0032  * A commercial use license is available from Mavimax, Ltd
0033  * contact@mavimax.com
0034  * -----------------------------------------------------------------------------
0035  */
0036 #include <string.h>
0037 #include <stdio.h>
0038 #include <stdlib.h>
0039 #include <memory.h>
0040 #include <math.h>
0041 
0042 #include <atmi.h>
0043 #include <atmi_int.h>
0044 #include <ubf.h>
0045 #include <ndebug.h>
0046 #include <test.fd.h>
0047 #include <ndrstandard.h>
0048 #include <nstopwatch.h>
0049 #include <fcntl.h>
0050 #include <unistd.h>
0051 #include <nstdutil.h>
0052 #include <ubfutil.h>
0053 #include "test56.h"
0054 #include <extest.h>
0055 #include "expr.h"
0056 #include "odebug.h"
0057 #include <exbase64.h>
0058 #include <exassert.h>
0059 #include <typed_buf.h>
0060 /*---------------------------Externs------------------------------------*/
0061 /*---------------------------Macros-------------------------------------*/
0062 /*---------------------------Enums--------------------------------------*/
0063 /*---------------------------Typedefs-----------------------------------*/
0064 /*---------------------------Globals------------------------------------*/
0065 /*---------------------------Statics------------------------------------*/
0066 /*---------------------------Prototypes---------------------------------*/
0067 
0068 
0069 /**
0070  * Quick buffer checking...
0071  * @param p_ub
0072  * @param expr
0073  * @return EXFAIL/EXSUCCEED/EXFAIL
0074  */
0075 exprivate int q_eval(UBFH *p_ub, char *expr)
0076 {
0077     int ret = EXSUCCEED;
0078     char *tree=NULL;
0079     
0080     if (NULL==(tree=Bboolco(expr)))
0081     {
0082         NDRX_LOG(log_error, "TESTERROR: Invalid check syntax for [%s]: %s", 
0083                 expr, Bstrerror(Berror));
0084         EXFAIL_OUT(ret);
0085     }
0086     
0087     ret = Bboolev(p_ub, tree);
0088     
0089 out:
0090     
0091     if (NULL!=tree)
0092     {
0093         Btreefree(tree);
0094     }
0095 
0096     return ret;
0097     
0098 }
0099 
0100 /**
0101  * Manual buffer import -> validate syntax
0102  * - Check single & array of UBF
0103  * - Check single view & array of views
0104  * - Check single ptr & array of ptrs
0105  * @return 
0106  */
0107 expublic int test_impexp_testemb_syntax(void)
0108 {
0109     int ret = EXSUCCEED;
0110     int i;
0111     long rsplen;
0112     UBFH *obuf=NULL;
0113     UBFH *p1_ubf=NULL;
0114     UBFH *p2_ubf=NULL;
0115     UBFH *p3_ubf=NULL;
0116     ndrx_growlist_t list;
0117     char *json_ubf_in = 
0118         "{"
0119             "\"buftype\":\"UBF\","
0120             "\"version\":1,"
0121             "\"data\":"
0122             "{"
0123                 "\"T_SHORT_FLD\":55,"
0124                 /* Single sub-UBF: */
0125                 "\"T_UBF_FLD\":"
0126                 "{"
0127                     "\"T_STRING_FLD\":\"HELLO WORLD INNER\""
0128                 "},"
0129                 /* Array of sub-UBFs */
0130                 "\"T_UBF_2_FLD\": ["
0131                     "{"
0132                         "\"T_STRING_FLD\":\"HELLO WORLD INNER 1\""
0133                     "},"
0134                     "{"
0135                         "\"T_STRING_FLD\":\"HELLO WORLD INNER 2\""
0136                     "},"
0137                     "{"
0138                     "}"
0139                 "],"
0140                 /* Test single view import */
0141                 "\"T_VIEW_FLD\":"
0142                 "{"
0143                    "\"UBTESTVIEW2\":"
0144                    "{"
0145                        "\"tshort1\":1,"
0146                        "\"tlong1\":2,"
0147                        "\"tchar1\":\"A\","
0148                        "\"tfloat1\":1,"
0149                        "\"tdouble1\":21,"
0150                        "\"tstring1\":\"ABC\","
0151                        "\"tcarray1\":\"SEVMTE8AAAAAAA==\""
0152                    "}"
0153                 "},"
0154                 /* Test view array: */
0155                 "\"T_VIEW_2_FLD\": ["
0156                     "{"
0157                        "\"UBTESTVIEW2\":"
0158                        "{"
0159                            "\"tshort1\":1,"
0160                            "\"tlong1\":2,"
0161                            "\"tchar1\":\"A\","
0162                            "\"tfloat1\":1,"
0163                            "\"tdouble1\":21,"
0164                            "\"tstring1\":\"ABC_2\","
0165                            "\"tcarray1\":\"SEVMTE8AAAAAAA==\""
0166                        "}"
0167                     "},"
0168                     "{"
0169                        "\"\":{}"
0170                     "},"
0171                     "{"
0172                        "\"UBTESTVIEW2\":"
0173                        "{"
0174                            "\"tshort1\":1,"
0175                            "\"tlong1\":2,"
0176                            "\"tchar1\":\"A\","
0177                            "\"tfloat1\":1,"
0178                            "\"tdouble1\":21,"
0179                            "\"tstring1\":\"ABC_3\","
0180                            "\"tcarray1\":\"SEVMTE8AAAAAAA==\""
0181                        "}"
0182                     "}"
0183                 "],"
0184                 /* Test single ptr: */
0185                 "\"T_PTR_FLD\":"
0186                 "{"
0187                     "\"buftype\":\"UBF\","
0188                     "\"version\":1,"
0189                     "\"data\":"
0190                     "{"
0191                         "\"T_SHORT_FLD\":1765,"
0192                         "\"T_LONG_FLD\":[115,2],"
0193                         "\"T_CHAR_FLD\":\"A\","
0194                         "\"T_FLOAT_FLD\":1,"
0195                         "\"T_DOUBLE_FLD\":[1111.220000,333,444],"
0196                         "\"T_STRING_FLD\":\"HELLO WORLD\","
0197                         "\"T_CARRAY_FLD\":\"AAECA0hFTExPIEJJTkFSWQQFAA==\""
0198                     "}"
0199                 "},"
0200                 /* Test array of ptr: */
0201                 "\"T_PTR_2_FLD\": ["
0202                     "{"
0203                         "\"buftype\":\"UBF\","
0204                         "\"version\":1,"
0205                         "\"data\":"
0206                         "{"
0207                             "\"T_SHORT_FLD\":1765,"
0208                             "\"T_LONG_FLD\":[1111,2],"
0209                             "\"T_CHAR_FLD\":\"A\","
0210                             "\"T_FLOAT_FLD\":1,"
0211                             "\"T_DOUBLE_FLD\":[1111.220000,333,444],"
0212                             "\"T_STRING_FLD\":\"HELLO WORLD 22\","
0213                             "\"T_CARRAY_FLD\":\"AAECA0hFTExPIEJJTkFSWQQFAA==\""
0214                         "}"
0215                     "},"
0216                     "{"
0217                         "\"buftype\":\"UBF\","
0218                         "\"version\":1,"
0219                         "\"data\":"
0220                         "{"
0221                             "\"T_SHORT_FLD\":1765,"
0222                             "\"T_LONG_FLD\":[4444,2],"
0223                             "\"T_CHAR_FLD\":\"A\","
0224                             "\"T_FLOAT_FLD\":1,"
0225                             "\"T_DOUBLE_FLD\":[1111.220000,333,444],"
0226                             "\"T_STRING_FLD\":\"HELLO WORLD 44\","
0227                             "\"T_CARRAY_FLD\":\"AAECA0hFTExPIEJJTkFSWQQFAA==\""
0228                         "}"
0229                     "}"
0230                 "]"
0231             "}"
0232         "}";
0233     
0234     NDRX_LOG(log_info, "JSON UBF IN: [%s]", json_ubf_in);
0235 
0236     if (NULL == (obuf = (UBFH *)tpalloc("UBF", NULL, NDRX_MSGSIZEMAX)))
0237     {
0238         NDRX_LOG(log_error, "Failed to allocate UBFH %ld bytes: %s",
0239                  NDRX_MSGSIZEMAX, tpstrerror(tperrno));
0240         EXFAIL_OUT(ret);
0241     }
0242 
0243     for (i=0; i<10000; i++)
0244     {
0245         rsplen=0L;
0246         if ( EXFAIL == tpimport(json_ubf_in, 
0247                                 (long)strlen(json_ubf_in), 
0248                                 (char **)&obuf, 
0249                                 &rsplen, 
0250                                 0L) )
0251         {
0252             NDRX_LOG(log_error, "TESTERROR: Failed to import JSON UBF!!!!");
0253             EXFAIL_OUT(ret);
0254         }
0255         
0256         ndrx_debug_dump_UBF(log_debug, "JSON UBF imported. Return obuf", (UBFH *)obuf);
0257         
0258         /* check the values */
0259         
0260         NDRX_ASSERT_VAL_OUT(EXTRUE==q_eval(obuf, "T_SHORT_FLD==55"), "Expected root level parsed");
0261         NDRX_ASSERT_VAL_OUT(EXTRUE==q_eval(obuf, "T_UBF_FLD.T_STRING_FLD=='HELLO WORLD INNER'"), 
0262                 "Expected single UBF parsed");
0263         NDRX_ASSERT_VAL_OUT(EXTRUE==q_eval(obuf, "T_UBF_2_FLD[0].T_STRING_FLD=='HELLO WORLD INNER 1'"), 
0264                 "Array UBF[0] inval value");
0265         NDRX_ASSERT_VAL_OUT(EXTRUE==q_eval(obuf, "T_UBF_2_FLD[1].T_STRING_FLD=='HELLO WORLD INNER 2'"), 
0266                 "Array UBF[1] inval value");
0267         
0268         NDRX_ASSERT_VAL_OUT(EXTRUE==q_eval(obuf, "T_VIEW_FLD[0].tstring1=='ABC'"), 
0269                 "Single T_VIEW_FLD[0].tstring1 inval value");
0270         
0271         NDRX_ASSERT_VAL_OUT(EXTRUE==q_eval(obuf, "T_VIEW_2_FLD[2].tstring1=='ABC_3'"), 
0272                 "Array T_VIEW_2_FLD[2].tstring1 inval value");
0273         
0274         /* read of the pointer allocs */
0275         NDRX_ASSERT_UBF_OUT(EXSUCCEED==Bget(obuf, T_PTR_FLD, 0, (char *)&p1_ubf, 0L), 
0276                 "Failed to get T_PTR_FLD");
0277         
0278         NDRX_ASSERT_VAL_OUT(EXTRUE==q_eval(p1_ubf, "T_STRING_FLD=='HELLO WORLD' && T_LONG_FLD[0]==115 && T_LONG_FLD[1]==2"), 
0279                 "Invalid p1_ubf");
0280         
0281         NDRX_ASSERT_UBF_OUT(EXSUCCEED==Bget(obuf, T_PTR_2_FLD, 0, (char *)&p2_ubf, 0L), 
0282                 "Failed to get T_PTR_2_FLD[0]");
0283         
0284         NDRX_ASSERT_VAL_OUT(EXTRUE==q_eval(p2_ubf, "T_STRING_FLD=='HELLO WORLD 22' && T_LONG_FLD[0]==1111 && T_LONG_FLD[1]==2"), 
0285                 "Invalid p2_ubf");
0286         
0287         NDRX_ASSERT_UBF_OUT(EXSUCCEED==Bget(obuf, T_PTR_2_FLD, 1, (char *)&p3_ubf, 0L), 
0288                 "Failed to get T_PTR_2_FLD[1]");
0289         
0290         NDRX_ASSERT_VAL_OUT(EXTRUE==q_eval(p3_ubf, "T_STRING_FLD=='HELLO WORLD 44' && T_LONG_FLD[0]==4444 && T_LONG_FLD[1]==2"), 
0291                 "Invalid p3_ubf");
0292         
0293         /* delete the buffer */
0294         tpfree((char *)obuf);
0295     
0296 
0297 /*
0298         recurive free takes care of this
0299         tpfree((char *)p1_ubf);
0300         tpfree((char *)p2_ubf);
0301         tpfree((char *)p3_ubf);
0302 */
0303         
0304         obuf=NULL;
0305     }
0306     
0307     
0308     ndrx_buffer_list(&list);
0309         
0310     /* print ptrs... */
0311     NDRX_ASSERT_VAL_OUT(-1==list.maxindexused, "Not all buffers are deallocated %d", list.maxindexused);
0312     
0313 out:
0314 
0315     return ret;
0316 }
0317 
0318 /* vim: set ts=4 sw=4 et smartindent: */