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-2018, 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 3 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 
0033 #include <stdio.h>
0034 #include <stdlib.h>
0035 #include <cgreen/cgreen.h>
0036 #include <ubf.h>
0037 #include <ndrstandard.h>
0038 #include <string.h>
0039 #include "test.fd.h"
0040 #include "ubfunit1.h"
0041 
0042 
0043 /**
0044  * Test - Test buffer read/write
0045  */
0046 Ensure(test_readwrite)
0047 {
0048     char fb[1024];
0049     UBFH *p_ub = (UBFH *)fb;
0050     char fb2[1024];
0051     UBFH *p_ub2 = (UBFH *)fb2;
0052     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0053     assert_equal(Binit(p_ub2, sizeof(fb2)), EXSUCCEED);
0054 
0055     /* Load test stuff */
0056     set_up_dummy_data(p_ub);
0057     open_test_temp("wb");
0058 
0059     assert_equal(Bwrite(p_ub, M_test_temp_file), EXSUCCEED);
0060     close_test_temp();
0061 
0062     /* Now read the stuff in second buffer */
0063     open_test_temp_for_read("rb");
0064     assert_equal(Bread(p_ub2, M_test_temp_file), EXSUCCEED);
0065 
0066     close_test_temp();
0067     remove_test_temp();
0068 
0069     /* Now compare the buffers */
0070     assert_equal(memcmp(p_ub, p_ub2, Bused(p_ub)), 0);
0071     /* run check on data */
0072     do_dummy_data_test(p_ub2);
0073 }
0074 
0075 /**
0076  * Dest buffer from read too short
0077  */
0078 Ensure(test_readwrite_err_space)
0079 {
0080     char fb[1024];
0081     UBFH *p_ub = (UBFH *)fb;
0082     char fb2[128];
0083     UBFH *p_ub2 = (UBFH *)fb2;
0084     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0085     assert_equal(Binit(p_ub2, sizeof(fb2)), EXSUCCEED);
0086 
0087     /* Load test stuff */
0088     set_up_dummy_data(p_ub);
0089     open_test_temp("wb");
0090 
0091     assert_equal(Bwrite(p_ub, M_test_temp_file), EXSUCCEED);
0092     close_test_temp();
0093 
0094     /* Now read the stuff in second buffer */
0095     open_test_temp_for_read("rb");
0096     assert_equal(Bread(p_ub2, M_test_temp_file), EXFAIL);
0097     assert_equal(Berror, BNOSPACE);
0098 
0099     close_test_temp();
0100     remove_test_temp();
0101 }
0102 
0103 /**
0104  * Test unix error on bad file descriptor
0105  */
0106 Ensure(test_readwrite_invalid_descr)
0107 {
0108     char fb[1024];
0109     UBFH *p_ub = (UBFH *)fb;
0110     char fb2[128];
0111     UBFH *p_ub2 = (UBFH *)fb2;
0112     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0113     assert_equal(Binit(p_ub2, sizeof(fb2)), EXSUCCEED);
0114 
0115     /* Load test stuff */
0116     set_up_dummy_data(p_ub);
0117     open_test_temp("r");
0118 
0119     assert_equal(Bwrite(p_ub, M_test_temp_file), EXFAIL);
0120     assert_equal(Berror, BEUNIX);
0121     close_test_temp();
0122 
0123     /* Now read the stuff in second buffer */
0124     open_test_temp_for_read("w");
0125     assert_equal(Bread(p_ub2, M_test_temp_file), EXFAIL);
0126     assert_equal(Berror, BEUNIX);
0127 
0128     close_test_temp();
0129     remove_test_temp();
0130 }
0131 
0132 TestSuite *ubf_readwrite_tests(void)
0133 {
0134     TestSuite *suite = create_test_suite();
0135 
0136     add_test(suite, test_readwrite);
0137     add_test(suite, test_readwrite_err_space);
0138     add_test(suite, test_readwrite_invalid_descr);
0139 
0140     return suite;
0141 }
0142