testsuite.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Copyright 2017-2021 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Polyfill String.endsWith
  14. if (!String.prototype.endsWith) {
  15. String.prototype.endsWith = function (searchString, position) {
  16. var subjectString = this.toString();
  17. if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) { position = subjectString.length; }
  18. position -= searchString.length;
  19. var lastIndex = subjectString.lastIndexOf(searchString, position);
  20. return lastIndex !== -1 && lastIndex === position;
  21. };
  22. }
  23. // Replace a string with a number if the string is an exact number
  24. function toNumberIfNumber(x) { if ((typeof x == 'string') && (+parseInt(x) == x)) { x = parseInt(x); } return x; }
  25. // Convert decimal to hex
  26. function char2hex(i) { return (i + 0x100).toString(16).substr(-2).toUpperCase(); }
  27. // Convert a raw string to a hex string
  28. function rstr2hex(input) { var r = '', i; for (i = 0; i < input.length; i++) { r += char2hex(input.charCodeAt(i)); } return r; }
  29. // Convert a buffer into a string
  30. function buf2rstr(buf) { var r = ''; for (var i = 0; i < buf.length; i++) { r += String.fromCharCode(buf[i]); } return r; }
  31. // Convert a hex string to a raw string // TODO: Do this using Buffer(), will be MUCH faster
  32. function hex2rstr(d) {
  33. if (typeof d != "string" || d.length == 0) return '';
  34. var r = '', m = ('' + d).match(/../g), t;
  35. while (t = m.shift()) r += String.fromCharCode('0x' + t);
  36. return r
  37. }
  38. // Convert an object to string with all functions
  39. function objToString(x, p, ret) {
  40. if (ret == undefined) ret = '';
  41. if (p == undefined) p = 0;
  42. if (x == null) { return '[null]'; }
  43. if (p > 8) { return '[...]'; }
  44. if (x == undefined) { return '[undefined]'; }
  45. if (typeof x == 'string') { if (p == 0) return x; return '"' + x + '"'; }
  46. if (typeof x == 'buffer') { return '[buffer]'; }
  47. if (typeof x != 'object') { return x; }
  48. var r = '{' + (ret ? '\r\n' : ' ');
  49. for (var i in x) { r += (addPad(p + 2, ret) + i + ': ' + objToString(x[i], p + 2, ret) + (ret ? '\r\n' : ' ')); }
  50. return r + addPad(p, ret) + '}';
  51. }
  52. // Return p number of spaces
  53. function addPad(p, ret) { var r = ''; for (var i = 0; i < p; i++) { r += ret; } return r; }
  54. // Split a string taking into account the quoats. Used for command line parsing
  55. function splitArgs(str) {
  56. var myArray = [], myRegexp = /[^\s"]+|"([^"]*)"/gi;
  57. do { var match = myRegexp.exec(str); if (match != null) { myArray.push(match[1] ? match[1] : match[0]); } } while (match != null);
  58. return myArray;
  59. }
  60. // Parse arguments string array into an object
  61. function parseArgs(argv) {
  62. var results = { '_': [] }, current = null;
  63. for (var i = 1, len = argv.length; i < len; i++) {
  64. var x = argv[i];
  65. if (x.length > 2 && x[0] == '-' && x[1] == '-') {
  66. if (current != null) { results[current] = true; }
  67. current = x.substring(2);
  68. } else {
  69. if (current != null) { results[current] = toNumberIfNumber(x); current = null; } else { results['_'].push(toNumberIfNumber(x)); }
  70. }
  71. }
  72. if (current != null) { results[current] = true; }
  73. return results;
  74. }
  75. // Parge a URL string into an options object
  76. function parseUrl(url) {
  77. var x = url.split('/');
  78. if (x.length < 4) return null;
  79. var y = x[2].split(':');
  80. var options = {};
  81. var options = { protocol: x[0], hostname: y[0], path: '/' + x.splice(3).join('/') };
  82. if (y.length == 1) { options.port = ((x[0] == 'https:') || (x[0] == 'wss:')) ? 443 : 80; } else { options.port = parseInt(y[1]); }
  83. if (isNaN(options.port) == true) return null;
  84. return options;
  85. }
  86. //console.log(objToString(db2, 2, ' '));
  87. {
  88. // TODO: Fix this to use the event emitor
  89. // TODO: Add SHA256 sync
  90. console.log('--- Test 1: SHA256 hashing ---');
  91. var sha256 = require('SHA256Stream');
  92. sha256.hashString = function (x) { if (x == '81B637D8FCD2C6DA6359E6963113A1170DE795E4B725B84D1E0B4CFD9EC58CE9') { console.log('Test 1 - OK: ' + x); } else { console.log('Test 1 - FAIL: ' + x); } };
  93. sha256.write('bob');
  94. sha256.end();
  95. }
  96. {
  97. // FAIL!!!!!!!!!
  98. var sha256x = require('SHA256Stream');
  99. sha256x.hashString = function (x) { if (x == '81B637D8FCD2C6DA6359E6963113A1170DE795E4B725B84D1E0B4CFD9EC58CE9') { console.log('Test 1 - OK: ' + x); } else { console.log('Test 1 - FAIL: ' + x); } };
  100. sha256x.write('bob');
  101. sha256x.end();
  102. }
  103. /*
  104. {
  105. console.log('--- Test 2: Database ---');
  106. var db = require('SimpleDataStore').Create('TestSuite.db');
  107. var sha256 = require('SHA256Stream');
  108. // Write a pile of hashes to the DB
  109. sha256.hashString = function (x) { db.Put(x.substring(0, 16), x.substring(16)); console.log('ADD: ' + x.substring(0, 16) + ': ' + x.substring(16)); };
  110. for (var i = 0; i < 10; i++) { console.log(i); sha256.write('A' + i); sha256.end(); }
  111. // Compact plenty of times
  112. for (var i = 0; i < 10; i++) { console.log(i); db.Compact(); }
  113. // Check all the hashes
  114. sha256.hashString = function (x) {
  115. var r = db.Get(x.substring(0, 16));
  116. console.log('GET: ' + x.substring(0, 16) + ': ' + r);
  117. if (r != x.substring(16)) { console.log('FAILED ' + x.substring(0, 16) + ': ' + x.substring(16) + ' != ' + r); }
  118. //db.Put(x.substring(0, 16), '');
  119. };
  120. for (var i = 0; i < 10; i++) { console.log(i); sha256.write('A' + i); sha256.end(); }
  121. console.log('Test 2 - Completed.');
  122. }
  123. */
  124. {
  125. console.log('--- Test 3: Files ---');
  126. var r, fs = require('fs');
  127. //console.log(objToString(fs, 2, ' '));
  128. r = fs.mkdirSync('TestSuite-123');
  129. r = fs.renameSync('TestSuite-123', 'TestSuite-1234');
  130. console.log(r);
  131. r = fs.unlinkSync('TestSuite-1234');
  132. }
  133. console.log('--- Tests Completed ---');
  134. process.exit(2);