Revert "Add unit tests for service contexts backend"

Revert submission 2110147

Reason for revert: Broken sdk_mac target.
Reverted Changes:
Ifcd00954b:Refactor sources in Android.bp
Ie313b6d63:Add unit tests for service contexts backend

Change-Id: I616f43ed57626b5255dcf193f5f2c750b47f2fae
This commit is contained in:
Thiébaud Weksteen 2022-06-16 23:46:53 +00:00
parent 74e8681330
commit 94ad1bb10e
5 changed files with 3 additions and 127 deletions

View file

@ -184,26 +184,6 @@ cc_library {
},
}
cc_test_host {
name: "libselinux_test",
defaults: ["libselinux_defaults"],
srcs: ["src/android/android_unittest.cpp"],
cflags: [
// regex.h will conflict with the default regex.h from libc.
// Skip regex for gtest.
"-DGTEST_HAS_POSIX_RE=0",
"-DANDROID_UNIT_TESTING",
],
whole_static_libs: [
"libpcre2",
"libbase",
],
// Use default stl.
stl:""
}
cc_binary_host {
name: "sefcontext_compile",
defaults: ["libselinux_defaults"],

View file

@ -128,10 +128,11 @@ struct selabel_handle* initialize_backend(
return sehandle;
}
struct selabel_handle* context_handle(
/* Initialize a backend using a set of context paths */
static struct selabel_handle* context_handle(
unsigned int backend,
const char* const context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS],
const char* name)
char *name)
{
const char* existing_paths[MAX_CONTEXT_PATHS];
struct selinux_opt opts[MAX_CONTEXT_PATHS];

View file

@ -2,11 +2,6 @@
#include "android_common.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Within each set of files, adds the first file that is accessible to `paths`.
* Returns the number of accessible files. */
size_t find_existing_files(
@ -27,14 +22,3 @@ struct selabel_handle* initialize_backend(
const char* name,
const struct selinux_opt* opts,
size_t nopts);
/* Initialize a backend using a set of context paths */
struct selabel_handle* context_handle(
unsigned int backend,
const char* const context_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS],
const char* name);
#ifdef __cplusplus
}
#endif

View file

@ -1,85 +0,0 @@
#include <gtest/gtest.h>
#include <android-base/file.h>
#include <android-base/stringprintf.h>
#include "android_internal.h"
using android::base::StringPrintf;
using android::base::WriteStringToFile;
using std::string;
class AndroidSELinuxTest : public ::testing::Test {
protected:
TemporaryDir tdir_;
};
TEST_F(AndroidSELinuxTest, LoadAndLookupServiceContext)
{
string service_contexts =
StringPrintf("%s/service_contexts", tdir_.path);
string unused_service_contexts =
StringPrintf("%s/unused_contexts", tdir_.path);
string vendor_contexts =
StringPrintf("%s/vendor_service_contexts", tdir_.path);
WriteStringToFile("account u:object_r:account_service:s0\n",
service_contexts);
WriteStringToFile("ignored u:object_r:ignored_service:s0\n",
unused_service_contexts);
WriteStringToFile(
"android.hardware.power.IPower/default u:object_r:hal_power_service:s0\n",
vendor_contexts);
static const char *const
service_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS] = {
{ service_contexts.c_str(),
unused_service_contexts.c_str() },
{ vendor_contexts.c_str() }
};
struct selabel_handle *handle = context_handle(
SELABEL_CTX_ANDROID_SERVICE, service_paths, "test_service");
EXPECT_NE(handle, nullptr);
char *tcontext;
EXPECT_EQ(selabel_lookup_raw(handle, &tcontext, "foobar",
SELABEL_CTX_ANDROID_SERVICE),
-1);
EXPECT_EQ(selabel_lookup_raw(handle, &tcontext, "account",
SELABEL_CTX_ANDROID_SERVICE),
0);
EXPECT_STREQ(tcontext, "u:object_r:account_service:s0");
free(tcontext);
EXPECT_EQ(selabel_lookup_raw(handle, &tcontext, "ignored",
SELABEL_CTX_ANDROID_SERVICE),
-1);
EXPECT_EQ(selabel_lookup_raw(handle, &tcontext,
"android.hardware.power.IPower/default",
SELABEL_CTX_ANDROID_SERVICE),
0);
EXPECT_STREQ(tcontext, "u:object_r:hal_power_service:s0");
free(tcontext);
selabel_close(handle);
}
TEST_F(AndroidSELinuxTest, FailLoadingServiceContext)
{
string service_contexts =
StringPrintf("%s/service_contexts", tdir_.path);
WriteStringToFile("garbage\n", service_contexts);
static const char *const
service_paths[MAX_CONTEXT_PATHS][MAX_ALT_CONTEXT_PATHS] = {
{ service_contexts.c_str() }
};
struct selabel_handle *handle = context_handle(
SELABEL_CTX_ANDROID_SERVICE, service_paths, "test_service");
EXPECT_EQ(handle, nullptr);
}

View file

@ -147,18 +147,14 @@ static void init_lib(void) __attribute__ ((constructor));
static void init_lib(void)
{
selinux_page_size = sysconf(_SC_PAGE_SIZE);
#ifndef ANDROID_UNIT_TESTING
init_selinuxmnt();
#ifndef ANDROID
has_selinux_config = (access(SELINUXCONFIG, F_OK) == 0);
#endif
#endif
}
static void fini_lib(void) __attribute__ ((destructor));
static void fini_lib(void)
{
#ifndef ANDROID_UNIT_TESTING
fini_selinuxmnt();
#endif
}