File: quirks_browsertest.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 (141 lines) | stat: -rw-r--r-- 5,160 bytes parent folder | download | duplicates (3)
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
134
135
136
137
138
139
140
141
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/compiler_specific.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/quirks/quirks_manager.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"

namespace quirks {

namespace {

const char kFakeIccJson[] = "{\n  \"icc\": \"AAAIkCAgICACEAAA\"\n}";
const uint8_t kFakeIccData[] = {0x00, 0x00, 0x08, 0x90, 0x20, 0x20,
                                0x20, 0x20, 0x02, 0x10, 0x00, 0x00};

// Full path to fake icc file in <tmp test directory>/display_profiles/.
base::FilePath GetPathForIccFile(int64_t product_id) {
  return QuirksManager::Get()->display_profile_path().Append(
      quirks::IdToFileName(product_id));
}

}  // namespace

class QuirksBrowserTest : public InProcessBrowserTest {
 public:
  QuirksBrowserTest() {
    test_url_loader_factory_.SetInterceptor(base::BindLambdaForTesting(
        [this](const network::ResourceRequest& request) {
          if (find_fake_file_) {
            test_url_loader_factory_.AddResponse(request.url.spec(),
                                                 kFakeIccJson);
          } else {
            test_url_loader_factory_.AddResponse(request.url.spec(), "",
                                                 net::HTTP_NOT_FOUND);
          }
        }));
  }

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

 protected:
  ~QuirksBrowserTest() override = default;

  // Query QuirksManager for icc file, then run msg loop to wait for callback.
  // |find_fake_file| indicates that URLLoaderFactory should respond with
  // success.
  void TestQuirksClient(int64_t product_id, bool find_fake_file) {
    find_fake_file_ = find_fake_file;

    base::RunLoop run_loop;
    end_message_loop_ = run_loop.QuitClosure();

    quirks::QuirksManager::Get()->RequestIccProfilePath(
        product_id, std::string(),
        base::BindOnce(&QuirksBrowserTest::OnQuirksClientFinished,
                       base::Unretained(this)));

    run_loop.Run();

    test_url_loader_factory_.ClearResponses();
  }

  // Callback from RequestIccProfilePath().
  void OnQuirksClientFinished(const base::FilePath& path, bool downloaded) {
    icc_path_ = path;
    file_existed_ = !downloaded;
    ASSERT_TRUE(!end_message_loop_.is_null());
    std::move(end_message_loop_).Run();
  }

  void SetUpOnMainThread() override {
    // NOTE: QuirksManager::Initialize() isn't necessary here, since it'll be
    // called in `ChromeBrowserMainPartsAsh::PreMainMessageLoopRun()`.

    // Create display_profiles subdirectory under temp profile directory.
    const base::FilePath path = QuirksManager::Get()->display_profile_path();
    base::File::Error error = base::File::FILE_OK;
    bool created = base::CreateDirectoryAndGetError(path, &error);
    ASSERT_TRUE(created);

    // Quirks clients can't run until after login.
    quirks::QuirksManager::Get()->OnLoginCompleted();

    // Set up interception for URL requests.
    QuirksManager::Get()->SetURLLoaderFactoryForTests(
        base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
            &test_url_loader_factory_));
  }

  base::OnceClosure end_message_loop_;  // Callback to terminate message loop.
  base::FilePath icc_path_;         // Path to icc file if found or downloaded.
  bool file_existed_ = false;       // File was previously downloaded.
  bool find_fake_file_ = false;     // Return success from Quirks server
                                    // request.

  network::TestURLLoaderFactory test_url_loader_factory_;
};

IN_PROC_BROWSER_TEST_F(QuirksBrowserTest, DownloadIccFile) {
  // Request a file from fake Quirks Server, verify that file is written with
  // correct location and data.
  TestQuirksClient(0x0000aaaa, true);
  base::FilePath path = GetPathForIccFile(0x0000aaaa);
  EXPECT_EQ(icc_path_, path);
  EXPECT_EQ(file_existed_, false);
  {
    base::ScopedAllowBlockingForTesting allow_io;
    EXPECT_TRUE(base::PathExists(path));
    char data[32];
    ReadFile(path, data, sizeof(data));
    EXPECT_EQ(0, UNSAFE_TODO(memcmp(data, kFakeIccData, sizeof(kFakeIccData))));
  }

  // Retest same file, this time expect it to already exist.
  TestQuirksClient(0x0000aaaa, true);
  EXPECT_EQ(icc_path_, path);
  EXPECT_EQ(file_existed_, true);

  // Finally, request a file that doesn't exist on fake Quirks Server.
  TestQuirksClient(0x1111bbbb, false);
  EXPECT_EQ(icc_path_, base::FilePath());
  EXPECT_EQ(file_existed_, false);
  {
    base::ScopedAllowBlockingForTesting allow_io;
    EXPECT_FALSE(base::PathExists(GetPathForIccFile(0x1111bbbb)));
  }
}

}  // namespace quirks