Skip to content

Commit

Permalink
utils test
Browse files Browse the repository at this point in the history
  • Loading branch information
lovelycs committed Oct 19, 2018
1 parent 97bc0ae commit 9d1235e
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 68 deletions.
58 changes: 28 additions & 30 deletions libs/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default {
bytesToHex(arr) {
bytesToHex(arr = []) {
let hexArr = Array.prototype.map.call(arr, function (bit) {
return ('00' + bit.toString(16)).slice(-2);
});
Expand All @@ -11,55 +11,53 @@ export default {
}));
return typedArray;
},
getBytesSize(str) {
let count = 0;
for (let i = 0; i < str.length; i++){
let c = str.charAt(i);
let bytes = /^[\u0000-\u00ff]$/.test(c) ? 1 : 2;
count += bytes;
}
return count;
},
strToHex(str) {
var hex = '';
for(let i=0; i<str.length; i++) {
hex += ''+str.charCodeAt(i).toString(16);
getBytesSize(str, charset){
var total = 0, code, i, len;
charset = charset ? charset.toLowerCase() : '';

if (charset === 'utf-16' || charset === 'utf16'){
for (i = 0, len = str.length; i < len; i++) {
code = str.charCodeAt(i);
total += code <= 0xffff ? 2 : 4;
}
return total;
}
return hex;
},
hexToStr (hex) {
var string = '';
for (let i = 0; i < hex.length; i += 2) {
string += String.fromCharCode(parseInt(hex.substr(i, 2), 16));

for (i = 0, len = str.length; i < len; i++) {
code = str.charCodeAt(i);
if (0x00 <= code && code <= 0x7f) {
total += 1;
} else if (0x80 <= code && code <= 0x7ff) {
total += 2;
} else if ((0x800 <= code && code <= 0xd7ff) || (0xe000 <= code && code <= 0xffff)) {
total += 3;
} else {
total += 4;
}
}
return string;
return total;
},
strToUtf8Bytes (str) {
if (!str) {
return null;
}
strToUtf8Bytes (str = '') {
var back = [];
// var byteSize = 0;
for (var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i);
if (0x00 <= code && code <= 0x7f) {
// byteSize += 1;
back.push(code);
} else if (0x80 <= code && code <= 0x7ff) {
// byteSize += 2;
back.push((192 | (31 & (code >> 6))));
back.push((128 | (63 & code)));
} else if ((0x800 <= code && code <= 0xd7ff)
|| (0xe000 <= code && code <= 0xffff)) {
// byteSize += 3;
back.push((224 | (15 & (code >> 12))));
back.push((128 | (63 & (code >> 6))));
back.push((128 | (63 & code)));
}
}

for (i = 0; i < back.length; i++) {
back[i] &= 0xff;
}

return new Uint8Array(back);
}
};
};
3 changes: 1 addition & 2 deletions test/Vite/account.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const assert = require('assert');
import vitejs from '../../index.js';

const HTTP_RPC = new vitejs.HTTP_RPC({});
const ViteJS = new vitejs(HTTP_RPC);
const ViteJS = new vitejs();
const Account = ViteJS.Vite.Account;

describe('Vite_Account', function () {
Expand Down
12 changes: 1 addition & 11 deletions test/Wallet/account.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
const assert = require('assert');
import vitejs from '../../index.js';

const HTTP_RPC = new vitejs.HTTP_RPC({});
const ViteJS = new vitejs(HTTP_RPC);
const ViteJS = new vitejs();
const Account = ViteJS.Wallet.Account;

describe('Wallet_Account', function () {
// it('test_encrypt_decrypt', function () {
// let pass = 'sdsdsdsds';
// let encrypt = Account.encrypt(pass);
// let verify = Account.verify(encrypt, pass);

// assert.equal(verify, true);
// });

it('test_encrypt_decrypt', function () {
let entropy = 'e92a9e90900908185e6c041b21be5602bc515357d60504b1f10d65ae445af516';
let encrypt = Account.encrypt(entropy, '1');
let encryptEntropy = Account.decrypt(encrypt, '1');
// console.log(encrypt);
assert.equal(encryptEntropy, entropy);
});
});
3 changes: 1 addition & 2 deletions test/Wallet/address.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const assert = require('assert');
import vitejs from '../../index.js';

const HTTP_RPC = new vitejs.HTTP_RPC({});
const ViteJS = new vitejs(HTTP_RPC);
const ViteJS = new vitejs();
const WalletHD = ViteJS.Wallet.Address;

describe('Wallet_Address', function () {
Expand Down
3 changes: 1 addition & 2 deletions test/Wallet/keystore.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const assert = require('assert');
import vitejs from '../../index.js';

const HTTP_RPC = new vitejs.HTTP_RPC({});
const ViteJS = new vitejs(HTTP_RPC);
const ViteJS = new vitejs();

let keyJSON = {'hexaddress':'vite_5f03e33f9550155548bf0a045a7a602384d3f1a65fb2ceff6b','id':'71b3bd76-a938-403d-b876-57ca22f993f3','crypto':{'ciphername':'aes-256-gcm','ciphertext':'03d8f2773ddce6132a5ceb136ba736ae1640ba2d664f2a8493a2f3ff2bb84dec8a3229edad6031a7ef494946852d3e3a45b0b3a88eb3167b41a3843d01d93c9abe52479f582b694e9c378dfb5aac7eb5','nonce':'a6bb299c35e48960e096351d','kdf':'scrypt','scryptparams':{'n':262144,'r':8,'p':1,'keylen':32,'salt':'a35724d6c46cb59e47c893a2bba3875c5b238c8a9ce9b2b1b3c4c0f6dec618db'}},'keystoreversion':1,'timestamp':1536059534};
let keystore = JSON.stringify(keyJSON);
Expand Down
22 changes: 11 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
describe('Base Libs Test', function() {
describe('Mnemonic Test: bip39', function () {
require('./base/bip39');
});
// describe('Base Libs Test', function() {
// describe('Mnemonic Test: bip39', function () {
// require('./base/bip39');
// });

describe('Ed25519 blake2b Test: tweetnale-blake2b', function () {
require('./base/ed25519');
});
// describe('Ed25519 blake2b Test: tweetnale-blake2b', function () {
// require('./base/ed25519');
// });
// });

describe('Utils Test: libs/utils', function () {
require('./utils');
});

describe('Address Test: src/utils/address', function () {
Expand All @@ -21,7 +25,3 @@ describe('Wallet Test: src/Wallet', function () {
require('./Wallet/address');
require('./Wallet/account');
});

describe('Utils Test: libs/utils', function () {
require('./utils');
});
44 changes: 34 additions & 10 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,39 @@ import utils from '../libs/utils.js';

describe('strToUtf8', function () {
it('test1', function () {
let utf8Bytes = utils.strToUtf8Bytes('为', true);
assert.equal('e4b8ba', utils.bytesToHex(utf8Bytes));
let utf8Bytes = utils.strToUtf8Bytes('为');
let hex = utils.bytesToHex(utf8Bytes);

assert.equal('e4b8ba', hex);
assert.deepEqual(utf8Bytes, utils.hexToBytes(hex));
});
it('test2', function () {
let utf8Bytes = utils.strToUtf8Bytes('sdsdsds。;。、lp.;,p[oo阿京东方]');
let hex = utils.bytesToHex(utf8Bytes);

assert.equal('73647364736473e38082efbc9be38082e380816c702e3b2c705b6f6fe998bfe4baace4b89ce696b95d', hex);
assert.deepEqual(utf8Bytes, utils.hexToBytes(hex));
});
it('test3', function () {
let utf8Bytes = utils.strToUtf8Bytes('[坏笑]😊🙂🙂😆😅😅');
let hex = utils.bytesToHex(utf8Bytes);

assert.equal('5be59d8fe7ac915d', hex);
assert.deepEqual(utf8Bytes, utils.hexToBytes(hex));
});
});

describe('getBytesSize', function () {
it('test1', function () {
assert.equal(40, utils.getBytesSize('是打发发发 水电费是否爽肤水'));
});
it('test2', function () {
assert.equal(30, utils.getBytesSize('sdjafofaodsfjwo8eifhsnodslkfjs'));
});
it('test3', function () {
assert.equal(56, utils.getBytesSize('[坏笑]😊🙂🙂😆😅😅'));
});
it('test3', function () {
assert.equal(32, utils.getBytesSize('[坏笑]😊🙂🙂😆😅😅', 'utf16'));
});
// it('test2', function () {
// let utf8Bytes = utils.strToUtf8Bytes('sdsdsds。;。、lp.;,p[oo阿京东方]', true);
// console.log(utils.bytesToHex(utf8Bytes));
// });
// it('test3', function () {
// let utf8Bytes = utils.strToUtf8Bytes('[坏笑]😊🙂🙂😆😅😅', true);
// console.log(utils.bytesToHex(utf8Bytes));
// });
});

0 comments on commit 9d1235e

Please sign in to comment.