Test certificate and key with a truncated middle

Test a valid certificate and valid key that have had 48 characters
removed from their middle, to simulate a malformed certificate and key.

Suggested by @DavidS in https://github.com/puppetlabs/puppetlabs-stdlib/pull/552
This commit is contained in:
Matt Bostock 2016-01-08 11:01:51 +00:00
parent 97320ab421
commit 4acba73b00

View file

@ -97,6 +97,14 @@ EOS
valid_key.gsub(/^/, ' ')
end
let(:malformed_cert) do
truncate_middle(valid_cert)
end
let(:malformed_key) do
truncate_middle(valid_key)
end
let(:bad_cert) do
'foo'
end
@ -126,6 +134,14 @@ EOS
it { is_expected.to run.with_params(valid_cert, valid_key_but_indented).and_raise_error(Puppet::ParseError, /Not a valid RSA key/) }
end
describe 'valid certificate, malformed key' do
it { is_expected.to run.with_params(valid_cert, malformed_key).and_raise_error(Puppet::ParseError, /Not a valid RSA key/) }
end
describe 'malformed certificate, valid key' do
it { is_expected.to run.with_params(malformed_cert, valid_key).and_raise_error(Puppet::ParseError, /Not a valid x509 certificate/) }
end
describe 'valid certificate, bad key' do
it { is_expected.to run.with_params(valid_cert, bad_key).and_raise_error(Puppet::ParseError, /Not a valid RSA key/) }
end
@ -151,4 +167,14 @@ EOS
it { is_expected.to run.with_params("baz", true).and_raise_error(Puppet::ParseError, /is not a string/) }
end
end
def truncate_middle(string)
chars_to_truncate = 48
middle = (string.length / 2).floor
start_pos = middle - (chars_to_truncate / 2)
end_pos = middle + (chars_to_truncate / 2)
string[start_pos...end_pos] = ''
return string
end
end