Handle Enter/Escape keys in message dialog.
[helenos.git] / uspace / app / tester / chardev / chardev1.c
blob553f3dd9efd25e2525e75e96357368d10769f01f
1 /*
2 * Copyright (c) 2019 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 #include <errno.h>
30 #include <str_error.h>
31 #include <ipc/services.h>
32 #include <io/chardev.h>
33 #include <loc.h>
34 #include "../tester.h"
36 #define SMALL_BUFFER_SIZE 64
37 #define LARGE_BUFFER_SIZE (DATA_XFER_LIMIT * 4)
39 static char small_buffer[SMALL_BUFFER_SIZE];
40 static char large_buffer[LARGE_BUFFER_SIZE];
42 /** Test device that always performs small transfers. */
43 static const char *test_chardev1_smallx(void)
45 chardev_t *chardev;
46 service_id_t sid;
47 async_sess_t *sess;
48 size_t nbytes;
49 errno_t rc;
51 TPRINTF("Test small transfer character device operations\n");
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;
59 sess = loc_service_connect(sid, INTERFACE_DDF, 0);
60 if (sess == NULL)
61 return "Failed connecting test device";
63 rc = chardev_open(sess, &chardev);
64 if (rc != EOK) {
65 async_hangup(sess);
66 return "Failed opening test device";
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";
76 TPRINTF("Sent %zu bytes\n", nbytes);
78 rc = chardev_read(chardev, small_buffer, SMALL_BUFFER_SIZE, &nbytes,
79 chardev_f_none);
80 if (rc != EOK) {
81 chardev_close(chardev);
82 async_hangup(sess);
83 return "Failed receiving data";
86 TPRINTF("Received %zu bytes\n", nbytes);
88 chardev_close(chardev);
89 async_hangup(sess);
91 TPRINTF("Done\n");
92 return NULL;
95 /** Test device that always performs large transfers. */
96 static const char *test_chardev1_largex(void)
98 chardev_t *chardev;
99 service_id_t sid;
100 async_sess_t *sess;
101 size_t nbytes;
102 errno_t rc;
104 TPRINTF("Test large transfer character device operations\n");
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;
112 sess = loc_service_connect(sid, INTERFACE_DDF, 0);
113 if (sess == NULL)
114 return "Failed connecting test device";
116 rc = chardev_open(sess, &chardev);
117 if (rc != EOK) {
118 async_hangup(sess);
119 return "Failed opening test device";
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";
129 TPRINTF("Sent %zu bytes\n", nbytes);
131 rc = chardev_read(chardev, large_buffer, LARGE_BUFFER_SIZE, &nbytes,
132 chardev_f_none);
133 if (rc != EOK) {
134 chardev_close(chardev);
135 async_hangup(sess);
136 return "Failed receiving data";
139 TPRINTF("Received %zu bytes\n", nbytes);
141 chardev_close(chardev);
142 async_hangup(sess);
144 TPRINTF("Done\n");
145 return NULL;
148 /** Test device where all transfers return partial success. */
149 static const char *test_chardev1_partialx(void)
151 chardev_t *chardev;
152 service_id_t sid;
153 async_sess_t *sess;
154 size_t nbytes;
155 errno_t rc;
157 TPRINTF("Test partially-successful character device operations\n");
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;
165 sess = loc_service_connect(sid, INTERFACE_DDF, 0);
166 if (sess == NULL)
167 return "Failed connecting test device";
169 rc = chardev_open(sess, &chardev);
170 if (rc != EOK) {
171 async_hangup(sess);
172 return "Failed opening test device";
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";
182 TPRINTF("Sent %zu bytes and got rc = %s (expected)\n", nbytes,
183 str_error_name(rc));
185 rc = chardev_read(chardev, small_buffer, SMALL_BUFFER_SIZE, &nbytes,
186 chardev_f_none);
187 if (rc != EIO || nbytes != 1) {
188 chardev_close(chardev);
189 async_hangup(sess);
190 return "Failed receiving data";
193 TPRINTF("Received %zu bytes and got rc = %s (expected)\n", nbytes,
194 str_error_name(rc));
196 chardev_close(chardev);
197 async_hangup(sess);
199 TPRINTF("Done\n");
200 return NULL;
203 const char *test_chardev1(void)
205 const char *s;
207 s = test_chardev1_smallx();
208 if (s != NULL)
209 return s;
211 s = test_chardev1_largex();
212 if (s != NULL)
213 return s;
215 return test_chardev1_partialx();