diff options
author | avi@google.com <avi@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-19 14:11:17 +0000 |
---|---|---|
committer | avi@google.com <avi@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-19 14:11:17 +0000 |
commit | 59fa437a67700abc30411251ef8c6efe69bb347f (patch) | |
tree | 694465988cdcb2c31ea02176f3d64604b188c086 /net | |
parent | fd6182cba02a9e95ca7e51e87f0adc0fdc436061 (diff) | |
download | chromium_src-59fa437a67700abc30411251ef8c6efe69bb347f.zip chromium_src-59fa437a67700abc30411251ef8c6efe69bb347f.tar.gz chromium_src-59fa437a67700abc30411251ef8c6efe69bb347f.tar.bz2 |
Add a byte factory method for unit testing.
Review URL: http://codereview.chromium.org/2975
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2403 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/x509_certificate.h | 4 | ||||
-rw-r--r-- | net/base/x509_certificate_mac.cc | 21 | ||||
-rw-r--r-- | net/base/x509_certificate_win.cc | 50 |
3 files changed, 46 insertions, 29 deletions
diff --git a/net/base/x509_certificate.h b/net/base/x509_certificate.h index 645454e..763084b 100644 --- a/net/base/x509_certificate.h +++ b/net/base/x509_certificate.h @@ -110,6 +110,10 @@ class X509Certificate : public base::RefCountedThreadSafe<X509Certificate> { // X509Certificate will properly dispose of |cert_handle| for you. static X509Certificate* CreateFromHandle(OSCertHandle cert_handle); + // Create an X509Certificate from the BER-encoded representation. + // Returns NULL on failure. + static X509Certificate* CreateFromBytes(const char* data, int length); + // Create an X509Certificate from the representation stored in the given // pickle. The data for this object is found relative to the given // pickle_iter, which should be passed to the pickle's various Read* methods. diff --git a/net/base/x509_certificate_mac.cc b/net/base/x509_certificate_mac.cc index 59964af..66e491a 100644 --- a/net/base/x509_certificate_mac.cc +++ b/net/base/x509_certificate_mac.cc @@ -224,7 +224,7 @@ class X509Certificate::Cache { // Obtain an instance of X509Certificate::Cache via GetInstance(). Cache() { } - friend class DefaultSingletonTraits<X509Certificate::Cache>; + friend struct DefaultSingletonTraits<X509Certificate::Cache>; // You must acquire this lock before using any private data of this object. // You must not block while holding this lock. @@ -272,12 +272,8 @@ X509Certificate* X509Certificate::CreateFromHandle(OSCertHandle cert_handle) { } // static -X509Certificate* X509Certificate::CreateFromPickle(const Pickle& pickle, - void** pickle_iter) { - const char* data; - int length; - if (!pickle.ReadData(pickle_iter, &data, &length)) - return NULL; +X509Certificate* X509Certificate::CreateFromBytes(const char* data, + int length) { CSSM_DATA cert_data; cert_data.Data = const_cast<uint8*>(reinterpret_cast<const uint8*>(data)); cert_data.Length = length; @@ -293,6 +289,17 @@ X509Certificate* X509Certificate::CreateFromPickle(const Pickle& pickle, return CreateFromHandle(cert_handle); } +// static +X509Certificate* X509Certificate::CreateFromPickle(const Pickle& pickle, + void** pickle_iter) { + const char* data; + int length; + if (!pickle.ReadData(pickle_iter, &data, &length)) + return NULL; + + return CreateFromBytes(data, length); +} + X509Certificate::X509Certificate(OSCertHandle cert_handle) : cert_handle_(cert_handle) { Initialize(); diff --git a/net/base/x509_certificate_win.cc b/net/base/x509_certificate_win.cc index ab3fe64..c54ad07 100644 --- a/net/base/x509_certificate_win.cc +++ b/net/base/x509_certificate_win.cc @@ -24,7 +24,7 @@ namespace { // Returns true if this cert fingerprint is the null (all zero) fingerprint. // We use this as a bogus fingerprint value. bool IsNullFingerprint(const X509Certificate::Fingerprint& fingerprint) { - for (int i = 0; i < arraysize(fingerprint.data); ++i) { + for (size_t i = 0; i < arraysize(fingerprint.data); ++i) { if (fingerprint.data[i] != 0) return false; } @@ -187,7 +187,7 @@ typedef scoped_ptr_malloc<const CERT_CHAIN_CONTEXT, bool X509Certificate::FingerprintLessThan::operator()( const Fingerprint& lhs, const Fingerprint& rhs) const { - for (int i = 0; i < sizeof(lhs.data); ++i) { + for (size_t i = 0; i < sizeof(lhs.data); ++i) { if (lhs.data[i] < rhs.data[i]) return true; if (lhs.data[i] > rhs.data[i]) @@ -256,7 +256,7 @@ class X509Certificate::Cache { // Obtain an instance of X509Certificate::Cache via GetInstance(). Cache() { } - friend DefaultSingletonTraits<X509Certificate::Cache>; + friend struct DefaultSingletonTraits<X509Certificate::Cache>; // You must acquire this lock before using any private data of this object. // You must not block while holding this lock. @@ -265,7 +265,7 @@ class X509Certificate::Cache { // The certificate cache. You must acquire |lock_| before using |cache_|. CertMap cache_; - DISALLOW_EVIL_CONSTRUCTORS(X509Certificate::Cache); + DISALLOW_COPY_AND_ASSIGN(X509Certificate::Cache); }; void X509Certificate::Initialize() { @@ -319,13 +319,8 @@ X509Certificate* X509Certificate::CreateFromHandle(OSCertHandle cert_handle) { } // static -X509Certificate* X509Certificate::CreateFromPickle(const Pickle& pickle, - void** pickle_iter) { - const char* data; - int length; - if (!pickle.ReadData(pickle_iter, &data, &length)) - return NULL; - +X509Certificate* X509Certificate::CreateFromBytes(const char* data, + int length) { OSCertHandle cert_handle = NULL; if (!CertAddSerializedElementToStore( NULL, // the cert won't be persisted in any cert store @@ -337,6 +332,17 @@ X509Certificate* X509Certificate::CreateFromPickle(const Pickle& pickle, return CreateFromHandle(cert_handle); } +// static +X509Certificate* X509Certificate::CreateFromPickle(const Pickle& pickle, + void** pickle_iter) { + const char* data; + int length; + if (!pickle.ReadData(pickle_iter, &data, &length)) + return NULL; + + return CreateFromBytes(data, length); +} + X509Certificate::X509Certificate(OSCertHandle cert_handle) : cert_handle_(cert_handle) { Initialize(); @@ -461,19 +467,19 @@ void X509Certificate::ParsePrincipal(const std::string& description, const std::string kDelimiters("\r\n"); std::vector<std::string> common_names, locality_names, state_names, - country_names, street_addresses; + country_names; // TODO(jcampan): add business_category and serial_number. - const std::string kPrefixes[8] = { std::string("CN="), - std::string("L="), - std::string("S="), - std::string("C="), - std::string("STREET="), - std::string("O="), - std::string("OU="), - std::string("DC=") }; - - std::vector<std::string>* values[8] = { + const std::string kPrefixes[] = { std::string("CN="), + std::string("L="), + std::string("S="), + std::string("C="), + std::string("STREET="), + std::string("O="), + std::string("OU="), + std::string("DC=") }; + + std::vector<std::string>* values[] = { &common_names, &locality_names, &state_names, &country_names, &(principal->street_addresses), |