media key: use the new function normalize_fpr (not for 2.x)

It is important to keep this commit separated because the new function does not
exist on Engine 2.x; in case I need to backport bug fixes I need to avoid this.

Fix the media key test suite as well; now that FPRs are actually validated and
normalised I can no longer abuse media key functions passing intuitive
funny-looking FPRs.
master
positron 1 month ago
parent d9def86933
commit 5b3e13d68d

@ -63,27 +63,30 @@ PEP_STATUS media_key_insert(PEP_SESSION session,
/* Initialise heap-allocated pointers to NULL so that they all have a known
value in case of allocation failure. */
PEP_STATUS status = PEP_STATUS_OK;
char *normalized_fpr = NULL;
stringpair_t *new_pair = NULL;
#define FAIL(the_status) \
do { \
status = (the_status); \
goto error; \
} while (false)
/* Work with a normalised copy of the FPR. */
size_t fpr_length = strlen(fpr);
normalized_fpr = malloc(fpr_length + 1);
if (normalized_fpr == NULL)
goto out_of_memory;
int i;
for (i = 0; i /* <= , to include the trailing '\0' */ <= fpr_length; i ++)
normalized_fpr [i] = toupper(fpr [i]);
status = normalize_fpr(session, & normalized_fpr, fpr);
if (status != PEP_STATUS_OK)
FAIL(status);
/* Make the new entry. */
stringpair_list_t *old_map = session->media_key_map;
new_pair = new_stringpair(address_pattern, normalized_fpr);
if (new_pair == NULL)
goto out_of_memory;
FAIL(PEP_OUT_OF_MEMORY);
stringpair_list_t *new_last_element
= stringpair_list_add(old_map, new_pair);
if (new_last_element == NULL)
goto out_of_memory;
FAIL(PEP_OUT_OF_MEMORY);
/* Else the structured ponted by old_map is modified destructively, so we
have nothing else to do as long as the map was not previously NULL... */
if (old_map == NULL)
@ -91,10 +94,11 @@ PEP_STATUS media_key_insert(PEP_SESSION session,
free(normalized_fpr);
return PEP_STATUS_OK;
out_of_memory:
error:
free(normalized_fpr);
free(new_pair);
return PEP_OUT_OF_MEMORY;
return status;
#undef FAIL
}
PEP_STATUS media_key_remove(PEP_SESSION session,

@ -86,6 +86,10 @@ namespace {
TEST_F(MediaKeyTest, check_lookup) {
PEP_STATUS status = PEP_UNKNOWN_ERROR;
#define CHECK_STATUS \
do { \
ASSERT_EQ(status, PEP_STATUS_OK); \
} while (false)
#define TEST_KEY(ADDRESS, EXPECTED_KEY) \
do { \
const char *_address = (ADDRESS); \
@ -111,29 +115,34 @@ TEST_F(MediaKeyTest, check_lookup) {
/* Here instead of using actual key FPR we use human-readable strings which
are an abbreviated form or description of the domain. */
media_key_insert(session, "*@pep.foundation", "AA:PEP");
media_key_insert(session, "*@ageinghacker.net", "BB:AGE");
status = media_key_insert(session, "*@pep.foundation", "aaaa");
CHECK_STATUS;
status = media_key_insert(session, "*@ageinghacker.net", "bbbb");
CHECK_STATUS;
/* A pattern like "*@*.ageinghacker.net" would have been more explicit, but
it is nice to have instead "*ageinghacker.net", which is more general
than the previous one "*@ageinghacker.net": the media-key map order will
make (proper) subdomains of ageinghacker.net match "*@ageinghacker.net"
but not "*ageinghacker.net". */
media_key_insert(session, "*ageinghacker.net", "BB:AGE-SUBDOMAIN");
media_key_insert(session, "mailto:*@run-for-your.life", "CC:RUN");
media_key_insert(session, "?lice@the-world-is-burning.com", "DD:ENJOY");
status = media_key_insert(session, "*ageinghacker.net", "bbcc");
CHECK_STATUS;
status = media_key_insert(session, "mailto:*@run-for-your.life", "cccc");
CHECK_STATUS;
status = media_key_insert(session, "?lice@the-world-is-burning.com", "dddd");
CHECK_STATUS;
/* Do the actual media-key lookups. */
TEST_KEY("luca@pep.foundation", "AA:PEP");
TEST_KEY("luca-pep@run-for-your.life", "CC:RUN");
TEST_KEY("mailto:luca-pep@run-for-your.life", "CC:RUN");
TEST_KEY("somebodyelse@run-for-your.life", "CC:RUN");
TEST_KEY("lucasaiu-pep@ageinghacker.net", "BB:AGE");
TEST_KEY("saiu-pep@ageinghacker.net", "BB:AGE");
TEST_KEY("pep-saiu@abelson.ageinghacker.net", "BB:AGE-SUBDOMAIN");
TEST_KEY("saiu-pep@sussman.ageinghacker.net", "BB:AGE-SUBDOMAIN");
TEST_KEY("alice@the-world-is-burning.com", "DD:ENJOY");
TEST_KEY("mailto:alice@the-world-is-burning.com", "DD:ENJOY");
TEST_KEY("blice@the-world-is-burning.com", "DD:ENJOY");
TEST_KEY("luca@pep.foundation", "AAAA");
TEST_KEY("luca-pep@run-for-your.life", "CCCC");
TEST_KEY("mailto:luca-pep@run-for-your.life", "CCCC");
TEST_KEY("somebodyelse@run-for-your.life", "CCCC");
TEST_KEY("lucasaiu-pep@ageinghacker.net", "BBBB");
TEST_KEY("saiu-pep@ageinghacker.net", "BBBB");
TEST_KEY("pep-saiu@abelson.ageinghacker.net", "BBCC");
TEST_KEY("saiu-pep@sussman.ageinghacker.net", "BBCC");
TEST_KEY("alice@the-world-is-burning.com", "DDDD");
TEST_KEY("mailto:alice@the-world-is-burning.com", "DDDD");
TEST_KEY("blice@the-world-is-burning.com", "DDDD");
TEST_KEY("luca@aaargh.com", NULL);
TEST_KEY("bob@aaargh.com", NULL);
TEST_KEY("luca-and-bob@aaargh.com", NULL);
@ -178,24 +187,24 @@ TEST_F(MediaKeyTest, check_removal) {
CHECK_LOOKUP_FAILURE("foo@foo.bar");
INSERT("*@foo.bar", "foo");
CHECK_LOOKUP("foo@foo.bar", "FOO");
INSERT("*@foo.bar", "f000");
CHECK_LOOKUP("foo@foo.bar", "F000");
CHECK_LOOKUP_FAILURE("bar@bar.bar");
INSERT("*@bar.bar", "bar");
INSERT("*@quux.bar", "quux");
INSERT("*@foobar.bar", "foobar");
INSERT("*@bar.bar", "1234");
INSERT("*@quux.bar", "0012");
INSERT("*@foobar.bar", "aacd");
CHECK_REMOVE_FAILURE("*@nonexisting.bar");
REMOVE("*@quux.bar");
CHECK_LOOKUP_FAILURE("quux@quux.bar");
CHECK_LOOKUP("foobar@foobar.bar", "FOOBAR");
CHECK_LOOKUP("foobar@foobar.bar", "AACD");
CHECK_LOOKUP_FAILURE("foobar@foooooooooobar.bar");
CHECK_LOOKUP("bar@bar.bar", "BAR");
CHECK_LOOKUP("foobar@foobar.bar", "FOOBAR");
CHECK_LOOKUP("bar@bar.bar", "1234");
CHECK_LOOKUP("foobar@foobar.bar", "AACD");
REMOVE("*@foobar.bar");
CHECK_LOOKUP("bar@bar.bar", "BAR");
CHECK_LOOKUP("bar@bar.bar", "1234");
CHECK_LOOKUP_FAILURE("foobar@fooobar.bar");
REMOVE("*@bar.bar");
@ -205,8 +214,8 @@ TEST_F(MediaKeyTest, check_removal) {
CHECK_REMOVE_FAILURE("*@nonexisting.bar");
CHECK_LOOKUP_FAILURE("bar@fooobar.bar");
INSERT("*@bar.bar", "bar");
CHECK_LOOKUP("bar@bar.bar", "BAR");
INSERT("*@bar.bar", "1234");
CHECK_LOOKUP("bar@bar.bar", "1234");
REMOVE("*@bar.bar");
CHECK_REMOVE_FAILURE("*@bar.bar");
CHECK_LOOKUP_FAILURE("bar@bar.bar");

@ -349,50 +349,66 @@ TEST_F(TrustwordsTest, check_trustwords) {
}
TEST_F(TrustwordsTest, check_trustwords_short_trailing_space) {
// Horrible kludge: these variables are needed for SET_VERSION, which I
// am reusing from another test case above.
int outer_iteration_i = 0;
PEP_STATUS status = PEP_STATUS_OK;
pEp_identity* identity1 = new_identity(
"leon.schumacher@digitalekho.com",
"8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
"23",
"Leon Schumacher");
SET_VERSION(identity1);
pEp_identity* identity2 = new_identity(
"krista@darthmama.org",
"62D4932086185C15917B72D30571AFBCA5493553",
"blargh",
"Krista Bennett");
SET_VERSION(identity2);
char* wordlist = nullptr;
size_t wsize_full = 0;
get_trustwords(session, identity1, identity2, "en", &wordlist, &wsize_full, false);
// These will be for RIPEMD-160, since we explicitly set the protocol version above.
ASSERT_NE(wsize_full, 0);
ASSERT_NOTNULL(wordlist);
ASSERT_NE(wordlist[wsize_full - 1], ' ');
ASSERT_STREQ(wordlist, "CAPTIVITY BULIMIC EXEMPT BETTYE NEWTONIAN");
ASSERT_STREQ(wordlist, "CHUMASH KIDDER GASEOUS CLASSIFY POLARITY");
}
TEST_F(TrustwordsTest, check_trustwords_long_trailing_space) {
// Horrible kludge: these variables are needed for SET_VERSION, which I
// am reusing from another test case above.
int outer_iteration_i = 0;
PEP_STATUS status = PEP_STATUS_OK;
pEp_identity* identity1 = new_identity(
"leon.schumacher@digitalekho.com",
"8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
"23",
"Leon Schumacher");
SET_VERSION(identity1);
pEp_identity* identity2 = new_identity(
"krista@darthmama.org",
"62D4932086185C15917B72D30571AFBCA5493553",
"blargh",
"Krista Bennett");
SET_VERSION(identity2);
char* wordlist = nullptr;
size_t wsize_full = 0;
// These will be for RIPEMD-160, since we explicitly set the protocol version above.
get_trustwords(session, identity1, identity2, "en", &wordlist, &wsize_full, true);
ASSERT_NE(wsize_full, 0);
ASSERT_NOTNULL(wordlist);
ASSERT_NE(wordlist[wsize_full - 1], ' ');
ASSERT_STREQ(wordlist, "CAPTIVITY BULIMIC EXEMPT BETTYE NEWTONIAN DANDELION CLASSICISM BAGGAGEMAN BABBAGE VERSATILITY");
ASSERT_STREQ(wordlist, "CHUMASH KIDDER GASEOUS CLASSIFY POLARITY ENDANGER GENEVRA CUSTARD DECELERATOR BRONNIE");
}

Loading…
Cancel
Save