Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(227)

Side by Side Diff: chrome/common/safe_browsing/file_type_policies_unittest.cc

Issue 1909653002: FileTypePolicies class to load types from proto. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix up comments, add more tests Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 #include "chrome/common/safe_browsing/file_type_policies.h"
6
7 #include <string.h>
8
9 #include <memory>
10
11 #include "base/files/file_path.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using testing::NiceMock;
16
17 namespace safe_browsing {
18
19 class MockFileTypePolicies : public FileTypePolicies {
20 public:
21 MockFileTypePolicies() {}
22 virtual ~MockFileTypePolicies() {}
23
24 MOCK_METHOD2(RecordUpdateMetrics, void(UpdateResult, const std::string&));
25
26 private:
27 DISALLOW_COPY_AND_ASSIGN(MockFileTypePolicies);
28 };
29
30 class FileTypePoliciesTest : public testing::Test {
31 protected:
32 FileTypePoliciesTest() {}
33 ~FileTypePoliciesTest() {}
34
35 protected:
36 NiceMock<MockFileTypePolicies> policies_;
37 };
38
39 TEST_F(FileTypePoliciesTest, UnpackResourceBundle) {
Jialiu Lin 2016/04/22 21:36:41 This test seems to fail on a couple of try bots.
40 EXPECT_CALL(policies_,
41 RecordUpdateMetrics(FileTypePolicies::UpdateResult::SUCCESS,
42 "ResourceBundle"));
43 policies_.PopulateFromResourceBundle();
44
45 // Look up a well known type to ensure it's present.
46 base::FilePath exe_file(FILE_PATH_LITERAL("a/foo.fooobar"));
47 DownloadFileType file_type = policies_.PolicyForFile(exe_file);
48 EXPECT_EQ("exe", file_type.extension());
49 EXPECT_EQ(0, file_type.uma_value());
50 EXPECT_EQ(false, file_type.is_archive());
51 EXPECT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE,
52 file_type.platform_settings(0).danger_level());
53 EXPECT_EQ(DownloadFileType::DISALLOW_AUTO_OPEN,
54 file_type.platform_settings(0).auto_open_hint());
55 EXPECT_EQ(DownloadFileType::FULL_PING,
56 file_type.platform_settings(0).ping_setting());
57
58 // Check other accessors
59 EXPECT_EQ(0ul, policies_.UmaValueForFile(exe_file));
60 EXPECT_EQ(false, policies_.IsFileAnArchive(exe_file));
61
62 // Verify settings on default type.
63 file_type = policies_.PolicyForFile(
64 base::FilePath(FILE_PATH_LITERAL("a/foo.fooobar")));
65
66 EXPECT_EQ("", file_type.extension());
67 EXPECT_EQ(18, file_type.uma_value());
68 EXPECT_EQ(false, file_type.is_archive());
69 EXPECT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE,
70 file_type.platform_settings(0).danger_level());
71 EXPECT_EQ(DownloadFileType::DISALLOW_AUTO_OPEN,
72 file_type.platform_settings(0).auto_open_hint());
73 EXPECT_EQ(DownloadFileType::SAMPLED_PING,
74 file_type.platform_settings(0).ping_setting());
75 }
76
77 TEST_F(FileTypePoliciesTest, BadProto) {
78 EXPECT_EQ(FileTypePolicies::UpdateResult::FAILED_EMPTY,
79 policies_.PopulateFromBinaryPb(std::string()));
80
81 EXPECT_EQ(FileTypePolicies::UpdateResult::FAILED_PROTO_PARSE,
82 policies_.PopulateFromBinaryPb("foobar"));
83
84 DownloadFileTypeConfig cfg;
85 cfg.set_sampled_ping_probability(0.1);
86 EXPECT_EQ(FileTypePolicies::UpdateResult::FAILED_DEFAULT_SETTING_SET,
87 policies_.PopulateFromBinaryPb(cfg.SerializeAsString()));
88
89 cfg.mutable_default_file_type()->add_platform_settings();
90 auto file_type = cfg.add_file_types(); // This is missing a platform_setting.
91 EXPECT_EQ(FileTypePolicies::UpdateResult::FAILED_WRONG_SETTINGS_COUNT,
92 policies_.PopulateFromBinaryPb(cfg.SerializeAsString()));
93
94 file_type->add_platform_settings();
95 EXPECT_EQ(FileTypePolicies::UpdateResult::SUCCESS,
96 policies_.PopulateFromBinaryPb(cfg.SerializeAsString()));
97 }
98
99 TEST_F(FileTypePoliciesTest, BadUpdateFromExisting) {
100 // Make a minimum viable config
101 DownloadFileTypeConfig cfg;
102 cfg.mutable_default_file_type()->add_platform_settings();
103 cfg.add_file_types()->add_platform_settings();
104 cfg.set_version_id(2);
105 EXPECT_EQ(FileTypePolicies::UpdateResult::SUCCESS,
106 policies_.PopulateFromBinaryPb(cfg.SerializeAsString()));
107
108 // Can't update to the same version
109 EXPECT_EQ(FileTypePolicies::UpdateResult::FAILED_VERSION_CHECK,
110 policies_.PopulateFromBinaryPb(cfg.SerializeAsString()));
111
112 // Can't go backward
113 cfg.set_version_id(1);
114 EXPECT_EQ(FileTypePolicies::UpdateResult::FAILED_VERSION_CHECK,
115 policies_.PopulateFromBinaryPb(cfg.SerializeAsString()));
116 }
117 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698