END_TEST START_TEST (test_kvpair_storage) { char* args[] = {"arg1", "arg2", NULL}; kvpair_t *pair = mk_kvpair("some_key", args); fail_if(pair == NULL, "Didn't create a pair."); fail_unless(strcmp(pair->key, "some_key") == 0, "Key is broken."); fail_unless(strcmp(pair->values[0], "arg1") == 0, "First value is broken."); fail_unless(strcmp(pair->values[1], "arg2") == 0, "Second value is broken."); fail_unless(pair->used_values == 2, "Wrong number of used values."); fail_unless(pair->allocated_values >= pair->used_values, "Allocated values can't be smaller than used values."); fail_unless(pair->next == NULL, "Next pointer is non-null."); fail_unless(save_kvpairs(handle, pair, db_loc), "Failed to save kv pairs."); kvpair_t *dbpair = load_kvpairs(handle, db_loc); check_pair_equality(pair, dbpair); free_kvpair(pair); free_kvpair(dbpair); }
static xmpp_stanza_t* n_handler(const char *cmd, xmpp_stanza_t* cmd_stanza, xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata, bool direct, conflate_mgmt_cb_t cb) { conflate_handle_t *handle = (conflate_handle_t*) userdata; xmpp_ctx_t *ctx = handle->ctx; conflate_form_result result = { .conn = conn, .ctx = ctx, .reply = NULL, .cmd_res = NULL, .container = NULL }; kvpair_t *form = NULL; assert(cb); result.reply = create_reply(ctx, stanza); result.cmd_res = create_cmd_response(ctx, cmd_stanza); add_and_release(result.reply, result.cmd_res); xmpp_stanza_t *x = xmpp_stanza_get_child_by_name(cmd_stanza, "x"); if (x) { xmpp_stanza_t *fields = xmpp_stanza_get_child_by_name(x, "field"); if (fields) { form = grok_form(fields); } } enum conflate_mgmt_cb_result rv = cb(handle->conf->userdata, handle, cmd, direct, form, &result); CONFLATE_LOG(handle, LOG_LVL_DEBUG, "Result of %s: %s", cmd, cb_name(rv)); switch (rv) { case RV_ERROR: add_cmd_error(ctx, result.reply, "500", "urn:ietf:params:xml:ns:xmpp-stanzas", "internal-server-error", NULL, NULL); break; case RV_BADARG: add_cmd_error(ctx, result.reply, "400", "urn:ietf:params:xml:ns:xmpp-stanzas", "bad-request", "http://jabber.org/protocol/commands", "bad-payload"); break; case RV_OK: /* Things are good, use the built form */ break; } free_kvpair(form); return result.reply; }
void free_kvpair(kvpair_t* pair) { if (pair) { free_kvpair(pair->next); free(pair->key); free_string_list(pair->values); free(pair); } }
void* run_conflate(void *arg) { conflate_handle_t* handle = (conflate_handle_t*)arg; /* Before connecting and all that, load the stored config */ kvpair_t* conf = load_kvpairs(handle, handle->conf->save_path); if (conf) { handle->conf->new_config(handle->conf->userdata, conf); free_kvpair(conf); } xmpp_log_t strophe_logger = { &conflate_strophe_logger, handle }; /* Run forever */ for (;;) { handle->ctx = xmpp_ctx_new(NULL, &strophe_logger); assert(handle->ctx); handle->conn = xmpp_conn_new(handle->ctx); assert(handle->conn); /* Use the stored jid if there is one */ char *db_jid = conflate_get_private(handle, STORED_JID_KEY, handle->conf->save_path); if (db_jid) { CONFLATE_LOG(handle, LOG_LVL_DEBUG, "Using jid from db: %s", db_jid); xmpp_conn_set_jid(handle->conn, db_jid); free(db_jid); } else { CONFLATE_LOG(handle, LOG_LVL_DEBUG, "Using provided jid: %s", handle->conf->jid); xmpp_conn_set_jid(handle->conn, handle->conf->jid); } xmpp_conn_set_pass(handle->conn, handle->conf->pass); xmpp_connect_client(handle->conn, handle->conf->host, 0, conn_handler, handle); xmpp_run(handle->ctx); CONFLATE_LOG(handle, LOG_LVL_INFO, "xmpp_run exited"); xmpp_conn_release(handle->conn); xmpp_ctx_free(handle->ctx); sleep(5); CONFLATE_LOG(handle, LOG_LVL_INFO, "reconnecting"); } CONFLATE_LOG(handle, LOG_LVL_FATAL, "Exited an infinite loop."); return NULL; }
END_TEST START_TEST (test_copy_pair) { char *args1[] = {"arg1", "arg2", NULL}; char *args2[] = {"other", NULL}; kvpair_t *pair1 = mk_kvpair("some_key", args1); pair = mk_kvpair("some_other_key", args2); pair->next = pair1; kvpair_t *copy = dup_kvpair(pair); fail_if(copy == NULL, "Copy failed."); fail_if(copy == pair, "Copy something not an identity."); fail_unless(strcmp(copy->key, pair->key) == 0, "Keys don't match."); fail_if(copy->key == pair->key, "Keys were identical."); check_pair_equality(pair, copy); free_kvpair(copy); }
static void teardown(void) { free_kvpair(pair); }