File: timers.c

package info (click to toggle)
proftpd-dfsg 1.3.3a-6squeeze7
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,868 kB
  • ctags: 10,217
  • sloc: ansic: 111,549; perl: 94,506; sh: 17,265; makefile: 1,986
file content (247 lines) | stat: -rw-r--r-- 6,736 bytes parent folder | download
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
/*
 * ProFTPD - FTP server testsuite
 * Copyright (c) 2008 The ProFTPD Project team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * As a special exemption, The ProFTPD Project team and other respective
 * copyright holders give permission to link this program with OpenSSL, and
 * distribute the resulting executable, without including the source code for
 * OpenSSL in the source distribution.
 */

/*
 * Timers API tests
 * $Id: timers.c,v 1.1 2008/10/06 18:16:50 castaglia Exp $
 */

#include "tests.h"

static pool *p = NULL;

static int repeat_cb = FALSE;
static unsigned int timer_triggered_count = 0;

/* Fixtures */

static void set_up(void) {
  if (p == NULL) {
    p = make_sub_pool(NULL);
  }
  permanent_pool = p;

  repeat_cb = FALSE;
  timer_triggered_count = 0;
}

static void tear_down(void) {
  if (p) {
    destroy_pool(p);
    p = permanent_pool = NULL;
  } 
}

/* Helper functions */

static void timers_handle_signals(void) {
  if (recvd_signal_flags) {
    recvd_signal_flags &= ~RECEIVED_SIG_ALRM;
    handle_alarm();
  }
}

static int timers_test_cb(CALLBACK_FRAME) {
  timer_triggered_count++;

  if (repeat_cb)
    return 1;

  return 0;
}

/* Tests */

START_TEST (timer_add_test) {
  int res;
  unsigned int ok = 0;

  res = pr_timer_add(-1, 0, NULL, NULL, NULL);
  fail_unless(res == -1, "Failed to handle negative seconds");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL");

  res = pr_timer_add(0, 0, NULL, NULL, NULL);
  fail_unless(res == -1, "Failed to handle null description");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL");

  res = pr_timer_add(0, 0, NULL, NULL, "test");
  fail_unless(res == -1, "Failed to handle zero count");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL");

  res = pr_timer_add(1, 0, NULL, NULL, "test");
  fail_unless(res == -1, "Failed to handle null callback");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL");

  res = pr_timer_add(1, 0, NULL, timers_test_cb, "test");
  fail_unless(res == 0, "Failed to allocate timer: %s", strerror(errno));

  res = pr_timer_add(1, 0, NULL, timers_test_cb, "test");
  fail_unless(res == -1, "Failed to handle duplicate timer: %s",
    strerror(errno));
  fail_unless(errno == EPERM, "Failed to set errno to EPERM");

  sleep(2);
  timers_handle_signals();

  ok = 1;
  fail_unless(timer_triggered_count == ok,
    "Timer failed to fire (expected count %u, got %u)", ok,
    timer_triggered_count);

  repeat_cb = TRUE;

  /* Make sure that pr_timer_add() returns an incrementing timer ID,
   * starting with 1024, if the input timer ID is -1.
   */
  res = pr_timer_add(1, -1, NULL, timers_test_cb, "test");
  fail_unless(res == 1024, "Failed to allocate timer: %s", strerror(errno));

  sleep(1);
  timers_handle_signals();

  ok = 2;
  fail_unless(timer_triggered_count == ok,
    "Timer failed to fire (expected count %u, got %u)", ok,
    timer_triggered_count);

  sleep(1);
  timers_handle_signals();

  ok = 3;
  fail_unless(timer_triggered_count == ok,
    "Timer failed to fire (expected count %u, got %u)", ok,
    timer_triggered_count);
}
END_TEST

START_TEST (timer_remove_test) {
  int res;

  res = pr_timer_remove(0, NULL);
  fail_unless(res == 0);

  res = pr_timer_add(1, 0, NULL, timers_test_cb, "test");
  fail_unless(res == 0, "Failed to add timer: %s", strerror(errno));

  res = pr_timer_remove(1, NULL);
  fail_unless(res == -1, "Failed to return -1 for non-matching timer ID");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT");

  res = pr_timer_remove(0, NULL);
  fail_unless(res == 0, "Failed to remove timer: %s", strerror(errno));

  fail_unless(timer_triggered_count == 0,
    "Expected trigger count of 0, got %u", timer_triggered_count);
}
END_TEST

START_TEST (timer_reset_test) {
  int res;
  unsigned int ok = 0;

  mark_point();
  res = pr_timer_reset(0, NULL);
  fail_unless(res == -1, "Failed to handle empty timer list");
  fail_unless(errno == EPERM, "Failed to set errno to EPERM");

  mark_point();
  res = pr_timer_add(2, 1, NULL, timers_test_cb, "test");
  fail_unless(res == 1, "Failed to add timer: %s", strerror(errno));

  mark_point();
  res = pr_timer_reset(2, NULL);
  fail_unless(res == 0, "Expected timer ID 1, got %d", res);

  sleep(1);
  timers_handle_signals();

  mark_point();
  fail_unless(timer_triggered_count == ok,
    "Timer fired unexpectedly (expected count %u, got %u)", ok,
    timer_triggered_count);

  mark_point();
  res = pr_timer_reset(1, NULL);
  fail_unless(res == 1, "Failed to reset timer");

  sleep(1);
  timers_handle_signals();

  fail_unless(timer_triggered_count == ok,
    "Timer fired unexpectedly (expected count %u, got %u)", ok,
    timer_triggered_count);

  sleep(1);
  timers_handle_signals();

  ok = 1;
  fail_unless(timer_triggered_count == ok,
    "Timer failed to fire (expected count %u, got %u)", ok,
    timer_triggered_count);
}
END_TEST

START_TEST (timer_sleep_test) {
  int res;

  res = pr_timer_sleep(0);
  fail_unless(res == -1, "Failed to handle sleep len of zero");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL");

  pr_alarms_block();
  res = pr_timer_sleep(1);
  fail_unless(res == -1, "Failed to handle blocked alarms when sleeping");
  fail_unless(errno == EPERM, "Failed to set errno to EPERM");

  pr_alarms_unblock();

  res = pr_timer_sleep(1);
  fail_unless(res == 0, "Failed to sleep: %s", strerror(errno));
}
END_TEST

Suite *tests_get_timers_suite(void) {
  Suite *suite;
  TCase *testcase;

  suite = suite_create("timers");

  testcase = tcase_create("base");

  tcase_add_checked_fixture(testcase, set_up, tear_down);

  tcase_add_test(testcase, timer_add_test);
  tcase_add_test(testcase, timer_remove_test);
  tcase_add_test(testcase, timer_reset_test);
  tcase_add_test(testcase, timer_sleep_test);

  /* Allow a longer timeout on these tests, as they will need a second or
   * two to actually run through the test itself, plus overhead.
   */
  tcase_set_timeout(testcase, 5);

  suite_add_tcase(suite, testcase);

  return suite;
}