Back to home page

Enduro/X

 
 

    


0001 /**
0002  *
0003  * @file ubfunit1.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 <unistd.h>
0037 #include <cgreen/cgreen.h>
0038 #include <ubf.h>
0039 #include <ndrstandard.h>
0040 #include <string.h>
0041 #include "test.fd.h"
0042 #include "test1.fd.h"
0043 #include "ubfunit1.h"
0044 #include "ndebug.h"
0045 #include <fdatatype.h>
0046 #include <math.h>
0047 #include <fdatatype.h>
0048 #define DEFAULT_BUFFER  128
0049 UBFH *M_p_ub = NULL;
0050 
0051 
0052 char M_test_temp_filename[]="/tmp/ubf-test-com-XXXXXX";
0053 FILE *M_test_temp_file=NULL;
0054 int M_has_init = EXFALSE;
0055 
0056 /**
0057  * Open test file for write
0058  */
0059 void open_test_temp(char *mode)
0060 {
0061     NDRX_STRCPY_SAFE(M_test_temp_filename, "/tmp/ubf-test-com-XXXXXX");
0062     assert_not_equal(mkstemp(M_test_temp_filename), EXFAIL);
0063     assert_not_equal((M_test_temp_file=fopen(M_test_temp_filename, mode)), NULL);
0064 }
0065 
0066 /**
0067  * Open temp file for reading
0068  */
0069 void open_test_temp_for_read(char *mode)
0070 {
0071     assert_not_equal((M_test_temp_file=fopen(M_test_temp_filename, mode)), NULL);
0072 }
0073 
0074 /**
0075  * Write to temp file
0076  * @param data
0077  */
0078 void write_to_temp(char **data)
0079 {
0080     int i;
0081     for (i=0; NULL!=data[i]; i++)
0082     {
0083         assert_equal(fwrite (data[i] , 1 , strlen(data[i]) , M_test_temp_file ), strlen(data[i]));
0084     }
0085 }
0086 
0087 /**
0088  * Close temp file
0089  */
0090 void close_test_temp(void)
0091 {
0092     fclose(M_test_temp_file);
0093 }
0094 
0095 /**
0096  * Remove temp file
0097  */
0098 void remove_test_temp(void)
0099 {
0100     /* Remove test file */
0101     assert_equal(unlink(M_test_temp_filename), EXSUCCEED);
0102 }
0103 
0104 /**
0105  * Set up field table environment
0106  */
0107 void load_field_table(void)
0108 {
0109     if (!M_has_init)
0110     {
0111         setenv("FLDTBLDIR", "./ubftab", 1);
0112         setenv("FIELDTBLS", "test.fd,Exfields", 1);
0113 
0114         setenv("VIEWDIR", "../libextest", 1);
0115         setenv("VIEWFILES", "test_view.V", 1);
0116         M_has_init=EXTRUE;
0117     }
0118     
0119 }
0120 
0121 /**
0122  * Basic preparation before the test
0123  */
0124 void std_basic_setup(void)
0125 {
0126     /* set view env... */
0127     load_field_table();
0128 
0129     setenv("NDRX_APIFLAGS", "ubf_ptrparse", 1);
0130 }
0131 
0132 /**
0133  * Fill up buffer with something so that we have more interesting stuff there
0134  * bigger buffer, etc... (incl ptr, view, ubf)
0135  * @param p_ub
0136  */
0137 void set_up_dummy_data(UBFH *p_ub)
0138 {
0139     load_field_table();
0140     extest_ubf_set_up_dummy_data(p_ub, EXTEST_PROC_PTR|EXTEST_PROC_UBF|EXTEST_PROC_VIEW);
0141 }
0142 
0143 /**
0144  * Test the dummy data (incl ptr, view, ubf)
0145  * @param p_ub ubf buffer to validate
0146  */
0147 void do_dummy_data_test(UBFH *p_ub)
0148 {
0149     extest_ubf_do_dummy_data_test(p_ub, EXTEST_PROC_PTR|EXTEST_PROC_UBF|EXTEST_PROC_VIEW);
0150 }
0151 
0152 /**
0153  * Load ptr data
0154  * @param p_ub buffer where to load
0155  * @param occ occurrence of data
0156  * @param offset data offset (value to change from base)
0157  * @param fldoff field offset (different field set)
0158  */
0159 void gen_load_ptr_dbg(char *file, int line, UBFH *p_ub, BFLDOCC occ, int offset, BFLDID32 fldoff)
0160 {
0161     long ptr=9000+offset;   
0162     UBF_LOG(log_debug, "Asserting %s:%d, start", file, line);
0163     assert_equal(CBchg(p_ub, T_PTR_FLD+fldoff, occ, (char *)&ptr, 0, BFLD_LONG), EXSUCCEED);
0164     UBF_LOG(log_debug, "Asserting %s:%d, done", file, line);
0165 }
0166 
0167 /**
0168  * Set view data, load into static..
0169  * @param file dbg file
0170  * @param line db line
0171  * @param vf view field struct for output
0172  * @param offset data offset for field..
0173  */
0174 void gen_set_view_dbg(char *file, int line, BVIEWFLD *vf, int offset)
0175 {
0176     static struct UBTESTVIEW2 v;
0177     char str[2];
0178     
0179     memset(&v, 9, sizeof(v));
0180     v.tshort1=1+offset;
0181     v.tlong1=2+offset;
0182     v.tchar1='3'+offset;
0183     v.tfloat1=4+offset;
0184     v.tdouble1=5+offset;
0185     str[0]='A'+offset;
0186     str[1]=EXEOS;
0187     
0188     NDRX_STRCPY_SAFE(v.tstring1, str);
0189     str[0]='C'+offset;
0190     str[1]=EXEOS;
0191     
0192     NDRX_STRCPY_SAFE(v.tcarray1, str);
0193     memset(vf, 0, sizeof(BVIEWFLD));
0194     vf->data=(char *)&v;
0195     vf->vflags=0;
0196     NDRX_STRCPY_SAFE(vf->vname, "UBTESTVIEW2");
0197     
0198 }
0199 
0200 /**
0201  * Load test data for view
0202  * @param p_ub buffer where to load
0203  * @param occ occurrence of data
0204  * @param offset data offset (value to change from base)
0205  * @param fldoff field offset (different field set)
0206  */
0207 void gen_load_view_dbg(char *file, int line, UBFH *p_ub, BFLDOCC occ, int offset, BFLDID32 fldoff)
0208 {
0209     BVIEWFLD vf;
0210     
0211     UBF_LOG(log_debug, "Loading %s:%d, start", file, line);
0212     
0213     gen_set_view_dbg(file, line, &vf, offset);
0214     
0215     assert_equal(Bchg(p_ub, T_VIEW_FLD+fldoff, occ, (char *)&vf, 0L), EXSUCCEED);
0216     UBF_LOG(log_debug, "Loading %s:%d, end", file, line);
0217 }
0218 
0219 /**
0220  * Setup the UBF buffer for value
0221  * @param file source file
0222  * @param line source line
0223  * @param p_ub_tmp buffer to init
0224  * @param offset data offset
0225  */
0226 void gen_set_ubf_dbg(char *file, int line, UBFH *p_ub_tmp, int offset)
0227 {
0228     char str[2];
0229     str[0]='S'+offset;
0230     str[1]=EXEOS;
0231     assert_equal(Bchg(p_ub_tmp, T_STRING_FLD+offset, 0, str, 0), EXSUCCEED);   
0232 }
0233 
0234 /**
0235  * Load test data for ubf
0236  * @param p_ub buffer where to load
0237  * @param occ occurrence of data
0238  * @param offset data offset (value to change from base)
0239  * @param fldoff field offset (different field set)
0240  */
0241 void gen_load_ubf_dbg(char *file, int line, UBFH *p_ub, BFLDOCC occ, int offset, BFLDID32 fldoff)
0242 {
0243     char tmp[1024];
0244     UBFH* p_ub_tmp=(UBFH*)tmp;
0245     memset(tmp, 17, sizeof(tmp));
0246     
0247     
0248     UBF_LOG(log_debug, "Loading UBF %s:%d, start", file, line);
0249     
0250     assert_equal(Binit(p_ub_tmp, sizeof(tmp)), EXSUCCEED);
0251     
0252     /* Load some sub-field */
0253     gen_set_ubf_dbg(file, line, p_ub_tmp, offset);
0254     assert_equal(Bchg(p_ub, T_UBF_FLD+fldoff, occ, (char *)p_ub_tmp, 0), EXSUCCEED);
0255     UBF_LOG(log_debug, "Loading UBF %s:%d, end", file, line);
0256 }
0257 
0258 /**
0259  * Validate ptr data
0260  * @param p_ub
0261  * @param occ
0262  * @param offset
0263  * @param fldoff UBF field offset
0264  */
0265 void gen_test_ptr_dbg(char *file, int line, UBFH *p_ub, BFLDOCC occ, int offset, BFLDID32 fldoff)
0266 {
0267     ndrx_longptr_t ptr;
0268     BFLDLEN len=sizeof(ptr);
0269     assert_equal(CBget(p_ub, T_PTR_FLD+fldoff, occ, (char *)&ptr, &len, BFLD_PTR), EXSUCCEED);
0270     gen_test_ptr_val_dbg(file, line, ptr, offset, &len);
0271 }
0272 
0273 
0274 /**
0275  * Validate the data is OK
0276  * @param p_ub
0277  * @param occ
0278  * @param offset
0279  * @param fldoff UBF field offset
0280  */
0281 void gen_test_view_dbg(char *file, int line, UBFH *p_ub, BFLDOCC occ, int offset, BFLDID32 fldoff)
0282 {
0283     struct UBTESTVIEW2 v;
0284     BVIEWFLD vf;
0285     BFLDLEN len;
0286     
0287     memset(&v, 13, sizeof(v));
0288 
0289     UBF_LOG(log_debug, "Asserting %s:%d, start", file, line);
0290     
0291     /* View test */
0292     vf.data=(char *)&v;
0293     len=sizeof(v)-1;
0294     assert_equal(Bget(p_ub, T_VIEW_FLD+fldoff, occ, (char *)&vf, &len), EXFAIL);
0295     assert_equal(Berror, BNOSPACE);
0296     
0297     len=sizeof(v);
0298     assert_equal(Bget(p_ub, T_VIEW_FLD+fldoff, occ, (char *)&vf, &len), EXSUCCEED);
0299     
0300     /* Check value of view fields... */
0301     gen_test_view_val_dbg(file, line, &vf, offset, &len);
0302     
0303     UBF_LOG(log_debug, "Asserting %s:%d, end", file, line);
0304 }
0305 
0306 
0307 /**
0308  * Validate the ubf data is OK
0309  * @param p_ub
0310  * @param occ
0311  * @param offset
0312  * @param fldoff UBF field offset
0313  */
0314 void gen_test_ubf_dbg(char *file, int line, UBFH *p_ub, BFLDOCC occ, int offset, BFLDID32 fldoff)
0315 {
0316     char tmp[1024];
0317     UBFH* p_ub_tmp=(UBFH*)tmp;
0318     BFLDLEN len;
0319     
0320     UBF_LOG(log_debug, "Asserting %s:%d, start", file, line);
0321     
0322     /* UBF test */
0323     len=1;
0324     assert_equal(Bget(p_ub, T_UBF_FLD+fldoff, occ, (char *)p_ub_tmp, &len), EXFAIL);
0325     assert_equal(Berror, BNOSPACE);
0326     
0327     len=sizeof(tmp);
0328     assert_equal(Bget(p_ub, T_UBF_FLD+fldoff, occ, (char *)p_ub_tmp, &len), EXSUCCEED);
0329     
0330     
0331     gen_test_ubf_val_dbg(file, line, p_ub_tmp, offset, &len);
0332     
0333     
0334     UBF_LOG(log_debug, "Asserting %s:%d, end", file, line);
0335 }
0336 
0337 /**
0338  * Test ptr value
0339  * @param file src file
0340  * @param line src line
0341  * @param ptr data ptr
0342  * @param offset data offset to test
0343  * @param len optional len
0344  */
0345 void gen_test_ptr_val_dbg(char *file, int line, ndrx_longptr_t ptr,  int offset, BFLDLEN *len)
0346 {
0347     UBF_LOG(log_debug, "Asserting %s:%d, start", file, line);
0348     
0349     if (NULL!=len)
0350     {
0351         assert_equal(*len, sizeof(ndrx_longptr_t));
0352     }
0353     
0354     assert_equal(ptr, 9000+offset);
0355     UBF_LOG(log_debug, "Asserting %s:%d, end", file, line);
0356 }
0357 
0358 /**
0359  * Validate the data is OK
0360  * @param p_ub
0361  * @param occ
0362  * @param offset
0363  * @param fldoff UBF field offset
0364  * @param len ptr to optional len
0365  */
0366 void gen_test_view_val_dbg(char *file, int line, BVIEWFLD *vf, int offset, BFLDLEN *len)
0367 {
0368     struct UBTESTVIEW2 *v=(struct UBTESTVIEW2 *)vf->data;
0369     char str[2];
0370     
0371     UBF_LOG(log_debug, "Asserting %s:%d, start", file, line);
0372     
0373     if (NULL!=len)
0374     {
0375         assert_equal(*len, sizeof(struct UBTESTVIEW2));
0376     }
0377 
0378     /* Check value of view fields... */
0379     
0380     assert_equal(v->tshort1, 1+offset);
0381     assert_equal(v->tlong1, 2+offset);
0382     assert_equal(v->tchar1, '3'+offset);
0383     assert_equal(v->tfloat1, 4+offset);
0384     assert_equal(v->tdouble1, 5+offset);
0385     
0386     str[0]='A'+offset;
0387     str[1]=EXEOS;
0388     assert_string_equal(v->tstring1,str);
0389     
0390     str[0]='C'+offset;
0391     str[1]=EXEOS;
0392     assert_string_equal(v->tcarray1, str);
0393     assert_string_equal(vf->vname, "UBTESTVIEW2");
0394     
0395     UBF_LOG(log_debug, "Asserting %s:%d, end", file, line);
0396 }
0397 
0398 
0399 /**
0400  * Check ubf value
0401  * @param file src file
0402  * @param line src line
0403  * @param p_ub UBF value to test
0404  * @param offset value offset
0405  * @param len ptr to len (optional)
0406  */
0407 void gen_test_ubf_val_dbg(char *file, int line, UBFH *p_ub, int offset, BFLDLEN *len)
0408 {
0409     char str[2];
0410     
0411     UBF_LOG(log_debug, "Asserting %s:%d, start", file, line);
0412 
0413     /* check minimum UBF size */
0414     if (NULL!=len)
0415     {
0416         assert_equal( !!(*len >= sizeof(UBF_header_t)), EXTRUE);
0417     }
0418 
0419     /* UBF test */
0420     str[0]='S'+offset;
0421     str[1]=EXEOS;
0422     assert_string_equal(Bfind(p_ub, T_STRING_FLD+offset, 0, 0L), str);
0423     
0424     UBF_LOG(log_debug, "Asserting %s:%d, end", file, line);
0425 }
0426 
0427 /**
0428  * Basic preparation before the test
0429  */
0430 void basic_setup(void)
0431 {
0432     /*printf("basic_setup\n");*/
0433     
0434     if (NULL!=M_p_ub)
0435     {
0436         NDRX_FREE(M_p_ub);
0437     }
0438     M_p_ub = NDRX_MALLOC(DEFAULT_BUFFER);
0439     memset(M_p_ub, 255, DEFAULT_BUFFER);
0440     if (EXFAIL==Binit(M_p_ub, DEFAULT_BUFFER))
0441     {
0442         fprintf(stderr, "Binit failed!\n");
0443     }
0444 
0445     load_field_table();
0446     
0447 }
0448 
0449 void basic_teardown(void)
0450 {
0451     /*printf("basic_teardown\n"); */
0452     
0453     if (NULL!=M_p_ub)
0454     {
0455         NDRX_FREE(M_p_ub);
0456     }
0457 }
0458 
0459 /**
0460  * Base Binit test
0461  */
0462 Ensure(test_Binit) {
0463     char tmpbuf[1024];
0464     UBFH * p_ub =  (UBFH *) tmpbuf;
0465     /* Check basic Binit */
0466     assert_equal(Binit(p_ub, 1024), EXSUCCEED);
0467     /* Check the size of Binit */
0468     assert_equal(Bsizeof(p_ub), 1024);
0469 }
0470 
0471 /**
0472  * Basic Field table tests
0473  */
0474 Ensure(test_fld_table)
0475 {
0476     assert_equal(strcmp(Bfname(T_STRING_FLD), "T_STRING_FLD"), 0);
0477     assert_equal(Bfldid("T_STRING_FLD"), T_STRING_FLD);
0478     
0479     assert_equal(Bfname(-1), NULL);
0480     
0481 }
0482 
0483 /**
0484  * NOTE: These are running in 16 bit mode (currently, unless we recompiled the
0485  * header files for fields.
0486  *
0487  * Or replace with generic numbers?
0488  */
0489 Ensure(test_Bmkfldid)
0490 {
0491     /* Short */
0492     assert_equal(Bmkfldid(BFLD_SHORT, 1021), T_SHORT_FLD);
0493     /* Long */
0494     assert_equal(Bmkfldid(BFLD_LONG, 1031), T_LONG_FLD);
0495     /* Char */
0496     assert_equal(Bmkfldid(BFLD_CHAR, 1011), T_CHAR_FLD);
0497     /* Float */
0498     assert_equal(Bmkfldid(BFLD_FLOAT, 1041), T_FLOAT_FLD);
0499     /* Double */
0500     assert_equal(Bmkfldid(BFLD_DOUBLE, 1051), T_DOUBLE_FLD);
0501     /* String */
0502     assert_equal(Bmkfldid(BFLD_STRING, 1061), T_STRING_FLD);
0503     /* Carray */
0504     assert_equal(Bmkfldid(BFLD_CARRAY, 1081), T_CARRAY_FLD);
0505 }
0506 
0507 Ensure(test_Bmkfldid_multidir)
0508 {
0509     /* load_field_table(); */
0510     setenv("FLDTBLDIR", "./ubftab_test:./ubftab", 1);
0511     setenv("FIELDTBLS", "test.fd,test1.fd,Exfields", 1);
0512     
0513     /* from test.fd */
0514     assert_equal(strcmp(Bfname(T_STRING_FLD), "T_STRING_FLD"), 0);
0515     /* from test1.fd */
0516     /* Char */
0517     assert_equal(Bmkfldid(BFLD_CHAR, 1111), T1_CHAR_FLD);
0518     assert_equal(strcmp(Bfname(T1_CHAR_FLD), "T1_CHAR_FLD"), 0);
0519     /* Long */
0520     assert_equal(Bmkfldid(BFLD_LONG, 1118), T1_LONG_2_FLD);
0521     assert_equal(strcmp(Bfname(T1_LONG_2_FLD), "T1_LONG_2_FLD"), 0);
0522     /* Short */
0523     assert_equal(Bmkfldid(BFLD_SHORT, 1116), T1_SHORT_3_FLD);
0524     assert_equal(strcmp(Bfname(T1_SHORT_3_FLD), "T1_SHORT_3_FLD"), 0);
0525     /* Float */
0526     assert_equal(Bmkfldid(BFLD_FLOAT, 1122), T1_FLOAT_3_FLD);
0527     assert_equal(strcmp(Bfname(T1_FLOAT_3_FLD), "T1_FLOAT_3_FLD"), 0);
0528     /* Double */
0529     assert_equal(Bmkfldid(BFLD_DOUBLE, 1126), T1_DOUBLE_4_FLD);
0530     assert_equal(strcmp(Bfname(T1_DOUBLE_4_FLD), "T1_DOUBLE_4_FLD"), 0);
0531     /* String */
0532     assert_equal(Bmkfldid(BFLD_STRING, 1130), T1_STRING_4_FLD);
0533     assert_equal(strcmp(Bfname(T1_STRING_4_FLD), "T1_STRING_4_FLD"), 0);
0534     /* Carray */
0535     assert_equal(Bmkfldid(BFLD_CARRAY, 1137), T1_CARRAY_FLD);
0536     assert_equal(strcmp(Bfname(T1_CARRAY_FLD), "T1_CARRAY_FLD"), 0);
0537 }
0538 
0539 /**
0540  * Test function the returns field number out of ID
0541  */
0542 Ensure(test_Bfldno)
0543 {
0544        /* Short */
0545     assert_equal(Bfldno(T_SHORT_FLD), 1021);
0546     /* Long */
0547     assert_equal(Bfldno(T_LONG_FLD), 1031);
0548     /* Char */
0549     assert_equal(Bfldno(T_CHAR_FLD), 1011);
0550     /* Float */
0551     assert_equal(Bfldno(T_FLOAT_FLD), 1041);
0552     /* Double */
0553     assert_equal(Bfldno(T_DOUBLE_FLD), 1051);
0554     /* String */
0555     assert_equal(Bfldno(T_STRING_FLD), 1061);
0556     /* Carray */
0557     assert_equal(Bfldno(T_CARRAY_FLD), 1081);
0558 }
0559 
0560 Ensure(test_Btype)
0561 {
0562     assert_string_equal(Btype(T_SHORT_FLD), "short");
0563     assert_string_equal(Btype(T_LONG_FLD), "long");
0564     assert_string_equal(Btype(T_CHAR_FLD), "char");
0565     assert_string_equal(Btype(T_FLOAT_FLD), "float");
0566     assert_string_equal(Btype(T_DOUBLE_FLD), "double");
0567     assert_string_equal(Btype(T_STRING_FLD), "string");
0568     assert_string_equal(Btype(T_CARRAY_FLD), "carray");
0569     
0570     assert_string_equal(Btype(T_PTR_FLD), "ptr");
0571     assert_string_equal(Btype(T_UBF_FLD), "ubf");
0572     assert_string_equal(Btype(T_VIEW_FLD), "view");
0573     
0574     assert_string_equal(Btype(0xffffffff), NULL);
0575     assert_equal(Berror, BTYPERR);
0576 }
0577 
0578 /**
0579  * Test Bisubf function
0580  */
0581 Ensure(test_Bisubf)
0582 {
0583     char tmpbuf[72];
0584     UBFH * p_ub =  (UBFH *) tmpbuf;
0585     /* Check basic Binit */
0586     assert_equal(Binit(p_ub, sizeof(tmpbuf)), EXSUCCEED);
0587     assert_equal(Bisubf(p_ub), EXTRUE);
0588     memset(p_ub, 0, sizeof(tmpbuf));
0589     assert_equal(Bisubf(p_ub), EXFALSE);
0590     /* Error should not be set */
0591     assert_equal(Berror, BMINVAL);
0592 }
0593 
0594 /**
0595  * Test Bsizeof
0596  */
0597 Ensure(test_Bsizeof)
0598 {
0599     char tmpbuf[72];
0600     UBFH * p_ub =  (UBFH *) tmpbuf;
0601 
0602     assert_equal(Binit(p_ub, sizeof(tmpbuf)), EXSUCCEED);
0603     assert_equal(Bsizeof(p_ub), sizeof(tmpbuf));
0604 
0605 }
0606 
0607 /**
0608  * Test buffer usage.
0609  */
0610 Ensure(test_Bunused)
0611 {
0612     char tmpbuf[76]; /* +2 for short align, +4 for string dlen */
0613     short s;
0614     UBFH * p_ub =  (UBFH *) tmpbuf;
0615 
0616     /* Check basic Binit */
0617     assert_equal(Binit(p_ub, sizeof(tmpbuf)), EXSUCCEED);
0618 #if EX_ALIGNMENT_BYTES == 8
0619     assert_equal(Bunused(p_ub), sizeof(tmpbuf) - sizeof(UBF_header_t) + sizeof(BFLDID)*2);
0620 #else
0621     assert_equal(Bunused(p_ub), sizeof(tmpbuf) - sizeof(UBF_header_t) + sizeof(BFLDID));
0622 #endif
0623     
0624 #if EX_ALIGNMENT_BYTES != 8
0625     /* Add some field and then see what happens */
0626     assert_equal(Bchg(p_ub, T_SHORT_FLD, 0, (char *)&s, 0), EXSUCCEED);
0627     assert_equal(Bunused(p_ub), sizeof(tmpbuf) - sizeof(UBF_header_t)-sizeof(s)-2/* align of short */);
0628     /* fill up to zero */
0629     assert_equal(Bchg(p_ub, T_STRING_FLD, 0, "abc", 0), EXSUCCEED);
0630     assert_equal(Bunused(p_ub), 0);
0631 #endif
0632 }
0633 
0634 
0635 /**
0636  * Simple Blen test.
0637  */
0638 Ensure(test_Blen)
0639 {
0640     char fb[1024];
0641     UBFH *p_ub = (UBFH *)fb;
0642 
0643     short s = 88;
0644     long l = -1021;
0645     char c = 'c';
0646     float f = 17.31;
0647     double d = 12312.1111;
0648     char carr[] = "CARRAY1 TEST STRING DATA";
0649     BFLDLEN len = strlen(carr);
0650     char *str="TEST STR VAL";
0651     
0652     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0653 
0654     assert_equal(Bchg(p_ub, T_SHORT_FLD, 1, (char *)&s, 0), EXSUCCEED);
0655     assert_equal(Bchg(p_ub, T_LONG_FLD, 1, (char *)&l, 0), EXSUCCEED);
0656     assert_equal(Bchg(p_ub, T_CHAR_FLD, 1, (char *)&c, 0), EXSUCCEED);
0657     assert_equal(Bchg(p_ub, T_FLOAT_FLD, 1, (char *)&f, 0), EXSUCCEED);
0658     assert_equal(Bchg(p_ub, T_DOUBLE_FLD, 1, (char *)&d, 0), EXSUCCEED);
0659     assert_equal(Bchg(p_ub, T_STRING_FLD, 1, (char *)str, 0), EXSUCCEED);
0660     assert_equal(Bchg(p_ub, T_CARRAY_FLD, 1, (char *)carr, len), EXSUCCEED);
0661     
0662     
0663     assert_equal(Blen(p_ub, T_SHORT_FLD, 1), sizeof(s));
0664     assert_equal(Blen(p_ub, T_LONG_FLD, 1), sizeof(l));
0665     assert_equal(Blen(p_ub, T_CHAR_FLD, 1), sizeof(c));
0666     assert_equal(Blen(p_ub, T_FLOAT_FLD, 1), sizeof(f));
0667     assert_equal(Blen(p_ub, T_DOUBLE_FLD, 1), sizeof(d));
0668     assert_equal(Blen(p_ub, T_STRING_FLD, 1), strlen(str)+1);
0669     assert_equal(Blen(p_ub, T_CARRAY_FLD, 1), len);
0670     
0671     assert_equal(Blen(p_ub, T_CARRAY_FLD, 2), EXFAIL);
0672     assert_equal(Berror, BNOTPRES);
0673 }
0674 
0675 /**
0676  * This tests so that buffer terminats with BBADFLDID
0677  * and then pre-last item is the data.
0678  */
0679 Ensure(test_buffer_align_fadd)
0680 {
0681 #if 0
0682     /* Cannot test this directly because of align... */
0683     char buf[1024];
0684     UBFH *p_ub = (UBFH *)buf;
0685     short data=0xffff;
0686     int *err;
0687     char *p = buf+1024-sizeof(BFLDID);
0688     BFLDID *check = (BFLDID *)p;
0689     short *short_check;
0690     assert_equal(Binit(p_ub, sizeof(buf)), EXSUCCEED);
0691     while (EXSUCCEED==Badd(p_ub, T_SHORT_FLD, (char *)&data, 0)){}
0692     /* check that buffer is full */
0693     err = ndrx_Bget_Ferror_addr();
0694     assert_equal(*err, BNOSPACE);
0695     /* Check last element */
0696     assert_equal(*check, BBADFLDID);
0697     /* Pre last must data! */
0698     p = buf+1024-sizeof(BFLDID)-sizeof(short)-4;
0699     short_check = (short *)p;
0700     assert_equal(*short_check, data);
0701 #endif
0702 }
0703 
0704 /**
0705  * Test is not actual anymore - we do not end with BBADFLD
0706  * - we operation with actual length of the buffer to find the EOF
0707  * ---------------------------------------------------------------
0708  * This tests so that buffer terminats with BBADFLDID
0709  * and then pre-last item is the data.
0710  * Do test with Bchg
0711  * Tests Bpres and Boccur
0712  */
0713 Ensure(test_buffer_align_fchg_and_fpresocc)
0714 {
0715     char buf[1024];
0716     UBFH *p_ub = (UBFH *)buf;
0717     short data=0xffff;
0718     int *err;
0719     char *p = buf+1024-sizeof(BFLDID);
0720     BFLDID *check = (BFLDID *)p;
0721     short *short_check;
0722     assert_equal(Binit(p_ub, sizeof(buf)), EXSUCCEED);
0723     BFLDOCC occ=0;
0724     int i;
0725 
0726     while (EXSUCCEED==Bchg(p_ub, T_SHORT_FLD, occ, (char *)&data, 0)){occ++;}
0727     /* check that buffer is full */
0728     err = ndrx_Bget_Ferror_addr();
0729     assert_equal(*err, BNOSPACE);
0730     /* Check last element */
0731     assert_equal(*check, BBADFLDID);
0732     /* Pre last must data! */
0733 #if 0
0734     /* Due to align it is hard to check */
0735     p = buf+1024-sizeof(BFLDID)-sizeof(short)-4;
0736     short_check = (short *)p;
0737     assert_equal(*short_check, data);
0738 #endif
0739     /* test the Boccur */
0740     assert_equal(Boccur(p_ub, T_SHORT_FLD), occ);
0741     /* If not found, then 0 */
0742     assert_equal(Boccur(p_ub, T_LONG_FLD), 0);
0743 
0744     /* Check that every field is present */
0745     for (i=0; i<occ; i++)
0746     {
0747         assert_equal(Bpres(p_ub, T_SHORT_FLD, i), EXTRUE);
0748     }
0749     /* check for non existing, should fail */
0750     assert_equal(Bpres(p_ub, T_SHORT_FLD, occ), EXFALSE);
0751 }
0752 
0753 /**
0754  * Basically we should test all API functions here which operate with FB!
0755  * This also seems to be not valid... We do not end with BADFLDID anymore.
0756  */
0757 Ensure(test_buffer_alignity)
0758 {
0759     char buf[1024];
0760     int short_v;
0761     UBFH *p_ub = (UBFH *)buf;
0762     BFLDID bfldid=BBADFLDID;
0763     BFLDOCC occ;
0764     
0765     assert_equal(Binit(p_ub, sizeof(buf)), EXSUCCEED);
0766     memset(buf+sizeof(UBF_header_t)-sizeof(BFLDID), 0xff,
0767             sizeof(buf)-sizeof(UBF_header_t)+sizeof(BFLDID));
0768 
0769     assert_equal(Bget(p_ub, T_SHORT_FLD, 0, (char *)&short_v, 0), EXFAIL);
0770     assert_equal(Berror, BALIGNERR);
0771     assert_equal(Badd(p_ub, T_SHORT_FLD, (char *)&short_v, 0), EXFAIL);
0772     assert_equal(Berror, BALIGNERR);
0773     assert_equal(Bchg(p_ub, T_SHORT_FLD, 0, (char *)&short_v, 0), EXFAIL);
0774     assert_equal(Berror, BALIGNERR);
0775     assert_equal(Bdel(p_ub, T_SHORT_FLD, 0), EXFAIL);
0776     assert_equal(Berror, BALIGNERR);
0777     assert_equal(Bnext(p_ub, &bfldid, &occ, NULL, 0), EXFAIL);
0778     assert_equal(Berror, BALIGNERR);
0779 }
0780 
0781 /**
0782  * Very basic tests of the framework
0783  * @return
0784  */
0785 TestSuite *ubf_basic_tests() {
0786     TestSuite *suite = create_test_suite();
0787     
0788     set_setup(suite, basic_setup);
0789     set_teardown(suite, basic_teardown);
0790 
0791     /*  UBF init test */
0792     add_test(suite, test_Binit);
0793     add_test(suite, test_fld_table);
0794     add_test(suite, test_Bmkfldid);
0795     add_test(suite, test_Bfldno);
0796 /* no more for new processing priciples of bytes used.
0797     add_test(suite, test_buffer_align_fadd);
0798     add_test(suite, test_buffer_align_fchg_and_fpresocc);
0799 */
0800 /*
0801     - not valid any more the trailer might non zero
0802     add_test(suite, test_buffer_alignity);
0803 */
0804     add_test(suite, test_Bisubf);
0805     add_test(suite, test_Bunused);
0806     add_test(suite, test_Bsizeof);
0807     add_test(suite, test_Btype);
0808     add_test(suite, test_Blen);
0809 
0810     return suite;
0811 }
0812 
0813 /**
0814  * Testing bmkfldid with multi directory in FLDTBLDIR environment
0815  * @return 
0816  */
0817 TestSuite *ubf_bmkfldid_multidir_tests(void) {
0818     TestSuite *suite = create_test_suite();
0819     
0820 
0821     add_test(suite, test_Bmkfldid_multidir);
0822 
0823     return suite;
0824 }
0825 
0826 /*
0827  * Main test entry.
0828  */
0829 int main(int argc, char** argv)
0830 {    
0831     TestSuite *suite = create_test_suite();
0832     int ret;
0833 
0834     /*
0835      * NSTD Library tests
0836      */
0837     add_suite(suite, ubf_nstd_atomicadd());
0838     add_suite(suite, ubf_nstd_fpa());
0839     add_suite(suite, ubf_nstd_standard());
0840     add_suite(suite, ubf_nstd_util());
0841     add_suite(suite, ubf_nstd_debug());
0842     add_suite(suite, ubf_nstd_lh());
0843     add_suite(suite, ubf_nstd_sm());
0844     add_suite(suite, test_nstd_macros());
0845     add_suite(suite, ubf_nstd_crypto());
0846     add_suite(suite, ubf_nstd_base64());
0847     add_suite(suite, ubf_nstd_growlist());
0848     
0849     add_suite(suite, ubf_nstd_mtest());
0850     add_suite(suite, ubf_nstd_mtest2());
0851     add_suite(suite, ubf_nstd_mtest3());
0852     add_suite(suite, ubf_nstd_mtest4());
0853     add_suite(suite, ubf_nstd_mtest5());
0854     add_suite(suite, ubf_nstd_mtest6_dupcursor());
0855     add_suite(suite, ubf_nstd_mtest6());
0856     add_suite(suite, ubf_nstd_mtest7());
0857     add_suite(suite, ubf_nstd_fsync());
0858     add_suite(suite, ubf_nstd_cid());
0859     
0860     add_suite(suite, test_rbt_tree());
0861 
0862     /*
0863      * UBF tests
0864      */
0865     add_suite(suite, ubf_basic_tests());
0866     add_suite(suite, ubf_Badd_tests());
0867     add_suite(suite, ubf_genbuf_tests());
0868     add_suite(suite, ubf_cfchg_tests());
0869     add_suite(suite, ubf_cfget_tests());
0870     add_suite(suite, ubf_fdel_tests());
0871     add_suite(suite, ubf_expr_tests());
0872     add_suite(suite, ubf_bnext_tests());
0873     add_suite(suite, ubf_fproj_tests());
0874     add_suite(suite, ubf_mem_tests());
0875     add_suite(suite, ubf_fupdate_tests());
0876     add_suite(suite, ubf_fconcat_tests());
0877     add_suite(suite, ubf_find_tests());
0878     add_suite(suite, ubf_get_tests());
0879     add_suite(suite, ubf_print_tests());
0880     add_suite(suite, ubf_printv_tests());
0881     add_suite(suite, ubf_macro_tests());
0882     add_suite(suite, ubf_readwrite_tests());
0883     add_suite(suite, ubf_mkfldhdr_tests());
0884     add_suite(suite, ubf_bcmp_tests());
0885     add_suite(suite, ubf_bnum_tests());
0886     add_suite(suite, ubf_bjoin_tests());
0887     add_suite(suite, ubf_bojoin_tests());
0888     add_suite(suite, ubf_bmkfldid_multidir_tests());
0889     add_suite(suite, ubf_embubf_tests());
0890 
0891     if (argc > 1)
0892     {
0893         ret=run_single_test(suite,argv[1],create_text_reporter());
0894     }
0895     else
0896     {
0897         ret=run_test_suite(suite, create_text_reporter());
0898     }
0899 
0900     destroy_test_suite(suite);
0901 
0902     return ret;
0903     
0904 }
0905 /* vim: set ts=4 sw=4 et smartindent: */