| 1 |
/* |
| 2 |
* Copyright (C) 2020 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "common/cockpitframe.h" |
| 9 |
#include "testlib/cockpittest.h" |
| 10 |
|
| 11 |
#include <glib.h> |
| 12 |
#include <errno.h> |
| 13 |
#include <fcntl.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <unistd.h> |
| 16 |
|
| 17 |
typedef struct |
| 18 |
{ |
| 19 |
FILE *write_fp; |
| 20 |
int read_fd; |
| 21 |
} Fixture; |
| 22 |
|
| 23 |
typedef struct |
| 24 |
{ |
| 25 |
const char *input; |
| 26 |
int expect_errno; |
| 27 |
} TestCase; |
| 28 |
|
| 29 |
static void |
| 30 |
fixture_setup (Fixture *self, |
| 31 |
const TestCase *tc) |
| 32 |
{ |
| 33 |
int pipefd[2]; |
| 34 |
|
| 35 |
int r = pipe (pipefd); |
| 36 |
g_assert_cmpint (r, ==, 0); |
| 37 |
|
| 38 |
self->write_fp = fdopen (pipefd[1], "w"); |
| 39 |
g_assert (self->write_fp != NULL); |
| 40 |
self->read_fd = pipefd[0]; |
| 41 |
|
| 42 |
if (tc->input) |
| 43 |
{ |
| 44 |
fprintf (self->write_fp, "%s", tc->input); |
| 45 |
fflush (self->write_fp); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
static void |
| 50 |
fixture_close_write (Fixture *self) |
| 51 |
{ |
| 52 |
if (self->write_fp) |
| 53 |
{ |
| 54 |
fclose (self->write_fp); |
| 55 |
self->write_fp = NULL; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
static void |
| 60 |
fixture_close_read (Fixture *self) |
| 61 |
{ |
| 62 |
if (self->read_fd != -1) |
| 63 |
{ |
| 64 |
close (self->read_fd); |
| 65 |
self->read_fd = -1; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
static void |
| 70 |
fixture_teardown (Fixture *self, |
| 71 |
const TestCase *tc) |
| 72 |
{ |
| 73 |
if (tc->expect_errno) |
| 74 |
{ |
| 75 |
alarm (10); |
| 76 |
|
| 77 |
g_autofree unsigned char *output = NULL; |
| 78 |
ssize_t size = cockpit_frame_read (self->read_fd, &output); |
| 79 |
|
| 80 |
g_assert_cmpint (size, ==, -1); |
| 81 |
g_assert_cmpint (errno, ==, tc->expect_errno); |
| 82 |
g_assert (output == NULL); |
| 83 |
|
| 84 |
alarm (0); |
| 85 |
} |
| 86 |
|
| 87 |
fixture_close_write (self); |
| 88 |
fixture_close_read (self); |
| 89 |
} |
| 90 |
|
| 91 |
static void |
| 92 |
test_valid (Fixture *pipe, const TestCase *tc) |
| 93 |
{ |
| 94 |
/* Try sending valid frames of various sizes */ |
| 95 |
for (gint i = 1; i < 1000; i++) |
| 96 |
{ |
| 97 |
/* Write a frame consisting of `i` spaces. After the frame, write |
| 98 |
* a pattern that we can use to detect that only the correct |
| 99 |
* amount of bytes were read. |
| 100 |
*/ |
| 101 |
fprintf (pipe->write_fp, "%u\n%*sTHEEND", i, i, ""); |
| 102 |
fflush (pipe->write_fp); |
| 103 |
|
| 104 |
/* Read it back and see what happens */ |
| 105 |
g_autofree unsigned char *output = NULL; |
| 106 |
ssize_t size = cockpit_frame_read (pipe->read_fd, &output); |
| 107 |
|
| 108 |
g_assert_cmpint (size, ==, i); |
| 109 |
for (gint j = 0; j < size; j++) |
| 110 |
g_assert (output[j] == ' '); |
| 111 |
/* ensure it's NUL terminated */ |
| 112 |
g_assert (output[size] == '\0'); |
| 113 |
|
| 114 |
/* Make sure our pattern is there */ |
| 115 |
char buffer[7]; |
| 116 |
size = read (pipe->read_fd, buffer, sizeof buffer); |
| 117 |
g_assert_cmpint (size, ==, 6); |
| 118 |
g_assert (memcmp (buffer, "THEEND", 6) == 0); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
static void |
| 123 |
test_fail_badfd (Fixture *fixture, const TestCase *tc) |
| 124 |
{ |
| 125 |
/* cause cockpit_frame_read() to read from -1 */ |
| 126 |
fixture_close_read (fixture); |
| 127 |
} |
| 128 |
|
| 129 |
static void |
| 130 |
test_fail_short (Fixture *fixture, const TestCase *tc) |
| 131 |
{ |
| 132 |
/* cause cockpit_frame_read() to read the message, then EOF */ |
| 133 |
fixture_close_write (fixture); |
| 134 |
} |
| 135 |
|
| 136 |
static void |
| 137 |
test_fail_nonblocking (Fixture *pipe, const TestCase *tc) |
| 138 |
{ |
| 139 |
/* cause cockpit_frame_read() to read the message, then EAGAIN */ |
| 140 |
(void) fcntl (pipe->read_fd, F_SETFL, O_NONBLOCK); |
| 141 |
} |
| 142 |
|
| 143 |
/* many of the testcases are driven entirely by the fixture setup/teardown */ |
| 144 |
static void nil (void) { } |
| 145 |
|
| 146 |
int |
| 147 |
main (int argc, |
| 148 |
char *argv[]) |
| 149 |
{ |
| 150 |
cockpit_test_init (&argc, &argv); |
| 151 |
|
| 152 |
/* kwargs hack, plus avoid casting gconstpointer */ |
| 153 |
#define PIPE_TEST(name,func,...) g_test_add_vtable(name, sizeof(Fixture), \ |
| 154 |
&(TestCase) { __VA_ARGS__ }, \ |
| 155 |
(GTestFixtureFunc) fixture_setup, \ |
| 156 |
(GTestFixtureFunc) func, \ |
| 157 |
(GTestFixtureFunc) fixture_teardown) |
| 158 |
|
| 159 |
PIPE_TEST("/frame/read-frame/valid", test_valid); |
| 160 |
|
| 161 |
PIPE_TEST("/frame/read-frame/fail/badfd", test_fail_badfd, |
| 162 |
.expect_errno=EBADF); |
| 163 |
|
| 164 |
PIPE_TEST("/frame/read-frame/fail/short", test_fail_short, |
| 165 |
.input="10\nabc", .expect_errno=EBADMSG); |
| 166 |
|
| 167 |
PIPE_TEST("/frame/read-frame/fail/nonblocking", test_fail_nonblocking, |
| 168 |
.input="10\nabc", .expect_errno=EAGAIN); |
| 169 |
|
| 170 |
/* This valid message should fail because we get EAGAIN while trying to read it... */ |
| 171 |
PIPE_TEST("/frame/read-frame/fail/nonblocking-big", test_fail_nonblocking, |
| 172 |
.input="99999999\nabc", .expect_errno=EAGAIN); |
| 173 |
/* ...but add one byte more, and it's now an invalid message. */ |
| 174 |
PIPE_TEST("/frame/read-frame/fail/nonblocking-toobig", test_fail_nonblocking, |
| 175 |
.input="100000000\nabc", .expect_errno=EBADMSG); |
| 176 |
|
| 177 |
/* Some generic failures due to broken messages */ |
| 178 |
PIPE_TEST("/frame/read-frame/fail/non-numeric", nil, |
| 179 |
.input="abc\nabc", .expect_errno=EBADMSG); |
| 180 |
PIPE_TEST("/frame/read-frame/fail/semi-numeric", nil, |
| 181 |
.input="1000abc\nabc", .expect_errno=EBADMSG); |
| 182 |
PIPE_TEST("/frame/read-frame/fail/toobig", nil, |
| 183 |
.input="100000000\nabc", .expect_errno=EBADMSG); |
| 184 |
PIPE_TEST("/frame/read-frame/fail/toobig-nonnumeric", nil, |
| 185 |
.input="10000000a\nabc", .expect_errno=EBADMSG); |
| 186 |
PIPE_TEST("/frame/read-frame/fail/leading-zero", nil, |
| 187 |
.input="03\nabc", .expect_errno=EBADMSG); |
| 188 |
PIPE_TEST("/frame/read-frame/fail/empty-header", nil, |
| 189 |
.input="\nabc", .expect_errno=EBADMSG); |
| 190 |
|
| 191 |
return g_test_run (); |
| 192 |
} |