Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Basic INI tests...
0003  *
0004  * @file atmiclt29.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 <unistd.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 "test029.h"
0046 #include <inicfg.h>
0047 #include <stdarg.h>     /* va_list, va_start, va_arg, va_end */
0048 #include <exhash.h>
0049 #include <nerror.h>
0050 #include <cconfig.h>
0051 #include <errno.h>
0052 /*---------------------------Externs------------------------------------*/
0053 /*---------------------------Macros-------------------------------------*/
0054 /*---------------------------Enums--------------------------------------*/
0055 /*---------------------------Typedefs-----------------------------------*/
0056 /*---------------------------Globals------------------------------------*/
0057 /*---------------------------Statics------------------------------------*/
0058 /*---------------------------Prototypes---------------------------------*/
0059 
0060 
0061 /**
0062  * Append text file
0063  * @param file
0064  * @param line
0065  * @return 
0066  */
0067 int append_text_file(char *file, char *line)
0068 {
0069     int ret = EXSUCCEED;
0070     FILE *f = NDRX_FOPEN(file, "a");
0071     
0072     if (NULL==f)
0073     {
0074         NDRX_LOG(log_error, "TESTERROR: Failed to open [%s]: %s", file, strerror(errno));
0075         EXFAIL_OUT(ret);
0076     }
0077     
0078     fprintf(f, "%s", line);
0079     
0080 out:
0081     if (NULL!=f)
0082     {
0083         NDRX_FCLOSE(f);
0084     }
0085     return ret;
0086 }
0087 
0088 /*
0089  * Main test case entry
0090  */
0091 int main(int argc, char** argv)
0092 {
0093     int ret = EXSUCCEED;
0094     ndrx_inicfg_t *cfg;
0095     ndrx_inicfg_section_keyval_t *out = NULL;
0096     ndrx_inicfg_section_keyval_t *val;
0097     ndrx_inicfg_section_t *sections, *iter, *iter_tmp;
0098     char *iterfilter[] = {"my", NULL};
0099     int i;
0100     
0101     for (i=0; i<5; i++)
0102     {
0103         out = NULL;
0104         
0105         if (EXSUCCEED!=system("/bin/cp C_test_template.ini C_test.ini"))
0106         {
0107             NDRX_LOG(log_error, "TESTERROR: Failed to copy ini file!!", strerror(errno));
0108             EXFAIL_OUT(ret);       
0109         }
0110         
0111         if (NULL==(cfg=ndrx_inicfg_new()))
0112         {
0113             NDRX_LOG(log_error, "TESTERROR: failed to make inicfg: %s", Nstrerror(Nerror));
0114             EXFAIL_OUT(ret);
0115         }
0116 
0117         /* any sections */
0118         if (EXSUCCEED!=ndrx_inicfg_add(cfg, "./cfg_folder1", NULL))
0119         {
0120             NDRX_LOG(log_error, "TESTERROR: failed to add resource: %s", Nstrerror(Nerror));
0121             EXFAIL_OUT(ret);       
0122         }
0123 
0124         if (EXSUCCEED!=ndrx_inicfg_add(cfg, "A_test.ini", NULL))
0125         {
0126             NDRX_LOG(log_error, "TESTERROR: failed to make resource: %s", Nstrerror(Nerror));
0127             EXFAIL_OUT(ret);        
0128         }
0129 
0130         if (EXSUCCEED!=ndrx_inicfg_add(cfg, "B_test.ini", NULL))
0131         {
0132             NDRX_LOG(log_error, "TESTERROR: failed to make resource: %s", Nstrerror(Nerror));
0133             EXFAIL_OUT(ret);    
0134         }
0135         
0136         if (EXSUCCEED!=ndrx_inicfg_add(cfg, "C_test.ini", NULL))
0137         {
0138             NDRX_LOG(log_error, "TESTERROR: failed to make resource: %s", Nstrerror(Nerror));
0139             EXFAIL_OUT(ret);    
0140         }
0141 
0142 
0143         /* resource == NULL, any resource. */
0144         if (EXSUCCEED!=ndrx_inicfg_get_subsect(cfg, NULL, "mysection/subsect1/f/f", &out))
0145         {
0146             NDRX_LOG(log_error, "TESTERROR: Failed to resolve [mysection/subsect1/f/f]: %s",
0147                     Nstrerror(Nerror));
0148             EXFAIL_OUT(ret);    
0149         }
0150 
0151 
0152         /* get the value from  'out' - should be 4 
0153          * As firstly it will get the stuff 
0154          * [mysection/subsect1/f]
0155          * MYVALUE1=3
0156          * and then it will get the exact value from B_test.ini
0157          */
0158 
0159         if (NULL==(val=ndrx_keyval_hash_get(out, "MYVALUE1")))
0160         {
0161             NDRX_LOG(log_error, "TESTERROR: Failed to get MYVALUE1!");
0162             EXFAIL_OUT(ret);
0163         }
0164 
0165         if (0!=strcmp(val->val, "4"))
0166         {
0167             NDRX_LOG(log_error, "TESTERROR: [mysection/subsect1/f/f] not 4!");
0168             EXFAIL_OUT(ret);
0169         }
0170 
0171         /* test multi-word param */
0172         /* some multi word value */
0173         if (NULL==(val=ndrx_keyval_hash_get(out, "some multi word value")))
0174         {
0175             NDRX_LOG(log_error, "TESTERROR: Failed to get [some multi word value]!");
0176             EXFAIL_OUT(ret);
0177         }
0178 
0179         if (0!=strcmp(val->val, "yes"))
0180         {
0181             NDRX_LOG(log_error, "TESTERROR: [some multi word value] not yes!");
0182             EXFAIL_OUT(ret);
0183         }
0184 
0185         
0186         /* test a multi-line string */
0187         if (NULL==(val=ndrx_keyval_hash_get(out, "MYVALUE3")))
0188         {
0189             NDRX_LOG(log_error, "TESTERROR: Failed to get MYVALUE3!");
0190             EXFAIL_OUT(ret);
0191         }
0192         
0193         NDRX_LOG(log_debug, "Got multi-line value [%s]", val->val);
0194 
0195         if (NULL==strstr(val->val, "MULTILINE"))
0196         {
0197             NDRX_LOG(log_error, "TESTERROR: [MYVALUE3] does not contain 'MULTILINE'!");
0198             EXFAIL_OUT(ret);
0199         }
0200         
0201         if (NULL==strstr(val->val, "VALUE"))
0202         {
0203             NDRX_LOG(log_error, "TESTERROR: [MYVALUE3] does not contain 'VALUE'!");
0204             EXFAIL_OUT(ret);
0205         }
0206 
0207         /* free the list */
0208         ndrx_keyval_hash_free(out);
0209         
0210 
0211         out=NULL;
0212         /* try some iteration over the config */
0213         sections = NULL;
0214         iter = NULL;
0215         iter_tmp = NULL;
0216 
0217         if (EXSUCCEED!=ndrx_inicfg_iterate(cfg, NULL, iterfilter, &sections))
0218         {
0219             NDRX_LOG(log_error, "TESTERROR: Failed to iterate config!");
0220             EXFAIL_OUT(ret);
0221         }
0222 
0223         /* print the stuff we have in config */
0224 
0225         EXHASH_ITER(hh, sections, iter, iter_tmp)
0226         {
0227             NDRX_LOG(log_info, "iter: section [%s]", iter->section);
0228             
0229             if (0==strcmp(iter->section, "mytest"))
0230             {
0231                 /* we should have "THIS" key there */
0232                 if (NULL==(val=ndrx_keyval_hash_get(iter->values, "THIS")))
0233                 {
0234                     NDRX_LOG(log_error, "TESTERROR: Failed to get THIS of [mytest]!");
0235                     EXFAIL_OUT(ret);
0236                 }
0237 
0238                 if (0!=strcmp(val->val, "IS TEST"))
0239                 {
0240                     NDRX_LOG(log_error, "TESTERROR: [mytest]/THIS not "
0241                             "equal to 'IS TEST' but [%s]!", 
0242                             val->val);
0243                     EXFAIL_OUT(ret);
0244                 }
0245             }
0246         }
0247         
0248         /* kill the section listing */
0249         ndrx_inicfg_sections_free(sections);
0250         
0251         sleep(1); /* have some time to change file-system timestamp... */
0252         
0253         /* Update config file */
0254         append_text_file("C_test.ini", "\n[MOTORCYCLE]\n");
0255         append_text_file("C_test.ini", "SPEED=120\n");
0256         append_text_file("C_test.ini", "COLOR=red\n");
0257         
0258         /* update/reload */
0259         if (EXSUCCEED!=ndrx_inicfg_reload(cfg, NULL))
0260         {
0261             NDRX_LOG(log_error, "Failed to reload: %s", Nstrerror(Nerror));
0262             EXFAIL_OUT(ret);
0263         }
0264         
0265         /* out=NULL; - should be already */
0266         /* resource == NULL, any resource. */
0267         if (EXSUCCEED!=ndrx_inicfg_get_subsect(cfg, NULL, "MOTORCYCLE", &out))
0268         {
0269             NDRX_LOG(log_error, "TESTERROR: Failed to resolve [MOTORCYCLE]: %s",
0270                     Nstrerror(Nerror));
0271             EXFAIL_OUT(ret);    
0272         }
0273 
0274         if (NULL==(val=ndrx_keyval_hash_get(out, "COLOR")))
0275         {
0276             NDRX_LOG(log_error, "TESTERROR: Failed to get COLOR!");
0277             EXFAIL_OUT(ret);
0278         }
0279 
0280         if (0!=strcmp(val->val, "red"))
0281         {
0282             NDRX_LOG(log_error, "TESTERROR: [COLOR] is not red, but [%s]!", val->val);
0283             EXFAIL_OUT(ret);
0284         }
0285         
0286         /* free-up the lookup */
0287         ndrx_keyval_hash_free(out);
0288         out=NULL;
0289         
0290         /* free up the config */
0291         ndrx_inicfg_free(cfg);
0292     }
0293     
0294     
0295     /* get the Enduro/X handler */
0296     cfg = NULL;
0297     out = NULL;
0298     
0299     if (EXSUCCEED!=ndrx_cconfig_load_general(&cfg))
0300     {
0301         NDRX_LOG(log_error, "TESTERROR: Failed to get Enduro/X config handler: %s", Nstrerror(Nerror));
0302         EXFAIL_OUT(ret);  
0303     }
0304     
0305     if (EXSUCCEED!=ndrx_cconfig_get_cf(cfg, "@debug", &out))
0306     {
0307         NDRX_LOG(log_error, "TESTERROR: Failed to resolve [debug]: %s",
0308                 Nstrerror(Nerror));
0309         EXFAIL_OUT(ret);    
0310     }
0311     
0312     /* there should be * */
0313     if (NULL==(val=ndrx_keyval_hash_get(out, "*")))
0314     {
0315         NDRX_LOG(log_error, "TESTERROR: Failed to get *!");
0316         EXFAIL_OUT(ret);
0317     }
0318 
0319     if (0!=strcmp(val->val, "ndrx=5"))
0320     {
0321         NDRX_LOG(log_error, "TESTERROR: invalid value for debug: [%s] vs ", 
0322                 val->val, "ndrx=5");
0323         EXFAIL_OUT(ret);
0324     }
0325     
0326     /* we can append the result "virtual secton" with new data: */
0327     if (EXSUCCEED!=ndrx_cconfig_get_cf(cfg, "HELLO2", &out))
0328     {
0329         NDRX_LOG(log_error, "TESTERROR: Failed to resolve [debug]: %s",
0330                 Nstrerror(Nerror));
0331         EXFAIL_OUT(ret);    
0332     }
0333     
0334     if (NULL==(val=ndrx_keyval_hash_get(out, "MY")))
0335     {
0336         NDRX_LOG(log_error, "TESTERROR: Failed to get MY!");
0337         EXFAIL_OUT(ret);
0338     }
0339 
0340     if (0!=strcmp(val->val, "PHONE"))
0341     {
0342         NDRX_LOG(log_error, "TESTERROR: invalid value for debug: [%s]", val->val);
0343         EXFAIL_OUT(ret);
0344     }
0345     
0346     ndrx_keyval_hash_free(out);
0347     ndrx_inicfg_free(cfg);
0348     
0349     /* Now test only Enduro/X cconfig (must not have HELLO2) */
0350     
0351     cfg = NULL;
0352     out = NULL;
0353     
0354     if (EXSUCCEED!=ndrx_cconfig_load())
0355     {
0356         NDRX_LOG(log_error, "TESTERROR: Failed to get Enduro/X config handler: %s", Nstrerror(Nerror));
0357         EXFAIL_OUT(ret);  
0358     }
0359     
0360     if (EXSUCCEED!=ndrx_cconfig_get("HELLO2", &out))
0361     {
0362         NDRX_LOG(log_error, "TESTERROR: Enduro/X internal config must not have [HELLO2]!");
0363         EXFAIL_OUT(ret);    
0364     }
0365     
0366     ndrx_keyval_hash_free(out);
0367     ndrx_inicfg_free(ndrx_get_G_cconfig());
0368     
0369 out:
0370     NDRX_LOG(log_info, "Test returns %d", ret);
0371     return ret;
0372 }
0373 
0374 /* vim: set ts=4 sw=4 et smartindent: */