Back to home page

Enduro/X

 
 

    


0001 /**
0002  * Print tests for view
0003  * @file test_printv.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 <unistd.h>
0041 #include "test.fd.h"
0042 #include "ubfunit1.h"
0043 #include "ndebug.h"
0044 #include "atmi_int.h"
0045 
0046 /**
0047  * Test - test plus command
0048  */
0049 Ensure(test_bvextread_plus)
0050 {
0051     struct UBTESTVIEW2 v;
0052     
0053     /* Check with sub-fields */
0054     char *test_plus[]= {
0055         "tshort1\t1\n",
0056     "tlong1\t2\n",
0057     "+ tshort1\t2\n",
0058     "+ tlong1\t99999\n",
0059         NULL
0060     };
0061     
0062     memset(&v, 0, sizeof(v));
0063     
0064     /* load field table */
0065     load_field_table();
0066 
0067     open_test_temp("w");
0068     write_to_temp(test_plus);
0069     close_test_temp();
0070 
0071     open_test_temp_for_read("r");
0072     assert_equal(Bvextread((char *)&v, "UBTESTVIEW2", M_test_temp_file), EXSUCCEED);
0073     close_test_temp();
0074     /* now open the file */
0075     remove_test_temp();
0076     
0077     /* check the view contents  for those fields shall be 2 & 3*/
0078     assert_equal(v.tshort1, 2);
0079     assert_equal(v.tlong1, 99999);
0080 }
0081 
0082 
0083 /**
0084  * Test - test minus command
0085  */
0086 Ensure(test_bvextread_minus)
0087 {
0088     struct UBTESTVIEW2 v;
0089     
0090     /* Check with sub-fields */
0091     char *test_plus[]= {
0092         "tshort1\t8888\n",
0093     "tlong1\t2\n",
0094     "- tshort1\t\n",
0095     "- tlong1\t\n",
0096         NULL
0097     };
0098     
0099     memset(&v, 0, sizeof(v));
0100     
0101     /* load field table */
0102     load_field_table();
0103 
0104     open_test_temp("w");
0105     write_to_temp(test_plus);
0106     close_test_temp();
0107 
0108     open_test_temp_for_read("r");
0109     assert_equal(Bvextread((char *)&v, "UBTESTVIEW2", M_test_temp_file), EXSUCCEED);
0110     close_test_temp();
0111     /* now open the file */
0112     remove_test_temp();
0113     
0114     /* check the view contents  for those fields shall be 2 & 3*/
0115     assert_equal(v.tshort1, 2000);
0116     assert_equal(v.tlong1, 5);
0117 }
0118 
0119 
0120 /**
0121  * Test - test assign command
0122  */
0123 Ensure(test_bvextread_assign)
0124 {
0125     struct UBTESTVIEW2 v;
0126     
0127     /* Check with sub-fields */
0128     char *test_plus[]= {
0129         "tshort1\t8888\n",
0130     "tlong1\t7771\n",
0131     "= tshort1\ttlong1\n",
0132         NULL
0133     };
0134     
0135     memset(&v, 0, sizeof(v));
0136     
0137     /* load field table */
0138     load_field_table();
0139 
0140     open_test_temp("w");
0141     write_to_temp(test_plus);
0142     close_test_temp();
0143 
0144     open_test_temp_for_read("r");
0145     assert_equal(Bvextread((char *)&v, "UBTESTVIEW2", M_test_temp_file), EXSUCCEED);
0146     close_test_temp();
0147     /* now open the file */
0148     remove_test_temp();
0149     
0150     /* check the view contents  for those fields shall be 2 & 3*/
0151     assert_equal(v.tshort1, 7771);
0152     
0153 }
0154 
0155 
0156 /**
0157  * Test Bvfprint
0158  */
0159 Ensure(test_bvfprint)
0160 {
0161     FILE *f;
0162     int fstdout;
0163     char filename[]="/tmp/view-test-XXXXXX";
0164     
0165     struct UBTESTVIEW1 v1;
0166     struct UBTESTVIEW1 v2;
0167 
0168     assert_not_equal(mkstemp(filename), EXFAIL);
0169     
0170     memset(&v1, 0, sizeof(v1));
0171     memset(&v2, 0, sizeof(v2));
0172     
0173     extest_init_UBTESTVIEW1(&v1);
0174     /*
0175     Bvprint((char *)&v1, "UBTESTVIEW1");
0176     
0177     printf("EOF\n");
0178     */
0179     
0180     /* nothing much to test here... */
0181     /*close(1);  close stdout */
0182     assert_not_equal((f=fopen(filename, "w")), NULL);
0183     /* fstdout = dup2(fileno(f), 1); make file appear as stdout */
0184     assert_equal(Bvfprint((char *)&v1, "UBTESTVIEW1", f), NULL);
0185     fclose(f);
0186 
0187     /* OK, if we have that output, try to extread it! */
0188     assert_not_equal((f=fopen(filename, "r")), NULL);
0189 
0190     /*
0191     Bvprint((char *)&v2, "UBTESTVIEW1");
0192     */
0193     Bvprint((char *)&v1, "UBTESTVIEW1");
0194     
0195     
0196     assert_equal(Bvextread((char *)&v2, "UBTESTVIEW1", f), EXSUCCEED);
0197     /* compare read buffer */
0198     assert_equal(Bvcmp((char *)&v1, "UBTESTVIEW1", (char *)&v2, "UBTESTVIEW1"), 0);
0199     /* Remove test file */
0200     assert_equal(unlink(filename), EXSUCCEED);
0201 }
0202 
0203 /**
0204  * Test bvprint
0205  * There is special note for this!
0206  * If running in single test mode, then STDOUT will be lost!
0207  */
0208 Ensure(test_bvprint)
0209 {
0210     FILE *f;
0211     int fstdout;
0212     char filename[]="/tmp/view-test-XXXXXX";
0213     struct UBTESTVIEW1 v1;
0214     struct UBTESTVIEW1 v2;
0215 
0216     
0217     memset(&v1, 0, sizeof(v1));
0218     memset(&v2, 0, sizeof(v2));
0219     extest_init_UBTESTVIEW1(&v1);
0220     
0221     assert_not_equal(mkstemp(filename), EXFAIL);
0222     
0223     /* nothing much to test here... */
0224     close(1); /* close stdout */
0225     assert_not_equal((f=fopen(filename, "w")), NULL);
0226     fstdout = dup2(fileno(f), 1); /* make file appear as stdout */
0227     assert_equal(Bvprint((char *)&v1, "UBTESTVIEW1"), NULL);
0228     fclose(f);
0229 
0230     /* OK, if we have that output, try to extread it! */
0231     assert_not_equal((f=fopen(filename, "r")), NULL);
0232 
0233     assert_equal(Bvextread((char *)&v2, "UBTESTVIEW1", f), EXSUCCEED);
0234     /* compare read buffer */
0235     assert_equal(Bvcmp((char *)&v1, "UBTESTVIEW1", (char *)&v2, "UBTESTVIEW1"), 0);
0236     /* Remove test file */
0237     assert_equal(unlink(filename), EXSUCCEED);
0238 }
0239 
0240 
0241 /**
0242  * Test data holder for bfprintcb_data
0243  */
0244 typedef struct bfprintcb_data bfprintcb_data_t;
0245 struct bfprintcb_data
0246 {
0247     int nrlines;
0248     int curline;
0249     char lines[1024][100];
0250 };
0251 
0252 /**
0253  * Write callback, this will fill in passed array
0254  * @param buffer, we may realloc
0255  * @param datalen output data len
0256  * @param dataptr1 custom pointer
0257  * @param do_write shall the Enduro/X wirte to output log..
0258  * @param outf output stream currencly used
0259  * @param fid field it processing
0260  * @return 
0261  */
0262 exprivate int test_bfprintcb_writef(char **buffer, long datalen, void *dataptr1, 
0263         int *do_write, FILE *outf, BFLDID fid)
0264 {
0265     bfprintcb_data_t *data = (bfprintcb_data_t *)dataptr1;
0266     
0267     assert_equal(strlen(*buffer)+1, datalen);
0268     
0269     NDRX_STRCPY_SAFE(data->lines[data->nrlines], *buffer);
0270     data->nrlines++;
0271     return EXSUCCEED;
0272 }
0273 
0274 /**
0275  * Return the buffer line
0276  * @param buffer buffer to put in the result. Note that is should go line by line
0277  * @param bufsz buffer size
0278  * @param dataptr1 user data ptr
0279  * @return number of bytes written to buffer
0280  */
0281 exprivate long bextreadcb_readf(char *buffer, long bufsz, void *dataptr1)
0282 {
0283     bfprintcb_data_t *data = (bfprintcb_data_t *)dataptr1;
0284     
0285     if (data->curline < data->nrlines)
0286     {
0287         NDRX_STRCPY_SAFE_DST(buffer, data->lines[data->curline], bufsz);
0288         data->curline++;
0289         
0290         return strlen(buffer)+1;
0291         
0292     }
0293     else
0294     {
0295         return 0;
0296     }
0297 }
0298 
0299 /**
0300  * Bvfprintcb testing (i.e. callback testing...)
0301  * @return
0302  */
0303 Ensure(test_bvfprintcb)
0304 {
0305     bfprintcb_data_t data;
0306     int line_counter=0;
0307 
0308     struct UBTESTVIEW1 v1;
0309     struct UBTESTVIEW1 v2;
0310 
0311     
0312     memset(&v1, 0, sizeof(v1));
0313     memset(&v2, 0, sizeof(v2));
0314     extest_init_UBTESTVIEW1(&v1);
0315     
0316     memset(&data, 0, sizeof(data));
0317     
0318     assert_equal(Bvfprintcb((char *)&v1, "UBTESTVIEW1", 
0319         test_bfprintcb_writef, (void *)&data), EXSUCCEED);
0320     
0321     UBF_LOG(log_error, "Bvfprintcb: %s", Bstrerror(Berror));
0322     
0323     /* ok now read back from callback.. */
0324     assert_equal(Bvextreadcb((char *)&v2, "UBTESTVIEW1", bextreadcb_readf, (char *)&data), EXSUCCEED);
0325     
0326     assert_equal(Bvcmp((char *)&v1, "UBTESTVIEW1", (char *)&v2, "UBTESTVIEW1"), 0);
0327     
0328 }
0329 
0330 
0331 TestSuite *ubf_printv_tests(void)
0332 {
0333     TestSuite *suite = create_test_suite();
0334 
0335     std_basic_setup();
0336     
0337     add_test(suite, test_bvprint);
0338     add_test(suite, test_bvfprint);
0339     add_test(suite, test_bvfprintcb);
0340     add_test(suite, test_bvextread_plus);
0341     add_test(suite, test_bvextread_minus);
0342     add_test(suite, test_bvextread_assign);
0343     return suite;
0344 }
0345 /* vim: set ts=4 sw=4 et smartindent: */