1 #include <sdbusplus/bus.hpp>
2 #include <gtest/gtest.h>
3 
4 #include "types.hpp"
5 #include "manager.hpp"
6 #include "mocked_property_change_listener.hpp"
7 
8 using ::testing::_;
9 
10 namespace phosphor
11 {
12 namespace time
13 {
14 
15 class TestManager : public testing::Test
16 {
17     public:
18         sdbusplus::bus::bus bus;
19         Manager manager;
20         MockPropertyChangeListner listener1;
21         MockPropertyChangeListner listener2;
22 
23         TestManager()
24             : bus(sdbusplus::bus::new_default()),
25               manager(bus)
26         {
27             // Add two mocked listeners so that we can test
28             // the behavior related to listeners
29             manager.addListener(&listener1);
30             manager.addListener(&listener2);
31         }
32 
33         // Proxies for Manager's private members and functions
34         Mode getTimeMode()
35         {
36             return manager.timeMode;
37         }
38         Owner getTimeOwner()
39         {
40             return manager.timeOwner;
41         }
42         bool hostOn()
43         {
44             return manager.hostOn;
45         }
46         std::string getRequestedMode()
47         {
48             return manager.requestedMode;
49         }
50         std::string getRequestedOwner()
51         {
52             return manager.requestedOwner;
53         }
54         void notifyPropertyChanged(const std::string& key,
55                                    const std::string& value)
56         {
57             manager.onPropertyChanged(key, value);
58         }
59         void notifyOnHostState(bool hostOn)
60         {
61             manager.onHostState(hostOn);
62         }
63 };
64 
65 TEST_F(TestManager, DISABLED_empty)
66 {
67     EXPECT_FALSE(hostOn());
68     EXPECT_EQ("", getRequestedMode());
69     EXPECT_EQ("", getRequestedOwner());
70 
71     // Default mode/owner is MANUAL/BOTH
72     EXPECT_EQ(Mode::Manual, getTimeMode());
73     EXPECT_EQ(Owner::Both, getTimeOwner());
74 }
75 
76 
77 TEST_F(TestManager, DISABLED_hostStateChange)
78 {
79     notifyOnHostState(true);
80     EXPECT_TRUE(hostOn());
81     notifyOnHostState(false);
82     EXPECT_FALSE(hostOn());
83 }
84 
85 TEST_F(TestManager, DISABLED_propertyChanged)
86 {
87     // When host is off, property change will be notified to listners
88     EXPECT_FALSE(hostOn());
89 
90     // Check mocked listeners shall receive notifications on property changed
91     EXPECT_CALL(listener1, onModeChanged(Mode::Manual)).Times(1);
92     EXPECT_CALL(listener1, onOwnerChanged(Owner::Host)).Times(1);
93     EXPECT_CALL(listener2, onModeChanged(Mode::Manual)).Times(1);
94     EXPECT_CALL(listener2, onOwnerChanged(Owner::Host)).Times(1);
95 
96     notifyPropertyChanged(
97         "TimeSyncMethod",
98         "xyz.openbmc_project.Time.Synchronization.Method.Manual");
99     notifyPropertyChanged(
100         "TimeOwner",
101         "xyz.openbmc_project.Time.Owner.Owners.Host");
102 
103     EXPECT_EQ("", getRequestedMode());
104     EXPECT_EQ("", getRequestedOwner());
105 
106     // When host is on, property changes are saved as requested ones
107     notifyOnHostState(true);
108 
109     // Check mocked listeners shall not receive notifications
110     EXPECT_CALL(listener1, onModeChanged(Mode::Manual)).Times(0);
111     EXPECT_CALL(listener1, onOwnerChanged(Owner::Host)).Times(0);
112     EXPECT_CALL(listener2, onModeChanged(Mode::Manual)).Times(0);
113     EXPECT_CALL(listener2, onOwnerChanged(Owner::Host)).Times(0);
114 
115     notifyPropertyChanged(
116         "TimeSyncMethod",
117         "xyz.openbmc_project.Time.Synchronization.Method.NTP");
118     notifyPropertyChanged(
119         "TimeOwner",
120         "xyz.openbmc_project.Time.Owner.Owners.Split");
121 
122     EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP",
123               getRequestedMode());
124     EXPECT_EQ("xyz.openbmc_project.Time.Owner.Owners.Split",
125               getRequestedOwner());
126 
127 
128     // When host becomes off, the requested mode/owner shall be notified
129     // to listners, and be cleared
130     EXPECT_CALL(listener1, onModeChanged(Mode::NTP)).Times(1);
131     EXPECT_CALL(listener1, onOwnerChanged(Owner::Split)).Times(1);
132     EXPECT_CALL(listener2, onModeChanged(Mode::NTP)).Times(1);
133     EXPECT_CALL(listener2, onOwnerChanged(Owner::Split)).Times(1);
134 
135     notifyOnHostState(false);
136 
137     EXPECT_EQ("", getRequestedMode());
138     EXPECT_EQ("", getRequestedOwner());
139 
140     // When host is on, and invalid property is changed,
141     // verify the code asserts because it shall never occur
142     notifyOnHostState(true);
143     ASSERT_DEATH(notifyPropertyChanged("invalid property", "whatever"), "");
144 }
145 
146 TEST_F(TestManager, DISABLED_propertyChangedAndChangedbackWhenHostOn)
147 {
148     // Property is now MANUAL/HOST
149     notifyPropertyChanged(
150         "TimeSyncMethod",
151         "xyz.openbmc_project.Time.Synchronization.Method.Manual");
152     notifyPropertyChanged(
153         "TimeOwner",
154         "xyz.openbmc_project.Time.Owner.Owners.Host");
155 
156     // Set host on
157     notifyOnHostState(true);
158 
159     // Check mocked listeners shall not receive notifications
160     EXPECT_CALL(listener1, onModeChanged(_)).Times(0);
161     EXPECT_CALL(listener1, onOwnerChanged(_)).Times(0);
162     EXPECT_CALL(listener2, onModeChanged(_)).Times(0);
163     EXPECT_CALL(listener2, onOwnerChanged(_)).Times(0);
164 
165     notifyPropertyChanged(
166         "TimeSyncMethod",
167         "xyz.openbmc_project.Time.Synchronization.Method.NTP");
168     notifyPropertyChanged(
169         "TimeOwner",
170         "xyz.openbmc_project.Time.Owner.Owners.Split");
171 
172     // Saved as requested mode/owner
173     EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.NTP",
174               getRequestedMode());
175     EXPECT_EQ("xyz.openbmc_project.Time.Owner.Owners.Split",
176               getRequestedOwner());
177 
178     // Property changed back to MANUAL/HOST
179     notifyPropertyChanged(
180         "TimeSyncMethod",
181         "xyz.openbmc_project.Time.Synchronization.Method.Manual");
182     notifyPropertyChanged(
183         "TimeOwner",
184         "xyz.openbmc_project.Time.Owner.Owners.Host");
185 
186     // Requested mode/owner shall be updated
187     EXPECT_EQ("xyz.openbmc_project.Time.Synchronization.Method.Manual",
188               getRequestedMode());
189     EXPECT_EQ("xyz.openbmc_project.Time.Owner.Owners.Host",
190               getRequestedOwner());
191 
192     // Because the latest mode/owner is the same as when host is off,
193     // The listeners shall not be notified, and requested mode/owner
194     // shall be cleared
195     EXPECT_CALL(listener1, onModeChanged(_)).Times(0);
196     EXPECT_CALL(listener1, onOwnerChanged(_)).Times(0);
197     EXPECT_CALL(listener2, onModeChanged(_)).Times(0);
198     EXPECT_CALL(listener2, onOwnerChanged(_)).Times(0);
199 
200     notifyOnHostState(false);
201 
202     EXPECT_EQ("", getRequestedMode());
203     EXPECT_EQ("", getRequestedOwner());
204 }
205 
206 // TODO: if gmock is ready, add case to test
207 // updateNtpSetting() and updateNetworkSetting()
208 
209 }
210 }
211