OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/browser/metrics/extension_metrics.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "components/metrics/proto/system_profile.pb.h" | |
10 #include "extensions/common/extension.h" | |
11 #include "extensions/common/extension_builder.h" | |
12 #include "extensions/common/extension_set.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace { | |
16 | |
17 class TestHashedExtensionMetrics : public HashedExtensionMetrics { | |
18 public: | |
19 explicit TestHashedExtensionMetrics(uint64 client_id) | |
20 : HashedExtensionMetrics(client_id) {} | |
21 | |
22 // Makes the protected HashExtension method available to testing code. | |
23 using HashedExtensionMetrics::HashExtension; | |
24 | |
25 protected: | |
26 // Override the GetInstalledExtensions method to return a set of extensions | |
27 // for tests. | |
28 virtual scoped_ptr<extensions::ExtensionSet> GetInstalledExtensions() | |
29 OVERRIDE { | |
30 scoped_ptr<extensions::ExtensionSet> extensions( | |
31 new extensions::ExtensionSet()); | |
32 scoped_refptr<const extensions::Extension> extension; | |
33 extension = extensions::ExtensionBuilder() | |
34 .SetManifest(extensions::DictionaryBuilder() | |
35 .Set("name", "Test extension") | |
36 .Set("version", "1.0.0") | |
37 .Set("manifest_version", 2)) | |
38 .SetID("ahfgeienlihckogmohjhadlkjgocpleb") | |
39 .Build(); | |
40 extensions->Insert(extension); | |
41 extension = extensions::ExtensionBuilder() | |
42 .SetManifest(extensions::DictionaryBuilder() | |
43 .Set("name", "Test extension 2") | |
44 .Set("version", "1.0.0") | |
45 .Set("manifest_version", 2)) | |
46 .SetID("pknkgggnfecklokoggaggchhaebkajji") | |
47 .Build(); | |
48 extensions->Insert(extension); | |
49 extension = extensions::ExtensionBuilder() | |
50 .SetManifest(extensions::DictionaryBuilder() | |
51 .Set("name", "Colliding Extension") | |
52 .Set("version", "1.0.0") | |
53 .Set("manifest_version", 2)) | |
54 .SetID("mdhofdjgenpkhlmddfaegdjddcecipmo") | |
55 .Build(); | |
56 extensions->Insert(extension); | |
57 return extensions.Pass(); | |
58 } | |
59 }; | |
60 | |
61 } // namespace | |
62 | |
63 // Checks that the hash function used to hide precise extension IDs produces | |
64 // the expected values. | |
65 TEST(HashedExtensionMetrics, HashExtension) { | |
66 EXPECT_EQ(978, TestHashedExtensionMetrics::HashExtension( | |
67 "ahfgeienlihckogmohjhadlkjgocpleb", 0)); | |
68 EXPECT_EQ(10, TestHashedExtensionMetrics::HashExtension( | |
69 "ahfgeienlihckogmohjhadlkjgocpleb", 3817)); | |
70 EXPECT_EQ(1007, TestHashedExtensionMetrics::HashExtension( | |
71 "pknkgggnfecklokoggaggchhaebkajji", 3817)); | |
72 EXPECT_EQ(10, TestHashedExtensionMetrics::HashExtension( | |
73 "mdhofdjgenpkhlmddfaegdjddcecipmo", 3817)); | |
74 } | |
75 | |
76 // Checks that the fake set of extensions provided by | |
77 // TestHashedExtensionMetrics is encoded properly. | |
78 TEST(HashedExtensionMetrics, SystemProtoEncoding) { | |
79 metrics::SystemProfileProto system_profile; | |
80 TestHashedExtensionMetrics extension_metrics(0x3f1bfee9); | |
81 extension_metrics.WriteExtensionList(&system_profile); | |
82 ASSERT_EQ(2, system_profile.occupied_extension_bucket_size()); | |
83 EXPECT_EQ(10, system_profile.occupied_extension_bucket(0)); | |
84 EXPECT_EQ(1007, system_profile.occupied_extension_bucket(1)); | |
85 } | |
OLD | NEW |