From 96564f6d4c0d68a4986b0b1ed9acca1107d3f9c9 Mon Sep 17 00:00:00 2001
From: rafaelatc3sl <rpd17@c3sl>
Date: Mon, 10 Jun 2019 11:18:41 -0300
Subject: [PATCH] Issue #17: Add bios date

Signed-off-by: rafaelatc3sl <rpd17@c3sl>
---
 include/agent/get_bios_date.h | 10 +++++++
 include/agent/inventory.h     |  1 +
 src/agent/get_bios_date.cpp   | 53 +++++++++++++++++++++++++++++++++++
 src/agent/inventory.cpp       |  8 ++++++
 test/returnTest.bats          | 14 +++++++--
 5 files changed, 84 insertions(+), 2 deletions(-)
 create mode 100644 include/agent/get_bios_date.h
 create mode 100644 src/agent/get_bios_date.cpp

diff --git a/include/agent/get_bios_date.h b/include/agent/get_bios_date.h
new file mode 100644
index 00000000..dce7feb5
--- /dev/null
+++ b/include/agent/get_bios_date.h
@@ -0,0 +1,10 @@
+#pragma once
+#include <fstream>
+#include <string>
+
+/**
+ * @file get_bios_date.h
+ * @brief Get data from bios format (YYYY-MM-DD)
+ */
+
+std::string get_bios_date();
\ No newline at end of file
diff --git a/include/agent/inventory.h b/include/agent/inventory.h
index 914dee8e..6efee4e0 100644
--- a/include/agent/inventory.h
+++ b/include/agent/inventory.h
@@ -6,6 +6,7 @@
 #include <iostream>
 #include <agent/agent.h>
 #include <agent/get_date.h>
+#include <agent/get_bios_date.h>
 #include <agent/get_disks_info.h>
 #include <agent/get_distro.h>
 #include <agent/get_macaddr.h>
diff --git a/src/agent/get_bios_date.cpp b/src/agent/get_bios_date.cpp
new file mode 100644
index 00000000..846c3f70
--- /dev/null
+++ b/src/agent/get_bios_date.cpp
@@ -0,0 +1,53 @@
+/* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
+ * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+ *
+ * This file is part of simmc-agent
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
+
+#include <agent/get_bios_date.h>
+
+std::string get_bios_date() {
+    #ifdef __unix__
+
+    std::ifstream file;
+    std::string line, d, m, y, format = "";
+    file.open("/sys/devices/virtual/dmi/id/bios_date");
+
+    if (file.is_open()) {
+        getline(file, line);
+        d = line.substr(0, 2);
+        m = line.substr(3, 2);
+        y = line.substr(6, 4);
+        format = y + "-" + m + "-" + d;
+    } else {
+        std::string err;
+        err = "Could not open /sys/devices/virtual/dmi/id/bios_date";
+        throw err;
+    }
+
+    file.close();
+
+    return format;
+
+    #elif WIN32
+
+    std::string err = "Feature not supported on Windows";
+    throw err;
+
+    #endif
+}
diff --git a/src/agent/inventory.cpp b/src/agent/inventory.cpp
index 45ab5a4b..ed895576 100644
--- a/src/agent/inventory.cpp
+++ b/src/agent/inventory.cpp
@@ -151,6 +151,14 @@ Json::Value get_inventory(Agent* agent) {
 
          inv["error_inventory"]["disk1_used"] = err;
     }
+
+    try {
+        inv["data_inventory"]["date_bios"] = get_bios_date();
+    } catch (std::string err) {
+        inv["data_inventory"]["date_bios"] = Json::nullValue;
+
+        inv["error_inventory"]["date_bios"] = err;
+    }
     // hw info
     /*
     inv["disk1_used"] =
diff --git a/test/returnTest.bats b/test/returnTest.bats
index 3b546afa..873e82a6 100755
--- a/test/returnTest.bats
+++ b/test/returnTest.bats
@@ -43,6 +43,11 @@ function processor() {
     [[ "$obj" != "null" ]]
 }
 
+function date_bios() {
+    obj=$(cat inventory.json | jq -r '.data_inventory.date_bios')
+    [[ "$obj" != "null" ]]
+}
+
 function bytes_received() {
     obj=$(cat net.json | jq -r '.data_net.bytes_received')
     [[ "$obj" -ge 0  ]]
@@ -95,9 +100,9 @@ function id_point() {
 
 setup() {
     /opt/agentC3SL/agent-v* --print --once | head -n -1  > export.json
-    pt1=$(cat export.json | head -n -9)
+    pt1=$(jq "del(."data_net")" export.json | head -n -1)
     echo $pt1 > inventory.json
-    pt2=$(cat export.json | tail -n 9)
+    pt2=$(jq "del(."data_inventory")" export.json | tail -n +2)
     echo $pt2 > net.json
     [ -s export.json ]
     [ -s inventory.json ]
@@ -121,6 +126,11 @@ teardown() {
     [ "$status" -eq 0 ]
 }
 
+@test "date_bios" {
+    run date_bios
+    [ "$status" -eq 0 ]
+}
+
 @test "port" {
     run port
     [ "$status" -eq 0 ]
-- 
GitLab