00001 #include "tests/lib.h"
00002
00003 #include <random.h>
00004 #include <stdarg.h>
00005 #include <stdio.h>
00006 #include <string.h>
00007 #include <syscall.h>
00008
00009 const char *test_name;
00010 bool quiet = false;
00011
00012 static void
00013 vmsg (const char *format, va_list args, const char *suffix)
00014 {
00015
00016
00017
00018
00019
00020 static char buf[1024];
00021
00022 snprintf (buf, sizeof buf, "(%s) ", test_name);
00023 vsnprintf (buf + strlen (buf), sizeof buf - strlen (buf), format, args);
00024 strlcpy (buf + strlen (buf), suffix, sizeof buf - strlen (buf));
00025 write (STDOUT_FILENO, buf, strlen (buf));
00026 }
00027
00028 void
00029 msg (const char *format, ...)
00030 {
00031 va_list args;
00032
00033 if (quiet)
00034 return;
00035 va_start (args, format);
00036 vmsg (format, args, "\n");
00037 va_end (args);
00038 }
00039
00040 void
00041 fail (const char *format, ...)
00042 {
00043 va_list args;
00044
00045 va_start (args, format);
00046 vmsg (format, args, ": FAILED\n");
00047 va_end (args);
00048
00049 exit (1);
00050 }
00051
00052 static void
00053 swap (void *a_, void *b_, size_t size)
00054 {
00055 uint8_t *a = a_;
00056 uint8_t *b = b_;
00057 size_t i;
00058
00059 for (i = 0; i < size; i++)
00060 {
00061 uint8_t t = a[i];
00062 a[i] = b[i];
00063 b[i] = t;
00064 }
00065 }
00066
00067 void
00068 shuffle (void *buf_, size_t cnt, size_t size)
00069 {
00070 char *buf = buf_;
00071 size_t i;
00072
00073 for (i = 0; i < cnt; i++)
00074 {
00075 size_t j = i + random_ulong () % (cnt - i);
00076 swap (buf + i * size, buf + j * size, size);
00077 }
00078 }
00079
00080 void
00081 exec_children (const char *child_name, pid_t pids[], size_t child_cnt)
00082 {
00083 size_t i;
00084
00085 for (i = 0; i < child_cnt; i++)
00086 {
00087 char cmd_line[128];
00088 snprintf (cmd_line, sizeof cmd_line, "%s %zu", child_name, i);
00089 CHECK ((pids[i] = exec (cmd_line)) != PID_ERROR,
00090 "exec child %zu of %zu: \"%s\"", i + 1, child_cnt, cmd_line);
00091 }
00092 }
00093
00094 void
00095 wait_children (pid_t pids[], size_t child_cnt)
00096 {
00097 size_t i;
00098
00099 for (i = 0; i < child_cnt; i++)
00100 {
00101 int status = wait (pids[i]);
00102 CHECK (status == (int) i,
00103 "wait for child %zu of %zu returned %d (expected %zu)",
00104 i + 1, child_cnt, status, i);
00105 }
00106 }
00107
00108 void
00109 check_file_handle (int fd,
00110 const char *file_name, const void *buf_, size_t size)
00111 {
00112 const char *buf = buf_;
00113 size_t ofs = 0;
00114 size_t file_size;
00115
00116
00117
00118
00119 file_size = filesize (fd);
00120 if (file_size != size)
00121 msg ("size of %s (%zu) differs from expected (%zu)",
00122 file_name, file_size, size);
00123
00124
00125 while (ofs < size)
00126 {
00127 char block[512];
00128 size_t block_size, ret_val;
00129
00130 block_size = size - ofs;
00131 if (block_size > sizeof block)
00132 block_size = sizeof block;
00133
00134 ret_val = read (fd, block, block_size);
00135 if (ret_val != block_size)
00136 fail ("read of %zu bytes at offset %zu in \"%s\" returned %zu",
00137 block_size, ofs, file_name, ret_val);
00138
00139 compare_bytes (block, buf + ofs, block_size, ofs, file_name);
00140 ofs += block_size;
00141 }
00142
00143
00144 if (file_size != size)
00145 fail ("size of %s (%zu) differs from expected (%zu)",
00146 file_name, file_size, size);
00147
00148 msg ("verified contents of \"%s\"", file_name);
00149 }
00150
00151 void
00152 check_file (const char *file_name, const void *buf, size_t size)
00153 {
00154 int fd;
00155
00156 CHECK ((fd = open (file_name)) > 1, "open \"%s\" for verification",
00157 file_name);
00158 check_file_handle (fd, file_name, buf, size);
00159 msg ("close \"%s\"", file_name);
00160 close (fd);
00161 }
00162
00163 void
00164 compare_bytes (const void *read_data_, const void *expected_data_, size_t size,
00165 size_t ofs, const char *file_name)
00166 {
00167 const uint8_t *read_data = read_data_;
00168 const uint8_t *expected_data = expected_data_;
00169 size_t i, j;
00170 size_t show_cnt;
00171
00172 if (!memcmp (read_data, expected_data, size))
00173 return;
00174
00175 for (i = 0; i < size; i++)
00176 if (read_data[i] != expected_data[i])
00177 break;
00178 for (j = i + 1; j < size; j++)
00179 if (read_data[j] == expected_data[j])
00180 break;
00181
00182 quiet = false;
00183 msg ("%zu bytes read starting at offset %zu in \"%s\" differ "
00184 "from expected.", j - i, ofs + i, file_name);
00185 show_cnt = j - i;
00186 if (j - i > 64)
00187 {
00188 show_cnt = 64;
00189 msg ("Showing first differing %zu bytes.", show_cnt);
00190 }
00191 msg ("Data actually read:");
00192 hex_dump (ofs + i, read_data + i, show_cnt, true);
00193 msg ("Expected data:");
00194 hex_dump (ofs + i, expected_data + i, show_cnt, true);
00195 fail ("%zu bytes read starting at offset %zu in \"%s\" differ "
00196 "from expected", j - i, ofs + i, file_name);
00197 }