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
|
// test-audit-fgets.c
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <auplugin.h>
static void test_simple_line(void)
{
int fds[2];
char buf[16];
assert(pipe(fds) == 0);
auplugin_fgets_clear();
// nothing in buffer yet
assert(auplugin_fgets_more(sizeof(buf)) == 0);
assert(auplugin_fgets_eof() == 0);
// feed exactly one line and close
const char *input = "hello\n";
assert(write(fds[1], input, strlen(input)));
close(fds[1]);
// now we should see a complete line
assert(auplugin_fgets_more(sizeof(buf)) == 0);
int len = auplugin_fgets(buf, sizeof(buf), fds[0]);
assert(len == 6);
assert(strcmp(buf, "hello\n") == 0);
assert(auplugin_fgets_more(sizeof(buf)) == 0);
assert(auplugin_fgets_eof() == 0);
// next call: see EOF, no data
len = auplugin_fgets(buf, sizeof(buf), fds[0]);
assert(len == 0);
assert(auplugin_fgets_eof() == 1);
close(fds[0]);
}
static void test_multiple_lines(void)
{
int fds[2];
char buf[16];
assert(pipe(fds) == 0);
auplugin_fgets_clear();
const char *input = "one\n two\n";
assert(write(fds[1], input, strlen(input)));
close(fds[1]);
// first line
int len = auplugin_fgets(buf, sizeof(buf), fds[0]);
assert(len == 4);
assert(strcmp(buf, "one\n") == 0);
// leftover " two\n" → there's a newline pending
assert(auplugin_fgets_more(sizeof(buf)) == 1);
// second line
len = auplugin_fgets(buf, sizeof(buf), fds[0]);
assert(len == 5);
assert(strcmp(buf, " two\n") == 0);
// now buffer empties, EOF
len = auplugin_fgets(buf, sizeof(buf), fds[0]);
assert(len == 0);
assert(auplugin_fgets_eof() == 1);
assert(auplugin_fgets_more(sizeof(buf)) == 0);
close(fds[0]);
}
static void test_partial_line(void)
{
int fds[2];
char buf[16];
assert(pipe(fds) == 0);
auplugin_fgets_clear();
const char *input = "partial"; // no '\n'
assert(write(fds[1], input, strlen(input)));
close(fds[1]);
// should hand back "partial" once EOF arrives
int len = auplugin_fgets(buf, sizeof(buf), fds[0]);
/* first call buffers the data but doesn't yet see EOF */
assert(len == 0);
assert(auplugin_fgets_eof() == 0);
/* second call returns the partial line and sets EOF */
len = auplugin_fgets(buf, sizeof(buf), fds[0]);
assert(len == (int)strlen(input));
assert(memcmp(buf, input, len) == 0);
assert(auplugin_fgets_eof() == 1);
/* further calls return 0 once drained */
len = auplugin_fgets(buf, sizeof(buf), fds[0]);
assert(len == 0);
close(fds[0]);
}
static void test_long_line(void)
{
int fds[2];
char buf[10];
assert(pipe(fds) == 0);
auplugin_fgets_clear();
// make a 20-byte 'a' line plus '\n'
char input[22];
memset(input, 'a', 20);
input[20] = '\n';
input[21] = '\0';
assert(write(fds[1], input, 21));
close(fds[1]);
// 1) first chunk (blen=10 → blen-1=9)
int len = auplugin_fgets(buf, sizeof(buf), fds[0]);
assert(len == 9);
for (int i = 0; i < len; i++) assert(buf[i] == 'a');
assert(buf[len] == '\0');
// still have >9 bytes left (and no '\n' within the first 9)
assert(auplugin_fgets_more(sizeof(buf)) == 1);
// 2) second chunk: still sees newline in the remainder,
// but clamps again to 9
len = auplugin_fgets(buf, sizeof(buf), fds[0]);
assert(len == 9);
for (int i = 0; i < len; i++) assert(buf[i] == 'a');
assert(buf[len] == '\0');
assert(auplugin_fgets_more(sizeof(buf)) == 1);
// 3) finally the last "aa\n"
len = auplugin_fgets(buf, sizeof(buf), fds[0]);
assert(len == 3);
assert(buf[0] == 'a' && buf[1] == 'a' && buf[2] == '\n');
assert(buf[3] == '\0');
/* we’ve drained the data but haven’t hit EOF yet */
assert(auplugin_fgets_more(sizeof(buf)) == 0);
assert(auplugin_fgets_eof() == 0);
/* one more call to drive the EOF read() == 0 */
len = auplugin_fgets(buf, sizeof(buf), fds[0]);
assert(len == 0);
assert(auplugin_fgets_eof() == 1);
close(fds[0]);
}
int main(void)
{
test_simple_line();
test_multiple_lines();
test_partial_line();
test_long_line();
printf("audit-fgets tests: all passed\n");
return 0;
}
|