--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 <fcntl.h>
+#include <linux/xattr.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/smack.h>
+#include <sys/stat.h>
+
+#include <memory>
+
+#include <dpl/test/test_runner.h>
+#include <dpl/test/test_runner_child.h>
+
+#include <app_install_helper.h>
+#include <memory.h>
+#include <scoped_installer.h>
+#include <sm_api.h>
+
+#include <security-manager.h>
+
+using namespace SecurityManagerTest;
+
+RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER_SHM)
+
+RUNNER_TEST(shm01_create_file) {
+ AppInstallHelper app("shm01_app");
+ const char *shmName = "shm01_testName";
+ auto exLabel = app.generateAppLabel();
+ ScopedInstaller req(app);
+ char *label = NULL;
+
+ // clean up environment
+ shm_unlink(shmName);
+
+ int fd = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, app.getAppId().c_str());
+ RUNNER_ASSERT_ERRNO_MSG(fd >= 0, "security_manager_shm_open failed");
+ FdUniquePtr file(&fd);
+
+ ssize_t len = smack_new_label_from_file(fd, XATTR_NAME_SMACK, &label);
+ RUNNER_ASSERT_MSG(len > 0, "");
+
+ CStringPtr ptr(label);
+
+ RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
+ << " Found: " << label);
+
+ int ret = shm_unlink(shmName);
+ RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
+}
+
+RUNNER_TEST(shm02_double_open) {
+ AppInstallHelper app("shm02_app");
+ const char *shmName = "shm02_testName";
+ auto exLabel = app.generateAppLabel();
+ ScopedInstaller req(app);
+ char *label = NULL;
+
+ // clean up environment
+ shm_unlink(shmName);
+
+ int fd1 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, app.getAppId().c_str());
+ RUNNER_ASSERT_ERRNO_MSG(fd1 >= 0, "security_manager_shm_open failed");
+ FdUniquePtr file1(&fd1);
+
+ ssize_t len = smack_new_label_from_file(fd1, XATTR_NAME_SMACK, &label);
+ RUNNER_ASSERT_MSG(len > 0, "");
+
+ CStringPtr ptr(label);
+
+ RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
+ << " Found: " << label);
+
+ int fd2 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, app.getAppId().c_str());
+ RUNNER_ASSERT_ERRNO_MSG(fd2 >= 0, "security_manager_shm_open failed");
+ FdUniquePtr file2(&fd2);
+
+ int ret = shm_unlink(shmName);
+ RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
+}
+
+RUNNER_TEST(shm03_double_share) {
+ const char *shmName = "shm03_testName";
+
+ AppInstallHelper appa("shm03_testAppA");
+ auto exLabel = appa.generateAppLabel();
+ ScopedInstaller reqa(appa);
+
+ AppInstallHelper appb("shm03_testAppB");
+ ScopedInstaller reqb(appb);
+
+ // clean up environment
+ shm_unlink(shmName);
+
+ char *label = NULL;
+
+ int fd1 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, appa.getAppId().c_str());
+ RUNNER_ASSERT_ERRNO_MSG(fd1 >= 0, "security_manager_shm_open failed");
+ FdUniquePtr file1(&fd1);
+
+ ssize_t len = smack_new_label_from_file(fd1, XATTR_NAME_SMACK, &label);
+ RUNNER_ASSERT_MSG(len > 0, "");
+
+ CStringPtr ptr(label);
+
+ RUNNER_ASSERT_MSG(exLabel == label, "Wrong label. Expected: " << exLabel
+ << " Found: " << label);
+
+ int fd2 = security_manager_shm_open(shmName, O_CREAT | O_RDWR, 0666, appb.getAppId().c_str());
+ int err = errno;
+ RUNNER_ASSERT_ERRNO_MSG(fd2 < 0 && err == EACCES, "security_manager_shm_open should failed with EPERM but is");
+ FdUniquePtr file2(&fd2);
+
+ int ret = shm_unlink(shmName);
+ RUNNER_ASSERT_ERRNO_MSG(0 == ret, "shm_unlink failed");
+}
+