-
Notifications
You must be signed in to change notification settings - Fork 3
/
2FA.test.ts
141 lines (111 loc) · 4.41 KB
/
2FA.test.ts
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
import twoFA from '../src/2FA';
describe('2FA Tests', () => {
describe('generateSecret()', () => {
test('should return string', () => {
const secret = twoFA.generateSecret();
expect(typeof secret).toBe('string');
});
test('should generate 26 char long secret without arg', () => {
const secret = twoFA.generateSecret();
expect(secret.length).toBe(26);
});
test('should generate 32 char long secret with arg 20', () => {
const secret = twoFA.generateSecret(20);
expect(secret.length).toBe(32);
});
});
describe('generateHOTP()', () => {
test('should return string', () => {
const secret = twoFA.generateSecret();
const counter: number = Math.floor(Date.now() / 30000);
const hotp = twoFA.generateHOTP(secret, counter);
expect(typeof hotp).toBe('string');
});
test('should generate 6 char long number without arg', () => {
const secret = twoFA.generateSecret();
const counter: number = Math.floor(Date.now() / 30000);
const hotp = twoFA.generateHOTP(secret, counter);
expect(hotp.length).toBe(6);
});
test(`should generate a correct length token with random arg`, () => {
const secret = twoFA.generateSecret();
const counter: number = Math.floor(Date.now() / 30000);
for (let i = 0; i < 10000; i++) {
const length = Math.floor(Math.random() * 9) + 1
const hotp = twoFA.generateHOTP(secret, counter, length);
expect(hotp.length).toBe(length);
}
})
});
describe('generateTOTP()', () => {
test('should return string', () => {
const secret = twoFA.generateSecret();
const totp = twoFA.generateTOTP(secret);
expect(typeof totp).toBe('string');
});
test('should generate present token without arg', () => {
const secret = twoFA.generateSecret();
const counter: number = Math.floor(Date.now() / 30000);
const hotp = twoFA.generateHOTP(secret, counter);
const totp = twoFA.generateTOTP(secret);
expect(totp).toBe(hotp);
});
test('should generate past token with arg -1', () => {
const secret = twoFA.generateSecret();
const counter: number = Math.floor(Date.now() / 30000);
const hotp = twoFA.generateHOTP(secret, (counter - 1));
const totp = twoFA.generateTOTP(secret, -1);
expect(totp).toBe(hotp);
});
test('should generate future token with arg 1', () => {
const secret = twoFA.generateSecret();
const counter: number = Math.floor(Date.now() / 30000);
const hotp = twoFA.generateHOTP(secret, (counter + 1));
const totp = twoFA.generateTOTP(secret, 1);
expect(totp).toBe(hotp);
});
});
describe('verifyTOTP()', () => {
test('should return boolean', () => {
const secret = twoFA.generateSecret();
const verify = twoFA.verifyTOTP('111111', secret);
expect(typeof verify).toBe('boolean');
});
test('should return false with incorrect token', () => {
const secret = twoFA.generateSecret();
const verify = twoFA.verifyTOTP('111111', secret);
expect(verify).toBeFalsy();
});
test('should return false with incorrect secret', () => {
const secret = twoFA.generateSecret();
const secret2 = twoFA.generateSecret();
const totp = twoFA.generateTOTP(secret);
const verify = twoFA.verifyTOTP(totp, secret2);
expect(verify).toBeFalsy();
});
test('should return false with too large window size', () => {
const secret = twoFA.generateSecret();
const totp = twoFA.generateTOTP(secret);
const verify = twoFA.verifyTOTP(totp, secret, 15);
expect(verify).toBeFalsy();
});
test('should return true with correct present token', () => {
const secret = twoFA.generateSecret();
const totp = twoFA.generateTOTP(secret);
const verify = twoFA.verifyTOTP(totp, secret);
expect(verify).toBeTruthy();
});
test('should return true with correct past token', () => {
const secret = twoFA.generateSecret();
const totp = twoFA.generateTOTP(secret, -1);
const verify = twoFA.verifyTOTP(totp, secret);
expect(verify).toBeTruthy();
});
test('should return true with correct future token', () => {
const secret = twoFA.generateSecret();
const totp = twoFA.generateTOTP(secret, 1);
const verify = twoFA.verifyTOTP(totp, secret);
expect(verify).toBeTruthy();
});
});
});