#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <errno.h>
#include <assert.h>
#include <fcntl.h>
+#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
return true;
}
+static int mkdir_one(const char *dir, mode_t mode)
+{
+ if (!dir)
+ return -EINVAL;
+
+ if (access(dir, F_OK) == 0)
+ return 0;
+
+ return mkdir(dir, mode);
+}
+
+static int create_directory(const char *path)
+{
+ char directory_path[PATH_MAX] = { 0 , };
+ char *p;
+ int ret;
+
+ if (!path)
+ return -EINVAL;
+
+ if (path[0] != '/')
+ return -EINVAL;
+
+ if (strlen(path) > PATH_MAX - 1)
+ return -ENAMETOOLONG;
+
+ // copy path except the last filename
+ strncpy(directory_path, path, strrchr(path, '/') - path);
+
+ p = strchr(directory_path + 1, '/');
+ for (;;) {
+ if (!p)
+ break;
+
+ *p = '\0';
+ ret = mkdir_one(directory_path, 0644);
+ if (ret < 0)
+ return ret;
+ *p = '/';
+
+ p = strchr(p + 1, '/');
+ }
+
+ return mkdir_one(directory_path, 0644);
+
+}
+
static int write_comaptibility_info(struct compatibility_info *info, int entry_size)
{
int fd = -1;
int ret;
ssize_t n_write;
+ ret = create_directory(compatibility_result_path);
+ if (ret < 0) {
+ errno = -ret;
+ _E("Failed to create directory for %s, %m",
+ compatibility_result_path);
+ return ret;
+ }
+
fd = open(compatibility_result_path,
O_WRONLY | O_CREAT | O_EXCL, 0644);
if (fd == -1) {
if (skip_version_check)
return 0;
- write_comaptibility_info(infos, HAL_MODULE_END);
-
- return 0;
+ return write_comaptibility_info(infos, HAL_MODULE_END);
}
int hal_api_cc_check_backend_compatibility(enum hal_module module,
using namespace std;
-#define TEST_COMPATIBILITY_RESULT_PATH "./compatibility-result"
-
class HalccObjectTest : public ::testing::Test
{
protected:
static void SetUpTestSuite() {
int ret;
- char cwd[PATH_MAX] = { 0, };
- ret = asprintf(&g_manifest_directory, "%s/%s",
- getcwd(cwd, sizeof(cwd)), "test-hal-manifest");
+ g_cwd = getcwd(NULL, 0);
+ if (!g_cwd)
+ FAIL();
+
+ ret = asprintf(&g_manifest_directory, "%s/%s", g_cwd, "test-hal-manifest");
+ if (ret == -1)
+ FAIL();
+
+ ret = asprintf(&g_compatibility_result_path, "%s/.hal-backend-compatibility", g_cwd);
if (ret == -1)
FAIL();
static void TearDownTestSuite() {
for (int module = HAL_MODULE_UNKNOWN; module < HAL_MODULE_END; ++module)
mock_hal_backend_data_unset_version((enum hal_module) module);
- unlink(TEST_COMPATIBILITY_RESULT_PATH);
+ unlink(g_compatibility_result_path);
unset_compatibility_manifest_directory();
+ free(g_compatibility_result_path);
free(g_manifest_directory);
+ free(g_cwd);
g_manifest_directory = NULL;
}
static halcc_manifest *g_manifest;
static char *g_manifest_directory;
+ static char *g_compatibility_result_path;
+ static char *g_cwd;
};
halcc_manifest* HalccObjectTest::g_manifest = NULL;
char* HalccObjectTest::g_manifest_directory = NULL;
+char* HalccObjectTest::g_compatibility_result_path = NULL;
+char* HalccObjectTest::g_cwd = NULL;
static bool have_exact_version(halcc_hal *hal, const char *version)
{
/* mock systemd generator context */
setenv("SYSTEMD_SCOPE", "system", 1);
- set_compatibility_result_path(TEST_COMPATIBILITY_RESULT_PATH);
+ set_compatibility_result_path(g_compatibility_result_path);
ret = hal_api_cc_check_backend_compatibility(HAL_MODULE_DEVICE_DISPLAY, &compatibility);
ASSERT_EQ(ret, 0);
- ret = access(TEST_COMPATIBILITY_RESULT_PATH, F_OK | ACCESS_DO_NOT_HOOK);
+ ret = access(g_compatibility_result_path, F_OK | ACCESS_DO_NOT_HOOK);
ASSERT_EQ(ret, 0);
- unlink(TEST_COMPATIBILITY_RESULT_PATH);
+ unlink(g_compatibility_result_path);
unset_compatibility_result_path();
unsetenv("SYSTEMD_SCOPE");
}
enum hal_common_backend_compatibility compatibility;
int ret;
- set_compatibility_result_path(TEST_COMPATIBILITY_RESULT_PATH);
+ set_compatibility_result_path(g_compatibility_result_path);
ret = hal_api_cc_check_backend_compatibility(HAL_MODULE_DEVICE_DISPLAY, &compatibility);
ASSERT_EQ(ret, 0);
- ret = access(TEST_COMPATIBILITY_RESULT_PATH, F_OK | ACCESS_DO_NOT_HOOK);
+ ret = access(g_compatibility_result_path, F_OK | ACCESS_DO_NOT_HOOK);
ASSERT_EQ(ret, -1);
unset_compatibility_result_path();
int ret;
setenv("SYSTEMD_SCOPE", "system", 1);
- set_compatibility_result_path(TEST_COMPATIBILITY_RESULT_PATH);
+ set_compatibility_result_path(g_compatibility_result_path);
- ret = access(TEST_COMPATIBILITY_RESULT_PATH, F_OK | ACCESS_DO_NOT_HOOK);
+ ret = access(g_compatibility_result_path, F_OK | ACCESS_DO_NOT_HOOK);
ASSERT_EQ(ret, -1);
mock_hal_backend_data_set_version(HAL_MODULE_DEVICE_DISPLAY, 1, 0); /* compatible with 1.2 */
ASSERT_EQ(ret, 0);
ASSERT_EQ(compatibility, HAL_COMMON_BACKEND_COMPATIBILITY_COMPATIBLE);
- ret = access(TEST_COMPATIBILITY_RESULT_PATH, F_OK | ACCESS_DO_NOT_HOOK);
+ ret = access(g_compatibility_result_path, F_OK | ACCESS_DO_NOT_HOOK);
ASSERT_EQ(ret, 0);
ret = hal_api_cc_check_backend_compatibility(HAL_MODULE_TDM, &compatibility);
mock_hal_backend_data_unset_version(HAL_MODULE_DEVICE_DISPLAY);
mock_hal_backend_data_unset_version(HAL_MODULE_TDM);
- unlink(TEST_COMPATIBILITY_RESULT_PATH);
+ unlink(g_compatibility_result_path);
unset_compatibility_result_path();
unsetenv("SYSTEMD_SCOPE");
}
enum hal_common_backend_compatibility compatibility;
int ret;
- set_compatibility_result_path(TEST_COMPATIBILITY_RESULT_PATH);
+ set_compatibility_result_path(g_compatibility_result_path);
- ret = access(TEST_COMPATIBILITY_RESULT_PATH, F_OK | ACCESS_DO_NOT_HOOK);
+ ret = access(g_compatibility_result_path, F_OK | ACCESS_DO_NOT_HOOK);
ASSERT_EQ(ret, -1);
mock_hal_backend_data_set_version(HAL_MODULE_DEVICE_DISPLAY, 1, 1);
ASSERT_EQ(ret, 0);
ASSERT_EQ(compatibility, HAL_COMMON_BACKEND_COMPATIBILITY_INCOMPATIBLE);
- ret = access(TEST_COMPATIBILITY_RESULT_PATH, F_OK | ACCESS_DO_NOT_HOOK);
+ ret = access(g_compatibility_result_path, F_OK | ACCESS_DO_NOT_HOOK);
ASSERT_EQ(ret, -1);
mock_hal_backend_data_unset_version(HAL_MODULE_DEVICE_DISPLAY);
int ret;
void *funcs;
- set_compatibility_result_path(TEST_COMPATIBILITY_RESULT_PATH);
+ set_compatibility_result_path(g_compatibility_result_path);
- ret = access(TEST_COMPATIBILITY_RESULT_PATH, F_OK | ACCESS_DO_NOT_HOOK);
+ ret = access(g_compatibility_result_path, F_OK | ACCESS_DO_NOT_HOOK);
ASSERT_EQ(ret, -1);
funcs = malloc(10000);
ret = hal_common_get_backend(HAL_MODULE_DEVICE_DISPLAY, (void **) funcs);
ASSERT_EQ(ret, 0);
- ret = access(TEST_COMPATIBILITY_RESULT_PATH, F_OK | ACCESS_DO_NOT_HOOK);
+ ret = access(g_compatibility_result_path, F_OK | ACCESS_DO_NOT_HOOK);
ASSERT_EQ(ret, -1);
free(funcs);
void *funcs;
setenv("SYSTEMD_SCOPE", "system", 1);
- set_compatibility_result_path(TEST_COMPATIBILITY_RESULT_PATH);
+ set_compatibility_result_path(g_compatibility_result_path);
- ret = access(TEST_COMPATIBILITY_RESULT_PATH, F_OK | ACCESS_DO_NOT_HOOK);
+ ret = access(g_compatibility_result_path, F_OK | ACCESS_DO_NOT_HOOK);
ASSERT_EQ(ret, -1);
funcs = malloc(10000);
ret = hal_common_get_backend(HAL_MODULE_DEVICE_DISPLAY, (void **) funcs);
ASSERT_EQ(ret, 0);
- ret = access(TEST_COMPATIBILITY_RESULT_PATH, F_OK | ACCESS_DO_NOT_HOOK);
+ ret = access(g_compatibility_result_path, F_OK | ACCESS_DO_NOT_HOOK);
ASSERT_EQ(ret, -1);
/**
* ASSERT_EQ(ret, -EINVAL);
*/
- ret = access(TEST_COMPATIBILITY_RESULT_PATH, F_OK | ACCESS_DO_NOT_HOOK);
+ ret = access(g_compatibility_result_path, F_OK | ACCESS_DO_NOT_HOOK);
ASSERT_EQ(ret, -1);
free(funcs);