-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeSyncTest.php
More file actions
executable file
·344 lines (302 loc) · 9.08 KB
/
TimeSyncTest.php
File metadata and controls
executable file
·344 lines (302 loc) · 9.08 KB
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
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_TimeSync
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
/**
* Zend_timeSync
*/
require_once 'Zend/TimeSync.php';
/**
* @category Zend
* @package Zend_TimeSync
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @group Zend_TimeSync
*/
class Zend_TimeSyncTest extends PHPUnit_Framework_TestCase
{
public $timeservers = array(
// invalid servers
'server_a' => 'ntp://be.foo.bar.org',
'server_b' => 'sntp://be.foo.bar.org',
'server_c' => 'sntp://foo:bar@be.foo.bar.org:123',
// valid servers
'server_d' => 'ntp://be.pool.ntp.org',
'server_e' => 'ntp://time.windows.com',
'server_f' => 'sntp://time-C.timefreq.bldrdoc.gov'
);
/**
* Test for object initialisation
*
* @return void
*/
public function testInitTimeserver()
{
$server = new Zend_TimeSync();
$this->assertTrue($server instanceof Zend_TimeSync);
}
/**
* Test for object initialisation with multiple timeservers
*
* @return void
*/
public function testInitTimeservers()
{
$server = new Zend_TimeSync($this->timeservers);
$result = $server->getServer('server_f');
$this->assertTrue($result instanceof Zend_TimeSync_Protocol);
}
/**
* Test for object initialisation with a single timeserver that will
* default to the default scheme (ntp), because no scheme is supplied
*
* @return void
*/
public function testInitDefaultScheme()
{
$server = new Zend_TimeSync('time.windows.com', 'windows_time');
$server->setServer('windows_time');
$result = $server->getServer();
$this->assertTrue($result instanceof Zend_TimeSync_Ntp);
}
/**
* Test for object initialisation with a single NTP timeserver
*
* @return void
*/
public function testInitNtpScheme()
{
$server = new Zend_TimeSync('ntp://time.windows.com', 'windows_time');
$server->setServer('windows_time');
$result = $server->getServer();
$this->assertTrue($result instanceof Zend_TimeSync_Ntp);
}
/**
* Test for object initialisation with a single SNTP timeserver
*
* @return void
*/
public function testInitSntpScheme()
{
$server = new Zend_TimeSync('sntp://time.zend.com', 'windows_time');
$server->setServer('windows_time');
$result = $server->getServer();
$this->assertTrue($result instanceof Zend_TimeSync_Sntp);
}
/**
* Test for object initialisation with an unsupported scheme. This will
* cause the default scheme to be used (ntp)
*
* @return void
*/
public function testInitUnknownScheme()
{
try {
$server = new Zend_TimeSync('http://time.windows.com', 'windows_time');
$this->fail('Exception expected because we supplied an invalid protocol');
} catch (Zend_TimeSync_Exception $e) {
// success
}
}
/**
* Test setting a single option
*
* @return void
*/
public function testSetOption()
{
$timeout = 5;
$server = new Zend_TimeSync();
$server->setOptions(array('timeout' => $timeout));
$this->assertEquals($timeout, $server->getOptions('timeout'));
}
/**
* Test setting an array of options
*
* @return void
*/
public function testSetOptions()
{
$options = array(
'timeout' => 5,
'foo' => 'bar'
);
$server = new Zend_TimeSync();
$server->setOptions($options);
$this->assertEquals($options['timeout'], $server->getOptions('timeout'));
$this->assertEquals($options['foo'], $server->getOptions('foo'));
}
/**
* Test getting an option that is not set
*
* @return void
*/
public function testGetInvalidOptionKey()
{
$server = new Zend_TimeSync();
try {
$result = $server->getOptions('foobar');
$this->fail('Exception expected because we supplied an invalid option key');
} catch (Zend_TimeSync_Exception $e) {
// success
}
}
/**
* Test marking a none existing timeserver as current
*
* @return void
*/
public function testSetUnknownCurrent()
{
$server = new Zend_TimeSync();
try {
$server->setServer('unkown_alias');
$this->fail('Exception expected because there is no timeserver which we can mark as current');
} catch (Zend_TimeSync_Exception $e) {
// success
}
}
/**
* Test getting the current timeserver when none is set
*
* @return void
*/
public function testGetUnknownCurrent()
{
$server = new Zend_TimeSync();
try {
$result = $server->getServer();
$this->fail('Exception expected because there is no current timeserver set');
} catch (Zend_TimeSync_Exception $e) {
// success
}
}
/**
* Test getting a none existing timeserver
*
* @return void
*/
public function testGetUnknownServer()
{
$server = new Zend_TimeSync();
try {
$result = $server->getServer('none_existing_server_alias');
$this->fail('Exception expected, because the requested timeserver does not exist');
} catch (Zend_TimeSync_Exception $e) {
// success
}
}
/**
* Test getting a date using the fallback mechanism, will try to
* return the date from the first server that returns a valid result
*
* @return void
*/
public function testGetDate()
{
$server = new Zend_TimeSync($this->timeservers);
try {
$result = $server->getDate();
$this->assertTrue($result instanceof Zend_Date);
} catch (Zend_TimeSync_Exception $e) {
$this->assertContains('all timeservers are bogus', $e->getMessage());
}
}
/**
* Test getting a date from an ntp timeserver
*
* @return void
*/
public function testGetNtpDate()
{
$server = new Zend_TimeSync('ntp://time.windows.com', 'time_windows');
try {
$result = $server->getDate();
$this->assertTrue($result instanceof Zend_Date);
} catch (Zend_TimeSync_Exception $e) {
$this->assertContains('all timeservers are bogus', $e->getMessage());
}
}
/**
* Test getting a date from an sntp timeserver
*
* @return void
*/
public function testGetSntpDate()
{
$server = new Zend_TimeSync('sntp://time-C.timefreq.bldrdoc.gov');
try {
$result = $server->getDate();
$this->assertTrue($result instanceof Zend_Date);
} catch (Zend_TimeSync_Exception $e) {
$this->assertContains('all timeservers are bogus', $e->getMessage());
}
}
/**
* Test getting a date from an invalid timeserver
*
* @return void
*/
public function testGetInvalidDate()
{
$servers = array(
'server_a' => 'dummy-ntp-timeserver.com',
'server_b' => 'another-dummy-ntp-timeserver.com'
);
$server = new Zend_TimeSync($servers);
try {
$result = $server->getDate();
} catch (Zend_TimeSync_Exception $e) {
$exceptions = $e->get();
foreach($exceptions as $key => $exception) {
$this->assertTrue($exception instanceof Zend_TimeSync_Exception);
}
}
}
/**
* Test walking through the server list
*
* @return void
*/
public function testWalkServers()
{
$servers = new Zend_TimeSync($this->timeservers);
foreach ($servers as $key => $server) {
$this->assertTrue($server instanceof Zend_TimeSync_Protocol);
}
}
/**
* Test getting info returned from the server
*
* @return void
*/
public function testGetInfo()
{
$server = new Zend_TimeSync('time.windows.com');
try {
$date = $server->getDate();
$result = $server->getInfo();
$this->assertTrue(count($result) > 0);
} catch (Zend_TimeSync_Exception $e) {
// nothing
}
}
}