| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 "components/browser_watcher/system_session_analyzer_win.h" |
| 6 |
| 7 #include <utility> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/time/time.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace browser_watcher { |
| 15 |
| 16 namespace { |
| 17 |
| 18 const uint16_t kIdSessionStart = 6005U; |
| 19 const uint16_t kIdSessionEnd = 6006U; |
| 20 const uint16_t kIdSessionEndUnclean = 6008U; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 // Ensure the fetcher retrieves events. |
| 25 // Note: this test fails if the host system doesn't have at least 1 prior |
| 26 // session. |
| 27 TEST(SystemSessionEventFetcherTest, TestRetrieval) { |
| 28 SystemSessionEventFetcher fetcher(1); |
| 29 std::vector<SystemSessionEventFetcher::EventInfo> events; |
| 30 ASSERT_TRUE(fetcher.FetchEvents(&events)); |
| 31 EXPECT_EQ(3U, events.size()); |
| 32 } |
| 33 |
| 34 // Ensure the fetcher's retrieved events conform to our expectations. This |
| 35 // conformance is validated using the SystemSessionAnalyzer. Note that the |
| 36 // ability of SystemSessionAnalyzer to validate the conformance is tested below. |
| 37 // Note: this test fails if the host system doesn't have at least 1 prior |
| 38 // session. |
| 39 TEST(SystemSessionEventFetcherTest, NoEvent) { |
| 40 SystemSessionEventFetcher fetcher(1U); |
| 41 SystemSessionAnalyzer analyzer(&fetcher); |
| 42 EXPECT_EQ(SystemSessionAnalyzer::CLEAN, |
| 43 analyzer.IsSessionUnclean(base::Time::Now())); |
| 44 } |
| 45 |
| 46 class StubSystemSessionEventFetcher : public SystemSessionEventFetcher { |
| 47 public: |
| 48 StubSystemSessionEventFetcher() : SystemSessionEventFetcher(10U) {} |
| 49 |
| 50 bool FetchEvents(std::vector<EventInfo>* event_infos) const override { |
| 51 DCHECK(event_infos); |
| 52 *event_infos = events_; |
| 53 return true; |
| 54 } |
| 55 |
| 56 void AddEvent(const EventInfo& info) { events_.push_back(info); } |
| 57 |
| 58 private: |
| 59 std::vector<EventInfo> events_; |
| 60 }; |
| 61 |
| 62 TEST(SystemSessionAnalyzerTest, StandardCase) { |
| 63 StubSystemSessionEventFetcher fetcher; |
| 64 |
| 65 base::Time time = base::Time::Now(); |
| 66 fetcher.AddEvent({kIdSessionStart, time}); |
| 67 fetcher.AddEvent( |
| 68 {kIdSessionEndUnclean, time - base::TimeDelta::FromSeconds(10)}); |
| 69 fetcher.AddEvent({kIdSessionStart, time - base::TimeDelta::FromSeconds(20)}); |
| 70 fetcher.AddEvent({kIdSessionEnd, time - base::TimeDelta::FromSeconds(22)}); |
| 71 fetcher.AddEvent({kIdSessionStart, time - base::TimeDelta::FromSeconds(28)}); |
| 72 |
| 73 SystemSessionAnalyzer analyzer(&fetcher); |
| 74 |
| 75 EXPECT_EQ(SystemSessionAnalyzer::OUTSIDE_RANGE, |
| 76 analyzer.IsSessionUnclean(time - base::TimeDelta::FromSeconds(30))); |
| 77 EXPECT_EQ(SystemSessionAnalyzer::CLEAN, |
| 78 analyzer.IsSessionUnclean(time - base::TimeDelta::FromSeconds(25))); |
| 79 EXPECT_EQ(SystemSessionAnalyzer::UNCLEAN, |
| 80 analyzer.IsSessionUnclean(time - base::TimeDelta::FromSeconds(20))); |
| 81 EXPECT_EQ(SystemSessionAnalyzer::UNCLEAN, |
| 82 analyzer.IsSessionUnclean(time - base::TimeDelta::FromSeconds(15))); |
| 83 EXPECT_EQ(SystemSessionAnalyzer::UNCLEAN, |
| 84 analyzer.IsSessionUnclean(time - base::TimeDelta::FromSeconds(10))); |
| 85 EXPECT_EQ(SystemSessionAnalyzer::CLEAN, |
| 86 analyzer.IsSessionUnclean(time - base::TimeDelta::FromSeconds(5))); |
| 87 EXPECT_EQ(SystemSessionAnalyzer::CLEAN, |
| 88 analyzer.IsSessionUnclean(time + base::TimeDelta::FromSeconds(5))); |
| 89 } |
| 90 |
| 91 TEST(SystemSessionAnalyzerTest, NoEvent) { |
| 92 StubSystemSessionEventFetcher fetcher; |
| 93 SystemSessionAnalyzer analyzer(&fetcher); |
| 94 EXPECT_EQ(SystemSessionAnalyzer::FAILED, |
| 95 analyzer.IsSessionUnclean(base::Time::Now())); |
| 96 } |
| 97 |
| 98 TEST(SystemSessionAnalyzerTest, TimeInversion) { |
| 99 StubSystemSessionEventFetcher fetcher; |
| 100 |
| 101 base::Time time = base::Time::Now(); |
| 102 fetcher.AddEvent({kIdSessionStart, time}); |
| 103 fetcher.AddEvent({kIdSessionEnd, time + base::TimeDelta::FromSeconds(1)}); |
| 104 fetcher.AddEvent({kIdSessionStart, time - base::TimeDelta::FromSeconds(1)}); |
| 105 |
| 106 SystemSessionAnalyzer analyzer(&fetcher); |
| 107 EXPECT_EQ(SystemSessionAnalyzer::FAILED, |
| 108 analyzer.IsSessionUnclean(base::Time::Now())); |
| 109 } |
| 110 |
| 111 TEST(SystemSessionAnalyzerTest, IdInversion) { |
| 112 StubSystemSessionEventFetcher fetcher; |
| 113 |
| 114 base::Time time = base::Time::Now(); |
| 115 fetcher.AddEvent({kIdSessionStart, time}); |
| 116 fetcher.AddEvent({kIdSessionStart, time - base::TimeDelta::FromSeconds(1)}); |
| 117 fetcher.AddEvent({kIdSessionEnd, time - base::TimeDelta::FromSeconds(2)}); |
| 118 |
| 119 SystemSessionAnalyzer analyzer(&fetcher); |
| 120 EXPECT_EQ(SystemSessionAnalyzer::FAILED, |
| 121 analyzer.IsSessionUnclean(base::Time::Now())); |
| 122 } |
| 123 |
| 124 } // namespace browser_watcher |
| OLD | NEW |