Add test cases for launch.c 46/258446/6
authorjh9216.park <jh9216.park@samsung.com>
Tue, 18 May 2021 04:35:34 +0000 (00:35 -0400)
committerjh9216.park <jh9216.park@samsung.com>
Thu, 20 May 2021 06:40:53 +0000 (02:40 -0400)
- Add a TC for aul_launch_app
- Add a TC for aul_terminate_pid

Change-Id: I29360b43698801e4bfe52a8824313e5667d418ea
Signed-off-by: jh9216.park <jh9216.park@samsung.com>
packaging/aul.spec
test/unit_tests/CMakeLists.txt
test/unit_tests/main.cc
test/unit_tests/mock/socket_mock.cc [new file with mode: 0644]
test/unit_tests/mock/socket_mock.h [new file with mode: 0644]
test/unit_tests/test_launch.cc [new file with mode: 0644]

index 5b83ac9..691f8f8 100644 (file)
@@ -143,7 +143,8 @@ find . -name '*.gcno' -exec cp '{}' gcov-obj ';'
 %endif
 
 %check
-ctest --output-on-failure %{?_smp_mflags}
+export LD_LIBRARY_PATH="../../"
+ctest -V
 %if 0%{?gcov:1}
 lcov -c --ignore-errors graph --no-external -q -d . -o %{name}.info
 genhtml %{name}.info -o %{name}.out
index 9232e70..79e009b 100644 (file)
@@ -1,18 +1,38 @@
 AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/ UNIT_TESTS_SRCS)
-AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/mock/ UNIT_TESTS_SRCS)
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/mock/ UNIT_TESTS_MOCK)
 
-ADD_EXECUTABLE(${TARGET_AUL_UNIT_TESTS} ${UNIT_TESTS_SRCS})
+ADD_EXECUTABLE(${TARGET_AUL_UNIT_TESTS}
+  ${UNIT_TESTS_SRCS}
+  ${UNIT_TESTS_MOCK}
+)
 
 TARGET_INCLUDE_DIRECTORIES(${TARGET_AUL_UNIT_TESTS} PUBLIC
   "${CMAKE_CURRENT_SOURCE_DIR}/../"
+  "${CMAKE_CURRENT_SOURCE_DIR}/../../"
   "${CMAKE_CURRENT_SOURCE_DIR}/../../include"
   "${CMAKE_CURRENT_SOURCE_DIR}/../../src"
+  "${CMAKE_CURRENT_SOURCE_DIR}/../../aul"
+  "${CMAKE_CURRENT_SOURCE_DIR}/../../aul/api"
 )
 
 APPLY_PKG_CONFIG(${TARGET_AUL_UNIT_TESTS} PUBLIC
-  GLIB_DEPS
   GMOCK_DEPS
   BUNDLE_DEPS
+  CAPI_SYSTEM_INFO_DEPS
+  DLOG_DEPS
+  GIO_DEPS
+  GLIB_DEPS
+  INIPARSER_DEPS
+  LIBSMACK_DEPS
+  LIBTZPLATFORM_CONFIG_DEPS
+  LIBXML_DEPS
+  PARCEL_DEPS
+  PKGMGR_INFO_DEPS
+  STORAGE_DEPS
+  TTRACE_DEPS
+  UUID_DEPS
+  VCONF_DEPS
+  XDGMIME_DEPS
 )
 
 TARGET_LINK_LIBRARIES(${TARGET_AUL_UNIT_TESTS} PUBLIC ${TARGET_AUL})
index 1702774..b3b4e12 100644 (file)
@@ -5,6 +5,22 @@
 #include <gtest/gtest.h>
 #include <gmock/gmock.h>
 
+//#define LOG_INTERNAL
+
+#include <dlog.h>
+
+extern "C" int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...) {
+#ifdef LOG_INTERNAL
+  printf("%s:", tag);
+  va_list ap;
+  va_start(ap, fmt);
+  vprintf(fmt, ap);
+  va_end(ap);
+  printf("\n");
+#endif
+  return 0;
+}
+
 int main(int argc, char** argv) {
   try {
     testing::InitGoogleTest(&argc, argv);
diff --git a/test/unit_tests/mock/socket_mock.cc b/test/unit_tests/mock/socket_mock.cc
new file mode 100644 (file)
index 0000000..081fb86
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2021 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 <glib.h>
+
+#include "mock/socket_mock.h"
+#include "mock/mock_hook.h"
+#include "mock/test_fixture.h"
+
+extern "C" ssize_t recv (int fd, void* buf, size_t n, int flags) {
+  return MOCK_HOOK_P4(SocketMock, recv, fd, buf, n, flags);
+}
+
+extern "C" ssize_t send (int fd, const void* buf, size_t n, int flags) {
+  return MOCK_HOOK_P4(SocketMock, send, fd, buf, n, flags);
+}
+
+extern "C" int socket (int domain, int type, int protocol) {
+  return MOCK_HOOK_P3(SocketMock, socket, domain, type, protocol);
+}
+
+extern "C" int connect(int sockfd, const struct sockaddr* addr,
+    socklen_t addrlen) {
+  return MOCK_HOOK_P3(SocketMock, connect, sockfd, addr, addrlen);
+}
+
+extern "C" int close(int fd) {
+  return 0;
+}
+
+extern "C" int fcntl(int fd, int cmd, ... /* arg */ ) {
+  return 0;
+}
\ No newline at end of file
diff --git a/test/unit_tests/mock/socket_mock.h b/test/unit_tests/mock/socket_mock.h
new file mode 100644 (file)
index 0000000..0f85412
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2021 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.
+ */
+
+#ifndef TEST_UNIT_TESTS_MOCK_SOCKET_MOCK_H_
+#define TEST_UNIT_TESTS_MOCK_SOCKET_MOCK_H_
+
+#include <fcntl.h>
+#include <gmock/gmock.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include "mock/module_mock.h"
+
+constexpr const int kFakeFd = 100;
+
+class SocketMock : public virtual ModuleMock {
+ public:
+  SocketMock() {
+    using ::testing::_;
+    using ::testing::Return;
+    using ::testing::Invoke;
+
+    ON_CALL(*this, send(_, _, _, _))
+        .WillByDefault(Invoke([&](
+            int, const void*, size_t size, int) -> ssize_t {
+            return size;
+        }));
+    ON_CALL(*this, socket(_, _, _))
+        .WillByDefault(Return(kFakeFd));
+  }
+
+  MOCK_METHOD4(recv, ssize_t (int, void*, size_t, int));
+  MOCK_METHOD4(send, ssize_t (int, const void*, size_t, int));
+  MOCK_METHOD3(socket, int (int, int, int));
+  MOCK_METHOD3(connect, int (int, const struct sockaddr*, socklen_t));
+};
+
+#endif  // TEST_UNIT_TESTS_MOCK_SOCKET_MOCK_H_
+
diff --git a/test/unit_tests/test_launch.cc b/test/unit_tests/test_launch.cc
new file mode 100644 (file)
index 0000000..1c84325
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2021 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 <stdlib.h>
+#include <gtest/gtest.h>
+
+#include <bundle_cpp.h>
+#include <iostream>
+#include <memory>
+#include <aul.h>
+
+#include "mock/socket_mock.h"
+#include "mock/mock_hook.h"
+#include "mock/test_fixture.h"
+#include "mock/socket_mock.h"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Return;
+using ::testing::SetArgPointee;
+using ::testing::Invoke;
+using ::testing::SaveArg;
+
+namespace {
+
+class Mocks : virtual public ::testing::NiceMock<SocketMock> {};
+
+}  // namespace
+
+class LaunchTest : public TestFixture {
+ public:
+  LaunchTest() : TestFixture(std::make_unique<::Mocks>()) {}
+
+  virtual void SetUp() { }
+  virtual void TearDown() {}
+};
+
+TEST_F(LaunchTest, aul_launch_app) {
+  tizen_base::Bundle b;
+  EXPECT_CALL(GetMock<SocketMock>(), socket(_, _, _))
+      .Times(1);
+
+  EXPECT_CALL(GetMock<SocketMock>(), connect(_, _, _))
+      .Times(1);
+
+  EXPECT_CALL(GetMock<SocketMock>(), send(_, _, _, _))
+      .Times(1);
+
+  EXPECT_CALL(GetMock<SocketMock>(), recv(_, _, _, _))
+      .Times(1)
+      .WillOnce(Invoke([](int fd, void* buf, size_t n, int flags) -> ssize_t {
+        int ret = 0;
+        memcpy(buf, &ret, sizeof(int));
+        return sizeof(int);
+      }));
+
+  int ret = aul_launch_app("test_appid", b.GetHandle());
+  EXPECT_EQ(ret, AUL_R_OK);
+}
+
+TEST_F(LaunchTest, aul_terminate_pid) {
+  tizen_base::Bundle b;
+  EXPECT_CALL(GetMock<SocketMock>(), socket(_, _, _))
+      .Times(1);
+
+  EXPECT_CALL(GetMock<SocketMock>(), connect(_, _, _))
+      .Times(1);
+
+  EXPECT_CALL(GetMock<SocketMock>(), send(_, _, _, _))
+      .Times(1);
+
+  EXPECT_CALL(GetMock<SocketMock>(), recv(_, _, _, _))
+      .Times(1)
+      .WillOnce(Invoke([](int fd, void* buf, size_t n, int flags) -> ssize_t {
+        int ret = 0;
+        memcpy(buf, &ret, sizeof(int));
+        return sizeof(int);
+      }));
+
+  int ret = aul_terminate_pid(1000);
+  EXPECT_EQ(ret, AUL_R_OK);
+}