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-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 
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <cgreen/cgreen.h>
0038 #include <ubf.h>
0039 #include <ndrstandard.h>
0040 #include <string.h>
0041 #include <ndebug.h>
0042 #include <excrypto.h>
0043 #include "test.fd.h"
0044 #include "ubfunit1.h"
0045 
0046 /**
0047  * Test string encrypt & decrypt
0048  */
0049 Ensure(test_crypto_enc_string)
0050 {
0051     char buf[1024]="";
0052     char buf2[1024]="";
0053     int i;
0054     long len;
0055 #define ENC_TEST_STRING "HELLO STRING _ 123 hello test"
0056     
0057     for (i=0; i<100; i++)
0058     {
0059         len=sizeof(buf);
0060         assert_equal(
0061                 ndrx_crypto_enc_string(ENC_TEST_STRING, buf, &len),
0062                 EXSUCCEED);
0063 
0064         assert_equal(strlen(buf)+1, len);
0065         
0066         NDRX_LOG(log_debug, "Encrypted string: [%s]", buf);
0067 
0068         len=sizeof(buf2);
0069         assert_equal(
0070                 ndrx_crypto_dec_string(buf, buf2, &len),
0071                 EXSUCCEED);
0072 
0073         /* decrypted strings must be equal... */
0074         assert_string_equal(buf2, ENC_TEST_STRING);
0075         assert_equal(strlen(buf2)+1, len);
0076     }
0077 }
0078 
0079 /**
0080  * Test substitute functions
0081  * Bug #268 issues with env substituion after crypto
0082  */
0083 Ensure(test_crypto_subst_func)
0084 {
0085     char encdata[PATH_MAX];
0086     char fmt[PATH_MAX];
0087     int i;
0088     long len;
0089     
0090 #define ENC_SUBST_STRING "Enduro/X"
0091 
0092     assert_equal(setenv("UBFTESTENV", "ENVTEST", EXTRUE), EXSUCCEED);
0093     assert_equal(setenv("UBFTESTENV2", "ENVTEST2", EXTRUE), EXSUCCEED);
0094 
0095     
0096     for (i=0; i < 100; i++)
0097     {
0098         /* Encrypt "Enduro/X" */
0099 
0100         len=sizeof(encdata);
0101         assert_equal(ndrx_crypto_enc_string(ENC_SUBST_STRING, encdata, &len), 
0102                 EXSUCCEED);
0103 
0104         snprintf(fmt, sizeof(fmt), "Hello ${dec=%s} ${UBFTESTENV} "
0105             "${dec=%s} ${UBFTESTENV2} from C", encdata, encdata);
0106 
0107         NDRX_LOG(log_debug, "String with decrypt func: [%s]", fmt);
0108 
0109         ndrx_str_env_subs_len(fmt, sizeof(fmt));
0110 
0111         assert_string_equal(fmt, "Hello Enduro/X ENVTEST Enduro/X ENVTEST2 from C");
0112     }
0113 }
0114 
0115 /**
0116  * LMDB/EXDB tests
0117  * @return
0118  */
0119 TestSuite *ubf_nstd_crypto(void)
0120 {
0121     TestSuite *suite = create_test_suite();
0122 
0123     /*
0124     set_setup(suite, basic_setup1);
0125     set_teardown(suite, basic_teardown1);
0126     */
0127     
0128     add_test(suite, test_crypto_enc_string);
0129     add_test(suite, test_crypto_subst_func);
0130             
0131     return suite;
0132 }
0133 /* vim: set ts=4 sw=4 et smartindent: */