Skip to content

Commit b5a8f7d

Browse files
authored
Make encryptor error-wrapping test deterministic (cloudfoundry#5110)
The "raises an EncryptorError" test relied on AES-CBC + PKCS#7 producing invalid padding when decrypting with the wrong key. With a random plaintext, padding is coincidentally valid ~1/256 of the time (dominated by the last byte being 0x01), so cipher.final returns garbage instead of raising and the test flakes. Stub OpenSSL::Cipher#final to raise CipherError so the test exercises the rescue/wrap logic in run_cipher deterministically. Add a parallel test in the "no key label" context, since the same wrapping applies there.
1 parent cb73f4b commit b5a8f7d

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

spec/unit/lib/cloud_controller/encryptor_spec.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ module VCAP::CloudController
161161

162162
expect(result).not_to eq(unencrypted_string)
163163
end
164+
165+
it 'wraps OpenSSL::Cipher::CipherError as an EncryptorError' do
166+
encrypted_string = Encryptor.encrypt(unencrypted_string, salt)
167+
allow_any_instance_of(OpenSSL::Cipher).to receive(:final).and_raise(OpenSSL::Cipher::CipherError, 'bad decrypt')
168+
169+
expect do
170+
Encryptor.decrypt(encrypted_string, salt, iterations: encryption_iterations)
171+
end.to raise_error(VCAP::CloudController::Encryptor::EncryptorError, %r{Encryption/Decryption failed: bad decrypt})
172+
end
164173
end
165174

166175
context 'when the wrong label is passed for decryption' do
@@ -176,13 +185,13 @@ module VCAP::CloudController
176185
expect(result).not_to eq(unencrypted_string)
177186
end
178187

179-
it 'raises an EncryptorError' do
180-
allow(Encryptor).to receive(:current_encryption_key_label).and_return('foo')
188+
it 'wraps OpenSSL::Cipher::CipherError as an EncryptorError' do
181189
encrypted_string = Encryptor.encrypt(unencrypted_string, salt)
190+
allow_any_instance_of(OpenSSL::Cipher).to receive(:final).and_raise(OpenSSL::Cipher::CipherError, 'bad decrypt')
182191

183192
expect do
184193
Encryptor.decrypt(encrypted_string, salt, label: 'bar', iterations: encryption_iterations)
185-
end.to raise_error(VCAP::CloudController::Encryptor::EncryptorError, %r{Encryption/Decryption failed: })
194+
end.to raise_error(VCAP::CloudController::Encryptor::EncryptorError, %r{Encryption/Decryption failed: bad decrypt})
186195
end
187196
end
188197
end

0 commit comments

Comments
 (0)