Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | /*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ztest.h>
#include <ztest_error_hook.h>
#define TIMEOUT K_MSEC(100)
#define PIPE_LEN 8
static ZTEST_DMEM unsigned char __aligned(4) data[] = "abcd1234";
struct k_pipe put_get_pipe;
static void put_fail(struct k_pipe *p)
{
size_t wt_byte = 0;
zassert_false(k_pipe_put(p, data, PIPE_LEN, &wt_byte,
1, K_FOREVER), NULL);
/**TESTPOINT: pipe put returns -EIO*/
zassert_equal(k_pipe_put(p, data, PIPE_LEN, &wt_byte,
1, K_NO_WAIT), -EIO, NULL);
zassert_false(wt_byte, NULL);
/**TESTPOINT: pipe put returns -EAGAIN*/
zassert_equal(k_pipe_put(p, data, PIPE_LEN, &wt_byte,
1, TIMEOUT), -EAGAIN, NULL);
zassert_true(wt_byte < 1, NULL);
zassert_equal(k_pipe_put(p, data, PIPE_LEN, &wt_byte,
PIPE_LEN + 1, TIMEOUT), -EINVAL, NULL);
}
/**
* @brief Test pipe put failure scenario
* @ingroup kernel_pipe_tests
* @see k_pipe_init(), k_pipe_put()
*/
void test_pipe_put_fail(void)
{
k_pipe_init(&put_get_pipe, data, PIPE_LEN);
put_fail(&put_get_pipe);
}
/**
* @brief Test pipe put by a user thread
* @ingroup kernel_pipe_tests
* @see k_pipe_put()
*/
#ifdef CONFIG_USERSPACE
void test_pipe_user_put_fail(void)
{
struct k_pipe *p = k_object_alloc(K_OBJ_PIPE);
zassert_true(p != NULL, NULL);
zassert_false(k_pipe_alloc_init(p, PIPE_LEN), NULL);
/* check the number of bytes that may be read from pipe. */
zassert_equal(k_pipe_read_avail(p), 0, NULL);
/* check the number of bytes that may be written to pipe.*/
zassert_equal(k_pipe_write_avail(p), PIPE_LEN, NULL);
put_fail(p);
}
#endif
static void get_fail(struct k_pipe *p)
{
unsigned char rx_data[PIPE_LEN];
size_t rd_byte = 0;
/**TESTPOINT: pipe put returns -EIO*/
zassert_equal(k_pipe_get(p, rx_data, PIPE_LEN, &rd_byte, 1,
K_NO_WAIT), -EIO, NULL);
zassert_false(rd_byte, NULL);
/**TESTPOINT: pipe put returns -EAGAIN*/
zassert_equal(k_pipe_get(p, rx_data, PIPE_LEN, &rd_byte, 1,
TIMEOUT), -EAGAIN, NULL);
zassert_true(rd_byte < 1, NULL);
zassert_equal(k_pipe_get(p, rx_data, PIPE_LEN, &rd_byte, 1,
TIMEOUT), -EAGAIN, NULL);
}
/**
* @brief Test pipe get failure scenario
* @ingroup kernel_pipe_tests
* @see k_pipe_init(), k_pipe_get()
*/
void test_pipe_get_fail(void)
{
k_pipe_init(&put_get_pipe, data, PIPE_LEN);
get_fail(&put_get_pipe);
}
#ifdef CONFIG_USERSPACE
static unsigned char user_unreach[PIPE_LEN];
static size_t unreach_byte;
/**
* @brief Test pipe get by a user thread
* @ingroup kernel_pipe_tests
* @see k_pipe_alloc_init()
*/
void test_pipe_user_get_fail(void)
{
struct k_pipe *p = k_object_alloc(K_OBJ_PIPE);
zassert_true(p != NULL, NULL);
zassert_false(k_pipe_alloc_init(p, PIPE_LEN), NULL);
get_fail(p);
}
/**
* @brief Test k_pipe_alloc_init() failure scenario
*
* @details See what will happen if an uninitialized
* k_pipe is passed to k_pipe_alloc_init().
*
* @ingroup kernel_pipe_tests
*
* @see k_pipe_alloc_init()
*/
void test_pipe_alloc_not_init(void)
{
struct k_pipe pipe;
ztest_set_fault_valid(true);
k_pipe_alloc_init(&pipe, PIPE_LEN);
}
/**
* @brief Test k_pipe_get() failure scenario
*
* @details See what will happen if an uninitialized
* k_pipe is passed to k_pipe_get().
*
* @ingroup kernel_pipe_tests
*
* @see k_pipe_get()
*/
void test_pipe_get_null(void)
{
unsigned char rx_data[PIPE_LEN];
size_t rd_byte = 0;
ztest_set_fault_valid(true);
k_pipe_get(NULL, rx_data, PIPE_LEN,
&rd_byte, 1, TIMEOUT);
}
/**
* @brief Test k_pipe_get() failure scenario
*
* @details See what will happen if the parameter
* address is accessed deny to test k_pipe_get
*
* @ingroup kernel_pipe_tests
*
* @see k_pipe_get()
*/
void test_pipe_get_unreach_data(void)
{
struct k_pipe *p = k_object_alloc(K_OBJ_PIPE);
size_t rd_byte = 0;
zassert_true(p != NULL, NULL);
zassert_false(k_pipe_alloc_init(p, PIPE_LEN), NULL);
ztest_set_fault_valid(true);
k_pipe_get(p, user_unreach, PIPE_LEN,
&rd_byte, 1, TIMEOUT);
}
/**
* @brief Test k_pipe_get() failure scenario
*
* @details See what will happen if the parameter
* address is accessed deny to test k_pipe_get
*
* @ingroup kernel_pipe_tests
*
* @see k_pipe_get()
*/
void test_pipe_get_unreach_size(void)
{
struct k_pipe *p = k_object_alloc(K_OBJ_PIPE);
unsigned char rx_data[PIPE_LEN];
zassert_true(p != NULL, NULL);
zassert_false(k_pipe_alloc_init(p, PIPE_LEN), NULL);
ztest_set_fault_valid(true);
k_pipe_get(p, rx_data, PIPE_LEN,
&unreach_byte, 1, TIMEOUT);
}
/**
* @brief Test k_pipe_put() failure scenario
*
* @details See what will happen if a null pointer
* is passed into the k_pipe_put as a parameter
*
* @ingroup kernel_pipe_tests
*
* @see k_pipe_put()
*/
void test_pipe_put_null(void)
{
unsigned char tx_data = 0xa;
size_t to_wt = 0, wt_byte = 0;
ztest_set_fault_valid(true);
k_pipe_put(NULL, &tx_data, to_wt,
&wt_byte, 1, TIMEOUT);
}
/**
* @brief Test k_pipe_put() failure scenario
*
* @details See what will happen if the parameter
* address is accessed deny to test k_pipe_put
*
* @ingroup kernel_pipe_tests
*
* @see k_pipe_put()
*/
void test_pipe_put_unreach_data(void)
{
struct k_pipe *p = k_object_alloc(K_OBJ_PIPE);
size_t to_wt = 0, wt_byte = 0;
zassert_true(p != NULL, NULL);
zassert_false(k_pipe_alloc_init(p, PIPE_LEN), NULL);
ztest_set_fault_valid(true);
k_pipe_put(p, &user_unreach[0], to_wt,
&wt_byte, 1, TIMEOUT);
}
/**
* @brief Test k_pipe_put() failure scenario
*
* @details See what will happen if the parameter
* address is accessed deny to test k_pipe_put
*
* @ingroup kernel_pipe_tests
*
* @see k_pipe_put()
*/
void test_pipe_put_unreach_size(void)
{
struct k_pipe *p = k_object_alloc(K_OBJ_PIPE);
unsigned char tx_data = 0xa;
size_t to_wt = 0;
zassert_true(p != NULL, NULL);
zassert_false(k_pipe_alloc_init(p, PIPE_LEN), NULL);
ztest_set_fault_valid(true);
k_pipe_put(p, &tx_data, to_wt,
&unreach_byte, 1, TIMEOUT);
}
/**
* @brief Test k_pipe_read_avail() failure scenario
*
* @details See what will happen if a null pointer
* is passed into the k_pipe_read_avail as a parameter
*
* @ingroup kernel_pipe_tests
*
* @see k_pipe_read_avail()
*/
void test_pipe_read_avail_null(void)
{
ztest_set_fault_valid(true);
k_pipe_read_avail(NULL);
}
/**
* @brief Test k_pipe_write_avail() failure scenario
*
* @details See what will happen if a null pointer
* is passed into the k_pipe_write_avail as a parameter
*
* @ingroup kernel_pipe_tests
*
* @see k_pipe_write_avail()
*/
void test_pipe_write_avail_null(void)
{
ztest_set_fault_valid(true);
k_pipe_write_avail(NULL);
}
#endif
|