blob: 6eb49f520d5b93f0f16759f1d040247747947238 [file] [log] [blame]
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -04001/*
2 * tlsdated-unittest.c - tlsdated unit tests
3 * Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Akerb12abad2012-10-16 01:25:00 -04008#include "config.h"
9
10#include "src/test_harness.h"
11#include "src/tlsdate.h"
12
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040013#include <fcntl.h>
14#include <limits.h>
15#include <stdlib.h>
16#include <unistd.h>
17
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040018FIXTURE(tempdir) {
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010019 char path[PATH_MAX];
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040020};
21
22FIXTURE_SETUP(tempdir) {
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010023 char *p;
24 strncpy(self->path, "/tmp/tlsdated-unit-XXXXXX", sizeof(self->path));
25 p = mkdtemp(self->path);
26 ASSERT_NE(NULL, p);
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040027}
28
29int rmrf(char *dir) {
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010030 char buf[256];
31 snprintf(buf, sizeof(buf), "rm -rf %s", dir);
32 return system(buf);
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040033}
34
35FIXTURE_TEARDOWN(tempdir) {
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010036 ASSERT_EQ(0, rmrf(self->path));
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040037}
38
39int write_time(const char *path, time_t time) {
Javanteab92b8d72013-01-12 01:39:42 -080040 int fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0600);
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010041 if (fd == -1)
42 return 1;
43 if (write(fd, &time, sizeof(time)) != sizeof(time)) {
44 close(fd);
45 return 1;
46 }
47 return close(fd);
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040048}
49
50int read_time(const char *path, time_t* time) {
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010051 int fd = open(path, O_RDONLY);
52 if (fd == -1)
53 return 1;
54 if (read(fd, time, sizeof(*time)) != sizeof(*time)) {
55 close(fd);
56 return 1;
57 }
58 return close(fd);
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040059}
60
61TEST(sane_time) {
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010062 ASSERT_EQ(0, is_sane_time(0));
63 ASSERT_EQ(0, is_sane_time(INT_MAX));
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040064}
65
66TEST(sane_host_time) {
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010067 ASSERT_EQ(1, is_sane_time(time(NULL)));
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040068}
69
70TEST_F(tempdir, load_time) {
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010071 char buf[PATH_MAX];
72 time_t tm = 3;
73 time_t now = time(NULL);
74 snprintf(buf, sizeof(buf), "%s/load", self->path);
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040075
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010076 ASSERT_EQ(0, write_time(buf, 0));
77 ASSERT_EQ(-1, load_disk_timestamp(buf, &tm));
78 ASSERT_EQ(3, tm);
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040079
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010080 ASSERT_EQ(0, write_time(buf, INT_MAX));
81 ASSERT_EQ(-1, load_disk_timestamp(buf, &tm));
82 ASSERT_EQ(3, tm);
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040083
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010084 ASSERT_EQ(0, write_time(buf, now));
85 ASSERT_EQ(0, truncate(buf, 2));
86 ASSERT_EQ(-1, load_disk_timestamp(buf, &tm));
87 ASSERT_EQ(3, tm);
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040088
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010089 ASSERT_EQ(0, unlink(buf));
90 ASSERT_EQ(-1, load_disk_timestamp(buf, &tm));
91 ASSERT_EQ(3, tm);
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040092
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010093 ASSERT_EQ(0, write_time(buf, now));
94 ASSERT_EQ(0, load_disk_timestamp(buf, &tm));
95 ASSERT_EQ(now, tm);
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -040096}
97
98TEST_F(tempdir, save_time) {
Jacob Appelbaum8d751a02012-10-30 16:39:58 +010099 char buf[PATH_MAX];
100 time_t now = time(NULL);
101 time_t tm;
102 snprintf(buf, sizeof(buf), "%s/save", self->path);
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -0400103
Jacob Appelbaum8d751a02012-10-30 16:39:58 +0100104 save_disk_timestamp(buf, now);
105 ASSERT_EQ(0, read_time(buf, &tm));
106 EXPECT_EQ(now, tm);
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -0400107}
108
109TEST(tlsdate_tests) {
elly0e35d052013-01-16 17:34:12 -0500110 struct source source = {
111 .next = NULL,
112 .host = "<host>",
113 .port = "<port>",
114 .proxy = "<proxy>"
115 };
Jacob Appelbaum8d751a02012-10-30 16:39:58 +0100116 char *args[] = { "/nonexistent", NULL, NULL };
elly0e35d052013-01-16 17:34:12 -0500117 struct opts opts;
118 memset(&opts, 0, sizeof(opts));
119 opts.sources = &source;
120 opts.base_argv = args;
121 opts.subprocess_tries = 2;
122 opts.subprocess_wait_between_tries = 1;
Jacob Appelbaum8d751a02012-10-30 16:39:58 +0100123 extern char **environ;
elly0e35d052013-01-16 17:34:12 -0500124 EXPECT_EQ(1, tlsdate(&opts, environ));
Jacob Appelbaum8d751a02012-10-30 16:39:58 +0100125 args[0] = "/bin/false";
elly0e35d052013-01-16 17:34:12 -0500126 EXPECT_EQ(1, tlsdate(&opts, environ));
Jacob Appelbaum8d751a02012-10-30 16:39:58 +0100127 args[0] = "/bin/true";
elly0e35d052013-01-16 17:34:12 -0500128 EXPECT_EQ(0, tlsdate(&opts, environ));
129 args[0] = "src/test/sleep-wrap.sh";
Jacob Appelbaum8d751a02012-10-30 16:39:58 +0100130 args[1] = "3";
elly0e35d052013-01-16 17:34:12 -0500131 EXPECT_EQ(-1, tlsdate(&opts, environ));
132 opts.subprocess_wait_between_tries = 5;
133 EXPECT_EQ(0, tlsdate(&opts, environ));
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -0400134}
135
ellyccd12452013-01-11 14:44:17 -0500136TEST(jitter) {
137 int i = 0;
138 int r;
139 const int kBase = 100;
140 const int kJitter = 25;
141 int nonequal = 0;
142 for (i = 0; i < 1000; i++) {
143 r = add_jitter(kBase, kJitter);
144 EXPECT_GE(r, kBase - kJitter);
145 EXPECT_LE(r, kBase + kJitter);
146 if (r != kBase)
147 nonequal++;
148 }
149 EXPECT_NE(nonequal, 0);
150}
151
elly0e35d052013-01-16 17:34:12 -0500152TEST(rotate_hosts) {
153 struct source s2 = {
154 .next = NULL,
155 .host = "host2",
156 .port = "port2",
157 .proxy = "proxy2"
158 };
159 struct source s1 = {
160 .next = &s2,
161 .host = "host1",
162 .port = "port1",
163 .proxy = "proxy1"
164 };
165 struct opts opts;
166 char *args[] = { "src/test/rotate.sh", NULL };
167 memset(&opts, 0, sizeof(opts));
168 opts.sources = &s1;
169 opts.base_argv = args;
170 opts.subprocess_tries = 2;
171 opts.subprocess_wait_between_tries = 1;
172 extern char **environ;
173 EXPECT_EQ(1, tlsdate(&opts, environ));
174 EXPECT_EQ(2, tlsdate(&opts, environ));
175 EXPECT_EQ(1, tlsdate(&opts, environ));
176 EXPECT_EQ(2, tlsdate(&opts, environ));
177}
178
Elly Fong-Jones6fb0d4b2012-10-06 14:10:37 -0400179TEST_HARNESS_MAIN