blob: d4fd555d6b8d2088bc3c408e44df5d888955729c [file] [log] [blame]
Darren Krahnfcb4e192012-06-08 14:52:47 -07001// Copyright (c) 2012 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
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -07007#include "brillo/secure_blob.h"
Darren Krahnfcb4e192012-06-08 14:52:47 -07008
Utkarsh Sanghic411f7f2014-09-08 15:01:31 -07009#include <algorithm>
10#include <iterator>
11#include <numeric>
12
Darren Krahnfcb4e192012-06-08 14:52:47 -070013#include <base/logging.h>
14#include <gtest/gtest.h>
15
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070016namespace brillo {
Darren Krahnfcb4e192012-06-08 14:52:47 -070017using std::string;
18
19class SecureBlobTest : public ::testing::Test {
20 public:
Alex Vakulenko05d29042015-01-13 09:39:25 -080021 SecureBlobTest() {}
22 virtual ~SecureBlobTest() {}
Darren Krahnfcb4e192012-06-08 14:52:47 -070023
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070024 static bool FindBlobInBlob(const brillo::Blob& haystack,
25 const brillo::Blob& needle) {
Alex Vakulenko05d29042015-01-13 09:39:25 -080026 auto pos = std::search(
27 haystack.begin(), haystack.end(), needle.begin(), needle.end());
Utkarsh Sanghic411f7f2014-09-08 15:01:31 -070028 return (pos != haystack.end());
29 }
30
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070031 static int FindBlobIndexInBlob(const brillo::Blob& haystack,
32 const brillo::Blob& needle) {
Alex Vakulenko05d29042015-01-13 09:39:25 -080033 auto pos = std::search(
34 haystack.begin(), haystack.end(), needle.begin(), needle.end());
Utkarsh Sanghic411f7f2014-09-08 15:01:31 -070035 if (pos == haystack.end()) {
36 return -1;
Darren Krahnfcb4e192012-06-08 14:52:47 -070037 }
Utkarsh Sanghic411f7f2014-09-08 15:01:31 -070038 return std::distance(haystack.begin(), pos);
Darren Krahnfcb4e192012-06-08 14:52:47 -070039 }
40
41 private:
42 DISALLOW_COPY_AND_ASSIGN(SecureBlobTest);
43};
44
45TEST_F(SecureBlobTest, AllocationSizeTest) {
46 // Check that allocating a SecureBlob of a specified size works
47 SecureBlob blob(32);
48
49 EXPECT_EQ(32, blob.size());
50}
51
52TEST_F(SecureBlobTest, AllocationCopyTest) {
53 // Check that allocating a SecureBlob with an iterator works
54 unsigned char from_data[32];
Alex Vakulenkoc3c53ee2015-03-25 16:21:34 -070055 std::iota(std::begin(from_data), std::end(from_data), 0);
Darren Krahnfcb4e192012-06-08 14:52:47 -070056
Alex Vakulenkoc3c53ee2015-03-25 16:21:34 -070057 SecureBlob blob(std::begin(from_data), std::end(from_data));
Darren Krahnfcb4e192012-06-08 14:52:47 -070058
59 EXPECT_EQ(sizeof(from_data), blob.size());
60
61 for (unsigned int i = 0; i < sizeof(from_data); i++) {
62 EXPECT_EQ(from_data[i], blob[i]);
63 }
64}
65
66TEST_F(SecureBlobTest, IteratorConstructorTest) {
67 // Check that allocating a SecureBlob with an iterator works
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070068 brillo::Blob from_blob(32);
Darren Krahnfcb4e192012-06-08 14:52:47 -070069 for (unsigned int i = 0; i < from_blob.size(); i++) {
70 from_blob[i] = i;
71 }
72
73 SecureBlob blob(from_blob.begin(), from_blob.end());
74
75 EXPECT_EQ(from_blob.size(), blob.size());
76 EXPECT_TRUE(SecureBlobTest::FindBlobInBlob(from_blob, blob));
77}
78
79TEST_F(SecureBlobTest, ResizeTest) {
80 // Check that resizing a SecureBlob wipes the excess memory. The test assumes
81 // that resize() down by one will not re-allocate the memory, so the last byte
82 // will still be part of the SecureBlob's allocation
Alex Vakulenkoc3c53ee2015-03-25 16:21:34 -070083 size_t length = 1024;
Darren Krahnfcb4e192012-06-08 14:52:47 -070084 SecureBlob blob(length);
85 void* original_data = blob.data();
Alex Vakulenkoc3c53ee2015-03-25 16:21:34 -070086 for (size_t i = 0; i < length; i++) {
Darren Krahnfcb4e192012-06-08 14:52:47 -070087 blob[i] = i;
88 }
89
90 blob.resize(length - 1);
91
92 EXPECT_EQ(original_data, blob.data());
93 EXPECT_EQ(length - 1, blob.size());
Alex Vakulenkoc3c53ee2015-03-25 16:21:34 -070094 EXPECT_EQ(0, blob.data()[length - 1]);
Darren Krahnfcb4e192012-06-08 14:52:47 -070095}
96
Utkarsh Sanghic411f7f2014-09-08 15:01:31 -070097TEST_F(SecureBlobTest, CombineTest) {
98 SecureBlob blob1(32);
99 SecureBlob blob2(32);
100 std::iota(blob1.begin(), blob1.end(), 0);
101 std::iota(blob2.begin(), blob2.end(), 32);
102 SecureBlob combined_blob = SecureBlob::Combine(blob1, blob2);
103 EXPECT_EQ(combined_blob.size(), (blob1.size() + blob2.size()));
104 EXPECT_TRUE(SecureBlobTest::FindBlobInBlob(combined_blob, blob1));
105 EXPECT_TRUE(SecureBlobTest::FindBlobInBlob(combined_blob, blob2));
106 int blob1_index = SecureBlobTest::FindBlobIndexInBlob(combined_blob, blob1);
107 int blob2_index = SecureBlobTest::FindBlobIndexInBlob(combined_blob, blob2);
108 EXPECT_EQ(blob1_index, 0);
109 EXPECT_EQ(blob2_index, 32);
110}
111
112TEST_F(SecureBlobTest, BlobToStringTest) {
113 std::string test_string("Test String");
Alex Vakulenkoc3c53ee2015-03-25 16:21:34 -0700114 SecureBlob blob = SecureBlob(test_string.begin(), test_string.end());
Utkarsh Sanghic411f7f2014-09-08 15:01:31 -0700115 EXPECT_EQ(blob.size(), test_string.length());
116 std::string result_string = blob.to_string();
117 EXPECT_EQ(test_string.compare(result_string), 0);
118}
119
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700120} // namespace brillo