1 // Copyright 2014 The Chromium OS 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 <brillo/osrelease_reader.h>
6
7 #include <base/files/file_util.h>
8 #include <base/files/scoped_temp_dir.h>
9 #include <gtest/gtest.h>
10
11 using std::string;
12
13 namespace brillo {
14
15 class OsReleaseReaderTest : public ::testing::Test {
16 public:
SetUp()17 void SetUp() override {
18 CHECK(temp_dir_.CreateUniqueTempDir());
19 osreleased_ = temp_dir_.GetPath().Append("etc").Append("os-release.d");
20 osrelease_ = temp_dir_.GetPath().Append("etc").Append("os-release");
21 base::CreateDirectory(osreleased_);
22 }
23
24 protected:
25 base::FilePath temp_file_, osrelease_, osreleased_;
26 base::ScopedTempDir temp_dir_;
27 OsReleaseReader store_; // reader under test.
28 };
29
TEST_F(OsReleaseReaderTest,MissingOsReleaseTest)30 TEST_F(OsReleaseReaderTest, MissingOsReleaseTest) {
31 store_.LoadTestingOnly(temp_dir_.GetPath());
32 }
33
TEST_F(OsReleaseReaderTest,MissingOsReleaseDTest)34 TEST_F(OsReleaseReaderTest, MissingOsReleaseDTest) {
35 base::DeleteFile(osreleased_, true);
36 store_.LoadTestingOnly(temp_dir_.GetPath());
37 }
38
TEST_F(OsReleaseReaderTest,CompleteTest)39 TEST_F(OsReleaseReaderTest, CompleteTest) {
40 string hello = "hello";
41 string ola = "ola";
42 string bob = "bob";
43 string osreleasecontent = "TEST_KEY=bonjour\nNAME=bob\n";
44
45 base::WriteFile(osreleased_.Append("TEST_KEY"), hello.data(), hello.size());
46 base::WriteFile(osreleased_.Append("GREETINGS"), ola.data(), ola.size());
47 base::WriteFile(osrelease_, osreleasecontent.data(), osreleasecontent.size());
48
49 store_.LoadTestingOnly(temp_dir_.GetPath());
50
51 string test_key_value;
52 ASSERT_TRUE(store_.GetString("TEST_KEY", &test_key_value));
53
54 string greetings_value;
55 ASSERT_TRUE(store_.GetString("GREETINGS", &greetings_value));
56
57 string name_value;
58 ASSERT_TRUE(store_.GetString("NAME", &name_value));
59
60 string nonexistent_value;
61 // Getting the string should fail if the key does not exist.
62 ASSERT_FALSE(store_.GetString("DOES_NOT_EXIST", &nonexistent_value));
63
64 // hello in chosen (from os-release.d) instead of bonjour from os-release.
65 ASSERT_EQ(hello, test_key_value);
66
67 // greetings is set to ola.
68 ASSERT_EQ(ola, greetings_value);
69
70 // Name from os-release is set.
71 ASSERT_EQ(bob, name_value);
72 }
73
TEST_F(OsReleaseReaderTest,NoNewLine)74 TEST_F(OsReleaseReaderTest, NoNewLine) {
75 // New lines should be stripped from os-release.d files.
76 string hello = "hello\n";
77 string bonjour = "bonjour\ngarbage";
78
79 base::WriteFile(osreleased_.Append("HELLO"), hello.data(), hello.size());
80 base::WriteFile(
81 osreleased_.Append("BONJOUR"), bonjour.data(), bonjour.size());
82
83 store_.LoadTestingOnly(temp_dir_.GetPath());
84
85 string hello_value;
86 string bonjour_value;
87
88 ASSERT_TRUE(store_.GetString("HELLO", &hello_value));
89 ASSERT_TRUE(store_.GetString("BONJOUR", &bonjour_value));
90
91 ASSERT_EQ("hello", hello_value);
92 ASSERT_EQ("bonjour", bonjour_value);
93 }
94
95 } // namespace brillo
96