Matt Ziegelbaum | 91ba9be | 2020-06-10 16:56:40 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2020 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/requisition_util.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 | #include "update_engine/common/test_utils.h" |
| 27 | |
| 28 | using chromeos_update_engine::test_utils::WriteFileString; |
| 29 | using std::string; |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | const char kRemoraJSON[] = |
| 34 | "{\n" |
| 35 | " \"the_list\": [ \"val1\", \"val2\" ],\n" |
| 36 | " \"enrollment\": {\n" |
| 37 | " \"autostart\": true,\n" |
| 38 | " \"can_exit\": false,\n" |
| 39 | " \"device_requisition\": \"remora\"\n" |
| 40 | " },\n" |
| 41 | " \"some_String\": \"1337\",\n" |
| 42 | " \"some_int\": 42\n" |
| 43 | "}\n"; |
| 44 | |
| 45 | const char kNoEnrollmentJSON[] = |
| 46 | "{\n" |
| 47 | " \"the_list\": [ \"val1\", \"val2\" ],\n" |
| 48 | " \"enrollment\": {\n" |
| 49 | " \"autostart\": true,\n" |
| 50 | " \"can_exit\": false,\n" |
| 51 | " \"device_requisition\": \"\"\n" |
| 52 | " },\n" |
| 53 | " \"some_String\": \"1337\",\n" |
| 54 | " \"some_int\": 42\n" |
| 55 | "}\n"; |
| 56 | } // namespace |
| 57 | |
| 58 | namespace chromeos_update_engine { |
| 59 | |
| 60 | class RequisitionUtilTest : public ::testing::Test { |
| 61 | protected: |
| 62 | void SetUp() override { ASSERT_TRUE(root_dir_.CreateUniqueTempDir()); } |
| 63 | |
| 64 | void WriteJsonToFile(const string& json) { |
| 65 | path_ = |
| 66 | base::FilePath(root_dir_.GetPath().value() + "/chronos/Local State"); |
| 67 | ASSERT_TRUE(base::CreateDirectory(path_.DirName())); |
| 68 | ASSERT_TRUE(WriteFileString(path_.value(), json)); |
| 69 | } |
| 70 | |
| 71 | base::ScopedTempDir root_dir_; |
| 72 | base::FilePath path_; |
| 73 | }; |
| 74 | |
| 75 | TEST_F(RequisitionUtilTest, BadJsonReturnsEmpty) { |
| 76 | WriteJsonToFile("this isn't JSON"); |
| 77 | EXPECT_EQ("", ReadDeviceRequisition(path_)); |
| 78 | } |
| 79 | |
| 80 | TEST_F(RequisitionUtilTest, NoFileReturnsEmpty) { |
| 81 | EXPECT_EQ("", ReadDeviceRequisition(path_)); |
| 82 | } |
| 83 | |
| 84 | TEST_F(RequisitionUtilTest, EnrollmentRequisition) { |
| 85 | WriteJsonToFile(kRemoraJSON); |
| 86 | EXPECT_EQ("remora", ReadDeviceRequisition(path_)); |
| 87 | } |
| 88 | |
| 89 | TEST_F(RequisitionUtilTest, BlankEnrollment) { |
| 90 | WriteJsonToFile(kNoEnrollmentJSON); |
| 91 | EXPECT_EQ("", ReadDeviceRequisition(path_)); |
| 92 | } |
| 93 | |
| 94 | } // namespace chromeos_update_engine |