Ticket #25511: b25511-004.patch

File b25511-004.patch, 8.5 KB (added by neel, 12 months ago)

Patch (Revision 4)

  • src/common/util.c

    From 30115fe2a7d66741b5440bcdf432ecc71afc9dc4 Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Mon, 16 Apr 2018 20:14:49 -0400
    Subject: [PATCH 1/5] Add format_local_iso_time_nospace()
    
    ---
     src/common/util.c | 9 +++++++++
     src/common/util.h | 1 +
     2 files changed, 10 insertions(+)
    
    diff --git a/src/common/util.c b/src/common/util.c
    index 041e7aee3..b7516b8b6 100644
    a b format_iso_time(char *buf, time_t t) 
    18211821  strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t, &tm));
    18221822}
    18231823
     1824/** As format_local_iso_time, but use the yyyy-mm-ddThh:mm:ss format to avoid
     1825 * embedding an internal space. */
     1826void
     1827format_local_iso_time_nospace(char *buf, time_t t)
     1828{
     1829  format_local_iso_time(buf, t);
     1830  buf[10] = 'T';
     1831}
     1832
    18241833/** As format_iso_time, but use the yyyy-mm-ddThh:mm:ss format to avoid
    18251834 * embedding an internal space. */
    18261835void
  • src/common/util.h

    diff --git a/src/common/util.h b/src/common/util.h
    index ae27e5f01..5708b7fd8 100644
    a b int parse_rfc1123_time(const char *buf, time_t *t); 
    269269#define ISO_TIME_USEC_LEN (ISO_TIME_LEN+7)
    270270void format_local_iso_time(char *buf, time_t t);
    271271void format_iso_time(char *buf, time_t t);
     272void format_local_iso_time_nospace(char *buf, time_t t);
    272273void format_iso_time_nospace(char *buf, time_t t);
    273274void format_iso_time_nospace_usec(char *buf, const struct timeval *tv);
    274275int parse_iso_time_(const char *cp, time_t *t, int strict, int nospace);
  • src/or/control.c

    -- 
    2.17.0
    
    
    From 1c08a8973f8a3d676964a3fbafa732dddf701f0e Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Mon, 16 Apr 2018 20:16:37 -0400
    Subject: [PATCH 2/5] Add GETINFO current-time/{local,utc} command to
     ControlPort
    
    ---
     src/or/control.c | 28 ++++++++++++++++++++++++++++
     src/or/control.h |  4 ++++
     2 files changed, 32 insertions(+)
    
    diff --git a/src/or/control.c b/src/or/control.c
    index 0539ddaca..72122eaaf 100644
    a b getinfo_helper_listeners(control_connection_t *control_conn, 
    19311931  return 0;
    19321932}
    19331933
     1934/** Implementation helper for GETINFO: answers requests for information about
     1935 * the current time in both local and UTF forms. */
     1936STATIC int
     1937getinfo_helper_current_time(control_connection_t *control_conn,
     1938                         const char *question,
     1939                         char **answer, const char **errmsg)
     1940{
     1941  (void)control_conn;
     1942  (void)errmsg;
     1943
     1944  struct timeval now;
     1945  tor_gettimeofday(&now);
     1946  char timebuf[ISO_TIME_LEN+1];
     1947
     1948  if (!strcmp(question, "current-time/local"))
     1949    format_local_iso_time_nospace(timebuf, (time_t)now.tv_sec);
     1950  else if (!strcmp(question, "current-time/utc"))
     1951    format_iso_time_nospace(timebuf, (time_t)now.tv_sec);
     1952  else
     1953    return 0;
     1954
     1955  *answer = tor_strdup(timebuf);
     1956  return 0;
     1957}
     1958
    19341959/** Implementation helper for GETINFO: knows the answers for questions about
    19351960 * directory information. */
    19361961STATIC int
    static const getinfo_item_t getinfo_items[] = { 
    30733098  DOC("config/defaults",
    30743099      "List of default values for configuration options. "
    30753100      "See also config/names"),
     3101  PREFIX("current-time/", current_time, "Current time."),
     3102  DOC("current-time/local", "Current time on the local system."),
     3103  DOC("current-time/utc", "Current UTC time."),
    30763104  PREFIX("downloads/networkstatus/", downloads,
    30773105         "Download statuses for networkstatus objects"),
    30783106  DOC("downloads/networkstatus/ns",
  • src/or/control.h

    diff --git a/src/or/control.h b/src/or/control.h
    index 2fd3c553f..2f312a663 100644
    a b STATIC int getinfo_helper_dir( 
    311311    control_connection_t *control_conn,
    312312    const char *question, char **answer,
    313313    const char **errmsg);
     314STATIC int getinfo_helper_current_time(
     315    control_connection_t *control_conn,
     316    const char *question, char **answer,
     317    const char **errmsg);
    314318
    315319#endif /* defined(CONTROL_PRIVATE) */
    316320
  • src/common/compat_time.c

    -- 
    2.17.0
    
    
    From 6b85fd5c6e406dc0b46ed064c4282ef761de5459 Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Mon, 16 Apr 2018 20:18:40 -0400
    Subject: [PATCH 3/5] Make tor_gettimeofday() mockable
    
    ---
     src/common/compat_time.c | 4 ++--
     src/common/compat_time.h | 2 +-
     2 files changed, 3 insertions(+), 3 deletions(-)
    
    diff --git a/src/common/compat_time.c b/src/common/compat_time.c
    index b940447b6..966216768 100644
    a b tor_sleep_msec(int msec) 
    7171/** Set *timeval to the current time of day.  On error, log and terminate.
    7272 * (Same as gettimeofday(timeval,NULL), but never returns -1.)
    7373 */
    74 void
    75 tor_gettimeofday(struct timeval *timeval)
     74MOCK_IMPL(void,
     75tor_gettimeofday, (struct timeval *timeval))
    7676{
    7777#ifdef _WIN32
    7878  /* Epoch bias copied from perl: number of units between windows epoch and
  • src/common/compat_time.h

    diff --git a/src/common/compat_time.h b/src/common/compat_time.h
    index 75b57f6f2..09dd6add3 100644
    a b void monotime_coarse_add_msec(monotime_coarse_t *out, 
    173173#define monotime_coarse_add_msec monotime_add_msec
    174174#endif /* defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT) */
    175175
    176 void tor_gettimeofday(struct timeval *timeval);
     176MOCK_DECL(void, tor_gettimeofday, (struct timeval *timeval));
    177177
    178178#ifdef TOR_UNIT_TESTS
    179179void tor_sleep_msec(int msec);
  • src/test/test_controller.c

    -- 
    2.17.0
    
    
    From f01713bd028b3976d32eba8e7af16ed488c94b4f Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Mon, 16 Apr 2018 20:35:45 -0400
    Subject: [PATCH 4/5] Add GETINFO current-time/{local,utc} regression test
    
    ---
     src/test/test_controller.c | 56 ++++++++++++++++++++++++++++++++++++++
     1 file changed, 56 insertions(+)
    
    diff --git a/src/test/test_controller.c b/src/test/test_controller.c
    index 1c285bb3a..3b58a78e1 100644
    a b test_download_status_bridge(void *arg) 
    14701470  return;
    14711471}
    14721472
     1473/** Set timeval to a mock date and time. This is neccessary
     1474 * to make tor_gettimeofday() mockable. */
     1475static void
     1476mock_tor_gettimeofday(struct timeval *timeval)
     1477{
     1478  timeval->tv_sec = 1523405073;
     1479  timeval->tv_usec = 271645;
     1480}
     1481
     1482static void
     1483test_current_time(void *arg)
     1484{
     1485  /* We just need one of these to pass, it doesn't matter what's in it */
     1486  control_connection_t dummy;
     1487  /* Get results out */
     1488  char *answer = NULL;
     1489  const char *errmsg = NULL;
     1490
     1491  (void)arg;
     1492
     1493  /* We need these for storing the (mock) time. */
     1494  MOCK(tor_gettimeofday, mock_tor_gettimeofday);
     1495  struct timeval now;
     1496  tor_gettimeofday(&now);
     1497  char timebuf[ISO_TIME_LEN+1];
     1498
     1499  /* Case 1 - local time */
     1500  format_local_iso_time_nospace(timebuf, (time_t)now.tv_sec);
     1501  getinfo_helper_current_time(&dummy,
     1502                              "current-time/local",
     1503                              &answer, &errmsg);
     1504  tt_ptr_op(answer, OP_NE, NULL);
     1505  tt_ptr_op(errmsg, OP_EQ, NULL);
     1506  tt_str_op(answer, OP_EQ, timebuf);
     1507  tor_free(answer);
     1508  errmsg = NULL;
     1509
     1510  /* Case 2 - UTC time */
     1511  format_iso_time_nospace(timebuf, (time_t)now.tv_sec);
     1512  getinfo_helper_current_time(&dummy,
     1513                              "current-time/utc",
     1514                              &answer, &errmsg);
     1515  tt_ptr_op(answer, OP_NE, NULL);
     1516  tt_ptr_op(errmsg, OP_EQ, NULL);
     1517  tt_str_op(answer, OP_EQ, timebuf);
     1518  tor_free(answer);
     1519  errmsg = NULL;
     1520
     1521 done:
     1522  UNMOCK(tor_gettimeofday);
     1523  tor_free(answer);
     1524
     1525  return;
     1526}
     1527
    14731528struct testcase_t controller_tests[] = {
    14741529  { "add_onion_helper_keyarg_v2", test_add_onion_helper_keyarg_v2, 0,
    14751530    NULL, NULL },
    struct testcase_t controller_tests[] = { 
    14861541    NULL },
    14871542  { "download_status_desc", test_download_status_desc, 0, NULL, NULL },
    14881543  { "download_status_bridge", test_download_status_bridge, 0, NULL, NULL },
     1544  { "current_time", test_current_time, 0, NULL, NULL },
    14891545  END_OF_TESTCASES
    14901546};
    14911547
  • new file changes/ticket25511

    -- 
    2.17.0
    
    
    From 0ce113193a2efa92c3bef13e1c6dc248455053ed Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Mon, 16 Apr 2018 20:36:28 -0400
    Subject: [PATCH 5/5] Add changes file for GETINFO current-time/{local,utc}
    
    ---
     changes/ticket25511 | 5 +++++
     1 file changed, 5 insertions(+)
     create mode 100644 changes/ticket25511
    
    diff --git a/changes/ticket25511 b/changes/ticket25511
    new file mode 100644
    index 000000000..0a24e265c
    - +  
     1  o Minor features (control port):
     2    - Introduce GETINFO "current-time/{local,utc}" to return the local
     3      and UTC times respectively in ISO format. This helps a controller
     4      like Tor Browser detect a time-related error. Closes ticket 25511.
     5      Patch by Neel Chauhan.