gmock
dlog
)
-
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
INCLUDE(FindPkgConfig)
PKG_CHECK_MODULES(GTEST_TEST_PKG REQUIRED ${REQUIRES_LIST})
--- /dev/null
+/*
+ * 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 "common.h"
+
+GMainLoop *MainLoop::m_mainLoop = NULL;
+guint MainLoop::m_timerId = 0;
+
+MainLoop::MainLoop(void)
+{
+}
+
+MainLoop::~MainLoop(void)
+{
+ if (m_mainLoop)
+ g_main_loop_quit(m_mainLoop);
+ if (m_timerId)
+ g_source_remove(m_timerId);
+
+ m_mainLoop = NULL;
+ m_timerId = 0;
+}
+
+gboolean MainLoop::timeoutCb(gpointer data)
+{
+ if (m_mainLoop)
+ g_main_loop_quit(m_mainLoop);
+ if (m_timerId)
+ g_source_remove(m_timerId);
+
+ m_mainLoop = NULL;
+ m_timerId = 0;
+
+ return false;
+}
+
+void MainLoop::run(unsigned int timeout)
+{
+ if (m_timerId > 0)
+ return;
+
+ m_mainLoop = g_main_loop_new(NULL, false);
+ m_timerId = g_timeout_add(timeout,
+ (GSourceFunc) &MainLoop::timeoutCb,
+ NULL);
+ g_main_loop_run(m_mainLoop);
+}
+
+void MainLoop::quit(void)
+{
+ timeoutCb(NULL);
+}
+
--- /dev/null
+/*
+ * 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 <glib.h>
+class MainLoop {
+private:
+ static GMainLoop *m_mainLoop;
+ static guint m_timerId;
+
+ static gboolean timeoutCb(gpointer data);
+public:
+ MainLoop(void);
+ ~MainLoop(void);
+ void run(unsigned int timeout);
+ void quit(void);
+};
--- /dev/null
+/*
+ * Copyright (c) 2017 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 "firewall.h"
+#include "common.h"
+
+static void AsyncReadyCallback(GObject *source_object,
+ GAsyncResult *res, gpointer user_data)
+{
+ MainLoop *M = (MainLoop *)user_data;
+ GLOGD("Succeeded to response async callback");
+ M->quit();
+}
+
+FRule::FRule()
+{
+ this->chain[0] = '\0';
+ this->direction = 0;
+ this->siptype = 0;
+ this->diptype = 0;
+ this->sporttype = 0;
+ this->dporttype = 0;
+ this->protocol = 0;
+ this->family = 0;
+ this->sip1[0] = '\0';
+ this->dip1[0] = '\0';
+ this->sip2[0] = '\0';
+ this->dip2[0] = '\0';
+ this->dport1 = 0;
+ this->dport2 = 0;
+ this->sport1 = 0;
+ this->sport2 = 0;
+ this->iface[0] = '\0';
+ this->target = 0;
+}
+
+FRule::~FRule()
+{
+}
+
+Firewall::Firewall()
+{
+ Create();
+}
+
+Firewall::~Firewall()
+{
+ Destroy();
+}
+
+error_e Firewall::SetRule(const char *chain, guint16 direction,
+ guint16 siptype, guint16 diptype, guint16 sporttype,
+ guint16 dporttype, guint16 protocol, guint16 family,
+ const char *sip1, const char *dip1, const char *sip2,
+ const char *dip2, guint32 dport1, guint32 dport2,
+ guint32 sport1, guint32 sport2, const char *iface,
+ guint16 target)
+{
+ if (chain == NULL || strlen(chain) == 0)
+ this->m_Rule.chain[0] = '\0';
+ else
+ g_strlcpy(this->m_Rule.chain, chain, CHAIN_LEN);
+
+ if (sip1 == NULL || strlen(sip1) == 0)
+ this->m_Rule.sip1[0] = '\0';
+ else
+ g_strlcpy(this->m_Rule.sip1, sip1, IP_LEN);
+
+ if (sip2 == NULL || strlen(sip2) == 0)
+ this->m_Rule.sip2[0] = '\0';
+ else
+ g_strlcpy(this->m_Rule.sip2, sip2, IP_LEN);
+
+ if (dip1 == NULL || strlen(dip1) == 0)
+ this->m_Rule.dip1[0] = '\0';
+ else
+ g_strlcpy(this->m_Rule.dip1, dip1, IP_LEN);
+
+ if (dip2 == NULL || strlen(dip2) == 0)
+ this->m_Rule.dip2[0] = '\0';
+ else
+ g_strlcpy(this->m_Rule.dip2, dip2, IP_LEN);
+
+ if (iface == NULL || strlen(iface) == 0)
+ this->m_Rule.iface[0] = '\0';
+ else
+ g_strlcpy(this->m_Rule.iface, iface, IFACE_LEN);
+
+
+ this->m_Rule.direction = direction;
+ this->m_Rule.siptype = siptype;
+ this->m_Rule.diptype = diptype;
+ this->m_Rule.sporttype = sporttype;
+ this->m_Rule.dporttype = dporttype;
+ this->m_Rule.protocol = protocol;
+ this->m_Rule.family = family;
+ this->m_Rule.dport1 = dport1;
+ this->m_Rule.dport2 = dport2;
+ this->m_Rule.sport1 = sport1;
+ this->m_Rule.sport2 = sport2;
+ this->m_Rule.target = target;
+
+ return ERROR_NONE;
+}
+
+void Firewall::MakeRuleParams(GVariant **params)
+{
+ GVariantBuilder *builder;
+
+ builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_CHAIN,
+ g_variant_new_string(this->m_Rule.chain));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_DIRECTION,
+ g_variant_new_uint16(this->m_Rule.direction));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_IFNAME,
+ g_variant_new_string(this->m_Rule.iface));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_PROTOCOL,
+ g_variant_new_uint16(this->m_Rule.protocol));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_TARGET,
+ g_variant_new_uint16(this->m_Rule.target));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_FAMILY,
+ g_variant_new_uint16(this->m_Rule.family));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_SIPTYPE,
+ g_variant_new_uint16(this->m_Rule.siptype));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_SIP1,
+ g_variant_new_string(this->m_Rule.sip1));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_SIP2,
+ g_variant_new_string(this->m_Rule.sip2));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_DIPTYPE,
+ g_variant_new_uint16(this->m_Rule.diptype));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_DIP1,
+ g_variant_new_string(this->m_Rule.dip1));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_DIP2,
+ g_variant_new_string(this->m_Rule.dip2));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_SPORTTYPE,
+ g_variant_new_uint16(this->m_Rule.sporttype));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_SPORT1,
+ g_variant_new_uint32(this->m_Rule.sport1));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_SPORT2,
+ g_variant_new_uint32(this->m_Rule.sport2));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_DPORTTYPE,
+ g_variant_new_uint16(this->m_Rule.dporttype));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_DPORT1,
+ g_variant_new_uint32(this->m_Rule.dport1));
+
+ g_variant_builder_add(builder, "{sv}",
+ FIREWALL_RULE_DPORT2,
+ g_variant_new_uint32(this->m_Rule.dport2));
+
+ *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
+ g_variant_builder_unref(builder);
+}
+
+error_e Firewall::GetAllRule(void)
+{
+ MainLoop M;
+ error_e error = ERROR_NONE;
+
+ error = InvokeMethodNonblock(STC_MGR_SERVICE,
+ STC_MGR_FIREWALL_PATH,
+ STC_MGR_FIREWALL_INTERFACE,
+ STC_MGR_METHOD_FIREWALL_GET_ALL,
+ NULL,
+ DBUS_REPLY_TIMEOUT,
+ AsyncReadyCallback,
+ &M);
+
+ if (error != ERROR_NONE) {
+ GLOGD("Failed to invoke dbus method nonblock");
+ return error;
+ }
+
+ GLOGD("Succeeded to get all restriction");
+
+ M.run(GMAINTIMEOUT);
+ return ERROR_NONE;
+}
+
+error_e Firewall::UpdateRule(void)
+{
+ GVariant *message = NULL;
+ GVariant *params = NULL;
+ error_e error = ERROR_NONE;
+ int result = 0;
+
+ MakeRuleParams(¶ms);
+
+ message = InvokeMethod(STC_MGR_SERVICE,
+ STC_MGR_FIREWALL_PATH,
+ STC_MGR_FIREWALL_INTERFACE,
+ STC_MGR_METHOD_FIREWALL_UPDATE,
+ params,
+ &error);
+
+ if (message == NULL) {
+ GLOGD("Failed to invoke dbus method");
+ return error;
+ }
+
+ g_variant_get(message, "(i)", &result);
+ GLOGD("Succeeded to set restriction [%d]", result);
+ g_variant_unref(message);
+
+ return ERROR_NONE;
+}
+
+error_e Firewall::RemoveRule(void)
+{
+ GVariant *message = NULL;
+ GVariant *params = NULL;
+ error_e error = ERROR_NONE;
+ int result = 0;
+
+ MakeRuleParams(¶ms);
+
+ message = InvokeMethod(STC_MGR_SERVICE,
+ STC_MGR_FIREWALL_PATH,
+ STC_MGR_FIREWALL_INTERFACE,
+ STC_MGR_METHOD_FIREWALL_REMOVE,
+ params,
+ &error);
+
+ if (message == NULL) {
+ GLOGD("Failed to invoke dbus method");
+ return error;
+ }
+
+ g_variant_get(message, "(i)", &result);
+ GLOGD("Succeeded to set restriction [%d]", result);
+ g_variant_unref(message);
+
+ return ERROR_NONE;
+}
+
+error_e Firewall::AddRule(void)
+{
+ GVariant *message = NULL;
+ GVariant *params = NULL;
+ error_e error = ERROR_NONE;
+ int result = 0;
+
+ MakeRuleParams(¶ms);
+
+ message = InvokeMethod(STC_MGR_SERVICE,
+ STC_MGR_FIREWALL_PATH,
+ STC_MGR_FIREWALL_INTERFACE,
+ STC_MGR_METHOD_FIREWALL_ADD,
+ params,
+ &error);
+
+ if (message == NULL) {
+ GLOGD("Failed to invoke dbus method");
+ return error;
+ }
+
+ g_variant_get(message, "(i)", &result);
+ GLOGD("Succeeded to set restriction [%d]", result);
+ g_variant_unref(message);
+
+ return ERROR_NONE;
+}
+
+error_e Firewall::SetChain(const char *chain, unsigned int target)
+{
+ GVariant *message = NULL;
+ error_e error = ERROR_NONE;
+ int result = 0;
+
+ message = InvokeMethod(STC_MGR_SERVICE,
+ STC_MGR_FIREWALL_PATH,
+ STC_MGR_FIREWALL_INTERFACE,
+ STC_MGR_METHOD_FIREWALL_SET,
+ g_variant_new("(su)", chain, target),
+ &error);
+
+ if (message == NULL) {
+ GLOGD("Failed to invoke dbus method");
+ return error;
+ }
+
+ g_variant_get(message, "(i)", &result);
+ GLOGD("Succeeded to set restriction [%d]", result);
+ g_variant_unref(message);
+
+ return ERROR_NONE;
+}
+
+error_e Firewall::UnsetChain(const char *chain)
+{
+ GVariant *message = NULL;
+ error_e error = ERROR_NONE;
+ int result = 0;
+
+ message = InvokeMethod(STC_MGR_SERVICE,
+ STC_MGR_FIREWALL_PATH,
+ STC_MGR_FIREWALL_INTERFACE,
+ STC_MGR_METHOD_FIREWALL_UNSET,
+ g_variant_new("(s)", chain),
+ &error);
+
+ if (message == NULL) {
+ GLOGD("Failed to invoke dbus method");
+ return error;
+ }
+
+ g_variant_get(message, "(i)", &result);
+ GLOGD("Succeeded to set restriction [%d]", result);
+ g_variant_unref(message);
+
+ return ERROR_NONE;
+}
+
+error_e Firewall::FlushChain(const char *chain)
+{
+ GVariant *message = NULL;
+ error_e error = ERROR_NONE;
+ int result = 0;
+
+ message = InvokeMethod(STC_MGR_SERVICE,
+ STC_MGR_FIREWALL_PATH,
+ STC_MGR_FIREWALL_INTERFACE,
+ STC_MGR_METHOD_FIREWALL_FLUSH,
+ g_variant_new("(s)", chain),
+ &error);
+
+ if (message == NULL) {
+ GLOGD("Failed to invoke dbus method");
+ return error;
+ }
+
+ g_variant_get(message, "(i)", &result);
+ GLOGD("Succeeded to set restriction [%d]", result);
+ g_variant_unref(message);
+
+ return ERROR_NONE;
+}
+
+error_e Firewall::GetAllChain(void)
+{
+ MainLoop M;
+ error_e error = ERROR_NONE;
+
+ error = InvokeMethodNonblock(STC_MGR_SERVICE,
+ STC_MGR_FIREWALL_PATH,
+ STC_MGR_FIREWALL_INTERFACE,
+ STC_MGR_METHOD_FIREWALL_GET_CHAIN,
+ NULL,
+ DBUS_REPLY_TIMEOUT,
+ AsyncReadyCallback,
+ &M);
+
+ if (error != ERROR_NONE) {
+ GLOGD("Failed to invoke dbus method nonblock");
+ return error;
+ }
+
+ GLOGD("Succeeded to get all restriction");
+
+ M.run(GMAINTIMEOUT);
+ return ERROR_NONE;
+}
+
+error_e Firewall::RemoveChain(const char *chain)
+{
+ GVariant *message = NULL;
+ error_e error = ERROR_NONE;
+ int result = 0;
+
+ message = InvokeMethod(STC_MGR_SERVICE,
+ STC_MGR_FIREWALL_PATH,
+ STC_MGR_FIREWALL_INTERFACE,
+ STC_MGR_METHOD_FIREWALL_REMOVE_CHAIN,
+ g_variant_new("(s)", chain),
+ &error);
+
+ if (message == NULL) {
+ GLOGD("Failed to invoke dbus method");
+ return error;
+ }
+
+ g_variant_get(message, "(i)", &result);
+ GLOGD("Succeeded to set restriction [%d]", result);
+ g_variant_unref(message);
+
+ return ERROR_NONE;
+}
+
+error_e Firewall::AddChain(const char *chain)
+{
+ GVariant *message = NULL;
+ error_e error = ERROR_NONE;
+ int result = 0;
+
+ message = InvokeMethod(STC_MGR_SERVICE,
+ STC_MGR_FIREWALL_PATH,
+ STC_MGR_FIREWALL_INTERFACE,
+ STC_MGR_METHOD_FIREWALL_ADD_CHAIN,
+ g_variant_new("(s)", chain),
+ &error);
+
+ if (message == NULL) {
+ GLOGD("Failed to invoke dbus method");
+ return error;
+ }
+
+ g_variant_get(message, "(i)", &result);
+ GLOGD("Succeeded to set restriction [%d]", result);
+ g_variant_unref(message);
+
+ return ERROR_NONE;
+}
+
+error_e Firewall::GetLock(int *state)
+{
+ GVariant *message = NULL;
+ error_e error = ERROR_NONE;
+
+ message = InvokeMethod(STC_MGR_SERVICE,
+ STC_MGR_FIREWALL_PATH,
+ STC_MGR_FIREWALL_INTERFACE,
+ STC_MGR_METHOD_FIREWALL_GET_LOCK,
+ NULL,
+ &error);
+
+ if (message == NULL) {
+ GLOGD("Failed to invoke dbus method");
+ return error;
+ }
+
+ g_variant_get(message, "(i)", &state);
+ GLOGD("Succeeded to get lock state[%d]", state);
+ g_variant_unref(message);
+
+ return ERROR_NONE;
+}
+
+error_e Firewall::Unlock()
+{
+ GVariant *message = NULL;
+ error_e error = ERROR_NONE;
+ int result = 0;
+
+ message = InvokeMethod(STC_MGR_SERVICE,
+ STC_MGR_FIREWALL_PATH,
+ STC_MGR_FIREWALL_INTERFACE,
+ STC_MGR_METHOD_FIREWALL_UNLOCK,
+ NULL,
+ &error);
+
+ if (message == NULL) {
+ GLOGD("Failed to invoke dbus method");
+ return error;
+ }
+
+ g_variant_get(message, "(i)", &result);
+ GLOGD("Succeeded to unlock result[%d]", result);
+ g_variant_unref(message);
+
+ return ERROR_NONE;
+}
+
+error_e Firewall::Lock()
+{
+ GVariant *message = NULL;
+ error_e error = ERROR_NONE;
+ int result = 0;
+
+ message = InvokeMethod(STC_MGR_SERVICE,
+ STC_MGR_FIREWALL_PATH,
+ STC_MGR_FIREWALL_INTERFACE,
+ STC_MGR_METHOD_FIREWALL_LOCK,
+ NULL,
+ &error);
+
+ if (message == NULL) {
+ GLOGD("Failed to invoke dbus method");
+ return error;
+ }
+
+ g_variant_get(message, "(i)", &result);
+ GLOGD("Succeeded to unlock result[%d]", result);
+ g_variant_unref(message);
+
+ return ERROR_NONE;
+}
+
--- /dev/null
+/*
+ * Copyright (c) 2017 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 __STC_MGR_FIREWALL_H__
+#define __STC_MGR_FIREWALL_H__
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include "stcmgr.h"
+#include "gdbus.h"
+
+#define FIREWALL_RULE_CHAIN "chain"
+#define FIREWALL_RULE_DIRECTION "direction"
+#define FIREWALL_RULE_IFNAME "ifname"
+#define FIREWALL_RULE_PROTOCOL "protocol"
+#define FIREWALL_RULE_TARGET "target"
+#define FIREWALL_RULE_TARGETTYPE "target_type"
+
+#define FIREWALL_RULE_FAMILY "family"
+#define FIREWALL_RULE_SIPTYPE "s_ip_type"
+#define FIREWALL_RULE_SIP1 "s_ip1"
+#define FIREWALL_RULE_SIP2 "s_ip2"
+#define FIREWALL_RULE_DIPTYPE "d_ip_type"
+#define FIREWALL_RULE_DIP1 "d_ip1"
+#define FIREWALL_RULE_DIP2 "d_ip2"
+#define FIREWALL_RULE_SPORTTYPE "s_port_type"
+#define FIREWALL_RULE_SPORT1 "s_port1"
+#define FIREWALL_RULE_SPORT2 "s_port2"
+#define FIREWALL_RULE_DPORTTYPE "d_port_type"
+#define FIREWALL_RULE_DPORT1 "d_port1"
+#define FIREWALL_RULE_DPORT2 "d_port2"
+
+#define FIREWALL_RULE_LOG_LEVEL "log_level"
+#define FIREWALL_RULE_LOG_PREFIX "log_prefix"
+#define FIREWALL_RULE_NFLOG_GROUP "nflog_group"
+#define FIREWALL_RULE_NFLOG_PREFIX "nflog_prefix"
+#define FIREWALL_RULE_NFLOG_RANGE "nflog_range"
+#define FIREWALL_RULE_NFLOG_THRESHOLD "nflog_threshold"
+
+#define CHAIN_LEN 256
+#define IFACE_LEN 256
+#define IP_LEN 16
+
+class FRule {
+private:
+public:
+ char chain[CHAIN_LEN];
+ guint16 direction;
+ guint16 siptype;
+ guint16 diptype;
+ guint16 sporttype;
+ guint16 dporttype;
+ guint16 protocol;
+ guint16 family;
+ char sip1[IP_LEN];
+ char dip1[IP_LEN];
+ char sip2[IP_LEN];
+ char dip2[IP_LEN];
+ guint32 dport1;
+ guint32 dport2;
+ guint32 sport1;
+ guint32 sport2;
+ char iface[IFACE_LEN];
+ guint16 target;
+
+ FRule();
+ ~FRule();
+};
+
+class Firewall : public GDbus {
+private:
+ FRule m_Rule;
+public:
+ Firewall();
+ ~Firewall();
+ error_e SetRule(const char *app_id, const char *iface_name,
+ const char *subscriber_id, iface_type_e iface_type,
+ int64_t data_limit, int64_t data_warn_limit,
+ roaming_type_e roaming_type, const char *mac);
+ void MakeRuleParams(GVariant **params);
+ void MakeGetParams(GVariant **params);
+ void MakeStatusParams(GVariant **params);
+ error_e Lock(void);
+ error_e Unlock(void);
+ error_e GetLock(int *state);
+ error_e AddChain(const char * chain);
+ error_e RemoveChain(const char * chain);
+ error_e FlushChain(const char * chain);
+ error_e GetAllChain();
+ error_e SetChain(const char *chain, unsigned int target);
+ error_e UnsetChain(const char *chain);
+ error_e SetRule(const char *chain, guint16 direction,
+ guint16 siptype, guint16 diptype, guint16 sporttype,
+ guint16 dporttype, guint16 protocol, guint16 family,
+ const char *sip1, const char *dip1, const char *sip2,
+ const char *dip2, guint32 dport1, guint32 dport2,
+ guint32 sport1, guint32 sport2, const char *iface,
+ guint16 target);
+ error_e AddRule();
+ error_e RemoveRule();
+ error_e UpdateRule();
+ error_e GetAllRule();
+};
+
+#endif /* __STC_MGR_FIREWALL_H__ */
#define STC_MGR_STATISTICS_INTERFACE STC_MGR_SERVICE ".statistics"
#define STC_MGR_RESTRICTION_INTERFACE STC_MGR_SERVICE ".restriction"
#define STC_MGR_MANAGER_INTERFACE STC_MGR_SERVICE ".manager"
+#define STC_MGR_FIREWALL_INTERFACE STC_MGR_SERVICE ".firewall"
#define STC_MGR_STATISTICS_PATH "/net/stc/statistics"
#define STC_MGR_RESTRICTION_PATH "/net/stc/restriction"
#define STC_MGR_MANAGER_PATH "/net/stc/manager"
+#define STC_MGR_FIREWALL_PATH "/net/stc/firewall"
#define STC_MGR_METHOD_STATISTICS_INIT "Init"
#define STC_MGR_METHOD_STATISTICS_GET_PER_APP_ID "Get"
#define STC_MGR_METHOD_MANAGER_STOP "Stop"
+#define STC_MGR_METHOD_FIREWALL_GET_ALL "GetAllRule"
+#define STC_MGR_METHOD_FIREWALL_UPDATE "UpdateRule"
+#define STC_MGR_METHOD_FIREWALL_REMOVE "RemoveRule"
+#define STC_MGR_METHOD_FIREWALL_ADD "AddRule"
+#define STC_MGR_METHOD_FIREWALL_SET "SetChain"
+#define STC_MGR_METHOD_FIREWALL_UNSET "UnsetChain"
+#define STC_MGR_METHOD_FIREWALL_FLUSH "FlushChain"
+#define STC_MGR_METHOD_FIREWALL_GET_CHAIN "GetAllChain"
+#define STC_MGR_METHOD_FIREWALL_REMOVE_CHAIN "RemoveChain"
+#define STC_MGR_METHOD_FIREWALL_ADD_CHAIN "AddChain"
+#define STC_MGR_METHOD_FIREWALL_GET_LOCK "GetLock"
+#define STC_MGR_METHOD_FIREWALL_LOCK "Lock"
+#define STC_MGR_METHOD_FIREWALL_UNLOCK "Unlock"
+
class GDbus {
private:
GDBusConnection *m_pConnection;
#include <gtest/gtest.h>
#include "restriction.h"
-
-static GMainLoop *g_pMainLoop = NULL;
-static guint g_nTimeoutId = 0;
-
-static gboolean CallbackTimeut(gpointer data)
-{
- if (g_pMainLoop)
- g_main_loop_quit(g_pMainLoop);
-
- return false;
-}
-
-static void RunMainLoop(void)
-{
- g_pMainLoop = g_main_loop_new(NULL, false);
- g_nTimeoutId = g_timeout_add(GMAINTIMEOUT, CallbackTimeut, g_pMainLoop);
- g_main_loop_run(g_pMainLoop);
-
- g_source_remove(g_nTimeoutId);
- g_pMainLoop = NULL;
-}
-
-static void QuitMainLoop(void)
-{
- if (g_pMainLoop)
- g_main_loop_quit(g_pMainLoop);
-}
+#include "common.h"
static void AsyncReadyCallback(GObject *source_object,
GAsyncResult *res, gpointer user_data)
{
+ MainLoop *M = (MainLoop *)user_data;
GLOGD("Succeeded to response async callback");
- QuitMainLoop();
+ M->quit();
}
RRule::RRule()
error_e Restriction::SetRule(const char *app_id, const char *iface_name,
const char *subscriber_id, iface_type_e iface_type,
int64_t data_limit, int64_t data_warn_limit,
- roaming_type_e roaming_type)
+ roaming_type_e roaming_type, const char *mac)
{
if (app_id == NULL || strlen(app_id) == 0)
this->m_Rule.m_AppID[0] = '\0';
else
g_strlcpy(this->m_Rule.m_SubscriberID, subscriber_id, SUBSCRIBER_ID_LEN);
+ if (mac == NULL || strlen(mac) == 0)
+ this->m_Rule.m_Mac[0] = '\0';
+ else
+ g_strlcpy(this->m_Rule.m_Mac, mac, MAC_LEN);
+
switch(iface_type) {
case IFACE_UNKNOWN:
case IFACE_DATACALL:
RESTRICTION_RULE_DATA_WARN_LIMIT,
g_variant_new_int64(this->m_Rule.m_DataWarnLimit));
+ g_variant_builder_add(builder, "{sv}",
+ RESTRICTION_RULE_MAC,
+ g_variant_new_string(this->m_Rule.m_Mac));
+
+
*params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
g_variant_builder_unref(builder);
}
error_e Restriction::GetRstriction(void)
{
+ MainLoop M;
GVariant *params = NULL;
error_e error = ERROR_NONE;
params,
DBUS_REPLY_TIMEOUT,
AsyncReadyCallback,
- NULL);
+ &M);
if (error != ERROR_NONE) {
GLOGD("Failed to invoke dbus method nonblock");
GLOGD("Succeeded to get restriction per app ID");
- RunMainLoop();
+ M.run(GMAINTIMEOUT);
return ERROR_NONE;
}
error_e Restriction::GetAllRstriction(void)
{
+ MainLoop M;
error_e error = ERROR_NONE;
error = InvokeMethodNonblock(STC_MGR_SERVICE,
NULL,
DBUS_REPLY_TIMEOUT,
AsyncReadyCallback,
- NULL);
+ &M);
if (error != ERROR_NONE) {
GLOGD("Failed to invoke dbus method nonblock");
GLOGD("Succeeded to get all restriction");
- RunMainLoop();
+ M.run(GMAINTIMEOUT);
return ERROR_NONE;
-}
\ No newline at end of file
+}
#define RESTRICTION_RULE_SUBSCRIBER_ID "subscriber_id"
#define RESTRICTION_RULE_DATA_LIMIT "data_limit"
#define RESTRICTION_RULE_DATA_WARN_LIMIT "data_warn_limit"
+#define RESTRICTION_RULE_MAC "mac"
typedef enum {
RESTRICTION_UNKNOWN,
char m_AppID[APP_ID_LEN];
char m_IfaceName[IFNAME_LEN];
char m_SubscriberID[SUBSCRIBER_ID_LEN];
+ char m_Mac[MAC_LEN];
iface_type_e m_IfaceType;
int64_t m_DataLimit;
int64_t m_DataWarnLimit;
error_e SetRule(const char *app_id, const char *iface_name,
const char *subscriber_id, iface_type_e iface_type,
int64_t data_limit, int64_t data_warn_limit,
- roaming_type_e roaming_type);
+ roaming_type_e roaming_type, const char *mac);
void MakeRuleParams(GVariant **params);
void MakeGetParams(GVariant **params);
void MakeStatusParams(GVariant **params);
#include <gtest/gtest.h>
#include "statistics.h"
-
-static GMainLoop *g_pMainLoop = NULL;
-static guint g_nTimeoutId = 0;
-
-static gboolean CallbackTimeut(gpointer data)
-{
- if (g_pMainLoop)
- g_main_loop_quit(g_pMainLoop);
-
- return false;
-}
-
-static void RunMainLoop(void)
-{
- g_pMainLoop = g_main_loop_new(NULL, false);
- g_nTimeoutId = g_timeout_add(GMAINTIMEOUT, CallbackTimeut, g_pMainLoop);
- g_main_loop_run(g_pMainLoop);
-
- g_source_remove(g_nTimeoutId);
- g_pMainLoop = NULL;
-}
-
-static void QuitMainLoop(void)
-{
- if (g_pMainLoop)
- g_main_loop_quit(g_pMainLoop);
-}
+#include "common.h"
static void AsyncReadyCallback(GObject *source_object,
GAsyncResult *res, gpointer user_data)
{
+ MainLoop *M = (MainLoop *)user_data;
GLOGD("Succeeded to response async callback");
- QuitMainLoop();
+ M->quit();
}
SRule::SRule()
error_e Statistics::GetStatistics(void)
{
+ MainLoop M;
GVariant *params = NULL;
error_e error = ERROR_NONE;
params,
DBUS_REPLY_TIMEOUT,
AsyncReadyCallback,
- NULL);
+ &M);
if (error != ERROR_NONE) {
GLOGD("Failed to invoke dbus method nonblock");
return error;
}
- RunMainLoop();
+ M.run(GMAINTIMEOUT);
GLOGD("Succeeded to get statistics per app ID");
return ERROR_NONE;
error_e Statistics::GetTotalStatistics(void)
{
+ MainLoop M;
GVariant *params = NULL;
error_e error = ERROR_NONE;
params,
DBUS_REPLY_TIMEOUT,
AsyncReadyCallback,
- NULL);
+ &M);
if (error != ERROR_NONE) {
GLOGD("Failed to invoke dbus method nonblock");
return error;
}
- RunMainLoop();
+ M.run(GMAINTIMEOUT);
GLOGD("Succeeded to get total statistics");
return ERROR_NONE;
error_e Statistics::GetAllStatistics(void)
{
+ MainLoop M;
GVariant *params = NULL;
error_e error = ERROR_NONE;
params,
DBUS_REPLY_TIMEOUT,
AsyncReadyCallback,
- NULL);
+ &M);
if (error != ERROR_NONE) {
GLOGD("Failed to invoke dbus method nonblock");
return error;
}
- RunMainLoop();
+ M.run(GMAINTIMEOUT);
GLOGD("Succeeded to get all statistics");
return ERROR_NONE;
#define APP_ID_LEN 128
#define IFNAME_LEN 16
#define SUBSCRIBER_ID_LEN 128
+#define MAC_LEN 18
#ifdef USE_DLOG
#include <dlog.h>
#include "restriction.h"
#include "statistics.h"
#include "manager.h"
+#include "stc-manager.h"
+#include "firewall.h"
+
+#define GTEST_MAC "1:c:e:b:00:da"
using ::testing::InitGoogleTest;
using ::testing::Test;
using ::testing::TestCase;
+typedef enum {
+ FIREWALL_UNKONWN,
+ FIREWALL_UNLOCKED,
+ FIREWALL_LOCKED
+} firewall_lock_e;
+
TEST(StcManager_Statistics, Init_p)
{
error_e ret = ERROR_NONE;
IFACE_DATACALL,
0,
0,
- ROAMING_UNKNOWN);
+ ROAMING_UNKNOWN,
+ GTEST_MAC);
EXPECT_EQ(ERROR_NONE, ret);
ret = rest.SetRstriction();
IFACE_WIFI,
2,
1,
- ROAMING_UNKNOWN);
+ ROAMING_UNKNOWN,
+ GTEST_MAC);
EXPECT_EQ(ERROR_NONE, ret);
ret = rest.SetRstriction();
IFACE_WIRED,
100000,
80000,
- ROAMING_UNKNOWN);
+ ROAMING_UNKNOWN,
+ GTEST_MAC);
EXPECT_EQ(ERROR_NONE, ret);
ret = rest.SetRstriction();
EXPECT_EQ(ERROR_NONE, ret);
}
-TEST(StcManager_Restriction, Exclude_p)
-
-{
- error_e ret = ERROR_NONE;
- Restriction rest;
-
- ret = rest.SetRule("TOTAL_DATACALL",
- "seth_w0",
- NULL,
- IFACE_DATACALL,
- 0,
- 0,
- ROAMING_UNKNOWN);
- EXPECT_EQ(ERROR_NONE, ret);
-
- ret = rest.ExcludeRstriction();
- EXPECT_EQ(ERROR_NONE, ret);
-}
-
TEST(StcManager_Restriction, Get_p)
{
error_e ret = ERROR_NONE;
IFACE_DATACALL,
0,
0,
- ROAMING_UNKNOWN);
+ ROAMING_UNKNOWN,
+ GTEST_MAC);
EXPECT_EQ(ERROR_NONE, ret);
ret = rest.GetRstriction();
IFACE_WIFI,
0,
0,
- ROAMING_UNKNOWN);
+ ROAMING_UNKNOWN,
+ GTEST_MAC);
EXPECT_EQ(ERROR_NONE, ret);
ret = rest.GetRstriction();
IFACE_DATACALL,
0,
0,
- ROAMING_UNKNOWN);
+ ROAMING_UNKNOWN,
+ GTEST_MAC);
EXPECT_EQ(ERROR_NONE, ret);
ret = rest.GetRstriction();
EXPECT_EQ(ERROR_NONE, ret);
}
-TEST(StcManager_Restriction, GetRst_p)
-{
- error_e ret = ERROR_NONE;
- Restriction rest;
- restriction_status_e status = RESTRICTION_UNKNOWN;
-
- ret = rest.SetRule("Test_tel",
- "seth_w0",
- NULL,
- IFACE_DATACALL,
- 0,
- 0,
- ROAMING_UNKNOWN);
- EXPECT_EQ(ERROR_NONE, ret);
-
- ret = rest.GetRstrictionStatus(&status);
- EXPECT_EQ(ERROR_NONE, ret);
- EXPECT_LE(RESTRICTION_UNKNOWN, status);
- EXPECT_GE(RESTRICTION_LAST_ELEM, status);
-
- ret = rest.SetRule("Test_eth",
- "eth0",
- NULL,
- IFACE_WIRED,
- 100000,
- 80000,
- ROAMING_UNKNOWN);
- EXPECT_EQ(ERROR_NONE, ret);
-
- ret = rest.GetRstrictionStatus(&status);
- EXPECT_EQ(ERROR_NONE, ret);
- EXPECT_LE(RESTRICTION_UNKNOWN, status);
- EXPECT_GE(RESTRICTION_LAST_ELEM, status);
-}
TEST(StcManager_Restriction, Unset_p)
{
IFACE_DATACALL,
0,
0,
- ROAMING_UNKNOWN);
+ ROAMING_UNKNOWN,
+ GTEST_MAC);
EXPECT_EQ(ERROR_NONE, ret);
ret = rest.UnsetRstriction();
IFACE_WIFI,
2,
1,
- ROAMING_UNKNOWN);
+ ROAMING_UNKNOWN,
+ GTEST_MAC);
EXPECT_EQ(ERROR_NONE, ret);
ret = rest.UnsetRstriction();
IFACE_WIRED,
100000,
80000,
- ROAMING_UNKNOWN);
+ ROAMING_UNKNOWN,
+ GTEST_MAC);
EXPECT_EQ(ERROR_NONE, ret);
ret = rest.UnsetRstriction();
IFACE_DATACALL,
0,
0,
- ROAMING_UNKNOWN);
+ ROAMING_UNKNOWN,
+ GTEST_MAC);
EXPECT_EQ(ERROR_NONE, ret);
ret = rest.UnsetRstriction();
EXPECT_EQ(ERROR_NONE, ret);
}
+TEST(StcManager_Firewall, Lock_p)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ int state;
+ f.GetLock(&state);
+ if (state == FIREWALL_LOCKED)
+ f.Unlock();
+
+ ret = f.Lock();
+ EXPECT_EQ(ERROR_NONE, ret);
+ f.Unlock();
+}
+
+TEST(StcManager_Firewall, Unlock_p)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ int state;
+ f.GetLock(&state);
+ if (state != FIREWALL_LOCKED)
+ f.Lock();
+
+ ret = f.Unlock();
+ EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, GetLock_p)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ int state;
+ f.GetLock(&state);
+ EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, AddChain_p)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.AddChain("gtest");
+ EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, AddRule_p)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.SetRule("gtest",
+ STC_FW_DIRECTION_IN,
+ STC_FW_IP_SINGLE,
+ STC_FW_IP_SINGLE,
+ STC_FW_PORT_SINGLE,
+ STC_FW_PORT_SINGLE,
+ STC_FW_PROTOCOL_TCP,
+ STC_FW_FAMILY_V4,
+ "1.1.1.1",
+ "1.1.1.1",
+ "2.2.2.2",
+ "2.2.2.2",
+ 9000,
+ 9000,
+ 9000,
+ 9000,
+ "wlan0",
+ STC_FW_RULE_TARGET_ACCEPT);
+
+ EXPECT_EQ(ERROR_NONE, ret);
+
+ ret = f.AddRule();
+ EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, AddRule_n)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.SetRule("gtest",
+ STC_FW_DIRECTION_IN,
+ STC_FW_IP_SINGLE,
+ STC_FW_IP_SINGLE,
+ STC_FW_PORT_SINGLE,
+ STC_FW_PORT_SINGLE,
+ STC_FW_PROTOCOL_TCP,
+ STC_FW_FAMILY_V4,
+ "1.1.1.1",
+ "1.1.1.1",
+ "2.2.2.2",
+ "2.2.2.2",
+ 9000,
+ 9000,
+ 9000,
+ 9000,
+ "wlan0",
+ STC_FW_RULE_TARGET_MAX);
+ EXPECT_EQ(ERROR_NONE, ret);
+
+ ret = f.AddRule();
+ EXPECT_NE(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, UpdateRule_n)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.SetRule("gtest",
+ STC_FW_DIRECTION_IN,
+ STC_FW_IP_SINGLE,
+ STC_FW_IP_SINGLE,
+ STC_FW_PORT_SINGLE,
+ STC_FW_PORT_SINGLE,
+ STC_FW_PROTOCOL_TCP,
+ STC_FW_FAMILY_V4,
+ "1.1.1.1",
+ "1.1.1.1",
+ "2.2.2.2",
+ "2.2.2.2",
+ 9000,
+ 9000,
+ 9000,
+ 9000,
+ "wlan0",
+ STC_FW_RULE_TARGET_MAX);
+ EXPECT_EQ(ERROR_NONE, ret);
+
+ ret = f.UpdateRule();
+ EXPECT_NE(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, RemoveRule_n)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.SetRule("gtest",
+ STC_FW_DIRECTION_IN,
+ STC_FW_IP_SINGLE,
+ STC_FW_IP_SINGLE,
+ STC_FW_PORT_SINGLE,
+ STC_FW_PORT_SINGLE,
+ STC_FW_PROTOCOL_TCP,
+ STC_FW_FAMILY_V4,
+ "1.1.1.1",
+ "1.1.1.1",
+ "2.2.2.2",
+ "2.2.2.2",
+ 9000,
+ 9000,
+ 9000,
+ 9000,
+ "wlan0",
+ STC_FW_RULE_TARGET_MAX);
+ EXPECT_EQ(ERROR_NONE, ret);
+
+ ret = f.RemoveRule();
+ EXPECT_NE(ERROR_NONE, ret);
+}
+
+
+TEST(StcManager_Firewall, SetChain_p)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.SetChain("gtest", 1);
+ EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, SetChain_n)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.SetChain(NULL, 0);
+ EXPECT_NE(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, UnsetChain_p)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.UnsetChain("gtest");
+ EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, UnsetChain_n)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.UnsetChain(NULL);
+ EXPECT_NE(ERROR_NONE, ret);
+}
+
+
+TEST(StcManager_Firewall, RemoveChain_p)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.RemoveChain("gtest");
+ EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, RemoveChain_n)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.RemoveChain("not_a_chain");
+ EXPECT_NE(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, FlushChain_p)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.AddChain("gtest_chains");
+ EXPECT_EQ(ERROR_NONE, ret);
+
+ ret = f.FlushChain("gtest_chains");
+ EXPECT_EQ(ERROR_NONE, ret);
+
+ f.RemoveChain("gtest_chains");
+}
+
+
+TEST(StcManager_Firewall, FlushChain_n)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.FlushChain(NULL);
+ EXPECT_NE(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, GetAllChain_p)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.GetAllChain();
+ EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, GetAllRule_p)
+{
+ error_e ret = ERROR_NONE;
+ Firewall f;
+
+ ret = f.GetAllRule();
+ EXPECT_EQ(ERROR_NONE, ret);
+}
+
TEST(StcManager_Manager, Stop_p)
{
error_e ret = ERROR_NONE;