Flutter Engine
testing.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "testing.h"
6 
7 #include "flutter/fml/file.h"
8 
9 namespace flutter {
10 namespace testing {
11 
12 std::string GetCurrentTestName() {
13  return ::testing::UnitTest::GetInstance()->current_test_info()->name();
14 }
15 
17  auto fixtures_directory =
19  false, // create
20  fml::FilePermission::kRead // permission
21  );
22 
23  if (!fixtures_directory.is_valid()) {
24  FML_LOG(ERROR) << "Could not open fixtures directory.";
25  return {};
26  }
27  return fixtures_directory;
28 }
29 
30 fml::UniqueFD OpenFixture(std::string fixture_name) {
31  if (fixture_name.size() == 0) {
32  FML_LOG(ERROR) << "Invalid fixture name.";
33  return {};
34  }
35 
36  auto fixtures_directory = OpenFixturesDirectory();
37 
38  auto fixture_fd = fml::OpenFile(fixtures_directory, // base directory
39  fixture_name.c_str(), // path
40  false, // create
41  fml::FilePermission::kRead // permission
42  );
43  if (!fixture_fd.is_valid()) {
44  FML_LOG(ERROR) << "Could not open fixture for path: " << GetFixturesPath()
45  << "/" << fixture_name << ".";
46  return {};
47  }
48 
49  return fixture_fd;
50 }
51 
52 std::unique_ptr<fml::Mapping> OpenFixtureAsMapping(std::string fixture_name) {
53  return fml::FileMapping::CreateReadOnly(OpenFixture(fixture_name));
54 }
55 
56 bool MemsetPatternSetOrCheck(uint8_t* buffer, size_t size, MemsetPatternOp op) {
57  if (buffer == nullptr) {
58  return false;
59  }
60 
61  auto pattern = reinterpret_cast<const uint8_t*>("dErP");
62  constexpr auto pattern_length = 4;
63 
64  uint8_t* start = buffer;
65  uint8_t* p = buffer;
66 
67  while ((start + size) - p >= pattern_length) {
68  switch (op) {
70  memmove(p, pattern, pattern_length);
71  break;
73  if (memcmp(pattern, p, pattern_length) != 0) {
74  return false;
75  }
76  break;
77  };
78  p += pattern_length;
79  }
80 
81  if ((start + size) - p != 0) {
82  switch (op) {
84  memmove(p, pattern, (start + size) - p);
85  break;
87  if (memcmp(pattern, p, (start + size) - p) != 0) {
88  return false;
89  }
90  break;
91  }
92  }
93 
94  return true;
95 }
96 
97 bool MemsetPatternSetOrCheck(std::vector<uint8_t>& buffer, MemsetPatternOp op) {
98  return MemsetPatternSetOrCheck(buffer.data(), buffer.size(), op);
99 }
100 
101 } // namespace testing
102 } // namespace flutter
bool MemsetPatternSetOrCheck(uint8_t *buffer, size_t size, MemsetPatternOp op)
Depending on the operation, either scribbles a known pattern into the buffer or checks if that patter...
Definition: testing.cc:56
std::string GetCurrentTestName()
Gets the name of the currently running test. This is useful in generating logs or assets based on tes...
Definition: testing.cc:12
std::unique_ptr< fml::Mapping > OpenFixtureAsMapping(std::string fixture_name)
Opens a fixture of the given file name and returns a mapping to its contents.
Definition: testing.cc:52
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
#define FML_LOG(severity)
Definition: logging.h:65
fml::UniqueFD OpenDirectory(const char *path, bool create_if_necessary, FilePermission permission)
Definition: file_posix.cc:94
fml::UniqueFD OpenFixturesDirectory()
Opens the fixtures directory for the unit-test harness.
Definition: testing.cc:16
fml::UniqueFD OpenFixture(std::string fixture_name)
Opens a fixture of the given file name.
Definition: testing.cc:30
const char * GetFixturesPath()
Returns the directory containing the test fixture for the target if this target has fixtures configur...
fml::UniqueFD OpenFile(const char *path, bool create_if_necessary, FilePermission permission)
This can open a directory on POSIX, but not on Windows.
Definition: file_posix.cc:64
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition: mapping.cc:18