Skip to content

Commit 8075ae5

Browse files
committed
DRY unit tests
1 parent e63d8ce commit 8075ae5

File tree

1 file changed

+29
-106
lines changed

1 file changed

+29
-106
lines changed

test/xmlenc.encryptedkey.js

Lines changed: 29 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -8,132 +8,55 @@ var xpath = require('xpath');
88

99
describe('encrypt', function() {
1010

11-
it('should encrypt and decrypt xml (aes256-cbc)', function (done) {
12-
// cert created with:
13-
// openssl req -x509 -new -newkey rsa:2048 -nodes -subj '/CN=auth0.auth0.com/O=Auth0 LLC/C=US/ST=Washington/L=Redmond' -keyout auth0.key -out auth0.pem
14-
// pub key extracted from (only the RSA public key between BEGIN PUBLIC KEY and END PUBLIC KEY)
15-
// openssl x509 -in "test-auth0.pem" -pubkey
16-
17-
var options = {
18-
rsa_pub: fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
19-
pem: fs.readFileSync(__dirname + '/test-auth0.pem'),
20-
key: fs.readFileSync(__dirname + '/test-auth0.key'),
11+
var algorithms = [{
12+
name: 'aes-256-cbc',
13+
encryptionOptions: {
2114
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#aes256-cbc',
2215
keyEncryptionAlgorighm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
23-
};
24-
25-
xmlenc.encrypt('content to encrypt', options, function(err, result) {
26-
xmlenc.decrypt(result, { key: fs.readFileSync(__dirname + '/test-auth0.key')}, function(err, decrypted) {
27-
assert.equal(decrypted, 'content to encrypt');
28-
done();
29-
});
30-
});
31-
});
32-
33-
it('should encrypt and decrypt xml (aes256-cbc with utf8 chars)', function (done) {
34-
// cert created with:
35-
// openssl req -x509 -new -newkey rsa:2048 -nodes -subj '/CN=auth0.auth0.com/O=Auth0 LLC/C=US/ST=Washington/L=Redmond' -keyout auth0.key -out auth0.pem
36-
// pub key extracted from (only the RSA public key between BEGIN PUBLIC KEY and END PUBLIC KEY)
37-
// openssl x509 -in "test-auth0.pem" -pubkey
38-
39-
var options = {
40-
rsa_pub: fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
41-
pem: fs.readFileSync(__dirname + '/test-auth0.pem'),
42-
key: fs.readFileSync(__dirname + '/test-auth0.key'),
43-
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#aes256-cbc',
44-
keyEncryptionAlgorighm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
45-
};
46-
47-
xmlenc.encrypt('Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge', options, function(err, result) {
48-
xmlenc.decrypt(result, { key: fs.readFileSync(__dirname + '/test-auth0.key')}, function(err, decrypted) {
49-
assert.equal(decrypted, 'Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge');
50-
done();
51-
});
52-
});
53-
});
54-
55-
it('should encrypt and decrypt xml (aes128-cbc) with utf8 chars', function (done) {
56-
// cert created with:
57-
// openssl req -x509 -new -newkey rsa:2048 -nodes -subj '/CN=auth0.auth0.com/O=Auth0 LLC/C=US/ST=Washington/L=Redmond' -keyout auth0.key -out auth0.pem
58-
// pub key extracted from (only the RSA public key between BEGIN PUBLIC KEY and END PUBLIC KEY)
59-
// openssl x509 -in "test-auth0.pem" -pubkey
60-
61-
var options = {
62-
rsa_pub: fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
63-
pem: fs.readFileSync(__dirname + '/test-auth0.pem'),
64-
key: fs.readFileSync(__dirname + '/test-auth0.key'),
16+
}
17+
}, {
18+
name: 'aes-128-cbc',
19+
encryptionOptions: {
6520
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#aes128-cbc',
6621
keyEncryptionAlgorighm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
67-
};
22+
}
23+
}, {
24+
name: 'des-ede3-cbc',
25+
encryptionOptions: {
26+
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc',
27+
keyEncryptionAlgorighm: 'http://www.w3.org/2001/04/xmlenc#rsa-1_5'
28+
}
29+
}];
6830

69-
xmlenc.encrypt('Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge', options, function (err, result) {
70-
xmlenc.decrypt(result, { key: fs.readFileSync(__dirname + '/test-auth0.key')}, function (err, decrypted) {
71-
assert.equal(decrypted, 'Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge');
72-
done();
31+
algorithms.forEach(function (algorithm) {
32+
describe(algorithm.name, function () {
33+
it('should encrypt and decrypt xml', function (done) {
34+
_shouldEncryptAndDecrypt('content to encrypt', algorithm.encryptionOptions, done);
7335
});
74-
});
75-
});
76-
77-
it('should encrypt and decrypt xml (aes128-cbc)', function (done) {
78-
var options = {
79-
rsa_pub: fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
80-
pem: fs.readFileSync(__dirname + '/test-auth0.pem'),
81-
key: fs.readFileSync(__dirname + '/test-auth0.key'),
82-
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#aes128-cbc',
83-
keyEncryptionAlgorighm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
84-
};
8536

86-
xmlenc.encrypt('content to encrypt', options, function (err, result) {
87-
xmlenc.decrypt(result, { key: fs.readFileSync(__dirname + '/test-auth0.key')}, function (err, decrypted) {
88-
assert.equal(decrypted, 'content to encrypt');
89-
done();
37+
it('should encrypt and decrypt xml with utf8 chars', function (done) {
38+
_shouldEncryptAndDecrypt('Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge', algorithm.encryptionOptions, done);
9039
});
9140
});
9241
});
9342

94-
it('should encrypt and decrypt xml (encryption: http://www.w3.org/2001/04/xmlenc#tripledes-cbc, keyEncryption: http://www.w3.org/2001/04/xmlenc#rsa-1_5)', function (done) {
43+
function _shouldEncryptAndDecrypt(content, options, done) {
9544
// cert created with:
9645
// openssl req -x509 -new -newkey rsa:2048 -nodes -subj '/CN=auth0.auth0.com/O=Auth0 LLC/C=US/ST=Washington/L=Redmond' -keyout auth0.key -out auth0.pem
9746
// pub key extracted from (only the RSA public key between BEGIN PUBLIC KEY and END PUBLIC KEY)
9847
// openssl x509 -in "test-auth0.pem" -pubkey
9948

100-
var options = {
101-
rsa_pub: fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
102-
pem: fs.readFileSync(__dirname + '/test-auth0.pem'),
103-
key: fs.readFileSync(__dirname + '/test-auth0.key'),
104-
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc',
105-
keyEncryptionAlgorighm: 'http://www.w3.org/2001/04/xmlenc#rsa-1_5'
106-
};
49+
options.rsa_pub = fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
50+
options.pem = fs.readFileSync(__dirname + '/test-auth0.pem'),
51+
options.key = fs.readFileSync(__dirname + '/test-auth0.key'),
10752

108-
xmlenc.encrypt('content to encrypt', options, function(err, result) {
109-
xmlenc.decrypt(result, { key: fs.readFileSync(__dirname + '/test-auth0.key')}, function(err, decrypted) {
110-
assert.equal(decrypted, 'content to encrypt');
53+
xmlenc.encrypt(content, options, function(err, result) {
54+
xmlenc.decrypt(result, { key: fs.readFileSync(__dirname + '/test-auth0.key')}, function (err, decrypted) {
55+
assert.equal(decrypted, content);
11156
done();
11257
});
11358
});
114-
});
115-
116-
it('should encrypt and decrypt xml (encryption: http://www.w3.org/2001/04/xmlenc#tripledes-cbc, keyEncryption: http://www.w3.org/2001/04/xmlenc#rsa-1_5)', function (done) {
117-
// cert created with:
118-
// openssl req -x509 -new -newkey rsa:2048 -nodes -subj '/CN=auth0.auth0.com/O=Auth0 LLC/C=US/ST=Washington/L=Redmond' -keyout auth0.key -out auth0.pem
119-
// pub key extracted from (only the RSA public key between BEGIN PUBLIC KEY and END PUBLIC KEY)
120-
// openssl x509 -in "test-auth0.pem" -pubkey
121-
122-
var options = {
123-
rsa_pub: fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
124-
pem: fs.readFileSync(__dirname + '/test-auth0.pem'),
125-
key: fs.readFileSync(__dirname + '/test-auth0.key'),
126-
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc',
127-
keyEncryptionAlgorighm: 'http://www.w3.org/2001/04/xmlenc#rsa-1_5'
128-
};
129-
130-
xmlenc.encrypt('Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge', options, function (err, result) {
131-
xmlenc.decrypt(result, { key: fs.readFileSync(__dirname + '/test-auth0.key')}, function (err, decrypted) {
132-
assert.equal(decrypted, 'Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge');
133-
done();
134-
});
135-
});
136-
});
59+
}
13760

13861
it('should encrypt and decrypt keyinfo', function (done) {
13962
var options = {

0 commit comments

Comments
 (0)