Back to home page

Enduro/X

 
 

    


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