/* SSE framer tests. Every case is also fed one byte at a time: framing must not * depend on how the network splits the stream. */ #include #include #include #include "sse.h" typedef struct { char got[4096]; size_t len; int events; int stop_after; /* callback returns 7 at this event number; 0 never */ } sink; static int record(void *ctx, const char *event, const char *data, size_t data_len) { sink *s = ctx; int n; assert(strlen(data) == data_len); /* NUL-terminated at data_len */ n = snprintf(s->got + s->len, sizeof s->got - s->len, "[%s|%s]", event, data); assert(n > 0 && (size_t)n < sizeof s->got - s->len); s->len += (size_t)n; s->events++; return s->events == s->stop_after ? 7 : 0; } /* Feeds input whole and byte by byte; both must give want and pending. */ static void check(const char *input, size_t input_len, const char *want, int want_pending) { for (int bytewise = 0; bytewise < 2; bytewise++) { sink s = {{0}, 0, 0, 0}; pic_sse p; assert(pic_sse_init(&p, 256, 256, record, &s) == 0); if (bytewise) { for (size_t i = 0; i < input_len; i++) { assert(pic_sse_feed(&p, input + i, 1) == 0); } } else { assert(pic_sse_feed(&p, input, input_len) == 0); } if (strcmp(s.got, want) != 0 || pic_sse_pending(&p) != want_pending) { fprintf(stderr, "input %.60s\nwant %s pending=%d\ngot %s pending=%d (bytewise=%d)\n", input, want, want_pending, s.got, pic_sse_pending(&p), bytewise); assert(0); } pic_sse_free(&p); } } #define CHECK(input, want, pending) check(input, sizeof(input) - 1, want, pending) int main(void) { /* Basic events, default type, typed events. */ CHECK("data: hi\n\n", "[message|hi]", 0); CHECK("event: ping\ndata: {}\n\n", "[ping|{}]", 0); /* All three line endings, mixed. */ CHECK("data: a\r\n\r\ndata: b\r\rdata: c\n\n", "[message|a][message|b][message|c]", 0); /* Multi-line data joins with LF; only one leading space is dropped. */ CHECK("data: one\ndata: two\ndata:three\n\n", "[message|one\n two\nthree]", 0); /* Comments and unknown fields are ignored; a field with no colon has an empty value. */ CHECK(": keep-alive\nid: 9\nretry: 10\nfoo: bar\ndata\n\n", "[message|]", 0); /* A blank line with no data dispatches nothing and resets the event type. */ CHECK("event: x\n\ndata: y\n\n", "[message|y]", 0); /* A leading byte-order mark is skipped. */ CHECK("\xEF\xBB\xBF" "data: bom\n\n", "[message|bom]", 0); /* Embedded colons stay in the value. */ CHECK("data: {\"a\":\"b:c\"}\n\n", "[message|{\"a\":\"b:c\"}]", 0); /* A truncated stream leaves its unfinished event pending, undispatched. */ CHECK("data: whole\n\ndata: cut", "[message|whole]", 1); CHECK("event: e\ndata: x\n", "", 1); /* Limits fail the stream, and it stays failed. */ { sink s = {{0}, 0, 0, 0}; pic_sse p; char big[300]; memset(big, 'x', sizeof big); assert(pic_sse_init(&p, 256, 256, record, &s) == 0); assert(pic_sse_feed(&p, big, sizeof big) == -1); assert(pic_sse_feed(&p, "\n\n", 2) == -1); pic_sse_free(&p); assert(pic_sse_init(&p, 256, 8, record, &s) == 0); assert(pic_sse_feed(&p, "data: 123456789\n", 16) == -1); pic_sse_free(&p); assert(pic_sse_init(&p, 256, 256, record, &s) == 0); memset(big, 'e', 100); assert(pic_sse_feed(&p, "event: ", 7) == 0); assert(pic_sse_feed(&p, big, 100) == 0); assert(pic_sse_feed(&p, "\n", 1) == -1); /* event name over its cap */ pic_sse_free(&p); } /* A callback can stop the stream; its value is returned. */ { sink s = {{0}, 0, 0, 1}; pic_sse p; assert(pic_sse_init(&p, 256, 256, record, &s) == 0); assert(pic_sse_feed(&p, "data: a\n\ndata: b\n\n", 18) == 7); assert(s.events == 1); pic_sse_free(&p); } puts("sse: all cases pass, whole and byte by byte"); return 0; }