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