ESAPI-C 1.0
The OWASP Enterprise Security API for C

test/src/crypto_test.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include "minunit.h"
00004 #include "crypto.h"
00005 
00006 int tests_run = 0;
00007 
00008 void test_esapi_encrypt(void) 
00009 {
00010    (void)fprintf(stdout,"Testing %s\n", __func__);
00011    char *plaintext = "Hello";
00012    char *encrypted;
00013    char *decrypted;
00014 
00015    // FIXME: Why malloc these buffers?
00016 //   if((encrypted = (char *)malloc(1024)) == 0) {
00017 //      (void)fprintf(stderr, "malloc failure in %s\n", __func__);
00018 //      exit(EXIT_FAILURE);
00019 //   }
00020 
00021 //   if((decrypted = (char *)malloc(1024)) == 0) {
00022 //      (void)fprintf(stderr, "malloc failure in %s\n", __func__);
00023 //      exit(EXIT_FAILURE);
00024 //   }
00025    
00026    struct esapi_ctx *ctx = load_security_context("../configuration/ESAPI.properties");
00027 
00028    encrypted = esapi_encrypt(ctx, plaintext);
00029    decrypted = esapi_decrypt(ctx, encrypted);
00030    TEST(encrypted != 0);
00031    TEST(decrypted != 0);
00032    TEST(strcmp(plaintext, decrypted) == 0);
00033    if (encrypted != 0)
00034            free(encrypted);
00035    if (decrypted != 0)
00036            free(decrypted);
00037 }
00038 
00039 void test_esapi_decrypt(void) 
00040 {
00041    (void)fprintf(stdout,"Testing %s\n", __func__);
00042 
00043    test_esapi_encrypt();
00044 }
00045 
00046 /*
00047  * Note: Unless we fix the seed, we cannot test the hashed value.
00048  */
00049 void test_esapi_hash(void) 
00050 {
00051    (void)fprintf(stdout,"Testing %s\n", __func__);
00052 
00053    struct esapi_ctx *ctx = load_security_context("../configuration/ESAPI.properties");
00054 
00055    TEST(ctx != 0);
00056    char *plaintext = "Hello";
00057    char *hashed = esapi_hash(ctx, plaintext);
00058    TEST(strcmp(plaintext, hashed) != 0);
00059    free(hashed);
00060 }
00061 
00062 /*
00063  * Note: Unless we fix the seed, we cannot test the random value.
00064  */
00065 void test_esapi_fill_random_token(void) 
00066 {
00067    (void)fprintf(stdout,"Testing %s\n", __func__);
00068 
00069    struct esapi_ctx *ctx = load_security_context("../configuration/ESAPI.properties");
00070    TEST(ctx != 0);
00071    TEST(ctx->max_file_size == 100000);
00072    char buffer[9];
00073    esapi_fill_random_token(ctx, buffer, 8);
00074    buffer[8] = '\0';
00075    TEST(strlen(buffer) == 8);
00076         /* FIXME: Maybe also verify that buffer contains only chars from esapi_alphabet? */
00077 }
00078 
00079 
00080 /*
00081 char *esapi_encrypt(struct esapi_ctx *, char *);
00082 char *esapi_decrypt(struct esapi_ctx *, char *);
00083 char *esapi_hash(struct esapi_ctx *, const char *);
00084 void esapi_fill_random_token(struct esapi_ctx *, char *, int);
00085 char *esapi_sign(struct esapi_ctx *, char *);
00086 int esapi_verify_signature(struct esapi_ctx *, char *, char *);
00087 gcry_sexp_t convert_to_asymmetric_key(void *, int);
00088 */
00089 
00090 int main(void) {
00091    esapi_crypto_init();
00092    test_esapi_encrypt();
00093    test_esapi_decrypt();
00094    test_esapi_hash();
00095    test_esapi_fill_random_token();
00096    (void)fprintf(stdout, "%s: Pass <%i>, Fail<%i>\n", __FILE__, passed, failed);   
00097    return(EXIT_SUCCESS);
00098 }
 All Data Structures Files Functions Variables Typedefs Defines