7 #include "../lib/arc4.h"
12 vmsg (const char *format, va_list args, bool failure)
14 if (quiet && !failure)
17 printf ("(%s) ", test_name);
18 vprintf (format, args);
25 msg (const char *format, ...)
29 va_start (args, format);
30 vmsg (format, args, false);
35 fail (const char *format, ...)
41 va_start (args, format);
42 vmsg (format, args, true);
49 check (bool success, const char *format, ...)
53 va_start (args, format);
57 vmsg (format, args, false);
61 vmsg (format, args, true);
68 seq_test (const char *filename, void *buf, size_t size,
69 size_t initial_size, int seed,
70 size_t (*block_size_func) (void),
71 void (*check_func) (int fd, long ofs))
73 static struct arc4 arc4;
77 arc4_init (&arc4, &seed, sizeof seed);
78 arc4_crypt (&arc4, buf, size);
80 check (create (filename, initial_size), "create \"%s\"", filename);
81 check ((fd = open (filename)) > 1, "open \"%s\"", filename);
84 msg ("writing \"%s\"", filename);
87 size_t block_size = block_size_func ();
88 if (block_size > size - ofs)
89 block_size = size - ofs;
91 if (write (fd, buf + ofs, block_size) <= 0)
92 fail ("write %zu bytes at offset %zu in \"%s\" failed",
93 block_size, ofs, filename);
96 if (check_func != NULL)
99 msg ("close \"%s\"", filename);
101 check_file (filename, buf, size);
105 swap (void *a_, void *b_, size_t size)
111 for (i = 0; i < size; i++)
120 shuffle (void *buf_, size_t cnt, size_t size)
125 for (i = 0; i < cnt; i++)
127 size_t j = i + random_ulong () % (cnt - i);
128 swap (buf + i * size, buf + j * size, size);
133 check_file (const char *filename, const void *buf_, size_t size)
135 const char *buf = buf_;
140 check ((fd = open (filename)) > 1, "open \"%s\" for verification", filename);
145 size_t block_size = size - ofs;
146 if (block_size > sizeof block)
147 block_size = sizeof block;
149 if (read (fd, block, block_size) <= 0)
150 fail ("read %zu bytes at offset %zu in \"%s\" failed",
151 block_size, ofs, filename);
153 if (memcmp (buf + ofs, block, block_size))
155 if (block_size <= 512)
157 printf ("Expected data:\n");
158 hex_dump (ofs, buf + ofs, block_size, false);
159 printf ("Actually read data:\n");
160 hex_dump (ofs, block, block_size, false);
162 fail ("%zu bytes at offset %zu differed from expected",
169 msg ("close \"%s\"", filename);