File: logger_impl_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (133 lines) | stat: -rw-r--r-- 4,572 bytes parent folder | download | duplicates (11)
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
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2020 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/media_router/browser/logger_impl.h"

#include "base/i18n/time_formatting.h"
#include "base/json/json_reader.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "components/media_router/common/mojom/logger.mojom-shared.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media_router {

constexpr char kComponent[] = "MyComponent";
constexpr char kMessage[] = "My message";
constexpr char kSinkId[] = "cast:xyzzy";
constexpr char kMediaSource[] = "cast:ABCDEFGH";
constexpr char kSessionId[] = "6789012345";

class LoggerImplTest : public testing::Test {
 protected:
  void LogInfoWithSinkId(const std::string& sink_id) {
    logger_.LogInfo(mojom::LogCategory::kRoute, kComponent, kMessage, sink_id,
                    kMediaSource, kSessionId);
  }
  void LogWarningWithSessionId(const std::string& session_id) {
    logger_.LogWarning(mojom::LogCategory::kRoute, kComponent, kMessage,
                       kSinkId, kMediaSource, session_id);
  }
  void LogErrorWithSource(const std::string& media_source) {
    logger_.LogError(mojom::LogCategory::kRoute, kComponent, kMessage, kSinkId,
                     media_source, kSessionId);
  }

  std::string GetSinkId(const std::string& logs_json) {
    return GetAttributeOfFirstEntry(logs_json, "sinkId");
  }

  std::string GetSessionId(const std::string& logs_json) {
    return GetAttributeOfFirstEntry(logs_json, "sessionId");
  }

  std::string GetMediaSource(const std::string& logs_json) {
    return GetAttributeOfFirstEntry(logs_json, "mediaSource");
  }

  LoggerImpl logger_;

 private:
  std::string GetAttributeOfFirstEntry(const std::string& logs_json,
                                       const std::string& attribute) {
    base::Value logs = base::JSONReader::Read(logs_json).value();
    return *logs.GetList()[0].GetDict().FindString(attribute);
  }
};

TEST_F(LoggerImplTest, RecordAndGetLogs) {
  const base::Time time1 = base::Time::Now();
  const base::Time time2 = time1 + base::Seconds(20);
  const std::string expected_logs =
      R"([
    {
      "severity": "Error",
      "category": "Route",
      "time": ")" +
      base::UTF16ToUTF8(base::TimeFormatTimeOfDayWithMilliseconds(time1)) +
      R"(",
      "component": "MyComponent",
      "message": "My message",
      "sinkId": "cast:xyzz",
      "mediaSource": "cast:ABCDEFGH",
      "sessionId": "678901234"
    },
    {
      "severity": "Info",
      "category": "UI",
      "time": ")" +
      base::UTF16ToUTF8(base::TimeFormatTimeOfDayWithMilliseconds(time2)) +
      R"(",
      "component": "Component 2",
      "message": "Message 2",
      "sinkId": "cast:abcd",
      "mediaSource": "cast:IJKLMNOP",
      "sessionId": "stuvwxyz0"
    }
  ])";

  logger_.Log(LoggerImpl::Severity::kError, mojom::LogCategory::kRoute, time1,
              kComponent, kMessage, kSinkId, kMediaSource, kSessionId);
  logger_.Log(LoggerImpl::Severity::kInfo, mojom::LogCategory::kUi, time2,
              "Component 2", "Message 2", "cast:abcdefgh", "cast:IJKLMNOP",
              "stuvwxyz0123");
  const std::string logs = logger_.GetLogsAsJson();
  EXPECT_EQ(base::JSONReader::Read(logs),
            base::JSONReader::Read(expected_logs));
}

TEST_F(LoggerImplTest, TruncateSinkId) {
  LogInfoWithSinkId(kSinkId);
  const std::string logs = logger_.GetLogsAsJson();
  EXPECT_EQ(GetSinkId(logs), "cast:xyzz");
}

TEST_F(LoggerImplTest, TruncateSessionId) {
  LogWarningWithSessionId(kSessionId);
  const std::string logs = logger_.GetLogsAsJson();
  EXPECT_EQ(GetSessionId(logs), "678901234");
}

TEST_F(LoggerImplTest, HandleMirroringMediaSource) {
  // A mirroring source should get logged as-is.
  const std::string source = "urn:x-org.chromium.media:source:tab:*";
  LogErrorWithSource(source);
  const std::string logs = logger_.GetLogsAsJson();
  EXPECT_EQ(GetMediaSource(logs), source);
}

TEST_F(LoggerImplTest, TrimCastMediaSource) {
  LogErrorWithSource("cast:ABCDEFGH?a=b&c=d");
  const std::string logs = logger_.GetLogsAsJson();
  EXPECT_EQ(GetMediaSource(logs), "cast:ABCDEFGH");
}

TEST_F(LoggerImplTest, TrimPresentationMediaSource) {
  LogErrorWithSource("https://presentation.example.com/receiver.html?a=b");
  const std::string logs = logger_.GetLogsAsJson();
  EXPECT_EQ(GetMediaSource(logs), "https://presentation.example.com/");
}

}  // namespace media_router