Back to home page

Enduro/X

 
 

    


0001 /**
0002  *
0003  * @file test_readwrite.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 <ndebug.h>
0043 #include <ubfutil.h>
0044 
0045 
0046 /**
0047  * Test - Test buffer read/write
0048  */
0049 Ensure(test_readwrite)
0050 {
0051     char fb[2048];
0052     UBFH *p_ub = (UBFH *)fb;
0053     char fb2[2048];
0054     UBFH *p_ub2 = (UBFH *)fb2;
0055 
0056     /* needs to set padding to some known value */
0057     memset(fb, 7, sizeof(fb));
0058     memset(fb2, 7, sizeof(fb2));
0059 
0060     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0061     assert_equal(Binit(p_ub2, sizeof(fb2)), EXSUCCEED);
0062 
0063     /* Load test stuff */
0064     set_up_dummy_data(p_ub);
0065     open_test_temp("wb");
0066 
0067     assert_equal(Bwrite(p_ub, M_test_temp_file), EXSUCCEED);
0068     close_test_temp();
0069 
0070     /* Now read the stuff in second buffer */
0071     open_test_temp_for_read("rb");
0072     assert_equal(Bread(p_ub2, M_test_temp_file), EXSUCCEED);
0073 
0074     close_test_temp();
0075     remove_test_temp();
0076 
0077     /* Now compare the buffers */
0078     assert_equal(memcmp(p_ub, p_ub2, Bused(p_ub)), 0);
0079     /* run check on data */
0080     do_dummy_data_test(p_ub2);
0081 }
0082 
0083 /**
0084  * Dest buffer from read too short
0085  */
0086 Ensure(test_readwrite_err_space)
0087 {
0088     char fb[2048];
0089     UBFH *p_ub = (UBFH *)fb;
0090     char fb2[128];
0091     UBFH *p_ub2 = (UBFH *)fb2;
0092 
0093     memset(fb, 7, sizeof(fb));
0094     memset(fb2, 7, sizeof(fb2));
0095 
0096     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0097     assert_equal(Binit(p_ub2, sizeof(fb2)), EXSUCCEED);
0098 
0099     /* Load test stuff */
0100     set_up_dummy_data(p_ub);
0101     open_test_temp("wb");
0102 
0103     assert_equal(Bwrite(p_ub, M_test_temp_file), EXSUCCEED);
0104     close_test_temp();
0105 
0106     /* Now read the stuff in second buffer */
0107     open_test_temp_for_read("rb");
0108     assert_equal(Bread(p_ub2, M_test_temp_file), EXFAIL);
0109     assert_equal(Berror, BNOSPACE);
0110 
0111     close_test_temp();
0112     remove_test_temp();
0113 }
0114 
0115 /**
0116  * Test unix error on bad file descriptor
0117  */
0118 Ensure(test_readwrite_invalid_descr)
0119 {
0120     char fb[2048];
0121     UBFH *p_ub = (UBFH *)fb;
0122     char fb2[128];
0123     UBFH *p_ub2 = (UBFH *)fb2;
0124 
0125     memset(fb, 7, sizeof(fb));
0126     memset(fb2, 7, sizeof(fb2));
0127 
0128     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0129     assert_equal(Binit(p_ub2, sizeof(fb2)), EXSUCCEED);
0130 
0131     /* Load test stuff */
0132     set_up_dummy_data(p_ub);
0133     open_test_temp("r");
0134 
0135     assert_equal(Bwrite(p_ub, M_test_temp_file), EXFAIL);
0136     assert_equal(Berror, BEUNIX);
0137     close_test_temp();
0138 
0139     /* Now read the stuff in second buffer */
0140     open_test_temp_for_read("w");
0141     assert_equal(Bread(p_ub2, M_test_temp_file), EXFAIL);
0142     assert_equal(Berror, BEUNIX);
0143 
0144     close_test_temp();
0145     remove_test_temp();
0146 }
0147 
0148 /**
0149  * Place where to put the read/write data
0150  */
0151 exprivate char M_temp_space[2048];
0152 exprivate int M_cur_offset;
0153 
0154 /**
0155  * Write function to buffer
0156  * @param buffer buffer received
0157  * @param bufsz buffer to write
0158  * @param dataptr1 extra ptr must be 239
0159  * @return  number of bytes written
0160  */
0161 
0162 exprivate long test_writef(char *buffer, long bufsz, void *dataptr1)
0163 {
0164     memcpy(M_temp_space+M_cur_offset, buffer, bufsz);
0165     
0166     M_cur_offset+=(int)bufsz;
0167     
0168     assert_equal(dataptr1, (char *)675);
0169     
0170     return bufsz;
0171 }
0172 
0173 /**
0174  * Read data from buffer
0175  * @param buffer buffer to read from
0176  * @param bufsz data size to fill in the buffer / requested
0177  * @param dataptr1 optional ptr must be 675
0178  * @return 
0179  */
0180 exprivate long test_readf(char *buffer, long bufsz, void *dataptr1)
0181 {
0182     memcpy(buffer, M_temp_space+M_cur_offset, bufsz);
0183     
0184     M_cur_offset+=(int)bufsz;
0185     
0186     assert_equal(dataptr1, (char *)239);
0187     
0188     return bufsz;
0189 }
0190 
0191 /**
0192  * Perform testing of Brwritecb() and Breadcb()
0193  */
0194 Ensure(test_readwrite_callbacked)
0195 {
0196     char fb[2048];
0197     UBFH *p_ub = (UBFH *)fb;
0198 
0199     memset(fb, 1, sizeof(fb));
0200     
0201     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0202 
0203     /* Load test stuff */
0204     set_up_dummy_data(p_ub);
0205     
0206     M_cur_offset = 0;
0207     assert_equal(Bwritecb(p_ub, test_writef, (char *)675), EXSUCCEED);
0208     
0209     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0210     
0211     M_cur_offset = 0;
0212     assert_equal(Breadcb(p_ub, test_readf, (char *)239), EXSUCCEED);
0213     
0214     do_dummy_data_test(p_ub);
0215 }
0216 
0217 /**
0218  * Test that BFLD_PTR is stripped in recursive way
0219  */
0220 Ensure(test_readwrite_no_ptr)
0221 {
0222     char fb[2048];
0223     UBFH *p_ub = (UBFH *)fb;
0224     
0225     char fb2[2048];
0226     UBFH *p_ub2 = (UBFH *)fb2;
0227     
0228     char fb3[2048];
0229     UBFH *p_ub3 = (UBFH *)fb3;
0230     
0231     char fb4[2048];
0232     UBFH *p_ub4 = (UBFH *)fb4;
0233 
0234     BFLDLEN len;
0235     unsetenv("NDRX_APIFLAGS");
0236 
0237     memset(fb, 0, sizeof(fb));
0238     memset(fb2, 0, sizeof(fb2));
0239     memset(fb3, 0, sizeof(fb3));
0240     memset(fb4, 0, sizeof(fb4));
0241 
0242     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0243     assert_equal(Binit(p_ub2, sizeof(fb2)), EXSUCCEED);
0244     
0245     assert_equal(Binit(p_ub3, sizeof(fb3)), EXSUCCEED);
0246     assert_equal(Binit(p_ub4, sizeof(fb4)), EXSUCCEED);
0247     
0248     assert_equal(Bchg(p_ub4, T_STRING_9_FLD, 0, "HELLO1", 0), EXSUCCEED);
0249     assert_equal(Bchg(p_ub4, T_STRING_10_FLD, 0, "HELLO2", 0), EXSUCCEED);
0250     assert_equal(CBchg(p_ub4, T_PTR_FLD, 3, "999", 0, BFLD_STRING), EXSUCCEED);
0251     
0252     assert_equal(Bchg(p_ub3, T_UBF_FLD, 2, (char *)p_ub4, 0), EXSUCCEED);
0253     assert_equal(CBchg(p_ub3, T_PTR_FLD, 1, "888", 0, BFLD_STRING), EXSUCCEED);
0254     assert_equal(Bchg(p_ub3, T_STRING_8_FLD, 2, "HELLO3", 0), EXSUCCEED);
0255     
0256     assert_equal(Bchg(p_ub, T_UBF_FLD, 4, (char *)p_ub3, 0), EXSUCCEED);
0257     assert_equal(CBchg(p_ub, T_PTR_FLD, 5, "1231", 0, BFLD_STRING), EXSUCCEED);
0258     assert_equal(Bchg(p_ub, T_STRING_7_FLD, 2, "HELLO4", 0), EXSUCCEED);
0259     
0260     open_test_temp("wb");
0261 
0262     assert_equal(Bwrite(p_ub, M_test_temp_file), EXSUCCEED);
0263     close_test_temp();
0264 
0265     /* Now read the stuff in second buffer */
0266     open_test_temp_for_read("rb");
0267     assert_equal(Bread(p_ub2, M_test_temp_file), EXSUCCEED);
0268 
0269     close_test_temp();
0270     remove_test_temp();
0271 
0272     /* Now compare the buffers 
0273     
0274     Bprint(p_ub);
0275     printf("**************\n");
0276     Bprint(p_ub2);
0277     */
0278     
0279     assert_not_equal(memcmp(p_ub, p_ub2, Bused(p_ub)), 0);
0280     
0281     /* buffer 1 */
0282     assert_string_equal(Bfindrv (p_ub2, &len, 
0283             T_STRING_7_FLD, 2, BBADFLDOCC), "HELLO4");
0284     assert_equal(Bfindrv (p_ub2, &len,
0285             T_PTR_FLD, 0, BBADFLDOCC), NULL);
0286     assert_equal(Berror, BNOTPRES);
0287     
0288     assert_string_equal(Bfindrv (p_ub2, &len, 
0289             T_UBF_FLD, 4, T_STRING_8_FLD, 2, BBADFLDOCC), "HELLO3");
0290     assert_equal(Bfindrv (p_ub2, &len,
0291             T_UBF_FLD, 4, T_PTR_FLD, 0, BBADFLDOCC), NULL);
0292     assert_equal(Berror, BNOTPRES);
0293     
0294     
0295     assert_string_equal(Bfindrv (p_ub2, &len, 
0296             T_UBF_FLD, 4, T_UBF_FLD, 2, T_STRING_10_FLD, 0, BBADFLDOCC), "HELLO2");
0297     
0298     assert_string_equal(Bfindrv (p_ub2, &len, 
0299             T_UBF_FLD, 4, T_UBF_FLD, 2, T_STRING_9_FLD, 0, BBADFLDOCC), "HELLO1");
0300     
0301     assert_equal(Bfindrv (p_ub2, &len,
0302             T_UBF_FLD, 4, T_UBF_FLD, 2, T_PTR_FLD, 0, BBADFLDOCC), NULL);
0303     assert_equal(Berror, BNOTPRES);
0304     
0305 }
0306 
0307 TestSuite *ubf_readwrite_tests(void)
0308 {
0309     TestSuite *suite = create_test_suite();
0310 
0311     add_test(suite, test_readwrite);
0312     add_test(suite, test_readwrite_err_space);
0313     add_test(suite, test_readwrite_invalid_descr);
0314     add_test(suite, test_readwrite_callbacked);
0315     
0316     /* Test PTR stripping... unsetenv("NDRX_APIFLAGS"); */
0317     add_test(suite, test_readwrite_no_ptr);
0318     
0319     return suite;
0320 }
0321 
0322 /* vim: set ts=4 sw=4 et smartindent: */