Mark Salyzyn | 5649b31 | 2017-03-22 08:46:55 -0700 | [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 | |
| 17 | #include <stdio.h> |
| 18 | #include <sys/cdefs.h> |
| 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | #include <android-base/file.h> |
| 23 | #include <android-base/macros.h> |
| 24 | #include <android-base/stringprintf.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <private/android_filesystem_config.h> |
| 27 | #include <private/fs_config.h> |
| 28 | |
| 29 | #include "android_filesystem_config_test_data.h" |
| 30 | |
| 31 | // must run test in the test directory |
| 32 | const static char fs_config_generate_command[] = "./fs_config_generate_test"; |
| 33 | |
| 34 | static std::string popenToString(std::string command) { |
| 35 | std::string ret; |
| 36 | |
| 37 | FILE* fp = popen(command.c_str(), "r"); |
| 38 | if (fp) { |
| 39 | if (!android::base::ReadFdToString(fileno(fp), &ret)) ret = ""; |
| 40 | pclose(fp); |
| 41 | } |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | static void confirm(std::string&& data, const fs_path_config* config, |
| 46 | ssize_t num_config) { |
| 47 | const struct fs_path_config_from_file* pc = |
| 48 | reinterpret_cast<const fs_path_config_from_file*>(data.c_str()); |
| 49 | size_t len = data.size(); |
| 50 | |
| 51 | ASSERT_TRUE(config != NULL); |
| 52 | ASSERT_LT(0, num_config); |
| 53 | |
| 54 | while (len > 0) { |
| 55 | uint16_t host_len = pc->len; |
| 56 | if (host_len > len) break; |
| 57 | |
| 58 | EXPECT_EQ(config->mode, pc->mode); |
| 59 | EXPECT_EQ(config->uid, pc->uid); |
| 60 | EXPECT_EQ(config->gid, pc->gid); |
| 61 | EXPECT_EQ(config->capabilities, pc->capabilities); |
| 62 | EXPECT_STREQ(config->prefix, pc->prefix); |
| 63 | |
| 64 | EXPECT_LT(0, num_config); |
| 65 | --num_config; |
| 66 | if (num_config >= 0) ++config; |
| 67 | pc = reinterpret_cast<const fs_path_config_from_file*>( |
| 68 | reinterpret_cast<const char*>(pc) + host_len); |
| 69 | len -= host_len; |
| 70 | } |
| 71 | EXPECT_EQ(0, num_config); |
| 72 | } |
| 73 | |
| 74 | /* See local android_filesystem_config.h for test data */ |
| 75 | |
| 76 | TEST(fs_conf_test, dirs) { |
| 77 | confirm(popenToString( |
| 78 | android::base::StringPrintf("%s -D", fs_config_generate_command)), |
| 79 | android_device_dirs, arraysize(android_device_dirs)); |
| 80 | } |
| 81 | |
| 82 | TEST(fs_conf_test, files) { |
| 83 | confirm(popenToString( |
| 84 | android::base::StringPrintf("%s -F", fs_config_generate_command)), |
| 85 | android_device_files, arraysize(android_device_files)); |
| 86 | } |