Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/hwid_override.h" |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 18 | |
| 19 | #include <string> |
| 20 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 21 | #include <base/files/file_path.h> |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 22 | #include <base/files/file_util.h> |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 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: |
| 30 | HwidOverrideTest() {} |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 31 | ~HwidOverrideTest() override = default; |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 32 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 33 | void SetUp() override { |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 34 | ASSERT_TRUE(tempdir_.CreateUniqueTempDir()); |
Jay Civelli | a78ddf8 | 2017-03-30 11:02:03 -0700 | [diff] [blame^] | 35 | ASSERT_TRUE(base::CreateDirectory(tempdir_.GetPath().Append("etc"))); |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | protected: |
| 39 | base::ScopedTempDir tempdir_; |
| 40 | |
| 41 | private: |
| 42 | DISALLOW_COPY_AND_ASSIGN(HwidOverrideTest); |
| 43 | }; |
| 44 | |
| 45 | TEST_F(HwidOverrideTest, ReadGood) { |
| 46 | std::string expected_hwid("expected"); |
| 47 | std::string keyval(HwidOverride::kHwidOverrideKey); |
| 48 | keyval += ("=" + expected_hwid); |
Jay Civelli | a78ddf8 | 2017-03-30 11:02:03 -0700 | [diff] [blame^] | 49 | ASSERT_EQ(base::WriteFile(tempdir_.GetPath().Append("etc/lsb-release"), |
| 50 | keyval.c_str(), |
| 51 | keyval.length()), |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 52 | static_cast<int>(keyval.length())); |
Jay Civelli | a78ddf8 | 2017-03-30 11:02:03 -0700 | [diff] [blame^] | 53 | EXPECT_EQ(expected_hwid, HwidOverride::Read(tempdir_.GetPath())); |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | TEST_F(HwidOverrideTest, ReadNothing) { |
| 57 | std::string keyval("SOMETHING_ELSE=UNINTERESTING"); |
Jay Civelli | a78ddf8 | 2017-03-30 11:02:03 -0700 | [diff] [blame^] | 58 | ASSERT_EQ(base::WriteFile(tempdir_.GetPath().Append("etc/lsb-release"), |
| 59 | keyval.c_str(), |
| 60 | keyval.length()), |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 61 | static_cast<int>(keyval.length())); |
Jay Civelli | a78ddf8 | 2017-03-30 11:02:03 -0700 | [diff] [blame^] | 62 | EXPECT_EQ(std::string(), HwidOverride::Read(tempdir_.GetPath())); |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | TEST_F(HwidOverrideTest, ReadFailure) { |
Jay Civelli | a78ddf8 | 2017-03-30 11:02:03 -0700 | [diff] [blame^] | 66 | EXPECT_EQ(std::string(), HwidOverride::Read(tempdir_.GetPath())); |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | } // namespace chromeos_update_engine |