Commit 33fbc186 authored by G. Silva's avatar G. Silva Committed by Commit Bot

Create golden file loader for the time limit consistency tests.

Bug: 935711
Change-Id: Id4f5c77ab7e8d783507b2bdb0000ce5fcb2b417e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1487473Reviewed-by: default avatarMichael Giuffrida <michaelpg@chromium.org>
Reviewed-by: default avatarHenrique Grandinetti <hgrandinetti@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Commit-Queue: G. Silva <gfaus@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638569}
parent 39e6228e
......@@ -2587,6 +2587,7 @@ source_set("unit_tests") {
":arc_test_support",
":attestation_proto",
":test_support",
":time_limit_tests",
":user_activity_event_proto",
"//ash",
"//ash/system/message_center/arc:test_support",
......@@ -2757,3 +2758,35 @@ fuzzer_test("smb_url_fuzzer") {
seed_corpus = "smb_client/fuzzer_data/smb_url_corpus"
}
# Build target related to the time limit processor tests. Must be kept
# separated from the other unit tests because it is the only one allowed
# to use the :protobuf_full dependency.
source_set("time_limit_tests") {
testonly = true
check_includes = false
sources = [
"child_accounts/time_limit_consistency_test/consistency_golden_loader.cc",
"child_accounts/time_limit_consistency_test/consistency_golden_loader_unittest.cc",
]
deps = [
":chromeos",
":consistency_golden_proto",
"//base",
"//testing/gmock",
"//testing/gtest",
"//third_party/protobuf:protobuf_full",
]
data = [
"child_accounts/time_limit_consistency_test/goldens/",
"child_accounts/time_limit_consistency_test/test_goldens/",
]
}
proto_library("consistency_golden_proto") {
sources = [
"child_accounts/time_limit_consistency_test/goldens/consistency_golden.proto",
]
generate_python = false
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/child_accounts/time_limit_consistency_test/consistency_golden_loader.h"
#include "base/files/dir_reader_posix.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl.h"
#include "third_party/protobuf/src/google/protobuf/text_format.h"
namespace chromeos {
namespace time_limit_consistency {
namespace {
base::FilePath GetGoldensPath() {
base::FilePath path;
base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
return path.Append(
FILE_PATH_LITERAL("chrome/browser/chromeos/child_accounts/"
"time_limit_consistency_test/goldens"));
}
} // namespace
std::vector<GoldenParam> LoadGoldenCases() {
return LoadGoldenCasesFromPath(GetGoldensPath());
}
std::vector<GoldenParam> LoadGoldenCasesFromPath(
const base::FilePath& directory_path) {
std::vector<GoldenParam> golden_params_list;
base::DirReaderPosix dir_reader(directory_path.value().c_str());
while (dir_reader.Next()) {
if (!base::EndsWith(dir_reader.name(), ".textproto",
base::CompareCase::INSENSITIVE_ASCII)) {
continue;
}
ConsistencyGolden golden_suite;
base::File golden_file(directory_path.Append(dir_reader.name()),
base::File::FLAG_OPEN | base::File::FLAG_READ);
google::protobuf::io::FileInputStream stream(golden_file.GetPlatformFile());
google::protobuf::TextFormat::Parse(&stream, &golden_suite);
// Ignore suites that don't include CHROME_OS as a supported platform.
bool chromeos_supported =
std::count(golden_suite.supported_platforms().begin(),
golden_suite.supported_platforms().end(), CHROME_OS) > 0;
if (!chromeos_supported)
continue;
std::string suite_name = dir_reader.name();
base::ReplaceFirstSubstringAfterOffset(&suite_name, 0, ".textproto", "");
for (int i = 0; i < golden_suite.cases_size(); i++) {
golden_params_list.push_back(
GoldenParam({suite_name, i, golden_suite.cases(i)}));
}
}
return golden_params_list;
}
} // namespace time_limit_consistency
} // namespace chromeos
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// A utility for loading golden files to be used by the time limit processor
// consistency tests.
#ifndef CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMIT_CONSISTENCY_TEST_CONSISTENCY_GOLDEN_LOADER_H_
#define CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMIT_CONSISTENCY_TEST_CONSISTENCY_GOLDEN_LOADER_H_
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "chrome/browser/chromeos/child_accounts/time_limit_consistency_test/goldens/consistency_golden.pb.h"
namespace chromeos {
namespace time_limit_consistency {
// Holds information for one golden case and metadata used to generate the name
// for its test case (i.e. the name of the golden file it belongs and its index
// inside it).
struct GoldenParam {
const std::string suite_name;
const int index;
const ConsistencyGoldenCase golden_case;
};
// Loads all cases from all available golden files into a list of GoldenParams.
std::vector<GoldenParam> LoadGoldenCases();
// Loads all cases from all golden files from a given path into a list of
// GoldenParams. LoadGoldenCases() uses this function under the hood. Should not
// be called directly except for testing the golden loader itself.
std::vector<GoldenParam> LoadGoldenCasesFromPath(
const base::FilePath& directory_path);
} // namespace time_limit_consistency
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMIT_CONSISTENCY_TEST_CONSISTENCY_GOLDEN_LOADER_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/child_accounts/time_limit_consistency_test/consistency_golden_loader.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "chrome/browser/chromeos/child_accounts/time_limit_consistency_test/proto_matcher.h"
#include "chrome/browser/chromeos/child_accounts/time_limit_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace utils = time_limit_test_utils;
namespace time_limit_consistency {
using ConsistencyGoldenLoaderTest = testing::Test;
base::FilePath GetTestGoldensPath() {
base::FilePath path;
base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
return path.Append(
FILE_PATH_LITERAL("chrome/browser/chromeos/child_accounts/"
"time_limit_consistency_test/test_goldens"));
}
// Expected outcome is to ignore the test_golden_unsupported suite and return
// only the case within test_golden.
TEST_F(ConsistencyGoldenLoaderTest, LoadTestGoldenCases) {
std::vector<GoldenParam> goldens_list =
LoadGoldenCasesFromPath(GetTestGoldensPath());
ConsistencyGoldenCase golden_case;
golden_case.mutable_current_state()->set_time_millis(42);
ASSERT_EQ(goldens_list.size(), 1ul);
EXPECT_EQ(goldens_list[0].suite_name, "test_golden");
EXPECT_EQ(goldens_list[0].index, 0);
EXPECT_THAT(goldens_list[0].golden_case, EqualsProto(golden_case));
}
} // namespace time_limit_consistency
} // namespace chromeos
syntax = "proto2";
// Used to generate the ChromeOS C++ namespace
package chromeos.time_limit_consistency;
// Used to generate the Java package
option java_package = "com.google.kids.timelimit.consistency";
option java_multiple_files = true;
// The platforms where the test suite may be supported.
enum SupportedPlatform {
UNSPECIFIED_PLATFORM = 0;
ANDROID = 1;
CHROME_OS = 2;
}
// Policies which may be active.
enum ConsistencyGoldenPolicy {
UNSPECIFIED_POLICY = 0;
NO_ACTIVE_POLICY = 1;
OVERRIDE = 2;
FIXED_LIMIT = 3;
USAGE_LIMIT = 4;
}
// Days of the week.
enum ConsistencyGoldenEffectiveDay {
UNSPECIFIED_EFFECTIVE_DAY = 0;
MONDAY = 1;
TUESDAY = 2;
WEDNESDAY = 3;
THURSDAY = 4;
FRIDAY = 5;
SATURDAY = 6;
SUNDAY = 7;
}
// The main object, represents one test suite (and one golden file).
message ConsistencyGolden {
// The platforms where the test is supported. Required
repeated SupportedPlatform supported_platforms = 1;
// A list of test cases. Required
repeated ConsistencyGoldenCase cases = 2;
}
// Message representing one test case
message ConsistencyGoldenCase {
// Input policy data. Required
optional ConsistencyGoldenInput input = 1;
// Simulates the current state when executing. Required
optional ConsistencyGoldenCurrentState current_state = 2;
// The test's output, used for both the expected and the actual results.
// Required
optional ConsistencyGoldenOutput output = 3;
}
// The policies configured by the parent.
message ConsistencyGoldenInput {
// List of bedtime configurations for different days of the week.
repeated ConsistencyGoldenWindowLimitEntry window_limits = 1;
}
// Bedtime configuration for a given day.
message ConsistencyGoldenWindowLimitEntry {
// Which day of the week this configuration relates to. Required
optional ConsistencyGoldenEffectiveDay effective_day = 1;
// At which hour and minute this bedtime should start. Required
optional ConsistencyGoldenTimeOfDay starts_at = 2;
// At which hour and minute this bedtime should end. Required
optional ConsistencyGoldenTimeOfDay ends_at = 3;
}
// Represents a moment of a day.
message ConsistencyGoldenTimeOfDay {
// A given hour. [0-23]. Required
optional int32 hour = 1;
// A given minute. [0-59]. Required
optional int32 minute = 2;
}
// Information to represent the current state when executing.
message ConsistencyGoldenCurrentState {
// A timestamp for the desired current time. Required
optional int64 time_millis = 1;
// String representing the desired timezone, formatted like "GMT+2"/"GMT-3"
// or "America/Sao_Paulo". Required
optional string timezone = 2;
}
// Information on the output.
message ConsistencyGoldenOutput {
// Whether the device is locked. Required
optional bool is_locked = 1;
// What is the policy currently taking place. Required
optional ConsistencyGoldenPolicy active_policy = 2;
// Timestamp of when is the device supposed to unlock.
// This field must be present if and only if the device is locked.
optional int64 next_unlocking_time_millis = 3;
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// A gMock matcher for comparing protos and producing a human-readable
// message if the assertion fails.
#ifndef CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMIT_CONSISTENCY_TEST_PROTO_MATCHER_H_
#define CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMIT_CONSISTENCY_TEST_PROTO_MATCHER_H_
#include <string>
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/protobuf/src/google/protobuf/text_format.h"
namespace chromeos {
namespace time_limit_consistency {
MATCHER_P(EqualsProto, message, "equals golden proto") {
std::string expected_serialized, actual_serialized;
message.SerializeToString(&expected_serialized);
arg.SerializeToString(&actual_serialized);
if (expected_serialized == actual_serialized) {
return true;
}
std::string expected_readable, actual_readable;
google::protobuf::TextFormat::PrintToString(message, &expected_readable);
google::protobuf::TextFormat::PrintToString(arg, &actual_readable);
*result_listener << "\n\noutput parses to: \n----------\n"
<< actual_readable
<< "---------\n\n and should be: \n----------\n"
<< expected_readable << "----------";
return expected_serialized == actual_serialized;
}
} // namespace time_limit_consistency
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMIT_CONSISTENCY_TEST_PROTO_MATCHER_H_
supported_platforms: CHROME_OS
cases {
current_state {
time_millis: 42
}
}
......@@ -222,6 +222,10 @@ static_library("protobuf_full") {
# The SQLite fuzzer's corpus generator needs protobuf_full and is not
# included in Chrome.
"//third_party/sqlite:sqlite3_lpm_corpus_gen",
# Some tests inside ChromeOS need reflection to parse golden files.
# Not included in production code.
"//chrome/browser/chromeos:time_limit_tests",
]
sources = protobuf_lite_sources + [
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment