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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/types/expected.h"
#include "base/values.h"
#include "content/browser/aggregation_service/aggregation_service_test_utils.h"
#include "content/browser/attribution_reporting/interop/parser.h"
#include "content/browser/attribution_reporting/interop/runner.h"
#include "content/public/browser/network_service_util.h"
#include "content/public/test/content_test_suite_base.h"
#include "content/public/test/test_content_client_initializer.h"
#include "mojo/core/embedder/embedder.h"
namespace {
const content::aggregation_service::TestHpkeKey kHpkeKey;
class Env : public content::ContentTestSuiteBase {
public:
Env(int argc, char** argv) : ContentTestSuiteBase(argc, argv) {
Initialize();
content::ForceInProcessNetworkService();
// This initialization depends on the call to `Initialize()`, so we use a
// `unique_ptr` to defer initialization instead of storing the field
// directly.
test_content_initializer_ =
std::make_unique<content::TestContentClientInitializer>();
mojo::core::Init();
}
~Env() override = default;
private:
std::unique_ptr<content::TestContentClientInitializer>
test_content_initializer_;
};
std::optional<base::Value::Dict> ReadDictFromFile(
const base::CommandLine& cmd_line,
const char* flag) {
if (!cmd_line.HasSwitch(flag)) {
std::cerr << "must specify --" << flag << std::endl;
return std::nullopt;
}
std::string json;
if (!base::ReadFileToString(cmd_line.GetSwitchValuePath(flag), &json)) {
std::cerr << "failed to read " << flag << " path" << std::endl;
return std::nullopt;
}
auto result = base::JSONReader::ReadAndReturnValueWithError(json);
if (!result.has_value()) {
std::cerr << "failed to parse " << flag
<< " as JSON: " << result.error().message << std::endl;
return std::nullopt;
}
base::Value::Dict* dict = result->GetIfDict();
if (!dict) {
std::cerr << flag << " JSON must be a dictionary" << std::endl;
return std::nullopt;
}
return std::move(*dict);
}
} // namespace
// See //content/test/data/attribution_reporting/interop/README.md for details
// on the input format.
int main(int argc, char* argv[]) {
base::CommandLine::Init(argc, argv);
const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess();
auto input = ReadDictFromFile(cmd_line, "input");
if (!input.has_value()) {
return 1;
}
auto default_config_dict = ReadDictFromFile(cmd_line, "default_config");
if (!default_config_dict.has_value()) {
return 1;
}
auto default_config =
content::ParseAttributionInteropConfig(*std::move(default_config_dict));
if (!default_config.has_value()) {
std::cerr << "failed to parse default config: " << default_config.error()
<< std::endl;
return 1;
}
auto run =
content::AttributionInteropRun::Parse(*std::move(input), *default_config);
if (!run.has_value()) {
std::cerr << "failed to parse input: " << run.error() << std::endl;
return 1;
}
Env env(argc, argv);
auto output =
content::RunAttributionInteropSimulation(*std::move(run), kHpkeKey);
if (!output.has_value()) {
std::cerr << output.error() << std::endl;
return 1;
}
std::cout << output->ToJson() << std::endl;
return 0;
}
|