Ticket #25511: b25511-003.patch

File b25511-003.patch, 8.8 KB (added by neel, 13 months ago)

Patch (Revision 3)

  • src/common/util.c

    From 72b80411ac0e894e79e59dfb28732e7610a1cb82 Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Tue, 10 Apr 2018 19:45:40 -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 065b3245f..4695d6fbb 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 06691eb3e364a1b70bc31e3207c34e4e7270628a Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Tue, 10 Apr 2018 19:53:49 -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 5a2fae64e..7a47a04ce 100644
    a b getinfo_helper_listeners(control_connection_t *control_conn, 
    19321932  return 0;
    19331933}
    19341934
     1935/** Implementation helper for GETINFO: answers requests for information about
     1936 * the current time in both local and UTF forms. */
     1937STATIC int
     1938getinfo_helper_current_time(control_connection_t *control_conn,
     1939                         const char *question,
     1940                         char **answer, const char **errmsg)
     1941{
     1942  (void)control_conn;
     1943  (void)errmsg;
     1944
     1945  struct timeval now;
     1946  tor_gettimeofday(&now);
     1947  char timebuf[ISO_TIME_LEN+1];
     1948
     1949  if (!strcmp(question, "current-time/local"))
     1950    format_local_iso_time_nospace(timebuf, (time_t)now.tv_sec);
     1951  else if (!strcmp(question, "current-time/utc"))
     1952    format_iso_time_nospace(timebuf, (time_t)now.tv_sec);
     1953  else
     1954    return 0;
     1955
     1956  *answer = tor_strdup(timebuf);
     1957  return 0;
     1958}
     1959
    19351960/** Implementation helper for GETINFO: knows the answers for questions about
    19361961 * directory information. */
    19371962STATIC int
    static const getinfo_item_t getinfo_items[] = { 
    30743099  DOC("config/defaults",
    30753100      "List of default values for configuration options. "
    30763101      "See also config/names"),
     3102  PREFIX("current-time/", current_time, "Current time."),
     3103  DOC("current-time/local", "Current time on the local system."),
     3104  DOC("current-time/utc", "Current UTC time."),
    30773105  PREFIX("downloads/networkstatus/", downloads,
    30783106         "Download statuses for networkstatus objects"),
    30793107  DOC("downloads/networkstatus/ns",
  • src/or/control.h

    diff --git a/src/or/control.h b/src/or/control.h
    index 28ffeaed8..62c53650a 100644
    a b STATIC int getinfo_helper_dir( 
    314314    control_connection_t *control_conn,
    315315    const char *question, char **answer,
    316316    const char **errmsg);
     317STATIC int getinfo_helper_current_time(
     318    control_connection_t *control_conn,
     319    const char *question, char **answer,
     320    const char **errmsg);
    317321
    318322#endif /* defined(CONTROL_PRIVATE) */
    319323
  • src/common/compat_time.c

    -- 
    2.17.0
    
    
    From bd77d9f09723d49bbd28963856b3e6c83260ab56 Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Tue, 10 Apr 2018 20:53:52 -0400
    Subject: [PATCH 3/5] Add mock_tor_gettimeofday()
    
    ---
     src/common/compat_time.c | 13 +++++++++++--
     src/common/compat_time.h |  3 ++-
     2 files changed, 13 insertions(+), 3 deletions(-)
    
    diff --git a/src/common/compat_time.c b/src/common/compat_time.c
    index 183a60a48..e88252220 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
    tor_gettimeofday(struct timeval *timeval) 
    116116  return;
    117117}
    118118
     119/** Set timeval to a mock date and time. This is neccessary to make
     120 * tor_gettimeofday() mockable. */
     121MOCK_IMPL(void,
     122mock_tor_gettimeofday, (struct timeval *timeval))
     123{
     124  timeval->tv_sec = 1523405073;
     125  timeval->tv_usec = 271645;
     126}
     127
    119128#define ONE_MILLION ((int64_t) (1000 * 1000))
    120129#define ONE_BILLION ((int64_t) (1000 * 1000 * 1000))
    121130
  • src/common/compat_time.h

    diff --git a/src/common/compat_time.h b/src/common/compat_time.h
    index 6ddd11883..56d4a86ca 100644
    a b void monotime_coarse_add_msec(monotime_coarse_t *out, 
    172172#define monotime_coarse_add_msec monotime_add_msec
    173173#endif /* defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT) */
    174174
    175 void tor_gettimeofday(struct timeval *timeval);
     175MOCK_DECL(void, tor_gettimeofday, (struct timeval *timeval));
     176MOCK_DECL(void, mock_tor_gettimeofday, (struct timeval *timeval));
    176177
    177178#ifdef TOR_UNIT_TESTS
    178179void tor_sleep_msec(int msec);
  • src/test/test_controller.c

    -- 
    2.17.0
    
    
    From cc922ffc082ea53b06fa37f8af1137805ea36979 Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Tue, 10 Apr 2018 20:54:57 -0400
    Subject: [PATCH 4/5] Add GETINFO current-time/{local,utc} regression test
    
    ---
     src/test/test_controller.c | 47 ++++++++++++++++++++++++++++++++++++++
     1 file changed, 47 insertions(+)
    
    diff --git a/src/test/test_controller.c b/src/test/test_controller.c
    index 1c285bb3a..d6921d588 100644
    a b test_download_status_bridge(void *arg) 
    14701470  return;
    14711471}
    14721472
     1473static void
     1474test_current_time(void *arg)
     1475{
     1476  /* We just need one of these to pass, it doesn't matter what's in it */
     1477  control_connection_t dummy;
     1478  /* Get results out */
     1479  char *answer = NULL;
     1480  const char *errmsg = NULL;
     1481
     1482  (void)arg;
     1483
     1484  /* We need these for storing the (mock) time. */
     1485  MOCK(tor_gettimeofday, mock_tor_gettimeofday);
     1486  struct timeval now;
     1487  tor_gettimeofday(&now);
     1488  char timebuf[ISO_TIME_LEN+1];
     1489
     1490  /* Case 1 - local time */
     1491  format_local_iso_time_nospace(timebuf, (time_t)now.tv_sec);
     1492  getinfo_helper_current_time(&dummy,
     1493                              "current-time/local",
     1494                              &answer, &errmsg);
     1495  tt_ptr_op(answer, OP_NE, NULL);
     1496  tt_ptr_op(errmsg, OP_EQ, NULL);
     1497  tt_str_op(answer, OP_EQ, timebuf);
     1498  tor_free(answer);
     1499  errmsg = NULL;
     1500
     1501  /* Case 2 - UTC time */
     1502  format_iso_time_nospace(timebuf, (time_t)now.tv_sec);
     1503  getinfo_helper_current_time(&dummy,
     1504                              "current-time/utc",
     1505                              &answer, &errmsg);
     1506  tt_ptr_op(answer, OP_NE, NULL);
     1507  tt_ptr_op(errmsg, OP_EQ, NULL);
     1508  tt_str_op(answer, OP_EQ, timebuf);
     1509  tor_free(answer);
     1510  errmsg = NULL;
     1511
     1512 done:
     1513  UNMOCK(tor_gettimeofday);
     1514  tor_free(answer);
     1515
     1516  return;
     1517}
     1518
    14731519struct testcase_t controller_tests[] = {
    14741520  { "add_onion_helper_keyarg_v2", test_add_onion_helper_keyarg_v2, 0,
    14751521    NULL, NULL },
    struct testcase_t controller_tests[] = { 
    14861532    NULL },
    14871533  { "download_status_desc", test_download_status_desc, 0, NULL, NULL },
    14881534  { "download_status_bridge", test_download_status_bridge, 0, NULL, NULL },
     1535  { "current_time", test_current_time, 0, NULL, NULL },
    14891536  END_OF_TESTCASES
    14901537};
    14911538
  • new file changes/ticket25511

    -- 
    2.17.0
    
    
    From 6244dd779aaaa0e8c1844aed30a2a27253de8010 Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Tue, 10 Apr 2018 20:56:24 -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.