[57914494] | 1 | /*
|
---|
[f2d88f3] | 2 | * Copyright (c) 2019 Jiri Svoboda
|
---|
[57914494] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <errno.h>
|
---|
[c1694b6b] | 30 | #include <str_error.h>
|
---|
[57914494] | 31 | #include <ipc/services.h>
|
---|
| 32 | #include <io/chardev.h>
|
---|
| 33 | #include <loc.h>
|
---|
| 34 | #include "../tester.h"
|
---|
| 35 |
|
---|
| 36 | #define SMALL_BUFFER_SIZE 64
|
---|
| 37 | #define LARGE_BUFFER_SIZE (DATA_XFER_LIMIT * 4)
|
---|
| 38 |
|
---|
| 39 | static char small_buffer[SMALL_BUFFER_SIZE];
|
---|
| 40 | static char large_buffer[LARGE_BUFFER_SIZE];
|
---|
| 41 |
|
---|
| 42 | /** Test device that always performs small transfers. */
|
---|
| 43 | static const char *test_chardev1_smallx(void)
|
---|
| 44 | {
|
---|
| 45 | chardev_t *chardev;
|
---|
| 46 | service_id_t sid;
|
---|
| 47 | async_sess_t *sess;
|
---|
| 48 | size_t nbytes;
|
---|
[b7fd2a0] | 49 | errno_t rc;
|
---|
[57914494] | 50 |
|
---|
| 51 | TPRINTF("Test small transfer character device operations\n");
|
---|
| 52 |
|
---|
| 53 | rc = loc_service_get_id(SERVICE_NAME_CHARDEV_TEST_SMALLX, &sid, 0);
|
---|
| 54 | if (rc != EOK) {
|
---|
| 55 | return "Failed resolving test device "
|
---|
| 56 | SERVICE_NAME_CHARDEV_TEST_SMALLX;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | sess = loc_service_connect(sid, INTERFACE_DDF, 0);
|
---|
| 60 | if (sess == NULL)
|
---|
| 61 | return "Failed connecting test device";
|
---|
| 62 |
|
---|
| 63 | rc = chardev_open(sess, &chardev);
|
---|
| 64 | if (rc != EOK) {
|
---|
| 65 | async_hangup(sess);
|
---|
| 66 | return "Failed opening test device";
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | rc = chardev_write(chardev, small_buffer, SMALL_BUFFER_SIZE, &nbytes);
|
---|
| 70 | if (rc != EOK) {
|
---|
| 71 | chardev_close(chardev);
|
---|
| 72 | async_hangup(sess);
|
---|
| 73 | return "Failed sending data";
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | TPRINTF("Sent %zu bytes\n", nbytes);
|
---|
| 77 |
|
---|
[f2d88f3] | 78 | rc = chardev_read(chardev, small_buffer, SMALL_BUFFER_SIZE, &nbytes,
|
---|
| 79 | chardev_f_none);
|
---|
[57914494] | 80 | if (rc != EOK) {
|
---|
| 81 | chardev_close(chardev);
|
---|
| 82 | async_hangup(sess);
|
---|
| 83 | return "Failed receiving data";
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | TPRINTF("Received %zu bytes\n", nbytes);
|
---|
| 87 |
|
---|
| 88 | chardev_close(chardev);
|
---|
| 89 | async_hangup(sess);
|
---|
| 90 |
|
---|
| 91 | TPRINTF("Done\n");
|
---|
| 92 | return NULL;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /** Test device that always performs large transfers. */
|
---|
| 96 | static const char *test_chardev1_largex(void)
|
---|
| 97 | {
|
---|
| 98 | chardev_t *chardev;
|
---|
| 99 | service_id_t sid;
|
---|
| 100 | async_sess_t *sess;
|
---|
| 101 | size_t nbytes;
|
---|
[b7fd2a0] | 102 | errno_t rc;
|
---|
[57914494] | 103 |
|
---|
| 104 | TPRINTF("Test large transfer character device operations\n");
|
---|
| 105 |
|
---|
| 106 | rc = loc_service_get_id(SERVICE_NAME_CHARDEV_TEST_LARGEX, &sid, 0);
|
---|
| 107 | if (rc != EOK) {
|
---|
| 108 | return "Failed resolving test device "
|
---|
| 109 | SERVICE_NAME_CHARDEV_TEST_LARGEX;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | sess = loc_service_connect(sid, INTERFACE_DDF, 0);
|
---|
| 113 | if (sess == NULL)
|
---|
| 114 | return "Failed connecting test device";
|
---|
| 115 |
|
---|
| 116 | rc = chardev_open(sess, &chardev);
|
---|
| 117 | if (rc != EOK) {
|
---|
| 118 | async_hangup(sess);
|
---|
| 119 | return "Failed opening test device";
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | rc = chardev_write(chardev, large_buffer, LARGE_BUFFER_SIZE, &nbytes);
|
---|
| 123 | if (rc != EOK) {
|
---|
| 124 | chardev_close(chardev);
|
---|
| 125 | async_hangup(sess);
|
---|
| 126 | return "Failed sending data";
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | TPRINTF("Sent %zu bytes\n", nbytes);
|
---|
| 130 |
|
---|
[f2d88f3] | 131 | rc = chardev_read(chardev, large_buffer, LARGE_BUFFER_SIZE, &nbytes,
|
---|
| 132 | chardev_f_none);
|
---|
[57914494] | 133 | if (rc != EOK) {
|
---|
| 134 | chardev_close(chardev);
|
---|
| 135 | async_hangup(sess);
|
---|
| 136 | return "Failed receiving data";
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | TPRINTF("Received %zu bytes\n", nbytes);
|
---|
| 140 |
|
---|
| 141 | chardev_close(chardev);
|
---|
| 142 | async_hangup(sess);
|
---|
| 143 |
|
---|
| 144 | TPRINTF("Done\n");
|
---|
| 145 | return NULL;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /** Test device where all transfers return partial success. */
|
---|
| 149 | static const char *test_chardev1_partialx(void)
|
---|
| 150 | {
|
---|
| 151 | chardev_t *chardev;
|
---|
| 152 | service_id_t sid;
|
---|
| 153 | async_sess_t *sess;
|
---|
| 154 | size_t nbytes;
|
---|
[b7fd2a0] | 155 | errno_t rc;
|
---|
[57914494] | 156 |
|
---|
| 157 | TPRINTF("Test partially-successful character device operations\n");
|
---|
| 158 |
|
---|
| 159 | rc = loc_service_get_id(SERVICE_NAME_CHARDEV_TEST_PARTIALX, &sid, 0);
|
---|
| 160 | if (rc != EOK) {
|
---|
| 161 | return "Failed resolving test device "
|
---|
| 162 | SERVICE_NAME_CHARDEV_TEST_SMALLX;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | sess = loc_service_connect(sid, INTERFACE_DDF, 0);
|
---|
| 166 | if (sess == NULL)
|
---|
| 167 | return "Failed connecting test device";
|
---|
| 168 |
|
---|
| 169 | rc = chardev_open(sess, &chardev);
|
---|
| 170 | if (rc != EOK) {
|
---|
| 171 | async_hangup(sess);
|
---|
| 172 | return "Failed opening test device";
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | rc = chardev_write(chardev, small_buffer, SMALL_BUFFER_SIZE, &nbytes);
|
---|
| 176 | if (rc != EIO || nbytes != 1) {
|
---|
| 177 | chardev_close(chardev);
|
---|
| 178 | async_hangup(sess);
|
---|
| 179 | return "Failed sending data";
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[c1694b6b] | 182 | TPRINTF("Sent %zu bytes and got rc = %s (expected)\n", nbytes,
|
---|
| 183 | str_error_name(rc));
|
---|
[57914494] | 184 |
|
---|
[f2d88f3] | 185 | rc = chardev_read(chardev, small_buffer, SMALL_BUFFER_SIZE, &nbytes,
|
---|
| 186 | chardev_f_none);
|
---|
[57914494] | 187 | if (rc != EIO || nbytes != 1) {
|
---|
| 188 | chardev_close(chardev);
|
---|
| 189 | async_hangup(sess);
|
---|
| 190 | return "Failed receiving data";
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[c1694b6b] | 193 | TPRINTF("Received %zu bytes and got rc = %s (expected)\n", nbytes,
|
---|
| 194 | str_error_name(rc));
|
---|
[57914494] | 195 |
|
---|
| 196 | chardev_close(chardev);
|
---|
| 197 | async_hangup(sess);
|
---|
| 198 |
|
---|
| 199 | TPRINTF("Done\n");
|
---|
| 200 | return NULL;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | const char *test_chardev1(void)
|
---|
| 204 | {
|
---|
| 205 | const char *s;
|
---|
| 206 |
|
---|
| 207 | s = test_chardev1_smallx();
|
---|
| 208 | if (s != NULL)
|
---|
| 209 | return s;
|
---|
| 210 |
|
---|
| 211 | s = test_chardev1_largex();
|
---|
| 212 | if (s != NULL)
|
---|
| 213 | return s;
|
---|
| 214 |
|
---|
[84239b1] | 215 | return test_chardev1_partialx();
|
---|
[57914494] | 216 | }
|
---|