2
0

TimeSyncTest.php 8.3 KB

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