/
index.js
238 lines (177 loc) · 5.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
'use strict';
// external files
var assert = require('chai').assert;
var exec = require('child_process').exec;
var path = require('path');
// internal files
var baseN = require('../lib');
describe('baseN', function() {
describe('Javascript API', function() {
it('should find correct fixed length', function() {
var b64;
// 64
// 4096
// 262144
// 16777216
b64 = baseN.create({
max: 63
});
assert.equal(b64.length, 1);
b64 = baseN.create({
max: 64
});
assert.equal(b64.length, 2);
b64 = baseN.create({
max: 4096
});
assert.equal(b64.length, 3);
b64 = baseN.create({
max: 262144
});
assert.equal(b64.length, 4);
});
it('should generate fixed length string', function() {
var x;
var b64;
b64 = baseN.create({
length: 5
});
x = b64.encode(5);
assert.isString(x);
assert.lengthOf(x, 5);
b64 = baseN.create({
length: 2
});
x = b64.encode(5);
assert.isString(x);
assert.lengthOf(x, 2);
// can also fix length by giving a max integer value allowed.
// this should be two characters. (64^2)
b64 = baseN.create({
max: 4095
});
x = b64.encode(5);
assert.isString(x);
assert.lengthOf(x, 2);
});
it('should symmetric encode/decode', function(done) {
this.timeout(60000);
var b64 = baseN.create({
max: 1000000
});
var accumulator = {};
for (var i = 0; i < 1000000; i++) {
var encoded = b64.encode(i);
var decoded = b64.decode(encoded);
// ensure symmetrical encoding/decoding
assert.equal(decoded, i);
// ensure this encoded value is unique and not seen yet
assert.notOk(accumulator[encoded]);
// add it to accumulator
accumulator[encoded] = true;
}
done();
});
it('should support base-n', function() {
// binary tests
var b2 = baseN.create({
base: 2
});
assert.equal(b2.encode(1), '1');
assert.equal(b2.encode(3), '11');
assert.equal(b2.encode(8), '1000');
assert.equal(b2.encode(9), '1001');
});
it('should support base-n with arbitrary characters', function() {
// binary tests
var b2 = baseN.create({
characters: '_-?',
base: 2
});
assert.equal(b2.encode(1), '-');
assert.equal(b2.encode(3), '--');
assert.equal(b2.encode(8), '-___');
assert.equal(b2.encode(9), '-__-');
});
});
describe('CLI', function() {
var cliPath = path.join(__dirname, '../bin/cli.js');
it('should encode', function(done) {
exec(cliPath + ' encode 11', function(err, stdout, stderr) {
assert.ifError(err);
// replace all new lines in output
var parsedOut = stdout.replace('\n', '');
assert.equal(parsedOut, 'b');
done();
});
});
it('should decode', function(done) {
exec(cliPath + ' decode a', function(err, stdout, stderr) {
assert.ifError(err);
// replace all new lines in output
var parsedOut = stdout.replace('\n', '');
assert.equal(parsedOut, '10');
done();
});
});
});
describe('Error cases', function() {
it('should throw when generating string larger than allowed length',
function() {
var b64;
b64 = baseN.create({
length: 2
});
// 2 characters is 64^2 = 4096 max (0 based)
assert.doesNotThrow(function() {
b64.encode(4095);
});
assert.throws(function() {
b64.encode(4096);
});
b64 = baseN.create({
max: 4095
});
assert.doesNotThrow(function() {
b64.encode(4095);
});
assert.throws(function() {
b64.encode(4096);
}, RangeError);
});
it('should throw when encoding negative integers', function() {
var b64 = baseN.create();
assert.throws(function() {
b64.encode(-1);
}, RangeError);
});
it('should throw when encoding non integers', function() {
var b64 = baseN.create();
assert.throws(function() {
b64.encode('hello');
}, TypeError);
});
it('should throw when decoding unknown character', function() {
var b64 = baseN.create();
assert.throws(function() {
b64.decode('$');
});
});
it('should throw when value exceeds max supported Number value in JS',
function() {
var b64 = baseN.create();
assert.throws(function() {
b64.decode('-----------');
}, RangeError);
});
it('should throw when both max and lengh are specified in options',
function() {
assert.throws(function() {
baseN.create({
max: 1000,
length: 2
});
});
});
});
});