1f16e0440Sopenharmony_ci/* 2f16e0440Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3f16e0440Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f16e0440Sopenharmony_ci * you may not use this file except in compliance with the License. 5f16e0440Sopenharmony_ci * You may obtain a copy of the License at 6f16e0440Sopenharmony_ci * 7f16e0440Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f16e0440Sopenharmony_ci * 9f16e0440Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f16e0440Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f16e0440Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f16e0440Sopenharmony_ci * See the License for the specific language governing permissions and 13f16e0440Sopenharmony_ci * limitations under the License. 14f16e0440Sopenharmony_ci */ 15f16e0440Sopenharmony_ci 16f16e0440Sopenharmony_ci#include "test_utils.h" 17f16e0440Sopenharmony_ci 18f16e0440Sopenharmony_ci#include <dirent.h> 19f16e0440Sopenharmony_ci#include <fcntl.h> 20f16e0440Sopenharmony_ci#include <fstream> 21f16e0440Sopenharmony_ci#include <sys/stat.h> 22f16e0440Sopenharmony_ci#include <unistd.h> 23f16e0440Sopenharmony_ci#include <vector> 24f16e0440Sopenharmony_ci 25f16e0440Sopenharmony_ci#include "battery_log.h" 26f16e0440Sopenharmony_ci 27f16e0440Sopenharmony_ciusing namespace std; 28f16e0440Sopenharmony_ci 29f16e0440Sopenharmony_cinamespace OHOS { 30f16e0440Sopenharmony_cinamespace PowerMgr { 31f16e0440Sopenharmony_cinamespace { 32f16e0440Sopenharmony_ciconstexpr const char* MOCK_PATH = "/data/service/el0/battery"; 33f16e0440Sopenharmony_ciconstexpr const char* POWER_SUPPLY_PATH = "/sys/class/power_supply"; 34f16e0440Sopenharmony_civector<string> MOCK_DIR_NAME = { 35f16e0440Sopenharmony_ci "battery", 36f16e0440Sopenharmony_ci "USB", 37f16e0440Sopenharmony_ci "Wireless", 38f16e0440Sopenharmony_ci "Mains", 39f16e0440Sopenharmony_ci "ohos_charger", 40f16e0440Sopenharmony_ci "ohos-fgu" 41f16e0440Sopenharmony_ci}; 42f16e0440Sopenharmony_ci 43f16e0440Sopenharmony_civector<vector<string>> MOCK_FILE_NAME = { 44f16e0440Sopenharmony_ci { "capacity", "voltage_now", "temp", "health", "status", "present", "charge_counter", "technology" }, 45f16e0440Sopenharmony_ci { "type", "online", "current_max", "voltage_max" }, 46f16e0440Sopenharmony_ci { "type", "online" }, 47f16e0440Sopenharmony_ci { "type", "online" }, 48f16e0440Sopenharmony_ci { "type", "online" }, 49f16e0440Sopenharmony_ci { "type", "online" } 50f16e0440Sopenharmony_ci}; 51f16e0440Sopenharmony_ci} 52f16e0440Sopenharmony_ci 53f16e0440Sopenharmony_civoid TestUtils::WriteMock(const std::string& path, const std::string content) 54f16e0440Sopenharmony_ci{ 55f16e0440Sopenharmony_ci std::ofstream stream(path.c_str()); 56f16e0440Sopenharmony_ci if (!stream.is_open()) { 57f16e0440Sopenharmony_ci BATTERY_HILOGI(LABEL_TEST, "Cannot create file"); 58f16e0440Sopenharmony_ci return; 59f16e0440Sopenharmony_ci } 60f16e0440Sopenharmony_ci stream << content.c_str() << std::endl; 61f16e0440Sopenharmony_ci stream.close(); 62f16e0440Sopenharmony_ci} 63f16e0440Sopenharmony_ci 64f16e0440Sopenharmony_civoid TestUtils::InitTest() 65f16e0440Sopenharmony_ci{ 66f16e0440Sopenharmony_ci mkdir(MOCK_PATH, S_IRWXU); 67f16e0440Sopenharmony_ci for (size_t i = 0; i < MOCK_DIR_NAME.size(); ++i) { 68f16e0440Sopenharmony_ci mkdir((std::string(MOCK_PATH) + "/" + MOCK_DIR_NAME[i]).c_str(), S_IRWXU); 69f16e0440Sopenharmony_ci } 70f16e0440Sopenharmony_ci} 71f16e0440Sopenharmony_ci 72f16e0440Sopenharmony_civoid TestUtils::ResetOnline() 73f16e0440Sopenharmony_ci{ 74f16e0440Sopenharmony_ci for (size_t dInd = 0; dInd < MOCK_DIR_NAME.size(); ++dInd) { 75f16e0440Sopenharmony_ci for (size_t fInd = 0; fInd < MOCK_FILE_NAME[dInd].size(); ++fInd) { 76f16e0440Sopenharmony_ci if (MOCK_FILE_NAME[dInd][fInd] == "online") { 77f16e0440Sopenharmony_ci std::string file = std::string(MOCK_PATH) + "/" + MOCK_DIR_NAME[dInd] + "/" + "online"; 78f16e0440Sopenharmony_ci WriteMock(file, "0"); 79f16e0440Sopenharmony_ci } 80f16e0440Sopenharmony_ci } 81f16e0440Sopenharmony_ci } 82f16e0440Sopenharmony_ci} 83f16e0440Sopenharmony_ci 84f16e0440Sopenharmony_cibool TestUtils::IsMock() 85f16e0440Sopenharmony_ci{ 86f16e0440Sopenharmony_ci DIR* dir = opendir(POWER_SUPPLY_PATH); 87f16e0440Sopenharmony_ci if (dir == nullptr) { 88f16e0440Sopenharmony_ci return true; 89f16e0440Sopenharmony_ci } 90f16e0440Sopenharmony_ci struct dirent* ptr = nullptr; 91f16e0440Sopenharmony_ci while ((ptr = readdir(dir)) != nullptr) { 92f16e0440Sopenharmony_ci if (strcmp(".", ptr->d_name) != 0 && strcmp("..", ptr->d_name) != 0) { 93f16e0440Sopenharmony_ci closedir(dir); 94f16e0440Sopenharmony_ci return false; 95f16e0440Sopenharmony_ci } 96f16e0440Sopenharmony_ci } 97f16e0440Sopenharmony_ci closedir(dir); 98f16e0440Sopenharmony_ci return true; 99f16e0440Sopenharmony_ci} 100f16e0440Sopenharmony_ci} // namespace PowerMgr 101f16e0440Sopenharmony_ci} // namespace OHOS 102