Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Standard library tests - cryptography
0003  *
0004  * @file test_nstd_crypto.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-2018, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * GPL or Mavimax's license for commercial use.
0012  * -----------------------------------------------------------------------------
0013  * GPL license:
0014  * 
0015  * This program is free software; you can redistribute it and/or modify it under
0016  * the terms of the GNU General Public License as published by the Free Software
0017  * Foundation; either version 3 of the License, or (at your option) any later
0018  * version.
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 General Public License for more details.
0023  *
0024  * You should have received a copy of the GNU General Public License along with
0025  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
0026  * 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 <ndebug.h>
0041 #include <excrypto.h>
0042 #include "test.fd.h"
0043 #include "ubfunit1.h"
0044 
0045 /**
0046  * Test string encrypt & decrypt
0047  */
0048 Ensure(test_crypto_enc_string)
0049 {
0050     char buf[1024]="";
0051     char buf2[1024]="";
0052     int i;
0053     
0054 #define ENC_TEST_STRING "HELLO STRING _ 123 hello test"
0055     
0056     for (i=0; i<100; i++)
0057     {
0058         assert_equal(
0059                 ndrx_crypto_enc_string(ENC_TEST_STRING, buf, sizeof(buf)),
0060                 EXSUCCEED);
0061 
0062         NDRX_LOG(log_debug, "Encrypted string: [%s]", buf);
0063 
0064         assert_equal(
0065                 ndrx_crypto_dec_string(buf, buf2, sizeof(buf2)),
0066                 EXSUCCEED);
0067 
0068         /* decrypted strings must be equal... */
0069         assert_string_equal(buf2, ENC_TEST_STRING);
0070     }
0071 }
0072 
0073 /**
0074  * Test substitute functions
0075  * Bug #268 issues with env substituion after crypto
0076  */
0077 Ensure(test_crypto_subst_func)
0078 {
0079     char encdata[PATH_MAX];
0080     char fmt[PATH_MAX];
0081     int i;
0082     
0083 #define ENC_SUBST_STRING "Enduro/X"
0084 
0085     assert_equal(setenv("UBFTESTENV", "ENVTEST", EXTRUE), EXSUCCEED);
0086     assert_equal(setenv("UBFTESTENV2", "ENVTEST2", EXTRUE), EXSUCCEED);
0087 
0088     
0089     for (i=0; i < 100; i++)
0090     {
0091         /* Encrypt "Enduro/X" */
0092 
0093         assert_equal(ndrx_crypto_enc_string(ENC_SUBST_STRING, encdata, sizeof(encdata)), 
0094                 EXSUCCEED);
0095 
0096         snprintf(fmt, sizeof(fmt), "Hello ${dec=%s} ${UBFTESTENV} "
0097             "${dec=%s} ${UBFTESTENV2} from C", encdata, encdata);
0098 
0099         NDRX_LOG(log_debug, "String with decrypt func: [%s]", fmt);
0100 
0101         ndrx_str_env_subs_len(fmt, sizeof(fmt));
0102 
0103         assert_string_equal(fmt, "Hello Enduro/X ENVTEST Enduro/X ENVTEST2 from C");
0104     }
0105 }
0106 
0107 /**
0108  * LMDB/EXDB tests
0109  * @return
0110  */
0111 TestSuite *ubf_nstd_crypto(void)
0112 {
0113     TestSuite *suite = create_test_suite();
0114 
0115     /*
0116     set_setup(suite, basic_setup1);
0117     set_teardown(suite, basic_teardown1);
0118     */
0119     
0120     add_test(suite, test_crypto_enc_string);
0121     add_test(suite, test_crypto_subst_func);
0122             
0123     return suite;
0124 }