Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Typed JSON testing
0003  *
0004  * @file atmiclt26.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 
0039 #include <atmi.h>
0040 #include <ubf.h>
0041 #include <ndebug.h>
0042 #include <test.fd.h>
0043 #include <ndrstandard.h>
0044 #include "test026.h"
0045 #include "ubfutil.h"
0046 
0047 /*---------------------------Externs------------------------------------*/
0048 /*---------------------------Macros-------------------------------------*/
0049 /*---------------------------Enums--------------------------------------*/
0050 /*---------------------------Typedefs-----------------------------------*/
0051 /*---------------------------Globals------------------------------------*/
0052 /*---------------------------Statics------------------------------------*/
0053 /*---------------------------Prototypes---------------------------------*/
0054 
0055 /**
0056  * test case for #189 - fails to parse json
0057  * @return EXSUCCEED/EXFAIL
0058  */
0059 int test_tpjsontoubf(void)
0060 {
0061     int ret = EXSUCCEED;
0062     
0063     /* Bug #189 */
0064     char *data = "{\n"
0065                   "        \"T_STRING_FLD\":\"L\"\n"
0066                   "}";
0067     
0068     UBFH *p_ub = (UBFH *)tpalloc("UBF", NULL, 1024);
0069     
0070     if (NULL==p_ub)
0071     {
0072         NDRX_LOG(log_error, "TESTERROR! Failed to allocate p_ub: %s", 
0073                 tpstrerror(tperrno));
0074         EXFAIL_OUT(ret);
0075     }
0076     
0077     NDRX_LOG(log_info, "Testing Bug #189 tpjsontoubf() - failes to parse JSON");
0078     
0079     if (EXSUCCEED!=tpjsontoubf(p_ub, data))
0080     {
0081         NDRX_LOG(log_error, "TESTERROR! Failed to parse [%s]: %s", 
0082                 data, tpstrerror(tperrno));
0083         EXFAIL_OUT(ret);
0084     }
0085     
0086     ndrx_debug_dump_UBF(log_debug, "parsed ubf buffer", p_ub);
0087     
0088     
0089 out:
0090     tpfree((char *)p_ub);
0091     return ret;
0092 }
0093 
0094 /**
0095  * test case for Bug #796, tpubftojson() damages the log files
0096 */
0097 int test_tpjsontoubf_long(void)
0098 {
0099     int ret = EXSUCCEED;
0100     long lmax = LONG_MAX;
0101     long lmin = LONG_MIN;
0102 
0103 #ifdef SYS64BIT
0104     long lmax2 = 109832567849012365;
0105 #endif
0106     char tmp[1024];
0107 
0108     UBFH *p_ub = (UBFH *)tpalloc("UBF", NULL, 1024);
0109     UBFH *p_ub2 = (UBFH *)tpalloc("UBF", NULL, 1024);
0110     
0111     if (NULL==p_ub || NULL==p_ub2)
0112     {
0113         NDRX_LOG(log_error, "TESTERROR! Failed to allocate p_ub or p_ub2: %s", 
0114                 tpstrerror(tperrno));
0115         EXFAIL_OUT(ret);
0116     }
0117 
0118     if (EXSUCCEED!=Bchg(p_ub, T_LONG_FLD, 0, (char *)&lmax, 0L))
0119     {
0120         NDRX_LOG(log_error, "TESTERROR! Failed to set T_LONG_FLD: %s", 
0121                 Bstrerror(Berror));
0122         EXFAIL_OUT(ret);
0123     }
0124 
0125     if (EXSUCCEED!=Bchg(p_ub, T_LONG_2_FLD, 0, (char *)&lmin, 0L))
0126     {
0127         NDRX_LOG(log_error, "TESTERROR! Failed to set T_LONG_2_FLD: %s", 
0128                 Bstrerror(Berror));
0129         EXFAIL_OUT(ret);
0130     }
0131 
0132 #ifdef SYS64BIT
0133     if (EXSUCCEED!=Bchg(p_ub, T_LONG_3_FLD, 0, (char *)&lmax2, 0L))
0134     {
0135         NDRX_LOG(log_error, "TESTERROR! Failed to set T_LONG_3_FLD: %s", 
0136                 Bstrerror(Berror));
0137         EXFAIL_OUT(ret);
0138     }
0139     /* test array */
0140     if (EXSUCCEED!=Bchg(p_ub, T_LONG_3_FLD, 1, (char *)&lmax2, 0L))
0141     {
0142         NDRX_LOG(log_error, "TESTERROR! Failed to set T_LONG_3_FLD[1]: %s",
0143                 Bstrerror(Berror));
0144         EXFAIL_OUT(ret);
0145     }
0146 #endif
0147 
0148     if (EXSUCCEED!=Bchg(p_ub, T_CHAR_FLD, 0, (char *)"B", 0L))
0149     {
0150         NDRX_LOG(log_error, "TESTERROR! Failed to set T_CHAR_FLD: %s",
0151                 Bstrerror(Berror));
0152         EXFAIL_OUT(ret);
0153     }
0154 
0155     if (EXSUCCEED!=tpubftojson(p_ub, tmp, sizeof(tmp)))
0156     {
0157         NDRX_LOG(log_error, "TESTERROR! Failed to convert to json: %s", 
0158                 tpstrerror(tperrno));
0159         EXFAIL_OUT(ret);
0160     }
0161 
0162     NDRX_LOG(log_debug, "Got json: [%s]", tmp);
0163 
0164     if (EXSUCCEED!=tpjsontoubf(p_ub2, tmp))
0165     {
0166         NDRX_LOG(log_error, "TESTERROR! Failed to parse [%s]: %s", 
0167                 tmp, tpstrerror(tperrno));
0168         EXFAIL_OUT(ret);
0169     }
0170 
0171     if (EXSUCCEED!=Bcmp(p_ub, p_ub2))
0172     {
0173         NDRX_LOG(log_error, "TESTERROR! Values are not equal!");
0174         
0175         ndrx_debug_dump_UBF(log_debug, "Org buffer", p_ub);
0176         ndrx_debug_dump_UBF(log_debug, "Parsed buffer", p_ub2);
0177         EXFAIL_OUT(ret);
0178     }
0179     
0180 out:
0181     if (NULL!=p_ub)
0182     {
0183         tpfree((char *)p_ub);
0184     }
0185 
0186     if (NULL!=p_ub2)
0187     {
0188         tpfree((char *)p_ub2);
0189     }
0190     
0191     return ret;
0192 }
0193 /*
0194  * Do the test call to the server
0195  */
0196 int main(int argc, char** argv) {
0197 
0198     long rsplen;
0199     int i;
0200     int ret=EXSUCCEED;
0201     long l;
0202     char tmp[128];
0203     
0204     if (EXSUCCEED!=test_tpjsontoubf())
0205     {
0206         EXFAIL_OUT(ret);
0207     }
0208 
0209     if (EXSUCCEED!=test_tpjsontoubf_long())
0210     {
0211         EXFAIL_OUT(ret);
0212     }
0213     
0214     NDRX_LOG(log_info, "Testing UBF -> JSON auto conv...");
0215     for (i=0; i<10000; i++)
0216     {
0217         UBFH *p_ub = (UBFH *)tpalloc("UBF", NULL, 1024);
0218         UBFH *p_ub2=NULL;
0219 
0220         if (NULL==p_ub)
0221         {
0222             NDRX_LOG(log_error, "TESTERROR: failed to alloc UBF buffer!");
0223             EXFAIL_OUT(ret);
0224         }
0225 
0226         p_ub2 = (UBFH *)tpalloc("UBF", NULL, 1024);
0227         if (NULL==p_ub)
0228         {
0229             NDRX_LOG(log_error, "TESTERROR: failed to alloc UBF buffer!");
0230             EXFAIL_OUT(ret);
0231         }
0232 
0233         if (EXSUCCEED!=Bchg(p_ub, T_STRING_FLD, 0, "HELLO FROM UBF", 0L))
0234         {
0235             NDRX_LOG(log_error, "TESTERROR: Failed to set T_STRING_FLD");
0236             ret=EXFAIL;
0237             goto out;
0238         }
0239 
0240         if (EXSUCCEED!=Bchg(p_ub2, T_STRING_FLD, 0, "HELLO FROM INNER", 0L))
0241         {
0242             NDRX_LOG(log_error, "TESTERROR: Failed to set T_STRING_FLD");
0243             ret=EXFAIL;
0244             goto out;
0245         }
0246 
0247         if (EXSUCCEED!=Bchg(p_ub, T_PTR_FLD, 0, (char *)&p_ub2, 0L))
0248         {
0249             NDRX_LOG(log_error, "TESTERROR: Failed to set T_PTR_FLD");
0250             ret=EXFAIL;
0251             goto out;
0252         }
0253 
0254         if (EXFAIL==tpcall("TEST26_UBF2JSON", (char *)p_ub, 0L, (char **)&p_ub, &rsplen, 0))
0255         {
0256             NDRX_LOG(log_error, "TESTERROR: TEST26_UBF2JSON failed: %s", 
0257                     tpstrerror(tperrno));
0258             ret=EXFAIL;
0259             goto out;
0260         }
0261 
0262         NDRX_LOG(log_info, "Got response:");
0263         Bprint(p_ub);
0264 
0265         if (EXSUCCEED!=Bget(p_ub, T_STRING_FLD, 1, tmp, 0L))
0266         {
0267             NDRX_LOG(log_error, "TESTERROR: Failed to set T_STRING_FLD");
0268             ret=EXFAIL;
0269             goto out;
0270         }
0271 
0272         if (0!=strcmp(tmp, "HELLO FROM JSON!"))
0273         {
0274             NDRX_LOG(log_error, "TESTERROR: Invalid response [%s]!", tmp);
0275             ret=EXFAIL;
0276             goto out;
0277         }
0278 
0279         if (EXSUCCEED!=Bget(p_ub, T_PTR_FLD, 0, (char *)&p_ub2, 0L))
0280         {
0281             NDRX_LOG(log_error, "TESTERROR: Failed to get T_PTR_FLD");
0282             ret=EXFAIL;
0283             goto out;
0284         }
0285 
0286         if (EXSUCCEED!=Bget(p_ub2, T_STRING_FLD, 0, tmp, 0L))
0287         {
0288             NDRX_LOG(log_error, "TESTERROR: Failed to get T_STRING_FLD");
0289             ret=EXFAIL;
0290             goto out;
0291         }
0292 
0293         if (0!=strcmp(tmp, "HELLO FROM INNER"))
0294         {
0295             NDRX_LOG(log_error, "TESTERROR: Invalid response [%s]!", tmp);
0296             ret=EXFAIL;
0297             goto out;
0298         }
0299 
0300         if (EXSUCCEED!=Bget(p_ub, T_LONG_FLD, 0, (char *)&l, 0L))
0301         {
0302             NDRX_LOG(log_error, "TESTERROR: Failed to set T_STRING_FLD");
0303             ret=EXFAIL;
0304             goto out;
0305         }
0306 
0307         if (1001!=l)
0308         {
0309             NDRX_LOG(log_error, "TESTERROR: Invalid response [%ld]!", l);
0310             ret=EXFAIL;
0311             goto out;
0312         }
0313 
0314         tpfree((char *)p_ub);
0315     }
0316     
0317     NDRX_LOG(log_info, "Testing JSON -> UBF auto conv...");
0318     for (i=0; i<10000; i++)
0319     {
0320         char *json = tpalloc("JSON", NULL, 1024);
0321 
0322         if (NULL==json)
0323         {
0324             NDRX_LOG(log_error, "TESTERROR: failed to alloc JSON buffer!");
0325             EXFAIL_OUT(ret);
0326         }
0327         
0328         strcpy(json, "{\"T_STRING_FLD\":[\"HELLO UBF FROM JSON 1!\", \"HELLO UBF FROM JSON 2!\"]}");
0329 
0330         if (EXFAIL==tpcall("TEST26_JSON2UBF", (char *)json, 0L, (char **)&json, &rsplen, 0))
0331         {
0332             NDRX_LOG(log_error, "TESTERROR: TEST26_UBF2JSON failed: %s", 
0333                     tpstrerror(tperrno));
0334             ret=EXFAIL;
0335             goto out;
0336         }
0337 
0338         NDRX_LOG(log_info, "Got response: [%s]", json);
0339         
0340         if (0!=strcmp(json, "{\"T_STRING_FLD\":[\"HELLO UBF FROM JSON 1!\",\"HELLO UBF FROM JSON 2!\",\"HELLO FROM UBF!\"]}"))
0341         {
0342             NDRX_LOG(log_error, "TESTERROR: Invalid response [%s]", json);
0343             ret=EXFAIL;
0344             goto out;
0345         }
0346         tpfree((char *)json);
0347     }
0348     
0349     NDRX_LOG(log_info, "Testing invalid json request - shall got SVCERR!");
0350     for (i=0; i<10000; i++)
0351     {
0352         char *json = tpalloc("JSON", NULL, 1024);
0353 
0354         if (NULL==json)
0355         {
0356             NDRX_LOG(log_error, "TESTERROR: failed to alloc JSON buffer!");
0357             EXFAIL_OUT(ret);
0358         }
0359         
0360         strcpy(json, "Some ][ very bad json{");
0361 
0362         if (EXSUCCEED==tpcall("TEST26_JSON2UBF", (char *)json, 0L, (char **)&json, &rsplen, 0))
0363         {
0364             NDRX_LOG(log_error, "TESTERROR: TEST26_UBF2JSON must FAIL!");
0365             ret=EXFAIL;
0366             goto out;
0367         }
0368         
0369         if (tperrno!=TPESVCERR)
0370         {
0371             NDRX_LOG(log_error, "TESTERROR: Invalid error code [%d]", tperrno);
0372             ret=EXFAIL;
0373             goto out;
0374         }
0375 
0376         tpfree((char *)json);
0377     }
0378     
0379     
0380     
0381 out:
0382 
0383     if (ret>=0)
0384         ret=EXSUCCEED;
0385 
0386     return ret;
0387 }
0388 
0389 /* vim: set ts=4 sw=4 et smartindent: */