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

Unified Diff: chrome/browser/chromeos/login/owner_key_util_unittest.cc

Issue 10828032: Add DeviceSettingsService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 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: chrome/browser/chromeos/login/owner_key_util_unittest.cc
diff --git a/chrome/browser/chromeos/login/owner_key_util_unittest.cc b/chrome/browser/chromeos/login/owner_key_util_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ba5288c8de8293d052ef301f5b37e0e248ea90f0
--- /dev/null
+++ b/chrome/browser/chromeos/login/owner_key_util_unittest.cc
@@ -0,0 +1,82 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/login/owner_key_util.h"
+
+#include <string>
+#include <vector>
+
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/memory/ref_counted.h"
+#include "base/scoped_temp_dir.h"
+#include "crypto/nss_util.h"
+#include "crypto/nss_util_internal.h"
+#include "crypto/rsa_private_key.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace chromeos {
+
+class OwnerKeyUtilTest : public ::testing::Test {
+ public:
+ OwnerKeyUtilTest() {}
+ virtual ~OwnerKeyUtilTest() {}
+
+ virtual void SetUp() {
+ crypto::OpenPersistentNSSDB();
+
+ ASSERT_TRUE(tmpdir_.CreateUniqueTempDir());
+ key_file_ = tmpdir_.path().Append("key");
+ util_ = new OwnerKeyUtilImpl(key_file_);
+ }
+
+ // Key generation parameters.
+ static const uint16 kKeySizeInBits;
+
+ ScopedTempDir tmpdir_;
+ FilePath key_file_;
+ scoped_refptr<OwnerKeyUtil> util_;
+};
+
+// We're generating and using 2048-bit RSA keys.
+// static
+const uint16 OwnerKeyUtilTest::kKeySizeInBits = 2048;
+
+TEST_F(OwnerKeyUtilTest, ExportImportPublicKey) {
+ scoped_ptr<crypto::RSAPrivateKey> pair(
+ crypto::RSAPrivateKey::CreateSensitive(kKeySizeInBits));
+ ASSERT_NE(pair.get(), reinterpret_cast<crypto::RSAPrivateKey*>(NULL));
+
+ // Export public key to file.
+ ASSERT_TRUE(util_->ExportPublicKey(pair.get()));
+
+ // Export public key, so that we can compare it to the one we get off disk.
+ std::vector<uint8> public_key;
+ ASSERT_TRUE(pair->ExportPublicKey(&public_key));
+ std::vector<uint8> from_disk;
+ ASSERT_TRUE(util_->ImportPublicKey(&from_disk));
+
+ std::vector<uint8>::iterator pubkey_it;
+ std::vector<uint8>::iterator disk_it;
+ for (pubkey_it = public_key.begin(), disk_it = from_disk.begin();
+ pubkey_it < public_key.end();
+ pubkey_it++, disk_it++) {
+ EXPECT_EQ(*pubkey_it, *disk_it);
+ }
+}
+
+TEST_F(OwnerKeyUtilTest, ImportPublicKeyFailed) {
+ // First test the case where the file is missing which should fail.
+ std::vector<uint8> from_disk;
+ ASSERT_FALSE(util_->ImportPublicKey(&from_disk));
+
+ // Next try empty file. This should fail and the array should be empty.
+ from_disk.resize(10);
+ ASSERT_EQ(0, file_util::WriteFile(key_file_, "", 0));
+ ASSERT_FALSE(util_->ImportPublicKey(&from_disk));
+ ASSERT_FALSE(from_disk.size());
+}
+
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698