7b433c474943694ee107e2b9cb77b2acd9502acc
[oweals/gnunet.git] / src / abe / test_cpabe.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2002, 2003, 2004, 2006 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 */
19 /**
20  * @author Martin Schanzenbach
21  * @file util/test_crypto_abe.c
22  * @brief test for ABE ciphers
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_abe_lib.h"
27
28 #define TESTSTRING "Hello World!"
29
30 static int
31 testAbecipher ()
32 {
33   struct GNUNET_ABE_AbeMasterKey *msk;
34   struct GNUNET_ABE_AbeKey *key;
35   char *result;
36   char **attrs;
37   int size;
38   char *res;
39   msk = GNUNET_ABE_cpabe_create_master_key ();
40   size = GNUNET_ABE_cpabe_encrypt (TESTSTRING, strlen (TESTSTRING) + 1,
41                                       "testattr", //Policy
42                                       msk,
43                                       (void*)&result);
44   GNUNET_assert (-1 != size);
45   attrs = GNUNET_malloc (2 * sizeof (char*));
46   attrs[0] = "testattr";
47   attrs[1] = NULL;
48   key = GNUNET_ABE_cpabe_create_key (msk,
49                                         attrs);
50
51   size = GNUNET_ABE_cpabe_decrypt (result, size,
52                                       key,
53                                       (void*)&res);
54   if (strlen (TESTSTRING) + 1 != size)
55   {
56     printf ("abeciphertest failed: decryptBlock returned %d\n", size);
57     return 1;
58   }
59   if (0 != strcmp (res, TESTSTRING))
60   {
61     printf ("abeciphertest failed: %s != %s\n", res, TESTSTRING);
62     return 1;
63   }
64   else
65     return 0;
66 }
67
68
69 int
70 main (int argc, char *argv[])
71 {
72   int failureCount = 0;
73
74   GNUNET_log_setup ("test-crypto-abe", "WARNING", NULL);
75   failureCount += testAbecipher ();
76
77   if (failureCount != 0)
78   {
79     printf ("%d TESTS FAILED!\n", failureCount);
80     return -1;
81   }
82   return 0;
83 }
84
85 /* end of test_crypto_aes.c */