PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/nutils_tests.php

http://otpauth.googlecode.com/
PHP | 509 lines | 351 code | 144 blank | 14 comment | 23 complexity | cd315c9cb728436aa686176c48f36e97 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if (! defined('SIMPLE_TEST')) {
  3. define('SIMPLE_TEST', './simpletest/');
  4. }
  5. require_once('otpauthHtmlReporter.class.php');
  6. require_once('../nutils.php');
  7. class rShiftTests extends UnitTestCase {
  8. function rShiftTests() {
  9. $this->UnitTestCase("");
  10. }
  11. // function rshft_ulong($ulong, $amount, $wrap = 0) {
  12. }
  13. class lShiftTests extends UnitTestCase {
  14. function rShiftTests() {
  15. $this->UnitTestCase("");
  16. }
  17. // function lshft_ulong($ulong, $amount, $wrap = 0) {
  18. }
  19. class hexstr2IntTests extends UnitTestCase {
  20. function hexstr2IntTests() {
  21. $this->UnitTestCase("Proper functionality of hexstr2int");
  22. }
  23. function testNullParameter() {
  24. $parameter = null;
  25. try {
  26. $this->assertFalse(hexstr2int($parameter), "null parameter should throw exception and return false.");
  27. }
  28. catch (Exception $e) {
  29. $this->pass("Properly threw exception on null parameter");
  30. }
  31. }
  32. function testEmptyParameter() {
  33. $parameter = '';
  34. try {
  35. $this->assertFalse(hexstr2int($parameter), "empty string parameter should throw exception and return false.");
  36. }
  37. catch (Exception $e) {
  38. $this->pass("Properly threw exception on empty string parameter");
  39. }
  40. }
  41. function testIntegerParameter() {
  42. $parameter = 45;
  43. try {
  44. $this->assertFalse(hexstr2int($parameter), "integer parameter should throw exception and return false.");
  45. }
  46. catch (Exception $e) {
  47. $this->pass("Properly threw exception on integer parameter");
  48. }
  49. }
  50. function testArrayParameter() {
  51. $parameter = array();
  52. $parameter[0] = "zero";
  53. $parameter[1] = "one";
  54. try {
  55. $this->assertFalse(hexstr2int($parameter), "array parameter should throw exception and return false.");
  56. }
  57. catch (Exception $e) {
  58. $this->pass("Properly threw exception on array parameter");
  59. }
  60. }
  61. function testNonHexString() {
  62. $parameter = "55999ZZ";
  63. try {
  64. $this->assertFalse(hexstr2int($parameter), "non-hex string parameter should throw exception and return false.");
  65. }
  66. catch (Exception $e) {
  67. $this->pass("Properly threw exception on non-hex string parameter");
  68. }
  69. }
  70. function testValidHexString() {
  71. $parameter = "AA9900";
  72. $decval = 11180288;
  73. $retval = hexstr2int($parameter);
  74. $this->assertTrue($decval == $retval, "hex string $parameter should convert to $decval, converted to $retval");
  75. }
  76. function testValidShortHexString() {
  77. $parameter = "05c";
  78. $decval = 92;
  79. $retval = hexstr2int($parameter);
  80. $this->assertTrue($decval == $retval, "hex string $parameter should convert to $decval, converted to $retval");
  81. }
  82. function testValid8DigitHexString() {
  83. $parameter = "12345678";
  84. $decval = 305419896;
  85. $retval = hexstr2int($parameter);
  86. $this->assertTrue($decval == $retval, "hex string $parameter should convert to $decval, converted to $retval");
  87. }
  88. function testValid10DigitHexString() {
  89. $parameter = "123456789a";
  90. $decval = 78187493530;
  91. $retval = hexstr2int($parameter);
  92. $this->assertTrue($decval == $retval, "hex string $parameter should convert to $decval, converted to $retval");
  93. }
  94. function testValid12DigitHexString() {
  95. $parameter = "123456789abc";
  96. $decval = 20015998343868;
  97. $retval = hexstr2int($parameter);
  98. $this->assertTrue($decval == $retval, "hex string $parameter should convert to $decval, converted to $retval");
  99. }
  100. function testValid14DigitHexString() {
  101. $parameter = "123456789abcde";
  102. $decval = 5124095576030430;
  103. $retval = hexstr2int($parameter);
  104. $this->assertTrue($decval == $retval, "hex string $parameter should convert to $decval, converted to $retval");
  105. }
  106. function testValid16DigitHexString() {
  107. $parameter = "123456789abcdeff";
  108. $decval = 1311768467463790300;
  109. $retval = hexstr2int($parameter);
  110. $this->assertTrue($decval == $retval, "hex string $parameter should convert to $decval, converted to $retval");
  111. }
  112. function testHexStringWithLeading0x() {
  113. $parameter = "0x123456789a";
  114. $decval = 78187493530;
  115. $retval = hexstr2int($parameter);
  116. $this->assertTrue($decval == $retval, "hex string $parameter should convert to $decval, converted to $retval");
  117. }
  118. function testValid48DigitHexString() {
  119. $parameter = "fffffffffffffffffffffffffFFFFFFFFFFFFFFFFFFFFFFF";
  120. $decval = 6.2771017353867E+57;
  121. $retval = hexstr2int($parameter);
  122. $this->assertTrue($decval == $retval, "hex string $parameter should convert to $decval, converted to $retval");
  123. }
  124. function testValidHexStringWithUpperLowerAndNumeric() {
  125. $parameter = "1aBcD5";
  126. $decval = 1752277;
  127. $retval = hexstr2int($parameter);
  128. $this->assertTrue($decval == $retval, "hex string $parameter should convert to $decval, converted to $retval");
  129. }
  130. function testValidHexStringWithLeadingZero() {
  131. $parameter = "01aBcD5";
  132. $decval = 1752277;
  133. $retval = hexstr2int($parameter);
  134. $this->assertTrue($decval == $retval, "hex string $parameter should convert to $decval, converted to $retval");
  135. }
  136. function testValidHexStringWithTrailingZeros() {
  137. $parameter = "01aBcD500";
  138. $decval = 448582912;
  139. $retval = hexstr2int($parameter);
  140. $this->assertTrue($decval == $retval, "hex string $parameter should convert to $decval, converted to $retval");
  141. }
  142. }
  143. class binstr2IntTests extends UnitTestCase {
  144. function binstr2IntTests() {
  145. $this->UnitTestCase("Proper functionality of binstr2int");
  146. }
  147. function testNullParameter() {
  148. $parameter = null;
  149. try {
  150. $this->assertFalse(binstr2int($parameter), "null parameter should throw exception and return false.");
  151. }
  152. catch (Exception $e) {
  153. $this->pass("Properly threw exception on null parameter");
  154. }
  155. }
  156. function testEmptyParameter() {
  157. $parameter = '';
  158. try {
  159. $this->assertFalse(binstr2int($parameter), "empty string parameter should throw exception and return false.");
  160. }
  161. catch (Exception $e) {
  162. $this->pass("Properly threw exception on empty string parameter");
  163. }
  164. }
  165. function testIntegerParameter() {
  166. $parameter = 45;
  167. try {
  168. $this->assertFalse(binstr2int($parameter), "integer parameter should throw exception and return false.");
  169. }
  170. catch (Exception $e) {
  171. $this->pass("Properly threw exception on integer parameter");
  172. }
  173. }
  174. function testInvalidArrayParameter() {
  175. $parameter = array();
  176. $parameter[0] = "zero";
  177. $parameter[1] = "one";
  178. try {
  179. $this->assertFalse(hexstr2int($parameter), "bad array parameters should throw exception and return false.");
  180. }
  181. catch (Exception $e) {
  182. $this->pass("Properly threw exception on array parameter");
  183. }
  184. }
  185. function testNonBinString() {
  186. $parameter = "55999ZZ";
  187. try {
  188. $this->assertFalse(hexstr2int($parameter), "non-binary string parameter should throw exception and return false.");
  189. }
  190. catch (Exception $e) {
  191. $this->pass("Properly threw exception on non-binary string parameter");
  192. }
  193. }
  194. function testValidBinString() {
  195. $parameter = "101100";
  196. $decval = 44;
  197. $retval = binstr2int($parameter);
  198. $this->assertTrue($decval == $retval, "bin string $parameter should convert to $decval, converted to $retval");
  199. }
  200. function testValidBinStringWithTrailingB() {
  201. $parameter = "101100b";
  202. $decval = 44;
  203. $retval = binstr2int($parameter);
  204. $this->assertTrue($decval == $retval, "bin string $parameter should convert to $decval, converted to $retval");
  205. }
  206. function testValidShortBinString() {
  207. $parameter = "1";
  208. $decval = 1;
  209. $retval = binstr2int($parameter);
  210. $this->assertTrue($decval == $retval, "bin string $parameter should convert to $decval, converted to $retval");
  211. }
  212. function testValid8DigitBinString() {
  213. }
  214. function testValid16DigitBinString() {
  215. }
  216. function testValid32DigitBinString() {
  217. $parameter = "111101100010101110011010110001101";
  218. $decval = 16133018;
  219. $retval = binstr2int($parameter);
  220. $this->assertTrue($decval == $retval, "bin string $parameter should convert to $decval, converted to $retval");
  221. }
  222. function testValid33DigitBinString() {
  223. //should return invalid value, due to overflow
  224. }
  225. function testValidBinStringWithLeadingZero() {
  226. }
  227. function testValidBinStringWithTrailingZeros() {
  228. }
  229. }
  230. class ulong2binstrTests extends UnitTestCase {
  231. function ulong2binstrTests() {
  232. $this->UnitTestCase("");
  233. }
  234. // ulong2binstr
  235. }
  236. class ulong2hexstrTests extends UnitTestCase {
  237. function ulong2hexstrTests() {
  238. $this->UnitTestCase("");
  239. }
  240. // function ulong2hexstr($ulong, $padLength = -1) {
  241. }
  242. class strbin2ulongTests extends UnitTestCase {
  243. function strbin2ulong() {
  244. $this->UnitTestCase("");
  245. }
  246. // function strbin2ulong($value) {
  247. }
  248. class str2arrTests extends UnitTestCase {
  249. function str2arrTests() {
  250. $this->UnitTestCase("");
  251. }
  252. // function str2arr($string) {
  253. }
  254. class hexString_2_booleanArrayTests extends UnitTestCase {
  255. function hexString_2_booleanArrayTests() {
  256. $this->UnitTestCase("");
  257. }
  258. // function hexString_2_booleanArray($hex) {
  259. }
  260. class booleanArray_2_hexStringTests extends UnitTestCase {
  261. function booleanArray_2_hexStringTests() {
  262. $this->UnitTestCase("");
  263. }
  264. // function booleanArray_2_hexString($boolArr) {
  265. }
  266. class readbit_ulongTests extends UnitTestCase {
  267. function readbit_ulongTests() {
  268. $this->UnitTestCase("");
  269. }
  270. // function readbit_ulong($val, $bit) {
  271. }
  272. class writebit_ulongTests extends UnitTestCase {
  273. function writebit_ulongTests() {
  274. $this->UnitTestCase("");
  275. }
  276. // function writebit_ulong($val, $bit, $set = 'ON') {
  277. }
  278. class writebit_ulong_setTests extends UnitTestCase {
  279. function writebit_ulong_setTests() {
  280. $this->UnitTestCase("");
  281. }
  282. // function writebit_ulong_set($val, $bit) {
  283. }
  284. class writebit_ulong_unsetTests extends UnitTestCase {
  285. function writebit_ulong_unsetTests() {
  286. $this->UnitTestCase("");
  287. }
  288. // function writebit_ulong_unset($val, $bit) {
  289. }
  290. class randomBooleanArrayTests extends UnitTestCase {
  291. function randomBooleanArrayTests() {
  292. $this->UnitTestCase("");
  293. }
  294. // function randomBooleanArray($numBits) {
  295. }
  296. class randomBytesTests extends UnitTestCase {
  297. function randomBytesTests() {
  298. $this->UnitTestCase("");
  299. }
  300. function testRandom1ByteIsRandom() {
  301. $NUM_TESTS = 100;
  302. $NUM_BITS = 16;
  303. $randoms = array();
  304. for ($i = 0; $i < $NUM_TESTS; ++$i) {
  305. $randoms[] = randomBytes($NUM_BITS);
  306. }
  307. $randoms_uniq = array_unique($randoms);
  308. $this->assertTrue(count($randoms) == count($randoms_uniq), "Randoms array count (".count($randoms).") should be equal to randoms_uniq array count (".count($randoms_uniq).").");
  309. }
  310. function testRandom16BytesAreRandom() {
  311. $NUM_TESTS = 100;
  312. $NUM_BITS = 128;
  313. $randoms = array();
  314. for ($i = 0; $i < $NUM_TESTS; ++$i) {
  315. $randoms[] = randomBytes($NUM_BITS);
  316. }
  317. $randoms_uniq = array_unique($randoms);
  318. $this->assertTrue(count($randoms) == count($randoms_uniq), "Randoms array count (".count($randoms).") should be equal to randoms_uniq array count (".count($randoms_uniq).").");
  319. }
  320. function testRandom1024BytesAreRandom() {
  321. $NUM_TESTS = 100;
  322. $NUM_BITS = 8192;
  323. $randoms = array();
  324. for ($i = 0; $i < $NUM_TESTS; ++$i) {
  325. $randoms[] = randomBytes($NUM_BITS);
  326. }
  327. $randoms_uniq = array_unique($randoms);
  328. $this->assertTrue(count($randoms) == count($randoms_uniq), "Randoms array count (".count($randoms).") should be equal to randoms_uniq array count (".count($randoms_uniq).").");
  329. }
  330. }
  331. function nutils_run_tests(&$reporter) {
  332. $test = &new rShiftTests();
  333. $test->run($reporter);
  334. $test = &new lShiftTests();
  335. $test->run($reporter);
  336. $test = &new hexstr2IntTests();
  337. $test->run($reporter);
  338. $test = &new binstr2IntTests();
  339. $test->run($reporter);
  340. $test = &new ulong2binstrTests();
  341. $test->run($reporter);
  342. $test = &new ulong2hexstrTests();
  343. $test->run($reporter);
  344. $test = &new strbin2ulongTests();
  345. $test->run($reporter);
  346. $test = &new str2arrTests();
  347. $test->run($reporter);
  348. $test = &new hexString_2_booleanArrayTests();
  349. $test->run($reporter);
  350. $test = &new booleanArray_2_hexStringTests();
  351. $test->run($reporter);
  352. $test = &new readbit_ulongTests();
  353. $test->run($reporter);
  354. $test = &new writebit_ulongTests();
  355. $test->run($reporter);
  356. $test = &new writebit_ulong_setTests();
  357. $test->run($reporter);
  358. $test = &new writebit_ulong_unsetTests();
  359. $test->run($reporter);
  360. $test = &new randomBooleanArrayTests();
  361. $test->run($reporter);
  362. $test = &new randomBytesTests();
  363. $test->run($reporter);
  364. }
  365. ?>