diff options
| author | Mark Salyzyn <salyzyn@google.com> | 2016-11-07 09:39:30 -0800 |
|---|---|---|
| committer | Mark Salyzyn <salyzyn@google.com> | 2016-11-16 15:56:56 -0800 |
| commit | 52bd37e63373b410c009e8611508191dfbf31d30 (patch) | |
| tree | 32ed2ad56a69bb6d00b2193eaaed07d813e46982 /init/util_test.cpp | |
| parent | e631e470e059d84388f3aacfe11a3fa60a584ba7 (diff) | |
| download | system_core-52bd37e63373b410c009e8611508191dfbf31d30.tar.gz system_core-52bd37e63373b410c009e8611508191dfbf31d30.tar.bz2 system_core-52bd37e63373b410c009e8611508191dfbf31d30.zip | |
libcutils: move cutils/files.h to cutils/android_get_control_file.h
files.[h|cpp] is bound to be abused with junk, replace with
android_get_control_file.[h|cpp]. Plus some sundry cleanup.
Test: gTest libcutils-tests, logd-unit-tests, liblog-unit-tests,
logcat-unit-tests and init_tests
Bug: 32450474
Change-Id: Ibd4a7aa4624ea19a43d1f98a3c71ac37805d36b5
Diffstat (limited to 'init/util_test.cpp')
| -rw-r--r-- | init/util_test.cpp | 54 |
1 files changed, 30 insertions, 24 deletions
diff --git a/init/util_test.cpp b/init/util_test.cpp index 6ecbf908c..e9f164d82 100644 --- a/init/util_test.cpp +++ b/init/util_test.cpp @@ -16,6 +16,7 @@ #include "util.h" +#include <ctype.h> #include <errno.h> #include <fcntl.h> #include <stdlib.h> @@ -23,7 +24,9 @@ #include <sys/types.h> #include <unistd.h> -#include <cutils/files.h> +#include <android-base/stringprintf.h> +#include <android-base/test_utils.h> +#include <cutils/android_get_control_file.h> #include <gtest/gtest.h> #include <selinux/android.h> @@ -55,45 +58,48 @@ struct selabel_handle *sehandle; TEST(util, create_file) { if (!sehandle) sehandle = selinux_android_file_context_handle(); - static const char path[] = "/data/local/tmp/util.create_file.test"; - static const char key[] = ANDROID_FILE_ENV_PREFIX "_data_local_tmp_util_create_file_test"; - EXPECT_EQ(unsetenv(key), 0); - unlink(path); + TemporaryFile tf; + close(tf.fd); + EXPECT_GE(unlink(tf.path), 0); + + std::string key(ANDROID_FILE_ENV_PREFIX); + key += tf.path; + + std::for_each(key.begin(), key.end(), [] (char& c) { c = isalnum(c) ? c : '_'; }); + + EXPECT_EQ(unsetenv(key.c_str()), 0); - int fd; uid_t uid = decode_uid("logd"); gid_t gid = decode_uid("system"); mode_t perms = S_IRWXU | S_IWGRP | S_IRGRP | S_IROTH; static const char context[] = "u:object_r:misc_logd_file:s0"; - EXPECT_GE(fd = create_file(path, O_RDWR | O_CREAT, perms, uid, gid, context), 0); - if (fd < 0) return; + EXPECT_GE(tf.fd = create_file(tf.path, O_RDWR | O_CREAT, perms, uid, gid, context), 0); + if (tf.fd < 0) return; static const char hello[] = "hello world\n"; static const ssize_t len = strlen(hello); - EXPECT_EQ(write(fd, hello, len), len); - char buffer[sizeof(hello)]; + EXPECT_EQ(write(tf.fd, hello, len), len); + char buffer[sizeof(hello) + 1]; memset(buffer, 0, sizeof(buffer)); - EXPECT_GE(lseek(fd, 0, SEEK_SET), 0); - EXPECT_EQ(read(fd, buffer, sizeof(buffer)), len); - EXPECT_EQ(strcmp(hello, buffer), 0); - char val[32]; - snprintf(val, sizeof(val), "%d", fd); - EXPECT_EQ(android_get_control_file(path), -1); - setenv(key, val, true); - EXPECT_EQ(android_get_control_file(path), fd); - close(fd); - EXPECT_EQ(android_get_control_file(path), -1); - EXPECT_EQ(unsetenv(key), 0); + EXPECT_GE(lseek(tf.fd, 0, SEEK_SET), 0); + EXPECT_EQ(read(tf.fd, buffer, sizeof(buffer)), len); + EXPECT_EQ(std::string(hello), buffer); + EXPECT_EQ(android_get_control_file(tf.path), -1); + EXPECT_EQ(setenv(key.c_str(), android::base::StringPrintf("%d", tf.fd).c_str(), true), 0); + EXPECT_EQ(android_get_control_file(tf.path), tf.fd); + close(tf.fd); + EXPECT_EQ(android_get_control_file(tf.path), -1); + EXPECT_EQ(unsetenv(key.c_str()), 0); struct stat st; - EXPECT_EQ(stat(path, &st), 0); + EXPECT_EQ(stat(tf.path, &st), 0); EXPECT_EQ(st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO), perms); EXPECT_EQ(st.st_uid, uid); EXPECT_EQ(st.st_gid, gid); security_context_t con; - EXPECT_GE(getfilecon(path, &con), 0); + EXPECT_GE(getfilecon(tf.path, &con), 0); EXPECT_NE(con, static_cast<security_context_t>(NULL)); if (con) { EXPECT_EQ(context, std::string(con)); } freecon(con); - EXPECT_EQ(unlink(path), 0); + EXPECT_EQ(unlink(tf.path), 0); } |
