tests: unittest: Add hal-api-power mock class 73/276573/4
authorChanwoo Choi <cw00.choi@samsung.com>
Mon, 20 Jun 2022 08:10:30 +0000 (17:10 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Thu, 23 Jun 2022 06:08:12 +0000 (15:08 +0900)
PASS comunicates with hal-api-power to read/write the h/w resources
such as CPU/Memory Bus/GPU/Memroy and so on. For the testing on build
time, it requires the mocking of hal-api-power. In result, add hal-api-power
mock class.

Change-Id: I75e7537d3a94059d586943810c502882d24a1517
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
tests/unittest/hal-api-power-mock.cpp [new file with mode: 0644]
tests/unittest/hal-api-power-mock.hpp [new file with mode: 0644]

diff --git a/tests/unittest/hal-api-power-mock.cpp b/tests/unittest/hal-api-power-mock.cpp
new file mode 100644 (file)
index 0000000..b9a550f
--- /dev/null
@@ -0,0 +1,352 @@
+/*
+ * Copyright (C) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <iostream>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include <gio/gio.h>
+#include <gtest/gtest.h>
+#include <gmock/gmock.h>
+
+extern "C" {
+#include "pass.h"
+#include "pass-hal.h"
+#include "pass-parser.h"
+}
+
+#include "hal-api-power-mock.hpp"
+
+using namespace std;
+using ::testing::Return;
+using ::testing::_;
+
+HalApiPowerMock *gHalApiPowerMock;
+
+/**
+ * Get and put power hal backend
+ */
+int hal_power_get_backend(unsigned int res_type)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_get_backend(res_type);
+}
+int hal_power_put_backend(void)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_put_backend();
+}
+
+/**
+ * DVFS (Dynamic Voltage Frequency Scaling) Operation for CPU/BUS/GPU H/W
+ */
+/* Get and set the current governor. */
+int hal_power_dvfs_get_curr_governor(unsigned int res_type, char *res_name, char *governor)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_dvfs_get_curr_governor(
+                                       res_type, res_name, governor);
+}
+int hal_power_dvfs_set_curr_governor(unsigned int res_type, char *res_name, char *governor)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_dvfs_set_curr_governor(
+                                       res_type, res_name, governor);
+}
+int hal_power_dvfs_get_avail_governor(unsigned int res_type, char *res_name, char **avail_governor)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_dvfs_get_avail_governor(
+                                       res_type, res_name, avail_governor);
+}
+
+/* Get the current frequency. */
+int hal_power_dvfs_get_curr_freq(unsigned int res_type, char *res_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_dvfs_get_curr_freq(
+                                       res_type, res_name);
+}
+
+/* Get and set the minimum frequency. */
+int hal_power_dvfs_get_min_freq(unsigned int res_type, char *res_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_dvfs_get_min_freq(
+                                       res_type, res_name);
+}
+int hal_power_dvfs_set_min_freq(unsigned int res_type, char *res_name, int freq)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_dvfs_set_min_freq(
+                                       res_type, res_name, freq);
+}
+
+/* Get and set the maximum frequency. */
+int hal_power_dvfs_get_max_freq(unsigned int res_type, char *res_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_dvfs_get_max_freq(
+                                       res_type, res_name);
+}
+int hal_power_dvfs_set_max_freq(unsigned int res_type, char *res_name, int freq)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_dvfs_set_max_freq(
+                                       res_type, res_name, freq);
+}
+
+/* Get the minimum/maximum frequency which can be set to resource. */
+int hal_power_dvfs_get_available_min_freq(unsigned int res_type, char *res_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+       return gHalApiPowerMock->hal_power_dvfs_get_available_min_freq(
+                                       res_type, res_name);
+}
+int hal_power_dvfs_get_available_max_freq(unsigned int res_type, char *res_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_dvfs_get_available_max_freq(
+                                       res_type, res_name);
+}
+
+/* Get and set the up_threshold to support boosting. */
+int hal_power_dvfs_get_up_threshold(unsigned int res_type, char *res_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_dvfs_get_up_threshold(
+                                       res_type, res_name);
+}
+int hal_power_dvfs_set_up_threshold(unsigned int res_type, char *res_name, int up_threshold)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_dvfs_set_up_threshold(
+                                       res_type, res_name, up_threshold);
+}
+
+/* Get the load_table of each resource to estimate the system load. */
+int hal_power_dvfs_get_load_table(unsigned int res_type, char *name, void *pass_cpu_load_table)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_dvfs_get_load_table(
+                                       res_type, name, pass_cpu_load_table);
+}
+
+/**
+ * CPU Hotplug Operation for CPU H/W
+ */
+/* Get and set the online status of resource. */
+int hal_power_hotplug_get_online_state(unsigned int res_type, char *res_name, int cpu)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_hotplug_get_online_state(
+                                       res_type, res_name, cpu);
+}
+int hal_power_hotplug_set_online_state(unsigned int res_type, char *res_name, int cpu, int on)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_hotplug_set_online_state(
+                                       res_type, res_name, cpu, on);
+}
+
+/* Get and set the minimum number of online CPUs */
+int hal_power_hotplug_get_online_min_num (unsigned int res_type, char *res_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_hotplug_get_online_min_num (
+                                       res_type, res_name);
+}
+int hal_power_hotplug_set_online_min_num (unsigned int res_type, char *res_name, int min_num)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_hotplug_set_online_min_num (
+                                       res_type, res_name, min_num);
+}
+
+/* Get and set the maximum number of online CPUs */
+int hal_power_hotplug_get_online_max_num (unsigned int res_type, char *res_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_hotplug_get_online_max_num (
+                                       res_type, res_name);
+}
+int hal_power_hotplug_set_online_max_num (unsigned int res_type, char *res_name, int max_num)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_hotplug_set_online_max_num (
+                                       res_type, res_name, max_num);
+}
+
+/**
+ * Thermal Operation for CPU/BUS/GPU H/W
+ */
+/* Get the current temperature of resource. */
+int hal_power_thermal_get_temp(unsigned int res_type, char *res_thermal_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_thermal_get_temp(
+                                       res_type, res_thermal_name);
+}
+
+/* Get the policy of thermal management unit. */
+int hal_power_thermal_get_policy(unsigned int res_type, char *res_thermal_name, char *policy)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_thermal_get_policy(
+                                       res_type, res_thermal_name, policy);
+}
+
+/* Get and set the state of thermal cooling-device */
+int hal_power_thermal_get_cooling_device_state(unsigned int device_type, char *cooling_device_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_thermal_get_cooling_device_state(
+                                       device_type, cooling_device_name);
+}
+int hal_power_thermal_set_cooling_device_state(unsigned int device_type, char *cooling_device_name, int state)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_thermal_set_cooling_device_state(
+                                       device_type, cooling_device_name, state);
+}
+
+/* Get maximum state of thermal cooling-device */
+int hal_power_thermal_get_cooling_device_max_state(unsigned int device_type, char *cooling_device_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_thermal_get_cooling_device_max_state(
+                                       device_type, cooling_device_name);
+}
+
+/* Get and set the battery charging state */
+int hal_power_battery_get_charging_status(unsigned int device_type, char *res_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_battery_get_charging_status(
+                                       device_type, res_name);
+}
+int hal_power_battery_set_charging_status(unsigned int device_type, char *res_name, int state)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_battery_set_charging_status(
+                                       device_type, res_name, state);
+}
+
+/* Get and set the battery charging current (unit: uA) */
+int hal_power_battery_get_charging_current(unsigned int device_type, char *res_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_battery_get_charging_current(
+                                       device_type, res_name);
+}
+int hal_power_battery_set_charging_current(unsigned int device_type, char *res_name, int charing_current_uA)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_battery_set_charging_current(
+                                       device_type, res_name, charing_current_uA);
+}
+
+/**
+ * Memory Operation for Memory H/W
+ */
+/* Get and set the /sys/kernel/debug/fault_around_bytes */
+int hal_power_memory_get_fault_around_bytes(unsigned int res_type, char *res_name)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_memory_get_fault_around_bytes(
+                                       res_type, res_name);
+}
+int hal_power_memory_set_fault_around_bytes(unsigned int res_type, char *res_name, int fault_around_bytes)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_memory_set_fault_around_bytes(
+                                       res_type, res_name, fault_around_bytes);
+}
+
+/**
+ * Miscellaneous Operation for CPU/BUS/GPU H/W
+ */
+int hal_power_misc_set_pmqos_data(unsigned int res_type, char *res_name, void *data)
+{
+       if (!gHalApiPowerMock)
+               return -ENOTSUP;
+
+       return gHalApiPowerMock->hal_power_misc_set_pmqos_data(
+                                       res_type, res_name, data);
+}
diff --git a/tests/unittest/hal-api-power-mock.hpp b/tests/unittest/hal-api-power-mock.hpp
new file mode 100644 (file)
index 0000000..e03c2ba
--- /dev/null
@@ -0,0 +1,183 @@
+/*
+ * Copyright (C) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <iostream>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include <gio/gio.h>
+#include <gtest/gtest.h>
+#include <gmock/gmock.h>
+
+extern "C" {
+#include "pass.h"
+#include "pass-hal.h"
+#include "pass-parser.h"
+}
+
+using namespace std;
+using ::testing::Return;
+using ::testing::_;
+
+class HalApiPowerMockInterface {
+public:
+       virtual ~HalApiPowerMockInterface() {};
+
+       /**
+        * Get and put power hal backend
+        */
+       virtual int hal_power_get_backend(unsigned int res_type) = 0;
+       virtual int hal_power_put_backend(void) = 0;
+
+       /**
+        * DVFS (Dynamic Voltage Frequency Scaling) Operation for CPU/BUS/GPU H/W
+        */
+       /* Get and set the current governor. */
+       virtual int hal_power_dvfs_get_curr_governor(unsigned int res_type, char *res_name, char *governor) = 0;
+       virtual int hal_power_dvfs_set_curr_governor(unsigned int res_type, char *res_name, char *governor) = 0;
+       virtual int hal_power_dvfs_get_avail_governor(unsigned int res_type, char *res_name, char **avail_governor) = 0;
+
+       /* Get the current frequency. */
+       virtual int hal_power_dvfs_get_curr_freq(unsigned int res_type, char *res_name) = 0;
+
+       /* Get and set the minimum frequency. */
+       virtual int hal_power_dvfs_get_min_freq(unsigned int res_type, char *res_name) = 0;
+       virtual int hal_power_dvfs_set_min_freq(unsigned int res_type, char *res_name, int freq) = 0;
+
+       /* Get and set the maximum frequency. */
+       virtual int hal_power_dvfs_get_max_freq(unsigned int res_type, char *res_name) = 0;
+       virtual int hal_power_dvfs_set_max_freq(unsigned int res_type, char *res_name, int freq) = 0;
+
+       /* Get the minimum/maximum frequency which can be set to resource. */
+       virtual int hal_power_dvfs_get_available_min_freq(unsigned int res_type, char *res_name) = 0;
+       virtual int hal_power_dvfs_get_available_max_freq(unsigned int res_type, char *res_name) = 0;
+
+       /* Get and set the up_threshold to support boosting. */
+       virtual int hal_power_dvfs_get_up_threshold(unsigned int res_type, char *res_name) = 0;
+       virtual int hal_power_dvfs_set_up_threshold(unsigned int res_type, char *res_name, int up_threshold) = 0;
+
+       /* Get the load_table of each resource to estimate the system load. */
+       virtual int hal_power_dvfs_get_load_table(unsigned int res_type, char *name, void *pass_cpu_load_table) = 0;
+
+       /**
+        * CPU Hotplug Operation for CPU H/W
+        */
+       /* Get and set the online status of resource. */
+       virtual int hal_power_hotplug_get_online_state(unsigned int res_type, char *res_name, int cpu) = 0;
+       virtual int hal_power_hotplug_set_online_state(unsigned int res_type, char *res_name, int cpu, int on) = 0;
+
+       /* Get and set the minimum number of online CPUs */
+       virtual int hal_power_hotplug_get_online_min_num (unsigned int res_type, char *res_name) = 0;
+       virtual int hal_power_hotplug_set_online_min_num (unsigned int res_type, char *res_name, int min_num) = 0;
+
+       /* Get and set the maximum number of online CPUs */
+       virtual int hal_power_hotplug_get_online_max_num (unsigned int res_type, char *res_name) = 0;
+       virtual int hal_power_hotplug_set_online_max_num (unsigned int res_type, char *res_name, int max_num) = 0;
+
+       /**
+        * Thermal Operation for CPU/BUS/GPU H/W
+        */
+       /* Get the current temperature of resource. */
+       virtual int hal_power_thermal_get_temp(unsigned int res_type, char *res_thermal_name) = 0;
+
+       /* Get the policy of thermal management unit. */
+       virtual int hal_power_thermal_get_policy(unsigned int res_type, char *res_thermal_name, char *policy) = 0;
+
+       /* Get and set the state of thermal cooling-device */
+       virtual int hal_power_thermal_set_cooling_device_state(unsigned int device_type, char *cooling_device_name, int state) = 0;
+       virtual int hal_power_thermal_get_cooling_device_state(unsigned int device_type, char *cooling_device_name) = 0;
+
+       /* Get maximum state of thermal cooling-device */
+       virtual int hal_power_thermal_get_cooling_device_max_state(unsigned int device_type, char *cooling_device_name) = 0;
+
+       /* Get and set the battery charging state */
+       virtual int hal_power_battery_set_charging_status(unsigned int device_type, char *res_name, int state) = 0;
+       virtual int hal_power_battery_get_charging_status(unsigned int device_type, char *res_name) = 0;
+
+       /* Get and set the battery charging current (unit: uA) */
+       virtual int hal_power_battery_set_charging_current(unsigned int device_type, char *res_name, int charing_current_uA) = 0;
+       virtual int hal_power_battery_get_charging_current(unsigned int device_type, char *res_name) = 0;
+
+       /**
+        * Memory Operation for Memory H/W
+        */
+       /* Get and set the /sys/kernel/debug/fault_around_bytes */
+       virtual int hal_power_memory_get_fault_around_bytes(unsigned int res_type, char *res_name) = 0;
+       virtual int hal_power_memory_set_fault_around_bytes(unsigned int res_type, char *res_name, int fault_around_bytes) = 0;
+
+       /**
+        * Miscellaneous Operation for CPU/BUS/GPU H/W
+        */
+       virtual int hal_power_misc_set_pmqos_data(unsigned int res_type, char *res_name, void *data) = 0;
+};
+
+class HalApiPowerMock:HalApiPowerMockInterface {
+public:
+       MOCK_METHOD1(hal_power_get_backend, int (unsigned int res_type));
+       MOCK_METHOD0(hal_power_put_backend, int (void));
+
+       MOCK_METHOD3(hal_power_dvfs_get_curr_governor, int (unsigned int res_type, char *res_name, char *governor));
+       MOCK_METHOD3(hal_power_dvfs_set_curr_governor, int (unsigned int res_type, char *res_name, char *governor));
+       MOCK_METHOD3(hal_power_dvfs_get_avail_governor, int (unsigned int res_type, char *res_name, char **avail_governor));
+
+       MOCK_METHOD2(hal_power_dvfs_get_curr_freq, int (unsigned int res_type, char *res_name));
+
+       MOCK_METHOD2(hal_power_dvfs_get_min_freq, int (unsigned int res_type, char *res_name));
+       MOCK_METHOD3(hal_power_dvfs_set_min_freq, int (unsigned int res_type, char *res_name, int freq));
+
+       MOCK_METHOD2(hal_power_dvfs_get_max_freq, int (unsigned int res_type, char *res_name));
+       MOCK_METHOD3(hal_power_dvfs_set_max_freq, int (unsigned int res_type, char *res_name, int freq));
+
+       MOCK_METHOD2(hal_power_dvfs_get_available_min_freq, int (unsigned int res_type, char *res_name));
+       MOCK_METHOD2(hal_power_dvfs_get_available_max_freq, int (unsigned int res_type, char *res_name));
+
+       MOCK_METHOD2(hal_power_dvfs_get_up_threshold, int (unsigned int res_type, char *res_name));
+       MOCK_METHOD3(hal_power_dvfs_set_up_threshold, int (unsigned int res_type, char *res_name, int up_threshold));
+
+       MOCK_METHOD3(hal_power_dvfs_get_load_table, int (unsigned int res_type, char *name, void *pass_cpu_load_table));
+
+       MOCK_METHOD3(hal_power_hotplug_get_online_state, int (unsigned int res_type, char *res_name, int cpu));
+       MOCK_METHOD4(hal_power_hotplug_set_online_state, int (unsigned int res_type, char *res_name, int cpu, int on));
+
+       MOCK_METHOD2(hal_power_hotplug_get_online_min_num, int (unsigned int res_type, char *res_name));
+       MOCK_METHOD3(hal_power_hotplug_set_online_min_num, int (unsigned int res_type, char *res_name, int min_num));
+
+       MOCK_METHOD2(hal_power_hotplug_get_online_max_num, int (unsigned int res_type, char *res_name));
+       MOCK_METHOD3(hal_power_hotplug_set_online_max_num, int (unsigned int res_type, char *res_name, int max_num));
+
+       MOCK_METHOD2(hal_power_thermal_get_temp, int (unsigned int res_type, char *res_thermal_name));
+
+       MOCK_METHOD3(hal_power_thermal_get_policy, int (unsigned int res_type, char *res_thermal_name, char *policy));
+
+       MOCK_METHOD3(hal_power_thermal_set_cooling_device_state, int (unsigned int device_type, char *cooling_device_name, int state));
+       MOCK_METHOD2(hal_power_thermal_get_cooling_device_state, int (unsigned int device_type, char *cooling_device_name));
+
+       MOCK_METHOD2(hal_power_thermal_get_cooling_device_max_state, int (unsigned int device_type, char *cooling_device_name));
+
+       MOCK_METHOD3(hal_power_battery_set_charging_status, int (unsigned int device_type, char *res_name, int state));
+       MOCK_METHOD2(hal_power_battery_get_charging_status, int (unsigned int device_type, char *res_name));
+
+       MOCK_METHOD3(hal_power_battery_set_charging_current, int (unsigned int device_type, char *res_name, int charing_current_uA));
+       MOCK_METHOD2(hal_power_battery_get_charging_current, int (unsigned int device_type, char *res_name));
+
+       MOCK_METHOD2(hal_power_memory_get_fault_around_bytes, int (unsigned int res_type, char *res_name));
+       MOCK_METHOD3(hal_power_memory_set_fault_around_bytes, int (unsigned int res_type, char *res_name, int fault_around_bytes));
+
+       MOCK_METHOD3(hal_power_misc_set_pmqos_data, int (unsigned int res_type, char *res_name, void *data));
+
+};
+
+extern HalApiPowerMock *gHalApiPowerMock;