Agent updated.
authorLomtev Dmytro <d.lomtev@samsung.com>
Wed, 17 May 2017 08:18:26 +0000 (11:18 +0300)
committerLomtev Dmytro <d.lomtev@samsung.com>
Wed, 17 May 2017 08:18:26 +0000 (11:18 +0300)
16 files changed:
agent/.gitignore
agent/CMakeLists.txt [changed mode: 0755->0644]
agent/inc/tvpolicy.h [new file with mode: 0644]
agent/packaging/agent.spec
agent/scripts/.device-agent [new file with mode: 0644]
agent/scripts/.gitignore [new file with mode: 0644]
agent/scripts/build.sh [new file with mode: 0755]
agent/scripts/coverage.sh [new file with mode: 0755]
agent/scripts/deploy.sh [new file with mode: 0755]
agent/scripts/gbs.conf [new file with mode: 0644]
agent/src/main.cpp
agent/src/tvpolicy.cpp [new file with mode: 0644]
agent/tests/CMakeLists.txt [new file with mode: 0644]
agent/tests/agent-tests.manifest [new file with mode: 0644]
agent/tests/test_all.cpp [new file with mode: 0644]
agent/tests/test_tv_policy.cpp [new file with mode: 0644]

index 9a83867..f4fae38 100644 (file)
@@ -1,5 +1,7 @@
 BUILD
 Debug
+build
+build-gbs
 .cproject
 .project
 
old mode 100755 (executable)
new mode 100644 (file)
index 77c44d7..97bd5a4
@@ -1,15 +1,62 @@
+cmake_minimum_required(VERSION 2.8)
 get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
 string(REPLACE " " "_" ProjectId ${ProjectId})
-project(${ProjectId})
+project(agent)
+
+if (DEFINED DEBUG)
+SET (CMAKE_BUILD_TYPE "Debug")
+SET (CFLAGS " -O0 -g ")
+SET (CXXFLAGS " -O0 -g ")
+SET (CMAKE_CXX_FLAGS ${CXXFLAGS})
+SET (CMAKE_C_FLAGS ${CFLAGS})
+else()
+SET (CMAKE_BUILD_TYPE "Release")
+endif()
+
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
+
+IF("${FLAVOR}" STREQUAL "UBUNTU")
+       SET(INSTALL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/install")
+
+       SET (LIBDIR "${INSTALL_DIR}/usr/lib/")
+       if (NOT DEFINED TESTS_DIR)
+               SET (TESTS_DIR "/usr/apps/agent")
+       endif (NOT DEFINED TESTS_DIR)
+       SET (MANIFESTDIR "${INSTALL_DIR}/usr/share/packages")
+       SET (GTEST_LIB gtest gtest_main)
+
+#      include_directories()
+else()
+
+       SET(FLAVOR "GBS")
+       if (NOT DEFINED LIBDIR)
+               SET (LIBDIR ${LIB_INSTALL_DIR})
+       endif (NOT DEFINED LIBDIR)
+
+       if (NOT DEFINED TESTS_DIR)
+               SET (TESTS_DIR "/usr/apps/agent")
+       endif (NOT DEFINED TESTS_DIR)
+
+#      pkg_check_modules(pkgs REQUIRED iotivity>=1.2.1 boost libcurl dpm dlog)
+
+#      FOREACH(flag ${pkgs_CFLAGS})
+#              SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+#      ENDFOREACH(flag)
+
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -DTB_LOG -D__TIZEN__")
+endif()
 
 include_directories (
-    ${NETWORK_MANAGER_LIB}/dpm/inc
+       inc
 )
 
-add_executable (${PROJECT_NAME} main.cpp)
+FILE(GLOB AGENT_SRCS src/*.cpp)
+
+add_executable(${PROJECT_NAME} ${AGENT_SRCS})
+
+add_subdirectory(tests)
 
-add_dependencies (${PROJECT_NAME} ${NETWORK_MANAGER_LIB_PROJECT_NAME})
-target_link_libraries (${PROJECT_NAME} ${NETWORK_MANAGER_LIB_PROJECT_NAME})
+target_link_libraries (${PROJECT_NAME} jsoncpp)
 
 install(TARGETS ${PROJECT_NAME} DESTINATION ${TESTS_DIR})
 install(FILES agent.manifest DESTINATION ${MANIFESTDIR})
diff --git a/agent/inc/tvpolicy.h b/agent/inc/tvpolicy.h
new file mode 100644 (file)
index 0000000..e2a3eb3
--- /dev/null
@@ -0,0 +1,58 @@
+#ifndef TVPOLICY_H
+#define TVPOLICY_H
+
+#include <string>
+#include <vector>
+#include <jsoncpp/json/writer.h>
+
+enum IPTablesProtocol
+{
+    UDP,
+    TCP
+};
+
+class TvPolicy
+{
+public:
+    const static std::string TV_EXT_GROUP_NAME;
+
+    TvPolicy();
+
+    void setUsbSate(bool on);
+
+    Json::Value getUsbPolicy() const;
+
+    void setScreenCaptureState(bool on);
+
+    Json::Value getScreenCapturePolicy() const;
+
+    void setBluetoothState(bool on);
+
+    Json::Value getBluetoothPolicy() const;
+
+    void setIptablesState(bool on);
+
+    void iptablesAddBlock(const std::string& ip, const IPTablesProtocol proto, const unsigned char ports);
+
+    void iptablesAddBlock(const std::string& ip, const IPTablesProtocol proto, const std::vector<unsigned short>& ports);
+
+    void iptablesAddBlockRange(const std::string& ip, const IPTablesProtocol proto, unsigned short start_port, unsigned short end_port);
+
+    void iptablesClear();
+
+    void iptablesRemoveBlock(const std::string& ip, IPTablesProtocol proto);
+
+    Json::Value getIptablesPolicy() const;
+
+    std::string makePolicy() const;
+private:
+    bool USB_state;
+    bool screen_capture_state;
+    bool bluetooth_state;
+    bool iptables_state;
+    std::vector<std::string> iptables_items;
+
+    static Json::Value boolPolicy(const std::string& name, bool state);
+};
+
+#endif // TVPOLICY_H
index bbfdc12..b5fc3b4 100644 (file)
@@ -1,4 +1,4 @@
-Name:       iotswsec
+Name:       agent
 Version:    1.0.0
 Release:    2
 Summary:    TODO
@@ -7,16 +7,17 @@ License:    Apache
 Source0:    %{name}-%{version}.tar.gz
 
 BuildRequires: cmake
+BuildRequires:  pkgconfig(dlog)
+BuildRequires:  pkgconfig(jsoncpp)
 %if ("%{GTEST_TYPE}" == "mock")
 BuildRequires: pkgconfig(gtest_gmock)
 %else
 BuildRequires: gtest
 BuildRequires: gtest-devel
 %endif
-BuildRequires: pkgconfig(dpm)
 
 
-%define _tests_dir /usr/apps/network-manager
+%define _tests_dir /usr/apps/agent
 %define _manifestdir /usr/share/packages
 %if ("%{GTEST_TYPE}" == "mock")
 %define _gtest_lib gtest_gmock
@@ -28,7 +29,7 @@ BuildRequires: pkgconfig(dpm)
 %endif
 
 %description
-Network manager project
+Reference agent project
 
 %prep
 %setup -q
@@ -40,49 +41,35 @@ export COVFILE=~/%{_covfile}
 cov01 -1
 %endif
 
-cmake . \
-    -DLIB_INSTALL_DIR=%{_libdir} \
-    -DMANIFESTDIR=%{_manifestdir} \
-    -DTESTS_DIR=%{_tests_dir} \
-    -DGTEST_LIB=%{_gtest_lib} \
-    %{?USE_DEBUG: -DDEBUG=ON}
+cmake -H./ -B./build-gbs \
+       -DLIB_INSTALL_DIR=%{_libdir} \
+       -DMANIFESTDIR=%{_manifestdir} \
+       -DTESTS_DIR=%{_tests_dir} \
+       -DGTEST_LIB=%{_gtest_lib} \
+       %{?USE_DEBUG: -DDEBUG=ON}
+cd build-gbs
 make %{?jobs:-j%jobs} VERBOSE=1
 
 %install
+cd build-gbs
 %make_install
 
 %clean
 rm -rf %{buildroot}
 
 ##############################################
-# Network Manager
-##############################################
-
-%package nwmanager
-Summary: Network Manager library
-%description nwmanager
-Network Manager shared library
-
-
-%files
-%manifest %{_manifestdir}/NetworkManager.manifest
-%defattr(-,root,root,-)
-%{_libdir}/libnmlib.so
-
-
-##############################################
 # Unit tests
 ##############################################
 
 %package test
 Summary: Unit tests
-Requires: nmlib
+#Requires: nmlib
 %description test
 Google tests
 
 %files test
-%manifest %{_manifestdir}/tests.manifest
-%attr(0755,root,root) %{_tests_dir}/test
+%manifest %{_manifestdir}/agent-tests.manifest
+%attr(0755,root,root) %{_tests_dir}/tests
 
 ##############################################
 # agent
diff --git a/agent/scripts/.device-agent b/agent/scripts/.device-agent
new file mode 100644 (file)
index 0000000..0cfbf08
--- /dev/null
@@ -0,0 +1 @@
+2
diff --git a/agent/scripts/.gitignore b/agent/scripts/.gitignore
new file mode 100644 (file)
index 0000000..9c5e689
--- /dev/null
@@ -0,0 +1 @@
+.device
\ No newline at end of file
diff --git a/agent/scripts/build.sh b/agent/scripts/build.sh
new file mode 100755 (executable)
index 0000000..93911dc
--- /dev/null
@@ -0,0 +1,86 @@
+#!/bin/bash
+
+SCRIPT_PATH=$(readlink -m ${0})
+SCRIPT_DIR=${SCRIPT_PATH%/*}
+ROOT_DIR=${SCRIPT_DIR}/..
+DEVICE_TEMP=${SCRIPT_DIR}/.device-agent
+
+if [ ! -e "$DEVICE_TEMP" ] ; then
+    DEVICE_PREV=1
+else
+    DEVICE_PREV=`cat $DEVICE_TEMP`
+fi
+
+if [ -z "$1" ]; then
+    DEVICE=$DEVICE_PREV
+else
+    # Clean incremental build after changing $DEVICE
+    if [ $1 -ne $DEVICE_PREV ]; then
+        rm -r ${SCRIPT_DIR}/../build-gbs
+    fi
+    DEVICE=$1
+fi
+
+echo "$DEVICE" > $DEVICE_TEMP
+
+GREEN='\033[0;32m'
+NC='\033[0m'
+
+declare -a DEVICE_ARRAY=(
+"TM2 mobile"
+"TM1 mobile"
+"Kant TV"
+)
+
+arraylength=${#DEVICE_ARRAY[@]}
+for (( i=0; i<${arraylength}; i++ ));
+do
+       if [ $i -eq $DEVICE ]; then
+               echo -e "${GREEN}$i) ${DEVICE_ARRAY[$i]}${NC}"
+       else
+               echo "$i) ${DEVICE_ARRAY[$i]}"
+       fi
+done
+echo -e "\n\n"
+
+
+case $DEVICE in
+       0) PROFILE=profile.tzmb_3.0_TM2 ; TARGET_ARCH=aarch64 ; GTEST_TYPE="main" ;;
+       1) PROFILE=profile.tzmb_3.0_TM1 ; TARGET_ARCH=armv7l ; GTEST_TYPE="main" ;;
+       2) PROFILE=profile.Main2017_KantM ; TARGET_ARCH=armv7l ; GTEST_TYPE="mock" ;;
+       ?) echo "error: unknown DEVICE number"; exit 1 ;;
+esac
+
+
+
+
+for ARG in $*
+do
+    echo $ARG
+    if [ "DEBUG" = $ARG ];then
+        DEBUG_ON=1
+        echo 'DEBUG ON'
+    fi
+done
+
+cd $ROOT_DIR
+
+#init git repo for GBS
+git init
+git add CMakeLists.txt
+git commit -m "commit needed for GBS"
+
+# trap ctrl-c and call ctrl_c()
+trap cleanup INT
+
+function cleanup() {
+    rm -rf ${ROOT_DIR}/.git
+    exit 0
+}
+
+gbs --conf ${SCRIPT_DIR}/gbs.conf build -P ${PROFILE} -A ${TARGET_ARCH} --incremental --include-all --threads 1 \
+       --define '__debug_install_post %{nil}' --define 'debug_package %{nil}' \
+       --define 'DEBUG '$DEBUG_ON --define 'GTEST_TYPE '$GTEST_TYPE
+
+#remove temporary files
+cleanup
diff --git a/agent/scripts/coverage.sh b/agent/scripts/coverage.sh
new file mode 100755 (executable)
index 0000000..fddbce6
--- /dev/null
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+# @author Yaroslav Parhomenko  (y.parhomenko@samsung.com)
+
+echo -e "\e[0;31m Install lcov and set BUILD-MODE-COVERAGE=ON to calculate test coverage \e[0m"
+
+set -e
+
+CURRENT_DIR=$(pwd)
+#BUILD_DIR=~/SIM-GBS-ROOT/local/BUILD-ROOTS/scratch.armv7l.0/home/abuild/rpmbuild/BUILD/sim-0.1.3
+BUILD_DIR=~/GBS_ROOT_3.0/local/BUILD-ROOTS/scratch.armv7l.0/home/abuild/rpmbuild/BUILD/nwmanager-1.0.0
+
+#capture
+lcov --capture --directory $BUILD_DIR --output-file coverage.info
+
+#ignore external libraries
+lcov -r coverage.info /usr/\* --output-file coverage.info
+lcov -r coverage.info \*libs\* --output-file coverage.info
+lcov -r coverage.info \*test\* --output-file coverage.info
+lcov -r coverage.info \*ut\* --output-file coverage.info
+
+#make fancy html
+genhtml coverage.info --output-directory coverage.html
diff --git a/agent/scripts/deploy.sh b/agent/scripts/deploy.sh
new file mode 100755 (executable)
index 0000000..8bb5687
--- /dev/null
@@ -0,0 +1,64 @@
+#!/bin/bash
+
+SCRIPT_PATH=$(readlink -m ${0})
+SCRIPT_DIR=${SCRIPT_PATH%/*}
+DEVICE_TEMP=${SCRIPT_DIR}/.device-agent
+
+if [ -z "$1" ]; then
+       if [ ! -e "$DEVICE_TEMP" ] ; then
+               DEVICE=1
+       else
+               DEVICE=`cat $DEVICE_TEMP`
+       fi
+else
+       DEVICE=$1
+fi
+
+echo "$DEVICE" > $DEVICE_TEMP
+
+GREEN='\033[0;32m'
+NC='\033[0m'
+
+declare -a DEVICE_ARRAY=(
+"TM2 mobile"
+"TM1 mobile"
+"Kant TV"
+)
+
+arraylength=${#DEVICE_ARRAY[@]}
+for (( i=0; i<${arraylength}; i++ ));
+do
+       if [ $i -eq $DEVICE ]; then
+               echo -e "${GREEN}$i) ${DEVICE_ARRAY[$i]}${NC}"
+       else
+               echo "$i) ${DEVICE_ARRAY[$i]}"
+       fi
+done
+echo -e "\n\n"
+
+case $DEVICE in
+       0) BUILD_ROOT=GBS_ROOT_3.0_TM2 ; PROFILE=tzmb_3.0_TM2 ; TARGET_ARCH=aarch64 ;;
+       1) BUILD_ROOT=GBS_ROOT_3.0_TM1 ; PROFILE=tzmb_3.0_TM1 ; TARGET_ARCH=armv7l ;;
+       2) BUILD_ROOT=GBS_ROOT_3.0 ; PROFILE=Main2017_KantM ; TARGET_ARCH=armv7l ;;
+       ?) echo "error: unknown DEVICE number"; exit 1 ;;
+esac
+
+
+
+
+
+
+
+GBS_RPMS_DIR=~/${BUILD_ROOT}/local/repos/${PROFILE}/${TARGET_ARCH}/RPMS
+
+sdb root on
+sdb shell mount -o remount,rw /
+
+RPMS_TO_PUSH="${GBS_RPMS_DIR}/agent-*.rpm"
+sdb shell rm -r /tmp/nm/
+sdb shell mkdir /tmp/nm/
+sdb push ${RPMS_TO_PUSH} /tmp/nm/
+
+sdb shell "rpm -Uvih --nodeps --force --replacefiles /tmp/nm/agent-*.rpm"
+
+sdb shell "/usr/apps/agent/tests"
diff --git a/agent/scripts/gbs.conf b/agent/scripts/gbs.conf
new file mode 100644 (file)
index 0000000..006a7bf
--- /dev/null
@@ -0,0 +1,59 @@
+[general]
+#profile = profile.Main2017_KantM
+profile = profile.tizen_phone_arm64_3_0
+
+[obs.tizen_org]
+url = https://api.tizen.org
+
+[obs.tizentv]
+url = https://168.219.241.169/api
+user = obs_viewer
+passwdx = QlpoOTFBWSZTWRLL1vsAAASLgCEgAACSIJmAIAAxA0DQKZMGnqnmfEjRAP8XckU4UJASy9b7
+
+#############################################z KantM
+
+[repo.base_Main2017]
+url=http://10.103.211.119/tizen-rsa/tizen-3.0-base-main2017/standard/latest/repos/base/armv7l/packages/
+[repo.srk_Main2017]
+url = http://106.125.46.44/repo/kantm/packages/
+[repo.local_Main2017]
+url=~/GBS_ROOT_3.0/local/repos/Main2017/armv7l
+[repo.product_Main2017_KantM]
+url=http://10.103.211.119/releases/tizen-3.0-product-main2017/product/KantM/latest/repos/product/armv7l/packages/
+url=http://10.103.211.119/releases/tizen-3.0-product-main2017/product/KantM/TIZEN-3.0-MAIN2017-KantM-RELEASE_20170419.1/repos/product/armv7l/packages/
+
+
+[profile.Main2017_KantM]
+obs = obs.tizentv
+repos = repo.base_Main2017, repo.srk_Main2017, repo.product_Main2017_KantM, repo.local_Main2017
+buildroot = ~/GBS_ROOT_3.0
+
+############################################# Profile [profile.tzmb_3.0_TM2]
+
+[repo.tzmb_3.0_TM2_base]
+#url = http://download.tizen.org/snapshots/tizen/base/tizen-base_20170421.1/repos/arm64/packages/
+url = http://download.tizen.org/snapshots/tizen/base/latest/repos/arm64/packages/
+[repo.tzmb_3.0_TM2_srk]
+url = http://106.125.46.44/repo/tm2/packages/
+[repo.tzmb_4.0_unified]
+#url = http://download.tizen.org/snapshots/tizen/unified/tizen-unified_20170421.1/repos/standard/packages/
+url = http://download.tizen.org/snapshots/tizen/unified/latest/repos/standard/packages/
+
+[profile.tzmb_3.0_TM2]
+obs = obs.tizen_org
+repos = repo.tzmb_3.0_TM2_base, repo.tzmb_3.0_TM2_srk, repo.tzmb_4.0_unified
+buildroot = ~/GBS_ROOT_3.0_TM2
+
+############################################# Profile [profile.tzmb_3.0_TM1]
+
+[repo.tzmb_3.0_TM1_base]
+url = http://download.tizen.org/snapshots/tizen/base/latest/repos/arm/packages/
+[repo.tzmb_3.0_TM1_srk]
+url = http://106.125.46.44/repo/tm1/packages/
+[repo.tzmb_3.0_TM1]
+url = http://download.tizen.org/snapshots/tizen/mobile/latest/repos/arm-wayland/packages/
+
+[profile.tzmb_3.0_TM1]
+obs = obs.tizen_org
+repos = repo.tzmb_3.0_TM1_base, repo.tzmb_3.0_TM1_srk, repo.tzmb_4.0_unified
+buildroot = ~/GBS_ROOT_3.0_TM1
index cc9139c..a6d2e97 100644 (file)
-#include <unistd.h>
-#include <stdio.h>
-#include <string.h>
-#include <cstdlib>
-#include "dpm.h"
+#include <string>
+#include <vector>
+#include <iostream>
+#include <stdexcept>
+#include <cctype>
+#include "tvpolicy.h"
 
-char s[1024];
+// #include "dpm.h"
 
-DPM *dpm;
-char *id = (char*)"agent";
+using namespace std;
 
-void print_menu(const char *state_s, const char *menu_s)
+enum class State
 {
-       printf("\n=================\n");
-       printf("%s", state_s);
-       printf("Select operation:\n");
-       printf("%s", menu_s);
-       printf(" q - Back\n");
-}
-
-//##################
-int res;
-#define BEGIN(state_s, menu_s) \
-       print_menu(state_s, menu_s); \
-       while(1) \
-       {\
-               res = scanf("%s", s);\
-               if (!strcmp(s, "q")) return;
-
-//##################
-
-#define END            else printf("Wrong input\n");}
-//##################
-
-bool prest[4];
-
-void pack_restrict(int mode, bool on)
-{
-       DPM::package_mode pmod;
-
-       if (mode == 0) pmod = DPM::PACKAGE_INSTALL;
-       if (mode == 1) pmod = DPM::PACKAGE_UNINSTALL;
-       if (mode == 2) pmod = DPM::PACKAGE_REINSTALL;
-       if (mode == 3) pmod = DPM::PACKAGE_MOVE;
-       if (mode == 4) pmod = DPM::PACKAGE_ALL;
-
-       dpm_error err;
-       err = dpm->set_package_restriction(pmod, on);
-
-       if (err == DPM_OK)
-               printf("%s\n", on ? "Restricted" : "Allowed");
-       else
-               printf("Error: %s\n", dpm->get_error_string(err));
-
-       if (mode == 4)
-       {
-               prest[0] = on;
-               prest[1] = on;
-               prest[2] = on;
-               prest[3] = on;
-
-//             if (on)
-//                     printf("All restricted\n");
-//             else
-//                     printf("All allowed\n");
-       }
-       else if (mode >= 0 && mode < 4)
-       {
-               prest[mode] = on;
-
-//             if (mode == 0) printf("Package install ");
-//             if (mode == 1) printf("Package uninstall ");
-//             if (mode == 2) printf("Package reinstall ");
-//             if (mode == 3) printf("Package move ");
-//             printf("%s\n", on ? "restricted" : "allowed");
-       }
-}
-
-void priv_list(const char *name, bool add)
-{
-       dpm_error err;
-
-       if (add)
-               err = dpm->add_privilege_to_blacklist(name);
-       else
-               err = dpm->remove_privilege_from_blacklist(name);
-
-       if (err == DPM_OK)
-       {
-               if (add)
-                       printf("Privilege <%s> added\n", name);
-               else
-                       printf("Privilege <%s> removed\n", name);
-       }
-       else
-               printf("Error: %s\n", dpm->get_error_string(err));
-}
-
-void pack_state(char *st)
-{
-       sprintf(st, " %s: %s\n %s: %s\n %s: %s\n %s: %s\n",
-               "Package install", prest[0] ? "restricted" : "allowed",
-               "Package uninstall", prest[1] ? "restricted" : "allowed",
-               "Package reinstall", prest[2] ? "restricted" : "allowed",
-               "Package move", prest[3] ? "restricted" : "allowed"
-               );
-}
-
-//##################
-
-void menu_package()
-{
-       char st[256];
-
-       pack_state(st);
-
-       BEGIN(st,
-               " 1 - Package install restriction on\n"
-               " 2 - Package install restriction off\n"
-               " 3 - Package uninstall restriction on\n"
-               " 4 - Package uninstall restriction off\n"
-               " 5 - Package reinstall restriction on\n"
-               " 6 - Package reinstall restriction off\n"
-               " 7 - Package move restriction on\n"
-               " 8 - Package move restriction off\n"
-               " 9 - All package restrictions on\n"
-               " 0 - All package restrictions off\n"
-               )
-               else if (!strcmp(s, "1")) pack_restrict(0, true);
-               else if (!strcmp(s, "2")) pack_restrict(0, false);
-               else if (!strcmp(s, "3")) pack_restrict(1, true);
-               else if (!strcmp(s, "4")) pack_restrict(1, false);
-               else if (!strcmp(s, "5")) pack_restrict(2, true);
-               else if (!strcmp(s, "6")) pack_restrict(2, false);
-               else if (!strcmp(s, "7")) pack_restrict(3, true);
-               else if (!strcmp(s, "8")) pack_restrict(3, false);
-               else if (!strcmp(s, "9")) pack_restrict(4, true);
-               else if (!strcmp(s, "0")) pack_restrict(4, false);
-               else printf("Wrong input\n");
-               pack_state(st);
-               printf("State:\n%s\n", st);
-       }
-}
-
-void menu_app()
-{
-       while(1)
-       {
-               BEGIN("",       " 1 - package restrictions menu\n"
-                       " 2 - add privilege to blacklist\n"
-                       " 3 - remove privilege from blacklist\n")
-               else if (!strcmp(s, "1"))
-               {
-                       menu_package();
-                       break;
-               }
-               else if (!strcmp(s, "2"))
-               {
-                       printf("Enter the privilege name: ");
-                       res = scanf("%s", s);
-                       priv_list(s, true);
-               }
-               else if (!strcmp(s, "3"))
-               {
-                       printf("Enter the privilege name: ");
-                       res = scanf("%s", s);
-                       priv_list(s, false);
-               }
-               END;
-       }
-}
-
-//----------------------------------------------
-void dev_bl(const char *mac, bool add)
-{
-       dpm_error err;
-       if (add)
-               err = dpm->bluetooth_add_device_to_blacklist(mac);
-       else
-               err = dpm->bluetooth_remove_device_from_blacklist(mac);
-
-       if (err == DPM_OK)
-               printf("MAC %s %s\n", mac, add ? "added" : "removed");
-       else
-               printf("Error: %s\n", dpm->get_error_string(err));
-}
-
-void uuid_bl(const char *uuid, bool add)
-{
-       dpm_error err;
-       if (add)
-               err = dpm->bluetooth_add_uuid_to_blacklist(uuid);
-       else
-               err = dpm->bluetooth_remove_uuid_from_blacklist(uuid);
-
-       if (err == DPM_OK)
-               printf("UUID %s %s\n", uuid, add ? "added" : "removed");
-       else
-               printf("Error: %s\n", dpm->get_error_string(err));
-}
-
-bool device_restriction;
-bool uuid_restriction;
-
-void menu_bt()
-{
-       char st[256];
-
-       dpm_error err;
-       err = dpm->bluetooth_is_device_restricted(device_restriction);
-       if (err != DPM_OK)
-               printf("bluetooth_is_device_restricted() error: %s\n", dpm->get_error_string(err));
-       err = dpm->bluetooth_is_uuid_restricted(uuid_restriction);
-       if (err != DPM_OK)
-               printf("bluetooth_is_uuid_restricted() error: %s\n", dpm->get_error_string(err));
-
-       sprintf(st, "Bluetooth restrictions:\n %s: %s\n %s: %s\n",
-               "Device restriction", device_restriction ? "on" : "off",
-               "UUID restriction", uuid_restriction ? "on" : "off"
-               );
-
-       BEGIN(st,
-               " 1 - add device to blacklist\n"
-               " 2 - remove device from blacklist\n"
-               " 3 - enable device restriction\n"
-               " 4 - disable device restriction\n"
-               " 5 - add UUID to blacklist\n"
-               " 6 - remove UUID from blacklist\n"
-               " 7 - enable UUID restriction\n"
-               " 8 - disable UUID restriction\n"
-               )
-
-               else if (!strcmp(s, "1"))
-               {
-               printf("Enter MAC address: ");
-               res = scanf("%s", s);
-               dev_bl(s, true);
-               }
-               else if (!strcmp(s, "2"))
-               {
-                       printf("Enter MAC address: ");
-                       res = scanf("%s", s);
-                       dev_bl(s, false);
-               }
-               else if (!strcmp(s, "3"))
-               {
-                       device_restriction = true;
-                       printf("device restriction on\n");
-               }
-               else if (!strcmp(s, "4"))
-               {
-                       device_restriction = false;
-                       printf("device restriction off\n");
-               }
-               else if (!strcmp(s, "5"))
-               {
-                       printf("Enter UUID: ");
-                       res = scanf("%s", s);
-                       dev_bl(s, true);
-               }
-               else if (!strcmp(s, "6"))
-               {
-                       printf("Enter UUID: ");
-                       res = scanf("%s", s);
-                       dev_bl(s, false);
-               }
-               else if (!strcmp(s, "7"))
-               {
-                       uuid_restriction = true;
-                       printf("UUID restriction on\n");
-               }
-               else if (!strcmp(s, "8"))
-               {
-                       uuid_restriction = false;
-                       printf("UUID restriction off\n");
-               }
-       END;
-}
-
-//----------------------------------------------
-bool rst[32]; // restricted
-
-const char* item[] = {
-       "Camera                 ",
-       "Microphone             ",
-       "Location               ",
-       "USB mass storage       ",
-       "Clipboard              ",
-       "Debugging              ",
-       "WiFi                   ",
-       "WiFi hotspot           ",
-       "Bluetooth tethering    ",
-       "USB tethering          ",
-       "Bluetooth mode change  ",
-       "Bluetooth desktop conn ",
-       "Bluetooth pairing      ",
-       "SMS or text messaging  ",
-       "POP or IMAP email      ",
-       "WEB browser            "
+    Main,
+    Show,
+    Usb,
+    ScreenCapture,
+    Bluetooth,
+    Iptables,
+    IptablesAdd,
+    IptablesRemove
 };
 
-void get_rst_state(char *str)
+void applyPolicy(const TvPolicy& /*policy*/)
 {
-       char *p = str;
-       bool allow = false;
-       dpm_error err;
-       int i = 0;
-
-       err = dpm->get_camera_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_microphone_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_location_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_external_storage_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_clipboard_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_usb_debugging_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_wifi_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_wifi_hotspot_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_bluetooth_tethering_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_usb_tethering_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_bluetooth_mode_change_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_bluetooth_desktop_connectivity_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_bluetooth_pairing_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_messaging_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_popimap_email_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-       err = dpm->get_browser_state(allow);
-       if (err == DPM_OK)
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], allow ? "allowed" : "disallowed");
-       else
-               p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], dpm->get_error_string(err));
-       rst[i++] = allow;
-
-//     for(int i = 0; i < 16; i++)
-//             p += sprintf(p, " %i) %s: %s\n", i + 1, item[i], rst[i] ? "disallowed" : "allowed");
 }
 
-void menu_restrict()
+void read_line(istream& is, string& out)
 {
-       static char st[1024];
-       static char items[1024];
-       char *p = items;
-       dpm_error err = DPM_OK;
-
-       get_rst_state(st);
-
-       for(int i = 0; i < 16; i++)
-               p += sprintf(p, " %i - %s\n", i + 1, item[i]);
-
-       BEGIN(st, items)
-               else if (atoi(s) > 0 && atoi(s) <= 16)
-               {
-                       int n = atoi(s);
-                       rst[n - 1] = !rst[n - 1];
-
-                       if (n == 1) err = dpm->set_camera_state(rst[n - 1]);
-                       else if (n == 2) err = dpm->set_microphone_state(rst[n - 1]);
-                       else if (n == 3) err = dpm->set_location_state(rst[n - 1]);
-                       else if (n == 4) err = dpm->set_external_storage_state(rst[n - 1]);
-                       else if (n == 5) err = dpm->set_clipboard_state(rst[n - 1]);
-                       else if (n == 6) err = dpm->set_usb_debugging_state(rst[n - 1]);
-                       else if (n == 7) err = dpm->set_wifi_state(rst[n - 1]);
-                       else if (n == 8) err = dpm->set_wifi_hotspot_state(rst[n - 1]);
-                       else if (n == 9) err = dpm->set_bluetooth_tethering_state(rst[n - 1]);
-                       else if (n == 10) err = dpm->set_usb_tethering_state(rst[n - 1]);
-                       else if (n == 11) err = dpm->set_bluetooth_mode_change_state(rst[n - 1]);
-                       else if (n == 12) err = dpm->set_bluetooth_desktop_connectivity_state(rst[n - 1]);
-                       else if (n == 13) err = dpm->set_bluetooth_pairing_state(rst[n - 1]);
-                       else if (n == 14) err = dpm->set_messaging_state(rst[n - 1]);
-                       else if (n == 15) err = dpm->set_popimap_email_state(rst[n - 1]);
-                       else if (n == 16) err = dpm->set_browser_state(rst[n - 1]);
-
-                       if (err == DPM_OK)
-                               printf(" %s: %s\n", item[n-1], rst[n-1] ? "allowed" : "disallowed");
-                       else
-                               printf(" %s error: %s\n", item[n-1], dpm->get_error_string(err));
-
-                       get_rst_state(st);
-                       printf("State:\n%s", st);
-               }
-
-       END
+    char c;
+    out.clear();
+    is.get(c);
+    if (c != 'r' && c != '\n') out.push_back(c);
+
+    for (;;)
+    {
+        is.get(c);
+        if (c == '\r' || c == '\n') break;
+        out.push_back(c);
+    }
 }
-//----------------------------------------------
 
-void menu_sequrity()
+vector<string> split(const string& str, const char delim)
 {
-       dpm_error err;
-
-       BEGIN("",
-               " 1 - lock the device screen immediately\n"
-               " 2 - encrypt internal storage\n"
-               " 3 - decrypt internal storage\n"
-               " 4 - encrypt external storage\n"
-               " 5 - decrypt external storage\n"
-               " 6 - wipe internal memory\n"
-               " 7 - wipe external memory\n"
-               )
-
-       else if (!strcmp(s, "1"))
-       {
-               err = dpm->lockout_screen();
-               if (err == DPM_OK)
-                       printf("device screen locked\n");
-               else
-                       printf("%s\n", dpm->get_error_string(err));
-       }
-       else if (!strcmp(s, "2"))
-       {
-               err = dpm->set_internal_storage_encryption(true);
-               if (err == DPM_OK)
-                       printf("internal storage encrypted\n");
-               else
-                       printf("%s\n", dpm->get_error_string(err));
-       }
-       else if (!strcmp(s, "3"))
-       {
-               err = dpm->set_internal_storage_encryption(false);
-               if (err == DPM_OK)
-                       printf("internal storage decrypted\n");
-               else
-                       printf("%s\n", dpm->get_error_string(err));
-       }
-       else if (!strcmp(s, "4"))
-       {
-               err = dpm->set_external_storage_encryption(true);
-               if (err == DPM_OK)
-                       printf("external storage encrypted\n");
-               else
-                       printf("%s\n", dpm->get_error_string(err));
-       }
-       else if (!strcmp(s, "5"))
-       {
-               err = dpm->set_external_storage_encryption(false);
-               if (err == DPM_OK)
-                       printf("external storage decrypted\n");
-               else
-                       printf("%s\n", dpm->get_error_string(err));
-       }
-       else if (!strcmp(s, "6"))
-       {
-               err = dpm->wipe_internal_data();
-               if (err == DPM_OK)
-                       printf("internal memory wiped\n");
-               else
-                       printf("%s\n", dpm->get_error_string(err));
-       }
-       else if (!strcmp(s, "7"))
-       {
-               err = dpm->wipe_external_data();
-               if (err == DPM_OK)
-                       printf("external memory wiped\n");
-               else
-                       printf("%s\n", dpm->get_error_string(err));
-       }
-       END;
+    vector<string> v;
+    string::size_type spos = 0;
+    string::size_type epos = string::npos;
+
+    while((epos = str.find(delim, spos)) != string::npos)
+    {
+        v.push_back(str.substr(spos, epos - spos));
+        spos = epos + 1;
+        if (spos >= str.length()) break;
+    }
+
+    if (spos < str.length())
+    {
+        v.push_back(str.substr(spos, epos));
+    }
+
+    return v;
 }
 
-//----------------------------------------------
-
-bool wifi_prof_change;
-bool wifi_network_access;
-
-void menu_wifi()
+string strip(const string& str)
 {
-       static char st[256];
-       dpm_error err;
-
-       err = dpm->wifi_is_profile_change_restricted(wifi_prof_change);
-       if (err != DPM_OK)
-               printf("Profile change error: %s\n", dpm->get_error_string(err));
-       err = dpm->wifi_is_network_access_restricted(wifi_network_access);
-       if (err != DPM_OK)
-               printf("Network access error: %s\n", dpm->get_error_string(err));
-
-       sprintf(st, "WiFi restrictions:\n %s: %s\n %s: %s\n",
-               "Profile change", wifi_prof_change ? "restricted" : "allowed",
-               "Network access", wifi_network_access ? "restricted" : "allowed"
-               );
-
-       BEGIN(st,
-               " 1 - enable profile change restriction\n"
-               " 2 - disable profile change restriction\n"
-               " 3 - enable network access restriction\n"
-               " 4 - disable network access restriction\n"
-               " 5 - add SSID to blacklist\n"
-               " 6 - remove SSID from blacklist\n"
-               )
-
-       else if (!strcmp(s, "1"))
-       {
-               wifi_prof_change = true;
-               err = dpm->wifi_set_profile_change_restriction(wifi_prof_change);
-               if (err != DPM_OK)
-                       printf("Profile change error: %s\n", dpm->get_error_string(err));
-               else
-                       printf("Profile change restriction on\n");
-       }
-       else if (!strcmp(s, "2"))
-       {
-               wifi_prof_change = false;
-               err = dpm->wifi_set_profile_change_restriction(wifi_prof_change);
-               if (err != DPM_OK)
-                       printf("Profile change error: %s\n", dpm->get_error_string(err));
-               else
-                       printf("Profile change restriction off\n");
-       }
-               else if (!strcmp(s, "3"))
-               {
-                       wifi_network_access = true;
-                       err = dpm->wifi_set_network_access_restriction(wifi_network_access);
-                       if (err != DPM_OK)
-                               printf("Network access error: %s\n", dpm->get_error_string(err));
-                       else
-                               printf("Network access restriction on\n");
-               }
-               else if (!strcmp(s, "4"))
-               {
-                       wifi_network_access = false;
-                       err = dpm->wifi_set_network_access_restriction(wifi_network_access);
-                       if (err != DPM_OK)
-                               printf("Network access error: %s\n", dpm->get_error_string(err));
-                       else
-                               printf("Network access restriction off\n");
-               }
-               else if (!strcmp(s, "5"))
-               {
-                       printf("Enter SSID: ");
-                       res = scanf("%s", s);
-                       err = dpm->wifi_add_ssid_to_blocklist(s);
-                       if (err != DPM_OK)
-                               printf("Blacklist error: %s\n", dpm->get_error_string(err));
-                       else
-                               printf("SSID [%s] added\n", s);
-               }
-               else if (!strcmp(s, "6"))
-               {
-                       printf("Enter SSID: ");
-                       res = scanf("%s", s);
-                       err = dpm->wifi_remove_ssid_from_blocklist(s);
-                       if (err != DPM_OK)
-                               printf("Blacklist error: %s\n", dpm->get_error_string(err));
-                       else
-                               printf("SSID [%s] removed\n", s);
-               }
-       END;
-}
+    string s;
+    auto it = str.cbegin();
 
-const char* pname[] = {
-       "camera",
-       "microphone",
-       "location",
-       "usb_mass_storage",
-       "clipboard",
-       "debugging",
-       "wifi",
-       "wifi_hotspot",
-       "bt_tethering",
-       "usb_tethering",
-       "bt_mode_change",
-       "bt_desktop_conn",
-       "bt_pairing",
-       "messaging",
-       "email",
-       "browser",
-
-       "pack_install",
-       "pack_uninstall",
-       "pack_reinstall",
-       "pack_move",
-       "pack_all",
-
-       "add_ptobl", // add_privilege_to_blacklist
-       "remove_pfrombl", // remove_privilege_from_blacklist
-
-       "bt_bl_add_dev", // bluetooth_add_device_to_blacklist
-       "bt_bl_del_dev", // bluetooth_remove_device_from_blacklist
-       "bt_dev_restr", // bluetooth_set_device_restriction
-       "bt_bl_add_uuid", // bluetooth_add_uuid_to_blacklist
-       "bt_bl_del_uuid", // bluetooth_remove_uuid_from_blacklist
-       "bt_uuid_restr", // bluetooth_set_uuid_restriction
-
-       "lockout_screen", // lockout_screen();
-       "internal_storage_encryption", // set_internal_storage_encryption(bool encrypt);
-       "external_storage_encryption", // set_external_storage_encryption(bool encrypt);
-       "wipe_external_data", // wipe_external_data();
-       "wipe_internal_data", // wipe_internal_data();
-
-       "wifi_profile_change_restr", // wifi_set_profile_change_restriction(bool enable);
-       "wifi_network_access_restr", // wifi_set_network_access_restriction(bool enable);
-       "wifi_add_ssid_to_bl", // wifi_add_ssid_to_blocklist(const char* ssid);
-       "wifi_del_ssid_from_bl", // wifi_remove_ssid_from_blocklist(const char* ssid);
+    while(it != str.cend() && isspace(*it)) ++it;
 
-};
+    while(it != str.cend() && !isspace(*it))
+    {
+        s.push_back(*it);
+        ++it;
+    }
 
-void process_policy(FILE *f)
-{
-       dpm_error err = DPM_OK;
-       static char param[1024];
-
-       try
-       {
-               if (fscanf(f, "%s", s) != 1) throw "can't read policy name";
-               if (fscanf(f, "%s", param) != 1) throw "can't read policy parameter";
-
-               bool allow = false;
-               if (!strcmp(param, "on")) allow = true;
-               else if (!strcmp(param, "allow")) allow = true;
-               else if (!strcmp(param, "enable")) allow = true;
-
-               printf("[%s] policy: %s %s (%s)\n", id, s, param, allow ? "true" : "false");
-
-               int c = 0;
-               if (!strcmp(s, pname[c++]))
-                       err = dpm->set_camera_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_microphone_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_location_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_external_storage_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_clipboard_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_usb_debugging_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_wifi_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_wifi_hotspot_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_bluetooth_tethering_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_usb_tethering_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_bluetooth_mode_change_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_bluetooth_desktop_connectivity_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_bluetooth_pairing_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_messaging_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_popimap_email_state(allow);
-               else if (!strcmp(s, pname[c++]))
-                       err = dpm->set_browser_state(allow);
-
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->set_package_restriction(DPM::PACKAGE_INSTALL, allow);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->set_package_restriction(DPM::PACKAGE_UNINSTALL, allow);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->set_package_restriction(DPM::PACKAGE_REINSTALL, allow);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->set_package_restriction(DPM::PACKAGE_MOVE, allow);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->set_package_restriction(DPM::PACKAGE_ALL, allow);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->add_privilege_to_blacklist(param);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->remove_privilege_from_blacklist(param);
-
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->bluetooth_add_device_to_blacklist(param);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->bluetooth_remove_device_from_blacklist(param);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->bluetooth_set_device_restriction(allow);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->bluetooth_add_uuid_to_blacklist(param);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->bluetooth_remove_uuid_from_blacklist(param);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->bluetooth_set_uuid_restriction(allow);
-
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->lockout_screen();
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->set_internal_storage_encryption(allow);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->set_external_storage_encryption(allow);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->wipe_external_data();
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->wipe_internal_data();
-
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->wifi_set_profile_change_restriction(allow);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->wifi_set_network_access_restriction(allow);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->wifi_add_ssid_to_blocklist(param);
-               else if (!strcmp(s, pname[c++]))
-                   err = dpm->wifi_remove_ssid_from_blocklist(param);
-
-               //if (err != DPM_OK) printf("Error: %i\n", (int)err);
-
-       }
-       catch (const char *e)
-       {
-               printf("error: %s\n", e);
-       }
+    return s;
 }
 
 int main(int argc, char* argv[])
 {
-       printf("#############################\n");
-       printf("#### Reference Agent App ####\n");
-       printf("#############################\n");
-
-       if (argc > 1) id = argv[1];
-       printf("AgentID: %s\n", id);
-       dpm = new DPM(id);
-
-       if (argc == 3)
-       {
-               char *fname = argv[2];
-               FILE *f = fopen(fname, "r");
-               if (f)
-               {
-                       while (feof(f) != EOF)
-                       {
-                               if (fscanf(f, "%s", s) != 1) break;
-                               if (s[0] == '/')
-                               {
-                                       // skip comment
-                                       while (feof(f) != EOF)
-                                       {
-                                               if (fgetc(f) == '\n') break;
-                                       }
-                                       continue;
-                               }
-                               else if (!strcmp(s, "-t"))
-                               {
-                                       int t;
-                                       if (fscanf(f, "%i", &t) != 1) break;
-                                       printf("[%s] sleep(%i)\n", id, t);
-                                       usleep(t);
-                               }
-                               else if (!strcmp(s, "-p"))
-                                       process_policy(f);
-                               else
-                                       printf("[%s] error: unknown command %s\n", id, s);
-                       }
-                       fclose(f);
-               }
-               else
-                       printf("[%s] error: can't open file %s\n", id, fname);
-       }
-       else
-       {
-               try
-               {
-                       while (1)
-                       {
-                               printf("\n==============\n");
-                               printf("Select module:\n");
-                               printf(" 1 - Application\n");
-                               printf(" 2 - Bluetooth\n");
-                               printf(" 3 - Restriction\n");
-                               printf(" 4 - Sequrity\n");
-                               printf(" 5 - WiFi\n");
-                               printf(" q - Exit\n");
-
-                               while(1)
-                               {
-                                       res = scanf("%s", s);
-                                       if (!strcmp(s, "q")) throw 0;
-                                       else if (!strcmp(s, "1")) menu_app();
-                                       else if (!strcmp(s, "2")) menu_bt();
-                                       else if (!strcmp(s, "3")) menu_restrict();
-                                       else if (!strcmp(s, "4")) menu_sequrity();
-                                       else if (!strcmp(s, "5")) menu_wifi();
-                                       else
-                                       {
-                                               printf("Wrong input\n");
-                                               continue;
-                                       }
-
-                                       break;
-                               }
-                       }
-               }
-               catch(...)
-               {
-
-               }
-       }
-
-       delete dpm;
+    string id;
+
+    if (argc > 1)
+    {
+        id = argv[1];
+    }
+    else
+    {
+        cout << "AgentID: ";
+        cin >> id;
+    }
+
+    TvPolicy policy;
+
+    try
+    {
+        State state = State::Main;
+
+        bool work = true;
+        while (work)
+        {
+            char option;
+
+            switch (state) {
+            case State::Main:
+                cout << endl << "Select policy option:" << endl;
+                cout << "\t0 - Show policy" << endl;
+                cout << "\t1 - USB" << endl;
+                cout << "\t2 - Screen Capture" << endl;
+                cout << "\t3 - Bluetooth" << endl;
+                cout << "\t4 - IPtables" << endl;
+                cout << "\t------------------\n\tq - exit" << endl;
+
+                cin >> option;
+
+                switch (option)
+                {
+                case '0':
+                    state = State::Show;
+                    break;
+                case '1':
+                    state = State::Usb;
+                    break;
+                case '2':
+                    state = State::ScreenCapture;
+                    break;
+                case '3':
+                    state = State::Bluetooth;
+                    break;
+                case '4':
+                    state = State::Iptables;
+                    break;
+                case 'q':
+                    work = false;
+                    break;
+                default:
+                    cout << "Unsupported option" << endl << endl;
+                }
+
+                break;
+            case State::Show:
+                cout << policy.makePolicy() << endl;
+                state = State::Main;
+                break;
+            case State::Usb:
+                cout << endl << "USB options:" << endl;
+                cout << "----------------------" << endl;
+                cout << policy.getUsbPolicy().toStyledString();
+                cout << "----------------------" << endl;
+                cout << "    1 - USB enable" << endl;
+                cout << "    2 - USB disable" << endl;
+                cout << "    0 - Back" << endl;
+                cout << "    ------------------\n\tq - exit" << endl;
+
+                cin >> option;
+
+                switch (option)
+                {
+                case '1':
+                    policy.setUsbSate(true);
+                    applyPolicy(policy);
+                    break;
+                case '2':
+                    policy.setUsbSate(false);
+                    applyPolicy(policy);
+                    break;
+                case '0':
+                    state = State::Main;
+                    break;
+                case 'q':
+                    work = false;
+                    break;
+                default:
+                    cout << "Unsupported option" << endl << endl;
+                }
+
+                break;
+            case State::ScreenCapture:
+                cout << endl << "Screen Capture options:" << endl;
+                cout << "----------------------" << endl;
+                cout << policy.getScreenCapturePolicy().toStyledString();
+                cout << "----------------------" << endl;
+                cout << "    1 - Screen Capture enable" << endl;
+                cout << "    2 - Screen Capture disable" << endl;
+                cout << "    0 - Back" << endl;
+                cout << "    ------------------\n\tq - exit" << endl;
+
+                cin >> option;
+
+                switch (option)
+                {
+                case '1':
+                    policy.setScreenCaptureState(true);
+                    applyPolicy(policy);
+                    break;
+                case '2':
+                    policy.setScreenCaptureState(false);
+                    applyPolicy(policy);
+                    break;
+                case '0':
+                    state = State::Main;
+                    break;
+                case 'q':
+                    work = false;
+                    break;
+                default:
+                    cout << "Unsupported option" << endl << endl;
+                }
+
+                break;
+            case State::Bluetooth:
+                cout << endl << "Bluetooth options:" << endl;
+                cout << "----------------------" << endl;
+                cout << policy.getBluetoothPolicy().toStyledString();
+                cout << "----------------------" << endl;
+                cout << "    1 - Bluetooth enable" << endl;
+                cout << "    2 - Bluetooth disable" << endl;
+                cout << "    0 - Back" << endl;
+                cout << "    ------------------\n\tq - exit" << endl;
+
+                cin >> option;
+
+                switch (option)
+                {
+                case '1':
+                    policy.setBluetoothState(true);
+                    applyPolicy(policy);
+                    break;
+                case '2':
+                    policy.setBluetoothState(false);
+                    applyPolicy(policy);
+                    break;
+                case '0':
+                    state = State::Main;
+                    break;
+                case 'q':
+                    work = false;
+                    break;
+                default:
+                    cout << "Unsupported option" << endl << endl;
+                }
+
+                break;
+            case State::Iptables:
+                cout << endl << "Iptables options:" << endl;
+                cout << "----------------------" << endl;
+                cout << policy.getIptablesPolicy().toStyledString();
+                cout << "----------------------" << endl;
+                cout << "    1 - Iptables enable" << endl;
+                cout << "    2 - Iptables disable" << endl;
+                cout << "    3 - Iptables add to block list" << endl;
+                cout << "    4 - Iptables remove from blocklist" << endl;
+                cout << "    0 - Back" << endl;
+                cout << "    ------------------\n\tq - exit" << endl;
+
+                cin >> option;
+
+                switch (option)
+                {
+                case '1':
+                    policy.setIptablesState(true);
+                    applyPolicy(policy);
+                    break;
+                case '2':
+                    policy.setIptablesState(false);
+                    applyPolicy(policy);
+                    break;
+                case '3':
+                    state = State::IptablesAdd;
+                    break;
+                case '4':
+                    state = State::IptablesRemove;
+                    break;
+                case '0':
+                    state = State::Main;
+                    break;
+                case 'q':
+                    work = false;
+                    break;
+                default:
+                    cout << "Unsupported option" << endl << endl;
+                }
+                break;
+            case State::IptablesAdd:
+                {
+                    try
+                    {
+                        cout << endl << "Iptables add to block list:" << endl;
+                        cout << "----------------------" << endl;
+                        cout << "    Enter ip address: ";
+                        string ip, ports;
+                        cin >> ip;
+                        unsigned proto;
+                        do
+                        {
+                            cout << "    Enter protocol (1 - UDP, 2 - TCP): ";
+                            cin >> proto;
+                        } while (proto != 1 && proto != 2);
+
+                        cout << "    Enter ports (one value, coma separated list, or range as start-end)" << endl;
+                        cout << "    Examples:" << endl << "      1234" << endl << "      80,8080,8000" << endl << "      22-1000" << endl;
+                        read_line(cin, ports);
+
+                        auto pos = ports.find('-');
+
+                        IPTablesProtocol p = proto == 1 ? IPTablesProtocol::UDP : IPTablesProtocol::TCP;
+
+                        if (pos != string::npos)
+                        {
+                            string start = strip(ports.substr(0, pos));
+                            string end = strip(ports.substr(pos + 1, string::npos));
+                            policy.iptablesAddBlockRange(ip, p, std::stoul(start), std::stoul(end));
+                        }
+                        else
+                        {
+                            auto sports = split(ports, ',');
+
+                            std::vector<unsigned short> vports;
+                            for (auto sport : sports)
+                            {
+                                vports.push_back((unsigned short)std::stoul(strip(sport)));
+                            }
+
+                            policy.iptablesAddBlock(ip, p, vports);
+                            applyPolicy(policy);
+                        }
+                    }
+                    catch (...)
+                    {
+                        cout << "Wrong format" << endl;
+                    }
+
+                }
+                state = State::Iptables;
+                break;
+            case State::IptablesRemove:
+                {
+                    try
+                    {
+                        cout << endl << "Iptables remove from block list:" << endl;
+                        cout << "----------------------" << endl;
+                        cout << "    Enter ip address: ";
+                        string ip, ports;
+                        cin >> ip;
+                        unsigned proto;
+                        do
+                        {
+                            cout << "    Enter protocol (1 - UDP, 2 - TCP): ";
+                            cin >> proto;
+                        } while (proto != 1 && proto != 2);
+
+                        IPTablesProtocol p = proto == 1 ? IPTablesProtocol::UDP : IPTablesProtocol::TCP;
+
+                        policy.iptablesRemoveBlock(ip, p);
+                        applyPolicy(policy);
+                    }
+                    catch (...)
+                    {
+                        cout << "Wrong format" << endl;
+                    }
+
+                }
+                state = State::Iptables;
+                break;
+            }
+        }
+    }
+    catch(std::exception& e)
+    {
+        cerr << "Exception thrown: " << e.what() << endl;
+    }
+
+    cout << "Bye" << endl;
 
        return 0;
 }
diff --git a/agent/src/tvpolicy.cpp b/agent/src/tvpolicy.cpp
new file mode 100644 (file)
index 0000000..a4a58db
--- /dev/null
@@ -0,0 +1,166 @@
+
+#include "tvpolicy.h"
+//#include <strstream>
+#include <sstream>
+#include <algorithm>
+
+
+const std::string TvPolicy::TV_EXT_GROUP_NAME{"tv-extension"};
+
+TvPolicy::TvPolicy():
+    USB_state(true),
+    screen_capture_state(true),
+    bluetooth_state(true),
+    iptables_state(true),
+    iptables_items()
+{
+}
+
+void TvPolicy::setUsbSate(bool on)
+{
+    USB_state = on;
+}
+
+Json::Value TvPolicy::getUsbPolicy() const
+{
+    return boolPolicy("usb", USB_state);
+}
+
+void TvPolicy::setScreenCaptureState(bool on)
+{
+    screen_capture_state = on;
+}
+
+Json::Value TvPolicy::getScreenCapturePolicy() const
+{
+    return boolPolicy("screen-capture", screen_capture_state);
+}
+
+void TvPolicy::setBluetoothState(bool on)
+{
+    bluetooth_state = on;
+}
+
+Json::Value TvPolicy::getBluetoothPolicy() const
+{
+    return boolPolicy("bluetooth", bluetooth_state);
+}
+
+void TvPolicy::setIptablesState(bool on)
+{
+    iptables_state = on;
+    if (!iptables_state) iptables_items.clear();
+}
+
+void TvPolicy::iptablesAddBlock(const std::string& ip, const IPTablesProtocol proto, const unsigned char port)
+{
+    std::ostringstream os;
+    os << ip << '|' << (proto == UDP ? "UDP" : "TCP") << '|' << std::to_string(port);
+
+    iptables_items.push_back(os.str());
+}
+
+void TvPolicy::iptablesAddBlock(const std::string& ip, const IPTablesProtocol proto, const std::vector<unsigned short>& ports)
+{
+    std::ostringstream os;
+    os << ip << '|' << (proto == UDP ? "UDP" : "TCP") << '|';
+
+    bool first = true;
+
+    for (auto port: ports)
+    {
+        if (port > 65535) throw std::logic_error("Port could not be bigger than 65535");
+
+        if (first) first = false;
+        else os << ',';
+        os << std::to_string(port);
+    }
+
+    iptables_items.push_back(os.str());
+}
+
+void TvPolicy::iptablesAddBlockRange(const std::string& ip, const IPTablesProtocol proto, unsigned short start_port, unsigned short end_port)
+{
+    if (start_port > 65535 || end_port > 65535) throw std::logic_error("Port could not be bigger than 65535");
+
+    if (start_port == end_port)
+    {
+        iptablesAddBlock(ip, proto, start_port);
+        return;
+    }
+
+    unsigned start = start_port;
+    unsigned end = end_port;
+
+    if (start_port > end_port)
+    {
+        start = end_port;
+        end = start_port;
+    }
+
+    std::ostringstream os;
+
+    os << ip << '|' << (proto == UDP ? "UDP" : "TCP") << '|' << std::to_string(start) << '-' << std::to_string(end);
+
+    iptables_items.push_back(os.str());
+}
+
+void TvPolicy::iptablesClear()
+{
+    iptables_items.clear();
+}
+
+void TvPolicy::iptablesRemoveBlock(const std::string& ip, IPTablesProtocol proto)
+{
+    std::string filter{ip + (proto == UDP ? "|UDP" : "|TCP")};
+    iptables_items.erase(
+                std::remove_if(
+                    iptables_items.begin(),
+                    iptables_items.end(),
+                    [&filter] (const std::string& val) {
+                        return val.compare(0, filter.length(), filter) == 0;
+                    }),
+                iptables_items.end());
+}
+
+Json::Value TvPolicy::getIptablesPolicy() const
+{
+    Json::Value iptables_policy{boolPolicy("iptables", iptables_state)};
+
+    if (!iptables_items.empty())
+    {
+        Json::Value items;
+
+        for (auto item : iptables_items)
+        {
+            items.append(item);
+        }
+
+        iptables_policy["items"] = items;
+    }
+    return iptables_policy;
+}
+
+std::string TvPolicy::makePolicy() const
+{
+    Json::Value root;
+
+    root["group"] = TV_EXT_GROUP_NAME;
+
+    Json::Value policies;
+    policies.append(getUsbPolicy());
+    policies.append(getScreenCapturePolicy());
+    policies.append(getBluetoothPolicy());
+    policies.append(getIptablesPolicy());
+    root["policies"] = policies;
+    return root.toStyledString();
+}
+
+Json::Value TvPolicy::boolPolicy(const std::string& name, bool state)
+{
+    Json::Value policy;
+    policy["name"] = name;
+    policy["state"] = state ? 1: 0;
+    return policy;
+}
+
diff --git a/agent/tests/CMakeLists.txt b/agent/tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..f6561a0
--- /dev/null
@@ -0,0 +1,24 @@
+get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
+string(REPLACE " " "_" ProjectId ${ProjectId})
+project(${ProjectId})
+
+include_directories (
+       ../inc
+)
+
+FILE(GLOB SRCS *.cpp ../src/tvpolicy.cpp)
+
+add_executable (${PROJECT_NAME} ${SRCS})
+
+target_link_libraries(${PROJECT_NAME}
+    ${GTEST_LIB}
+       ${AGENT_LIB_PROJECT_NAME}
+       jsoncpp
+       pthread
+)
+
+install(TARGETS ${PROJECT_NAME} DESTINATION ${TESTS_DIR})
+install(FILES agent-tests.manifest DESTINATION ${MANIFESTDIR})
+
+message(STATUS "Configuring: " ${ProjectId})
+message(STATUS "CMAKE_CXX_FLAGS: " ${CMAKE_CXX_FLAGS})
diff --git a/agent/tests/agent-tests.manifest b/agent/tests/agent-tests.manifest
new file mode 100644 (file)
index 0000000..8df9558
--- /dev/null
@@ -0,0 +1,20 @@
+<manifest>
+    <define>
+        <domain name="iot-sec-agent-test" />
+               <request>
+            <smack request="System::Use_internet" type="rw"/>
+            <smack request="System::Privileged" type="rwxat"/>
+            <smack request="System::Run" type="rwxat"/>
+        </request>
+        <permit>
+            <smack permit="System::Use_internet" type="rw"/>
+            <smack permit="System::Privileged" type="rwxat"/>
+        </permit>
+    </define>
+    <request>
+               <domain name="iot-sec-agent-test"/>
+    </request>
+       <assign>
+               <filesystem path="/usr/apps/agent/tests" exec_label="User"/>
+       </assign>
+</manifest>
diff --git a/agent/tests/test_all.cpp b/agent/tests/test_all.cpp
new file mode 100644 (file)
index 0000000..5cee3cb
--- /dev/null
@@ -0,0 +1,9 @@
+#include <iostream>
+
+#include <gtest/gtest.h>
+
+int main(int argc, char** argv)
+{
+    ::testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}
diff --git a/agent/tests/test_tv_policy.cpp b/agent/tests/test_tv_policy.cpp
new file mode 100644 (file)
index 0000000..1826329
--- /dev/null
@@ -0,0 +1,189 @@
+/**
+ * @brief  tests for TvPolicy class
+ * @date   Created 17.05.2017
+ * @author Created 2017 in Samsung Ukraine R&D Center (SURC) under a contract
+ *         between LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine)
+ *         and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea).
+ *         Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+ * @author Mail to: <A HREF="mailto:d.lomtev@samsung.com">Dmytro Lomtev, d.lomtev@samsung.com</A>
+ */
+
+#include <iostream>
+#include <gtest/gtest.h>
+#include <string>
+#include <vector>
+#include "tvpolicy.h"
+#include <jsoncpp/json/reader.h>
+#include <algorithm>
+
+using namespace std;
+
+namespace
+{
+const std::string group_name{"tv-extension"};
+const std::string screen_capture_policy{"screen-capture"};
+const std::string usb_policy{"usb"};
+const std::string bluetooth_policy{"bluetooth"};
+const std::string iptables_policy{"iptables"};
+}
+
+void checkPolicy(const TvPolicy& policy,
+                 bool usb_state,
+                 bool screen_capture_state,
+                 bool bluetooth_state,
+                 bool iptables_state,
+                 const vector<string>& iptables_items)
+{
+    try
+    {
+        Json::Value root;
+        Json::Reader reader;
+        bool usb_present = false, screen_present = false, bt_present = false, ipt_present = false;
+
+        ASSERT_TRUE(reader.parse(policy.makePolicy(), root)) << "Failed to parse JSON policy";
+
+        ASSERT_EQ(group_name, root["group"].asString());
+        Json::Value policies = root["policies"];
+        ASSERT_FALSE(policies.isNull()) << "No policies found";
+        for (int i = 0; i < policies.size(); i++)
+        {
+            Json::Value policy = policies[i];
+
+            if (policy["name"].asString() == usb_policy)
+            {
+                ASSERT_EQ(usb_state, policy["state"].asBool());
+                usb_present = true;
+            }
+            else if (policy["name"].asString() == screen_capture_policy)
+            {
+                ASSERT_EQ(screen_capture_state, policy["state"].asBool());
+                screen_present = true;
+            }
+            else if (policy["name"].asString() == bluetooth_policy)
+            {
+                ASSERT_EQ(bluetooth_state, policy["state"].asBool());
+                bt_present = true;
+            }
+            else if (policy["name"].asString() == iptables_policy)
+            {
+                ASSERT_EQ(iptables_state, policy["state"].asBool());
+                ipt_present = true;
+
+                Json::Value items = policy["items"];
+
+                if (items.isNull() && iptables_items.size() != 0)
+                {
+                    FAIL() << "Policy has no items but must have " << iptables_items.size();
+                }
+                else
+                {
+                    ASSERT_EQ(items.size(), iptables_items.size());
+
+                    vector<string> result{iptables_items};
+                    for (int i = 0; i < items.size(); i++)
+                    {
+                        string v = items[i].asString();
+                        auto it = find(result.begin(), result.end(), v);
+                        ASSERT_NE(it, result.end()) << "Value \"" << v << "\" not found";
+
+                        result.erase(it);
+                    }
+
+                    ASSERT_EQ(0, result.size()) << "Not all items found";
+                }
+            }
+        }
+
+        ASSERT_TRUE(usb_present) << "USB policy not found";
+        ASSERT_TRUE(screen_present) << "Screen capture policy not found";
+        ASSERT_TRUE(bt_present) << "Bluetooth policy not found";
+        ASSERT_TRUE(ipt_present) << "IPTables policy not found";
+    }
+    catch(std::exception& e)
+    {
+        FAIL() << "Exception: " << e.what();
+    }
+    catch(...)
+    {
+        FAIL() << "Unknown exception";
+    }
+
+}
+
+/**
+ * Test check TvPolicy API
+ * 1. Create TvPolicy
+ * 2. set all policies to off state
+ * 3. check
+ */
+TEST(test_TvPolicy, test_all_off)
+{
+    TvPolicy policy;
+    policy.setBluetoothState(false);
+    policy.setUsbSate(false);
+    policy.setIptablesState(false);
+    policy.setScreenCaptureState(false);
+
+    checkPolicy(policy, false, false, false, false, vector<string>{});
+}
+
+/**
+ * Test check TvPolicy API
+ * 1. Create TvPolicy
+ * 2. set all policies to on state
+ * 3. check
+ */
+TEST(test_TvPolicy, test_all_on)
+{
+    TvPolicy policy;
+    policy.setBluetoothState(true);
+    policy.setUsbSate(true);
+    policy.setIptablesState(true);
+    policy.setScreenCaptureState(true);
+
+    checkPolicy(policy, true, true, true, true, vector<string>{});
+}
+
+/**
+ * Test check TvPolicy IPTables methods
+ * 1. Create TvPolicy
+ * 2. set all policies to on state
+ * 3. add records to iptables
+ * 4. check
+ * 5. add another one record
+ * 6. check
+ * 7. remove record
+ * 8. check
+ */
+TEST(test_TvPolicy, test_ip_tables_add_remove)
+{
+    TvPolicy policy;
+    policy.setBluetoothState(true);
+    policy.setUsbSate(true);
+    policy.setIptablesState(true);
+    policy.setScreenCaptureState(true);
+
+    // Add records
+    policy.iptablesAddBlock("106.125.33.55", IPTablesProtocol::TCP, vector<unsigned short>{10,20,30,40,50,60,70});
+    policy.iptablesAddBlockRange("106.125.12.34", IPTablesProtocol::UDP, 100, 1111);
+
+    vector<string> result{
+        "106.125.33.55|TCP|10,20,30,40,50,60,70",
+        "106.125.12.34|UDP|100-1111"
+    };
+    // Check
+    checkPolicy(policy, true, true, true, true, result);
+
+    // Add another record and check
+    policy.iptablesAddBlock("106.125.10.51", IPTablesProtocol::TCP, 80);
+    result.push_back("106.125.10.51|TCP|80");
+    checkPolicy(policy, true, true, true, true, result);
+
+    result = {
+            "106.125.33.55|TCP|10,20,30,40,50,60,70",
+            "106.125.10.51|TCP|80"
+        };
+    // Remove record and check
+    policy.iptablesRemoveBlock("106.125.12.34", IPTablesProtocol::UDP);
+    checkPolicy(policy, true, true, true, true, result);
+}