Skip to content

Commit

Permalink
Correctly add ERROR_2 and test #2
Browse files Browse the repository at this point in the history
  • Loading branch information
claucece committed Dec 4, 2017
1 parent 05ca7c9 commit 0a75ad0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/otrv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -1938,9 +1938,31 @@ static otr4_err_t otrv4_receive_data_message(otrv4_response_t *response,
memset(enc_key, 0, sizeof(m_enc_key_t));
memset(mac_key, 0, sizeof(m_mac_key_t));

// TODO: warn the user and send an error message with a code.
// TODO: check this case with Nik on otr3
if (otr->state != OTRV4_STATE_ENCRYPTED_MESSAGES) {
data_message_free(msg);
const char *err_code = otrv4_error_message(OTR4_ERR_MSG_NOT_PRIVATE);
char *err_msg = malloc(strlen(OTR4_ERROR_PREFIX) +
strlen(OTR4_ERROR_CODE_2) + strlen(err_code) + 1);
if (err_msg) {
strcpy(err_msg, OTR4_ERROR_PREFIX);
strcpy(err_msg + strlen(OTR4_ERROR_PREFIX), OTR4_ERROR_CODE_1);
strcat(err_msg, err_code);
}
free((char *)err_code);

size_t dstlen = strlen(err_msg) + 1;
uint8_t *dst = malloc(dstlen);
if (!dst) {
free(err_msg);
return OTR4_ERROR;
}

stpcpy((char *)dst, err_msg);
response->to_send = otrl_base64_otr_encode(dst, dstlen);

free(dst);
free(err_msg);
free(msg);
return OTR4_ERROR;
}

Expand Down Expand Up @@ -2285,19 +2307,7 @@ static otr4_err_t otrv4_prepare_to_send_data_message(string_t *to_send,
if (otr->state == OTRV4_STATE_FINISHED)
return OTR4_ERROR; // Should restart

// TODO: check this case with Nik on otr3
if (otr->state != OTRV4_STATE_ENCRYPTED_MESSAGES) {
const char *err_code = otrv4_error_message(OTR4_ERR_MSG_NOT_PRIVATE);
char *err_msg = malloc(strlen(OTR4_ERROR_PREFIX) +
strlen(OTR4_ERROR_CODE_2) + strlen(err_code) + 1);
if (err_msg) {
strcpy(err_msg, OTR4_ERROR_PREFIX);
strcpy(err_msg + strlen(OTR4_ERROR_PREFIX), OTR4_ERROR_CODE_1);
strcat(err_msg, err_code);
}

free((char *)err_code);
free(err_msg); // TODO: for the moment
return OTR4_STATE_NOT_ENCRYPTED; // TODO: queue message
}

Expand Down
1 change: 1 addition & 0 deletions src/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ int main(int argc, char **argv) {
test_api_non_interactive_conversation);
g_test_add_func("/api/non_interactive_conversation_enc_msg/v4",
test_api_non_interactive_conversation_with_enc_msg);
g_test_add_func("/api/conversation_errors", test_api_conversation_errors);
g_test_add_func("/api/conversation/v3", test_api_conversation_v3);
g_test_add_func("/api/smp", test_api_smp);
g_test_add_func("/api/smp_abort", test_api_smp_abort);
Expand Down
54 changes: 54 additions & 0 deletions src/test/test_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,60 @@ void test_api_non_interactive_conversation_with_enc_msg(void) {
OTR4_FREE;
}

void test_api_conversation_errors(void) {
OTR4_INIT;

otr4_client_state_t *alice_state = otr4_client_state_new(NULL);
otr4_client_state_t *bob_state = otr4_client_state_new(NULL);

otrv4_t *alice = set_up_otr(alice_state, ALICE_IDENTITY, PHI, 1);
otrv4_t *bob = set_up_otr(bob_state, BOB_IDENTITY, PHI, 2);

bob_state->pad = true;
alice_state->pad = true;

// DAKE HAS FINISHED.
do_dake_fixture(alice, bob);

int message_id = 2;
otrv4_response_t *response_to_alice = NULL;

// Alice sends a data message
string_t to_send = NULL;
tlv_t *tlvs = NULL;
otr4_err_t err;

err = otrv4_prepare_to_send_message(&to_send, "hi", &tlvs, alice);
assert_msg_sent(err, to_send);
otrv4_assert(tlvs);
otrv4_assert(!alice->keys->old_mac_keys);

// This is a follow up message.
g_assert_cmpint(alice->keys->i, ==, 0);
g_assert_cmpint(alice->keys->j, ==, message_id);

bob->state = OTRV4_STATE_START;

// Bob receives a data message in the incorrect state
response_to_alice = otrv4_response_new();
err = otrv4_receive_message(response_to_alice, to_send, bob);

otrv4_assert(err == OTR4_ERROR);
otrv4_assert(response_to_alice->to_send != NULL);
otrv4_assert(!bob->keys->old_mac_keys);
g_assert_cmpint(bob->keys->i, ==, 0);
g_assert_cmpint(bob->keys->j, ==, 0);

otrv4_tlv_free(tlvs);
free_message_and_response(response_to_alice, to_send);

otrv4_userstate_free_all(alice_state->userstate, bob_state->userstate);
otrv4_client_state_free_all(alice_state, bob_state);
otrv4_free_all(alice, bob);

OTR4_FREE;
}

static void do_ake_otr3(otrv4_t *alice, otrv4_t *bob) {
otrv4_response_t *response_to_bob = otrv4_response_new();
otrv4_response_t *response_to_alice = otrv4_response_new();
Expand Down

0 comments on commit 0a75ad0

Please sign in to comment.