普通文本  |  46行  |  1.58 KB

// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/renderer/safe_browsing/features.h"

#include "base/format_macros.h"
#include "base/strings/stringprintf.h"
#include "chrome/renderer/safe_browsing/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace safe_browsing {

TEST(PhishingFeaturesTest, TooManyFeatures) {
  FeatureMap features;
  for (size_t i = 0; i < FeatureMap::kMaxFeatureMapSize; ++i) {
    EXPECT_TRUE(features.AddBooleanFeature(
        base::StringPrintf("Feature%" PRIuS, i)));
  }
  EXPECT_EQ(FeatureMap::kMaxFeatureMapSize, features.features().size());

  // Attempting to add more features should fail.
  for (size_t i = 0; i < 3; ++i) {
    EXPECT_FALSE(features.AddBooleanFeature(
        base::StringPrintf("Extra%" PRIuS, i)));
  }
  EXPECT_EQ(FeatureMap::kMaxFeatureMapSize, features.features().size());
}

TEST(PhishingFeaturesTest, IllegalFeatureValue) {
  FeatureMap features;
  EXPECT_FALSE(features.AddRealFeature("toosmall", -0.1));
  EXPECT_TRUE(features.AddRealFeature("zero", 0.0));
  EXPECT_TRUE(features.AddRealFeature("pointfive", 0.5));
  EXPECT_TRUE(features.AddRealFeature("one", 1.0));
  EXPECT_FALSE(features.AddRealFeature("toolarge", 1.1));

  FeatureMap expected_features;
  expected_features.AddRealFeature("zero", 0.0);
  expected_features.AddRealFeature("pointfive", 0.5);
  expected_features.AddRealFeature("one", 1.0);
  ExpectFeatureMapsAreEqual(features, expected_features);
}

}  // namespace safe_browsing