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

Side by Side Diff: secure_blob_unittest.cc

Issue 2645008: Update on feedback, update dbus API, add unit tests. TEST=manual,unit,BVT BUG=3628 323 (Closed) Base URL: ssh://git@chromiumos-git/cryptohome.git
Patch Set: Address second round of feedback. Created 10 years, 6 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 unified diff | Download patch
« no previous file with comments | « secure_blob.cc ('k') | service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Unit tests for SecureBlob.
6
7 #include "secure_blob.h"
8
9 #include <base/logging.h>
10 #include <chromeos/utility.h>
11 #include <gtest/gtest.h>
12
13 namespace cryptohome {
14 using std::string;
15
16 class SecureBlobTest : public ::testing::Test {
17 public:
18 SecureBlobTest() { }
19 virtual ~SecureBlobTest() { }
20
21 static bool FindBlobInBlob(const chromeos::Blob& haystack,
22 const chromeos::Blob& needle) {
23 if (needle.size() > haystack.size()) {
24 return false;
25 }
26 for (unsigned int start = 0; start <= (haystack.size() - needle.size());
27 start++) {
28 if (memcmp(&haystack[start], &needle[0], needle.size()) == 0) {
29 return true;
30 }
31 }
32 return false;
33 }
34
35 private:
36 DISALLOW_COPY_AND_ASSIGN(SecureBlobTest);
37 };
38
39 TEST_F(SecureBlobTest, AllocationSizeTest) {
40 // Check that allocating a SecureBlob of a specified size works
41 SecureBlob blob(32);
42
43 EXPECT_EQ(32, blob.size());
44 }
45
46 TEST_F(SecureBlobTest, AllocationCopyTest) {
47 // Check that allocating a SecureBlob with an iterator works
48 unsigned char from_data[32];
49 for (unsigned int i = 0; i < sizeof(from_data); i++) {
50 from_data[i] = i;
51 }
52
53 SecureBlob blob(from_data, sizeof(from_data));
54
55 EXPECT_EQ(sizeof(from_data), blob.size());
56
57 for (unsigned int i = 0; i < sizeof(from_data); i++) {
58 EXPECT_EQ(from_data[i], blob[i]);
59 }
60 }
61
62 TEST_F(SecureBlobTest, IteratorConstructorTest) {
63 // Check that allocating a SecureBlob with an iterator works
64 chromeos::Blob from_blob(32);
65 for (unsigned int i = 0; i < from_blob.size(); i++) {
66 from_blob[i] = i;
67 }
68
69 SecureBlob blob(from_blob.begin(), from_blob.end());
70
71 EXPECT_EQ(from_blob.size(), blob.size());
72 EXPECT_EQ(true, SecureBlobTest::FindBlobInBlob(from_blob, blob));
73 }
74
75 TEST_F(SecureBlobTest, ResizeTest) {
76 // Check that resizing a SecureBlob wipes the excess memory. The test assumes
77 // that resize() down by one will not re-allocate the memory, so the last byte
78 // will still be part of the SecureBlob's allocation
79 unsigned int length = 1024;
80 SecureBlob blob(length);
81 void* original_data = blob.data();
82 for (unsigned int i = 0; i < length; i++) {
83 blob[i] = i;
84 }
85
86 blob.resize(length - 1);
87
88 EXPECT_EQ(original_data, blob.data());
89 EXPECT_EQ(length - 1, blob.size());
90 EXPECT_EQ(0, static_cast<unsigned char*>(blob.data())[length - 1]);
91 }
92
93 TEST_F(SecureBlobTest, DestructorTest) {
94 // Check that resizing a SecureBlob wipes memory on destruction. The test
95 // assumes that the unallocated memory will not be reused in the meantime,
96 // which means that this test needs to be done carefully. It intentionally
97 // accesses freed memory to check of it has been zeroed.
98 unsigned int length = 1024;
99 SecureBlob* blob = new SecureBlob(length);
100 unsigned char* data = static_cast<unsigned char*>(blob->data());
101 for (unsigned int i = 0; i < length; i++) {
102 data[i] = i % 256;
103 }
104
105 delete(blob);
106
107 for (unsigned int i = 0; i < length; i++) {
108 EXPECT_EQ(0, data[i]);
109 }
110 }
111
112 } // namespace cryptohome
OLDNEW
« no previous file with comments | « secure_blob.cc ('k') | service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698