Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Buffer type switch + null - client
0003  *
0004  * @file atmiclt64.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 <ubf.h>
0042 #include <ndebug.h>
0043 #include <test.fd.h>
0044 #include <ndrstandard.h>
0045 #include <nstopwatch.h>
0046 #include <fcntl.h>
0047 #include <unistd.h>
0048 #include <nstdutil.h>
0049 #include "test64.h"
0050 #include <extest.h>
0051 #include <tpadm.h>
0052 #include <typed_buf.h>
0053 /*---------------------------Externs------------------------------------*/
0054 /*---------------------------Macros-------------------------------------*/
0055 /*---------------------------Enums--------------------------------------*/
0056 /*---------------------------Typedefs-----------------------------------*/
0057 /*---------------------------Globals------------------------------------*/
0058 /*---------------------------Statics------------------------------------*/
0059 /*---------------------------Prototypes---------------------------------*/
0060 
0061 /**
0062  * Universal service cross-tester
0063  * @param svc service to call
0064  * @param input_type input buffer to allocate (use "NULL", for none)
0065  * @param intput_sub buffer sub-type
0066  * @param output_type output buffer to expect (use "NULL", for none)
0067  * @param output_sub output buffer sub-type, expected
0068  * @param flags tpcall flags
0069  * @param tpcallerr expected tpcall error code
0070  * @param validate shall we perform data validation?
0071  * @return EXSUCCEED/EXFAIL
0072  */
0073 exprivate int tester(char *svc, char *input_type, char *input_sub, 
0074         char *output_type, char *output_sub, long flags, int tpcallerr, int validate)
0075 {
0076     char *data = NULL;
0077     long len = 0;
0078     int tpcallerrgot = 0;
0079     int ret = EXSUCCEED;
0080     char btype[16]="";
0081     char stype[16]="";
0082     
0083     NDRX_LOG(log_debug, "svc: [%s] input_type: [%s] input_sub: [%s] output_type: "
0084             "[%s] output_sub: [%s] flags: %ld tpcallerr: %d validate: %d",
0085             svc, input_type, input_sub, output_type, output_sub, flags, 
0086             tpcallerr, validate);
0087     
0088     if (0!=strcmp("NULL", input_type) &&
0089             NULL==(data = tpalloc(input_type, input_sub, 1024)))
0090     {
0091         NDRX_LOG(log_error, "Failed to alloc buffer!");
0092         EXFAIL_OUT(ret);
0093     }
0094     
0095     if (0==strcmp(input_type, "STRING"))
0096     {
0097         strcpy(data, "HELLO CLIENT");
0098     }
0099     else if (0==strcmp(input_type, "JSON"))
0100     {
0101         strcpy(data, "[]");
0102     }
0103     else if (0==strcmp(input_type, "CARRAY"))
0104     {
0105         strcpy(data, "HELLO CARRAY CLIENT");
0106         len = strlen(data);
0107     }
0108     else if (0==strcmp(input_type, "VIEW"))
0109     {
0110         struct UBTESTVIEW1 *vv = (struct UBTESTVIEW1 *)data;
0111         strcpy(vv->tstring3[2], "HELLO VIEW");
0112     }
0113     else if (0==strcmp(input_type, "UBF"))
0114     {
0115         if (EXSUCCEED!=Bchg((UBFH *)data, T_STRING_9_FLD, 4, "HELLO UBF CLIENT", 0L))
0116         {
0117             NDRX_LOG(log_error, "Failed to add T_STRING_9_FLD[4]: %s", 
0118                     Bstrerror(Berror));
0119             EXFAIL_OUT(ret);
0120         }
0121     }
0122     else if (0==strcmp(input_type, "NULL"))
0123     {
0124         /* nothing todo */
0125     }
0126     else
0127     {
0128         NDRX_LOG(log_error, "Unsupported buffer type [%s]", input_type);
0129         EXFAIL_OUT(ret);
0130     }
0131     
0132     /* call server process!!! */
0133     
0134     if (EXSUCCEED!=tpcall(svc, data, len, &data, &len, flags))
0135     {
0136         NDRX_LOG(log_error, "Failed to call %s: %s", svc, tpstrerror(tperrno));
0137         
0138         /* is it failure? */
0139         tpcallerrgot = tperrno;
0140     }
0141     
0142     if (tpcallerr!=tpcallerrgot)
0143     {
0144         NDRX_LOG(log_error, "TESTERROR! Expected %d error, but got: %d", 
0145                 tpcallerr, tpcallerrgot);
0146         EXFAIL_OUT(ret);
0147     }
0148     
0149     /* validate response!!!!! */
0150     
0151     /* get buffer types.. first... */
0152     
0153     NDRX_LOG(log_debug, "Response ptr: %p", data);
0154     
0155     if (EXFAIL==tptypes(data, btype, stype))
0156     {
0157         NDRX_LOG(log_error, "TESTERROR! Failed to read buffer %p type: %s",
0158                 data, tpstrerror(tperrno));
0159         EXFAIL_OUT(ret);
0160     }
0161     
0162     if (0!=strcmp(output_type, btype))
0163     {
0164         NDRX_LOG(log_error, "TESTERROR! Expected response type [%s] but got [%s]", 
0165                 output_type, btype);
0166         EXFAIL_OUT(ret);
0167     }
0168     
0169     if (0!=strcmp(output_sub, stype))
0170     {
0171         NDRX_LOG(log_error, "TESTERROR! Expected response sub-type [%s] but got [%s]", 
0172                 output_sub, stype);
0173         EXFAIL_OUT(ret);
0174     }
0175     
0176     if (!validate)
0177     {
0178         NDRX_LOG(log_debug, "No validate");
0179         goto out;
0180     }
0181     
0182     /* validate some values... */
0183     
0184     if (0==strcmp(output_type, "STRING"))
0185     {
0186         if (0!=strcmp(data, "WORLD"))
0187         {
0188             NDRX_LOG(log_error, "Test error, expected: [WORLD] got [%s]",
0189                     data);
0190             EXFAIL_OUT(ret);
0191         }
0192     }
0193     else if (0==strcmp(output_type, "JSON"))
0194     {
0195         if (0!=strcmp(data, "{}"))
0196         {
0197             NDRX_LOG(log_error, "Test error, expected: [{}] got [%s]",
0198                     data);
0199             EXFAIL_OUT(ret);
0200         }
0201     }
0202     else if (0==strcmp(output_type, "CARRAY"))
0203     {
0204         if (0!=strncmp(data, "SPACE", 5))
0205         {
0206             NDRX_LOG(log_error, "Test error, expected: [SPACE] got [%c%c%c%c%c]",
0207                     data[0],data[1],data[2],data[3],data[4]);
0208             EXFAIL_OUT(ret);
0209         }
0210     }
0211     else if (0==strcmp(output_type, "VIEW"))
0212     {
0213         struct UBTESTVIEW2 *v = (struct UBTESTVIEW2 *)data;
0214         
0215         if (0!=strcmp(v->tstring1, "TEST 55"))
0216         {
0217             NDRX_LOG(log_error, "Test error, expected: [TEST 55] got [%s]",
0218                     v->tstring1);
0219             EXFAIL_OUT(ret);
0220         }
0221         
0222     }
0223     else if (0==strcmp(output_type, "UBF"))
0224     {
0225         char tmp[128];
0226         
0227         if (EXSUCCEED!=Bget((UBFH *)data, T_STRING_FLD, 1, tmp, 0L))
0228         {
0229             NDRX_LOG(log_error, "TESTERROR ! Failed to get data!");
0230             EXFAIL_OUT(ret);
0231         }
0232         
0233         if (0!=strcmp(tmp, "HELLO WORLD"))
0234         {
0235             NDRX_LOG(log_error, "TESTERROR, expected: [HELLO WORLD] got [%s]",
0236                     tmp);
0237             EXFAIL_OUT(ret);
0238         }
0239     }
0240     else if (0==strcmp(output_type, "NULL"))
0241     {
0242         
0243     }
0244     else
0245     {
0246         NDRX_LOG(log_error, "Unsupported buffer type [%s]", input_type);
0247         EXFAIL_OUT(ret);
0248     }
0249     
0250 out:
0251     
0252     if (data!=NULL)
0253     {
0254         tpfree(data);
0255     }
0256 
0257     return ret;
0258 }
0259 
0260 /**
0261  * Do the test call to the server
0262  */
0263 int main(int argc, char** argv)
0264 {
0265     int i;
0266     int ret=EXSUCCEED;
0267     char *bufs[] = {"NULL", "JSON", "STRING", "CARRAY", "VIEW", "UBF", ""};
0268     char **p;
0269     char subtyp[16];
0270     char osubtyp[16];
0271     char svcnm[XATMI_SERVICE_NAME_LENGTH+1];
0272     ndrx_growlist_t list;
0273     if (argc < 2)
0274     {
0275         NDRX_LOG(log_error, "Usage: %s NULL|JSON|STRING|"
0276                 "CARRAY|VIEW|UBF", argv[0]);
0277         EXFAIL_OUT(ret);
0278     }
0279 
0280     /* test case by case 
0281      * crossvalidate all types
0282      */
0283     snprintf(svcnm, sizeof(svcnm), "%sRSP", argv[1]);
0284     
0285     if (0==strcmp(argv[1], "VIEW"))
0286     {
0287         NDRX_STRCPY_SAFE(osubtyp, "UBTESTVIEW2");
0288     }
0289     else
0290     {
0291         osubtyp[0]=EXEOS;
0292     }
0293     
0294     for (i=0; i<1000; i++)
0295     {
0296         p = bufs;
0297         
0298         while (*p[0]!=EXEOS)
0299         {
0300             int ret_err = 0;
0301             if (0==strcmp(*p, "VIEW"))
0302             {
0303                 NDRX_STRCPY_SAFE(subtyp, "UBTESTVIEW1");
0304             }
0305             else
0306             {
0307                 NDRX_STRCPY_SAFE(subtyp, "");
0308             }
0309             
0310             /* call with buffer switch - normal call */
0311             if (EXSUCCEED!=tester(svcnm, *p, subtyp, argv[1], osubtyp, 0L, 0, EXTRUE))
0312             {
0313                 NDRX_LOG(log_error, "1 fail");
0314                 EXFAIL_OUT(ret);
0315             }
0316             
0317             /* deny buffer type switch */
0318             if (0!=strcmp(argv[1], *p) ||
0319                     /* for views */
0320                     0==strcmp(*p, "VIEW")
0321                     )
0322             {
0323                 ret_err = TPEOTYPE;
0324             }
0325             
0326             if (EXSUCCEED!=tester(svcnm, *p, subtyp, *p, subtyp, TPNOCHANGE, 
0327                     ret_err, EXFALSE))
0328             {
0329                 NDRX_LOG(log_error, "2 fail");
0330                 EXFAIL_OUT(ret);
0331             }
0332             
0333             p++;
0334         }
0335     }
0336     
0337 out:
0338     
0339     /* Count the buffers left in the system (allocated)
0340      * they must be 0.
0341      */
0342     
0343     if (EXSUCCEED==ndrx_buffer_list(&list))
0344     {
0345         NDRX_LOG(log_debug, "buffers left: %d", list.maxindexused);
0346         
0347         if (list.maxindexused > -1)
0348         {
0349             NDRX_LOG(log_error, "TESTERROR! Buffers in system: %d", 
0350                     list.maxindexused+1);
0351             ret=EXFAIL;
0352         }
0353     }
0354     else
0355     {
0356         ret=EXFAIL;
0357     }
0358     
0359     tpterm();
0360     fprintf(stderr, "Exit with %d\n", ret);
0361 
0362     return ret;
0363 }
0364 
0365 /* vim: set ts=4 sw=4 et smartindent: */