forked from ericwoodruff/passwordhasherplus
-
Notifications
You must be signed in to change notification settings - Fork 4
/
spec.js
197 lines (166 loc) · 5.17 KB
/
spec.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
mocha.ui('bdd');
mocha.reporter('html');
var expect = chai.expect;
describe("guid", function () {
it("generates uniquely", function () {
expect(generateGuid()).not.to.be.equal(generateGuid());
});
});
describe("bump", function () {
it("bumps foo", function () {
expect(bump("foo")).to.be.equal("foo:1");
});
it("bumps foo:1", function () {
expect(bump("foo:1")).to.be.equal("foo:2");
});
it("bumps foo:100", function () {
expect(bump("foo:100")).to.be.equal("foo:101");
});
});
describe("grepUrl", function () {
it("matches simple domain", function () {
expect(grepUrl("http://foo.com")).to.be.equal("foo");
expect(grepUrl("https://foo.com")).to.be.equal("foo");
});
it("matches standard domain", function () {
expect(grepUrl("http://www.foo.com")).to.be.equal("foo");
});
it("matches ip addrs", function () {
expect(grepUrl("http://1.1.1.1")).to.be.equal("1.1.1.1");
expect(grepUrl("http://111.111.111.111")).to.be.equal("111.111.111.111");
});
it("matches sub domains", function () {
expect(grepUrl("http://www2.foo.com")).to.be.equal("foo");
expect(grepUrl("http://test.foo.com")).to.be.equal("foo");
});
it("matches uk domains", function () {
expect(grepUrl("http://www.foo.co.uk")).to.be.equal("foo");
expect(grepUrl("http://test.foo.co.uk")).to.be.equal("foo");
});
});
describe("queryParam", function () {
it("finds in 1 param", function () {
expect(extractQueryParam("http://test.com?foo=true", "foo")).to.be.equal("true");
});
it("finds with no param", function () {
expect(extractQueryParam("http://test.com", "foo")).to.be.equal(null);
expect(extractQueryParam("http://test.com?a=1", "foo")).to.be.equal(null);
});
it("finds with 2 params", function () {
expect(extractQueryParam("http://test.com?a=1&b=2", "a")).to.be.equal("1");
expect(extractQueryParam("http://test.com?a=1&b=2", "b")).to.be.equal("2");
});
});
describe("generateHash", function () {
var DIGITS = 0;
var ALPHANUM = 1;
var SPECIAL = 2;
var config = {};
var seed = generateGuid();
var options = { compatibilityMode: false };
var input = "mypassword";
function generate(length, strength) {
var config = { tag: "mytag", options: options,
policy: {
length: length,
strength: strength,
seed: seed
}
};
return generateHash(config, input);
}
for(i=1; i <= 24; ++i) {
it("generates "+i+" digit(s)", function () {
var result = generate(i, DIGITS);
var result2 = generate(i, DIGITS);
expect(result.length).to.be.equal(i);
expect(result).to.match(/^[0-9]+$/);
expect(result).to.be.equal(result2);
});
it("generates "+i+" alphanum(s)", function () {
var result = generate(i, ALPHANUM);
var result2 = generate(i, ALPHANUM);
expect(result.length).to.be.equal(i);
expect(result).to.match(/^[a-zA-Z0-9]+$/);
expect(result).to.match(/[0-9]/);
expect(result).to.be.equal(result2);
});
it("generates "+i+" special(s)", function () {
var result = generate(i, SPECIAL);
var result2 = generate(i, SPECIAL);
expect(result.length).to.be.equal(i);
expect(result).to.match(/^[a-zA-Z0-9!@#$%^&*()\\\/+'",.-]+$/);
expect(result).to.match(/[!@#$%^&*()\\\/+'",.-]/);
expect(result).to.be.equal(result2);
});
}
it("ignores seed in compatibility mode", function () {
var withSeed = generate(24, SPECIAL);
var result2 = generate(24, SPECIAL);
options.compatibilityMode = true;
var withoutSeed = generate(24, SPECIAL);
seed = generateGuid();
var withNewSeed = generate(24, SPECIAL);
expect(withSeed).to.not.be.equal(withoutSeed);
expect(withoutSeed).to.be.equal(withNewSeed);
expect(withSeed).to.be.equal(result2);
});
});
describe("localStorage", function () {
it("can clear, dump and migrate", function () {
localStorage.clear ();
var dump = dumpDatabase ();
expect(JSON.stringify(JSON.parse(dump))).to.be.equal(JSON.stringify({}));
localStorage.migrate ();
dump = dumpDatabase ();
expect(JSON.stringify(JSON.parse(dump))).to.not.be.equal(JSON.stringify({}));
});
it("can save and load config", function () {
var seed = generateGuid();
var options = { compatibilityMode: false };
var input = "mypassword";
var config = { tag: "mytag", options: options,
policy: {
length: 8,
strength: 2,
seed: seed
},
fields: ["abc", "def"]
};
localStorage.saveConfig("foo", config);
var loaded = localStorage.loadConfig("foo");
expect(loaded.tag).to.be.equal(config.tag);
expect(loaded.policy).to.be.eql(config.policy);
expect(loaded.fields).to.be.eql(config.fields);
});
it("can load tags", function () {
var tags = localStorage.loadTags();
expect(tags.length).to.be.equal(1);
expect(tags[0]).to.be.equal("mytag");
});
it("can collect garbage", function () {
var seed = generateGuid();
var options = { compatibilityMode: false };
var input = "mypassword";
var config = { tag: "othertag", options: options,
policy: {
length: 8,
strength: 2,
seed: seed
},
fields: []
};
localStorage.saveConfig("foo", config);
var tags = localStorage.loadTags();
expect(tags.length).to.be.equal(2);
localStorage.collectGarbage();
var tags = localStorage.loadTags();
expect(tags.length).to.be.equal(1);
});
/*
it("can migrate", function () {
// TODO
});
*/
});
mocha.run();