-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
gpg.test.ts
182 lines (165 loc) · 6.34 KB
/
gpg.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
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
import {describe, expect, it} from '@jest/globals';
import * as fs from 'fs';
import * as gpg from '../src/gpg';
import {parseKeygripFromGpgColonsOutput} from '../src/gpg';
const userInfos = [
{
key: 'test-key',
pgp: fs.readFileSync('__tests__/fixtures/test-key.pgp', {
encoding: 'utf8',
flag: 'r'
}),
pgp_base64: fs.readFileSync('__tests__/fixtures/test-key-base64.pgp', {
encoding: 'utf8',
flag: 'r'
}),
passphrase: fs.readFileSync('__tests__/fixtures/test-key.pass', {
encoding: 'utf8',
flag: 'r'
}),
name: 'Joe Tester',
email: 'joe@foo.bar',
keyID: '7D851EB72D73BDA0',
primary_key_fingerprint: '27571A53B86AF0C799B38BA77D851EB72D73BDA0',
fingerprint: '27571A53B86AF0C799B38BA77D851EB72D73BDA0',
fingerprints: ['27571A53B86AF0C799B38BA77D851EB72D73BDA0', '5A282E1460C0BC419615D34DD523BD50DD70B0BA'],
keygrips: ['3E2D1142AA59E08E16B7E2C64BA6DDC773B1A627', 'BA83FC8947213477F28ADC019F6564A956456163']
},
{
key: 'test-subkey',
pgp: fs.readFileSync('__tests__/fixtures/test-subkey.pgp', {
encoding: 'utf8',
flag: 'r'
}),
pgp_base64: fs.readFileSync('__tests__/fixtures/test-subkey-base64.pgp', {
encoding: 'utf8',
flag: 'r'
}),
passphrase: fs.readFileSync('__tests__/fixtures/test-subkey.pass', {
encoding: 'utf8',
flag: 'r'
}),
name: 'Joe Bar',
email: 'joe@bar.foo',
keyID: '6071D218380FDCC8',
primary_key_fingerprint: '87F257B89CE462100BEC0FFE6071D218380FDCC8',
fingerprint: 'C17D11ADF199F12A30A0910F1F80449BE0B08CB8',
fingerprints: ['87F257B89CE462100BEC0FFE6071D218380FDCC8', 'C17D11ADF199F12A30A0910F1F80449BE0B08CB8'],
keygrips: ['F5C3ABFAAB36B427FD98C4EDD0387E08EA1E8092', 'DEE0FC98F441519CA5DE5D79773CB29009695FEB']
}
];
describe('getVersion', () => {
it('returns GnuPG and libgcrypt version', async () => {
await gpg.getVersion().then(version => {
expect(version.gnupg).not.toEqual('');
expect(version.libgcrypt).not.toEqual('');
});
});
});
describe('getDirs', () => {
it('returns GnuPG dirs', async () => {
await gpg.getDirs().then(dirs => {
expect(dirs.libdir).not.toEqual('');
expect(dirs.datadir).not.toEqual('');
expect(dirs.homedir).not.toEqual('');
});
});
});
describe('configureAgent', () => {
// eslint-disable-next-line jest/expect-expect
it('configures GnuPG agent', async () => {
await gpg.configureAgent(gpg.agentConfig);
});
});
for (const userInfo of userInfos) {
// eslint-disable-next-line jest/valid-title
describe(userInfo.key, () => {
describe('importKey', () => {
it('imports key (as armored string) to GnuPG', async () => {
await gpg.importKey(userInfo.pgp).then(output => {
expect(output).not.toEqual('');
});
});
it('imports key (as base64 string) to GnuPG', async () => {
await gpg.importKey(userInfo.pgp_base64).then(output => {
expect(output).not.toEqual('');
});
});
});
describe('getKeygrips', () => {
it('returns the keygrips', async () => {
await gpg.importKey(userInfo.pgp);
await gpg.getKeygrips(userInfo.fingerprint).then(keygrips => {
expect(keygrips.length).toEqual(userInfo.keygrips.length);
for (let i = 0; i < keygrips.length; i++) {
expect(keygrips[i]).toEqual(userInfo.keygrips[i]);
}
});
});
});
describe('getKeygrip', () => {
it('returns the keygrip for a given fingerprint', async () => {
await gpg.importKey(userInfo.pgp);
for (const {idx, fingerprint} of userInfo.fingerprints.map((fingerprint, idx) => ({idx, fingerprint}))) {
await gpg.getKeygrip(fingerprint).then(keygrip => {
expect(keygrip.length).toEqual(userInfo.keygrips[idx].length);
expect(keygrip).toEqual(userInfo.keygrips[idx]);
});
}
});
});
describe('presetPassphrase', () => {
it('presets passphrase', async () => {
await gpg.importKey(userInfo.pgp);
await gpg.configureAgent(gpg.agentConfig);
for (const keygrip of await gpg.getKeygrips(userInfo.fingerprint)) {
await gpg.presetPassphrase(keygrip, userInfo.passphrase).then(output => {
expect(output).not.toEqual('');
});
}
});
});
describe('setTrustLevel', () => {
it('set trust level', async () => {
await gpg.importKey(userInfo.pgp);
await gpg.configureAgent(gpg.agentConfig);
expect(() => {
gpg.setTrustLevel(userInfo.keyID, '5');
}).not.toThrow();
});
});
describe('deleteKey', () => {
// eslint-disable-next-line jest/expect-expect
it('removes key from GnuPG', async () => {
await gpg.importKey(userInfo.pgp);
await gpg.deleteKey(userInfo.primary_key_fingerprint);
});
});
});
}
describe('killAgent', () => {
// eslint-disable-next-line jest/expect-expect
it('kills GnuPG agent', async () => {
await gpg.killAgent();
});
});
describe('parseKeygripFromGpgColonsOutput', () => {
it('returns the keygrip of a given fingerprint from a GPG command output using the option: --with-colons', async () => {
const outputUsingTestKey = fs.readFileSync('__tests__/fixtures/test-key-gpg-output.txt', {
encoding: 'utf8',
flag: 'r'
});
const keygripPrimaryTestKey = parseKeygripFromGpgColonsOutput(outputUsingTestKey, '27571A53B86AF0C799B38BA77D851EB72D73BDA0');
expect(keygripPrimaryTestKey).toBe('3E2D1142AA59E08E16B7E2C64BA6DDC773B1A627');
const keygripSubkeyTestKey = parseKeygripFromGpgColonsOutput(outputUsingTestKey, '5A282E1460C0BC419615D34DD523BD50DD70B0BA');
expect(keygripSubkeyTestKey).toBe('BA83FC8947213477F28ADC019F6564A956456163');
const outputUsingTestSubkey = fs.readFileSync('__tests__/fixtures/test-subkey-gpg-output.txt', {
encoding: 'utf8',
flag: 'r'
});
const keygripPrimaryTestSubkey = parseKeygripFromGpgColonsOutput(outputUsingTestSubkey, '87F257B89CE462100BEC0FFE6071D218380FDCC8');
expect(keygripPrimaryTestSubkey).toBe('F5C3ABFAAB36B427FD98C4EDD0387E08EA1E8092');
const keygripSubkeyTestSubkey = parseKeygripFromGpgColonsOutput(outputUsingTestSubkey, 'C17D11ADF199F12A30A0910F1F80449BE0B08CB8');
expect(keygripSubkeyTestSubkey).toBe('DEE0FC98F441519CA5DE5D79773CB29009695FEB');
});
});