1// 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
7#include "brillo/secure_blob.h"
8
9#include <algorithm>
10#include <iterator>
11#include <numeric>
12
13#include <base/logging.h>
14#include <gtest/gtest.h>
15
16namespace brillo {
17using std::string;
18
19class SecureBlobTest : public ::testing::Test {
20 public:
21  SecureBlobTest() {}
22  virtual ~SecureBlobTest() {}
23
24  static bool FindBlobInBlob(const brillo::Blob& haystack,
25                             const brillo::Blob& needle) {
26    auto pos = std::search(
27        haystack.begin(), haystack.end(), needle.begin(), needle.end());
28    return (pos != haystack.end());
29  }
30
31  static int FindBlobIndexInBlob(const brillo::Blob& haystack,
32                                 const brillo::Blob& needle) {
33    auto pos = std::search(
34        haystack.begin(), haystack.end(), needle.begin(), needle.end());
35    if (pos == haystack.end()) {
36      return -1;
37    }
38    return std::distance(haystack.begin(), pos);
39  }
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];
55  std::iota(std::begin(from_data), std::end(from_data), 0);
56
57  SecureBlob blob(std::begin(from_data), std::end(from_data));
58
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
68  brillo::Blob from_blob(32);
69  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
83  size_t length = 1024;
84  SecureBlob blob(length);
85  void* original_data = blob.data();
86  for (size_t i = 0; i < length; i++) {
87    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());
94  EXPECT_EQ(0, blob.data()[length - 1]);
95}
96
97TEST_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");
114  SecureBlob blob = SecureBlob(test_string.begin(), test_string.end());
115  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
120}  // namespace brillo
121