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

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: changes per jialiul, and set upstream CL Created 4 years, 7 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
« no previous file with comments | « chrome/common/safe_browsing/file_type_policies.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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) {
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.exe"));
47 DownloadFileType file_type = policies_.PolicyForFile(exe_file);
48 EXPECT_EQ("exe", file_type.extension());
49 EXPECT_EQ(0l, 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(0l, 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 EXPECT_EQ("", file_type.extension());
66 EXPECT_EQ(18l, file_type.uma_value());
67 EXPECT_EQ(false, file_type.is_archive());
68 EXPECT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE,
69 file_type.platform_settings(0).danger_level());
70 EXPECT_EQ(DownloadFileType::DISALLOW_AUTO_OPEN,
71 file_type.platform_settings(0).auto_open_hint());
72 EXPECT_EQ(DownloadFileType::SAMPLED_PING,
73 file_type.platform_settings(0).ping_setting());
74 }
75
76 TEST_F(FileTypePoliciesTest, BadProto) {
77 EXPECT_EQ(FileTypePolicies::UpdateResult::FAILED_EMPTY,
78 policies_.PopulateFromBinaryPb(std::string()));
79
80 EXPECT_EQ(FileTypePolicies::UpdateResult::FAILED_PROTO_PARSE,
81 policies_.PopulateFromBinaryPb("foobar"));
82
83 DownloadFileTypeConfig cfg;
84 cfg.set_sampled_ping_probability(0.1f);
85 EXPECT_EQ(FileTypePolicies::UpdateResult::FAILED_DEFAULT_SETTING_SET,
86 policies_.PopulateFromBinaryPb(cfg.SerializeAsString()));
87
88 cfg.mutable_default_file_type()->add_platform_settings();
89 auto file_type = cfg.add_file_types(); // This is missing a platform_setting.
90 EXPECT_EQ(FileTypePolicies::UpdateResult::FAILED_WRONG_SETTINGS_COUNT,
91 policies_.PopulateFromBinaryPb(cfg.SerializeAsString()));
92
93 file_type->add_platform_settings();
94 EXPECT_EQ(FileTypePolicies::UpdateResult::SUCCESS,
95 policies_.PopulateFromBinaryPb(cfg.SerializeAsString()));
96 }
97
98 TEST_F(FileTypePoliciesTest, BadUpdateFromExisting) {
99 // Make a minimum viable config
100 DownloadFileTypeConfig cfg;
101 cfg.mutable_default_file_type()->add_platform_settings();
102 cfg.add_file_types()->add_platform_settings();
103 cfg.set_version_id(2);
104 EXPECT_EQ(FileTypePolicies::UpdateResult::SUCCESS,
105 policies_.PopulateFromBinaryPb(cfg.SerializeAsString()));
106
107 // Can't update to the same version
108 EXPECT_EQ(FileTypePolicies::UpdateResult::FAILED_VERSION_CHECK,
109 policies_.PopulateFromBinaryPb(cfg.SerializeAsString()));
110
111 // Can't go backward
112 cfg.set_version_id(1);
113 EXPECT_EQ(FileTypePolicies::UpdateResult::FAILED_VERSION_CHECK,
114 policies_.PopulateFromBinaryPb(cfg.SerializeAsString()));
115 }
116 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/common/safe_browsing/file_type_policies.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698