-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcrit_spec.rb
More file actions
79 lines (65 loc) · 2.83 KB
/
Copy pathcrit_spec.rb
File metadata and controls
79 lines (65 loc) · 2.83 KB
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
# frozen_string_literal: true
require 'spec_helper'
describe JWE do
describe '.check_crit' do
context 'when crit header is not present' do
it 'does not raise an error' do
header = { alg: 'RSA-OAEP', enc: 'A128GCM' }
expect { JWE.check_crit(header) }.not_to raise_error
end
end
context 'when crit header is present' do
context 'with valid critical headers' do
before do
JWE.supported_critical_headers = ['custom-header']
end
after do
JWE.supported_critical_headers = []
end
it 'accepts supported critical headers that exist in the header' do
header = { alg: 'RSA-OAEP', enc: 'A128GCM', crit: ['custom-header'], 'custom-header' => 'value' }
expect { JWE.check_crit(header) }.not_to raise_error
end
end
context 'with invalid critical headers' do
it 'raises an error when crit is not an array' do
header = { alg: 'RSA-OAEP', enc: 'A128GCM', crit: 'not-an-array' }
expect { JWE.check_crit(header) }.to raise_error(ArgumentError, /"crit" header must be a non-empty array/)
end
it 'raises an error when crit is an empty array' do
header = { alg: 'RSA-OAEP', enc: 'A128GCM', crit: [] }
expect { JWE.check_crit(header) }.to raise_error(ArgumentError, /"crit" header must be a non-empty array/)
end
it 'raises an error when crit contains a registered header' do
header = { alg: 'RSA-OAEP', enc: 'A128GCM', crit: ['alg'] }
expect { JWE.check_crit(header) }.to raise_error(ArgumentError, /registered header/)
end
it 'raises an error when crit references a non-existent header' do
header = { alg: 'RSA-OAEP', enc: 'A128GCM', crit: ['missing-header'] }
expect { JWE.check_crit(header) }.to raise_error(ArgumentError, /not present in header/)
end
it 'raises an error when crit contains an unsupported header' do
header = { alg: 'RSA-OAEP', enc: 'A128GCM', crit: ['unsupported'], 'unsupported' => 'value' }
expect { JWE.check_crit(header) }.to raise_error(JWE::InvalidData, /Unsupported critical header/)
end
end
end
end
describe 'encryption/decryption with crit header' do
let(:key) { OpenSSL::PKey::RSA.generate(2048) }
let(:plaintext) { 'Hello, World!' }
context 'with supported critical headers' do
before do
JWE.supported_critical_headers = ['custom-header']
end
after do
JWE.supported_critical_headers = []
end
it 'successfully encrypts and decrypts with crit header' do
encrypted = JWE.encrypt(plaintext, key, crit: ['custom-header'], 'custom-header': 'value')
decrypted = JWE.decrypt(encrypted, key)
expect(decrypted).to eq(plaintext)
end
end
end
end