/* * test_adapter.c -- tests for the adapter process manager * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. */ #include "dc_internal.h" #include #include #include #include static int fails; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \ #cond); \ fails++; \ } \ } while (0) struct Seen { int starting; int hello; int online; int backoff; int offline; int message; int sent; int answer; int error; char text[128]; char client[64]; }; static void on_event(DcAdapter *adapter, const DcJson *event, void *arg) { struct Seen *seen = arg; const char *type = dc_json_string(dc_json_get(event, "type")); const char *value; (void)adapter; if (type == NULL) return; if (strcmp(type, "hello") == 0) { seen->hello++; } else if (strcmp(type, "state") == 0) { const char *state = dc_json_string(dc_json_get(event, "state")); if (state == NULL) return; if (strcmp(state, "starting") == 0) seen->starting++; else if (strcmp(state, "online") == 0) seen->online++; else if (strcmp(state, "backoff") == 0) seen->backoff++; else if (strcmp(state, "offline") == 0) seen->offline++; } else if (strcmp(type, "message") == 0) { seen->message++; value = dc_json_string(dc_json_get(event, "text")); if (value != NULL) snprintf(seen->text, sizeof(seen->text), "%s", value); } else if (strcmp(type, "sent") == 0) { seen->sent++; value = dc_json_string(dc_json_get(event, "client_id")); if (value != NULL) snprintf(seen->client, sizeof(seen->client), "%s", value); } else if (strcmp(type, "answer-received") == 0) { seen->answer++; } else if (strcmp(type, "error") == 0) { seen->error++; } } static int has_online(void *arg) { return ((struct Seen *)arg)->online > 0; } static int has_hello(void *arg) { return ((struct Seen *)arg)->hello > 0; } static int has_two_hello(void *arg) { return ((struct Seen *)arg)->hello >= 2; } static int has_message(void *arg) { return ((struct Seen *)arg)->message > 0 && ((struct Seen *)arg)->sent > 0; } static int has_answer(void *arg) { return ((struct Seen *)arg)->answer > 0; } static int has_error(void *arg) { return ((struct Seen *)arg)->error > 0; } static int adapter_stopped(void *arg) { return dc_adapter_state((DcAdapter *)arg) == DC_ADAPTER_STOPPED; } static int wait_until(DcLoop *loop, int (*pred)(void *), void *arg, int ms) { long long start = dc_loop_now_ms(); while (!pred(arg) && dc_loop_now_ms() - start < ms) (void)dc_loop_run(loop, 20); return pred(arg); } static int fake_path(char *out, size_t cap, const char *argv0) { const char *slash = strrchr(argv0, '/'); if (slash != NULL) { snprintf(out, cap, "%.*s/fake_adapter", (int)(slash - argv0), argv0); } else { snprintf(out, cap, "./fake_adapter"); } return access(out, X_OK) == 0; } static DcJson * settings_with_mode(const char *mode) { DcJson *settings = dc_json_new_object(); (void)dc_json_object_set(settings, "mode", dc_json_new_string(mode)); return settings; } static void test_normal(DcLoop *loop, const char *path) { DcJson *settings = settings_with_mode("normal"); DcJson *secrets = dc_json_new_object(); struct Seen seen; DcAdapter *adapter; memset(&seen, 0, sizeof(seen)); adapter = dc_adapter_start(loop, path, "acct", settings, secrets, on_event, &seen); CHECK(adapter != NULL); if (adapter == NULL) return; CHECK(wait_until(loop, has_online, &seen, 3000)); CHECK(seen.hello == 1); CHECK(seen.starting == 1); CHECK(dc_adapter_state(adapter) == DC_ADAPTER_ONLINE); CHECK(dc_adapter_send_text(adapter, "#test", "hello", "c1") == 0); CHECK(wait_until(loop, has_message, &seen, 2000)); CHECK(strcmp(seen.text, "hello") == 0); CHECK(strcmp(seen.client, "c1") == 0); CHECK(dc_adapter_join(adapter, "#test") == 0); CHECK(dc_adapter_answer(adapter, 7, "code") == 0); CHECK(wait_until(loop, has_answer, &seen, 2000)); dc_adapter_stop(adapter); CHECK(wait_until(loop, adapter_stopped, adapter, 3000)); CHECK(dc_adapter_state(adapter) == DC_ADAPTER_STOPPED); dc_adapter_free(adapter); dc_json_free(settings); dc_json_free(secrets); } static void test_restart(DcLoop *loop, const char *path) { DcJson *settings = settings_with_mode("crash"); DcJson *secrets = dc_json_new_object(); struct Seen seen; DcAdapter *adapter; memset(&seen, 0, sizeof(seen)); adapter = dc_adapter_start(loop, path, "acct", settings, secrets, on_event, &seen); CHECK(adapter != NULL); if (adapter == NULL) return; CHECK(wait_until(loop, has_hello, &seen, 3000)); CHECK(wait_until(loop, has_two_hello, &seen, 6000)); CHECK(dc_adapter_restarts(adapter) >= 1); CHECK(seen.backoff >= 1); dc_adapter_stop(adapter); CHECK(adapter_stopped(adapter) || wait_until(loop, adapter_stopped, adapter, 3000)); dc_adapter_free(adapter); dc_json_free(settings); dc_json_free(secrets); } static void test_garbage(DcLoop *loop, const char *path) { DcJson *settings = settings_with_mode("garbage"); DcJson *secrets = dc_json_new_object(); struct Seen seen; DcAdapter *adapter; memset(&seen, 0, sizeof(seen)); adapter = dc_adapter_start(loop, path, "acct", settings, secrets, on_event, &seen); CHECK(adapter != NULL); if (adapter == NULL) return; CHECK(wait_until(loop, has_error, &seen, 3000)); dc_adapter_stop(adapter); (void)wait_until(loop, adapter_stopped, adapter, 3000); dc_adapter_free(adapter); dc_json_free(settings); dc_json_free(secrets); } int main(int argc, char **argv) { char path[512]; DcLoop *loop; if (argc < 1 || !fake_path(path, sizeof(path), argv[0])) { fprintf(stderr, "fake_adapter not found next to %s\n", argc > 0 ? argv[0] : "test_adapter"); return 1; } loop = dc_loop_new(); if (loop == NULL) { perror("dc_loop_new"); return 1; } test_normal(loop, path); test_restart(loop, path); test_garbage(loop, path); dc_loop_free(loop); if (fails != 0) { fprintf(stderr, "%d test(s) failed\n", fails); return 1; } printf("test_adapter: all ok\n"); return 0; }