aboutsummaryrefslogtreecommitdiff
path: root/src/tlsdated-unittest.c
blob: 0e22ee3221d819735105b1059374e4ee9bcd8f68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * tlsdated-unittest.c - tlsdated unit tests
 * Copyright (c) 2012 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "config.h"

#include "src/test_harness.h"
#include "src/tlsdate.h"
#include "src/util.h"

#include <event2/event.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>

FIXTURE (tempdir)
{
  char path[PATH_MAX];
};

FIXTURE_SETUP (tempdir)
{
  char *p;
  strncpy (self->path, "/tmp/tlsdated-unit-XXXXXX", sizeof (self->path));
  p = mkdtemp (self->path);
  ASSERT_NE (NULL, p);
}

FIXTURE_TEARDOWN(tempdir) {
  char buf[256];
  snprintf(buf, sizeof(buf), "%s/load", self->path);
  unlink(buf);
  snprintf(buf, sizeof(buf), "%s/save", self->path);
  unlink(buf);
  ASSERT_EQ(0, rmdir(self->path));
}

int write_time (const char *path, time_t time)
{
  int fd = open (path, O_WRONLY | O_TRUNC | O_CREAT, 0600);
  if (fd == -1)
    return 1;
  if (save_timestamp_to_fd (fd, time))
    return 1;
  if (write (fd, &time, sizeof (time)) != sizeof (time))
    {
      close (fd);
      return 1;
    }
  return close (fd);
}

int read_time (const char *path, time_t* time)
{
  int fd = open (path, O_RDONLY);
  if (fd == -1)
    return 1;
  if (read (fd, time, sizeof (*time)) != sizeof (*time))
    {
      close (fd);
      return 1;
    }
  return close (fd);
}

TEST (sane_time)
{
  ASSERT_EQ (0, is_sane_time (0));
  ASSERT_EQ (0, is_sane_time (INT_MAX));
}

TEST (sane_host_time)
{
  ASSERT_EQ (1, is_sane_time (time (NULL)));
}

TEST_F (tempdir, load_time)
{
  char buf[PATH_MAX];
  time_t tm = 3;
  time_t now = time (NULL);
  snprintf (buf, sizeof (buf), "%s/load", self->path);
  ASSERT_EQ (0, write_time (buf, 0));
  ASSERT_EQ (-1, load_disk_timestamp (buf, &tm));
  ASSERT_EQ (3, tm);
  ASSERT_EQ (0, write_time (buf, INT_MAX));
  ASSERT_EQ (-1, load_disk_timestamp (buf, &tm));
  ASSERT_EQ (3, tm);
  ASSERT_EQ (0, write_time (buf, now));
  ASSERT_EQ (0, truncate (buf, 2));
  ASSERT_EQ (-1, load_disk_timestamp (buf, &tm));
  ASSERT_EQ (3, tm);
  ASSERT_EQ (0, unlink (buf));
  ASSERT_EQ (-1, load_disk_timestamp (buf, &tm));
  ASSERT_EQ (3, tm);
  ASSERT_EQ (0, write_time (buf, now));
  ASSERT_EQ (0, load_disk_timestamp (buf, &tm));
  ASSERT_EQ (now, tm);
}


TEST_F (tempdir, save_time)
{
  char buf[PATH_MAX];
  time_t now = time (NULL);
  time_t tm;
  snprintf (buf, sizeof (buf), "%s/save", self->path);
  ASSERT_EQ (0, write_time (buf, now));
  ASSERT_EQ (0, read_time (buf, &tm));
  EXPECT_EQ (now, tm);
}

FIXTURE (tlsdate)
{
  struct state state;
  struct timeval timeout;
};


FIXTURE_SETUP (tlsdate)
{
  memset (self, 0, sizeof (self));
  /* TODO(wad) make this use the same function tlsdated uses. */
  self->state.base = event_base_new();
  set_conf_defaults (&self->state.opts);
  ASSERT_NE (NULL, self->state.base);
  event_base_priority_init (self->state.base, MAX_EVENT_PRIORITIES);
  ASSERT_EQ (0, setup_sigchld_event (&self->state, 1));
  self->state.events[E_TLSDATE] = event_new (self->state.base, -1, EV_TIMEOUT,
                                  action_run_tlsdate, &self->state);
  ASSERT_NE (NULL, self->state.events[E_TLSDATE]);
  event_priority_set (self->state.events[E_TLSDATE], PRI_NET);
  /* The timeout and fd will be filled in per-call. */
  ASSERT_EQ (0, setup_tlsdate_status (&self->state));
  self->timeout.tv_sec = 1;
}

FIXTURE_TEARDOWN (tlsdate)
{
  int i;
  for (i = 0; i < E_MAX; ++i)
    {
      struct event *e = self->state.events[i];
      if (e)
        {
          int fd = event_get_fd (e);
          if (fd >= 0 && ! (event_get_events (e) & EV_SIGNAL))
            close (fd);
          event_free (e);
          self->state.events[i] = NULL;
        }
    }
  /* The other half was closed above. */
  close (self->state.tlsdate_monitor_fd);
  if (self->state.tlsdate_pid)
    {
      kill (self->state.tlsdate_pid, SIGKILL);
      waitpid (self->state.tlsdate_pid, NULL, WNOHANG);
    }
  if (self->state.base)
    event_base_free (self->state.base);
}

static int
runner (FIXTURE_DATA (tlsdate) *self, time_t *newtime)
{
  if (newtime)
    *newtime = 0;
  trigger_event (&self->state, E_TLSDATE, 0);
  event_base_loopexit (self->state.base, &self->timeout);
  if (event_base_dispatch (self->state.base))
    return -1;
  if (self->state.last_time)
    {
      if (newtime)
        *newtime = self->state.last_time;
      return 0;
    }
  return 1;
}

TEST_F (tlsdate, runner)
{
  struct source source =
  {
    .next = NULL,
    .host = "host1",
    .port = "port1",
    .proxy = "proxy1"
  };
  char *args[] = { "/nonexistent", NULL, NULL };
  extern char **environ;
  self->state.opts.sources = &source;
  self->state.opts.base_argv = args;
  self->state.opts.subprocess_tries = 2;
  self->state.opts.subprocess_wait_between_tries = 1;
  self->state.opts.max_tries = 3;
  self->state.envp = environ;
  EXPECT_EQ (1, runner (self, NULL));
  args[0] = "/bin/false";
  self->state.tries = 0;
  self->state.last_sync_type = SYNC_TYPE_NONE;
  EXPECT_EQ (1, runner (self, NULL));
  args[0] = "src/test/check-host-1";
  self->state.tries = 0;
  self->state.last_sync_type = SYNC_TYPE_NONE;
  EXPECT_EQ (0, runner (self, NULL));
  args[0] = "src/test/sleep-wrap";
  args[1] = "3";
  self->state.tries = 0;
  self->state.last_sync_type = SYNC_TYPE_NONE;
  EXPECT_EQ (0, runner (self, NULL));
}

TEST (jitter)
{
  int i = 0;
  int r;
  const int kBase = 100;
  const int kJitter = 25;
  int nonequal = 0;
  for (i = 0; i < 1000; i++)
    {
      r = add_jitter (kBase, kJitter);
      EXPECT_GE (r, kBase - kJitter);
      EXPECT_LE (r, kBase + kJitter);
      if (r != kBase)
        nonequal++;
    }
  EXPECT_NE (nonequal, 0);
}

TEST_F (tlsdate, rotate_hosts)
{
  struct source s2 =
  {
    .next = NULL,
    .host = "host2",
    .port = "port2",
    .proxy = "proxy2"
  };
  struct source s1 =
  {
    .next = &s2,
    .host = "host1",
    .port = "port1",
    .proxy = "proxy1"
  };
  char *args[] = { "src/test/check-host-1",  NULL };
  extern char **environ;
  self->state.envp = environ;
  self->state.opts.sources = &s1;
  self->state.opts.base_argv = args;
  self->state.opts.subprocess_tries = 2;
  self->state.opts.subprocess_wait_between_tries = 1;
  self->state.opts.max_tries = 5;
  self->timeout.tv_sec = 2;
  EXPECT_EQ (0, runner (self, NULL));
  self->state.tries = 0;
  args[0] = "src/test/check-host-2";
  self->state.last_sync_type = SYNC_TYPE_NONE;
  EXPECT_EQ (0, runner (self, NULL));
  self->state.tries = 0;
  args[0] = "src/test/check-host-1";
  self->state.last_sync_type = SYNC_TYPE_NONE;
  EXPECT_EQ (0, runner (self, NULL));
  self->state.tries = 0;
  args[0] = "src/test/check-host-2";
  self->state.last_sync_type = SYNC_TYPE_NONE;
  EXPECT_EQ (0, runner (self, NULL));
}

TEST_F (tlsdate, proxy_override)
{
  struct source s1 =
  {
    .next = NULL,
    .host = "host",
    .port = "port",
    .proxy = NULL,
  };
  char *args[] = { "src/test/proxy-override", NULL };
  extern char **environ;
  self->state.envp = environ;
  self->state.opts.sources = &s1;
  self->state.opts.base_argv = args;
  self->state.opts.subprocess_tries = 2;
  self->state.opts.subprocess_wait_between_tries = 1;
  EXPECT_EQ (0, runner (self, NULL));
  EXPECT_EQ (RECENT_COMPILE_DATE + 1, self->state.last_time);
  s1.proxy = "socks5://bad.proxy";
  self->state.tries = 0;
  self->state.last_sync_type = SYNC_TYPE_NONE;
  EXPECT_EQ (0, runner (self, NULL));
  EXPECT_EQ (RECENT_COMPILE_DATE + 3, self->state.last_time);
  self->state.opts.proxy = "socks5://good.proxy";
  self->state.tries = 0;
  self->state.last_sync_type = SYNC_TYPE_NONE;
  EXPECT_EQ (0, runner (self, NULL));
  EXPECT_EQ (RECENT_COMPILE_DATE + 2, self->state.last_time);
}

FIXTURE(mock_platform) {
  struct platform platform;
  struct platform *old_platform;
};

FIXTURE_SETUP(mock_platform) {
  self->old_platform = platform;
  self->platform.rtc_open = NULL;
  self->platform.rtc_write = NULL;
  self->platform.rtc_read = NULL;
  self->platform.rtc_close = NULL;
  platform = &self->platform;
}

FIXTURE_TEARDOWN(mock_platform) {
  platform = self->old_platform;
}

/* TODO(wad) add a leap test. */

/*
 * The stuff below this line is ugly. For a lot of these mock functions, we want
 * to smuggle some state (our success) back to the caller, but there's no angle
 * for that, so we're basically stuck with some static variables to store
 * expectations and successes/failures. This could also be done with nested
 * functions, but only gcc supports them.
 */
static const time_t sync_hwclock_expected = 12345678;

static int sync_hwclock_time_get(struct timeval *tv) {
  tv->tv_sec = sync_hwclock_expected;
  tv->tv_usec = 0;
  return 0;
}

static int sync_hwclock_rtc_write(void *handle, const struct timeval *tv) {
  *(int *)handle = tv->tv_sec == sync_hwclock_expected;
  return 0;
}

/* TODO(wad)
TEST_F(mock_platform, sync_hwclock) {
  int ok = 0;
  void *fake_handle = (void *)&ok;
  self->platform.time_get = sync_hwclock_time_get;
  self->platform.rtc_write = sync_hwclock_rtc_write;
  sync_hwclock(fake_handle);
  ASSERT_EQ(ok, 1);
}
*/

static const time_t sync_and_save_expected = 12345678;

static int sync_and_save_time_get(struct timeval *tv) {
  tv->tv_sec = sync_and_save_expected;
  tv->tv_usec = 0;
  return 0;
}

static int sync_and_save_rtc_write(void *handle, const struct timeval *tv) {
  *(int *)handle += tv->tv_sec == sync_and_save_expected;
  return 0;
}

static int sync_and_save_file_write_ok = 0;

static int sync_and_save_file_write(const char *path, void *buf, size_t sz) {
  if (!strcmp(path, timestamp_path))
    sync_and_save_file_write_ok++;
  return 0;
}

/* TODO(wad) TODO ?
TEST_F(mock_platform, sync_and_save) {
  int nosave_ok = 0;
  self->platform.time_get = sync_and_save_time_get;
  self->platform.rtc_write = sync_and_save_rtc_write;
  self->platform.file_write = sync_and_save_file_write;
  sync_and_save(&sync_and_save_file_write_ok, 1);
  ASSERT_EQ(sync_and_save_file_write_ok, 2);
  sync_and_save(&nosave_ok, 0);
  ASSERT_EQ(nosave_ok, 1);
}
*/

TEST_HARNESS_MAIN