Dmitry V. Levin | 2aa9b32 | 2016-04-03 16:37:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Check decoding and dumping of read and write syscalls. |
| 3 | * |
| 4 | * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include "tests.h" |
| 31 | |
| 32 | #include <fcntl.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | static void |
| 38 | dump_str(const char *str, const unsigned int len) |
| 39 | { |
| 40 | static const char dots[16] = "................"; |
| 41 | unsigned int i; |
| 42 | |
| 43 | for (i = 0; i < len; i += 16) { |
| 44 | unsigned int n = len - i > 16 ? 16 : len - i; |
| 45 | const char *dump = hexdump_memdup(str + i, n); |
| 46 | |
| 47 | tprintf(" | %05x %-49s %-16.*s |\n", |
| 48 | i, dump, n, dots); |
| 49 | |
| 50 | free((void *) dump); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | print_hex(const char *str, const unsigned int len) |
| 56 | { |
| 57 | const unsigned char *ustr = (const unsigned char *) str; |
| 58 | unsigned int i; |
| 59 | |
| 60 | for (i = 0; i < len; ++i) { |
| 61 | unsigned int c = ustr[i]; |
| 62 | |
| 63 | switch (c) { |
| 64 | case '\t': |
| 65 | tprintf("\\t"); break; |
| 66 | case '\n': |
| 67 | tprintf("\\n"); break; |
| 68 | case '\v': |
| 69 | tprintf("\\v"); break; |
| 70 | case '\f': |
| 71 | tprintf("\\f"); break; |
| 72 | case '\r': |
| 73 | tprintf("\\r"); break; |
| 74 | default: |
| 75 | tprintf("\\%o", ustr[i]); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | test_dump(const unsigned int len) |
| 82 | { |
| 83 | static char *buf; |
| 84 | |
| 85 | if (buf) { |
| 86 | size_t ps1 = get_page_size() - 1; |
| 87 | buf = (void *) (((size_t) buf + ps1) & ~ps1) - len; |
| 88 | } else { |
| 89 | buf = tail_alloc(len); |
| 90 | } |
| 91 | |
| 92 | long rc = read(0, buf, len); |
| 93 | if (rc != (int) len) |
| 94 | perror_msg_and_fail("read: expected %d, returned %ld", |
| 95 | len, rc); |
| 96 | |
| 97 | tprintf("%s(%d, \"", "read", 0); |
| 98 | print_hex(buf, len); |
| 99 | tprintf("\", %d) = %ld\n", len, rc); |
| 100 | dump_str(buf, len); |
| 101 | |
| 102 | unsigned int i; |
| 103 | for (i = 0; i < len; ++i) |
| 104 | buf[i] = i; |
| 105 | |
| 106 | rc = write(1, buf, len); |
| 107 | if (rc != (int) len) |
| 108 | perror_msg_and_fail("write: expected %d, returned %ld", |
| 109 | len, rc); |
| 110 | |
| 111 | tprintf("%s(%d, \"", "write", 1); |
| 112 | print_hex(buf, len); |
| 113 | tprintf("\", %d) = %ld\n", len, rc); |
| 114 | dump_str(buf, len); |
| 115 | |
| 116 | if (!len) |
| 117 | buf = 0; |
| 118 | } |
| 119 | |
| 120 | int |
| 121 | main(void) |
| 122 | { |
| 123 | tprintf("%s", ""); |
| 124 | |
| 125 | static char tmp[] = "read-write-tmpfile"; |
| 126 | if (open(tmp, O_CREAT|O_RDONLY|O_TRUNC, 0600) != 0) |
| 127 | perror_msg_and_fail("creat: %s", tmp); |
| 128 | if (open(tmp, O_WRONLY) != 1) |
| 129 | perror_msg_and_fail("open: %s", tmp); |
| 130 | |
| 131 | static const char w_c[] = "0123456789abcde"; |
| 132 | const unsigned int w_len = LENGTH_OF(w_c); |
| 133 | const char *w_d = hexdump_strdup(w_c); |
| 134 | const void *w = tail_memdup(w_c, w_len); |
| 135 | |
| 136 | static const char r0_c[] = "01234567"; |
| 137 | const char *r0_d = hexdump_strdup(r0_c); |
| 138 | const unsigned int r0_len = (w_len + 1) / 2; |
| 139 | void *r0 = tail_alloc(r0_len); |
| 140 | |
| 141 | static const char r1_c[] = "89abcde"; |
| 142 | const char *r1_d = hexdump_strdup(r1_c); |
| 143 | const unsigned int r1_len = w_len - r0_len; |
| 144 | void *r1 = tail_alloc(w_len); |
| 145 | |
| 146 | void *efault = r1 - get_page_size(); |
| 147 | |
| 148 | long rc; |
| 149 | |
| 150 | rc = write(1, w, 0); |
| 151 | if (rc) |
| 152 | perror_msg_and_fail("write: expected 0, returned %ld", rc); |
| 153 | tprintf("write(1, \"\", 0) = 0\n"); |
| 154 | |
Dmitry V. Levin | a8e8205 | 2016-05-18 09:14:12 +0000 | [diff] [blame] | 155 | rc = write(1, efault, 1); |
Dmitry V. Levin | 2aa9b32 | 2016-04-03 16:37:43 +0000 | [diff] [blame] | 156 | if (rc != -1) |
| 157 | perror_msg_and_fail("write: expected -1 EFAULT" |
| 158 | ", returned %ld", rc); |
Dmitry V. Levin | a8e8205 | 2016-05-18 09:14:12 +0000 | [diff] [blame] | 159 | tprintf("write(1, %p, 1) = -1 EFAULT (%m)\n", efault); |
Dmitry V. Levin | 2aa9b32 | 2016-04-03 16:37:43 +0000 | [diff] [blame] | 160 | |
| 161 | rc = write(1, w, w_len); |
| 162 | if (rc != (int) w_len) |
| 163 | perror_msg_and_fail("write: expected %u, returned %ld", |
| 164 | w_len, rc); |
| 165 | tprintf("write(1, \"%s\", %u) = %ld\n" |
| 166 | " | 00000 %-49s %-16s |\n", |
| 167 | w_c, w_len, rc, w_d, w_c); |
| 168 | close(1); |
| 169 | |
| 170 | rc = read(0, r0, 0); |
| 171 | if (rc) |
| 172 | perror_msg_and_fail("read: expected 0, returned %ld", rc); |
| 173 | tprintf("read(0, \"\", 0) = 0\n"); |
| 174 | |
| 175 | rc = read(0, efault, 1); |
| 176 | if (rc != -1) |
| 177 | perror_msg_and_fail("read: expected -1, returned %ld", rc); |
| 178 | tprintf("read(0, %p, 1) = -1 EFAULT (%m)\n", efault); |
| 179 | |
| 180 | rc = read(0, r0, r0_len); |
| 181 | if (rc != (int) r0_len) |
| 182 | perror_msg_and_fail("read: expected %u, returned %ld", |
| 183 | r0_len, rc); |
| 184 | tprintf("read(0, \"%s\", %u) = %ld\n" |
| 185 | " | 00000 %-49s %-16s |\n", |
| 186 | r0_c, r0_len, rc, r0_d, r0_c); |
| 187 | |
| 188 | rc = read(0, r1, w_len); |
| 189 | if (rc != (int) r1_len) |
| 190 | perror_msg_and_fail("read: expected %u, returned %ld", |
| 191 | r1_len, rc); |
| 192 | tprintf("read(0, \"%s\", %u) = %ld\n" |
| 193 | " | 00000 %-49s %-16s |\n", |
| 194 | r1_c, w_len, rc, r1_d, r1_c); |
| 195 | close(0); |
| 196 | |
| 197 | if (open("/dev/zero", O_RDONLY)) |
| 198 | perror_msg_and_fail("open"); |
| 199 | |
| 200 | if (open("/dev/null", O_WRONLY) != 1) |
| 201 | perror_msg_and_fail("open"); |
| 202 | |
| 203 | unsigned int i; |
| 204 | for (i = 0; i <= 32; ++i) |
| 205 | test_dump(i); |
| 206 | |
| 207 | tprintf("+++ exited with 0 +++\n"); |
| 208 | return 0; |
| 209 | } |