2012-02-29 00:20:53 +00:00
|
|
|
import os
|
|
|
|
from libmproxy import certutils
|
2012-06-09 01:42:43 +00:00
|
|
|
import tutils
|
2012-02-29 00:20:53 +00:00
|
|
|
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
def test_dummy_ca():
|
|
|
|
with tutils.tmpdir() as d:
|
2012-02-29 00:20:53 +00:00
|
|
|
path = os.path.join(d, "foo/cert.cnf")
|
|
|
|
assert certutils.dummy_ca(path)
|
|
|
|
assert os.path.exists(path)
|
|
|
|
|
|
|
|
path = os.path.join(d, "foo/cert2.pem")
|
|
|
|
assert certutils.dummy_ca(path)
|
|
|
|
assert os.path.exists(path)
|
|
|
|
assert os.path.exists(os.path.join(d, "foo/cert2-cert.pem"))
|
|
|
|
assert os.path.exists(os.path.join(d, "foo/cert2-cert.p12"))
|
|
|
|
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestDummyCert:
|
2012-02-29 00:20:53 +00:00
|
|
|
def test_with_ca(self):
|
2012-06-09 01:42:43 +00:00
|
|
|
with tutils.tmpdir() as d:
|
|
|
|
cacert = os.path.join(d, "foo/cert.cnf")
|
|
|
|
assert certutils.dummy_ca(cacert)
|
|
|
|
p = certutils.dummy_cert(
|
|
|
|
os.path.join(d, "foo"),
|
|
|
|
cacert,
|
|
|
|
"foo.com",
|
|
|
|
["one.com", "two.com", "*.three.com"]
|
|
|
|
)
|
|
|
|
assert os.path.exists(p)
|
2012-02-29 00:20:53 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
# Short-circuit
|
|
|
|
assert certutils.dummy_cert(
|
|
|
|
os.path.join(d, "foo"),
|
|
|
|
cacert,
|
|
|
|
"foo.com",
|
|
|
|
[]
|
|
|
|
)
|
2012-02-29 00:20:53 +00:00
|
|
|
|
|
|
|
def test_no_ca(self):
|
2012-06-09 01:42:43 +00:00
|
|
|
with tutils.tmpdir() as d:
|
|
|
|
p = certutils.dummy_cert(
|
|
|
|
d,
|
|
|
|
None,
|
|
|
|
"foo.com",
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
assert os.path.exists(p)
|
2012-02-29 00:20:53 +00:00
|
|
|
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestSSLCert:
|
2012-02-29 00:20:53 +00:00
|
|
|
def test_simple(self):
|
2012-06-09 01:42:43 +00:00
|
|
|
c = certutils.SSLCert(file(tutils.test_data.path("data/text_cert"), "r").read())
|
2012-03-04 21:22:47 +00:00
|
|
|
assert c.cn == "google.com"
|
|
|
|
assert len(c.altnames) == 436
|
2012-02-29 00:20:53 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
c = certutils.SSLCert(file(tutils.test_data.path("data/text_cert_2"), "r").read())
|
2012-03-04 21:22:47 +00:00
|
|
|
assert c.cn == "www.inode.co.nz"
|
|
|
|
assert len(c.altnames) == 2
|
2012-04-02 04:19:00 +00:00
|
|
|
assert c.digest("sha1")
|
|
|
|
assert c.notbefore
|
|
|
|
assert c.notafter
|
|
|
|
assert c.subject
|
|
|
|
assert c.keyinfo == ("RSA", 2048)
|
|
|
|
assert c.serial
|
2012-04-02 23:10:25 +00:00
|
|
|
assert c.issuer
|
2012-04-02 04:19:00 +00:00
|
|
|
c.has_expired
|
|
|
|
|
|
|
|
def test_der(self):
|
2012-06-09 01:42:43 +00:00
|
|
|
d = file(tutils.test_data.path("data/dercert")).read()
|
2012-04-02 04:19:00 +00:00
|
|
|
s = certutils.SSLCert.from_der(d)
|
|
|
|
assert s.cn
|