Commit 0e9a963b authored by Nick Mathewson's avatar Nick Mathewson 🤹
Browse files

Revise nodefamily.c to match proposal 298

Prop298 says that family entries should be formatted with
$hexids in uppercase, nicknames in lower case, $hexid~names
truncated, and everything sorted lexically.  These changes implement
that ordering for nodefamily.c.

We don't _strictly speaking_ need to nodefamily.c formatting use
this for prop298 microdesc generation, but it seems silly to have
two separate canonicalization algorithms.
parent 3741f9e5
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -135,6 +135,7 @@ nodefamily_from_members(const smartlist_t *members,
      ptr[0] = NODEFAMILY_BY_NICKNAME;
      tor_assert(strlen(cp) < DIGEST_LEN); // guaranteed by is_legal_nickname
      memcpy(ptr+1, cp, strlen(cp));
      tor_strlower((char*) ptr+1);
      bad_element = false;
    } else if (is_legal_hexdigest(cp)) {
      char digest_buf[DIGEST_LEN];
@@ -346,6 +347,7 @@ nodefamily_format(const nodefamily_t *family)
        char buf[HEX_DIGEST_LEN+2];
        buf[0]='$';
        base16_encode(buf+1, sizeof(buf)-1, (char*)ptr+1, DIGEST_LEN);
        tor_strupper(buf);
        smartlist_add_strdup(sl, buf);
        break;
      }
+4 −4
Original line number Diff line number Diff line
@@ -26,12 +26,12 @@ struct nodefamily_t {

#define NODEFAMILY_MEMBER_LEN (1+DIGEST_LEN)

/** Tag byte, indicates that the following bytes are a NUL-padded nickname.
 */
#define NODEFAMILY_BY_NICKNAME 0
/** Tag byte, indicates that the following bytes are a RSA1024 SHA1 ID.
 */
#define NODEFAMILY_BY_RSA_ID 1
#define NODEFAMILY_BY_RSA_ID 0
/** Tag byte, indicates that the following bytes are a NUL-padded nickname.
 */
#define NODEFAMILY_BY_NICKNAME 1

/**
 * Number of bytes to allocate in the array for a nodefamily_t with N members.
+1 −1
Original line number Diff line number Diff line
@@ -176,7 +176,7 @@ test_md_cache(void *data)
  tt_ptr_op(md3->family, OP_NE, NULL);

  encoded_family = nodefamily_format(md3->family);
  tt_str_op(encoded_family, OP_EQ, "nodeX nodeY nodeZ");
  tt_str_op(encoded_family, OP_EQ, "nodex nodey nodez");

  /* Now rebuild the cache! */
  tt_int_op(microdesc_cache_rebuild(mc, 1), OP_EQ, 0);
+9 −9
Original line number Diff line number Diff line
@@ -299,7 +299,7 @@ test_nodelist_nodefamily(void *arg)
  tt_ptr_op(nf1, OP_EQ, nf3);

  /* Do we get the expected result when we re-encode? */
  tor_asprintf(&enc, "hello $%s", h1);
  tor_asprintf(&enc, "$%s hello", h1);
  enc2 = nodefamily_format(nf1);
  tt_str_op(enc2, OP_EQ, enc);
  tor_free(enc2);
@@ -399,8 +399,8 @@ test_nodelist_nodefamily_parse_err(void *arg)
        tt_assert(nf1);
        enc = nodefamily_format(nf1);
        tt_str_op(enc, OP_EQ,
                  "reticulatogranulate "
                  "$7468696E67732D696E2D7468656D73656C766573");
                  "$7468696E67732D696E2D7468656D73656C766573 "
                  "reticulatogranulate");
        tor_free(enc);
      }

@@ -470,11 +470,11 @@ test_nodelist_nodefamily_lookup(void *arg)
  tt_int_op(smartlist_len(sl), OP_EQ, 3);

  const node_t *n = smartlist_get(sl, 0);
  tt_str_op(n->identity, OP_EQ, "erewhon");
  n = smartlist_get(sl, 1);
  test_memeq_hex(n->identity, "3333333333333333333333333333333333333333");
  n = smartlist_get(sl, 2);
  n = smartlist_get(sl, 1);
  test_memeq_hex(n->identity, "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
  n = smartlist_get(sl, 2);
  tt_str_op(n->identity, OP_EQ, "erewhon");

 done:
  UNMOCK(node_get_by_nickname);
@@ -583,9 +583,9 @@ test_nodelist_node_nodefamily(void *arg)
  node_lookup_declared_family(nodes, &mock_node1);
  tt_int_op(smartlist_len(nodes), OP_EQ, 2);
  const node_t *n = smartlist_get(nodes, 0);
  tt_str_op(n->identity, OP_EQ, "NodeFour");
  n = smartlist_get(nodes, 1);
  tt_mem_op(n->identity, OP_EQ, "SecondNodeWe'reTestn", DIGEST_LEN);
  n = smartlist_get(nodes, 1);
  tt_str_op(n->identity, OP_EQ, "nodefour");

  // free, try the other one.
  SMARTLIST_FOREACH(nodes, node_t *, x, tor_free(x));
@@ -594,9 +594,9 @@ test_nodelist_node_nodefamily(void *arg)
  node_lookup_declared_family(nodes, &mock_node2);
  tt_int_op(smartlist_len(nodes), OP_EQ, 2);
  n = smartlist_get(nodes, 0);
  // This gets a truncated hex hex ID since it was looked up by name
  tt_str_op(n->identity, OP_EQ, "NodeThree");
  n = smartlist_get(nodes, 1);
  // This gets a truncated hex hex ID since it was looked up by name
  tt_str_op(n->identity, OP_EQ, "4e6f64654f6e654e6f6");

 done: