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