Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Eric Biggers | fec0c0e | 2021-02-16 15:59:17 -0800 | [diff] [blame] | 17 | #include <android-base/file.h> |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 18 | #include <gtest/gtest.h> |
| 19 | |
| 20 | #include "../Utils.h" |
| 21 | |
| 22 | namespace android { |
| 23 | namespace vold { |
| 24 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 25 | class UtilsTest : public testing::Test {}; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 26 | |
| 27 | TEST_F(UtilsTest, FindValueTest) { |
| 28 | std::string tmp; |
| 29 | |
| 30 | ASSERT_FALSE(FindValue("", "KEY", &tmp)); |
Paul Crowley | 95abfa0 | 2019-02-05 15:33:34 -0800 | [diff] [blame] | 31 | ASSERT_FALSE(FindValue("NOTMATCH=\"VALUE\"", "KEY", &tmp)); |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 32 | ASSERT_FALSE(FindValue("BADKEY=\"VALUE\"", "KEY", &tmp)); |
| 33 | |
| 34 | ASSERT_TRUE(FindValue("KEY=\"VALUE\"", "KEY", &tmp)); |
| 35 | ASSERT_EQ("VALUE", tmp); |
| 36 | |
| 37 | ASSERT_TRUE(FindValue("FOO=\"BAR\" KEY=\"VALUE VALUE\" BAR=\"BAZ\"", "KEY", &tmp)); |
| 38 | ASSERT_EQ("VALUE VALUE", tmp); |
| 39 | |
| 40 | ASSERT_TRUE(FindValue("BADKEY=\"VALUE\" KEY=\"BAZ\"", "KEY", &tmp)); |
| 41 | ASSERT_EQ("BAZ", tmp); |
Paul Crowley | 95abfa0 | 2019-02-05 15:33:34 -0800 | [diff] [blame] | 42 | |
| 43 | ASSERT_TRUE(FindValue("BADKEY=\"VALUE\" NOTKEY=\"OTHER\" KEY=\"QUUX\"", "KEY", &tmp)); |
| 44 | ASSERT_EQ("QUUX", tmp); |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 45 | } |
| 46 | |
Eric Biggers | fec0c0e | 2021-02-16 15:59:17 -0800 | [diff] [blame] | 47 | TEST_F(UtilsTest, MkdirsSyncTest) { |
| 48 | TemporaryDir temp_dir; |
| 49 | std::string temp_dir_path; |
| 50 | |
| 51 | ASSERT_TRUE(android::base::Realpath(temp_dir.path, &temp_dir_path)); |
| 52 | |
| 53 | ASSERT_FALSE(pathExists(temp_dir_path + "/a")); |
| 54 | ASSERT_TRUE(MkdirsSync(temp_dir_path + "/a/b/c", 0700)); |
| 55 | ASSERT_TRUE(pathExists(temp_dir_path + "/a")); |
| 56 | ASSERT_TRUE(pathExists(temp_dir_path + "/a/b")); |
| 57 | // The final component of the path should not be created; only the previous |
| 58 | // components should be. |
| 59 | ASSERT_FALSE(pathExists(temp_dir_path + "/a/b/c")); |
| 60 | |
| 61 | // Currently, MkdirsSync() only supports absolute paths. |
| 62 | ASSERT_FALSE(MkdirsSync("foo", 0700)); |
| 63 | } |
| 64 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 65 | } // namespace vold |
| 66 | } // namespace android |