Skip to content

Commit 11216b8

Browse files
committed
x509cert: simplify test cases for Certificate.load_file
Remove files from test/openssl/fixtures/pkey/ which are not pkeys. The test cases for OpenSSL::X509::Certificate.load_file can simply use issue_cert and Tempfile.
1 parent 49f9fd0 commit 11216b8

File tree

7 files changed

+35
-77
lines changed

7 files changed

+35
-77
lines changed
-1.29 KB
Binary file not shown.

test/openssl/fixtures/pkey/empty.der

Whitespace-only changes.

test/openssl/fixtures/pkey/empty.pem

Whitespace-only changes.

test/openssl/fixtures/pkey/fullchain.pem

Lines changed: 0 additions & 56 deletions
This file was deleted.

test/openssl/fixtures/pkey/garbage.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/openssl/test_x509cert.rb

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -370,34 +370,53 @@ def test_marshal
370370
end
371371

372372
def test_load_file_empty_pem
373-
empty_path = Fixtures.file_path("pkey", "empty.pem")
374-
assert_raise(OpenSSL::X509::CertificateError) do
375-
OpenSSL::X509::Certificate.load_file(empty_path)
373+
Tempfile.create("empty.pem") do |f|
374+
f.close
375+
376+
assert_raise(OpenSSL::X509::CertificateError) do
377+
OpenSSL::X509::Certificate.load_file(f.path)
378+
end
376379
end
377380
end
378381

379382
def test_load_file_fullchain_pem
380-
fullchain_path = Fixtures.file_path("pkey", "fullchain.pem")
381-
certificates = OpenSSL::X509::Certificate.load_file(fullchain_path)
382-
assert_equal 2, certificates.size
383-
assert_equal "/CN=www.codeotaku.com", certificates[0].subject.to_s
384-
assert_equal "/C=US/O=Let's Encrypt/CN=R3", certificates[1].subject.to_s
383+
cert1 = issue_cert(@ee1, @rsa2048, 1, [], nil, nil)
384+
cert2 = issue_cert(@ca, @rsa2048, 1, [], nil, nil)
385+
386+
Tempfile.create("fullchain.pem") do |f|
387+
f.puts cert1.to_pem
388+
f.puts cert2.to_pem
389+
f.close
390+
391+
certificates = OpenSSL::X509::Certificate.load_file(f.path)
392+
assert_equal 2, certificates.size
393+
assert_equal @ee1, certificates[0].subject
394+
assert_equal @ca, certificates[1].subject
395+
end
385396
end
386397

387398
def test_load_file_certificate_der
388-
fullchain_path = Fixtures.file_path("pkey", "certificate.der")
389-
certificates = OpenSSL::X509::Certificate.load_file(fullchain_path)
399+
cert = issue_cert(@ca, @rsa2048, 1, [], nil, nil)
400+
Tempfile.create("certificate.der", binmode: true) do |f|
401+
f.write cert.to_der
402+
f.close
390403

391-
# DER encoding can only contain one certificate:
392-
assert_equal 1, certificates.size
393-
assert_equal "/CN=www.codeotaku.com", certificates[0].subject.to_s
404+
certificates = OpenSSL::X509::Certificate.load_file(f.path)
405+
406+
# DER encoding can only contain one certificate:
407+
assert_equal 1, certificates.size
408+
assert_equal cert.to_der, certificates[0].to_der
409+
end
394410
end
395411

396412
def test_load_file_fullchain_garbage
397-
fullchain_path = Fixtures.file_path("pkey", "garbage.txt")
413+
Tempfile.create("garbage.txt") do |f|
414+
f.puts "not a certificate"
415+
f.close
398416

399-
assert_raise(OpenSSL::X509::CertificateError) do
400-
OpenSSL::X509::Certificate.load_file(fullchain_path)
417+
assert_raise(OpenSSL::X509::CertificateError) do
418+
OpenSSL::X509::Certificate.load_file(f.path)
419+
end
401420
end
402421
end
403422

test/openssl/utils.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ def read_file(category, name)
2424
@file_cache[[category, name]] ||=
2525
File.read(File.join(__dir__, "fixtures", category, name + ".pem"))
2626
end
27-
28-
def file_path(category, name)
29-
File.join(__dir__, "fixtures", category, name)
30-
end
3127
end
3228

3329
module_function

0 commit comments

Comments
 (0)