1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/common/hwid_override.h"
18
19 #include <string>
20
21 #include <base/files/file_path.h>
22 #include <base/files/file_util.h>
23 #include <base/files/scoped_temp_dir.h>
24 #include <gtest/gtest.h>
25
26 namespace chromeos_update_engine {
27
28 class HwidOverrideTest : public ::testing::Test {
29 public:
HwidOverrideTest()30 HwidOverrideTest() {}
31 ~HwidOverrideTest() override = default;
32
SetUp()33 void SetUp() override {
34 ASSERT_TRUE(tempdir_.CreateUniqueTempDir());
35 ASSERT_TRUE(base::CreateDirectory(tempdir_.GetPath().Append("etc")));
36 }
37
38 protected:
39 base::ScopedTempDir tempdir_;
40
41 private:
42 DISALLOW_COPY_AND_ASSIGN(HwidOverrideTest);
43 };
44
TEST_F(HwidOverrideTest,ReadGood)45 TEST_F(HwidOverrideTest, ReadGood) {
46 std::string expected_hwid("expected");
47 std::string keyval(HwidOverride::kHwidOverrideKey);
48 keyval += ("=" + expected_hwid);
49 ASSERT_EQ(base::WriteFile(tempdir_.GetPath().Append("etc/lsb-release"),
50 keyval.c_str(),
51 keyval.length()),
52 static_cast<int>(keyval.length()));
53 EXPECT_EQ(expected_hwid, HwidOverride::Read(tempdir_.GetPath()));
54 }
55
TEST_F(HwidOverrideTest,ReadNothing)56 TEST_F(HwidOverrideTest, ReadNothing) {
57 std::string keyval("SOMETHING_ELSE=UNINTERESTING");
58 ASSERT_EQ(base::WriteFile(tempdir_.GetPath().Append("etc/lsb-release"),
59 keyval.c_str(),
60 keyval.length()),
61 static_cast<int>(keyval.length()));
62 EXPECT_EQ(std::string(), HwidOverride::Read(tempdir_.GetPath()));
63 }
64
TEST_F(HwidOverrideTest,ReadFailure)65 TEST_F(HwidOverrideTest, ReadFailure) {
66 EXPECT_EQ(std::string(), HwidOverride::Read(tempdir_.GetPath()));
67 }
68
69 } // namespace chromeos_update_engine
70