File: policy_logger_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rw-r--r-- 4,678 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/policy/core/common/policy_logger.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_mock_time_task_runner.h"
#include "components/policy/core/common/features.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

using testing::_;
using testing::ElementsAre;
using testing::Eq;
using testing::Property;

namespace policy {

namespace {

void AddLogs(const std::string& message, PolicyLogger* policy_logger) {
  LOG_POLICY(INFO, POLICY_FETCHING) << "Element added: " << message;
}

}  // namespace

class PolicyLoggerTest : public PlatformTest {
 public:
  PolicyLoggerTest() = default;
  ~PolicyLoggerTest() override = default;

 protected:
  // Clears the logs list and resets the deletion flag before the test and its
  // tasks are deleted. This is important to prevent tests from affecting each
  // other's results.
  void SetUp() override {
    policy::PolicyLogger::GetInstance()->ResetLoggerForTesting();
  }

  base::test::ScopedFeatureList scoped_feature_list_;
  base::test::SingleThreadTaskEnvironment task_environment{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
};

// Checks that the logger is enabled by feature and that `GetAsList` returns an
// updated list of logs.
TEST_F(PolicyLoggerTest, PolicyLoggingEnabled) {
  PolicyLogger* policy_logger = policy::PolicyLogger::GetInstance();

  size_t logs_size_before_adding = policy_logger->GetPolicyLogsSizeForTesting();
  AddLogs("when the feature is enabled.", policy_logger);

  EXPECT_EQ(policy_logger->GetAsList().size(), logs_size_before_adding + 1);
  EXPECT_EQ(*(policy_logger->GetAsList()[logs_size_before_adding]
                  .GetDict()
                  .FindString("message")),
            "Element added: when the feature is enabled.");
}

// Checks that the deletion of expired logs works as expected.
TEST_F(PolicyLoggerTest, DeleteOldLogs) {
  PolicyLogger* policy_logger = policy::PolicyLogger::GetInstance();
  policy_logger->EnableLogDeletion();
  size_t logs_size_before_adding = policy_logger->GetPolicyLogsSizeForTesting();

  AddLogs("First log at t=0.", policy_logger);
  AddLogs("Second log at t=0+delta.", policy_logger);

  base::TimeDelta first_time_elapsed = policy::PolicyLogger::kTimeToLive / 2;
  task_environment.FastForwardBy(first_time_elapsed + base::Minutes(1));
  AddLogs("Third log at t=TimeToLive/2.", policy_logger);

  // Check that the logs that were in the list for `kTimeToLive` minutes were
  // deleted and that the one that did not expire is still in the list.
  task_environment.FastForwardBy(first_time_elapsed);
  task_environment.RunUntilIdle();
  EXPECT_EQ(policy_logger->GetAsList().size(), size_t(1));
  EXPECT_EQ(*(policy_logger->GetAsList()[logs_size_before_adding]
                  .GetDict()
                  .FindString("message")),
            "Element added: Third log at t=TimeToLive/2.");

  // Check that the last log was deleted after `kTimeToLive` minutes to ensure
  // that a second deleting task was scheduled after deleting the old ones.
  task_environment.FastForwardBy(policy::PolicyLogger::kTimeToLive);
  task_environment.RunUntilIdle();
  EXPECT_EQ(policy_logger->GetAsList().size(), size_t(0));
}

// Checks that the first log  added is deleted when `PolicyLogger::kMaxLogSize`
// is exceeded.
TEST_F(PolicyLoggerTest, MaxSizeExceededDeletesOldestLog) {
  PolicyLogger* policy_logger = policy::PolicyLogger::GetInstance();

  AddLogs("First log that will be removed.", policy_logger);

  // Adds kMaxLogsSize` - 1 more elements until `kMaxLogsSize` is reached.
  for (int i = 0; i < static_cast<int>(policy::PolicyLogger::kMaxLogsSize) - 1;
       i++) {
    AddLogs(base::NumberToString(i + 1), policy_logger);
  }
  EXPECT_EQ(policy_logger->GetPolicyLogsSizeForTesting(),
            policy::PolicyLogger::kMaxLogsSize);

  AddLogs("Last log added and size is exceeded.", policy_logger);

  size_t current_size = policy_logger->GetPolicyLogsSizeForTesting();
  base::Value::List current_logs = policy_logger->GetAsList();

  EXPECT_EQ(current_size, policy::PolicyLogger::kMaxLogsSize);

  EXPECT_EQ(*(current_logs[0].GetDict().FindString("message")),
            "Element added: 1");

  EXPECT_EQ(*(current_logs[current_size - 1].GetDict().FindString("message")),
            "Element added: Last log added and size is exceeded.");
}

}  // namespace policy