| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- <?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-2015 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-2015 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('unknown_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
- }
- }
- }
|