Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief ATMI unit tests (functions, etc..)
0003  *
0004  * @file atmiunit0.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 
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <unistd.h>
0038 #include <cgreen/cgreen.h>
0039 #include <ubf.h>
0040 #include <ndrstandard.h>
0041 #include <string.h>
0042 #include "test.fd.h"
0043 /*#include "ubfunit1.h"*/
0044 #include "ndebug.h"
0045 #include "xatmi.h"
0046 #include <fdatatype.h>
0047 #include <typed_buf.h>
0048 
0049 extern TestSuite *atmiunit0_mbuf(void);
0050 extern TestSuite *atmiunit0_exproto(void);
0051 
0052 /**
0053  * Basic preparation before the test
0054  */
0055 exprivate void basic_setup(void)
0056 {
0057     
0058 }
0059 
0060 exprivate void basic_teardown(void)
0061 {
0062     
0063 }
0064 
0065 /**
0066  * Ensure that we free up buffer recursively
0067  */
0068 Ensure(test_recursfree)
0069 {
0070     UBFH *p_ub1;
0071     UBFH *p_ub2;
0072     UBFH *p_ub3;
0073     UBFH *p_ub4;
0074     ndrx_growlist_t list;
0075     
0076     ndrx_buffer_list(&list);
0077     
0078     assert_equal(list.maxindexused, -1);
0079     
0080     p_ub1=(UBFH *)tpalloc("UBF", NULL, 1024);
0081     assert_not_equal(p_ub1, NULL);
0082     
0083     p_ub2=(UBFH *)tpalloc("UBF", NULL, 1024);
0084     assert_not_equal(p_ub2, NULL);
0085     
0086     p_ub3=(UBFH *)tpalloc("UBF", NULL, 1024);
0087     assert_not_equal(p_ub3, NULL);
0088     
0089     p_ub4=(UBFH *)tpalloc("UBF", NULL, 1024);
0090     assert_not_equal(p_ub4, NULL);
0091     
0092     
0093     assert_equal(Bchg(p_ub2, T_PTR_FLD, 0, (char *)&p_ub4, 0), EXSUCCEED);
0094     assert_equal(Bchg(p_ub2, T_STRING_FLD, 0, "HELLO WORLD", 0), EXSUCCEED);
0095     assert_equal(tpsetcallinfo((char *)p_ub1, p_ub2, 0), 0);
0096     
0097     assert_equal(Bchg(p_ub2, T_PTR_FLD, 0, (char *)&p_ub3, 0), EXSUCCEED);
0098     
0099     assert_equal(Bchg(p_ub3, T_STRING_FLD, 0, "HELLO WORLD 2", 0), EXSUCCEED);
0100     assert_equal(Bchg(p_ub1, T_PTR_FLD, 0, (char *)&p_ub2, 0), EXSUCCEED);
0101     assert_equal(Bchg(p_ub1, T_PTR_FLD, 1, (char *)&p_ub2, 0), EXSUCCEED);
0102     assert_equal(Bchg(p_ub1, T_UBF_FLD, 1, (char *)p_ub2, 0), EXSUCCEED);
0103     
0104     ndrx_buffer_list(&list);
0105     /* includes callinfo buffer... ptr */
0106     assert_equal(list.maxindexused, 4);
0107     
0108     tpfree((char *)p_ub1);
0109     
0110     ndrx_buffer_list(&list);
0111     assert_equal(list.maxindexused, -1);
0112 }
0113 /**
0114  * Test call info buffer processing routines
0115  * Check different extended modes.
0116  */
0117 Ensure(test_tpcallinfo)
0118 {
0119     char *call_buf;
0120     UBFH *p_meta_ubf;
0121     UBFH *p_other_buf;
0122     char tmp[100];
0123     char tmp2[1024];
0124     BFLDLEN len;
0125     assert_equal(tpinit(NULL), EXSUCCEED);
0126     
0127     /* try to set call infos buffer */
0128     call_buf=tpalloc("STRING", NULL, 1024);
0129     assert_not_equal(call_buf, NULL);
0130     
0131     p_meta_ubf=(UBFH *)tpalloc("UBF", NULL, 1024);
0132     assert_not_equal(p_meta_ubf, NULL);
0133     
0134     p_other_buf=(UBFH *)tpalloc("STRING", NULL, 100);
0135     assert_not_equal(p_other_buf, NULL);
0136     
0137     /* set some fields... */
0138     assert_equal(Bchg(p_meta_ubf, T_STRING_FLD, 0, "HELLO", 0), EXSUCCEED);
0139     assert_equal(Bchg(p_meta_ubf, T_STRING_FLD, 5, "WORLD", 0), EXSUCCEED);
0140     
0141     assert_equal(tpgetcallinfo(call_buf, &p_other_buf, 0), EXFAIL);
0142     assert_equal(tperrno, TPESYSTEM);
0143 
0144     /* no error, if no association */
0145     assert_equal(tpgetcallinfo(call_buf, &p_other_buf, TPCI_NOEOFERR), 0);
0146     assert_equal(tperrno, 0);
0147 
0148     assert_equal(tpsetcallinfo(call_buf, p_meta_ubf, 0), EXSUCCEED);
0149     
0150     /* the output buffer shall be realloc'd to UBF and data filled */
0151     assert_equal(tpgetcallinfo(call_buf, &p_other_buf, 0), EXSUCCEED);
0152     len=sizeof(tmp);
0153     assert_equal(Bget(p_other_buf, T_STRING_FLD, 0, tmp, &len), EXSUCCEED);
0154     assert_string_equal(tmp, "HELLO");
0155 
0156     assert_equal(tpgetcallinfo(call_buf, &p_other_buf, TPCI_NOEOFERR), EXTRUE);
0157     len=sizeof(tmp);
0158     assert_equal(Bget(p_other_buf, T_STRING_FLD, 0, tmp, &len), EXSUCCEED);
0159     assert_string_equal(tmp, "HELLO");
0160     
0161     len=sizeof(tmp);
0162     assert_equal(Bget(p_other_buf, T_STRING_FLD, 5, tmp, &len), EXSUCCEED);
0163     assert_string_equal(tmp, "WORLD");
0164     
0165     /* the same shall work with NUL buffer too */
0166     tpfree((char *)p_other_buf);
0167     p_other_buf = NULL;
0168     
0169     assert_equal(tpgetcallinfo(call_buf, &p_other_buf, 0), EXSUCCEED);
0170     
0171     len=sizeof(tmp);
0172     assert_equal(Bget(p_other_buf, T_STRING_FLD, 0, tmp, &len), EXSUCCEED);
0173     assert_string_equal(tmp, "HELLO");
0174     
0175     len=sizeof(tmp);
0176     assert_equal(Bget(p_other_buf, T_STRING_FLD, 5, tmp, &len), EXSUCCEED);
0177     assert_string_equal(tmp, "WORLD");
0178     
0179     /* free up */
0180     tpfree((char *)p_other_buf);
0181     
0182     /* now normal case with UBF... */
0183     assert_equal(Binit(p_meta_ubf, Bsizeof(p_meta_ubf)), EXSUCCEED);
0184     len=sizeof(tmp);
0185     assert_equal(Bget(p_meta_ubf, T_STRING_FLD, 0, tmp, &len), EXFAIL);
0186     
0187     assert_equal(tpgetcallinfo(call_buf, &p_meta_ubf, 0), EXSUCCEED);
0188     
0189     len=sizeof(tmp);
0190     assert_equal(Bget(p_meta_ubf, T_STRING_FLD, 0, tmp, &len), EXSUCCEED);
0191     assert_string_equal(tmp, "HELLO");
0192     
0193     len=sizeof(tmp);
0194     assert_equal(Bget(p_meta_ubf, T_STRING_FLD, 5, tmp, &len), EXSUCCEED);
0195     assert_string_equal(tmp, "WORLD");
0196     
0197     
0198     /* check custom set buffer */
0199     
0200     p_other_buf=(UBFH *)tmp2;
0201     Binit(p_other_buf, sizeof(tmp2));
0202     assert_equal(Bchg(p_other_buf, T_STRING_10_FLD, 0, "10 test", 0), EXSUCCEED);
0203     
0204     assert_equal(tpsetcallinfo(call_buf, p_other_buf, 0), EXSUCCEED);
0205     
0206     Binit(p_other_buf, sizeof(tmp2));
0207     
0208     /* this shall fail -> unknown buffer */
0209     assert_equal(tpgetcallinfo(call_buf, &p_other_buf, 0), EXFAIL);
0210     assert_equal(tperrno, TPEINVAL);
0211 
0212     /* try to get to null / new */
0213     p_other_buf=NULL;
0214     assert_equal(tpgetcallinfo(call_buf, &p_other_buf, 0), EXSUCCEED);
0215     assert_equal(Bget(p_other_buf, T_STRING_10_FLD, 0, tmp, 0), EXSUCCEED);
0216     assert_string_equal(tmp, "10 test");
0217     
0218     /* free up other buf */
0219     tpfree((char *)p_other_buf);
0220     
0221     
0222     NDRX_LOG(log_info, "Testing errors...");
0223     
0224     assert_equal(tpsetcallinfo(NULL, p_meta_ubf, 0), EXFAIL);
0225     assert_equal(tperrno, TPEINVAL);
0226     
0227     assert_equal(tpgetcallinfo(NULL, &p_meta_ubf, 0), EXFAIL);
0228     assert_equal(tperrno, TPEINVAL);
0229     
0230     /* flags no 0 */
0231     assert_equal(tpsetcallinfo(call_buf, p_meta_ubf, 999), EXFAIL);
0232     assert_equal(tperrno, TPEINVAL);
0233     
0234     assert_equal(tpgetcallinfo(call_buf, &p_meta_ubf, 999), EXFAIL);
0235     assert_equal(tperrno, TPEINVAL);
0236 
0237     assert_equal(tpgetcallinfo(call_buf, &p_meta_ubf, 999|TPCI_NOEOFERR), EXFAIL);
0238     assert_equal(tperrno, TPEINVAL);
0239     
0240     /* check output buffer is NULL */
0241     assert_equal(tpgetcallinfo(call_buf, NULL, 0), EXFAIL);
0242     assert_equal(tperrno, TPEINVAL);
0243     
0244     assert_equal(tpsetcallinfo(call_buf, NULL, 0), EXFAIL);
0245     assert_equal(tperrno, TPEINVAL);
0246     
0247     tpfree(call_buf);
0248     tpfree((char *)p_meta_ubf);
0249     
0250     assert_equal(tpterm(), EXSUCCEED);
0251 }
0252 
0253 /**
0254  * Base ATMI tests
0255  * @return
0256  */
0257 TestSuite *atmiunit0_base(void)
0258 {
0259     TestSuite *suite = create_test_suite();
0260 
0261     add_test(suite, test_tpcallinfo);
0262     add_test(suite, test_recursfree);
0263     
0264     return suite;
0265 }
0266 
0267 /*
0268  * Main test entry.
0269  */
0270 int main(int argc, char** argv)
0271 {    
0272     TestSuite *suite = create_test_suite();
0273     int ret;
0274 
0275     add_suite(suite, atmiunit0_exproto());
0276     add_suite(suite, atmiunit0_base());
0277     add_suite(suite, atmiunit0_mbuf());
0278 
0279     if (argc > 1)
0280     {
0281         ret=run_single_test(suite,argv[1],create_text_reporter());
0282     }
0283     else
0284     {
0285         ret=run_test_suite(suite, create_text_reporter());
0286     }
0287 
0288     destroy_test_suite(suite);
0289 
0290     return ret;
0291 }
0292 
0293 /* vim: set ts=4 sw=4 et smartindent: */