Implement BmcEpoch set/get elapsed()

Getting elapsed returns BMC's time;
Setting elapsed will check the current time mode/owner, and will return
error when mode is NTP or owner is HOST.

Add unit test cases.

Change-Id: Ibf4e90957f3b26b68c4a1b6dc66dc364c66bef10
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/bmc_epoch.cpp b/bmc_epoch.cpp
index c17ddf5..5923e8e 100644
--- a/bmc_epoch.cpp
+++ b/bmc_epoch.cpp
@@ -24,23 +24,22 @@
 
 uint64_t BmcEpoch::elapsed(uint64_t value)
 {
-    // TODO: set time based on current time mode and owner
-    auto time = std::chrono::microseconds(value);
-    switch (timeOwner)
+    if (timeMode == Mode::NTP)
     {
-        case Owner::BMC:
-        {
-            setTime(time);
-            break;
-        }
-        // TODO: below cases are to be implemented
-        case Owner::HOST:
-            break;
-        case Owner::SPLIT:
-            break;
-        case Owner::BOTH:
-            break;
+        log<level::ERR>("Setting BmcTime with NTP mode is not allowed");
+        // TODO: throw NotAllowed exception
+        return 0;
     }
+    if (timeOwner == Owner::HOST)
+    {
+        log<level::ERR>("Setting BmcTime with HOST owner is not allowed");
+        // TODO: throw NotAllowed exception
+        return 0;
+    }
+
+    auto time = std::chrono::microseconds(value);
+    setTime(time);
+
     server::EpochTime::elapsed(value);
     return value;
 }
diff --git a/test/TestBmcEpoch.cpp b/test/TestBmcEpoch.cpp
index 67f89bb..da65b3e 100644
--- a/test/TestBmcEpoch.cpp
+++ b/test/TestBmcEpoch.cpp
@@ -9,6 +9,7 @@
 namespace time
 {
 
+using namespace std::chrono;
 class TestBmcEpoch : public testing::Test
 {
     public:
@@ -34,6 +35,14 @@
         {
             return bmcEpoch.timeOwner;
         }
+        void setTimeOwner(Owner owner)
+        {
+            bmcEpoch.timeOwner = owner;
+        }
+        void setTimeMode(Mode mode)
+        {
+            bmcEpoch.timeMode = mode;
+        }
 };
 
 TEST_F(TestBmcEpoch, empty)
@@ -50,6 +59,27 @@
     EXPECT_GE(t2, t1);
 }
 
+TEST_F(TestBmcEpoch, setElapsedNotAllowed)
+{
+    auto epochNow = duration_cast<microseconds>(
+        system_clock::now().time_since_epoch()).count();
+    // In NTP mode, setting time is not allowed
+    auto ret = bmcEpoch.elapsed(epochNow);
+    EXPECT_EQ(0, ret);
+
+    // In Host owner, setting time is not allowed
+    setTimeMode(Mode::MANUAL);
+    setTimeOwner(Owner::HOST);
+    ret = bmcEpoch.elapsed(epochNow);
+    EXPECT_EQ(0, ret);
+}
+
+TEST_F(TestBmcEpoch, setElapsedOK)
+{
+    // TODO: setting time will call sd-bus functions and it will fail on host
+    // if we have gmock for sdbusplus::bus, we can test setElapsed.
+    // But for now we can not test it
+}
 
 }
 }