2020-11-18 12:33:08 -07:00
|
|
|
/*
|
2021-01-13 05:20:33 -07:00
|
|
|
Copyright 2021 Yarmo Mackenbach
|
2020-11-18 12:33:08 -07:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
const chai = require('chai')
|
|
|
|
const expect = chai.expect
|
2020-12-05 15:13:44 -07:00
|
|
|
chai.use(require('chai-as-promised'))
|
2020-11-18 12:33:08 -07:00
|
|
|
|
2020-11-20 01:13:08 -07:00
|
|
|
const path = require('path')
|
2020-12-09 18:15:39 -07:00
|
|
|
const openpgp = require('openpgp')
|
2020-11-18 12:33:08 -07:00
|
|
|
const doipjs = require('../src')
|
|
|
|
|
2020-11-18 13:50:58 -07:00
|
|
|
const pubKeyFingerprint = `3637202523e7c1309ab79e99ef2dc5827b445f4b`
|
2020-12-05 15:13:44 -07:00
|
|
|
const pubKeyEmail = `test@doip.rocks`
|
2021-06-03 03:55:08 -06:00
|
|
|
|
2020-12-05 15:13:44 -07:00
|
|
|
const pubKeyPlaintext = `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
2020-11-18 12:33:08 -07:00
|
|
|
|
|
|
|
mQGNBF+036UBDACoxWRdp7rBAFB2l/+dxX0XA50NJC92EEacB5L0TnC0lP/MsNHv
|
|
|
|
fAv/A9vgTwrPudvcHdE/urAjQswfIU3LpFxbBOWNYWOv6ssrzBH4vVGMyxfu2GGu
|
|
|
|
b2mxjWj0eWXnWXnzkO5fscX2y0HqNjBZjDSkYohHZJTbz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o87GjESOd+QTuKdtQBzegLotgeNtCOFBKOoY2g+24FsbSbIm5H27vw/odgV
|
|
|
|
cqvy+yineO/jWCWp6pHbALSg1INuVnluwyAqXoM4Gx7rUboISN2nIzYpdjXAUgnX
|
|
|
|
XxFjll8b3+FRQAH80qkvtuDDZ/z2CQQ/mdJgNJdMwqvwBQZnCMts0PyqTlzw1mcy
|
|
|
|
x77L7mBkREbuZpFoD/c=
|
|
|
|
=w7qB
|
|
|
|
-----END PGP PUBLIC KEY BLOCK-----`
|
|
|
|
|
2021-06-03 03:55:08 -06:00
|
|
|
const pubKeyWithOtherNotations = `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
|
|
|
|
|
|
mDMEX9Mt6xYJKwYBBAHaRw8BAQdAch8jfp+8KHH5cy/t45GjPvl6dkEv2soIy9fo
|
|
|
|
Oe9DbP20EVlhcm1vJ3MgRXZpbCBUd2luiNsEExYIAIMCGwMFCwkIBwIGFQoJCAsC
|
|
|
|
BBYCAwECHgECF4AWIQTeePcduHH8EU2iM3aw5zJVrULhnwUCX9MuHBkUgAAAAAAN
|
|
|
|
AANldmlsQHlhcm1vLmV1eWVzMBSAAAAAABIAFXByb29mQG1ldGFjb2RlLmJpemRu
|
|
|
|
czp5YXJtby5ldT90eXBlPVRYVAAKCRCw5zJVrULhn4DtAQCVkyI8UxUbkxspXkWB
|
|
|
|
qUL+3uqCl9gTbNImhv/OxxJdEAEAqf8SJ9FSeAwgWhPHOidR1m+J6/qVdAJdp0HJ
|
|
|
|
Yn6RMQ8=
|
|
|
|
=Oo3X
|
|
|
|
-----END PGP PUBLIC KEY BLOCK-----`
|
|
|
|
|
|
|
|
|
2021-04-22 07:38:42 -06:00
|
|
|
describe('keys.fetchURI', () => {
|
2020-11-18 12:33:08 -07:00
|
|
|
it('should be a function (1 argument)', () => {
|
2021-04-22 07:38:42 -06:00
|
|
|
expect(doipjs.keys.fetchURI).to.be.a('function')
|
|
|
|
expect(doipjs.keys.fetchURI).to.have.length(1)
|
2020-11-18 12:33:08 -07:00
|
|
|
})
|
|
|
|
it('should return a Key object when provided a hkp: uri', async () => {
|
2020-12-05 15:13:44 -07:00
|
|
|
expect(
|
2021-04-22 07:38:42 -06:00
|
|
|
await doipjs.keys.fetchURI(`hkp:${pubKeyFingerprint}`)
|
2020-12-05 15:13:44 -07:00
|
|
|
).to.be.instanceOf(openpgp.key.Key)
|
2021-03-22 10:23:49 -06:00
|
|
|
}).timeout('12s')
|
2020-12-05 15:13:44 -07:00
|
|
|
it('should reject when provided an invalid uri', () => {
|
|
|
|
return expect(
|
2021-04-22 07:38:42 -06:00
|
|
|
doipjs.keys.fetchURI(`inv:${pubKeyFingerprint}`)
|
2020-12-05 15:13:44 -07:00
|
|
|
).to.eventually.be.rejectedWith('Invalid URI protocol')
|
2021-03-22 10:23:49 -06:00
|
|
|
}).timeout('12s')
|
2020-11-18 12:33:08 -07:00
|
|
|
})
|
|
|
|
|
2021-04-22 07:38:42 -06:00
|
|
|
describe('keys.fetchHKP', () => {
|
2020-11-18 12:33:08 -07:00
|
|
|
it('should be a function (2 arguments)', () => {
|
2021-04-22 07:38:42 -06:00
|
|
|
expect(doipjs.keys.fetchHKP).to.be.a('function')
|
|
|
|
expect(doipjs.keys.fetchHKP).to.have.length(2)
|
2020-11-18 12:33:08 -07:00
|
|
|
})
|
|
|
|
it('should return a Key object when provided a valid fingerprint', async () => {
|
2021-04-22 07:38:42 -06:00
|
|
|
expect(await doipjs.keys.fetchHKP(pubKeyFingerprint)).to.be.instanceOf(
|
2020-11-18 12:33:08 -07:00
|
|
|
openpgp.key.Key
|
|
|
|
)
|
|
|
|
})
|
|
|
|
it('should return a Key object when provided a valid email address', async () => {
|
2021-04-22 07:38:42 -06:00
|
|
|
expect(await doipjs.keys.fetchHKP(pubKeyEmail)).to.be.instanceOf(
|
2020-11-18 12:33:08 -07:00
|
|
|
openpgp.key.Key
|
|
|
|
)
|
|
|
|
})
|
2020-12-05 15:14:48 -07:00
|
|
|
it('should reject when provided an invalid fingerprint', async () => {
|
2020-12-05 15:13:44 -07:00
|
|
|
return expect(
|
2021-04-22 07:38:42 -06:00
|
|
|
doipjs.keys.fetchHKP('4637202523e7c1309ab79e99ef2dc5827b445f4b')
|
2020-12-05 15:13:44 -07:00
|
|
|
).to.eventually.be.rejectedWith(
|
|
|
|
'Key does not exist or could not be fetched'
|
|
|
|
)
|
2020-11-18 12:33:08 -07:00
|
|
|
})
|
2020-12-05 15:14:48 -07:00
|
|
|
it('should reject when provided an invalid email address', async () => {
|
2020-12-05 15:13:44 -07:00
|
|
|
return expect(
|
2021-04-22 07:38:42 -06:00
|
|
|
doipjs.keys.fetchHKP('invalid@doip.rocks')
|
2020-12-05 15:13:44 -07:00
|
|
|
).to.eventually.be.rejectedWith(
|
|
|
|
'Key does not exist or could not be fetched'
|
|
|
|
)
|
2020-11-18 12:33:08 -07:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-22 07:38:42 -06:00
|
|
|
describe('keys.fetchPlaintext', () => {
|
2020-11-18 12:33:08 -07:00
|
|
|
it('should be a function (1 argument)', () => {
|
2021-04-22 07:38:42 -06:00
|
|
|
expect(doipjs.keys.fetchPlaintext).to.be.a('function')
|
|
|
|
expect(doipjs.keys.fetchPlaintext).to.have.length(1)
|
2020-11-18 12:33:08 -07:00
|
|
|
})
|
|
|
|
it('should return a Key object', async () => {
|
2021-04-22 07:38:42 -06:00
|
|
|
expect(await doipjs.keys.fetchPlaintext(pubKeyPlaintext)).to.be.instanceOf(
|
2020-11-18 12:33:08 -07:00
|
|
|
openpgp.key.Key
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2020-11-18 13:50:58 -07:00
|
|
|
|
|
|
|
describe('keys.process', () => {
|
|
|
|
it('should be a function (1 argument)', () => {
|
|
|
|
expect(doipjs.keys.process).to.be.a('function')
|
|
|
|
expect(doipjs.keys.process).to.have.length(1)
|
|
|
|
})
|
|
|
|
it('should return an object with specific keys', async () => {
|
2021-04-22 07:38:42 -06:00
|
|
|
const pubKey = await doipjs.keys.fetchPlaintext(pubKeyPlaintext)
|
2020-11-18 13:50:58 -07:00
|
|
|
const obj = await doipjs.keys.process(pubKey)
|
2021-04-19 05:38:09 -06:00
|
|
|
expect(obj).to.have.keys([
|
|
|
|
'users',
|
|
|
|
'fingerprint',
|
|
|
|
'primaryUserIndex',
|
|
|
|
'key',
|
|
|
|
])
|
2020-11-18 13:50:58 -07:00
|
|
|
})
|
2021-06-03 03:55:08 -06:00
|
|
|
it('should ignore non-proof notations', async () => {
|
|
|
|
const pubKey = await doipjs.keys.fetchPlaintext(pubKeyWithOtherNotations)
|
|
|
|
const obj = await doipjs.keys.process(pubKey)
|
|
|
|
console.log(obj.users[0]);
|
|
|
|
expect(obj.users).to.be.lengthOf(1)
|
|
|
|
expect(obj.users[0].notations).to.be.lengthOf(1)
|
|
|
|
expect(obj.users[0].notations[0]).to.be.equal('dns:yarmo.eu?type=TXT')
|
|
|
|
})
|
2020-11-18 13:50:58 -07:00
|
|
|
})
|