dtchat: add the core API, JSON reader and line framing (T1.1)
include/DtChat/DtChat.h and Ipc.h define the UI-facing core API and the adapter contract. lib/libDtChat builds libDtChatIpc (strict JSON parser/writer, 1 MiB line framing) and the libDtChat core stub; test_json and test_frame cover escapes, UTF-8, depth 32, duplicate keys, overlong lines, CRLF, NUL and the write path. Wired into configure.ac, lib/Makefile.am and the installed headers. Standalone tests pass, ASan/UBSan clean.
This commit is contained in:
1 parent
13deb228d1
commit
e645e8160d
11 files changed
+2109
-1
No files matched your search
@@ -802,6 +802,7 @@ util/tradcpp/Makefile
|
||||
lib/Makefile
|
||||
lib/McdeSys/Makefile
|
||||
lib/McdeSound/Makefile
|
||||
lib/libDtChat/Makefile
|
||||
|
||||
lib/DtXinerama/Makefile
|
||||
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* DtChat.h -- dtchat's public core API
|
||||
*
|
||||
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
||||
*
|
||||
* The UI and the headless tests use only this header. No Xt, no protocol
|
||||
* library, no audio/video type appears here. The core owns the event queue;
|
||||
* the caller drains it with dtchat_event_next() and frees each event.
|
||||
*/
|
||||
#ifndef _DtChat_DtChat_h
|
||||
#define _DtChat_DtChat_h
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct DtChat DtChat;
|
||||
|
||||
typedef enum {
|
||||
DTCHAT_EV_STATE, /* an account or a local peer changed state */
|
||||
DTCHAT_EV_CONV, /* a conversation was added, changed or removed */
|
||||
DTCHAT_EV_MESSAGE, /* a message, incoming or echoed */
|
||||
DTCHAT_EV_PRESENCE, /* a buddy or a local peer changed presence */
|
||||
DTCHAT_EV_TYPING, /* someone started or stopped typing */
|
||||
DTCHAT_EV_REQUEST, /* the adapter needs an answer: code, password, QR */
|
||||
DTCHAT_EV_CALL, /* call state changed */
|
||||
DTCHAT_EV_ERROR /* an account or the local peer failed */
|
||||
} DtChatEventType;
|
||||
|
||||
/* DtChatEvent.flags for DTCHAT_EV_MESSAGE */
|
||||
#define DTCHAT_MSG_SELF 0x01 /* our own message, echoed */
|
||||
#define DTCHAT_MSG_ACTION 0x02 /* /me */
|
||||
#define DTCHAT_MSG_SYSTEM 0x04 /* join, part, topic, ... */
|
||||
#define DTCHAT_MSG_NOTICE 0x08
|
||||
|
||||
/* dtchat_call() media bits */
|
||||
#define DTCHAT_MEDIA_AUDIO 0x01
|
||||
#define DTCHAT_MEDIA_VIDEO 0x02
|
||||
|
||||
/*
|
||||
* One event. The strings are owned by the event; dtchat_event_free()
|
||||
* releases them. "account" is NULL for a local peer, "conv" is the
|
||||
* conversation id inside that account, "from" is the sender's display
|
||||
* name, "text" is UTF-8. request_id is only set for DTCHAT_EV_REQUEST;
|
||||
* data is reserved for the event-specific payload (a call object, later).
|
||||
*/
|
||||
typedef struct {
|
||||
DtChatEventType type;
|
||||
char *account;
|
||||
char *conv;
|
||||
char *from;
|
||||
char *text;
|
||||
long long ts; /* seconds since the epoch, 0 if unknown */
|
||||
int flags;
|
||||
int request_id;
|
||||
void *data;
|
||||
} DtChatEvent;
|
||||
|
||||
/*
|
||||
* Open the core with its state directory. NULL means ~/.dt/dtchat.
|
||||
* The call does not create the directory; the store does that when the
|
||||
* first account is saved (T1.3). Returns NULL on out of memory.
|
||||
*/
|
||||
DtChat *dtchat_open(const char *config_dir);
|
||||
|
||||
/* Stop adapters, close the local listener and free the core. */
|
||||
void dtchat_close(DtChat *chat);
|
||||
|
||||
/*
|
||||
* A file descriptor that becomes readable when events are queued, for
|
||||
* XtAppAddInput() or poll(). Returns -1 when the core has no wakeup pipe
|
||||
* yet (the T1.1 stub).
|
||||
*/
|
||||
int dtchat_fd(const DtChat *chat);
|
||||
|
||||
/*
|
||||
* Run the core's event loop once. timeout_ms < 0 waits without a timeout,
|
||||
* 0 returns immediately. Returns the number of ready descriptors, 0 on a
|
||||
* timeout, -1 on error.
|
||||
*/
|
||||
int dtchat_poll(DtChat *chat, int timeout_ms);
|
||||
|
||||
/* Return the next queued event, or NULL when the queue is empty. */
|
||||
DtChatEvent *dtchat_event_next(DtChat *chat);
|
||||
|
||||
/* Free one event returned by dtchat_event_next(). NULL is allowed. */
|
||||
void dtchat_event_free(DtChatEvent *event);
|
||||
|
||||
/* Send UTF-8 text to CONV. Returns 0 when it was queued, -1 on error. */
|
||||
int dtchat_send(DtChat *chat, const char *conv, const char *text);
|
||||
|
||||
/* Join a channel/room; the adapter decides what that means. */
|
||||
int dtchat_join(DtChat *chat, const char *account, const char *conv);
|
||||
|
||||
/* Answer a DTCHAT_EV_REQUEST; VALUE may be NULL for "cancel". */
|
||||
int dtchat_answer(DtChat *chat, const char *account, int request_id,
|
||||
const char *value);
|
||||
|
||||
/* Start a call to a local peer; MEDIA is DTCHAT_MEDIA_AUDIO/VIDEO. */
|
||||
int dtchat_call(DtChat *chat, const char *peer, int media);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _DtChat_DtChat_h */
|
||||
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Ipc.h -- dtchat's adapter contract: JSON lines, at most 1 MiB each
|
||||
*
|
||||
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
||||
*
|
||||
* This header is also the contract for adapters written outside this tree.
|
||||
* The rules are in docs/reference/dtchat-adapter-protocol.txt:
|
||||
* - one JSON object per line, UTF-8, '\n' terminated
|
||||
* - at most DC_IPC_MAX_LINE bytes per line (without the '\n')
|
||||
* - no NUL byte, no pretty-printing, no multiple objects per line
|
||||
* - unknown fields are ignored; a breaking change raises DC_IPC_VERSION
|
||||
*/
|
||||
#ifndef _DtChat_Ipc_h
|
||||
#define _DtChat_Ipc_h
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define DC_IPC_VERSION 1
|
||||
#define DC_IPC_MAX_LINE (1024 * 1024)
|
||||
#define DC_JSON_MAX_DEPTH 32
|
||||
|
||||
/*
|
||||
* UTF-8 validation. Returns 1 for a valid sequence, 0 otherwise (overlong
|
||||
* forms, surrogates, values above U+10FFFF and truncated sequences are
|
||||
* invalid). N may be 0; S may be NULL only when N is 0.
|
||||
*/
|
||||
int dc_utf8_valid(const char *s, size_t n);
|
||||
|
||||
/*
|
||||
* A parsed or built JSON value. Opaque; the accessors below are the API.
|
||||
*/
|
||||
typedef struct DcJson DcJson;
|
||||
|
||||
typedef enum {
|
||||
DC_JSON_NULL,
|
||||
DC_JSON_BOOL,
|
||||
DC_JSON_INT,
|
||||
DC_JSON_DOUBLE,
|
||||
DC_JSON_STRING,
|
||||
DC_JSON_ARRAY,
|
||||
DC_JSON_OBJECT
|
||||
} DcJsonType;
|
||||
|
||||
/*
|
||||
* Parse one JSON value from LEN bytes. On error returns NULL and writes a
|
||||
* short message (including the byte offset) into ERR when ERR is not NULL.
|
||||
* The parser is strict: it rejects trailing data, duplicate object keys,
|
||||
* unescaped control characters, invalid UTF-8, invalid escapes, a depth
|
||||
* above DC_JSON_MAX_DEPTH, and input larger than DC_IPC_MAX_LINE.
|
||||
*/
|
||||
DcJson *dc_json_parse(const char *text, size_t len, char *err, size_t errlen);
|
||||
|
||||
void dc_json_free(DcJson *json);
|
||||
|
||||
DcJsonType dc_json_type(const DcJson *json);
|
||||
const char *dc_json_type_name(DcJsonType type);
|
||||
|
||||
/* Constructors; NULL on out of memory. */
|
||||
DcJson *dc_json_new_null(void);
|
||||
DcJson *dc_json_new_bool(int value);
|
||||
DcJson *dc_json_new_int(long long value);
|
||||
DcJson *dc_json_new_double(double value);
|
||||
DcJson *dc_json_new_string(const char *value); /* value is UTF-8 */
|
||||
DcJson *dc_json_new_array(void);
|
||||
DcJson *dc_json_new_object(void);
|
||||
|
||||
/*
|
||||
* Object/array builders. Both take ownership of VALUE: on success it is
|
||||
* stored, on failure it is freed and -1 is returned. Setting an existing
|
||||
* object key replaces the old value.
|
||||
*/
|
||||
int dc_json_object_set(DcJson *object, const char *key, DcJson *value);
|
||||
int dc_json_array_append(DcJson *array, DcJson *value);
|
||||
|
||||
/*
|
||||
* Accessors. They return NULL/0/-1 when JSON is NULL or the wrong type.
|
||||
* dc_json_string() returns a pointer into JSON, valid until it is freed.
|
||||
*/
|
||||
DcJson *dc_json_get(const DcJson *object, const char *key);
|
||||
const char *dc_json_string(const DcJson *json);
|
||||
long long dc_json_int(const DcJson *json);
|
||||
double dc_json_double(const DcJson *json);
|
||||
int dc_json_bool(const DcJson *json);
|
||||
|
||||
size_t dc_json_array_size(const DcJson *array);
|
||||
DcJson *dc_json_array_get(const DcJson *array, size_t index);
|
||||
|
||||
/*
|
||||
* Serialize to a newly allocated, NUL-terminated UTF-8 string without a
|
||||
* trailing newline. LEN, when not NULL, receives the string length. The
|
||||
* caller frees the result with free(3). NULL on out of memory.
|
||||
*/
|
||||
char *dc_json_write(const DcJson *json, size_t *len);
|
||||
|
||||
/*
|
||||
* Line framing for a non-blocking adapter pipe. The buffer owns its
|
||||
* memory; dc_frame_next() returns a newly allocated line (without '\n'
|
||||
* and without a trailing '\r') that the caller frees with free(3), or
|
||||
* NULL when no complete line is buffered.
|
||||
*/
|
||||
typedef struct {
|
||||
char *buf;
|
||||
size_t len;
|
||||
size_t cap;
|
||||
} DcFrameBuf;
|
||||
|
||||
void dc_frame_init(DcFrameBuf *frame);
|
||||
void dc_frame_reset(DcFrameBuf *frame);
|
||||
void dc_frame_free(DcFrameBuf *frame);
|
||||
|
||||
/*
|
||||
* Append N bytes. Returns 0 on success, -1 when the data contains a NUL
|
||||
* byte or the buffered line would exceed DC_IPC_MAX_LINE. On -1 the
|
||||
* buffer content is undefined; the caller closes the connection.
|
||||
*/
|
||||
int dc_frame_feed(DcFrameBuf *frame, const void *data, size_t n);
|
||||
|
||||
/*
|
||||
* Return the next complete line (without '\n' and without a trailing
|
||||
* '\r') or NULL when no line is buffered. The result is newly allocated;
|
||||
* the caller frees it with free(3). LEN, when not NULL, receives its
|
||||
* length.
|
||||
*/
|
||||
char *dc_frame_next(DcFrameBuf *frame, size_t *len);
|
||||
|
||||
/* Write LINE plus '\n' to FD, handling short writes and EINTR. 0/-1. */
|
||||
int dc_frame_send(int fd, const char *line, size_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _DtChat_Ipc_h */
|
||||
@@ -8,6 +8,8 @@ nobase_include_HEADERS = Mcde/GlobalMenu.h \
|
||||
Mcde/Thumb.h \
|
||||
Mcde/McdeSys.h \
|
||||
Mcde/Sound.h \
|
||||
DtChat/DtChat.h \
|
||||
DtChat/Ipc.h \
|
||||
Dt/Editor.h \
|
||||
Dt/TermPrim.h \
|
||||
Dt/Term.h \
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
MAINTAINERCLEANFILES = Makefile.in
|
||||
|
||||
SUBDIRS = McdeSys McdeSound DtXinerama tt DtSvc DtSearch DtWidget DtHelp DtPrint DtTerm \
|
||||
SUBDIRS = McdeSys McdeSound libDtChat DtXinerama tt DtSvc DtSearch DtWidget DtHelp DtPrint DtTerm \
|
||||
DtMrm csa DtMmdb
|
||||
@@ -0,0 +1,25 @@
|
||||
MAINTAINERCLEANFILES = Makefile.in
|
||||
|
||||
# libDtChatIpc: JSON and line framing for adapters (no X, no TLS, no codec).
|
||||
# libDtChat: the core, which links the IPC part and adds the event loop,
|
||||
# storage, adapters and the local peer in the later tasks.
|
||||
lib_LTLIBRARIES = libDtChatIpc.la libDtChat.la
|
||||
|
||||
libDtChatIpc_la_CPPFLAGS = -I$(top_srcdir)/include
|
||||
libDtChatIpc_la_SOURCES = json.c
|
||||
libDtChatIpc_la_LDFLAGS = -version-info 1:0:0
|
||||
|
||||
libDtChat_la_CPPFLAGS = -I$(top_srcdir)/include
|
||||
libDtChat_la_SOURCES = core.c dc_internal.h
|
||||
libDtChat_la_LIBADD = libDtChatIpc.la
|
||||
libDtChat_la_LDFLAGS = -version-info 1:0:0
|
||||
|
||||
# make check, and tools/mcde-tests.sh from a native build
|
||||
check_PROGRAMS = test_json test_frame
|
||||
test_json_SOURCES = test_json.c
|
||||
test_json_CPPFLAGS = -I$(top_srcdir)/include
|
||||
test_json_LDADD = libDtChatIpc.la
|
||||
test_frame_SOURCES = test_frame.c
|
||||
test_frame_CPPFLAGS = -I$(top_srcdir)/include
|
||||
test_frame_LDADD = libDtChatIpc.la
|
||||
TESTS = test_json test_frame
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* core.c -- dtchat's core lifecycle (T1.1 stub)
|
||||
*
|
||||
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
||||
*
|
||||
* T1.1 only needs the public API to exist and link. The event loop (T1.2),
|
||||
* storage (T1.3), conversations (T1.4) and adapters (T1.5) replace the
|
||||
* stubs below; the signatures are already the ones the UI compiles against.
|
||||
*/
|
||||
#include "DtChat/DtChat.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct DtChat {
|
||||
char *dir;
|
||||
};
|
||||
|
||||
DtChat *
|
||||
dtchat_open(const char *config_dir)
|
||||
{
|
||||
DtChat *chat = calloc(1, sizeof(*chat));
|
||||
|
||||
if (chat == NULL)
|
||||
return NULL;
|
||||
chat->dir = strdup(config_dir ? config_dir : "");
|
||||
if (chat->dir == NULL) {
|
||||
free(chat);
|
||||
return NULL;
|
||||
}
|
||||
return chat;
|
||||
}
|
||||
|
||||
void
|
||||
dtchat_close(DtChat *chat)
|
||||
{
|
||||
if (chat == NULL)
|
||||
return;
|
||||
free(chat->dir);
|
||||
free(chat);
|
||||
}
|
||||
|
||||
int
|
||||
dtchat_fd(const DtChat *chat)
|
||||
{
|
||||
(void)chat;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
dtchat_poll(DtChat *chat, int timeout_ms)
|
||||
{
|
||||
(void)chat;
|
||||
(void)timeout_ms;
|
||||
return 0;
|
||||
}
|
||||
|
||||
DtChatEvent *
|
||||
dtchat_event_next(DtChat *chat)
|
||||
{
|
||||
(void)chat;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
dtchat_event_free(DtChatEvent *event)
|
||||
{
|
||||
if (event == NULL)
|
||||
return;
|
||||
free(event->account);
|
||||
free(event->conv);
|
||||
free(event->from);
|
||||
free(event->text);
|
||||
free(event);
|
||||
}
|
||||
|
||||
int
|
||||
dtchat_send(DtChat *chat, const char *conv, const char *text)
|
||||
{
|
||||
(void)chat;
|
||||
(void)conv;
|
||||
(void)text;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
dtchat_join(DtChat *chat, const char *account, const char *conv)
|
||||
{
|
||||
(void)chat;
|
||||
(void)account;
|
||||
(void)conv;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
dtchat_answer(DtChat *chat, const char *account, int request_id,
|
||||
const char *value)
|
||||
{
|
||||
(void)chat;
|
||||
(void)account;
|
||||
(void)request_id;
|
||||
(void)value;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
dtchat_call(DtChat *chat, const char *peer, int media)
|
||||
{
|
||||
(void)chat;
|
||||
(void)peer;
|
||||
(void)media;
|
||||
return -1;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* dc_internal.h -- private types of libDtChat
|
||||
*
|
||||
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
||||
*
|
||||
* Nothing here is installed. The public surface is include/DtChat/*.h.
|
||||
*/
|
||||
#ifndef _DtChat_dc_internal_h
|
||||
#define _DtChat_dc_internal_h
|
||||
|
||||
#include "DtChat/DtChat.h"
|
||||
#include "DtChat/Ipc.h"
|
||||
|
||||
#endif /* _DtChat_dc_internal_h */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* test_frame.c -- unit tests for libDtChatIpc's line framing
|
||||
*
|
||||
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
||||
*/
|
||||
#include "DtChat/Ipc.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int fails;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { \
|
||||
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
|
||||
#cond); \
|
||||
fails++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static void
|
||||
feed(DcFrameBuf *frame, const char *s)
|
||||
{
|
||||
CHECK(dc_frame_feed(frame, s, strlen(s)) == 0);
|
||||
}
|
||||
|
||||
static void
|
||||
test_lines(void)
|
||||
{
|
||||
DcFrameBuf frame;
|
||||
char *line;
|
||||
size_t len;
|
||||
|
||||
dc_frame_init(&frame);
|
||||
feed(&frame, "hello\n");
|
||||
line = dc_frame_next(&frame, &len);
|
||||
CHECK(line != NULL);
|
||||
if (line != NULL) {
|
||||
CHECK(strcmp(line, "hello") == 0);
|
||||
CHECK(len == 5);
|
||||
free(line);
|
||||
}
|
||||
CHECK(dc_frame_next(&frame, &len) == NULL);
|
||||
|
||||
feed(&frame, "par");
|
||||
feed(&frame, "tial\n");
|
||||
line = dc_frame_next(&frame, &len);
|
||||
CHECK(line != NULL && strcmp(line, "partial") == 0);
|
||||
free(line);
|
||||
|
||||
feed(&frame, "a\nb\n");
|
||||
line = dc_frame_next(&frame, &len);
|
||||
CHECK(line != NULL && strcmp(line, "a") == 0);
|
||||
free(line);
|
||||
line = dc_frame_next(&frame, &len);
|
||||
CHECK(line != NULL && strcmp(line, "b") == 0);
|
||||
free(line);
|
||||
CHECK(dc_frame_next(&frame, &len) == NULL);
|
||||
|
||||
feed(&frame, "x\r\n");
|
||||
line = dc_frame_next(&frame, &len);
|
||||
CHECK(line != NULL && strcmp(line, "x") == 0);
|
||||
CHECK(len == 1);
|
||||
free(line);
|
||||
|
||||
feed(&frame, "\n");
|
||||
line = dc_frame_next(&frame, &len);
|
||||
CHECK(line != NULL && strcmp(line, "") == 0);
|
||||
CHECK(len == 0);
|
||||
free(line);
|
||||
|
||||
dc_frame_free(&frame);
|
||||
}
|
||||
|
||||
static void
|
||||
test_bad(void)
|
||||
{
|
||||
DcFrameBuf frame;
|
||||
char *big;
|
||||
size_t n;
|
||||
|
||||
dc_frame_init(&frame);
|
||||
CHECK(dc_frame_feed(&frame, "a\0b", 3) == -1);
|
||||
dc_frame_free(&frame);
|
||||
|
||||
dc_frame_init(&frame);
|
||||
n = DC_IPC_MAX_LINE + 2;
|
||||
big = malloc(n);
|
||||
if (big == NULL)
|
||||
return;
|
||||
memset(big, 'a', n);
|
||||
CHECK(dc_frame_feed(&frame, big, n) == -1);
|
||||
dc_frame_free(&frame);
|
||||
|
||||
/* exactly the maximum line length is allowed */
|
||||
dc_frame_init(&frame);
|
||||
big[DC_IPC_MAX_LINE] = '\n';
|
||||
CHECK(dc_frame_feed(&frame, big, DC_IPC_MAX_LINE + 1) == 0);
|
||||
{
|
||||
char *line = dc_frame_next(&frame, NULL);
|
||||
CHECK(line != NULL);
|
||||
if (line != NULL) {
|
||||
CHECK(strlen(line) == DC_IPC_MAX_LINE);
|
||||
free(line);
|
||||
}
|
||||
}
|
||||
dc_frame_free(&frame);
|
||||
free(big);
|
||||
}
|
||||
|
||||
static void
|
||||
test_send(void)
|
||||
{
|
||||
int fds[2];
|
||||
char buf[32];
|
||||
ssize_t n;
|
||||
|
||||
if (pipe(fds) != 0) {
|
||||
perror("pipe");
|
||||
fails++;
|
||||
return;
|
||||
}
|
||||
CHECK(dc_frame_send(fds[1], "abc", 3) == 0);
|
||||
n = read(fds[0], buf, sizeof(buf));
|
||||
CHECK(n == 4);
|
||||
CHECK(n == 4 && memcmp(buf, "abc\n", 4) == 0);
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
test_lines();
|
||||
test_bad();
|
||||
test_send();
|
||||
if (fails != 0) {
|
||||
fprintf(stderr, "%d test(s) failed\n", fails);
|
||||
return 1;
|
||||
}
|
||||
printf("test_frame: all ok\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,292 @@
|
||||
/*
|
||||
* test_json.c -- unit tests for libDtChatIpc's JSON reader/writer
|
||||
*
|
||||
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
||||
*/
|
||||
#include "DtChat/Ipc.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int fails;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { \
|
||||
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
|
||||
#cond); \
|
||||
fails++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static DcJson *
|
||||
parse_ok(const char *text)
|
||||
{
|
||||
char err[256];
|
||||
DcJson *json = dc_json_parse(text, strlen(text), err, sizeof(err));
|
||||
|
||||
if (json == NULL)
|
||||
fprintf(stderr, "parse failed for [%s]: %s\n", text, err);
|
||||
return json;
|
||||
}
|
||||
|
||||
static void
|
||||
expect_bad(const char *text)
|
||||
{
|
||||
char err[256];
|
||||
DcJson *json = dc_json_parse(text, strlen(text), err, sizeof(err));
|
||||
|
||||
if (json != NULL) {
|
||||
fprintf(stderr, "expected failure for [%s]\n", text);
|
||||
fails++;
|
||||
dc_json_free(json);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_basic(void)
|
||||
{
|
||||
DcJson *json = parse_ok("{\"a\":1,\"b\":\"x\",\"c\":true,"
|
||||
"\"d\":false,\"e\":null,\"f\":[1,2,3]}");
|
||||
|
||||
if (json == NULL)
|
||||
return;
|
||||
CHECK(dc_json_type(json) == DC_JSON_OBJECT);
|
||||
CHECK(dc_json_int(dc_json_get(json, "a")) == 1);
|
||||
CHECK(strcmp(dc_json_string(dc_json_get(json, "b")), "x") == 0);
|
||||
CHECK(dc_json_bool(dc_json_get(json, "c")) == 1);
|
||||
CHECK(dc_json_bool(dc_json_get(json, "d")) == 0);
|
||||
CHECK(dc_json_type(dc_json_get(json, "e")) == DC_JSON_NULL);
|
||||
CHECK(dc_json_array_size(dc_json_get(json, "f")) == 3);
|
||||
CHECK(dc_json_int(dc_json_array_get(dc_json_get(json, "f"), 2)) == 3);
|
||||
CHECK(dc_json_array_get(dc_json_get(json, "f"), 3) == NULL);
|
||||
CHECK(dc_json_get(json, "missing") == NULL);
|
||||
CHECK(dc_json_string(json) == NULL);
|
||||
dc_json_free(json);
|
||||
}
|
||||
|
||||
static void
|
||||
test_strings(void)
|
||||
{
|
||||
DcJson *json = parse_ok("\"\\u00e4 \\u20ac \\ud83d\\ude00\"");
|
||||
const char *s;
|
||||
|
||||
if (json == NULL)
|
||||
return;
|
||||
s = dc_json_string(json);
|
||||
CHECK(s != NULL);
|
||||
if (s != NULL) {
|
||||
CHECK(strcmp(s, "\xc3\xa4 \xe2\x82\xac \xf0\x9f\x98\x80") == 0);
|
||||
CHECK(dc_utf8_valid(s, strlen(s)));
|
||||
}
|
||||
dc_json_free(json);
|
||||
|
||||
json = parse_ok("\"a\\\"b\\\\c\\/d\\b\\f\\n\\r\\t\"");
|
||||
if (json != NULL) {
|
||||
CHECK(strcmp(dc_json_string(json), "a\"b\\c/d\b\f\n\r\t") == 0);
|
||||
dc_json_free(json);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_numbers(void)
|
||||
{
|
||||
DcJson *json = parse_ok("[-9223372036854775808,1.5,1e3,-0]");
|
||||
|
||||
if (json == NULL)
|
||||
return;
|
||||
CHECK(dc_json_type(dc_json_array_get(json, 0)) == DC_JSON_INT);
|
||||
CHECK(dc_json_int(dc_json_array_get(json, 0)) == (-9223372036854775807LL - 1));
|
||||
CHECK(dc_json_double(dc_json_array_get(json, 1)) == 1.5);
|
||||
CHECK(dc_json_double(dc_json_array_get(json, 2)) == 1000.0);
|
||||
CHECK(dc_json_type(dc_json_array_get(json, 3)) == DC_JSON_INT);
|
||||
dc_json_free(json);
|
||||
}
|
||||
|
||||
static void
|
||||
test_invalid(void)
|
||||
{
|
||||
expect_bad("");
|
||||
expect_bad("{");
|
||||
expect_bad("}");
|
||||
expect_bad("[]x");
|
||||
expect_bad("{\"a\":1,}");
|
||||
expect_bad("[1,]");
|
||||
expect_bad("{\"a\":1,\"a\":2}");
|
||||
expect_bad("\"\\u0000\"");
|
||||
expect_bad("\"\\ud800\"");
|
||||
expect_bad("\"\\udc00\"");
|
||||
expect_bad("\"\\ud800\\ud800\"");
|
||||
expect_bad("\"\\u12\"");
|
||||
expect_bad("\"\\q\"");
|
||||
expect_bad("\"a\nb\"");
|
||||
expect_bad("01");
|
||||
expect_bad("-");
|
||||
expect_bad("1.");
|
||||
expect_bad("1e");
|
||||
expect_bad("tru");
|
||||
expect_bad("nul");
|
||||
expect_bad("{\"a\" 1}");
|
||||
expect_bad("{\"a\":}");
|
||||
}
|
||||
|
||||
static void
|
||||
test_utf8(void)
|
||||
{
|
||||
const char bad1[] = "\"\xc0\xaf\"";
|
||||
const char bad2[] = "\"\xed\xa0\x80\"";
|
||||
const char good[] = "\"\xc3\xa4\"";
|
||||
char err[256];
|
||||
DcJson *json;
|
||||
|
||||
CHECK(dc_utf8_valid("hello", 5));
|
||||
CHECK(dc_utf8_valid("\xc3\xa4", 2));
|
||||
CHECK(!dc_utf8_valid("\xc0\xaf", 2));
|
||||
CHECK(!dc_utf8_valid("\xed\xa0\x80", 3));
|
||||
CHECK(!dc_utf8_valid("\xf4\x90\x80\x80", 4));
|
||||
CHECK(!dc_utf8_valid("\xc3", 1));
|
||||
|
||||
json = dc_json_parse(bad1, sizeof(bad1) - 1, err, sizeof(err));
|
||||
CHECK(json == NULL);
|
||||
json = dc_json_parse(bad2, sizeof(bad2) - 1, err, sizeof(err));
|
||||
CHECK(json == NULL);
|
||||
json = dc_json_parse(good, sizeof(good) - 1, err, sizeof(err));
|
||||
CHECK(json != NULL);
|
||||
dc_json_free(json);
|
||||
}
|
||||
|
||||
static void
|
||||
test_depth(void)
|
||||
{
|
||||
char buf[128];
|
||||
size_t i;
|
||||
char err[256];
|
||||
DcJson *json;
|
||||
|
||||
for (i = 0; i < 33; i++)
|
||||
buf[i] = '[';
|
||||
for (i = 0; i < 33; i++)
|
||||
buf[33 + i] = ']';
|
||||
buf[66] = '\0';
|
||||
json = dc_json_parse(buf, 66, err, sizeof(err));
|
||||
CHECK(json == NULL);
|
||||
if (json != NULL)
|
||||
dc_json_free(json);
|
||||
|
||||
buf[32] = '\0';
|
||||
for (i = 0; i < 32; i++)
|
||||
buf[32 + i] = ']';
|
||||
buf[64] = '\0';
|
||||
json = dc_json_parse(buf, 64, err, sizeof(err));
|
||||
CHECK(json != NULL);
|
||||
dc_json_free(json);
|
||||
}
|
||||
|
||||
static void
|
||||
test_too_long(void)
|
||||
{
|
||||
char *buf = malloc(DC_IPC_MAX_LINE + 1);
|
||||
char err[256];
|
||||
DcJson *json;
|
||||
|
||||
if (buf == NULL)
|
||||
return;
|
||||
memset(buf, 'a', DC_IPC_MAX_LINE + 1);
|
||||
json = dc_json_parse(buf, DC_IPC_MAX_LINE + 1, err, sizeof(err));
|
||||
CHECK(json == NULL);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
static void
|
||||
test_writer(void)
|
||||
{
|
||||
DcJson *obj = dc_json_new_object();
|
||||
DcJson *arr = dc_json_new_array();
|
||||
DcJson *nested = dc_json_new_object();
|
||||
char *text;
|
||||
size_t len;
|
||||
DcJson *again;
|
||||
|
||||
CHECK(obj != NULL && arr != NULL && nested != NULL);
|
||||
CHECK(dc_json_object_set(obj, "a", dc_json_new_int(1)) == 0);
|
||||
CHECK(dc_json_object_set(obj, "b", dc_json_new_string("x")) == 0);
|
||||
CHECK(dc_json_array_append(arr, dc_json_new_bool(1)) == 0);
|
||||
CHECK(dc_json_array_append(arr, dc_json_new_null()) == 0);
|
||||
CHECK(dc_json_object_set(obj, "c", arr) == 0);
|
||||
CHECK(dc_json_object_set(nested, "e", dc_json_new_int(2)) == 0);
|
||||
CHECK(dc_json_object_set(obj, "d", nested) == 0);
|
||||
|
||||
text = dc_json_write(obj, &len);
|
||||
CHECK(text != NULL);
|
||||
if (text != NULL) {
|
||||
again = dc_json_parse(text, len, NULL, 0);
|
||||
CHECK(again != NULL);
|
||||
if (again != NULL) {
|
||||
CHECK(dc_json_int(dc_json_get(again, "a")) == 1);
|
||||
CHECK(strcmp(dc_json_string(dc_json_get(again, "b")),
|
||||
"x") == 0);
|
||||
CHECK(dc_json_array_size(dc_json_get(again, "c")) == 2);
|
||||
CHECK(dc_json_int(dc_json_get(
|
||||
dc_json_get(again, "d"), "e")) == 2);
|
||||
dc_json_free(again);
|
||||
}
|
||||
free(text);
|
||||
}
|
||||
|
||||
/* replace an existing key */
|
||||
CHECK(dc_json_object_set(obj, "a", dc_json_new_int(3)) == 0);
|
||||
text = dc_json_write(obj, &len);
|
||||
CHECK(text != NULL);
|
||||
if (text != NULL) {
|
||||
again = dc_json_parse(text, len, NULL, 0);
|
||||
CHECK(again != NULL);
|
||||
if (again != NULL) {
|
||||
CHECK(dc_json_int(dc_json_get(again, "a")) == 3);
|
||||
dc_json_free(again);
|
||||
}
|
||||
free(text);
|
||||
}
|
||||
dc_json_free(obj);
|
||||
|
||||
/* escaping round-trip */
|
||||
obj = dc_json_new_object();
|
||||
CHECK(dc_json_object_set(obj, "s",
|
||||
dc_json_new_string("a\"b\\c\n\t\xc3\xa4")) == 0);
|
||||
text = dc_json_write(obj, &len);
|
||||
CHECK(text != NULL);
|
||||
if (text != NULL) {
|
||||
again = dc_json_parse(text, len, NULL, 0);
|
||||
CHECK(again != NULL);
|
||||
if (again != NULL) {
|
||||
CHECK(strcmp(dc_json_string(dc_json_get(again, "s")),
|
||||
"a\"b\\c\n\t\xc3\xa4") == 0);
|
||||
dc_json_free(again);
|
||||
}
|
||||
free(text);
|
||||
}
|
||||
dc_json_free(obj);
|
||||
|
||||
CHECK(dc_json_new_string("\xc0\xaf") == NULL);
|
||||
CHECK(dc_json_new_double(NAN) == NULL);
|
||||
CHECK(dc_json_new_double(INFINITY) == NULL);
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
test_basic();
|
||||
test_strings();
|
||||
test_numbers();
|
||||
test_invalid();
|
||||
test_utf8();
|
||||
test_depth();
|
||||
test_too_long();
|
||||
test_writer();
|
||||
if (fails != 0) {
|
||||
fprintf(stderr, "%d test(s) failed\n", fails);
|
||||
return 1;
|
||||
}
|
||||
printf("test_json: all ok\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user