File: api_activity_logger_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (88 lines) | stat: -rw-r--r-- 3,338 bytes parent folder | download
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/renderer/api_activity_logger.h"

#include <string>

#include "base/run_loop.h"
#include "base/values.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/extension_messages.h"
#include "extensions/common/features/feature.h"
#include "extensions/renderer/bindings/api_binding_test.h"
#include "extensions/renderer/bindings/api_binding_test_util.h"
#include "extensions/renderer/ipc_message_sender.h"
#include "extensions/renderer/native_extension_bindings_system_test_base.h"
#include "extensions/renderer/script_context.h"
#include "extensions/renderer/script_context_set.h"
#include "extensions/renderer/test_extensions_renderer_client.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_test_sink.h"

namespace extensions {

namespace {

class ScopedAllowActivityLogging {
 public:
  ScopedAllowActivityLogging() { APIActivityLogger::set_log_for_testing(true); }

  ScopedAllowActivityLogging(const ScopedAllowActivityLogging&) = delete;
  ScopedAllowActivityLogging& operator=(const ScopedAllowActivityLogging&) =
      delete;

  ~ScopedAllowActivityLogging() {
    APIActivityLogger::set_log_for_testing(false);
  }
};

}  // namespace

using ActivityLoggerTest = APIBindingTest;
using ActivityLogCallType = IPCMessageSender::ActivityLogCallType;

// Regression test for crbug.com/740866.
TEST_F(ActivityLoggerTest, DontCrashOnUnconvertedValues) {
  TestExtensionsRendererClient client;
  std::set<ExtensionId> extension_ids;
  ScriptContextSet script_context_set(&extension_ids);

  ScopedAllowActivityLogging scoped_allow_activity_logging;

  v8::HandleScope handle_scope(isolate());
  v8::Local<v8::Context> context = MainContext();

  scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
  extension_ids.insert(extension->id());
  const Feature::Context kContextType = Feature::BLESSED_EXTENSION_CONTEXT;
  script_context_set.AddForTesting(std::make_unique<ScriptContext>(
      context, nullptr, extension.get(), kContextType, extension.get(),
      kContextType));

  v8::LocalVector<v8::Value> args(isolate(), {v8::Undefined(isolate())});

  std::unique_ptr<TestIPCMessageSender> ipc_sender =
      std::make_unique<testing::StrictMock<TestIPCMessageSender>>();
  EXPECT_CALL(*ipc_sender,
              SendActivityLogIPC(extension->id(), ActivityLogCallType::APICALL,
                                 testing::_, testing::_, testing::_))
      .WillOnce([&](const ExtensionId& extension_id,
                    ActivityLogCallType call_type, const std::string& call_name,
                    base::Value::List args, const std::string& extra) {
        EXPECT_EQ("someApiMethod", call_name);
        ASSERT_EQ(1u, args.size());
        EXPECT_EQ(base::Value::Type::NONE, args[0].type());
      });

  APIActivityLogger::LogAPICall(ipc_sender.get(), context, "someApiMethod",
                                args);

  ScriptContext* script_context = script_context_set.GetByV8Context(context);
  script_context_set.Remove(script_context);
  base::RunLoop().RunUntilIdle();  // Let script context destruction complete.
  ::testing::Mock::VerifyAndClearExpectations(ipc_sender.get());
}

}  // namespace extensions