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 142 143
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/printing/ipp_client_info_calculator.h"
#include <memory>
#include <string_view>
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_chromeos_version_info.h"
#include "base/time/time.h"
#include "chrome/browser/ash/policy/core/device_attributes_fake.h"
#include "chrome/browser/ash/settings/scoped_testing_cros_settings.h"
#include "chrome/browser/ash/settings/stub_cros_settings_provider.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "printing/mojom/print.mojom-forward.h"
#include "printing/mojom/print.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash::printing {
namespace {
using ::printing::mojom::IppClientInfo;
using ::printing::mojom::IppClientInfoPtr;
constexpr char kLsbRelease[] =
"CHROMEOS_RELEASE_NAME=Chrome OS\n"
"CHROMEOS_RELEASE_VERSION=15183.69.3\n";
void ExpectClientInfoEqual(const IppClientInfo& lhs, const IppClientInfo& rhs) {
EXPECT_EQ(lhs.client_name, rhs.client_name);
EXPECT_EQ(lhs.client_type, rhs.client_type);
EXPECT_EQ(lhs.client_patches, rhs.client_patches);
EXPECT_EQ(lhs.client_string_version, rhs.client_string_version);
EXPECT_EQ(lhs.client_version, rhs.client_version);
}
// Test fixture to test `IppClientInfoCalculator` with fake ChromeOS version,
// device attributes and `DevicePrintingClientNameTemplate` policy.
class IppClientInfoCalculatorTest : public testing::Test {
public:
IppClientInfoCalculatorTest() {
auto fake_device_attibutes =
std::make_unique<policy::FakeDeviceAttributes>();
device_attributes_ = fake_device_attibutes.get();
client_info_calculator_ = IppClientInfoCalculator::CreateForTesting(
std::move(fake_device_attibutes), "42");
}
void SetClientNameTemplatePolicy(std::string_view value) {
testing_cros_settings_.device_settings()->Set(
kDevicePrintingClientNameTemplate, base::Value(value));
}
policy::FakeDeviceAttributes* DeviceAttributes() {
return device_attributes_;
}
IppClientInfoPtr GetOsInfo() const {
return client_info_calculator_->GetOsInfo();
}
IppClientInfoPtr GetDeviceInfo() const {
return client_info_calculator_->GetDeviceInfo();
}
private:
base::test::ScopedChromeOSVersionInfo cros_version_info_{kLsbRelease,
base::Time()};
ScopedTestingCrosSettings testing_cros_settings_;
raw_ptr<policy::FakeDeviceAttributes, DanglingUntriaged> device_attributes_;
std::unique_ptr<IppClientInfoCalculator> client_info_calculator_;
};
TEST_F(IppClientInfoCalculatorTest, OsInfo) {
const IppClientInfoPtr os_info = GetOsInfo();
const IppClientInfo expected(IppClientInfo::ClientType::kOperatingSystem,
"ChromeOS", "15183.69.3", "42", std::nullopt);
ASSERT_TRUE(os_info);
ExpectClientInfoEqual(*os_info, expected);
}
TEST_F(IppClientInfoCalculatorTest, DeviceInfoUnsetPolicy) {
const IppClientInfoPtr device_info = GetDeviceInfo();
ASSERT_FALSE(device_info);
}
TEST_F(IppClientInfoCalculatorTest, DeviceInfoSimplePolicy) {
SetClientNameTemplatePolicy("chromebook");
const IppClientInfoPtr device_info = GetDeviceInfo();
const IppClientInfo expected(IppClientInfo::ClientType::kOther, "chromebook",
std::nullopt, "", std::nullopt);
ASSERT_TRUE(device_info);
ExpectClientInfoEqual(*device_info, expected);
}
TEST_F(IppClientInfoCalculatorTest, DeviceInfoPolicyWithVariables) {
DeviceAttributes()->SetFakeDeviceAssetId("asset-id");
DeviceAttributes()->SetFakeDirectoryApiId("1234-abcd");
DeviceAttributes()->SetFakeDeviceSerialNumber("serial");
DeviceAttributes()->SetFakeDeviceAnnotatedLocation("location");
SetClientNameTemplatePolicy(
"chromebook_${DEVICE_ASSET_ID}_${DEVICE_DIRECTORY_API_ID}_${DEVICE_"
"SERIAL_NUMBER}_${DEVICE_ANNOTATED_LOCATION}");
const IppClientInfoPtr device_info = GetDeviceInfo();
const IppClientInfo expected(IppClientInfo::ClientType::kOther,
"chromebook_asset-id_1234-abcd_serial_location",
std::nullopt, "", std::nullopt);
ASSERT_TRUE(device_info);
ExpectClientInfoEqual(*device_info, expected);
}
TEST_F(IppClientInfoCalculatorTest, DeviceInfoPolicyChange) {
{
const IppClientInfoPtr device_info = GetDeviceInfo();
ASSERT_FALSE(device_info);
}
{
SetClientNameTemplatePolicy("initial");
const IppClientInfoPtr device_info = GetDeviceInfo();
const IppClientInfo expected(IppClientInfo::ClientType::kOther, "initial",
std::nullopt, "", std::nullopt);
ASSERT_TRUE(device_info);
ExpectClientInfoEqual(*device_info, expected);
}
{
SetClientNameTemplatePolicy("changed");
const IppClientInfoPtr device_info = GetDeviceInfo();
const IppClientInfo expected(IppClientInfo::ClientType::kOther, "changed",
std::nullopt, "", std::nullopt);
ASSERT_TRUE(device_info);
ExpectClientInfoEqual(*device_info, expected);
}
}
} // namespace
} // namespace ash::printing
|