Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Unified Diff: net/base/x509_certificate_unittest.cc

Issue 7384002: Added CreateOriginBound method to x509_certificate.h. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Changed ObCertOIDWrapper from a Singleton to a LeakySingleton to avoid a runtime error. Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/base/x509_certificate_unittest.cc
diff --git a/net/base/x509_certificate_unittest.cc b/net/base/x509_certificate_unittest.cc
index 32417ac3e9394bb02a3aa1f9164f2c1a73373bd0..b2e00ce94bb7947efb0f3d022dd7b6af83592892 100644
--- a/net/base/x509_certificate_unittest.cc
+++ b/net/base/x509_certificate_unittest.cc
@@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <cert.h>
+#include <secoid.h>
wtc 2011/08/23 01:32:21 Remove these two lines.
mdietz 2011/08/23 20:52:56 Done.
+
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
@@ -20,6 +23,11 @@
#include "net/base/x509_certificate.h"
#include "testing/gtest/include/gtest/gtest.h"
+#if defined(USE_NSS)
+#include <cert.h>
+#include <secoid.h>
+#endif
+
// Unit tests aren't allowed to access external resources. Unfortunately, to
// properly verify the EV-ness of a cert, we need to check for its revocation
// through online servers. If you're manually running unit tests, feel free to
@@ -1119,6 +1127,7 @@ TEST(X509CertificateTest, CreateSelfSigned) {
EXPECT_FALSE(cert->HasExpired());
}
wtc 2011/08/23 01:32:21 Remove this blank line.
mdietz 2011/08/23 20:52:56 Done.
+
TEST(X509CertificateTest, GetDEREncoded) {
scoped_ptr<crypto::RSAPrivateKey> private_key(
crypto::RSAPrivateKey::Create(1024));
@@ -1132,6 +1141,84 @@ TEST(X509CertificateTest, GetDEREncoded) {
}
#endif
+#if defined(USE_NSS)
+// This test creates an origin-bound cert from a private key and
+// then verifies the content of the certificate.
+TEST(X509CertificateTest, CreateOriginBound) {
+ // Origin Bound Cert OID
+ static const char oid_string[] = "1.3.6.1.4.1.11129.2.1.6";
+
+ // Sample ASCII weborigin
+ std::string origin = "http://weborigin.com:443";
+
+ // Create object neccissary for extension lookup call
+ SECItem extension_object = {
+ siAsciiString,
+ (unsigned char*)origin.data(),
+ origin.size()
+ };
+
+ scoped_ptr<crypto::RSAPrivateKey> private_key(
+ crypto::RSAPrivateKey::Create(1024));
+ scoped_refptr<X509Certificate> cert =
+ X509Certificate::CreateOriginBound(private_key.get(),
+ origin, 1,
+ base::TimeDelta::FromDays(1));
+
+ EXPECT_EQ("subject", cert->subject().GetDisplayName());
+ EXPECT_FALSE(cert->HasExpired());
+
+ // IA5Encode and arena allocate SECItem
+ PLArenaPool* arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
+ SECItem* expected = SEC_ASN1EncodeItem(arena,
+ NULL,
+ &extension_object,
+ SEC_ASN1_GET(SEC_IA5StringTemplate));
+
+ ASSERT_NE(static_cast<SECItem*>(NULL), expected);
+
+ // Create OID SECItem
+ SECItem ob_cert_oid = { siDEROID, NULL, 0 };
+ SECStatus ok = SEC_StringToOID(arena, &ob_cert_oid,
+ oid_string, NULL);
+
+ ASSERT_EQ(SECSuccess, ok);
+
+ SECOidTag ob_cert_oid_tag = SECOID_FindOIDTag(&ob_cert_oid);
+
+ ASSERT_NE(SEC_OID_UNKNOWN, ob_cert_oid_tag);
+
+ // Lookup Origin Bound Cert extension in generated cert
+ SECItem actual = { siBuffer, NULL, 0 };
+ ok = CERT_FindCertExtension(cert->os_cert_handle(),
+ ob_cert_oid_tag,
+ &actual);
+ ASSERT_EQ(SECSuccess, ok);
+
+ // Compare expected and actual extension values
+ PRBool result = SECITEM_ItemsAreEqual(expected, &actual);
+ ASSERT_TRUE(result);
+
+ // Cleanup
+ SECITEM_FreeItem(&actual, PR_FALSE);
+ PORT_FreeArena(arena, PR_FALSE);
+}
+#else // defined(USE_NSS)
+// On other platforms, X509Certificate::CreateOriginBound() is not implemented
+// and should return NULL. This unit test ensures that a stub implementation
+// is present.
+TEST(X509CertificateTest, CreateOriginBoundNotImplemented) {
+ std::string origin = "http://weborigin.com:443";
+ scoped_ptr<crypto::RSAPrivateKey> private_key(
+ crypto::RSAPrivateKey::Create(1024));
+ scoped_refptr<X509Certificate> cert =
+ X509Certificate::CreateOriginBound(private_key.get(),
+ origin, 2,
+ base::TimeDelta::FromDays(1));
+ EXPECT_FALSE(cert);
+}
+#endif // defined(USE_NSS)
+
class X509CertificateParseTest
: public testing::TestWithParam<CertificateFormatTestData> {
public:

Powered by Google App Engine
This is Rietveld 408576698