blob: 35e64382805bfaf9426e53d98f2a3933c3d3e076 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
Chris Masonef8d037f2014-02-19 01:53:00 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/hwid_override.h"
Chris Masonef8d037f2014-02-19 01:53:00 +000018
19#include <string>
20
Alex Vakulenko75039d72014-03-25 12:36:28 -070021#include <base/files/file_path.h>
Ben Chan06c76a42014-09-05 08:21:06 -070022#include <base/files/file_util.h>
Chris Masonef8d037f2014-02-19 01:53:00 +000023#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() {}
Alex Deymo610277e2014-11-11 21:18:11 -080031 ~HwidOverrideTest() override = default;
Chris Masonef8d037f2014-02-19 01:53:00 +000032
Alex Deymo610277e2014-11-11 21:18:11 -080033 void SetUp() override {
Chris Masonef8d037f2014-02-19 01:53:00 +000034 ASSERT_TRUE(tempdir_.CreateUniqueTempDir());
Jay Civellia78ddf82017-03-30 11:02:03 -070035 ASSERT_TRUE(base::CreateDirectory(tempdir_.GetPath().Append("etc")));
Chris Masonef8d037f2014-02-19 01:53:00 +000036 }
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);
Jay Civellia78ddf82017-03-30 11:02:03 -070049 ASSERT_EQ(base::WriteFile(tempdir_.GetPath().Append("etc/lsb-release"),
50 keyval.c_str(),
51 keyval.length()),
Alex Deymo80f70ff2016-02-10 16:08:11 -080052 static_cast<int>(keyval.length()));
Jay Civellia78ddf82017-03-30 11:02:03 -070053 EXPECT_EQ(expected_hwid, HwidOverride::Read(tempdir_.GetPath()));
Chris Masonef8d037f2014-02-19 01:53:00 +000054}
55
56TEST_F(HwidOverrideTest, ReadNothing) {
57 std::string keyval("SOMETHING_ELSE=UNINTERESTING");
Jay Civellia78ddf82017-03-30 11:02:03 -070058 ASSERT_EQ(base::WriteFile(tempdir_.GetPath().Append("etc/lsb-release"),
59 keyval.c_str(),
60 keyval.length()),
Alex Deymo80f70ff2016-02-10 16:08:11 -080061 static_cast<int>(keyval.length()));
Jay Civellia78ddf82017-03-30 11:02:03 -070062 EXPECT_EQ(std::string(), HwidOverride::Read(tempdir_.GetPath()));
Chris Masonef8d037f2014-02-19 01:53:00 +000063}
64
65TEST_F(HwidOverrideTest, ReadFailure) {
Jay Civellia78ddf82017-03-30 11:02:03 -070066 EXPECT_EQ(std::string(), HwidOverride::Read(tempdir_.GetPath()));
Chris Masonef8d037f2014-02-19 01:53:00 +000067}
68
69} // namespace chromeos_update_engine