Back to home page

Enduro/X

 
 

    


0001 /**
0002  * Test of Bnext() UBF call
0003  * @file test_bnext.c
0004  */
0005 /* -----------------------------------------------------------------------------
0006  * Enduro/X Middleware Platform for Distributed Transaction Processing
0007  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0008  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0009  * This software is released under one of the following licenses:
0010  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0011  * See LICENSE file for full text.
0012  * -----------------------------------------------------------------------------
0013  * AGPL license:
0014  *
0015  * This program is free software; you can redistribute it and/or modify it under
0016  * the terms of the GNU Affero General Public License, version 3 as published
0017  * by the Free Software Foundation;
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 Affero General Public License, version 3
0022  * for more details.
0023  *
0024  * You should have received a copy of the GNU Affero General Public License along 
0025  * with this program; if not, write to the Free Software Foundation, Inc.,
0026  * 59 Temple 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 
0034 #include <stdio.h>
0035 #include <stdlib.h>
0036 #include <cgreen/cgreen.h>
0037 #include <ubf.h>
0038 #include <ndrstandard.h>
0039 #include <string.h>
0040 #include "test.fd.h"
0041 #include "ubfunit1.h"
0042 #include <fdatatype.h>
0043 
0044 
0045 void load_fdel_test_data(UBFH *p_ub)
0046 {
0047     short s = 88;
0048     long l = -1021;
0049     char c = 'c';
0050     float f = 17.31;
0051     double d = 12312.1111;
0052     char carr[] = "CARRAY1 TEST STRING DATA";
0053     BFLDLEN len = strlen(carr);
0054 
0055     assert_equal(Bchg(p_ub, T_SHORT_FLD, 0, (char *)&s, 0), EXSUCCEED);
0056     assert_equal(Bchg(p_ub, T_LONG_FLD, 0, (char *)&l, 0), EXSUCCEED);
0057     assert_equal(Bchg(p_ub, T_CHAR_FLD, 0, (char *)&c, 0), EXSUCCEED);
0058     assert_equal(Bchg(p_ub, T_FLOAT_FLD, 0, (char *)&f, 0), EXSUCCEED);
0059     assert_equal(Bchg(p_ub, T_DOUBLE_FLD, 0, (char *)&d, 0), EXSUCCEED);
0060     assert_equal(Bchg(p_ub, T_STRING_FLD, 0, (char *)"TEST STR VAL", 0), EXSUCCEED);
0061     assert_equal(Bchg(p_ub, T_CARRAY_FLD, 0, (char *)carr, len), EXSUCCEED);
0062 
0063     gen_load_ubf(p_ub, 0, 1, 0);
0064     gen_load_view(p_ub, 0, 1, 0);
0065     gen_load_ptr(p_ub, 0, 1, 0);
0066     
0067     /* Make second copy of field data (another for not equal test)*/
0068     s = 88;
0069     l = -1021;
0070     c = '.';
0071     f = 17.31;
0072     d = 12312.1111;
0073     carr[0] = 'Y';
0074     len = strlen(carr);
0075 
0076     assert_equal(Bchg(p_ub, T_SHORT_FLD, 1, (char *)&s, 0), EXSUCCEED);
0077     assert_equal(Bchg(p_ub, T_LONG_FLD, 1, (char *)&l, 0), EXSUCCEED);
0078     assert_equal(Bchg(p_ub, T_CHAR_FLD, 1, (char *)&c, 0), EXSUCCEED);
0079     assert_equal(Bchg(p_ub, T_FLOAT_FLD, 1, (char *)&f, 0), EXSUCCEED);
0080     assert_equal(Bchg(p_ub, T_DOUBLE_FLD, 1, (char *)&d, 0), EXSUCCEED);
0081     assert_equal(Bchg(p_ub, T_STRING_FLD, 1, (char *)"TEST STRING ARRAY2", 0), EXSUCCEED);
0082     assert_equal(Bchg(p_ub, T_CARRAY_FLD, 1, (char *)carr, len), EXSUCCEED);
0083     
0084     gen_load_ubf(p_ub, 1, 2, 0);
0085     gen_load_view(p_ub, 1, 2, 0);
0086     gen_load_ptr(p_ub, 1, 2, 0);
0087 
0088     s = 212;
0089     l = 212;
0090     c = 'b';
0091     f = 12127;
0092     d = 1231232.1;
0093     carr[0] = 'X';
0094     assert_equal(Bchg(p_ub, T_SHORT_2_FLD, 0, (char *)&s, 0), EXSUCCEED);
0095     assert_equal(Bchg(p_ub, T_LONG_2_FLD, 0, (char *)&l, 0), EXSUCCEED);
0096     assert_equal(Bchg(p_ub, T_CHAR_2_FLD, 0, (char *)&c, 0), EXSUCCEED);
0097     assert_equal(Bchg(p_ub, T_FLOAT_2_FLD, 0, (char *)&f, 0), EXSUCCEED);
0098     assert_equal(Bchg(p_ub, T_DOUBLE_2_FLD, 0, (char *)&d, 0), EXSUCCEED);
0099     assert_equal(Bchg(p_ub, T_STRING_2_FLD, 0, (char *)"XTEST STR VAL", 0), EXSUCCEED);
0100     assert_equal(Bchg(p_ub, T_CARRAY_2_FLD, 0, (char *)carr, len), EXSUCCEED);
0101     
0102     gen_load_ubf(p_ub, 0, 2, 1);
0103     gen_load_view(p_ub, 0, 2, 1);
0104     gen_load_ptr(p_ub, 0, 2, 1);
0105 }
0106 
0107 /**
0108  * This simply reads all field and adds them to another buffer, then do compare
0109  */
0110 Ensure(test_Bnext_simple)
0111 {
0112     char fb[1400];
0113     char fb_2[1400];
0114     UBFH *p_ub = (UBFH *)fb;
0115     UBFH *p_ub_2 = (UBFH *)fb_2;
0116     BFLDID bfldid;
0117     BFLDOCC occ;
0118     char data_buf[200];
0119     BVIEWFLD *vf;
0120     BFLDLEN  len;
0121     int fldcount=0;
0122     
0123     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0124     assert_equal(Binit(p_ub_2, sizeof(fb_2)), EXSUCCEED);
0125     load_fdel_test_data(p_ub);
0126     
0127     len = sizeof(data_buf);
0128     bfldid = BFIRSTFLDID;
0129     
0130     while(1==Bnext(p_ub, &bfldid, &occ, data_buf, &len))
0131     {
0132         assert_equal(Bchg(p_ub_2, bfldid, occ, data_buf, len), EXSUCCEED);
0133         /* Got the value? */
0134         len = sizeof(data_buf);
0135     }
0136     
0137     /* Now do the mem compare (this is not the best way to test,
0138      * but will be OK for now */
0139     assert_equal(Bcmp(p_ub, p_ub_2),0);
0140 
0141     /* Now test the count because buffer is NULL */
0142     bfldid = BFIRSTFLDID;
0143     while(1==Bnext(p_ub_2, &bfldid, &occ, NULL, NULL))
0144     {
0145         fldcount++;
0146     }
0147 
0148     assert_equal(fldcount, 30);
0149 }
0150 
0151 /**
0152  * This simply reads all field and adds them to another buffer, then do compare
0153  * operate with two buffer sets at the same time
0154  * also compare the values of dptr
0155  */
0156 Ensure(test_Bnext2_simple)
0157 {
0158     char fb[1400];
0159     char fb_2[1400];
0160     UBFH *p_ub = (UBFH *)fb;
0161     UBFH *p_ub_2 = (UBFH *)fb_2;
0162 
0163     char fb_3[1400];
0164     char fb_4[1400];
0165     UBFH *p_ub_3 = (UBFH *)fb_3;
0166     UBFH *p_ub_4 = (UBFH *)fb_4;
0167 
0168     BFLDID bfldid;
0169     BFLDID bfldid3;
0170     BFLDOCC occ;
0171     BFLDOCC occ3;
0172     char data_buf[200];
0173     BVIEWFLD *vf;
0174     BFLDLEN  len;
0175     char *dptr;
0176     int fldcount=0;
0177 
0178     Bnext_state_t s1, s2;
0179     
0180     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0181     assert_equal(Binit(p_ub_2, sizeof(fb_2)), EXSUCCEED);
0182     load_fdel_test_data(p_ub);
0183 
0184     assert_equal(Binit(p_ub_3, sizeof(fb_3)), EXSUCCEED);
0185     assert_equal(Binit(p_ub_4, sizeof(fb_4)), EXSUCCEED);
0186     load_fdel_test_data(p_ub_3);
0187     
0188     len = sizeof(data_buf);
0189     bfldid = BFIRSTFLDID;
0190     bfldid3 = BFIRSTFLDID;
0191 
0192     while(1==Bnext2(&s1, p_ub, &bfldid, &occ, data_buf, &len, NULL) &&
0193             1==Bnext2(&s2, p_ub_3, &bfldid3, &occ3, NULL, NULL, &dptr))
0194     {
0195         assert_equal(Bchg(p_ub_2, bfldid, occ, data_buf, len), EXSUCCEED);
0196 
0197         /* load from returned ptr: */
0198         assert_equal(Bchg(p_ub_4, bfldid3, occ3, dptr, len), EXSUCCEED);
0199 
0200         /* Got the value? */
0201         len = sizeof(data_buf);
0202     }
0203     
0204     /* Now do the mem compare (this is not the best way to test,
0205      * but will be OK for now */
0206     assert_equal(Bcmp(p_ub, p_ub_2),0);
0207     assert_equal(Bcmp(p_ub, p_ub_4),0);
0208 
0209     /* Now test the count because buffer is NULL */
0210     bfldid = BFIRSTFLDID;
0211     while(1==Bnext(p_ub_2, &bfldid, &occ, NULL, NULL))
0212     {
0213         fldcount++;
0214     }
0215 
0216     assert_equal(fldcount, 30);
0217 }
0218 
0219 /**
0220  * This simply reads all field and adds them to another buffer, then do compare
0221  */
0222 Ensure(test_Bnext_chk_errors)
0223 {
0224     char fb[1400];
0225     UBFH *p_ub = (UBFH *)fb;
0226     BFLDID bfldid;
0227     BFLDOCC occ;
0228     char data_buf[100];
0229     BFLDLEN  len;
0230 
0231     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0232     load_fdel_test_data(p_ub);
0233 
0234     bfldid = BFIRSTFLDID;
0235     assert_equal(Bnext(p_ub, NULL, &occ, data_buf, &len), EXFAIL);
0236     assert_equal(Berror, BEINVAL);
0237     assert_equal(Bnext(p_ub, &bfldid, NULL, data_buf, &len), EXFAIL);
0238     assert_equal(Berror, BEINVAL);
0239     
0240     bfldid = BFIRSTFLDID;
0241     len = sizeof(data_buf);
0242     assert_equal(Bnext(p_ub, &bfldid, &occ, data_buf, &len), 1);
0243     /* Now try again, but change the buffer */
0244     assert_equal(Bdel(p_ub, T_SHORT_2_FLD, 0), EXSUCCEED);
0245 
0246     /* Buffer have been changed */
0247     assert_equal(Bnext(p_ub, &bfldid, &occ, data_buf, &len), EXFAIL);
0248     assert_equal(Berror, BEINVAL);
0249 
0250     /* Now try again, but p_ub is NULL */
0251     bfldid = BFIRSTFLDID;
0252     assert_equal(Bnext(NULL, &bfldid, &occ, data_buf, &len), EXFAIL);
0253     assert_equal(Berror, BNOTFLD);
0254 }
0255 
0256 /**
0257  * Ensure that we return length even if output buffer is not present
0258  * Bug #341
0259  */
0260 Ensure(test_Bnext_len)
0261 {
0262     char fb[1400];
0263     UBFH *p_ub = (UBFH *)fb;
0264     BFLDID bfldid;
0265     BFLDOCC occ;
0266     BFLDLEN len;
0267     
0268     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0269     
0270     assert_equal(CBchg(p_ub, T_SHORT_FLD, 0, "1", 0L, BFLD_STRING), EXSUCCEED);
0271     assert_equal(CBchg(p_ub, T_LONG_FLD, 0, "1", 0L, BFLD_STRING), EXSUCCEED);
0272     assert_equal(CBchg(p_ub, T_CHAR_FLD, 0, "1", 0L, BFLD_STRING), EXSUCCEED);
0273     assert_equal(CBchg(p_ub, T_FLOAT_FLD, 0, "1", 0L, BFLD_STRING), EXSUCCEED);
0274     assert_equal(CBchg(p_ub, T_DOUBLE_FLD, 0, "1", 0L, BFLD_STRING), EXSUCCEED);
0275     assert_equal(CBchg(p_ub, T_STRING_FLD, 0, "123", 0L, BFLD_STRING), EXSUCCEED);
0276     assert_equal(CBchg(p_ub, T_CARRAY_FLD, 0, "123", 0L, BFLD_STRING), EXSUCCEED);
0277     
0278     gen_load_ubf(p_ub, 0, 1, 0);
0279     gen_load_view(p_ub, 0, 1, 0);
0280     gen_load_ptr(p_ub, 0, 1, 0);
0281     
0282     bfldid = BFIRSTFLDID;
0283     assert_equal(Bnext(p_ub, &bfldid, &occ, NULL, &len), 1);
0284     assert_equal(bfldid, T_SHORT_FLD);
0285     assert_equal(len, sizeof(short));
0286     
0287     assert_equal(Bnext(p_ub, &bfldid, &occ, NULL, &len), 1);
0288     assert_equal(bfldid, T_LONG_FLD);
0289     assert_equal(len, sizeof(long));
0290     
0291     assert_equal(Bnext(p_ub, &bfldid, &occ, NULL, &len), 1);
0292     assert_equal(bfldid, T_CHAR_FLD);
0293     assert_equal(len, sizeof(char));
0294     
0295     assert_equal(Bnext(p_ub, &bfldid, &occ, NULL, &len), 1);
0296     assert_equal(bfldid, T_FLOAT_FLD);
0297     assert_equal(len, sizeof(float));
0298     
0299     assert_equal(Bnext(p_ub, &bfldid, &occ, NULL, &len), 1);
0300     assert_equal(bfldid, T_DOUBLE_FLD);
0301     assert_equal(len, sizeof(double));
0302     
0303     assert_equal(Bnext(p_ub, &bfldid, &occ, NULL, &len), 1);
0304     assert_equal(bfldid, T_STRING_FLD);
0305     assert_equal(len, 4); /* + EOS*/
0306     
0307     assert_equal(Bnext(p_ub, &bfldid, &occ, NULL, &len), 1);
0308     assert_equal(bfldid, T_CARRAY_FLD);
0309     assert_equal(len, 3); /* + EOS*/
0310     
0311     assert_equal(Bnext(p_ub, &bfldid, &occ, NULL, &len), 1);
0312     assert_equal(bfldid, T_PTR_FLD);
0313     
0314 #ifdef SYS32BIT
0315     /* this is for 64bit machines: */
0316     assert_equal(len, 4); /* + EOS*/
0317 #else
0318      /* this is for 64bit machines: */
0319     assert_equal(len, 8); /* + EOS*/
0320 #endif
0321     
0322     assert_equal(Bnext(p_ub, &bfldid, &occ, NULL, &len), 1);
0323     assert_equal( (len > sizeof(UBF_header_t)), EXTRUE );
0324     
0325     /* some data in buffer */
0326     assert_equal( (len < sizeof(UBF_header_t) + 100), EXTRUE );
0327     
0328     assert_equal(Bnext(p_ub, &bfldid, &occ, NULL, &len), 1);
0329     assert_equal(bfldid, T_VIEW_FLD);
0330     assert_equal(len, sizeof(struct UBTESTVIEW2));
0331     
0332 }
0333 
0334 TestSuite *ubf_bnext_tests(void)
0335 {
0336     TestSuite *suite = create_test_suite();
0337 
0338     add_test(suite, test_Bnext_simple);
0339     add_test(suite, test_Bnext2_simple);
0340     add_test(suite, test_Bnext_chk_errors);
0341     add_test(suite, test_Bnext_len);
0342 
0343     return suite;
0344 }
0345 /* vim: set ts=4 sw=4 et smartindent: */