1 // Copyright 2013 The Chromium 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.
5 #include "base/pickle.h"
6 #include "components/nacl/browser/nacl_validation_cache.h"
7 #include "testing/gtest/include/gtest/gtest.h"
12 "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
14 "a 64-byte string of various junk................................";
15 const char sig1
[33] = "0123456789ABCDEF0123456789ABCDEF";
16 const char sig2
[33] = "a 32-byte string of various junk";
18 class NaClValidationCacheTest
: public ::testing::Test
{
20 NaClValidationCache cache1
;
21 NaClValidationCache cache2
;
23 void SetUp() override
{
24 // The compiler chokes if std::string(key1) is passed directly as an arg.
25 std::string
key(key1
);
26 cache1
.SetValidationCacheKey(key
);
27 cache2
.SetValidationCacheKey(key
);
30 bool IsIdentical(const NaClValidationCache
& a
,
31 const NaClValidationCache
& b
) const {
32 if (a
.GetValidationCacheKey() != b
.GetValidationCacheKey())
34 if (a
.size() != b
.size())
36 return a
.GetContents() == b
.GetContents();
40 TEST_F(NaClValidationCacheTest
, Sanity
) {
41 ASSERT_EQ(0, (int) cache1
.size());
42 ASSERT_FALSE(cache1
.QueryKnownToValidate(sig1
, true));
43 ASSERT_FALSE(cache1
.QueryKnownToValidate(sig2
, true));
46 TEST_F(NaClValidationCacheTest
, Sig1
) {
47 cache1
.SetKnownToValidate(sig1
);
48 ASSERT_EQ(1, (int) cache1
.size());
49 ASSERT_TRUE(cache1
.QueryKnownToValidate(sig1
, true));
50 ASSERT_FALSE(cache1
.QueryKnownToValidate(sig2
, true));
53 TEST_F(NaClValidationCacheTest
, Sig2
) {
54 cache1
.SetKnownToValidate(sig2
);
55 ASSERT_EQ(1, (int) cache1
.size());
56 ASSERT_FALSE(cache1
.QueryKnownToValidate(sig1
, true));
57 ASSERT_TRUE(cache1
.QueryKnownToValidate(sig2
, true));
60 TEST_F(NaClValidationCacheTest
, SigBoth
) {
61 cache1
.SetKnownToValidate(sig1
);
62 cache1
.SetKnownToValidate(sig2
);
63 ASSERT_EQ(2, (int) cache1
.size());
64 ASSERT_TRUE(cache1
.QueryKnownToValidate(sig1
, true));
65 ASSERT_TRUE(cache1
.QueryKnownToValidate(sig2
, true));
68 TEST_F(NaClValidationCacheTest
, DoubleSet
) {
69 cache1
.SetKnownToValidate(sig1
);
70 cache1
.SetKnownToValidate(sig1
);
71 ASSERT_EQ(1, (int) cache1
.size());
72 ASSERT_TRUE(cache1
.QueryKnownToValidate(sig1
, true));
75 TEST_F(NaClValidationCacheTest
, EmptyIdentical
) {
76 ASSERT_TRUE(IsIdentical(cache1
, cache2
));
79 TEST_F(NaClValidationCacheTest
, DifferentKeysNotIdentical
) {
80 std::string
key(key2
);
81 cache2
.SetValidationCacheKey(key
);
82 ASSERT_FALSE(IsIdentical(cache1
, cache2
));
86 TEST_F(NaClValidationCacheTest
, DifferentSizesNotIdentical
) {
87 cache1
.SetKnownToValidate(sig1
);
89 ASSERT_FALSE(IsIdentical(cache1
, cache2
));
92 TEST_F(NaClValidationCacheTest
, SameSigsIdentical
) {
93 cache1
.SetKnownToValidate(sig1
);
95 cache2
.SetKnownToValidate(sig1
);
97 ASSERT_TRUE(IsIdentical(cache1
, cache2
));
100 TEST_F(NaClValidationCacheTest
, DifferentSigsNotIdentical
) {
101 cache1
.SetKnownToValidate(sig1
);
103 cache2
.SetKnownToValidate(sig2
);
105 ASSERT_FALSE(IsIdentical(cache1
, cache2
));
108 TEST_F(NaClValidationCacheTest
, InOrderIdentical
) {
109 cache1
.SetKnownToValidate(sig1
);
110 cache1
.SetKnownToValidate(sig2
);
112 cache2
.SetKnownToValidate(sig1
);
113 cache2
.SetKnownToValidate(sig2
);
115 ASSERT_TRUE(IsIdentical(cache1
, cache2
));
118 TEST_F(NaClValidationCacheTest
, QueryReorders
) {
119 cache1
.SetKnownToValidate(sig1
);
120 cache1
.SetKnownToValidate(sig2
);
122 cache2
.SetKnownToValidate(sig2
);
123 cache2
.SetKnownToValidate(sig1
);
125 ASSERT_FALSE(IsIdentical(cache1
, cache2
));
126 cache2
.QueryKnownToValidate(sig2
, true);
127 ASSERT_TRUE(IsIdentical(cache1
, cache2
));
130 TEST_F(NaClValidationCacheTest
, ForceNoReorder
) {
131 cache1
.SetKnownToValidate(sig1
);
132 cache1
.SetKnownToValidate(sig2
);
134 cache2
.SetKnownToValidate(sig2
);
135 cache2
.SetKnownToValidate(sig1
);
137 cache2
.QueryKnownToValidate(sig2
, false);
138 ASSERT_FALSE(IsIdentical(cache1
, cache2
));
141 TEST_F(NaClValidationCacheTest
, SerializeDeserialize
) {
142 std::string
key(key2
);
143 cache1
.SetValidationCacheKey(key
);
144 cache1
.SetKnownToValidate(sig1
);
145 cache1
.SetKnownToValidate(sig2
);
148 cache1
.Serialize(&pickle
);
149 ASSERT_TRUE(cache2
.Deserialize(&pickle
));
150 ASSERT_EQ(2, (int) cache2
.size());
151 ASSERT_TRUE(IsIdentical(cache1
, cache2
));
154 TEST_F(NaClValidationCacheTest
, SerializeDeserializeTruncated
) {
155 std::string
key(key2
);
156 cache1
.SetValidationCacheKey(key
);
157 cache1
.SetKnownToValidate(sig1
);
158 cache1
.SetKnownToValidate(sig2
);
161 cache1
.Serialize(&pickle
);
162 base::Pickle
truncated(static_cast<const char*>(pickle
.data()),
164 ASSERT_FALSE(cache2
.Deserialize(&truncated
));
165 ASSERT_EQ(0, (int) cache2
.size());
168 TEST_F(NaClValidationCacheTest
, DeserializeBadKey
) {
169 std::string
key(sig1
); // Too short, will cause the deserialization to error.
170 cache1
.SetValidationCacheKey(key
);
171 cache1
.SetKnownToValidate(sig1
);
172 cache1
.SetKnownToValidate(sig2
);
175 cache1
.Serialize(&pickle
);
176 ASSERT_FALSE(cache2
.Deserialize(&pickle
));
177 ASSERT_EQ(0, (int) cache2
.size());
180 TEST_F(NaClValidationCacheTest
, DeserializeNothing
) {
181 cache1
.SetKnownToValidate(sig1
);
182 base::Pickle
pickle("", 0);
183 ASSERT_FALSE(cache1
.Deserialize(&pickle
));
184 ASSERT_EQ(0, (int) cache1
.size());
187 TEST_F(NaClValidationCacheTest
, DeserializeJunk
) {
188 cache1
.SetKnownToValidate(sig1
);
189 base::Pickle
pickle(key1
, strlen(key1
));
190 ASSERT_FALSE(cache1
.Deserialize(&pickle
));
191 ASSERT_EQ(0, (int) cache1
.size());