Ejemplo n.º 1
0
static void test_otrl_instag_find(void)
{
	OtrlUserState us = otrl_userstate_create();
    OtrlInsTag* p1 = (OtrlInsTag *)malloc(sizeof(OtrlInsTag));
    OtrlInsTag* p2 = (OtrlInsTag *)malloc(sizeof(OtrlInsTag));

    p1->accountname = strdup("account one");
    p1->protocol = strdup("protocol one");
    p1->instag = otrl_instag_get_new();
    p1->next = us->instag_root;
    p1->tous = &(us->instag_root);
    us->instag_root = p1;

    p2->accountname = strdup("account two");
    p2->protocol = strdup("protocol two");
    p2->instag = otrl_instag_get_new();
    p2->next = us->instag_root;
	p2->next->tous = &(p2->next);
    p2->tous = &(us->instag_root);
    us->instag_root = p2;

	ok(otrl_instag_find(us, "account two", "protocol two") == p2,
			"Found instag");
	ok(otrl_instag_find(us, "account one", "protocol two") == NULL,
			"Instag not found");
	ok(otrl_instag_find(us, "account three", "protocol three") == NULL,
			"Instag not found");
}
Ejemplo n.º 2
0
static void test_otrl_instag_read(void)
{
	OtrlUserState us = otrl_userstate_create();
	OtrlInsTag* one, *two, *three, *four;
	char sone[9] = {0},
		 stwo[9] = {0},
		 sfour[9] = {0};
	one = two = three = four = NULL;
	ok(otrl_instag_read(us, "/non_existent_file") ==
			gcry_error_from_errno(ENOENT),
			"Non-existent file detected");

	ok(otrl_instag_read(us, instag_filepath) == GPG_ERR_NO_ERROR,
			"Instag called with success");

	one = otrl_instag_find(us, "alice_xmpp", "XMPP");
	snprintf(sone, sizeof(sone), "%08x", one->instag);

	two = otrl_instag_find(us, "alice_irc", "IRC");
	snprintf(stwo, sizeof(stwo), "%08x", two->instag);
	
	three = otrl_instag_find(us, "alice_inv", "IRC");
	
	four = otrl_instag_find(us, "alice_icq", "ICQ");
	snprintf(sfour, sizeof(sfour), "%08x", four->instag);

	ok(one && two && !three && four &&
			strcmp(sone, "01234567") == 0 &&
			strcmp(stwo, "9abcdef0") == 0 &&
			strcmp(sfour, "98765432") == 0,
			"Instag succesfully read");
}
Ejemplo n.º 3
0
static void test_otrl_instag_read_FILEp(void)
{
    FILE* instf = fopen(instag_filepath, "rb");
	OtrlUserState us = otrl_userstate_create();
	OtrlInsTag* one, *two, *three, *four;
	char sone[9] = {0},
		 stwo[9] = {0},
		 sfour[9] = {0};
	one = two = three = four = NULL;

    ok(otrl_instag_read_FILEp(us, instf) == gcry_error(GPG_ERR_NO_ERROR),
			"Instead read from FILEp");
    fclose(instf);

	one = otrl_instag_find(us, "alice_xmpp", "XMPP");
	snprintf(sone, sizeof(sone), "%08x", one->instag);

	two = otrl_instag_find(us, "alice_irc", "IRC");
	snprintf(stwo, sizeof(stwo), "%08x", two->instag);
	
	three = otrl_instag_find(us, "alice_inv", "IRC");
	
	four = otrl_instag_find(us, "alice_icq", "ICQ");
	snprintf(sfour, sizeof(sfour), "%08x", four->instag);

	ok(one && two && !three && four &&
			strcmp(sone, "01234567") == 0 &&
			strcmp(stwo, "9abcdef0") == 0 &&
			strcmp(sfour, "98765432") == 0,
			"Instag succesfully read");
}
Ejemplo n.º 4
0
/* Look up a connection context by name/account/protocol/instag from the given
 * OtrlUserState.  If add_if_missing is true, allocate and return a new
 * context if one does not currently exist.  In that event, call
 * add_app_data(data, context) so that app_data and app_data_free can be
 * filled in by the application, and set *addedp to 1.
 * In the 'their_instance' field note that you can also specify a 'meta-
 * instance' value such as OTRL_INSTAG_MASTER, OTRL_INSTAG_RECENT,
 * OTRL_INSTAG_RECENT_RECEIVED and OTRL_INSTAG_RECENT_SENT. */
ConnContext * otrl_context_find(OtrlUserState us, const char *user,
	const char *accountname, const char *protocol,
	otrl_instag_t their_instance, int add_if_missing, int *addedp,
	void (*add_app_data)(void *data, ConnContext *context), void *data)
{
    ConnContext ** curp;
    int usercmp = 1, acctcmp = 1, protocmp = 1;
    if (addedp) *addedp = 0;
    if (!user || !accountname || !protocol) return NULL;

    for (curp = &(us->context_root); *curp; curp = &((*curp)->next)) {
	if ((usercmp = strcmp((*curp)->username, user)) > 0 ||
		(usercmp == 0 &&
		(acctcmp = strcmp((*curp)->accountname, accountname)) > 0) ||
		(usercmp == 0 && acctcmp == 0 &&
		(protocmp = strcmp((*curp)->protocol, protocol)) > 0) ||
		(usercmp == 0 && acctcmp == 0 && protocmp == 0
		&& (their_instance < OTRL_MIN_VALID_INSTAG ||
		    ((*curp)->their_instance >= their_instance))))
	    /* We're at the right place in the list.  We've either found
	     * it, or gone too far. */
	    break;
    }

    if (usercmp == 0 && acctcmp == 0 && protocmp == 0 && *curp &&
	    (their_instance < OTRL_MIN_VALID_INSTAG ||
	    (their_instance == (*curp)->their_instance))) {
	/* Found one! */
	if (their_instance >= OTRL_MIN_VALID_INSTAG ||
		their_instance == OTRL_INSTAG_MASTER) {
	    return *curp;
	}

	/* We need to go back and check more values in the context */
	switch(their_instance) {
	    case OTRL_INSTAG_BEST:
		return otrl_context_find_recent_secure_instance(*curp);
	    case OTRL_INSTAG_RECENT:
	    case OTRL_INSTAG_RECENT_RECEIVED:
	    case OTRL_INSTAG_RECENT_SENT:
		return otrl_context_find_recent_instance(*curp, their_instance);
	    default:
		return NULL;
	}
    }

    if (add_if_missing) {
	ConnContext *newctx;
	OtrlInsTag *our_instag = (OtrlInsTag *)otrl_instag_find(us, accountname,
		protocol);

	if (addedp) *addedp = 1;
	newctx = new_context(user, accountname, protocol);
	newctx->next = *curp;
	if (*curp) {
	    (*curp)->tous = &(newctx->next);
	}
	*curp = newctx;
	newctx->tous = curp;
	if (add_app_data) {
	    add_app_data(data, *curp);
	}

	/* Initialize specified instance tags */
	if (our_instag) {
	    newctx->our_instance = our_instag->instag;
	}

	if (their_instance >= OTRL_MIN_VALID_INSTAG ||
		their_instance == OTRL_INSTAG_MASTER) {
	    newctx->their_instance = their_instance;
	}

	if (their_instance >= OTRL_MIN_VALID_INSTAG) {
	    newctx->m_context = otrl_context_find(us, user, accountname,
		protocol, OTRL_INSTAG_MASTER, 1, NULL, add_app_data, data);
	}

	if (their_instance == OTRL_INSTAG_MASTER) {
	    /* if we're adding a master, there are no children, so the most
	     * recent context is the one we add. */
	    newctx->recent_child = newctx;
	    newctx->recent_rcvd_child = newctx;
	    newctx->recent_sent_child = newctx;
	}

	return *curp;
    }
    return NULL;
}