Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Typed VIEW testing
0003  *
0004  * @file atmiclt40.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-2018, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * GPL or Mavimax's license for commercial use.
0012  * -----------------------------------------------------------------------------
0013  * GPL license:
0014  * 
0015  * This program is free software; you can redistribute it and/or modify it under
0016  * the terms of the GNU General Public License as published by the Free Software
0017  * Foundation; either version 3 of the License, or (at your option) any later
0018  * version.
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 General Public License for more details.
0023  *
0024  * You should have received a copy of the GNU General Public License along with
0025  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
0026  * Place, Suite 330, Boston, MA 02111-1307 USA
0027  *
0028  * -----------------------------------------------------------------------------
0029  * A commercial use license is available from Mavimax, Ltd
0030  * contact@mavimax.com
0031  * -----------------------------------------------------------------------------
0032  */
0033 #include <string.h>
0034 #include <stdio.h>
0035 #include <stdlib.h>
0036 #include <memory.h>
0037 #include <math.h>       /* fabs */
0038 
0039 #include <atmi.h>
0040 #include <ubf.h>
0041 #include <ndebug.h>
0042 #include <test.fd.h>
0043 #include <ndrstandard.h>
0044 #include "test040.h"
0045 #include "tperror.h"
0046 
0047 /*---------------------------Externs------------------------------------*/
0048 /*---------------------------Macros-------------------------------------*/
0049 /*---------------------------Enums--------------------------------------*/
0050 /*---------------------------Typedefs-----------------------------------*/
0051 /*---------------------------Globals------------------------------------*/
0052 /*---------------------------Statics------------------------------------*/
0053 /*---------------------------Prototypes---------------------------------*/
0054 
0055 /**
0056  * Test json2view and vice versa
0057  * @return 
0058  */
0059 int test_view2json(void)
0060 {
0061     int ret = EXSUCCEED;
0062     struct MYVIEW1 v1;
0063     char msg[NDRX_MSGSIZEMAX+1];
0064     char *abuf = NULL;
0065     char view[NDRX_VIEW_NAME_LEN+1];
0066     char *testbuf = "{\"MYVIEW2\":{\"tshort1\":1,\"tlong1\":2,\"tchar1\":\"A\",\""
0067     "tfloat1\":1,\"tdouble1\":21,\"tstring1\":\"ABC\",\""
0068     "tcarray1\":\"SEVMTE8AAAAAAA==\"}}";
0069 
0070     char *t3buf_null = "{\"MYVIEW3\":{\"tshort1\":0,\"tshort2\":0,\"tshort3\":0}}";
0071     char *t3buf_nnull = "{\"MYVIEW3\":{}}";
0072     
0073     struct MYVIEW2 *v2;
0074     struct MYVIEW3 v3;
0075     
0076     init_MYVIEW1(&v1);
0077 
0078     if (EXSUCCEED!=tpviewtojson((char *)&v1, "MYVIEW1", msg, sizeof(msg), 
0079                              BVACCESS_NOTNULL))
0080     {
0081         NDRX_LOG(log_error, "TESTERROR: tpviewtojson() failed: %s", 
0082                  tpstrerror(tperrno));
0083         EXFAIL_OUT(ret);
0084     }
0085 
0086     memset(&v1, 0, sizeof(v1));
0087 
0088     NDRX_LOG(log_debug, "Got json: [%s]", msg);
0089 
0090     if (NULL==(abuf=tpjsontoview(view, msg)))
0091     {
0092         NDRX_LOG(log_error, "TESTERROR: Failed to get view from JSON: %s", 
0093                  tpstrerror(tperrno));
0094         EXFAIL_OUT(ret);
0095     }
0096 
0097     /* test structure... */
0098     if (EXSUCCEED!=validate_MYVIEW1((struct MYVIEW1 *)abuf))
0099     {
0100         NDRX_LOG(log_error, "TESTERROR: Failed to validate view recovery!");
0101         EXFAIL_OUT(ret);
0102     }
0103 
0104 
0105     tpfree(abuf);
0106     abuf = NULL;
0107 
0108     /* test full convert... */
0109     init_MYVIEW1(&v1);
0110 
0111     msg[0] = EXEOS;
0112     if (EXSUCCEED!=tpviewtojson((char *)&v1, "MYVIEW1", msg, sizeof(msg), 0))
0113     {
0114         NDRX_LOG(log_error, "TESTERROR: tpviewtojson() failed: %s", 
0115                  tpstrerror(tperrno));
0116         EXFAIL_OUT(ret);
0117     }
0118 
0119     NDRX_LOG(log_debug, "Got json2: [%s]", msg);
0120 
0121     if (NULL==(abuf=tpjsontoview(view, msg)))
0122     {
0123         NDRX_LOG(log_error, "TESTERROR: Failed to get view from JSON: %s", 
0124                  tpstrerror(tperrno));
0125         EXFAIL_OUT(ret);
0126     }
0127 
0128     /* test structure... */
0129     if (EXSUCCEED!=validate_MYVIEW1((struct MYVIEW1 *)abuf))
0130     {
0131         NDRX_LOG(log_error, "TESTERROR: Failed to validate view recovery!");
0132         EXFAIL_OUT(ret);
0133     }
0134     
0135     tpfree(abuf);
0136     abuf=NULL;
0137 
0138 
0139     /* test manual convert */
0140 
0141     if (NULL==(abuf=tpjsontoview(view, testbuf)))
0142     {
0143         NDRX_LOG(log_error, "TESTERROR: Failed to get view from JSON: %s", 
0144                  tpstrerror(tperrno));
0145         EXFAIL_OUT(ret);
0146     }
0147 
0148     v2 = (struct MYVIEW2 *)abuf;
0149 
0150     if (1!=v2->tshort1)
0151     {
0152         NDRX_LOG(log_error, "TESTERROR: tshort1 got %hd expected 1", 
0153                  v2->tshort1);
0154         EXFAIL_OUT(ret);
0155     }
0156 
0157     if (2!=v2->tlong1)
0158     {
0159         NDRX_LOG(log_error, "TESTERROR: tlong1 got %hd expected 2", 
0160                  v2->tlong1);
0161         EXFAIL_OUT(ret);
0162     }
0163 
0164     if ('A'!=v2->tchar1)
0165     {
0166         NDRX_LOG(log_error, "TESTERROR: tchar1 got %c expected A", 
0167                  v2->tchar1);
0168         EXFAIL_OUT(ret);
0169     }
0170 
0171     if (fabs(v2->tfloat1 - 1.0f) > 0.1)
0172     {
0173         NDRX_LOG(log_error, "TESTERROR: tfloat1 got %f expected 1", 
0174                  v2->tfloat1);
0175         EXFAIL_OUT(ret);
0176     }
0177 
0178     if ((v2->tdouble1 - 21.0f) > 0.1)
0179     {
0180         NDRX_LOG(log_error, "TESTERROR: tdouble1 got %lf expected 21", 
0181                  v2->tdouble1);
0182         EXFAIL_OUT(ret);
0183     }
0184 
0185     if (0!=strcmp(v2->tstring1, "ABC"))
0186     {
0187         NDRX_LOG(log_error, "TESTERROR: tstring1 got [%s] expected ABC", 
0188                  v2->tdouble1);
0189         EXFAIL_OUT(ret);
0190     }
0191 
0192     if (0!=strncmp(v2->tcarray1, "HELLO", 5))
0193     {
0194         NDRX_LOG(log_error, "TESTERROR: tstring1 got [%10s] expected "
0195                 "[HELLO]", 
0196                  v2->tdouble1); 
0197         EXFAIL_OUT(ret);
0198     }
0199 
0200 
0201     msg[0] = EXEOS;
0202 
0203     if (EXSUCCEED!=tpviewtojson((char *)v2, "MYVIEW2", msg, sizeof(msg), 0))
0204     {
0205         NDRX_LOG(log_error, "TESTERROR: tpviewtojson() failed: %s", 
0206                  tpstrerror(tperrno));
0207         EXFAIL_OUT(ret);
0208     }
0209 
0210     NDRX_LOG(log_debug, "v2 json: [%s]", msg);
0211 
0212     if (0!=strcmp(msg, testbuf))
0213     {
0214         NDRX_LOG(log_error, "TESTERROR: Built json: [%s] expected [%s]", 
0215                  msg, testbuf);
0216         EXFAIL_OUT(ret);
0217     }
0218     
0219     /* test error cases */
0220     tpfree(abuf);
0221     abuf = NULL;
0222 
0223     
0224     if (NULL!=(abuf=tpjsontoview(view, "HELLO WORLD")))
0225     {
0226         NDRX_LOG(log_error, "TESTERROR: Failure must occur - invalid json");
0227         EXFAIL_OUT(ret);
0228     }
0229     
0230     if (TPEINVAL!=tperrno)
0231     {
0232         NDRX_LOG(log_error, "TESTERROR: invalid error on json parse: "
0233                 "expected TPEINVAL, got: %s",
0234                 tpstrerror(tperrno));
0235         EXFAIL_OUT(ret);
0236     }
0237     
0238     if (NULL!=(abuf=tpjsontoview(view, "{\"NONEXIST\":{}}")))
0239     {
0240         NDRX_LOG(log_error, "TESTERROR: Failure must occur - invalid json");
0241         EXFAIL_OUT(ret);
0242     }
0243     
0244     if (TPEINVAL!=tperrno)
0245     {
0246         NDRX_LOG(log_error, "TESTERROR: invalid error on json parse: "
0247                 "expected TPEINVAL, got: %s",
0248                 tpstrerror(tperrno));
0249         EXFAIL_OUT(ret);
0250     }
0251     
0252     /* Test case for NULL field... */
0253     
0254     if (EXSUCCEED!=Bvsinit((char *)&v3, "MYVIEW3"))
0255     {
0256         NDRX_LOG(log_error, "TESTERROR: MYVIEW3 failed to init: %s", 
0257                 Bstrerror(Berror));
0258         EXFAIL_OUT(ret);
0259     }
0260     
0261     if (EXSUCCEED!=tpviewtojson((char *)&v3, "MYVIEW3", msg, sizeof(msg), 0))
0262     {
0263         NDRX_LOG(log_error, "TESTERROR: tpviewtojson() failed: %s", 
0264                  tpstrerror(tperrno));
0265         EXFAIL_OUT(ret);
0266     }
0267 
0268     NDRX_LOG(log_debug, "v3 json: [%s]", msg);
0269 
0270     if (0!=strcmp(msg, t3buf_null))
0271     {
0272         NDRX_LOG(log_error, "TESTERROR: Built json: [%s] expected [%s]", 
0273                  msg, t3buf_null);
0274         EXFAIL_OUT(ret);
0275     }
0276     
0277     
0278     if (EXSUCCEED!=tpviewtojson((char *)&v3, "MYVIEW3", msg, sizeof(msg), 
0279             BVACCESS_NOTNULL))
0280     {
0281         NDRX_LOG(log_error, "TESTERROR: tpviewtojson() failed: %s", 
0282                  tpstrerror(tperrno));
0283         EXFAIL_OUT(ret);
0284     }
0285 
0286     NDRX_LOG(log_debug, "v3 json: [%s]", msg);
0287 
0288     if (0!=strcmp(msg, t3buf_nnull))
0289     {
0290         NDRX_LOG(log_error, "TESTERROR: Built json: [%s] expected [%s]", 
0291                  msg, t3buf_nnull);
0292         EXFAIL_OUT(ret);
0293     }
0294  
0295     if (NULL==(abuf=tpjsontoview(view, msg)))
0296     {
0297         NDRX_LOG(log_error, "TESTERROR: failed to parse empty json: %s", 
0298                 Bstrerror(Berror));
0299         EXFAIL_OUT(ret);
0300     }
0301     
0302     tpfree(abuf);
0303     abuf = NULL;
0304     
0305     
0306 out:
0307     if (NULL!=abuf)
0308     {
0309         tpfree(abuf);
0310     }
0311     
0312     return ret;
0313 }
0314 
0315 /**
0316  * Call JSON service (TEST40_V2JSON) with VIEW buffer
0317  * @return 
0318  */
0319 int test_x_view2json(void)
0320 {
0321     int ret = EXSUCCEED;
0322     struct MYVIEW3 *v3 = NULL;
0323     long rsplen;
0324     
0325     v3 = (struct MYVIEW3 *)tpalloc("VIEW", "MYVIEW3", 0);
0326     
0327     if (NULL==v3)
0328     {
0329         NDRX_LOG(log_error, "TESTERROR: Failed to allocate MYVIEW3: %s", 
0330                 tpstrerror(tperrno));
0331         EXFAIL_OUT(ret);
0332     }
0333     
0334     v3->tshort1 = 1;
0335     v3->tshort2 = 2;
0336     v3->tshort3 = 3;
0337     
0338     if (EXSUCCEED!=tpcall("TEST40_V2JSON", (char *) v3, 0, 
0339             (char **)&v3, &rsplen, 0))
0340     {
0341         NDRX_LOG(log_error, "TESTERROR: failed to call TEST40_V2JSON");
0342         EXFAIL_OUT(ret);
0343     }
0344     
0345     if (4!=v3->tshort1)
0346     {
0347         NDRX_LOG(log_error, "TESTERROR: tshort1 must be 4 but must be %hd",
0348                 v3->tshort1);
0349         EXFAIL_OUT(ret);
0350     }
0351     
0352     if (5!=v3->tshort2)
0353     {
0354         NDRX_LOG(log_error, "TESTERROR: tshort2 must be 5 but must be %hd",
0355                 v3->tshort2);
0356         EXFAIL_OUT(ret);
0357     }
0358     
0359     if (6!=v3->tshort3)
0360     {
0361         NDRX_LOG(log_error, "TESTERROR: tshort3 must be 6 but must be %hd",
0362                 v3->tshort3);
0363         EXFAIL_OUT(ret);
0364     }
0365     
0366 out:
0367     
0368     if (NULL!=v3)
0369     {
0370         tpfree((char *)v3);
0371     }
0372 
0373     return ret;
0374 }
0375 
0376 /**
0377  * Call VIEW service with JSON (TEST40_JSON2V)
0378  * @return 
0379  */
0380 int test_x_json2view(void)
0381 {
0382     int ret = EXSUCCEED;
0383     long rsplen;
0384     char *buf;
0385     
0386     char *input = "{\"MYVIEW3\":{\"tshort1\":6,\"tshort2\":7,\"tshort3\":8}}";
0387     char *output = "{\"MYVIEW3\":{\"tshort1\":9,\"tshort2\":1,\"tshort3\":2}}";
0388     
0389     buf = tpalloc("JSON", NULL, 1024);
0390     
0391     if (NULL==buf)
0392     {
0393         NDRX_LOG(log_error, "TESTERROR: Failed to allocate MYVIEW3: %s", 
0394                 tpstrerror(tperrno));
0395         EXFAIL_OUT(ret);
0396     }
0397     
0398     strcpy(buf, input);
0399     
0400     if (EXSUCCEED!=tpcall("TEST40_JSON2V", buf, 0, &buf, &rsplen, 0))
0401     {
0402         NDRX_LOG(log_error, "TESTERROR: failed to call TEST40_JSON2V: %s",
0403                 tpstrerror(tperrno));
0404         EXFAIL_OUT(ret);
0405     }
0406     
0407     if (0!=strcmp(buf, output))
0408     {
0409         NDRX_LOG(log_error, "TESTERROR: Received [%s] expected [%s]", 
0410                 buf, output);
0411         EXFAIL_OUT(ret);
0412     }
0413     
0414 out:
0415     
0416     if (NULL!=buf)
0417     {
0418         tpfree(buf);
0419     }
0420 
0421     return ret;
0422 }
0423 
0424 /*
0425  * Do the test call to the server
0426  */
0427 int main(int argc, char** argv) {
0428 
0429     long rsplen;
0430     int i, j;
0431     int ret=EXSUCCEED;
0432     
0433     struct MYVIEW1 *v1;
0434     struct MYVIEW3 *v3;
0435     
0436     if (EXSUCCEED!=tpinit(NULL))
0437     {
0438         NDRX_LOG(log_error, "TESTERROR: failed to init: %s", tpstrerror(tperrno));
0439         EXFAIL_OUT(ret);
0440     }
0441     
0442     for (i=0; i<1000; i++)
0443     {
0444         if (EXSUCCEED!=test_x_json2view())
0445         {
0446             NDRX_LOG(log_error, "TESTERROR: test_x_json2view() fail!");
0447             EXFAIL_OUT(ret);
0448         }
0449     }
0450     
0451     
0452     for (i=0; i<1000; i++)
0453     {
0454         if (EXSUCCEED!=test_x_view2json())
0455         {
0456             NDRX_LOG(log_error, "TESTERROR: test_x_view2json() fail!");
0457             EXFAIL_OUT(ret);
0458         }
0459     }
0460     
0461     /* view ops with json */
0462     
0463     for (i=0; i<10000; i++)
0464     {
0465         if (EXSUCCEED!=test_view2json())
0466         {
0467             NDRX_LOG(log_error, "TESTERROR: JSON2VIEW failed!");
0468             EXFAIL_OUT(ret);
0469         }
0470     }
0471 
0472     /* View ops with services */
0473     for (j=0; j<1000; j++)
0474     {
0475         
0476         v1= (struct MYVIEW1 *) tpalloc("VIEW", "MYVIEW1", sizeof(struct MYVIEW1));
0477         if (NULL==v1)
0478         {
0479             NDRX_LOG(log_error, "TESTERROR: failed to alloc VIEW buffer!");
0480             EXFAIL_OUT(ret);
0481         }
0482 
0483         init_MYVIEW1(v1);
0484         
0485         NDRX_DUMP(log_debug, "VIEW1 request...", v1, sizeof(struct MYVIEW1));
0486 
0487         if (EXSUCCEED!=tpcall("TEST40_VIEW", (char *)v1, 0, (char **)&v1, &rsplen, 0))
0488         {
0489             NDRX_LOG(log_error, "TESTERROR: failed to call TEST40_VIEW");
0490             EXFAIL_OUT(ret);
0491         }
0492         
0493         v3 = (struct MYVIEW3 *)v1;
0494         if (EXSUCCEED!=validate_MYVIEW3((v3)))
0495         {
0496             NDRX_LOG(log_error, "Failed to validate V3!");
0497             EXFAIL_OUT(ret);
0498         }
0499         
0500         tpfree((char *)v3);
0501     }
0502     
0503 out:
0504 
0505     if (ret>=0)
0506         ret=EXSUCCEED;
0507 
0508     return ret;
0509 }
0510