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
26namespace chromeos_update_engine {
27
28class HwidOverrideTest : public ::testing::Test {
29 public:
30  HwidOverrideTest() {}
31  ~HwidOverrideTest() override = default;
32
33  void SetUp() override {
34    ASSERT_TRUE(tempdir_.CreateUniqueTempDir());
35    ASSERT_TRUE(base::CreateDirectory(tempdir_.path().Append("etc")));
36  }
37
38 protected:
39  base::ScopedTempDir tempdir_;
40
41 private:
42  DISALLOW_COPY_AND_ASSIGN(HwidOverrideTest);
43};
44
45TEST_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_.path().Append("etc/lsb-release"),
50                            keyval.c_str(), keyval.length()),
51            static_cast<int>(keyval.length()));
52  EXPECT_EQ(expected_hwid, HwidOverride::Read(tempdir_.path()));
53}
54
55TEST_F(HwidOverrideTest, ReadNothing) {
56  std::string keyval("SOMETHING_ELSE=UNINTERESTING");
57  ASSERT_EQ(base::WriteFile(tempdir_.path().Append("etc/lsb-release"),
58                            keyval.c_str(), keyval.length()),
59            static_cast<int>(keyval.length()));
60  EXPECT_EQ(std::string(), HwidOverride::Read(tempdir_.path()));
61}
62
63TEST_F(HwidOverrideTest, ReadFailure) {
64  EXPECT_EQ(std::string(), HwidOverride::Read(tempdir_.path()));
65}
66
67}  // namespace chromeos_update_engine
68