1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/*
* Copyright (C) 2014 - Julien Voisin <julien.voisin@dustri.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, version 2 only, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <limits.h>
#include <pthread.h>
#include <context.h>
#include <tap/tap.h>
#define NUM_TESTS 22
static void test_otrl_context_find_fingerprint(void)
{
unsigned char fingerprint[20] = {0};
int add_if_missing = 0, addedp = 0;
ok(otrl_context_find_fingerprint(NULL, fingerprint,
add_if_missing, &addedp) == NULL, "NULL context detected");
}
static ConnContext *new_context(const char *user, const char *account,
const char *protocol)
{
ConnContext *context;
context = calloc(1, sizeof(ConnContext));
context->username = strdup(user);
context->accountname = strdup(account);
context->protocol = strdup(protocol);
context->m_context = context;
context->active_fingerprint = calloc(1, sizeof(Fingerprint));
context->context_priv = calloc(1, sizeof(ConnContextPriv));
return context;
}
static void free_context(ConnContext *context)
{
free(context->username);
free(context->accountname);
free(context->protocol);
free(context);
}
static void test_otrl_context_find_recent_instance()
{
ConnContext *context = new_context("main", "main", "main");
ConnContext *context_child = new_context("child", "child", "child");
ConnContext *context_rcvd = new_context("rcvd", "rcvd", "rcvd");
ConnContext *context_sent = new_context("sent", "sent", "sent");
ConnContext *tmp;
context->recent_child = context_child;
context->recent_rcvd_child = context_rcvd;
context->recent_sent_child = context_sent;
ok(otrl_context_find_recent_instance(NULL, OTRL_INSTAG_RECENT) == NULL,
"NULL context detected");
tmp = otrl_context_find_recent_instance(context, OTRL_INSTAG_RECENT);
ok(strcmp(tmp->username, "child") == 0, "OTRL_INSTAG_RECENT ok");
tmp = otrl_context_find_recent_instance(context,
OTRL_INSTAG_RECENT_RECEIVED);
ok(strcmp(tmp->username, "rcvd") == 0, "OTRL_INSTAG_RECENT_RECEIVED ok");
tmp = otrl_context_find_recent_instance(context, OTRL_INSTAG_RECENT_SENT);
ok(strcmp(tmp->username, "sent") == 0, "OTRL_INSTAG_RECENT_SENT ok");
tmp = otrl_context_find_recent_instance(context, INT_MAX);
ok(!tmp, "Invalid instag detected");
free_context(context);
free_context(context_child);
free_context(context_rcvd);
free_context(context_sent);
}
static void test_otrl_context_find_recent_secure_instance(void)
{
ConnContext *context1 = new_context("1", "1", "1");
ConnContext *context2 = new_context("2", "2", "2");
ConnContext *tmp;
ok(otrl_context_find_recent_secure_instance(NULL) == NULL,
"NULL detected");
context1->next = context2;
context2->next = NULL;
context2->m_context = context1;
context1->msgstate = OTRL_MSGSTATE_PLAINTEXT;
context2->msgstate = OTRL_MSGSTATE_PLAINTEXT;
tmp = otrl_context_find_recent_secure_instance(context1);
ok(tmp == context2, "Same msgstate");
context1->msgstate = OTRL_MSGSTATE_PLAINTEXT;
context2->msgstate = OTRL_MSGSTATE_FINISHED;
tmp = otrl_context_find_recent_secure_instance(context1);
ok(tmp == context2, "plaintext then finished");
context1->msgstate = OTRL_MSGSTATE_PLAINTEXT;
context2->msgstate = OTRL_MSGSTATE_ENCRYPTED;
tmp = otrl_context_find_recent_secure_instance(context1);
ok(tmp == context2, "Most secure context found");
context1->msgstate = OTRL_MSGSTATE_ENCRYPTED;
context2->msgstate = OTRL_MSGSTATE_ENCRYPTED;
tmp = otrl_context_find_recent_secure_instance(context1);
ok(tmp == context2, "Most secure context found");
context1->msgstate = OTRL_MSGSTATE_PLAINTEXT;
context2->msgstate = OTRL_MSGSTATE_PLAINTEXT;
context2->active_fingerprint->trust = strdup("hello");
tmp = otrl_context_find_recent_secure_instance(context1);
ok(tmp == context2, "Most secure context found");
free(context2->active_fingerprint);
context2->active_fingerprint = NULL;
context1->msgstate = OTRL_MSGSTATE_PLAINTEXT;
context2->msgstate = OTRL_MSGSTATE_PLAINTEXT;
context2->context_priv->lastrecv = 1;
tmp = otrl_context_find_recent_secure_instance(context1);
ok(tmp == context2, "Most secure context found");
context1->msgstate = OTRL_MSGSTATE_PLAINTEXT;
context1->context_priv->lastrecv = 2;
context2->msgstate = OTRL_MSGSTATE_PLAINTEXT;
tmp = otrl_context_find_recent_secure_instance(context1);
ok(tmp == context1, "Most secure context found");
free_context(context1);
free_context(context2);
}
static void test_otrl_context_is_fingerprint_trusted()
{
Fingerprint fprint;
fprint.trust = NULL;
ok(otrl_context_is_fingerprint_trusted(NULL) == 0,
"NULL fingerprint detected");
ok(otrl_context_is_fingerprint_trusted(&fprint) == 0,
"NULL trust detected");
fprint.trust = "1234";
ok(otrl_context_is_fingerprint_trusted(&fprint) != 0,
"Trusted fingerprint detected");
}
static void test_otrl_context_update_recent_child()
{
ConnContext context1, context2;
context1.m_context = &context1;
context2.m_context = &context1;
otrl_context_update_recent_child(&context1, 0);
ok(context1.recent_rcvd_child == &context1 &&
context1.recent_child == &context1,
"Recent self rcvd set");
otrl_context_update_recent_child(&context1, 1);
ok(context1.recent_sent_child == &context1 &&
context1.recent_child == &context1,
"Recent self sent set");
otrl_context_update_recent_child(&context2, 0);
ok(context1.recent_rcvd_child == &context2 &&
context1.recent_child == &context2,
"Recent rcvd set");
otrl_context_update_recent_child(&context2, 1);
ok(context1.recent_sent_child == &context2 &&
context1.recent_child == &context2,
"Recent sent set");
}
static void test_otrl_context_set_trust(void)
{
Fingerprint fprint;
const char *trust = "I don't trust anyone.";
fprint.trust = NULL;
otrl_context_set_trust(&fprint, trust);
ok(strcmp(fprint.trust, trust) == 0, "Fingerprint set with success");
}
int main(int argc, char **argv)
{
plan_tests(NUM_TESTS);
test_otrl_context_set_trust();
test_otrl_context_find_recent_instance();
test_otrl_context_find_fingerprint();
test_otrl_context_find_recent_secure_instance();
test_otrl_context_is_fingerprint_trusted();
test_otrl_context_update_recent_child();
return 0;
}
|