--- /dev/null
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics 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.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+package com.samsung.ci.basic.api;
+
+import io.restassured.RestAssured;
+import io.restassured.http.ContentType;
+import io.restassured.response.Response;
+import java.util.Date;
+import static io.restassured.RestAssured.given;
+
+/**
+ * The type Reports.
+ *
+ * @author Mail to: <A HREF="mailto:m.dalakov@samsung.com">Mikhail Dalakov</A>
+ * @version 1.0
+ * @file Reports
+ * @brief Base class for Reports
+ * @date Created : 10/3/2017
+ * @date Modified : 10/3/2017
+ * @copyright In Samsung Ukraine R&D Center (SRK under a contract between)
+ * @par LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+ * @par Copyright : (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+ */
+public final class Reports {
+ static {
+ RestAssured.baseURI = AppConfigInit.getEnv().getRequiredProperty("dsmURI");
+ }
+
+ private Reports() {
+ }
+
+ /**
+ * Find reports by result response.
+ *
+ * @param result the result
+ * @return response
+ */
+ public static Response findReportsByResult(Integer result) {
+ Response resp = given().
+ params("result", result).
+ accept(ContentType.JSON).
+ when().
+ get(RestAssured.baseURI + "/dsm/restapi/report/findReportsByResult/");
+ resp.then().log().all();
+ return resp;
+ }
+
+ /**
+ * Find reports by type response.
+ *
+ * @param type the type
+ * @return the response
+ */
+ public static Response findReportsByType(String type) {
+ Response resp = given().
+ params("type", type).
+ accept(ContentType.JSON).
+ when().
+ get(RestAssured.baseURI + "/dsm/restapi/report/findReportsByType/");
+ resp.then().log().all();
+ return resp;
+ }
+
+ /**
+ * Find reports by date between response.
+ *
+ * @param dateFrom the date from
+ * @param dateTo the date to
+ * @return the response
+ */
+ public static Response findReportsByDateBetween(Date dateFrom, Date dateTo) {
+ Response resp = given().
+ params("from", dateFrom).
+ params("to", dateTo).
+ accept(ContentType.JSON).
+ when().
+ get(RestAssured.baseURI + "/dsm/restapi/report/findReportsByDateBetween/");
+ resp.then().log().all();
+ return resp;
+ }
+
+}
--- /dev/null
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics 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.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+package com.samsung.ci.test.reports;
+
+import com.google.gson.Gson;
+import com.samsung.ci.basic.api.MQPublisherAPI;
+import com.samsung.ci.basic.api.Reports;
+import com.samsung.ci.basic.utils.Utils;
+import com.samsung.ci.test.useractions.BasicUserAction;
+import io.restassured.response.Response;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Map;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+
+/**
+ * The type TIAReports.
+ *
+ * @author Mail to: <A HREF="mailto:m.dalakov@samsung.com">Mikhail Dalakov</A>
+ * @version 1.0
+ * @file TIAReports
+ * @brief Base class for Reports
+ * @date Created : 10/3/2017
+ * @date Modified : 10/3/2017
+ * @copyright In Samsung Ukraine R&D Center (SRK under a contract between)
+ * @par LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+ * @par Copyright : (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+ */
+
+public class TIAReports extends BasicUserAction {
+
+ private static final String DEVICE_DUID = "TIA-0000000-1111-2222-3333-4444444444444";
+ private static final String DEVICE_UNREGISTER_ACTION = "unregister";
+
+ private static final String SMACK = "smack";
+ private static final String DAC = "dac";
+ private static final String SYSCALL = "syscall";
+ private static final String PAD = "pad";
+ private static final String DLP = "dlp";
+ private static final String SMACK_REPORT = "smackReport.json";
+ private static final String DAC_REPORT = "dacReport.json";
+ private static final String SYSCALL_REPORT = "syscallReport.json";
+ private static final String PAD_REPORT = "padReport.json";
+ private static final String DLP_REPORT = "dlpReport.json";
+ private static final String SMACK_REPORT_EXPECTED = "TIAsmackReportExpected.json";
+ private static final String DAC_REPORT_EXPECTED = "TIAdacReportExpected.json";
+ private static final String SYSCALL_REPORT_EXPECTED = "TIAsyscallReportExpected.json";
+ private static final String PAD_REPORT_EXPECTED = "TIApadReportExpected.json";
+ private static final String DLP_REPORT_EXPECTED = "TIAdlpReportExpected.json";
+
+ private static final String DEVICE_NAME = "TV";
+ private static final String DEVICE_TYPE = "tv";
+ private static final String DEVICE_MODEL = "standard";
+ private static final String DEVICE_PARENT_DUID = "TIA-55555555-6666-7777-8888-999999999999";
+
+ /**
+ * Info.
+ */
+ @BeforeAll
+ public static void info() {
+ //@formatter:off
+ log("Positive case for TIA reports:\n" +
+ "1. Create new user \n" +
+ "2. Create IoTCloud User\n" +
+ "3. Register device\n" +
+ "4. Save reports for all types\n" +
+ "5. Check findReportsByDateBetween\n" +
+ "6. checkfindReportsByResult\n" +
+ "7. checkfindReportsByType\n" +
+ "8. Unregister device\n" +
+ "9. Delete IoTCloud User\n" +
+ "10. Login as admin;\n" +
+ "11. Delete user if admin\n" +
+ "12. Logout\n");
+ //@formatter:on
+ }
+
+ private String readReport(String reportName) throws IOException {
+ return Utils.readAllFile(reportName).replace("\n", "").replace("\r", "").replace("\t", "").replace("\r", "");
+ }
+
+ private void registerDevice(String uuid) {
+ log("Device registration");
+ assertTrue(MQPublisherAPI.deviceRegistrationInDSM(DEVICE_DUID, uuid, DEVICE_NAME, DEVICE_TYPE, DEVICE_MODEL, DEVICE_PARENT_DUID));
+ }
+
+
+ private void checkReportSize(Response resp, int size) {
+ String reports = resp.getBody().asString();
+ assertNotNull(reports);
+ Gson gson = new Gson();
+ @SuppressWarnings("unchecked")
+ Map<Integer, Map<String, String>> map = (Map<Integer, Map<String, String>>) gson.fromJson(reports, Map.class);
+ assertTrue(map.size() == size);
+ }
+
+ private void checkfindReportsByDateBetweenSize(String DateFrom, String DateTo, int size) {
+ try {
+ String datePattern = "MM-dd-yyyy";
+ SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
+ Date df = sdf.parse(DateFrom);
+ Date dt = sdf.parse(DateTo);
+ Response resp = Reports.findReportsByDateBetween(df,dt);
+ checkReportSize(resp, size);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void checkfindReportsByDateBetween() {
+ log("checkfindReportsByDateBetween");
+ final int size1 = 3;
+ checkfindReportsByDateBetweenSize("05-08-2017","05-09-2017",size1);
+ final int size2 = 2;
+ checkfindReportsByDateBetweenSize("10-09-2017","10-10-2017",size2);
+ final int size3 = 5;
+ checkfindReportsByDateBetweenSize("05-08-2017","10-10-2017",size3);
+ }
+
+ private void checkfindReportsByResult() {
+ log("checkfindReportsByResult");
+ Response resp = Reports.findReportsByResult(0);
+ final int size = 5;
+ checkReportSize(resp, size);
+ }
+
+ private boolean checkEqualsJSON(String json1, String json2) {
+ boolean res = false;
+ try {
+ ObjectMapper om = new ObjectMapper();
+ @SuppressWarnings("unchecked")
+ Map<String, Object> map1 = (Map<String, Object>)(om.readValue(json1, Map.class));
+ @SuppressWarnings("unchecked")
+ Map<String, Object> map2 = (Map<String, Object>)(om.readValue(json2, Map.class));
+ res = map1.equals(map2);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return res;
+ }
+
+ private void assertReportEquals(String current, String expected) {
+ try {
+ Response resp = Reports.findReportsByType(current);
+ String json1 = resp.getBody().asString();
+ String json2 = readReport(expected);
+ assertTrue(checkEqualsJSON(json1, json2));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void checkfindReportsByType() {
+ log("checkfindReportsByType");
+ assertReportEquals(SMACK, SMACK_REPORT_EXPECTED);
+ assertReportEquals(DAC, DAC_REPORT_EXPECTED);
+ assertReportEquals(SYSCALL, SYSCALL_REPORT_EXPECTED);
+ assertReportEquals(PAD, PAD_REPORT_EXPECTED);
+ assertReportEquals(DLP, DLP_REPORT_EXPECTED);
+ }
+
+ public void testTIAReports() throws InterruptedException, IOException {
+ log("Create new user for TIA get report scenario");
+ createNewUser();
+ String uuid = "TIA-test-uuid";
+ createIoTUser(USER_EMAIL, uuid);
+
+ registerDevice(uuid);
+ Thread.sleep(TIMEOUT);
+
+ // SMACK
+ String report = readReport(SMACK_REPORT);
+ assertTrue(MQPublisherAPI.sendReportToDSM(report, DEVICE_DUID));
+ // DAC
+ report = readReport(DAC_REPORT);
+ assertTrue(MQPublisherAPI.sendReportToDSM(report, DEVICE_DUID));
+ // SYSCALL
+ report = readReport(SYSCALL_REPORT);
+ assertTrue(MQPublisherAPI.sendReportToDSM(report, DEVICE_DUID));
+ // PAD
+ report = readReport(PAD_REPORT);
+ assertTrue(MQPublisherAPI.sendReportToDSM(report, DEVICE_DUID));
+ // DLP
+ report = readReport(DLP_REPORT);
+ assertTrue(MQPublisherAPI.sendReportToDSM(report, DEVICE_DUID));
+
+ checkfindReportsByDateBetween();
+ checkfindReportsByResult();
+ checkfindReportsByType();
+
+ assertTrue(MQPublisherAPI.deviceUnRegistrationInDSM(DEVICE_DUID, DEVICE_UNREGISTER_ACTION));
+ Thread.sleep(TIMEOUT);
+
+ deleteIoTUser(uuid);
+ Response resp = loginAdmin();
+ deleteNewUserIfAdmin(resp.getCookie("JSESSIONID"));
+ logoutAdmin();
+ }
+
+}
--- /dev/null
+{
+ "1": {
+ "date": "12:25 PM; May 08, 2017",
+ "syscall": "5",
+ "gid": "100",
+ "fsgid": "100",
+ "pid": "1005",
+ "suid": "5001",
+ "type": "dac",
+ "uid": "5001",
+ "egid": "100",
+ "exe": "/usr/bin/ls",
+ "sgid": "100",
+ "per": "800000",
+ "subj": "User::Shell",
+ "ses": "4294967295",
+ "auid": "4294967295",
+ "comm": "ls",
+ "euid": "5001",
+ "a0": "b7e3c6a0",
+ "ppid": "1001",
+ "a1": "a4800",
+ "fsuid": "5001",
+ "exit": "-13",
+ "a2": "b6ef84d0",
+ "a3": "2f",
+ "success": "no",
+ "tty": "pts1",
+ "arch": "40000028",
+ "items": "1"
+ }
+}
\ No newline at end of file
--- /dev/null
+{
+ "1": {
+ "date": "12:00 PM; Oct 09, 2017",
+ "appid": "org.tizen.gloftasp6",
+ "destination": "www.google.com",
+ "time": "1469476424.501:359",
+ "category": "IMEI",
+ "type": "dlp"
+ }
+}
\ No newline at end of file
--- /dev/null
+{
+ "1": {
+ "mode": "debug",
+ "date": "12:00 PM; Oct 09, 2017",
+ "start_symbol": "_text",
+ "gpa": "7fffd19c5592",
+ "end_symbol": "__start_rodata",
+ "type": "pad",
+ "operation": "write"
+ }
+}
\ No newline at end of file
--- /dev/null
+{
+ "1": {
+ "ppid_comm": "bash",
+ "date": "12:25 PM; May 08, 2017",
+ "wait": "no",
+ "comm": "bash",
+ "subject": "User",
+ "lsm": "SMACK",
+ "fn": "smack_inode_getattr",
+ "pid": "23926",
+ "ino": "18596",
+ "type": "smack",
+ "ppid": "22847",
+ "mode": "enforcing",
+ "d_inst": "1",
+ "path": "/tmp/wm_start",
+ "requested": "r",
+ "dev": "tmpfs",
+ "action": "denied",
+ "user_bt": "23926",
+ "object": "System"
+ }
+}
\ No newline at end of file
--- /dev/null
+{
+ "1": {
+ "ppid_comm": "bash",
+ "date": "12:25 PM; May 08, 2017",
+ "wait": "no",
+ "comm": "ls",
+ "subject": "User",
+ "lsm": "syscall",
+ "fn": "syscall_inode_getattr",
+ "pid": "25055",
+ "ino": "18596",
+ "type": "syscall",
+ "ppid": "23926",
+ "mode": "enforcing",
+ "d_inst": "1",
+ "path": "/tmp/wm_start",
+ "requested": "r",
+ "dev": "tmpfs",
+ "action": "denied",
+ "user_bt": "25055",
+ "object": "System"
+ }
+}
\ No newline at end of file
package com.samsung.commons.repository;
import com.samsung.commons.domain.Report;
+import com.samsung.commons.domain.ReportType;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
+import java.util.Date;
import java.util.List;
/**
@Query(value = "select * from report r where r.device_id= :device_id and r.type_id= :type_id order by id desc limit :limit", nativeQuery = true)
List<Report> findByIdAndTypeOrderByIdDesc(@Param("device_id") long deviceId, @Param("type_id") long typeId,
@Param("limit") int limit);
+
+ /**
+ * Find by type list.
+ *
+ * @param type the type
+ * @return the list
+ */
+ List<Report> findByType(ReportType type);
+
+ /**
+ * Find by result list.
+ *
+ * @param result the result
+ * @return the list
+ */
+ List<Report> findByResult(Integer result);
+
+ /**
+ * Find by date between list.
+ *
+ * @param dateFrom the date from
+ * @param dateTo the date to
+ * @return the list
+ */
+ List<Report> findByDateBetween(Date dateFrom, Date dateTo);
+ List<Report> findAll();
+
}
*/
package com.samsung.commons.service;
+import java.util.Date;
import java.util.List;
import com.samsung.commons.domain.Report;
* @return list of device's report types
*/
List<ReportType.Type> getDeviceReportTypes(long deviceId);
+
+ /**
+ * Find reports by type list.
+ *
+ * @param type the type
+ * @return the list
+ */
+ List<Report> findReportsByType(ReportType type);
+
+ /**
+ * Find reports by result list.
+ *
+ * @param result the result
+ * @return the list
+ */
+ List<Report> findReportsByResult(Integer result);
+
+ /**
+ * Find reports by date between list.
+ *
+ * @param dateFrom the date from
+ * @param dateTo the date to
+ * @return the list
+ */
+ List<Report> findReportsByDateBetween(Date dateFrom, Date dateTo);
}
import org.springframework.stereotype.Service;
import java.util.ArrayList;
+import java.util.Date;
import java.util.List;
/**
return reportTypes;
}
+
+ @Override
+ public List<Report> findReportsByType(ReportType type) {
+ return reportRepository.findByType(type);
+ }
+ @Override
+ public List<Report> findReportsByResult(Integer result) {
+ return reportRepository.findByResult(result);
+ }
+ @Override
+ public List<Report> findReportsByDateBetween(Date dateFrom, Date dateTo) {
+ // TODO: add date field to database and use function findByDateBetween(dateFrom, dateTo)
+ return reportRepository.findAll();
+ }
}
\ No newline at end of file
import static java.lang.String.format;
import static org.springframework.http.HttpStatus.OK;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.text.SimpleDateFormat;
+import java.util.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.format.annotation.DateTimeFormat;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.ResponseBody;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
import com.google.gson.Gson;
import com.samsung.commons.domain.Device;
}
return new ResponseEntity<>(OK);
}
+
+ private void putReport2Map (Integer repNo, Report report, Map map) {
+ ReportType reportType = report.getType();
+ ReportAnalyzer analyzer = ReportAnalyzerFactory.getAnalyzer(reportType.getType());
+ Map<String, String> dataMap = analyzer.extractReportData(report.getReport());
+ dataMap.put("date", report.getDate());
+ dataMap.put("type", reportType.getType());
+ map.put(repNo, dataMap);
+ }
+
+ /**
+ * Find reports by result response entity.
+ *
+ * @param result the result
+ * @return the response entity
+ */
+ @RequestMapping(value = "/findReportsByResult/{result}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
+ @ResponseBody
+ public ResponseEntity<String> findReportsByResult(@PathVariable Integer result) {
+ final HttpHeaders httpHeaders= new HttpHeaders();
+ httpHeaders.setContentType(MediaType.APPLICATION_JSON);
+ SortedMap<Integer, Map<String, String>> mapReports = new TreeMap<>();
+ List<Report> reports = reportService.findReportsByResult(result);
+ int rec = 0;
+ for (Report report : reports) putReport2Map(++rec,report,mapReports);
+ LOG.debug(format("findReportsByResult by result:%s - %s ", result, mapReports));
+ return new ResponseEntity<>(new Gson().toJson(mapReports), httpHeaders, HttpStatus.OK);
+ }
+
+ /**
+ * Find reports by type response entity.
+ *
+ * @param type the type
+ * @return the response entity
+ */
+ @RequestMapping(value = "/findReportsByType/{type}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
+ @ResponseBody
+ public ResponseEntity<String> findReportsByType(@PathVariable String type) {
+ final HttpHeaders httpHeaders= new HttpHeaders();
+ httpHeaders.setContentType(MediaType.APPLICATION_JSON);
+ SortedMap<Integer, Map<String, String>> mapReports = new TreeMap<>();
+ ReportType reportType = reportTypeService.findByName(type);
+ List<Report> reports = reportService.findReportsByType(reportType);
+ int rec = 0;
+ for (Report report : reports) putReport2Map(++rec,report,mapReports);
+ LOG.debug(format("findReportsByType by type:%s - %s ", type, mapReports));
+ return new ResponseEntity<>(new Gson().toJson(mapReports), httpHeaders, HttpStatus.OK);
+ }
+
+ /**
+ * Find reports by date between response entity.
+ *
+ * @param dateFrom the date from
+ * @param dateTo the date to
+ * @return the response entity
+ */
+ @RequestMapping(value = "/findReportsByDateBetween", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
+ @ResponseBody
+ public ResponseEntity<String> findReportsByDateBetween(@RequestParam(value = "from") @DateTimeFormat(pattern="MM-dd-yyyy") Date dateFrom,
+ @RequestParam(value = "to") @DateTimeFormat(pattern="MM-dd-yyyy") Date dateTo) {
+ final HttpHeaders httpHeaders= new HttpHeaders();
+ httpHeaders.setContentType(MediaType.APPLICATION_JSON);
+ SortedMap<Integer, Map<String, String>> mapReports = new TreeMap<>();
+ List<Report> reports = reportService.findReportsByDateBetween(dateFrom, dateTo);
+ int rec = 0;
+ String datePattern = "hh:mm a; MMM dd, yyyy"; //12:25 PM; May 08, 2017
+ SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
+ for (Report report : reports) {
+ try {
+ Date date = sdf.parse(report.getDate());
+ if (date.before(dateTo) && date.after(dateFrom)) putReport2Map(++rec,report,mapReports);
+ } catch (Exception e) {
+ LOG.error("Wrong date format: " + report.getReport());
+ }
+ }
+ LOG.debug(format("findReportsByDateBetween from:%s to:%s - %s ", dateFrom, dateTo, mapReports));
+ return new ResponseEntity<>(new Gson().toJson(mapReports), httpHeaders, HttpStatus.OK);
+ }
+
}