Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief UBF Field table database tests - client
0003  *
0004  * @file atmiclt50.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 #include <math.h>
0039 
0040 #include <atmi.h>
0041 #include <ubf.h>
0042 #include <ndebug.h>
0043 #include <test.fd.h>
0044 #include <ndrstandard.h>
0045 #include <nstopwatch.h>
0046 #include <fcntl.h>
0047 #include <unistd.h>
0048 #include <nstdutil.h>
0049 #include <cgreen/cgreen.h>
0050 #include "test50.h"
0051 /*---------------------------Externs------------------------------------*/
0052 /*---------------------------Macros-------------------------------------*/
0053 /*---------------------------Enums--------------------------------------*/
0054 /*---------------------------Typedefs-----------------------------------*/
0055 /*---------------------------Globals------------------------------------*/
0056 /*---------------------------Statics------------------------------------*/
0057 /*---------------------------Prototypes---------------------------------*/
0058 
0059 
0060 /**
0061  * Basic preparation before the test
0062  */
0063 exprivate void basic_setup(void)
0064 {
0065 
0066 }
0067 
0068 exprivate void basic_teardown(void)
0069 {
0070 
0071 }
0072 
0073 /**
0074  * Get transaction object..
0075  * @param txn
0076  */
0077 exprivate void get_tran(EDB_txn **txn)
0078 {
0079     EDB_env * dbenv = NULL;
0080     EDB_dbi *dbi_id = NULL, *dbi_nm = NULL;
0081     
0082     /* get DB Env */
0083     dbenv = Bfldddbgetenv(&dbi_id, &dbi_nm);
0084     
0085     assert_not_equal(dbenv, NULL);
0086     assert_not_equal(dbi_id, NULL);
0087     assert_not_equal(dbi_nm, NULL);
0088     
0089     /* begin transaction */
0090     assert_equal(edb_txn_begin(dbenv, NULL, 0, txn), EXSUCCEED);
0091 }
0092 
0093 char *M_ref_print[]= {
0094 "T_HELLO_SHORT\t56\n",
0095 "T_HELLO_LONG\t777777\n",
0096 "T_HELLO_CHAR\tA\n",
0097 "T_STRING_FLD\tABC\n",
0098 "T_HELLO_STR\tHELLO WORLD\n",
0099 NULL
0100 };
0101 
0102 char *M_ref_print_nofld[]= {
0103 "((BFLDID32)3001)\t56\n",
0104 "T_HELLO_LONG\t777777\n",
0105 "T_HELLO_CHAR\tA\n",
0106 "T_STRING_FLD\tABC\n",
0107 NULL
0108 };
0109 
0110 /**
0111  * Bfprint testing.
0112  * @return
0113  */
0114 exprivate void test_Bprint(UBFH *p_ub, char **ref_print)
0115 {
0116     FILE *f=NULL;
0117     char filename[]="/tmp/ubf-test-XXXXXX";
0118     char readbuf[1024];
0119     int line_counter=0;
0120 
0121     assert_not_equal(mkstemp(filename), EXFAIL);
0122     assert_not_equal((f=fopen(filename, "w")), NULL);
0123     assert_equal(Bfprint(p_ub, f), EXSUCCEED);
0124     fclose(f);
0125 
0126     /* Re-open file in read mode end re-compare the buffer. */
0127     assert_not_equal((f=fopen(filename, "r")), NULL);
0128 
0129     /* compare the buffers */
0130     while(NULL!=fgets(readbuf, sizeof(readbuf), f))
0131     {
0132         if (NULL==ref_print[line_counter])
0133         {
0134             /* reached end of our knowledge of the test data */
0135             assert_equal_with_message(0, 1, "output file too big!");
0136             break;
0137         }
0138         assert_string_equal(ref_print[line_counter], readbuf);
0139         line_counter++;
0140     }
0141     fclose(f);
0142 
0143     /* remove the file */
0144     assert_equal(unlink(filename), EXSUCCEED);
0145 
0146     /* cannot print on null file */
0147     assert_equal(Bfprint(p_ub, NULL), EXFAIL);
0148     assert_equal(Berror, BEINVAL);
0149     
0150 }
0151 
0152 
0153 /**
0154  * Local testing of the UBF DB
0155  */
0156 Ensure(ubfdb_local_tests)
0157 {
0158     BFLDID fld;
0159     BFLDID fld1;
0160     BFLDID fld2;
0161     BFLDID fld3;
0162     EDB_txn *txn;
0163     UBFH *p_ub;
0164     char *tree = NULL;
0165     
0166     /* Unlink db firstly... */
0167     
0168     /* close any open... */
0169     Bflddbunload();
0170     
0171     assert_equal(Bflddbunlink(), EXSUCCEED);
0172     
0173     /* Load DB... (open handles, etc...) */
0174     assert_equal(Bflddbload(), EXTRUE);
0175     
0176     /* Check that fields are missing */
0177     assert_equal(Bflddbid("T_HELLO_STR"), BBADFLDID);
0178     /* error shall be set to bad field */
0179     assert_equal(Berror, BBADNAME);
0180     
0181     /* the same here */
0182     assert_equal(Bfldid("T_HELLO_STR"), BBADFLDID);
0183     assert_equal(Berror, BBADNAME);
0184     
0185     /* Check by field id */
0186     fld = Bmkfldid (BFLD_STRING, 3000);
0187     assert_not_equal(fld, BBADFLDID);
0188     
0189     fld1 = Bmkfldid (BFLD_SHORT, 3001);
0190     assert_not_equal(fld1, BBADFLDID);
0191     
0192     fld2 = Bmkfldid (BFLD_CHAR, 3002);
0193     assert_not_equal(fld2, BBADFLDID);
0194     
0195     fld3 = Bmkfldid (BFLD_LONG, 3003);
0196     assert_not_equal(fld3, BBADFLDID);
0197     
0198     /* field must not exists in the db */
0199     
0200     /* Check that fields are missing */
0201     assert_equal(Bflddbname(fld), NULL);
0202     /* error shall be set to bad field */
0203     assert_equal(Berror, BBADFLD);
0204     
0205     /* the same here */
0206     assert_equal(Bfname(fld), BBADFLDID);
0207     assert_equal(Berror, BBADFLD);
0208     
0209     
0210     /* add some fields now */
0211     get_tran(&txn);
0212 
0213     UBF_LOG(log_error, "About to ADD custom fields!!!");
0214     assert_equal(Bflddbadd(txn, BFLD_STRING, 3000, "T_HELLO_STR"), EXSUCCEED);
0215     assert_equal(Bflddbadd(txn, BFLD_SHORT, 3001, "T_HELLO_SHORT"), EXSUCCEED);
0216     assert_equal(Bflddbadd(txn, BFLD_CHAR, 3002, "T_HELLO_CHAR"), EXSUCCEED);
0217     assert_equal(Bflddbadd(txn, BFLD_LONG, 3003, "T_HELLO_LONG"), EXSUCCEED);
0218     
0219     assert_equal(edb_txn_commit(txn), EXSUCCEED);
0220     
0221     
0222     assert_equal(Bfldid("T_HELLO_STR"), fld);
0223     assert_equal(Bfldid("T_HELLO_SHORT"), fld1);
0224     assert_equal(Bfldid("T_HELLO_CHAR"), fld2);
0225     assert_equal(Bfldid("T_HELLO_LONG"), fld3);
0226     
0227     
0228     assert_string_equal(Bfname(fld), "T_HELLO_STR");
0229     assert_string_equal(Bfname(fld1), "T_HELLO_SHORT");
0230     assert_string_equal(Bfname(fld2), "T_HELLO_CHAR");
0231     assert_string_equal(Bfname(fld3), "T_HELLO_LONG");
0232     
0233     /* run some boolean expression */
0234     
0235     UBF_LOG(log_error, "About to run boolean expression...");
0236     
0237     p_ub = (UBFH *)tpalloc("UBF", NULL, 56000);
0238     assert_not_equal(p_ub, NULL);
0239     assert_equal(CBchg(p_ub, Bfldid("T_HELLO_STR"), 0, "HELLO WORLD", 0L, BFLD_STRING), 
0240             EXSUCCEED);
0241     assert_equal(CBchg(p_ub, Bfldid("T_HELLO_SHORT"), 0, "56", 0L, BFLD_STRING), 
0242             EXSUCCEED);
0243     assert_equal(CBchg(p_ub, Bfldid("T_HELLO_CHAR"), 0, "A", 0L, BFLD_STRING),
0244             EXSUCCEED);
0245     assert_equal(CBchg(p_ub, Bfldid("T_HELLO_LONG"), 0, "777777", 0L, BFLD_STRING),
0246             EXSUCCEED);
0247     /* test standard field too */
0248     assert_equal(CBchg(p_ub, T_STRING_FLD, 0, "ABC", 0L, BFLD_STRING),
0249             EXSUCCEED);
0250     
0251     /* run some expression */
0252     tree=Bboolco ("T_HELLO_STR=='HELLO WORLD' && T_HELLO_SHORT==56 && "
0253             "T_HELLO_CHAR=='A' && T_HELLO_LONG==777777 && T_STRING_FLD=='ABC'");
0254     assert_not_equal(tree, NULL);
0255     assert_equal(Bboolev(p_ub, tree), EXTRUE);
0256     Btreefree(tree);
0257     
0258     /* Test buffer printing */
0259     test_Bprint(p_ub, M_ref_print);
0260     
0261     tpfree((char *)p_ub);    
0262 }
0263 
0264 /**
0265  * Test field delete
0266  */
0267 Ensure(ubfdb_delete)
0268 {
0269     BFLDID fld;
0270     BFLDID fld1;
0271     BFLDID fld2;
0272     BFLDID fld3;
0273     EDB_txn *txn;
0274     UBFH *p_ub;
0275     FILE *f=NULL;
0276     
0277     p_ub = (UBFH *)tpalloc("UBF", NULL, 56000);
0278     assert_not_equal(p_ub, NULL);
0279     
0280     /* Unlink db firstly... */
0281     Bflddbunload();
0282     assert_equal(Bflddbunlink(), EXSUCCEED);
0283     
0284     /* Load DB... (open handles, etc...) */
0285     assert_equal(Bflddbload(), EXTRUE);
0286     
0287     fld = Bmkfldid (BFLD_STRING, 3000);
0288     assert_not_equal(fld, BBADFLDID);
0289     
0290     fld1 = Bmkfldid (BFLD_SHORT, 3001);
0291     assert_not_equal(fld1, BBADFLDID);
0292     
0293     fld2 = Bmkfldid (BFLD_CHAR, 3002);
0294     assert_not_equal(fld2, BBADFLDID);
0295     
0296     fld3 = Bmkfldid (BFLD_LONG, 3003);
0297     assert_not_equal(fld3, BBADFLDID);
0298     
0299     
0300     /* add some fields now */
0301     get_tran(&txn);
0302     assert_equal(Bflddbadd(txn, BFLD_STRING, 3000, "T_HELLO_STR"), EXSUCCEED);
0303     assert_equal(Bflddbadd(txn, BFLD_SHORT, 3001, "T_HELLO_SHORT"), EXSUCCEED);
0304     assert_equal(Bflddbadd(txn, BFLD_CHAR, 3002, "T_HELLO_CHAR"), EXSUCCEED);
0305     assert_equal(Bflddbadd(txn, BFLD_LONG, 3003, "T_HELLO_LONG"), EXSUCCEED);
0306     assert_equal(edb_txn_commit(txn), EXSUCCEED);
0307     
0308     
0309     /* Test field that field is present */
0310     assert_string_equal(Bfname(fld), "T_HELLO_STR");
0311     assert_string_equal(Bfname(fld1), "T_HELLO_SHORT");
0312     assert_string_equal(Bfname(fld2), "T_HELLO_CHAR");
0313     assert_string_equal(Bfname(fld3), "T_HELLO_LONG");
0314     
0315     /* Load some data: */
0316     assert_equal(CBchg(p_ub, Bfldid("T_HELLO_SHORT"), 0, "56", 0L, BFLD_STRING), 
0317             EXSUCCEED);
0318     assert_equal(CBchg(p_ub, Bfldid("T_HELLO_CHAR"), 0, "A", 0L, BFLD_STRING),
0319             EXSUCCEED);
0320     assert_equal(CBchg(p_ub, Bfldid("T_HELLO_LONG"), 0, "777777", 0L, BFLD_STRING),
0321             EXSUCCEED);
0322     /* test standard field too */
0323     assert_equal(CBchg(p_ub, T_STRING_FLD, 0, "ABC", 0L, BFLD_STRING),
0324             EXSUCCEED);
0325     
0326     get_tran(&txn);
0327     assert_equal(Bflddbdel(txn, fld), EXSUCCEED);
0328     assert_equal(Bflddbdel(txn, fld1), EXSUCCEED);
0329     
0330     /* try to delete already removed fields, what will happen? */
0331     
0332     assert_equal(Bflddbdel(txn, fld), EXSUCCEED);
0333     assert_equal(Bflddbdel(txn, fld1), EXSUCCEED);
0334     
0335     assert_equal(edb_txn_commit(txn), EXSUCCEED);
0336     
0337     assert_string_equal(Bfname(fld), NULL);
0338     assert_equal(Berror, BBADFLD);
0339     
0340     assert_string_equal(Bfname(fld1), NULL);
0341     assert_equal(Berror, BBADFLD);
0342     
0343     assert_string_equal(Bfname(fld2), "T_HELLO_CHAR");
0344     assert_string_equal(Bfname(fld3), "T_HELLO_LONG");
0345     
0346     
0347     /* Test buffer printing */
0348     test_Bprint(p_ub, M_ref_print_nofld);
0349     
0350     /* try to parse back id... */
0351     /* assert_equal(Bfldid("((BFLDID32)3001)"), 3001); */
0352     f = fopen("./extfile", "r");
0353     assert_not_equal(f, NULL);
0354     
0355     assert_equal(Bextread(p_ub, f), EXSUCCEED);
0356     
0357     fclose(f);
0358     
0359     assert_equal(Bpres(p_ub, fld1, 0), EXTRUE);
0360     assert_equal(Bpres(p_ub, fld2, 0), EXTRUE);
0361     
0362     
0363     UBF_LOG(log_info, "drop the database now...");
0364     get_tran(&txn);
0365     assert_equal(Bflddbdrop(txn), EXSUCCEED);
0366     assert_equal(edb_txn_commit(txn), EXSUCCEED);
0367     
0368     
0369     
0370     assert_equal(Bfldid("T_HELLO_STR"), BBADFLDID);
0371     assert_equal(Bfldid("T_HELLO_SHORT"), BBADFLDID);
0372     assert_equal(Bfldid("T_HELLO_CHAR"), BBADFLDID);
0373     assert_equal(Bfldid("T_HELLO_LONG"), BBADFLDID);
0374     
0375     assert_string_equal(Bfname(fld), NULL);
0376     assert_string_equal(Bfname(fld1), NULL);
0377     assert_string_equal(Bfname(fld2), NULL);
0378     assert_string_equal(Bfname(fld3), NULL);
0379     
0380     /* unload finally */
0381     Bflddbunload();
0382     
0383     tpfree((char *)p_ub);    
0384 }
0385 
0386 /**
0387  * UBFDB testing suite
0388  * @return
0389  */
0390 TestSuite *ubfdb_tests() 
0391 {
0392     TestSuite *suite = create_test_suite();
0393     
0394     set_setup(suite, basic_setup);
0395     set_teardown(suite, basic_teardown);
0396 
0397     add_test(suite, ubfdb_local_tests);
0398     add_test(suite, ubfdb_delete);
0399     
0400     return suite;
0401 }
0402 
0403 /*
0404  * Main test entry.
0405  */
0406 int main(int argc, char** argv)
0407 {
0408     TestSuite *suite = create_test_suite();
0409     int ret;
0410 
0411     add_suite(suite, ubfdb_tests());
0412     
0413     if (argc > 1)
0414     {
0415         ret=run_single_test(suite,argv[1],create_text_reporter());
0416     }
0417     else
0418     {
0419         ret=run_test_suite(suite, create_text_reporter());
0420     }
0421 
0422     destroy_test_suite(suite);
0423 
0424     return ret;
0425 }
0426 /* vim: set ts=4 sw=4 et smartindent: */