TimeSyncTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_TimeSync
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_timeSync
  24. */
  25. require_once 'Zend/TimeSync.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_TimeSync
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_TimeSync
  33. */
  34. class Zend_TimeSyncTest extends PHPUnit_Framework_TestCase
  35. {
  36. public $timeservers = array(
  37. // invalid servers
  38. 'server_a' => 'ntp://be.foo.bar.org',
  39. 'server_b' => 'sntp://be.foo.bar.org',
  40. 'server_c' => 'sntp://foo:bar@be.foo.bar.org:123',
  41. // valid servers
  42. 'server_d' => 'ntp://be.pool.ntp.org',
  43. 'server_e' => 'ntp://time.windows.com',
  44. 'server_f' => 'sntp://time-C.timefreq.bldrdoc.gov'
  45. );
  46. /**
  47. * Test for object initialisation
  48. *
  49. * @return void
  50. */
  51. public function testInitTimeserver()
  52. {
  53. $server = new Zend_TimeSync();
  54. $this->assertTrue($server instanceof Zend_TimeSync);
  55. }
  56. /**
  57. * Test for object initialisation with multiple timeservers
  58. *
  59. * @return void
  60. */
  61. public function testInitTimeservers()
  62. {
  63. $server = new Zend_TimeSync($this->timeservers);
  64. $result = $server->getServer('server_f');
  65. $this->assertTrue($result instanceof Zend_TimeSync_Protocol);
  66. }
  67. /**
  68. * Test for object initialisation with a single timeserver that will
  69. * default to the default scheme (ntp), because no scheme is supplied
  70. *
  71. * @return void
  72. */
  73. public function testInitDefaultScheme()
  74. {
  75. $server = new Zend_TimeSync('time.windows.com', 'windows_time');
  76. $server->setServer('windows_time');
  77. $result = $server->getServer();
  78. $this->assertTrue($result instanceof Zend_TimeSync_Ntp);
  79. }
  80. /**
  81. * Test for object initialisation with a single NTP timeserver
  82. *
  83. * @return void
  84. */
  85. public function testInitNtpScheme()
  86. {
  87. $server = new Zend_TimeSync('ntp://time.windows.com', 'windows_time');
  88. $server->setServer('windows_time');
  89. $result = $server->getServer();
  90. $this->assertTrue($result instanceof Zend_TimeSync_Ntp);
  91. }
  92. /**
  93. * Test for object initialisation with a single SNTP timeserver
  94. *
  95. * @return void
  96. */
  97. public function testInitSntpScheme()
  98. {
  99. $server = new Zend_TimeSync('sntp://time.zend.com', 'windows_time');
  100. $server->setServer('windows_time');
  101. $result = $server->getServer();
  102. $this->assertTrue($result instanceof Zend_TimeSync_Sntp);
  103. }
  104. /**
  105. * Test for object initialisation with an unsupported scheme. This will
  106. * cause the default scheme to be used (ntp)
  107. *
  108. * @return void
  109. */
  110. public function testInitUnknownScheme()
  111. {
  112. try {
  113. $server = new Zend_TimeSync('http://time.windows.com', 'windows_time');
  114. $this->fail('Exception expected because we supplied an invalid protocol');
  115. } catch (Zend_TimeSync_Exception $e) {
  116. // success
  117. }
  118. }
  119. /**
  120. * Test setting a single option
  121. *
  122. * @return void
  123. */
  124. public function testSetOption()
  125. {
  126. $timeout = 5;
  127. $server = new Zend_TimeSync();
  128. $server->setOptions(array('timeout' => $timeout));
  129. $this->assertEquals($timeout, $server->getOptions('timeout'));
  130. }
  131. /**
  132. * Test setting an array of options
  133. *
  134. * @return void
  135. */
  136. public function testSetOptions()
  137. {
  138. $options = array(
  139. 'timeout' => 5,
  140. 'foo' => 'bar'
  141. );
  142. $server = new Zend_TimeSync();
  143. $server->setOptions($options);
  144. $this->assertEquals($options['timeout'], $server->getOptions('timeout'));
  145. $this->assertEquals($options['foo'], $server->getOptions('foo'));
  146. }
  147. /**
  148. * Test getting an option that is not set
  149. *
  150. * @return void
  151. */
  152. public function testGetInvalidOptionKey()
  153. {
  154. $server = new Zend_TimeSync();
  155. try {
  156. $result = $server->getOptions('foobar');
  157. $this->fail('Exception expected because we supplied an invalid option key');
  158. } catch (Zend_TimeSync_Exception $e) {
  159. // success
  160. }
  161. }
  162. /**
  163. * Test marking a none existing timeserver as current
  164. *
  165. * @return void
  166. */
  167. public function testSetUnknownCurrent()
  168. {
  169. $server = new Zend_TimeSync();
  170. try {
  171. $server->setServer('unknown_alias');
  172. $this->fail('Exception expected because there is no timeserver which we can mark as current');
  173. } catch (Zend_TimeSync_Exception $e) {
  174. // success
  175. }
  176. }
  177. /**
  178. * Test getting the current timeserver when none is set
  179. *
  180. * @return void
  181. */
  182. public function testGetUnknownCurrent()
  183. {
  184. $server = new Zend_TimeSync();
  185. try {
  186. $result = $server->getServer();
  187. $this->fail('Exception expected because there is no current timeserver set');
  188. } catch (Zend_TimeSync_Exception $e) {
  189. // success
  190. }
  191. }
  192. /**
  193. * Test getting a none existing timeserver
  194. *
  195. * @return void
  196. */
  197. public function testGetUnknownServer()
  198. {
  199. $server = new Zend_TimeSync();
  200. try {
  201. $result = $server->getServer('none_existing_server_alias');
  202. $this->fail('Exception expected, because the requested timeserver does not exist');
  203. } catch (Zend_TimeSync_Exception $e) {
  204. // success
  205. }
  206. }
  207. /**
  208. * Test getting a date using the fallback mechanism, will try to
  209. * return the date from the first server that returns a valid result
  210. *
  211. * @return void
  212. */
  213. public function testGetDate()
  214. {
  215. $server = new Zend_TimeSync($this->timeservers);
  216. try {
  217. $result = $server->getDate();
  218. $this->assertTrue($result instanceof Zend_Date);
  219. } catch (Zend_TimeSync_Exception $e) {
  220. $this->assertContains('all timeservers are bogus', $e->getMessage());
  221. }
  222. }
  223. /**
  224. * Test getting a date from an ntp timeserver
  225. *
  226. * @return void
  227. */
  228. public function testGetNtpDate()
  229. {
  230. $server = new Zend_TimeSync('ntp://time.windows.com', 'time_windows');
  231. try {
  232. $result = $server->getDate();
  233. $this->assertTrue($result instanceof Zend_Date);
  234. } catch (Zend_TimeSync_Exception $e) {
  235. $this->assertContains('all timeservers are bogus', $e->getMessage());
  236. }
  237. }
  238. /**
  239. * Test getting a date from an sntp timeserver
  240. *
  241. * @return void
  242. */
  243. public function testGetSntpDate()
  244. {
  245. $server = new Zend_TimeSync('sntp://time-C.timefreq.bldrdoc.gov');
  246. try {
  247. $result = $server->getDate();
  248. $this->assertTrue($result instanceof Zend_Date);
  249. } catch (Zend_TimeSync_Exception $e) {
  250. $this->assertContains('all timeservers are bogus', $e->getMessage());
  251. }
  252. }
  253. /**
  254. * Test getting a date from an invalid timeserver
  255. *
  256. * @return void
  257. */
  258. public function testGetInvalidDate()
  259. {
  260. $servers = array(
  261. 'server_a' => 'dummy-ntp-timeserver.com',
  262. 'server_b' => 'another-dummy-ntp-timeserver.com'
  263. );
  264. $server = new Zend_TimeSync($servers);
  265. try {
  266. $result = $server->getDate();
  267. } catch (Zend_TimeSync_Exception $e) {
  268. $exceptions = $e->get();
  269. foreach($exceptions as $key => $exception) {
  270. $this->assertTrue($exception instanceof Zend_TimeSync_Exception);
  271. }
  272. }
  273. }
  274. /**
  275. * Test walking through the server list
  276. *
  277. * @return void
  278. */
  279. public function testWalkServers()
  280. {
  281. $servers = new Zend_TimeSync($this->timeservers);
  282. foreach ($servers as $key => $server) {
  283. $this->assertTrue($server instanceof Zend_TimeSync_Protocol);
  284. }
  285. }
  286. /**
  287. * Test getting info returned from the server
  288. *
  289. * @return void
  290. */
  291. public function testGetInfo()
  292. {
  293. $server = new Zend_TimeSync('time.windows.com');
  294. try {
  295. $date = $server->getDate();
  296. $result = $server->getInfo();
  297. $this->assertTrue(count($result) > 0);
  298. } catch (Zend_TimeSync_Exception $e) {
  299. // nothing
  300. }
  301. }
  302. }