startup_unittest.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2020 The Chromium 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. #include "chromeos/startup/startup.h"
  5. #include <sys/mman.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #include <string>
  9. #include "base/command_line.h"
  10. #include "base/files/file_util.h"
  11. #include "base/files/scoped_file.h"
  12. #include "base/logging.h"
  13. #include "base/strings/string_number_conversions.h"
  14. #include "base/strings/string_piece.h"
  15. #include "base/test/scoped_command_line.h"
  16. #include "chromeos/startup/startup_switches.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. namespace chromeos {
  20. namespace {
  21. base::ScopedFD CreateMemoryFile(const base::StringPiece content) {
  22. base::ScopedFD file(memfd_create("test", 0));
  23. if (!file.is_valid()) {
  24. PLOG(ERROR) << "Failed to create a memory file";
  25. return base::ScopedFD();
  26. }
  27. if (!base::WriteFileDescriptor(file.get(), content)) {
  28. LOG(ERROR) << "Failed to write the data";
  29. return base::ScopedFD();
  30. }
  31. // Reset the cursor.
  32. if (lseek(file.get(), 0, SEEK_SET) < 0) {
  33. PLOG(ERROR) << "Failed to reset the file position";
  34. return base::ScopedFD();
  35. }
  36. return file;
  37. }
  38. } // namespace
  39. TEST(ChromeOSStartup, Startup) {
  40. constexpr char kTestData[] = "test test test test";
  41. base::ScopedFD file = CreateMemoryFile(kTestData);
  42. base::test::ScopedCommandLine scoped_command_line;
  43. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  44. // Release the FD. The FD is consumed in ReadStartupData().
  45. command_line->AppendSwitchASCII(switches::kCrosStartupDataFD,
  46. base::NumberToString(file.release()));
  47. absl::optional<std::string> data = ReadStartupData();
  48. EXPECT_EQ(data, kTestData);
  49. }
  50. TEST(ChromeOSStartup, NoFlag) {
  51. EXPECT_FALSE(ReadStartupData());
  52. }
  53. } // namespace chromeos