2
0

TimeSyncTest.php 9.2 KB

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