master
js 157 lines 6.19 KB
Raw
1 /*
2 Copyright 2017-2021 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18 // Polyfill String.endsWith
19 if (!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;
26 };
27 }
28
29 // Replace a string with a number if the string is an exact number
30 function toNumberIfNumber(x) { if ((typeof x == 'string') && (+parseInt(x) == x)) { x = parseInt(x); } return x; }
31
32 // Convert decimal to hex
33 function char2hex(i) { return (i + 0x100).toString(16).substr(-2).toUpperCase(); }
34
35 // Convert a raw string to a hex string
36 function rstr2hex(input) { var r = '', i; for (i = 0; i < input.length; i++) { r += char2hex(input.charCodeAt(i)); } return r; }
37
38 // Convert a buffer into a string
39 function buf2rstr(buf) { var r = ''; for (var i = 0; i < buf.length; i++) { r += String.fromCharCode(buf[i]); } return r; }
40
41 // Convert a hex string to a raw string // TODO: Do this using Buffer(), will be MUCH faster
42 function hex2rstr(d) {
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);
46 return r
47 }
48
49 // Convert an object to string with all functions
50 function 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) + '}';
62 }
63
64 // Return p number of spaces
65 function addPad(p, ret) { var r = ''; for (var i = 0; i < p; i++) { r += ret; } return r; }
66
67 // Split a string taking into account the quoats. Used for command line parsing
68 function 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);
71 return myArray;
72 }
73
74 // Parse arguments string array into an object
75 function parseArgs(argv) {
76 var results = { '_': [] }, current = null;
77 for (var i = 1, len = argv.length; i < len; i++) {
78 var x = argv[i];
79 if (x.length > 2 && x[0] == '-' && x[1] == '-') {
80 if (current != null) { results[current] = true; }
81 current = x.substring(2);
82 } else {
83 if (current != null) { results[current] = toNumberIfNumber(x); current = null; } else { results['_'].push(toNumberIfNumber(x)); }
84 }
85 }
86 if (current != null) { results[current] = true; }
87 return results;
88 }
89
90 // Parge a URL string into an options object
91 function parseUrl(url) {
92 var x = url.split('/');
93 if (x.length < 4) return null;
94 var y = x[2].split(':');
95 var options = {};
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;
99 return options;
100 }
101
102 //console.log(objToString(db2, 2, ' '));
103
104 {
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); } };
110 sha256.write('bob');
111 sha256.end();
112 }
113 {
114 // FAIL!!!!!!!!!
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');
118 sha256x.end();
119 }
120
121 /*
122 {
123 console.log('--- Test 2: Database ---');
124 var db = require('SimpleDataStore').Create('TestSuite.db');
125 var sha256 = require('SHA256Stream');
126
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(); }
130
131 // Compact plenty of times
132 for (var i = 0; i < 10; i++) { console.log(i); db.Compact(); }
133
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), '');
140 };
141 for (var i = 0; i < 10; i++) { console.log(i); sha256.write('A' + i); sha256.end(); }
142 console.log('Test 2 - Completed.');
143 }
144 */
145
146 {
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');
152 console.log(r);
153 r = fs.unlinkSync('TestSuite-1234');
154 }
155
156 console.log('--- Tests Completed ---');
157 process.exit(2);