1 /*
2 * Copyright (C) 2024 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 #pragma once
18
19 #include <android-base/logging.h>
20
wpa_to_android_level(int level)21 static ::android::base::LogSeverity wpa_to_android_level(int level)
22 {
23 if (level == MSG_ERROR)
24 return ::android::base::ERROR;
25 if (level == MSG_WARNING)
26 return ::android::base::WARNING;
27 if (level == MSG_INFO)
28 return ::android::base::INFO;
29 return ::android::base::DEBUG;
30 }
31
32 // don't use hostapd's wpa_printf for unit testing. It won't compile otherwise
wpa_printf(int level,const char * fmt,...)33 void wpa_printf(int level, const char *fmt, ...) {
34 va_list ap;
35 va_start(ap, fmt);
36 LOG(wpa_to_android_level(level)) << ::android::base::StringPrintf(fmt, ap);
37 va_end(ap);
38 }
39
40 static int hostapd_unittest_stat_ret = 0;
stat(const char * pathname,struct stat * stabuf)41 int stat(const char* pathname, struct stat* stabuf) {
42 if (hostapd_unittest_stat_ret != 0) {
43 errno = EINVAL;
44 }
45 return hostapd_unittest_stat_ret;
46 }
47
48 static int hostapd_unittest_accessRet = 0;
access(const char * pathname,int mode)49 int access(const char* pathname, int mode) {
50 if (hostapd_unittest_accessRet != 0) {
51 errno = EINVAL;
52 }
53 return hostapd_unittest_accessRet;
54 }
55
56
57 // You can inspect the string here to see what we tried to write to a file
58 static std::string hostapd_unittest_config_output = "";
59 static bool hostapd_unittest_WriteStringToFileRet = true;
WriteStringToFile(const std::string & content,const std::string & path,mode_t mode,uid_t owner,gid_t group)60 bool WriteStringToFile(const std::string& content, const std::string& path, mode_t mode,
61 uid_t owner, gid_t group) {
62 if (!hostapd_unittest_WriteStringToFileRet) {
63 errno = EINVAL;
64 } else {
65 hostapd_unittest_config_output = content;
66 }
67 return hostapd_unittest_WriteStringToFileRet;
68 }
69
70 // You can simulate a file having content with this string
71 static std::string hostapd_unittest_overlay_content = "";
72 static bool hostapd_unittest_ReadFileToStringRet = true;
ReadFileToString(const std::string & path,std::string * content)73 bool ReadFileToString(const std::string& path, std::string* content) {
74 *content = hostapd_unittest_overlay_content;
75 LOG(INFO) << "*content = " << *content;
76 return hostapd_unittest_ReadFileToStringRet;
77 }
78
79 /**
80 * We can simulate I/O operations failing by re-defining the calls.
81 *
82 * By default, all files are empty, and all calls succeed.
83 */
resetOverrides()84 void resetOverrides() {
85 hostapd_unittest_stat_ret = 0;
86 hostapd_unittest_WriteStringToFileRet = true;
87 hostapd_unittest_config_output = "";
88 hostapd_unittest_accessRet = 0;
89 hostapd_unittest_overlay_content = "";
90 hostapd_unittest_ReadFileToStringRet = true;
91 }
92