This source file includes following definitions.
- status_
- QueryKnownToValidate
- SetKnownToValidate
- ResolveFileToken
- SetUp
- AssertQuerySame
- AssertQueryDifferent
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "components/nacl/loader/nacl_validation_db.h"
#include "components/nacl/loader/nacl_validation_query.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kKey[] = "bogus key for HMAC...";
const char kKeyAlt[] = "bogus key for HMAC!!!";
const char kVersion[] = "bogus version";
const char kVersionAlt[] = "bogus!version";
const char kShortData[] = "Short data 1234567890";
const char kAltShortData[] = "Short!data 1234567890";
const char kLongData[] = "Long data."
"1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890";
class MockValidationDB : public NaClValidationDB {
public:
MockValidationDB()
: did_query_(false),
did_set_(false),
status_(true) {
}
virtual bool QueryKnownToValidate(const std::string& signature) OVERRIDE {
EXPECT_EQ((int) NaClValidationQuery::kDigestLength,
(int) signature.length());
EXPECT_FALSE(did_query_);
EXPECT_FALSE(did_set_);
did_query_ = true;
memcpy(query_signature_, signature.data(),
NaClValidationQuery::kDigestLength);
return status_;
}
virtual void SetKnownToValidate(const std::string& signature) OVERRIDE {
ASSERT_EQ((int) NaClValidationQuery::kDigestLength,
(int) signature.length());
EXPECT_TRUE(did_query_);
EXPECT_FALSE(did_set_);
did_set_ = true;
memcpy(set_signature_, signature.data(),
NaClValidationQuery::kDigestLength);
EXPECT_EQ(0, memcmp(query_signature_, set_signature_,
NaClValidationQuery::kDigestLength));
}
virtual bool ResolveFileToken(struct NaClFileToken* file_token, int32* fd,
std::string* path) OVERRIDE {
*fd = -1;
*path = "";
return false;
}
bool did_query_;
bool did_set_;
bool status_;
uint8 query_signature_[NaClValidationQuery::kDigestLength];
uint8 set_signature_[NaClValidationQuery::kDigestLength];
};
class TestQuery {
public:
TestQuery(const char* key, const char* version) {
db.reset(new MockValidationDB());
context.reset(new NaClValidationQueryContext(db.get(), key, version));
query.reset(context->CreateQuery());
}
scoped_ptr<MockValidationDB> db;
scoped_ptr<NaClValidationQueryContext> context;
scoped_ptr<NaClValidationQuery> query;
};
class NaClValidationQueryTest : public ::testing::Test {
protected:
scoped_ptr<TestQuery> query1;
scoped_ptr<TestQuery> query2;
virtual void SetUp() {
query1.reset(new TestQuery(kKey, kVersion));
query2.reset(new TestQuery(kKey, kVersion));
}
void AssertQuerySame() {
ASSERT_TRUE(query1->db->did_query_);
ASSERT_TRUE(query2->db->did_query_);
ASSERT_EQ(0, memcmp(query1->db->query_signature_,
query2->db->query_signature_,
NaClValidationQuery::kDigestLength));
}
void AssertQueryDifferent() {
ASSERT_TRUE(query1->db->did_query_);
ASSERT_TRUE(query2->db->did_query_);
ASSERT_NE(0, memcmp(query1->db->query_signature_,
query2->db->query_signature_,
NaClValidationQuery::kDigestLength));
}
};
TEST_F(NaClValidationQueryTest, Sanity) {
query1->query->AddData(kShortData, sizeof(kShortData));
ASSERT_FALSE(query1->db->did_query_);
ASSERT_FALSE(query1->db->did_set_);
ASSERT_EQ(1, query1->query->QueryKnownToValidate());
ASSERT_TRUE(query1->db->did_query_);
ASSERT_FALSE(query1->db->did_set_);
query1->query->SetKnownToValidate();
ASSERT_TRUE(query1->db->did_query_);
ASSERT_TRUE(query1->db->did_set_);
}
TEST_F(NaClValidationQueryTest, ConsistentShort) {
query1->query->AddData(kShortData, sizeof(kShortData));
query1->query->QueryKnownToValidate();
query2->query->AddData(kShortData, sizeof(kShortData));
query2->query->QueryKnownToValidate();
AssertQuerySame();
}
TEST_F(NaClValidationQueryTest, InconsistentShort) {
query1->query->AddData(kShortData, sizeof(kShortData));
query1->query->QueryKnownToValidate();
query2->query->AddData(kAltShortData, sizeof(kAltShortData));
query2->query->QueryKnownToValidate();
AssertQueryDifferent();
}
TEST_F(NaClValidationQueryTest, ConsistentShortBug) {
query1->query->AddData(kShortData, sizeof(kShortData));
query1->query->AddData(kShortData, sizeof(kShortData));
query1->query->QueryKnownToValidate();
query2->query->AddData(kShortData, sizeof(kShortData));
query2->query->AddData(kShortData, sizeof(kShortData));
query2->query->QueryKnownToValidate();
AssertQuerySame();
}
TEST_F(NaClValidationQueryTest, InconsistentShortBug1) {
query1->query->AddData(kShortData, sizeof(kShortData));
query1->query->AddData(kShortData, sizeof(kShortData));
query1->query->QueryKnownToValidate();
query2->query->AddData(kAltShortData, sizeof(kAltShortData));
query2->query->AddData(kShortData, sizeof(kShortData));
query2->query->QueryKnownToValidate();
AssertQueryDifferent();
}
TEST_F(NaClValidationQueryTest, InconsistentShort2) {
query1->query->AddData(kShortData, sizeof(kShortData));
query1->query->AddData(kShortData, sizeof(kShortData));
query1->query->QueryKnownToValidate();
query2->query->AddData(kShortData, sizeof(kShortData));
query2->query->AddData(kAltShortData, sizeof(kAltShortData));
query2->query->QueryKnownToValidate();
AssertQueryDifferent();
}
TEST_F(NaClValidationQueryTest, InconsistentZeroSizedAdd) {
query1->query->AddData(kShortData, sizeof(kShortData));
query1->query->QueryKnownToValidate();
query2->query->AddData(kShortData, sizeof(kShortData));
query2->query->AddData(kShortData, 0);
query2->query->QueryKnownToValidate();
AssertQueryDifferent();
}
TEST_F(NaClValidationQueryTest, ConsistentZeroSizedAdd) {
query1->query->AddData(kShortData, sizeof(kShortData));
query1->query->AddData("a", 0);
query1->query->QueryKnownToValidate();
query2->query->AddData(kShortData, sizeof(kShortData));
query2->query->AddData("b", 0);
query2->query->QueryKnownToValidate();
AssertQuerySame();
}
TEST_F(NaClValidationQueryTest, ConsistentRepeatedShort) {
for (int i = 0; i < 30; i++) {
query1->query->AddData(kShortData, sizeof(kShortData));
}
query1->query->QueryKnownToValidate();
for (int i = 0; i < 30; i++) {
query2->query->AddData(kShortData, sizeof(kShortData));
}
query2->query->QueryKnownToValidate();
AssertQuerySame();
}
TEST_F(NaClValidationQueryTest, ConsistentLong) {
query1->query->AddData(kLongData, sizeof(kLongData));
query1->query->QueryKnownToValidate();
query2->query->AddData(kLongData, sizeof(kLongData));
query2->query->QueryKnownToValidate();
AssertQuerySame();
}
TEST_F(NaClValidationQueryTest, ConsistentRepeatedLong) {
for (int i = 0; i < 30; i++) {
query1->query->AddData(kLongData, sizeof(kLongData));
}
query1->query->QueryKnownToValidate();
for (int i = 0; i < 30; i++) {
query2->query->AddData(kLongData, sizeof(kLongData));
}
query2->query->QueryKnownToValidate();
AssertQuerySame();
}
TEST_F(NaClValidationQueryTest, PerturbKey) {
query2.reset(new TestQuery(kKeyAlt, kVersion));
query1->query->AddData(kShortData, sizeof(kShortData));
query1->query->QueryKnownToValidate();
query2->query->AddData(kShortData, sizeof(kShortData));
query2->query->QueryKnownToValidate();
AssertQueryDifferent();
}
TEST_F(NaClValidationQueryTest, PerturbVersion) {
query2.reset(new TestQuery(kKey, kVersionAlt));
query1->query->AddData(kShortData, sizeof(kShortData));
query1->query->QueryKnownToValidate();
query2->query->AddData(kShortData, sizeof(kShortData));
query2->query->QueryKnownToValidate();
AssertQueryDifferent();
}
}