diff options
Diffstat (limited to 'src/datacache/test_datacache_quota.c')
-rw-r--r-- | src/datacache/test_datacache_quota.c | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/src/datacache/test_datacache_quota.c b/src/datacache/test_datacache_quota.c new file mode 100644 index 000000000..1cad2a907 --- /dev/null +++ b/src/datacache/test_datacache_quota.c | |||
@@ -0,0 +1,152 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2006, 2009 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | 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 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | /* | ||
21 | * @file datacache/test_datacache_quota.c | ||
22 | * @brief Test for the quota code of the datacache implementations. | ||
23 | * @author Nils Durner | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_util_lib.h" | ||
27 | #include "gnunet_datacache_lib.h" | ||
28 | |||
29 | #define VERBOSE GNUNET_NO | ||
30 | |||
31 | #define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0) | ||
32 | |||
33 | static int ok; | ||
34 | |||
35 | /** | ||
36 | * Quota is 1 MB. Each iteration of the test puts in about 1 MB of | ||
37 | * data. We do 10 iterations. Afterwards we check that the data from | ||
38 | * the first 5 iterations has all been discarded and that at least | ||
39 | * some of the data from the last iteration is still there. | ||
40 | */ | ||
41 | static void | ||
42 | run (void *cls, | ||
43 | struct GNUNET_SCHEDULER_Handle *sched, | ||
44 | char *const *args, | ||
45 | const char *cfgfile, | ||
46 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
47 | { | ||
48 | struct GNUNET_DATACACHE_Handle *h; | ||
49 | GNUNET_HashCode k; | ||
50 | GNUNET_HashCode n; | ||
51 | unsigned int i; | ||
52 | unsigned int j; | ||
53 | char buf[3200]; | ||
54 | struct GNUNET_TIME_Absolute exp; | ||
55 | |||
56 | ok = 0; | ||
57 | h = GNUNET_DATACACHE_create (sched, | ||
58 | cfg, | ||
59 | "testcache"); | ||
60 | |||
61 | ASSERT (NULL != h); | ||
62 | exp = GNUNET_TIME_absolute_get (); | ||
63 | exp.value += 5 * 60 * 1000; | ||
64 | memset (buf, 1, sizeof (buf)); | ||
65 | memset (&k, 0, sizeof (GNUNET_HashCode)); | ||
66 | for (i = 0; i < 10; i++) | ||
67 | { | ||
68 | fprintf (stderr, "."); | ||
69 | GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n); | ||
70 | for (j = i; j < sizeof (buf); j += 10) | ||
71 | { | ||
72 | exp.value++; | ||
73 | buf[j] = i; | ||
74 | ASSERT (GNUNET_OK == | ||
75 | GNUNET_DATACACHE_put (h, | ||
76 | &k, | ||
77 | j, | ||
78 | buf, | ||
79 | 1+i, | ||
80 | exp)); | ||
81 | ASSERT (0 < GNUNET_DATACACHE_get (h, | ||
82 | &k, 1+i, | ||
83 | NULL, NULL)); | ||
84 | } | ||
85 | k = n; | ||
86 | } | ||
87 | fprintf (stderr, "\n"); | ||
88 | memset (&k, 0, sizeof (GNUNET_HashCode)); | ||
89 | for (i = 0; i < 10; i++) | ||
90 | { | ||
91 | fprintf (stderr, "."); | ||
92 | GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n); | ||
93 | if (i < 2) | ||
94 | ASSERT (0 == GNUNET_DATACACHE_get (h, | ||
95 | &k, 1+i, | ||
96 | NULL, NULL)); | ||
97 | if (i == 9) | ||
98 | ASSERT (0 < GNUNET_DATACACHE_get (h, | ||
99 | &k, 1+i, | ||
100 | NULL, NULL)); | ||
101 | k = n; | ||
102 | } | ||
103 | fprintf (stderr, "\n"); | ||
104 | return; | ||
105 | FAILURE: | ||
106 | if (h != NULL) | ||
107 | GNUNET_DATACACHE_destroy (h); | ||
108 | ok = GNUNET_SYSERR; | ||
109 | } | ||
110 | |||
111 | |||
112 | static int | ||
113 | check () | ||
114 | { | ||
115 | char *const argv[] = { "test-datacache-api-quota", | ||
116 | "-c", | ||
117 | "test_datacache_data.conf", | ||
118 | #if VERBOSE | ||
119 | "-L", "DEBUG", | ||
120 | #endif | ||
121 | NULL | ||
122 | }; | ||
123 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
124 | GNUNET_GETOPT_OPTION_END | ||
125 | }; | ||
126 | GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, | ||
127 | argv, "test-datacache-api-quota", "nohelp", | ||
128 | options, &run, NULL); | ||
129 | if (ok != 0) | ||
130 | fprintf (stderr, "Missed some testcases: %d\n", ok); | ||
131 | return ok; | ||
132 | } | ||
133 | |||
134 | |||
135 | int | ||
136 | main (int argc, char *argv[]) | ||
137 | { | ||
138 | int ret; | ||
139 | |||
140 | GNUNET_log_setup ("test-datacache-api-quota", | ||
141 | #if VERBOSE | ||
142 | "DEBUG", | ||
143 | #else | ||
144 | "WARNING", | ||
145 | #endif | ||
146 | NULL); | ||
147 | ret = check (); | ||
148 | |||
149 | return ret; | ||
150 | } | ||
151 | |||
152 | /* end of test_datacache_quota.c */ | ||