Projekt

Obecné

Profil

Stáhnout (5.54 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
var assert  = require('assert');
2
var forge   = require('node-forge');
3
var fs      = require('fs');
4
var exec    = require('child_process').exec;
5
6
describe('generate', function () {
7
8
  var generate = require('../index').generate;
9
10
  it('should work without attrs/options', function (done) {
11
    var pems = generate();
12
    assert.ok(!!pems.private, 'has a private key');
13
    assert.ok(!!pems.fingerprint, 'has fingerprint');
14
    assert.ok(!!pems.public, 'has a public key');
15
    assert.ok(!!pems.cert, 'has a certificate');
16
    assert.ok(!pems.pkcs7, 'should not include a pkcs7 by default');
17
    assert.ok(!pems.clientcert, 'should not include a client cert by default');
18
    assert.ok(!pems.clientprivate, 'should not include a client private key by default');
19
    assert.ok(!pems.clientpublic, 'should not include a client public key by default');
20
21
    var caStore = forge.pki.createCaStore();
22
    caStore.addCertificate(pems.cert);
23
    done();
24
  });
25
26
  it('should generate client cert', function (done) {
27
    var pems = generate(null, {clientCertificate: true});
28
29
    assert.ok(!!pems.clientcert, 'should include a client cert when requested');
30
    assert.ok(!!pems.clientprivate, 'should include a client private key when requested');
31
    assert.ok(!!pems.clientpublic, 'should include a client public key when requested');
32
    done();
33
  });
34
35
  it('should include pkcs7', function (done) {
36
    var pems = generate([{ name: 'commonName', value: 'contoso.com' }], {pkcs7: true});
37
38
    assert.ok(!!pems.pkcs7, 'has a pkcs7');
39
40
    try {
41
      fs.unlinkSync('/tmp/tmp.pkcs7');
42
    } catch (er) {}
43
44
    fs.writeFileSync('/tmp/tmp.pkcs7', pems.pkcs7);
45
    exec('openssl pkcs7 -print_certs -in /tmp/tmp.pkcs7', function (err, stdout, stderr) {
46
      if (err) {
47
        return done(err);
48
      }
49
50
      const errorMessage = stderr.toString();
51
      if (errorMessage.length) {
52
        return done(new Error(errorMessage));
53
      }
54
55
      const expected = stdout.toString().replace(/\n/g, '\r\n'); //node-forge uses \r\n
56
      assert.equal(
57
        `subject=/CN=contoso.com\r\nissuer=/CN=contoso.com\r\n` +
58
          pems.cert +
59
          '\r\n',
60
        expected
61
      );
62
63
      done();
64
    });
65
  });
66
67
  it('should support sha1 algorithm', function (done) {
68
    var pems_sha1 = generate(null, { algorithm: 'sha1' });
69
    assert.ok(forge.pki.certificateFromPem(pems_sha1.cert).siginfo.algorithmOid === forge.pki.oids['sha1WithRSAEncryption'], 'can generate sha1 certs');
70
    done();
71
  });
72
73
  it('should support sha256 algorithm', function (done) {
74
    var pems_sha256 = generate(null, { algorithm: 'sha256' });
75
    assert.ok(forge.pki.certificateFromPem(pems_sha256.cert).siginfo.algorithmOid === forge.pki.oids['sha256WithRSAEncryption'], 'can generate sha256 certs');
76
    done();
77
  });
78
79
  describe('with callback', function () {
80
    it('should work without attrs/options', function (done) {
81
      generate(function (err, pems) {
82
        if (err) done(err);
83
        assert.ok(!!pems.private, 'has a private key');
84
        assert.ok(!!pems.public, 'has a public key');
85
        assert.ok(!!pems.cert, 'has a certificate');
86
        assert.ok(!pems.pkcs7, 'should not include a pkcs7 by default');
87
        assert.ok(!pems.clientcert, 'should not include a client cert by default');
88
        assert.ok(!pems.clientprivate, 'should not include a client private key by default');
89
        assert.ok(!pems.clientpublic, 'should not include a client public key by default');
90
        done();
91
      });
92
    });
93
94
    it('should generate client cert', function (done) {
95
      generate(null, {clientCertificate: true}, function (err, pems) {
96
        if (err) done(err);
97
        assert.ok(!!pems.clientcert, 'should include a client cert when requested');
98
        assert.ok(!!pems.clientprivate, 'should include a client private key when requested');
99
        assert.ok(!!pems.clientpublic, 'should include a client public key when requested');
100
        done();
101
      });
102
    });
103
104
    it('should include pkcs7', function (done) {
105
      generate([{ name: 'commonName', value: 'contoso.com' }], {pkcs7: true}, function (err, pems) {
106
        if (err) done(err);
107
        assert.ok(!!pems.pkcs7, 'has a pkcs7');
108
109
        try {
110
          fs.unlinkSync('/tmp/tmp.pkcs7');
111
        } catch (er) {}
112
113
        fs.writeFileSync('/tmp/tmp.pkcs7', pems.pkcs7);
114
        exec('openssl pkcs7 -print_certs -in /tmp/tmp.pkcs7', function (err, stdout, stderr) {
115
          if (err) {
116
            return done(err);
117
          }
118
119
          const errorMessage = stderr.toString();
120
          if (errorMessage.length) {
121
            return done(new Error(errorMessage));
122
          }
123
124
          const expected = stdout.toString().replace(/\n/g, '\r\n'); //node-forge uses \r\n
125
          assert.equal(
126
            `subject=/CN=contoso.com\r\nissuer=/CN=contoso.com\r\n` +
127
              pems.cert +
128
              '\r\n',
129
            expected
130
          );
131
132
          done();
133
        });
134
      });
135
    });
136
137
    it('should support sha1 algorithm', function (done) {
138
      generate(null, { algorithm: 'sha1' }, function (err, pems_sha1) {
139
        if (err) done(err);
140
        assert.ok(forge.pki.certificateFromPem(pems_sha1.cert).siginfo.algorithmOid === forge.pki.oids['sha1WithRSAEncryption'], 'can generate sha1 certs');
141
        done();
142
      });
143
    });
144
145
    it('should support sha256 algorithm', function (done) {
146
      generate(null, { algorithm: 'sha256' }, function (err, pems_sha256) {
147
        if (err) done(err);
148
        assert.ok(forge.pki.certificateFromPem(pems_sha256.cert).siginfo.algorithmOid === forge.pki.oids['sha256WithRSAEncryption'], 'can generate sha256 certs');
149
        done();
150
      });
151
    });
152
  });
153
});