blob: 2fd55a50bcecadeba6e1a3c56f9c79cd04b9f8ac [file] [log] [blame]
Lei YU96232822017-01-20 14:05:46 +08001#include <sdbusplus/bus.hpp>
2#include <gtest/gtest.h>
3
4#include "bmc_epoch.hpp"
5#include "config.h"
Lei YU415b9642017-02-09 11:37:26 +08006#include "types.hpp"
Lei YU96232822017-01-20 14:05:46 +08007
8namespace phosphor
9{
10namespace time
11{
12
Lei YUe7abcdc2017-01-16 15:05:24 +080013using namespace std::chrono;
Lei YU96232822017-01-20 14:05:46 +080014class TestBmcEpoch : public testing::Test
15{
16 public:
Lei YU96232822017-01-20 14:05:46 +080017 sdbusplus::bus::bus bus;
18 BmcEpoch bmcEpoch;
19
20 TestBmcEpoch()
21 : bus(sdbusplus::bus::new_default()),
22 bmcEpoch(bus, OBJPATH_BMC)
23 {
24 // Empty
25 }
26
27 // Proxies for BmcEpoch's private members and functions
28 Mode getTimeMode()
29 {
30 return bmcEpoch.timeMode;
31 }
32 Owner getTimeOwner()
33 {
34 return bmcEpoch.timeOwner;
35 }
Lei YUe7abcdc2017-01-16 15:05:24 +080036 void setTimeOwner(Owner owner)
37 {
38 bmcEpoch.timeOwner = owner;
39 }
40 void setTimeMode(Mode mode)
41 {
42 bmcEpoch.timeMode = mode;
43 }
Lei YU96232822017-01-20 14:05:46 +080044};
45
46TEST_F(TestBmcEpoch, empty)
47{
48 EXPECT_EQ(Mode::NTP, getTimeMode());
49 EXPECT_EQ(Owner::BMC, getTimeOwner());
50}
51
52TEST_F(TestBmcEpoch, getElapsed)
53{
54 auto t1 = bmcEpoch.elapsed();
55 EXPECT_NE(0, t1);
56 auto t2 = bmcEpoch.elapsed();
57 EXPECT_GE(t2, t1);
58}
59
Lei YUe7abcdc2017-01-16 15:05:24 +080060TEST_F(TestBmcEpoch, setElapsedNotAllowed)
61{
62 auto epochNow = duration_cast<microseconds>(
63 system_clock::now().time_since_epoch()).count();
64 // In NTP mode, setting time is not allowed
65 auto ret = bmcEpoch.elapsed(epochNow);
66 EXPECT_EQ(0, ret);
67
68 // In Host owner, setting time is not allowed
69 setTimeMode(Mode::MANUAL);
70 setTimeOwner(Owner::HOST);
71 ret = bmcEpoch.elapsed(epochNow);
72 EXPECT_EQ(0, ret);
73}
74
75TEST_F(TestBmcEpoch, setElapsedOK)
76{
77 // TODO: setting time will call sd-bus functions and it will fail on host
78 // if we have gmock for sdbusplus::bus, we can test setElapsed.
79 // But for now we can not test it
80}
Lei YU96232822017-01-20 14:05:46 +080081
82}
83}