Imported Upstream version 3.3.3
[debian/amanda] / perl / Amanda / Tests.swg
1 /*
2  * Copyright (c) 2008-2012 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21
22 %module "Amanda::Tests"
23 %include "amglue/amglue.swg"
24 %include "exception.i"
25
26 %include "Amanda/Tests.pod"
27
28 %{
29 #include "simpleprng.h"
30 %}
31
32 %inline %{
33
34 /*
35  * exercise bigint.c / integer.swg
36  */
37
38 char *take_guint64(guint64 input) {
39     if (input == G_MAXUINT64) return "MAX";
40     if (input == 0) return "ZERO";
41     return "OTHER";
42 }
43
44 char *take_gint64(gint64 input) {
45     if (input == G_MAXINT64) return "MAX";
46     if (input == G_MININT64) return "MIN";
47     if (input == 0) return "ZERO";
48     return "OTHER";
49 }
50
51 char *take_guint32(guint32 input) {
52     if (input == G_MAXUINT32) return "MAX";
53     if (input == 0) return "ZERO";
54     return "OTHER";
55 }
56
57 char *take_gint32(gint32 input) {
58     if (input == G_MAXINT32) return "MAX";
59     if (input == G_MININT32) return "MIN";
60     if (input == 0) return "ZERO";
61     return "OTHER";
62 }
63
64 char *take_guint16(guint16 input) {
65     if (input == G_MAXUINT16) return "MAX";
66     if (input == 0) return "ZERO";
67     return "OTHER";
68 }
69
70 char *take_gint16(gint16 input) {
71     if (input == G_MAXINT16) return "MAX";
72     if (input == G_MININT16) return "MIN";
73     if (input == 0) return "ZERO";
74     return "OTHER";
75 }
76
77 char *take_guint8(guint8 input) {
78     if (input == G_MAXUINT8) return "MAX";
79     if (input == 0) return "ZERO";
80     return "OTHER";
81 }
82
83 char *take_gint8(gint8 input) {
84     if (input == G_MAXINT8) return "MAX";
85     if (input == G_MININT8) return "MIN";
86     if (input == 0) return "ZERO";
87     return "OTHER";
88 }
89
90
91 guint64 give_guint64(char *input) {
92     if (input[0] == '+') return G_MAXUINT64;
93     return 0;
94 }
95
96 gint64 give_gint64(char *input) {
97     if (input[0] == '-') return G_MININT64;
98     if (input[0] == '+') return G_MAXINT64;
99     return 0;
100 }
101
102 guint32 give_guint32(char *input) {
103     if (input[0] == '+') return G_MAXUINT32;
104     return 0;
105 }
106
107 gint32 give_gint32(char *input) {
108     if (input[0] == '-') return G_MININT32;
109     if (input[0] == '+') return G_MAXINT32;
110     return 0;
111 }
112
113 guint16 give_guint16(char *input) {
114     if (input[0] == '+') return G_MAXUINT16;
115     return 0;
116 }
117
118 gint16 give_gint16(char *input) {
119     if (input[0] == '-') return G_MININT16;
120     if (input[0] == '+') return G_MAXINT16;
121     return 0;
122 }
123
124 guint8 give_guint8(char *input) {
125     if (input[0] == '+') return G_MAXUINT8;
126     return 0;
127 }
128
129 gint8 give_gint8(char *input) {
130     if (input[0] == '-') return G_MININT8;
131     if (input[0] == '+') return G_MAXINT8;
132     return 0;
133 }
134 %}
135
136 /*
137  * Various compiler/system characteristics
138  */
139
140 %inline %{
141
142 int sizeof_size_t(void) {
143     return sizeof(size_t);
144 }
145
146 %}
147
148 /*
149  * simpleprng interface
150  */
151
152 %inline %{
153
154 /* write LENGTH bytes of random data to FILENAME, seeded with SEED */
155 void
156 write_random_file(guint32 seed, size_t length, char *filename) {
157     simpleprng_state_t prng;
158     int fd;
159     char buf[10240];
160
161     simpleprng_seed(&prng, seed);
162
163     fd = open(filename, O_CREAT|O_WRONLY|O_TRUNC, 0666);
164     if (fd < 0)
165         g_critical(_("Could not open test file '%s': %s"), filename, strerror(errno));
166
167     while (length) {
168         size_t to_write = min(sizeof(buf), length);
169         size_t written;
170
171         simpleprng_fill_buffer(&prng, buf, to_write);
172
173         written = full_write(fd, buf, to_write);
174         if (written < to_write)
175             g_critical(_("Error writing test file: %s"), strerror(errno));
176
177         length -= written;
178     }
179
180     close(fd);
181 }
182
183 /* read LENGTH bytes of random data from FILENAME verifying it against
184  * a PRNG seeded with SEED.  Sends any error messages to stderr.
185  *
186  * If check_eof is true, then check that the file is exactly LENGTH bytes long;
187  * otherwise, trailing bytes (such as zero padding from a Device) are ignored.
188  */
189 gboolean
190 verify_random_file(guint32 seed, size_t length, char *filename, gboolean check_eof) {
191     simpleprng_state_t prng;
192     int fd;
193     char buf[10240];
194
195     simpleprng_seed(&prng, seed);
196
197     fd = open(filename, O_RDONLY, 0666);
198     if (fd < 0)
199         g_critical(_("Could not open test file '%s': %s"), filename, strerror(errno));
200
201     while (length) {
202         size_t to_read = min(sizeof(buf), length);
203         size_t bytes_read;
204
205         bytes_read = full_read(fd, buf, to_read);
206         if (bytes_read < to_read) {
207             if (errno) {
208                 g_critical(_("Error reading test file: %s"), strerror(errno));
209             } else {
210                 g_fprintf(stderr, _("Verify of '%s' failed: early EOF with %zd bytes left\n"),
211                         filename, length - bytes_read);
212                 goto error;
213             }
214         }
215
216         if (!simpleprng_verify_buffer(&prng, buf, bytes_read))
217             goto error;
218
219         length -= bytes_read;
220     }
221
222     /* verify that the file contains no extra bytes */
223     if (check_eof) {
224         if (read(fd, buf, 1)) {
225             g_fprintf(stderr, _("Verify of '%s' failed: file is too long\n"), filename);
226             goto error;
227         }
228     }
229
230     close(fd);
231     return TRUE;
232
233 error:
234     close(fd);
235     return FALSE;
236 }
237 %}
238
239 /*
240  * Simple threading test (start a thread and join it)
241  */
242
243 %{
244 static gpointer
245 thread_fn(gpointer data)
246 {
247     guint *d = data;
248     *d = 1;
249     return NULL;
250 }
251 %}
252
253 %inline %{
254 void
255 try_threads(void)
256 {
257     guint data = 0;
258     GThread *thd;
259
260     glib_init();
261
262     thd = g_thread_create(thread_fn, (gpointer)&data, TRUE, NULL);
263     g_thread_join(thd);
264     g_assert(data == 1);
265 }
266 %}