Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Test tpencrypt()/tpdecrypt() functionality
0003  *
0004  * @file atmiclt43_tp.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 "test43.h"
0050 /*---------------------------Externs------------------------------------*/
0051 /*---------------------------Macros-------------------------------------*/
0052 /*---------------------------Enums--------------------------------------*/
0053 /*---------------------------Typedefs-----------------------------------*/
0054 /*---------------------------Globals------------------------------------*/
0055 /*---------------------------Statics------------------------------------*/
0056 /*---------------------------Prototypes---------------------------------*/
0057 
0058 /**
0059  * Pass on CLI encrypted string value
0060  */
0061 int main(int argc, char** argv)
0062 {
0063     int ret = EXSUCCEED;
0064     char buf1[1024];
0065     char buf2[1064];
0066     char buf3[1024];
0067     long len1;
0068     long len2;
0069     long len3;
0070 #define EXPECTED_VAL    "hello Test 2"
0071     
0072     if (argc<2)
0073     {
0074         fprintf(stderr, "usage: %s <encrypted_data_block>\n", argv[0]);
0075         EXFAIL_OUT(ret);
0076     }
0077     
0078     /* check that we can decrypt */
0079     
0080     /* no space for EOS! */
0081     len1 = strlen(EXPECTED_VAL);
0082     if (EXSUCCEED==tpdecrypt(argv[1], 0, buf1, &len1, TPEX_STRING))
0083     {
0084         NDRX_LOG(log_error, "TESTERROR: Expected error due to size issues!");
0085         EXFAIL_OUT(ret);
0086     }
0087     
0088     NDRX_LOG(log_debug, "error: %d: %s", tperrno, tpstrerror(tperrno));
0089     
0090     if (tperrno!=TPELIMIT)
0091     {
0092         NDRX_LOG(log_error, "TESTERROR: Expected TPELIMIT got %d!", tperrno);
0093         EXFAIL_OUT(ret);
0094     }
0095     
0096     if (len1!= strlen(EXPECTED_VAL)+1)
0097     {
0098         NDRX_LOG(log_error, "TESTERROR: Invalid clr value estimate: %d vs %d!", 
0099                 len1, strlen(EXPECTED_VAL)+1);
0100         EXFAIL_OUT(ret);
0101     }
0102     
0103     /* got space OK */
0104     len1 = strlen(EXPECTED_VAL)+1;
0105     if (EXSUCCEED!=tpdecrypt(argv[1], 0, buf1, &len1, TPEX_STRING))
0106     {
0107         NDRX_LOG(log_error, "TESTERROR: Expected OK, got: %d: %s", 
0108                 tperrno, tpstrerror(tperrno));
0109         EXFAIL_OUT(ret);
0110     }
0111     
0112     /* check the value */
0113     NDRX_LOG(log_debug, "[%s] vs [%s]", EXPECTED_VAL, buf1);
0114     
0115     if (0!=strcmp(EXPECTED_VAL, buf1))
0116     {
0117         NDRX_LOG(log_error, "TESTERROR: Invalid decrypted value: [%s] vs [%s]", 
0118                 EXPECTED_VAL, buf1);
0119         EXFAIL_OUT(ret);
0120     }
0121     
0122     /* now encrypt the same data.. */
0123     len1=sizeof(buf1);
0124     
0125     if (EXSUCCEED!=tpencrypt(EXPECTED_VAL, 0, buf1, &len1, TPEX_STRING))
0126     {
0127         NDRX_LOG(log_error, "TESTERROR: Expected OK, got: %d: %s", 
0128                 tperrno, tpstrerror(tperrno));
0129         EXFAIL_OUT(ret);
0130     }
0131     
0132     if (0!=strcmp(argv[1], buf1))
0133     {
0134         NDRX_LOG(log_error, "TESTERROR: Invalid encrypted value: [%s] vs [%s]", 
0135                 argv[1], buf1);
0136         EXFAIL_OUT(ret);
0137     }
0138     
0139     if (strlen(argv[1])+1!=len1)
0140     {
0141         NDRX_LOG(log_error, "TESTERROR: Invalid output len of string enc: %d vs %ld", 
0142                 strlen(argv[1])+1, len1);
0143         EXFAIL_OUT(ret);
0144     }
0145     
0146     /* now work with raw data block... */
0147     memset(buf1, '9', sizeof(buf1));
0148     memset(buf2, '9', sizeof(buf2));
0149     
0150     len1=sizeof(buf1);
0151     len2=sizeof(buf2)-100;
0152     
0153     if (EXSUCCEED==tpencrypt(buf1, len1, buf2, &len2, 0))
0154     {
0155         NDRX_LOG(log_error, "TESTERROR: tpencrypt(bin) Expected fail, got OK");
0156         EXFAIL_OUT(ret);
0157     }
0158     
0159     if (tperrno!=TPELIMIT)
0160     {
0161         NDRX_LOG(log_error, "TESTERROR: Expected TPELIMIT got %d!", tperrno);
0162         EXFAIL_OUT(ret);
0163     }
0164     
0165     if (len2<len1)
0166     {
0167         NDRX_LOG(log_error, "TESTERROR: Expected output data size be >= to orgdata (%ld vs %dl)!",
0168                 len2, len1);
0169         EXFAIL_OUT(ret);
0170     }
0171     
0172     /*len2=sizeof(buf2); -> use estimated space... */
0173     if (EXSUCCEED!=tpencrypt(buf1, len1, buf2, &len2, 0))
0174     {
0175         NDRX_LOG(log_error, "TESTERROR: Expected OK tpencrypt(bin), got: %d: %s", 
0176                 tperrno, tpstrerror(tperrno));
0177         EXFAIL_OUT(ret);
0178     }
0179     
0180     /* now compare output data, must not match... */
0181     if (0==memcmp(buf1, buf2, sizeof(buf1)))
0182     {
0183         NDRX_LOG(log_error, "TESTERROR: Encrypted data blocks does not match org!");
0184         EXFAIL_OUT(ret);
0185     }
0186     
0187     /* len2 shall be bit smaller than org size, but equal or larger the initial data */
0188     
0189     if (len2>=sizeof(buf2) || len2<len1)
0190     {
0191         NDRX_LOG(log_error, "TESTERROR: Invalid sizes after encrypt: %ld, %z %ld",
0192                 len2, sizeof(buf2), len1);
0193         EXFAIL_OUT(ret);
0194     }
0195     /* decrypt data block */
0196     len3=sizeof(buf3);
0197     if (EXSUCCEED!=tpdecrypt(buf2, len2, buf3, &len3, 0))
0198     {
0199         NDRX_LOG(log_error, "TESTERROR: tpdecrypt(bin) Expected OK, got: %d: %s", 
0200                 tperrno, tpstrerror(tperrno));
0201         EXFAIL_OUT(ret);
0202     }
0203     
0204     /* now compare output data, must not match... */
0205     if (0!=memcmp(buf1, buf3, sizeof(buf3)))
0206     {
0207         NDRX_LOG(log_error, "TESTERROR: Encrypted data blocks does match org!");
0208         EXFAIL_OUT(ret);
0209     }
0210     
0211     /* dec size shall be original */
0212     if (len3!=len1)
0213     {
0214         NDRX_LOG(log_error, "TESTERROR: Decrypted size does not match org [%ld] vs [%ld]",
0215                 len3, len1);
0216         EXFAIL_OUT(ret);
0217     }
0218     
0219     /* check invalid arguments */
0220     
0221     NDRX_LOG(log_info, "Checking tpdecrypt invalid values");
0222     len3=sizeof(buf3);
0223     if (EXSUCCEED==tpdecrypt(NULL, len2, buf3, &len3, 0))
0224     {
0225         NDRX_LOG(log_error, "TESTERROR: tpdecrypt() expected inval");
0226         EXFAIL_OUT(ret);
0227     }
0228     
0229     if (tperrno!=TPEINVAL)
0230     {
0231         NDRX_LOG(log_error, "TESTERROR: Expected TPEINVAL got %d!", tperrno);
0232         EXFAIL_OUT(ret);
0233     }
0234     
0235     len3=sizeof(buf3);
0236     if (EXSUCCEED==tpdecrypt(buf2, 0, buf3, &len3, 0))
0237     {
0238         NDRX_LOG(log_error, "TESTERROR: tpdecrypt() expected inval");
0239         EXFAIL_OUT(ret);
0240     }
0241     
0242     if (tperrno!=TPEINVAL)
0243     {
0244         NDRX_LOG(log_error, "TESTERROR: Expected TPEINVAL got %d!", tperrno);
0245         EXFAIL_OUT(ret);
0246     }
0247     
0248     len3=sizeof(buf3);
0249     if (EXSUCCEED==tpdecrypt(buf2, len2, NULL, &len3, 0))
0250     {
0251         NDRX_LOG(log_error, "TESTERROR: tpdecrypt() expected inval");
0252         EXFAIL_OUT(ret);
0253     }
0254     
0255     if (tperrno!=TPEINVAL)
0256     {
0257         NDRX_LOG(log_error, "TESTERROR: Expected TPEINVAL got %d!", tperrno);
0258         EXFAIL_OUT(ret);
0259     }
0260     
0261     if (EXSUCCEED==tpdecrypt(buf2, len2, buf3, NULL, 0))
0262     {
0263         NDRX_LOG(log_error, "TESTERROR: tpdecrypt() expected inval");
0264         EXFAIL_OUT(ret);
0265     }
0266     
0267     if (tperrno!=TPEINVAL)
0268     {
0269         NDRX_LOG(log_error, "TESTERROR: Expected TPEINVAL got %d!", tperrno);
0270         EXFAIL_OUT(ret);
0271     }
0272     
0273     len3=0;
0274     if (EXSUCCEED==tpdecrypt(buf2, len2, buf3, &len3, 0))
0275     {
0276         NDRX_LOG(log_error, "TESTERROR: tpdecrypt() expected TPELIMIT");
0277         EXFAIL_OUT(ret);
0278     }
0279     
0280     if (tperrno!=TPELIMIT)
0281     {
0282         NDRX_LOG(log_error, "TESTERROR: Expected TPELIMIT got %d!", tperrno);
0283         EXFAIL_OUT(ret);
0284     }
0285     
0286     NDRX_LOG(log_info, "Checking tpencrypt invalid values");
0287     
0288     
0289     /* now work with raw data block... */
0290     memset(buf1, '9', sizeof(buf1));
0291     memset(buf2, '9', sizeof(buf2));
0292     
0293     len1=sizeof(buf1);
0294     len2=sizeof(buf2);
0295     
0296     if (EXSUCCEED==tpencrypt(NULL, len1, buf2, &len2, 0))
0297     {
0298         NDRX_LOG(log_error, "TESTERROR: tpencrypt() expected inval");
0299         EXFAIL_OUT(ret);
0300     }
0301     
0302     if (tperrno!=TPEINVAL)
0303     {
0304         NDRX_LOG(log_error, "TESTERROR: Expected TPEINVAL got %d!", tperrno);
0305         EXFAIL_OUT(ret);
0306     }
0307     
0308     if (EXSUCCEED==tpencrypt(buf1, 0, buf2, &len2, 0))
0309     {
0310         NDRX_LOG(log_error, "TESTERROR: tpencrypt() expected inval");
0311         EXFAIL_OUT(ret);
0312     }
0313     
0314     if (tperrno!=TPEINVAL)
0315     {
0316         NDRX_LOG(log_error, "TESTERROR: Expected TPEINVAL got %d!", tperrno);
0317         EXFAIL_OUT(ret);
0318     }
0319     
0320     if (tperrno!=TPEINVAL)
0321     {
0322         NDRX_LOG(log_error, "TESTERROR: Expected TPEINVAL got %d!", tperrno);
0323         EXFAIL_OUT(ret);
0324     }
0325     
0326     len1=sizeof(buf1);
0327     if (EXSUCCEED==tpencrypt(buf1, len1, NULL, &len2, 0))
0328     {
0329         NDRX_LOG(log_error, "TESTERROR: tpencrypt() expected inval");
0330         EXFAIL_OUT(ret);
0331     }
0332     
0333     if (tperrno!=TPEINVAL)
0334     {
0335         NDRX_LOG(log_error, "TESTERROR: Expected TPEINVAL got %d!", tperrno);
0336         EXFAIL_OUT(ret);
0337     }
0338     
0339     
0340     len1=sizeof(buf1);
0341     if (EXSUCCEED==tpencrypt(buf1, len1, buf2, NULL, 0))
0342     {
0343         NDRX_LOG(log_error, "TESTERROR: tpencrypt() expected inval");
0344         EXFAIL_OUT(ret);
0345     }
0346     
0347     if (tperrno!=TPEINVAL)
0348     {
0349         NDRX_LOG(log_error, "TESTERROR: Expected TPEINVAL got %d!", tperrno);
0350         EXFAIL_OUT(ret);
0351     }
0352     
0353     
0354 out:
0355     
0356     fprintf(stderr, "Exit with %d\n", ret);
0357 
0358     return ret;
0359 }
0360 /* vim: set ts=4 sw=4 et smartindent: */