Replace Haltest using nfc CAPI with haltest using d-bus methods. 11/187111/6 submit/tizen/20180820.085416
authorAbhishek Sansanwal <abhishek.s94@samsung.com>
Mon, 20 Aug 2018 06:01:08 +0000 (11:31 +0530)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 20 Aug 2018 08:37:05 +0000 (17:37 +0900)
Description: Adhere to the new haltest guidelines by using
the dbus methods instead of nfc CAPI.

Signed-off-by: Abhishek Sansanwal <abhishek.s94@samsung.com>
Change-Id: Ie7fa0644926d298119161aa3c3f68c9c17963977

CMakeLists.txt [changed mode: 0755->0644]
haltests/CMakeLists.txt [new file with mode: 0644]
haltests/gdbus.cpp [new file with mode: 0644]
haltests/gdbus.h [new file with mode: 0644]
haltests/manager.cpp [new file with mode: 0644]
haltests/manager.h [new file with mode: 0644]
haltests/nfcmgr.cpp [new file with mode: 0644]
haltests/nfcmgr.h [new file with mode: 0644]
packaging/nfc-manager.spec

old mode 100755 (executable)
new mode 100644 (file)
index 9e5170b..00c975e
@@ -10,3 +10,4 @@ ADD_DEFINITIONS("-D_GNU_SOURCE")
 
 ADD_SUBDIRECTORY(src/commonlib)
 ADD_SUBDIRECTORY(src/manager)
+ADD_SUBDIRECTORY(haltests)
diff --git a/haltests/CMakeLists.txt b/haltests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..1ad608d
--- /dev/null
@@ -0,0 +1,33 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(gtest-haltest-nfc-manager C CXX)
+
+SET(HALTEST "nfc-manager-haltests")
+ADD_DEFINITIONS("-DUSE_DLOG")
+
+SET(REQUIRES_LIST ${REQUIRES_LIST}
+       glib-2.0
+       gio-2.0
+       gmock
+       dlog
+       capi-system-info
+)
+
+INCLUDE(FindPkgConfig)
+PKG_CHECK_MODULES(HALTEST_PKG REQUIRED ${REQUIRES_LIST})
+
+FOREACH(flag ${HALTEST_PKG_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall -fPIE")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS}")
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
+
+FILE(GLOB HALTEST_SRCS *.cpp)
+SET(HALTEST_SRCS ${HALTEST_SRCS})
+
+ADD_EXECUTABLE(${HALTEST} ${HALTEST_SRCS})
+TARGET_LINK_LIBRARIES(${HALTEST} ${HALTEST_LDFLAGS} ${HALTEST_PKG_LDFLAGS} -ldl -lgcov)
+
+INSTALL(TARGETS ${HALTEST} RUNTIME DESTINATION ${BIN_DIR})
diff --git a/haltests/gdbus.cpp b/haltests/gdbus.cpp
new file mode 100644 (file)
index 0000000..6138af8
--- /dev/null
@@ -0,0 +1,185 @@
+/*
+ * Copyright (c) 2018 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 <stdio.h>
+#include <stdlib.h>
+#include <iostream>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "gdbus.h"
+
+GDbus::GDbus()
+{
+       this->m_pConnection = NULL;
+       this->m_pCancellable = NULL;
+}
+
+GDbus::~GDbus()
+{
+       GDBusConnection *conn = this->m_pConnection;
+       GCancellable *cancel = this->m_pCancellable;
+
+       if (cancel) {
+               g_cancellable_cancel(cancel);
+               g_object_unref(cancel);
+               cancel = NULL;
+       }
+
+       if (conn) {
+               g_object_unref(conn);
+               conn = NULL;
+       }
+}
+
+error_e GDbus::Create(void)
+{
+       GError *err = NULL;
+
+#if !GLIB_CHECK_VERSION(2, 36, 0)
+       g_type_init();
+#endif
+
+       this->m_pConnection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
+       if (this->m_pConnection == NULL) {
+               if (err != NULL) {
+                       GLOGD("Failed to connect to the D-BUS daemon [%s]", err->message);
+                       g_error_free(err);
+               }
+
+               return ERROR_OPERATION_FAILED;
+       }
+
+       this->m_pCancellable = g_cancellable_new();
+
+       return ERROR_NONE;
+}
+
+error_e GDbus::Destroy(void)
+{
+       g_cancellable_cancel(this->m_pCancellable);
+       g_object_unref(this->m_pCancellable);
+       this->m_pCancellable = NULL;
+
+       g_object_unref(this->m_pConnection);
+       this->m_pConnection = NULL;
+
+       return ERROR_NONE;
+}
+
+GDBusConnection *GDbus::GetConnection(void)
+{
+       return this->m_pConnection;
+}
+
+GCancellable *GDbus::GetCancellable(void)
+{
+       return this->m_pCancellable;
+}
+
+error_e GDbus::ConvertErrorStringToEnum(const char *error)
+{
+       if (NULL != strstr(error, "NoReply"))
+               return ERROR_INVALID_OPERATION;
+       else if (NULL != strstr(error, "Failed"))
+               return ERROR_OPERATION_FAILED;
+       else if (NULL != strstr(error, "UnknownMethod"))
+               return ERROR_INVALID_OPERATION;
+       else if (NULL != strstr(error, "InvalidArguments"))
+               return ERROR_INVALID_PARAMETER;
+       else if (NULL != strstr(error, "AccessDenied"))
+               return ERROR_PERMISSION_DENIED;
+       else if (NULL != strstr(error, "PermissionDenied"))
+               return ERROR_PERMISSION_DENIED;
+       else if (NULL != strstr(error, "NotSupported"))
+               return ERROR_NOT_SUPPORTED;
+       else if (NULL != strstr(error, "InProgress"))
+               return ERROR_IN_PROGRESS;
+
+       return ERROR_OPERATION_FAILED;
+}
+
+GVariant *GDbus::InvokeMethod(const char *dest, const char *path,
+       const char *iface_name, const char *method, GVariant *params, error_e *dbus_error)
+{
+       GError *error = NULL;
+       GVariant *reply = NULL;
+       GDBusConnection *connection = NULL;
+       *dbus_error = ERROR_NONE;
+
+       connection = GetConnection();
+       if (connection == NULL) {
+               GLOGD("GDBusconnection is NULL");
+               *dbus_error = ERROR_NOT_INITIALIZED;
+               return reply;
+       }
+
+       reply = g_dbus_connection_call_sync(connection,
+                       dest,
+                       path,
+                       iface_name,
+                       method,
+                       params,
+                       NULL,
+                       G_DBUS_CALL_FLAGS_NONE,
+                       DBUS_REPLY_TIMEOUT,
+                       GetCancellable(),
+                       &error);
+
+       if (reply == NULL) {
+               if (error != NULL) {
+                       GLOGD("g_dbus_connection_call_sync() failed "
+                               "error [%d: %s]", error->code, error->message);
+                       *dbus_error = ConvertErrorStringToEnum(error->message);
+                       g_error_free(error);
+               } else {
+                       GLOGD("g_dbus_connection_call_sync() failed");
+                       *dbus_error = ERROR_OPERATION_FAILED;
+               }
+
+               return NULL;
+       }
+
+       return reply;
+}
+
+error_e GDbus::InvokeMethodNonblock(const char *dest, const char *path,
+       const char *iface_name, const char *method, GVariant *params, int timeout,
+       GAsyncReadyCallback notify_func, void *user_data)
+{
+       GDBusConnection *connection = NULL;
+
+       connection = GetConnection();
+       if (connection == NULL) {
+               GLOGD("GDBusconnection is NULL");
+               return ERROR_NOT_INITIALIZED;
+       }
+
+       g_dbus_connection_call(connection,
+                       dest,
+                       path,
+                       iface_name,
+                       method,
+                       params,
+                       NULL,
+                       G_DBUS_CALL_FLAGS_NONE,
+                       timeout,
+                       GetCancellable(),
+                       (GAsyncReadyCallback) notify_func,
+                       (gpointer)user_data);
+
+       return ERROR_NONE;
+}
diff --git a/haltests/gdbus.h b/haltests/gdbus.h
new file mode 100644 (file)
index 0000000..eb5312c
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2018 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.
+ */
+#ifndef __NFC_MGR_GDBUS_H__
+#define __NFC_MGR_GDBUS_H__
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include "nfcmgr.h"
+
+#define GMAINTIMEOUT 10000
+#define DBUS_REPLY_TIMEOUT (120 * 1000)
+
+#define NFC_MGR_SERVICE "org.tizen.NetNfcService"
+#define NFC_MGR_PATH "/org/tizen/NetNfcService/Manager"
+#define NFC_MANAGER_INTERFACE NFC_MGR_SERVICE ".Manager"
+
+#define NFC_MANAGER_SET_ACTIVE "SetActive"
+
+
+class GDbus {
+private:
+       GDBusConnection *m_pConnection;
+       GCancellable *m_pCancellable;
+public:
+       GDbus();
+       ~GDbus();
+       error_e Create(void);
+       error_e Destroy(void);
+       GDBusConnection *GetConnection(void);
+       GCancellable *GetCancellable(void);
+       error_e ConvertErrorStringToEnum(const char *error);
+       GVariant *InvokeMethod(const char *dest, const char *path,
+               const char *iface_name, const char *method, GVariant *params, error_e *dbus_error);
+       error_e InvokeMethodNonblock(const char *dest, const char *path,
+               const char *iface_name, const char *method, GVariant *params, int timeout,
+               GAsyncReadyCallback notify_func, void *user_data);
+};
+
+#endif /* __NFC_MGR_GDBUS_H__ */
diff --git a/haltests/manager.cpp b/haltests/manager.cpp
new file mode 100644 (file)
index 0000000..e47806a
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2018 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 <stdio.h>
+#include <stdlib.h>
+#include <iostream>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "manager.h"
+
+Manager::Manager()
+{
+       Create();
+}
+
+Manager::~Manager()
+{
+       Destroy();
+}
+
+error_e Manager::SetActivation(bool is_active)
+{
+       GVariant *message = NULL;
+       error_e error = ERROR_NONE;
+       int result = 0;
+
+       message = InvokeMethod(NFC_MGR_SERVICE".Manager",
+               NFC_MGR_PATH,
+               NFC_MANAGER_INTERFACE,
+               NFC_MANAGER_SET_ACTIVE,
+               g_variant_new("(b)", is_active),
+               &error);
+
+       if (message == NULL) {
+               GLOGD("Failed to invoke dbus method");
+               return error;
+       }
+
+       g_variant_get(message, "(i)", &result);
+       GLOGD("Successfully set activation [%d]", result);
+       g_variant_unref(message);
+
+       return ERROR_NONE;
+}
diff --git a/haltests/manager.h b/haltests/manager.h
new file mode 100644 (file)
index 0000000..32544a5
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2018 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.
+ */
+#ifndef __NFC_MGR_MANAGER_H__
+#define __NFC_MGR_MANAGER_H__
+
+#include "nfcmgr.h"
+#include "gdbus.h"
+
+#define WMESHD_STATION_TYPE_MESH_POINT 0
+
+class Manager:public GDbus {
+private:
+public:
+       Manager();
+       ~Manager();
+       error_e SetActivation(bool is_active);
+};
+
+
+#endif /* __NFC_MGR_MANAGER_H__ */
diff --git a/haltests/nfcmgr.cpp b/haltests/nfcmgr.cpp
new file mode 100644 (file)
index 0000000..f02b473
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2018 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 <iostream>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <system_info.h>
+
+#include "manager.h"
+
+using ::testing::InitGoogleTest;
+using ::testing::Test;
+using ::testing::TestCase;
+
+char *feature_name = (char *)"http://tizen.org/feature/network.nfc";
+
+bool CheckFeature()
+{
+       bool nfc_supported = FALSE;
+       if (!system_info_get_platform_bool(feature_name, &nfc_supported)) {
+               if (FALSE == nfc_supported) {
+                       GLOGD("nfc feature is disabled");
+                       return ERROR_NOT_SUPPORTED;
+               }
+               return ERROR_NONE;
+       } else {
+               GLOGD("Error - Feature getting from System Info");
+               return ERROR_INVALID_PARAMETER;
+       }
+}
+
+/*
+@testcase      Set_Activation_true_p
+@since_tizen   5.0
+@author                SRID(abhishek.s94)
+@reviewer      HQ(jh8801.jung)
+@type          auto
+@description   Positive, Activate NFC manager
+@apicovered    "SetActivation" dbus method on org.tizen.NetNfcService.Manager interface
+@passcase      when SetActivation() returns ERROR_NONE
+@failcase      when SetActivation() does not return ERROR_NONE
+@precondition  None
+@postcondition None
+*/
+TEST(Hal_nfc, Set_Activation_true_p)
+{
+       error_e ret = ERROR_NONE;
+       Manager mgr;
+
+       if (ERROR_NONE == CheckFeature()) {
+               ret = mgr.SetActivation(true);
+       }
+       EXPECT_EQ(ERROR_NONE, ret);
+}
+
+/*
+@testcase      Set_Activation_false_p
+@since_tizen   5.0
+@author                SRID(abhishek.s94)
+@reviewer      HQ(jh8801.jung)
+@type          auto
+@description   Positive, Deactivate NFC manager
+@apicovered    "SetActivation" dbus method on org.tizen.NetNfcService.Manager interface
+@passcase      when SetActivation() returns ERROR_NONE
+@failcase      when SetActivation() does not return ERROR_NONE
+@precondition  None
+@postcondition None
+*/
+TEST(Hal_nfc, Set_Activation_false_p)
+{
+       error_e ret = ERROR_NONE;
+       Manager mgr;
+
+       if (ERROR_NONE == CheckFeature()) {
+               ret = mgr.SetActivation(false);
+       }
+       EXPECT_EQ(ERROR_NONE, ret);
+}
+
+int main(int argc, char **argv)
+{
+       int ret = 0;
+       try {
+               testing::InitGoogleTest(&argc, argv);
+       } catch (const ::testing::internal::GoogleTestFailureException& ex) {
+               std::cerr << "Caught: GoogleTestFailureException& " << ex.what() << std::endl;
+       } catch (...) {
+               std::cerr << "Caught: unknown exception" << std::endl;
+       }
+
+       try {
+               ret = RUN_ALL_TESTS();
+       } catch (const ::testing::internal::GoogleTestFailureException& ex) {
+               std::cerr << "Caught: GoogleTestFailureException& " << ex.what() << std::endl;
+       } catch (...) {
+               std::cerr << "Caught: unknown exception" << std::endl;
+       }
+       return ret;
+}
diff --git a/haltests/nfcmgr.h b/haltests/nfcmgr.h
new file mode 100644 (file)
index 0000000..efa0119
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2018 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.
+ */
+#ifndef __NFC_MGR_H__
+#define __NFC_MGR_H__
+
+#include <glib.h>
+
+#ifdef USE_DLOG
+#include <dlog.h>
+#undef LOG_TAG
+#define LOG_TAG "NFC_MGR_GTEST"
+#define GLOGD(format, args...) LOGD(format, ##args)
+#else
+#define GLOGD(format, args...)
+#endif
+
+typedef enum {
+       ERROR_NONE = 0,
+       ERROR_NOT_PERMITTED = -1,
+       ERROR_OUT_OF_MEMORY = -2,
+       ERROR_PERMISSION_DENIED = -3,
+       ERROR_RESOURCE_BUSY = -4,
+       ERROR_INVALID_OPERATION = -5,
+       ERROR_INVALID_PARAMETER = -6,
+       ERROR_NOT_SUPPORTED = -7,
+       ERROR_OPERATION_FAILED = -8,
+       ERROR_NOT_INITIALIZED = -9,
+       ERROR_ALREADY_INITIALIZED = -10,
+       ERROR_IN_PROGRESS = -11,
+} error_e;
+
+
+#endif /* __NFC_MGR_H__ */
index afb92b0844e5b26b15f625d8eec718668f21c1c7..1d06bf4b46c687139e2b02c003cc3f213ea4e845 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       nfc-manager
 Summary:    NFC framework manager
-Version:    0.1.173
+Version:    0.1.174
 Release:    0
 Group:      Network & Connectivity/NFC
 License:    Flora-1.1
@@ -47,6 +47,14 @@ Requires(postun): /sbin/ldconfig
 %description
 A NFC Service Framework for support Tizen NFC.
 
+%package haltests
+Summary: nfc-manager extension for HAL test
+BuildRequires: pkgconfig(gmock)
+BuildRequires: pkgconfig(capi-network-nfc)
+BuildRequires: pkgconfig(capi-system-info)
+Requires: %{name} = %{version}-%{release}
+%description haltests
+TIZEN nfc-manager extension for HAL test.
 
 %prep
 %setup -q
@@ -82,6 +90,7 @@ LDFLAGS="$LDFLAGS" cmake . \
                -DTIZEN_ENGINEER_MODE=1 \
                -DCMAKE_INSTALL_PREFIX=%{_prefix} \
                -DTIZEN_TELEPHONY_ENABLED=1 \
+               -DBIN_DIR=%{_bindir} \
 %ifarch aarch64 x86_64
                -DTIZEN_ARCH_64=1 \
 %endif
@@ -167,3 +176,7 @@ systemctl daemon-reload
 %defattr(-,root,root,-)
 %{_libdir}/pkgconfig/nfc-common-lib.pc
 %{_includedir}/*.h
+
+%files haltests
+%manifest %{name}.manifest
+%{_bindir}/*haltests