Back to home page

Enduro/X

 
 

    


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