1 //
2 // Copyright (C) 2014 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "shill/wifi/wake_on_wifi.h"
18 
19 #include <linux/nl80211.h>
20 
21 #include <set>
22 #include <string>
23 #include <utility>
24 
25 #include <base/message_loop/message_loop.h>
26 #if defined(__ANDROID__)
27 #include <dbus/service_constants.h>
28 #else
29 #include <chromeos/dbus/service_constants.h>
30 #endif  // __ANDROID__
31 #include <gmock/gmock.h>
32 #include <gtest/gtest.h>
33 
34 #include "shill/error.h"
35 #include "shill/event_dispatcher.h"
36 #include "shill/ip_address_store.h"
37 #include "shill/logging.h"
38 #include "shill/mock_event_dispatcher.h"
39 #include "shill/mock_log.h"
40 #include "shill/mock_metrics.h"
41 #include "shill/net/byte_string.h"
42 #include "shill/net/ip_address.h"
43 #include "shill/net/mock_netlink_manager.h"
44 #include "shill/net/mock_time.h"
45 #include "shill/net/netlink_message_matchers.h"
46 #include "shill/net/netlink_packet.h"
47 #include "shill/net/nl80211_message.h"
48 #include "shill/net/shill_time.h"
49 #include "shill/nice_mock_control.h"
50 #include "shill/test_event_dispatcher.h"
51 #include "shill/testing.h"
52 
53 using base::Bind;
54 using base::Closure;
55 using base::Unretained;
56 using std::set;
57 using std::string;
58 using std::vector;
59 using testing::_;
60 using ::testing::AnyNumber;
61 using ::testing::HasSubstr;
62 using ::testing::Return;
63 
64 namespace shill {
65 
66 namespace {
67 
68 const uint16_t kNl80211FamilyId = 0x13;
69 
70 const uint8_t kSSIDBytes1[] = {0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65,
71                                0x47, 0x75, 0x65, 0x73, 0x74};
72 // Bytes representing a NL80211_CMD_SET_WOWLAN reporting that the system woke
73 // up because of an SSID match. The net detect results report a single SSID
74 // match represented by kSSIDBytes1, occurring in the frequencies in
75 // kSSID1FreqMatches.
76 const uint8_t kWakeReasonSSIDNlMsg[] = {
77     0x90, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78     0x00, 0x00, 0x00, 0x00, 0x4a, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
79     0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x99, 0x00, 0x01, 0x00, 0x00, 0x00,
80     0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00, 0x02, 0x00, 0x00, 0x00,
81     0x60, 0x00, 0x75, 0x00, 0x5c, 0x00, 0x13, 0x00, 0x58, 0x00, 0x00, 0x00,
82     0x0f, 0x00, 0x34, 0x00, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x47, 0x75,
83     0x65, 0x73, 0x74, 0x00, 0x44, 0x00, 0x2c, 0x00, 0x08, 0x00, 0x00, 0x00,
84     0x6c, 0x09, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00, 0x85, 0x09, 0x00, 0x00,
85     0x08, 0x00, 0x02, 0x00, 0x9e, 0x09, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
86     0x3c, 0x14, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, 0x78, 0x14, 0x00, 0x00,
87     0x08, 0x00, 0x05, 0x00, 0x71, 0x16, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
88     0xad, 0x16, 0x00, 0x00, 0x08, 0x00, 0x07, 0x00, 0xc1, 0x16, 0x00, 0x00};
89 const uint32_t kTimeToNextLeaseRenewalShort = 1;
90 const uint32_t kTimeToNextLeaseRenewalLong = 1000;
91 const uint32_t kNetDetectScanIntervalSeconds = 120;
92 // These blobs represent NL80211 messages from the kernel reporting the NIC's
93 // wake-on-packet settings, sent in response to NL80211_CMD_GET_WOWLAN requests.
94 const uint8_t kResponseNoIPAddresses[] = {
95     0x14, 0x00, 0x00, 0x00, 0x13, 0x00, 0x01, 0x00, 0x01, 0x00,
96     0x00, 0x00, 0x57, 0x40, 0x00, 0x00, 0x49, 0x01, 0x00, 0x00};
97 const uint8_t kResponseIPV40[] = {
98     0x4C, 0x00, 0x00, 0x00, 0x13, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
99     0x00, 0x57, 0x40, 0x00, 0x00, 0x49, 0x01, 0x00, 0x00, 0x38, 0x00,
100     0x75, 0x00, 0x34, 0x00, 0x04, 0x00, 0x30, 0x00, 0x01, 0x00, 0x08,
101     0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x22, 0x00, 0x02, 0x00,
102     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104     0x00, 0x00, 0x00, 0x00, 0xC0, 0xA8, 0x0A, 0x14, 0x00, 0x00};
105 const uint8_t kResponseIPV40WakeOnDisconnect[] = {
106     0x50, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
107     0x57, 0x40, 0x00, 0x00, 0x49, 0x01, 0x00, 0x00, 0x3C, 0x00, 0x75, 0x00,
108     0x04, 0x00, 0x02, 0x00, 0x34, 0x00, 0x04, 0x00, 0x30, 0x00, 0x01, 0x00,
109     0x08, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x22, 0x00, 0x02, 0x00,
110     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
112     0x00, 0x00, 0xC0, 0xA8, 0x0A, 0x14, 0x00, 0x00};
113 const uint8_t kResponseIPV401[] = {
114     0x7C, 0x00, 0x00, 0x00, 0x13, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
115     0x57, 0x40, 0x00, 0x00, 0x49, 0x01, 0x00, 0x00, 0x68, 0x00, 0x75, 0x00,
116     0x64, 0x00, 0x04, 0x00, 0x30, 0x00, 0x01, 0x00, 0x08, 0x00, 0x01, 0x00,
117     0x00, 0x00, 0x00, 0x3C, 0x22, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
118     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
119     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
120     0x03, 0x04, 0x00, 0x00, 0x30, 0x00, 0x02, 0x00, 0x08, 0x00, 0x01, 0x00,
121     0x00, 0x00, 0x00, 0x3C, 0x22, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
122     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xA8,
124     0x0A, 0x14, 0x00, 0x00};
125 const uint8_t kResponseIPV401IPV60[] = {
126     0xB8, 0x00, 0x00, 0x00, 0x13, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
127     0x57, 0x40, 0x00, 0x00, 0x49, 0x01, 0x00, 0x00, 0xA4, 0x00, 0x75, 0x00,
128     0xA0, 0x00, 0x04, 0x00, 0x30, 0x00, 0x01, 0x00, 0x08, 0x00, 0x01, 0x00,
129     0x00, 0x00, 0x00, 0x3C, 0x22, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
130     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
132     0x03, 0x04, 0x00, 0x00, 0x30, 0x00, 0x02, 0x00, 0x08, 0x00, 0x01, 0x00,
133     0x00, 0x00, 0x00, 0x3C, 0x22, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
134     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xA8,
136     0x0A, 0x14, 0x00, 0x00, 0x3C, 0x00, 0x03, 0x00, 0x09, 0x00, 0x01, 0x00,
137     0x00, 0x00, 0xC0, 0xFF, 0x3F, 0x00, 0x00, 0x00, 0x2A, 0x00, 0x02, 0x00,
138     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
139     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xDC,
140     0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54,
141     0x32, 0x10, 0x00, 0x00};
142 const uint8_t kResponseIPV401IPV601[] = {
143     0xF4, 0x00, 0x00, 0x00, 0x13, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
144     0x57, 0x40, 0x00, 0x00, 0x49, 0x01, 0x00, 0x00, 0xE0, 0x00, 0x75, 0x00,
145     0xDC, 0x00, 0x04, 0x00, 0x30, 0x00, 0x01, 0x00, 0x08, 0x00, 0x01, 0x00,
146     0x00, 0x00, 0x00, 0x3C, 0x22, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
147     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
148     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
149     0x03, 0x04, 0x00, 0x00, 0x3C, 0x00, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00,
150     0x00, 0x00, 0xC0, 0xFF, 0x3F, 0x00, 0x00, 0x00, 0x2A, 0x00, 0x02, 0x00,
151     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
152     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x80,
153     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x00, 0x20, 0x0C,
154     0x41, 0x7A, 0x00, 0x00, 0x30, 0x00, 0x03, 0x00, 0x08, 0x00, 0x01, 0x00,
155     0x00, 0x00, 0x00, 0x3C, 0x22, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
156     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
157     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xA8,
158     0x0A, 0x14, 0x00, 0x00, 0x3C, 0x00, 0x04, 0x00, 0x09, 0x00, 0x01, 0x00,
159     0x00, 0x00, 0xC0, 0xFF, 0x3F, 0x00, 0x00, 0x00, 0x2A, 0x00, 0x02, 0x00,
160     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
161     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xDC,
162     0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54,
163     0x32, 0x10, 0x00, 0x00};
164 // This blob represents an NL80211 messages from the kernel reporting that the
165 // NIC is programmed to wake on the SSIDs represented by kSSIDBytes1 and
166 // kSSIDBytes2, and scans for these SSIDs at interval
167 // kNetDetectScanIntervalSeconds.
168 const uint8_t kResponseWakeOnSSID[] = {
169     0x60, 0x01, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x9a, 0x01, 0x00, 0x00,
170     0xfa, 0x02, 0x00, 0x00, 0x49, 0x01, 0x00, 0x00, 0x4c, 0x01, 0x75, 0x00,
171     0x48, 0x01, 0x12, 0x00, 0x08, 0x00, 0x77, 0x00, 0xc0, 0xd4, 0x01, 0x00,
172     0x0c, 0x01, 0x2c, 0x00, 0x08, 0x00, 0x00, 0x00, 0x6c, 0x09, 0x00, 0x00,
173     0x08, 0x00, 0x01, 0x00, 0x71, 0x09, 0x00, 0x00, 0x08, 0x00, 0x02, 0x00,
174     0x76, 0x09, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00, 0x7b, 0x09, 0x00, 0x00,
175     0x08, 0x00, 0x04, 0x00, 0x80, 0x09, 0x00, 0x00, 0x08, 0x00, 0x05, 0x00,
176     0x85, 0x09, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00, 0x8a, 0x09, 0x00, 0x00,
177     0x08, 0x00, 0x07, 0x00, 0x8f, 0x09, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00,
178     0x94, 0x09, 0x00, 0x00, 0x08, 0x00, 0x09, 0x00, 0x99, 0x09, 0x00, 0x00,
179     0x08, 0x00, 0x0a, 0x00, 0x9e, 0x09, 0x00, 0x00, 0x08, 0x00, 0x0b, 0x00,
180     0x3c, 0x14, 0x00, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x50, 0x14, 0x00, 0x00,
181     0x08, 0x00, 0x0d, 0x00, 0x64, 0x14, 0x00, 0x00, 0x08, 0x00, 0x0e, 0x00,
182     0x78, 0x14, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00, 0x8c, 0x14, 0x00, 0x00,
183     0x08, 0x00, 0x10, 0x00, 0xa0, 0x14, 0x00, 0x00, 0x08, 0x00, 0x11, 0x00,
184     0xb4, 0x14, 0x00, 0x00, 0x08, 0x00, 0x12, 0x00, 0xc8, 0x14, 0x00, 0x00,
185     0x08, 0x00, 0x13, 0x00, 0x7c, 0x15, 0x00, 0x00, 0x08, 0x00, 0x14, 0x00,
186     0x90, 0x15, 0x00, 0x00, 0x08, 0x00, 0x15, 0x00, 0xa4, 0x15, 0x00, 0x00,
187     0x08, 0x00, 0x16, 0x00, 0xb8, 0x15, 0x00, 0x00, 0x08, 0x00, 0x17, 0x00,
188     0xcc, 0x15, 0x00, 0x00, 0x08, 0x00, 0x18, 0x00, 0x1c, 0x16, 0x00, 0x00,
189     0x08, 0x00, 0x19, 0x00, 0x30, 0x16, 0x00, 0x00, 0x08, 0x00, 0x1a, 0x00,
190     0x44, 0x16, 0x00, 0x00, 0x08, 0x00, 0x1b, 0x00, 0x58, 0x16, 0x00, 0x00,
191     0x08, 0x00, 0x1c, 0x00, 0x71, 0x16, 0x00, 0x00, 0x08, 0x00, 0x1d, 0x00,
192     0x85, 0x16, 0x00, 0x00, 0x08, 0x00, 0x1e, 0x00, 0x99, 0x16, 0x00, 0x00,
193     0x08, 0x00, 0x1f, 0x00, 0xad, 0x16, 0x00, 0x00, 0x08, 0x00, 0x20, 0x00,
194     0xc1, 0x16, 0x00, 0x00, 0x30, 0x00, 0x84, 0x00, 0x14, 0x00, 0x00, 0x00,
195     0x0f, 0x00, 0x01, 0x00, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x47, 0x75,
196     0x65, 0x73, 0x74, 0x00, 0x18, 0x00, 0x01, 0x00, 0x12, 0x00, 0x01, 0x00,
197     0x54, 0x50, 0x2d, 0x4c, 0x49, 0x4e, 0x4b, 0x5f, 0x38, 0x37, 0x36, 0x44,
198     0x33, 0x35, 0x00, 0x00};
199 const uint8_t kSSIDBytes2[] = {0x54, 0x50, 0x2d, 0x4c, 0x49, 0x4e, 0x4b,
200                                0x5f, 0x38, 0x37, 0x36, 0x44, 0x33, 0x35};
201 
202 // Bytes representing a NL80211_CMD_NEW_WIPHY message reporting the WiFi
203 // capabilities of a NIC. This message reports that the NIC supports wake on
204 // pattern (on up to |kNewWiphyNlMsg_MaxPatterns| registered patterns), supports
205 // wake on SSID (on up to |kNewWiphyNlMsg_MaxSSIDs| SSIDs), and supports wake on
206 // disconnect.
207 const uint8_t kNewWiphyNlMsg[] = {
208     0xb8, 0x0d, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
209     0xd9, 0x53, 0x00, 0x00, 0x03, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
210     0x02, 0x00, 0x00, 0x00, 0x09, 0x00, 0x02, 0x00, 0x70, 0x68, 0x79, 0x30,
211     0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x2e, 0x00, 0x01, 0x00, 0x00, 0x00,
212     0x05, 0x00, 0x3d, 0x00, 0x07, 0x00, 0x00, 0x00, 0x05, 0x00, 0x3e, 0x00,
213     0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x3f, 0x00, 0xff, 0xff, 0xff, 0xff,
214     0x08, 0x00, 0x40, 0x00, 0xff, 0xff, 0xff, 0xff, 0x05, 0x00, 0x59, 0x00,
215     0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x2b, 0x00, 0x14, 0x00, 0x00, 0x00,
216     0x05, 0x00, 0x7b, 0x00, 0x14, 0x00, 0x00, 0x00, 0x06, 0x00, 0x38, 0x00,
217     0xa9, 0x01, 0x00, 0x00, 0x06, 0x00, 0x7c, 0x00, 0xe6, 0x01, 0x00, 0x00,
218     0x05, 0x00, 0x85, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x04, 0x00, 0x68, 0x00,
219     0x04, 0x00, 0x82, 0x00, 0x1c, 0x00, 0x39, 0x00, 0x04, 0xac, 0x0f, 0x00,
220     0x02, 0xac, 0x0f, 0x00, 0x01, 0xac, 0x0f, 0x00, 0x05, 0xac, 0x0f, 0x00,
221     0x06, 0xac, 0x0f, 0x00, 0x01, 0x72, 0x14, 0x00, 0x05, 0x00, 0x56, 0x00,
222     0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x66, 0x00, 0x08, 0x00, 0x71, 0x00,
223     0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00,
224     0x24, 0x00, 0x20, 0x00, 0x04, 0x00, 0x01, 0x00, 0x04, 0x00, 0x02, 0x00,
225     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x04, 0x00, 0x04, 0x00, 0x06, 0x00,
226     0x04, 0x00, 0x08, 0x00, 0x04, 0x00, 0x09, 0x00, 0x04, 0x00, 0x0a, 0x00,
227     0x94, 0x05, 0x16, 0x00, 0xe8, 0x01, 0x00, 0x00, 0x14, 0x00, 0x03, 0x00,
228     0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x01,
229     0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0xe2, 0x11, 0x00, 0x00,
230     0x05, 0x00, 0x05, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00,
231     0x05, 0x00, 0x00, 0x00, 0x18, 0x01, 0x01, 0x00, 0x14, 0x00, 0x00, 0x00,
232     0x08, 0x00, 0x01, 0x00, 0x6c, 0x09, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
233     0x98, 0x08, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00, 0x08, 0x00, 0x01, 0x00,
234     0x71, 0x09, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00,
235     0x14, 0x00, 0x02, 0x00, 0x08, 0x00, 0x01, 0x00, 0x76, 0x09, 0x00, 0x00,
236     0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00, 0x14, 0x00, 0x03, 0x00,
237     0x08, 0x00, 0x01, 0x00, 0x7b, 0x09, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
238     0x98, 0x08, 0x00, 0x00, 0x14, 0x00, 0x04, 0x00, 0x08, 0x00, 0x01, 0x00,
239     0x80, 0x09, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00,
240     0x14, 0x00, 0x05, 0x00, 0x08, 0x00, 0x01, 0x00, 0x85, 0x09, 0x00, 0x00,
241     0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00, 0x14, 0x00, 0x06, 0x00,
242     0x08, 0x00, 0x01, 0x00, 0x8a, 0x09, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
243     0x98, 0x08, 0x00, 0x00, 0x14, 0x00, 0x07, 0x00, 0x08, 0x00, 0x01, 0x00,
244     0x8f, 0x09, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00,
245     0x14, 0x00, 0x08, 0x00, 0x08, 0x00, 0x01, 0x00, 0x94, 0x09, 0x00, 0x00,
246     0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00, 0x14, 0x00, 0x09, 0x00,
247     0x08, 0x00, 0x01, 0x00, 0x99, 0x09, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
248     0x98, 0x08, 0x00, 0x00, 0x14, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x01, 0x00,
249     0x9e, 0x09, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00,
250     0x1c, 0x00, 0x0b, 0x00, 0x08, 0x00, 0x01, 0x00, 0xa3, 0x09, 0x00, 0x00,
251     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00, 0x08, 0x00, 0x06, 0x00,
252     0x98, 0x08, 0x00, 0x00, 0x1c, 0x00, 0x0c, 0x00, 0x08, 0x00, 0x01, 0x00,
253     0xa8, 0x09, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00,
254     0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00, 0xa0, 0x00, 0x02, 0x00,
255     0x0c, 0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x00, 0x00,
256     0x10, 0x00, 0x01, 0x00, 0x08, 0x00, 0x01, 0x00, 0x14, 0x00, 0x00, 0x00,
257     0x04, 0x00, 0x02, 0x00, 0x10, 0x00, 0x02, 0x00, 0x08, 0x00, 0x01, 0x00,
258     0x37, 0x00, 0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x10, 0x00, 0x03, 0x00,
259     0x08, 0x00, 0x01, 0x00, 0x6e, 0x00, 0x00, 0x00, 0x04, 0x00, 0x02, 0x00,
260     0x0c, 0x00, 0x04, 0x00, 0x08, 0x00, 0x01, 0x00, 0x3c, 0x00, 0x00, 0x00,
261     0x0c, 0x00, 0x05, 0x00, 0x08, 0x00, 0x01, 0x00, 0x5a, 0x00, 0x00, 0x00,
262     0x0c, 0x00, 0x06, 0x00, 0x08, 0x00, 0x01, 0x00, 0x78, 0x00, 0x00, 0x00,
263     0x0c, 0x00, 0x07, 0x00, 0x08, 0x00, 0x01, 0x00, 0xb4, 0x00, 0x00, 0x00,
264     0x0c, 0x00, 0x08, 0x00, 0x08, 0x00, 0x01, 0x00, 0xf0, 0x00, 0x00, 0x00,
265     0x0c, 0x00, 0x09, 0x00, 0x08, 0x00, 0x01, 0x00, 0x68, 0x01, 0x00, 0x00,
266     0x0c, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x01, 0x00, 0xe0, 0x01, 0x00, 0x00,
267     0x0c, 0x00, 0x0b, 0x00, 0x08, 0x00, 0x01, 0x00, 0x1c, 0x02, 0x00, 0x00,
268     0xa8, 0x03, 0x01, 0x00, 0x14, 0x00, 0x03, 0x00, 0xff, 0xff, 0x00, 0x00,
269     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x01, 0x01, 0x00, 0x00, 0x00,
270     0x06, 0x00, 0x04, 0x00, 0xe2, 0x11, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00,
271     0x03, 0x00, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00, 0x05, 0x00, 0x00, 0x00,
272     0x0c, 0x00, 0x07, 0x00, 0xfa, 0xff, 0x00, 0x00, 0xfa, 0xff, 0x00, 0x00,
273     0x08, 0x00, 0x08, 0x00, 0xa0, 0x71, 0x80, 0x03, 0x00, 0x03, 0x01, 0x00,
274     0x1c, 0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00, 0x3c, 0x14, 0x00, 0x00,
275     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00, 0x08, 0x00, 0x06, 0x00,
276     0x98, 0x08, 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x08, 0x00, 0x01, 0x00,
277     0x50, 0x14, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00,
278     0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00, 0x1c, 0x00, 0x02, 0x00,
279     0x08, 0x00, 0x01, 0x00, 0x64, 0x14, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00,
280     0x04, 0x00, 0x03, 0x00, 0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00,
281     0x1c, 0x00, 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x78, 0x14, 0x00, 0x00,
282     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00, 0x08, 0x00, 0x06, 0x00,
283     0x98, 0x08, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x08, 0x00, 0x01, 0x00,
284     0x8c, 0x14, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00,
285     0x04, 0x00, 0x05, 0x00, 0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00,
286     0x20, 0x00, 0x05, 0x00, 0x08, 0x00, 0x01, 0x00, 0xa0, 0x14, 0x00, 0x00,
287     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00,
288     0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00, 0x20, 0x00, 0x06, 0x00,
289     0x08, 0x00, 0x01, 0x00, 0xb4, 0x14, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00,
290     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x08, 0x00, 0x06, 0x00,
291     0x98, 0x08, 0x00, 0x00, 0x20, 0x00, 0x07, 0x00, 0x08, 0x00, 0x01, 0x00,
292     0xc8, 0x14, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00,
293     0x04, 0x00, 0x05, 0x00, 0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00,
294     0x20, 0x00, 0x08, 0x00, 0x08, 0x00, 0x01, 0x00, 0x7c, 0x15, 0x00, 0x00,
295     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00,
296     0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00, 0x20, 0x00, 0x09, 0x00,
297     0x08, 0x00, 0x01, 0x00, 0x90, 0x15, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00,
298     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x08, 0x00, 0x06, 0x00,
299     0x98, 0x08, 0x00, 0x00, 0x20, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x01, 0x00,
300     0xa4, 0x15, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00,
301     0x04, 0x00, 0x05, 0x00, 0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00,
302     0x20, 0x00, 0x0b, 0x00, 0x08, 0x00, 0x01, 0x00, 0xb8, 0x15, 0x00, 0x00,
303     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00,
304     0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00, 0x20, 0x00, 0x0c, 0x00,
305     0x08, 0x00, 0x01, 0x00, 0xcc, 0x15, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00,
306     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x08, 0x00, 0x06, 0x00,
307     0x98, 0x08, 0x00, 0x00, 0x20, 0x00, 0x0d, 0x00, 0x08, 0x00, 0x01, 0x00,
308     0xe0, 0x15, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00,
309     0x04, 0x00, 0x05, 0x00, 0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00,
310     0x20, 0x00, 0x0e, 0x00, 0x08, 0x00, 0x01, 0x00, 0xf4, 0x15, 0x00, 0x00,
311     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00,
312     0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00, 0x20, 0x00, 0x0f, 0x00,
313     0x08, 0x00, 0x01, 0x00, 0x08, 0x16, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00,
314     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x08, 0x00, 0x06, 0x00,
315     0x98, 0x08, 0x00, 0x00, 0x20, 0x00, 0x10, 0x00, 0x08, 0x00, 0x01, 0x00,
316     0x1c, 0x16, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00,
317     0x04, 0x00, 0x05, 0x00, 0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00,
318     0x20, 0x00, 0x11, 0x00, 0x08, 0x00, 0x01, 0x00, 0x30, 0x16, 0x00, 0x00,
319     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00,
320     0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00, 0x20, 0x00, 0x12, 0x00,
321     0x08, 0x00, 0x01, 0x00, 0x44, 0x16, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00,
322     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x08, 0x00, 0x06, 0x00,
323     0x98, 0x08, 0x00, 0x00, 0x20, 0x00, 0x13, 0x00, 0x08, 0x00, 0x01, 0x00,
324     0x58, 0x16, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00,
325     0x04, 0x00, 0x05, 0x00, 0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00,
326     0x1c, 0x00, 0x14, 0x00, 0x08, 0x00, 0x01, 0x00, 0x71, 0x16, 0x00, 0x00,
327     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00, 0x08, 0x00, 0x06, 0x00,
328     0x98, 0x08, 0x00, 0x00, 0x1c, 0x00, 0x15, 0x00, 0x08, 0x00, 0x01, 0x00,
329     0x85, 0x16, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00,
330     0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00, 0x1c, 0x00, 0x16, 0x00,
331     0x08, 0x00, 0x01, 0x00, 0x99, 0x16, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00,
332     0x04, 0x00, 0x03, 0x00, 0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00,
333     0x1c, 0x00, 0x17, 0x00, 0x08, 0x00, 0x01, 0x00, 0xad, 0x16, 0x00, 0x00,
334     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00, 0x08, 0x00, 0x06, 0x00,
335     0x98, 0x08, 0x00, 0x00, 0x1c, 0x00, 0x18, 0x00, 0x08, 0x00, 0x01, 0x00,
336     0xc1, 0x16, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x03, 0x00,
337     0x08, 0x00, 0x06, 0x00, 0x98, 0x08, 0x00, 0x00, 0x64, 0x00, 0x02, 0x00,
338     0x0c, 0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00, 0x3c, 0x00, 0x00, 0x00,
339     0x0c, 0x00, 0x01, 0x00, 0x08, 0x00, 0x01, 0x00, 0x5a, 0x00, 0x00, 0x00,
340     0x0c, 0x00, 0x02, 0x00, 0x08, 0x00, 0x01, 0x00, 0x78, 0x00, 0x00, 0x00,
341     0x0c, 0x00, 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0xb4, 0x00, 0x00, 0x00,
342     0x0c, 0x00, 0x04, 0x00, 0x08, 0x00, 0x01, 0x00, 0xf0, 0x00, 0x00, 0x00,
343     0x0c, 0x00, 0x05, 0x00, 0x08, 0x00, 0x01, 0x00, 0x68, 0x01, 0x00, 0x00,
344     0x0c, 0x00, 0x06, 0x00, 0x08, 0x00, 0x01, 0x00, 0xe0, 0x01, 0x00, 0x00,
345     0x0c, 0x00, 0x07, 0x00, 0x08, 0x00, 0x01, 0x00, 0x1c, 0x02, 0x00, 0x00,
346     0xdc, 0x00, 0x32, 0x00, 0x08, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00, 0x00,
347     0x08, 0x00, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
348     0x0b, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, 0x0f, 0x00, 0x00, 0x00,
349     0x08, 0x00, 0x05, 0x00, 0x13, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
350     0x19, 0x00, 0x00, 0x00, 0x08, 0x00, 0x07, 0x00, 0x25, 0x00, 0x00, 0x00,
351     0x08, 0x00, 0x08, 0x00, 0x26, 0x00, 0x00, 0x00, 0x08, 0x00, 0x09, 0x00,
352     0x27, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0a, 0x00, 0x28, 0x00, 0x00, 0x00,
353     0x08, 0x00, 0x0b, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0c, 0x00,
354     0x37, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0d, 0x00, 0x39, 0x00, 0x00, 0x00,
355     0x08, 0x00, 0x0e, 0x00, 0x3b, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
356     0x43, 0x00, 0x00, 0x00, 0x08, 0x00, 0x10, 0x00, 0x31, 0x00, 0x00, 0x00,
357     0x08, 0x00, 0x11, 0x00, 0x41, 0x00, 0x00, 0x00, 0x08, 0x00, 0x12, 0x00,
358     0x42, 0x00, 0x00, 0x00, 0x08, 0x00, 0x13, 0x00, 0x4b, 0x00, 0x00, 0x00,
359     0x08, 0x00, 0x14, 0x00, 0x54, 0x00, 0x00, 0x00, 0x08, 0x00, 0x15, 0x00,
360     0x57, 0x00, 0x00, 0x00, 0x08, 0x00, 0x16, 0x00, 0x55, 0x00, 0x00, 0x00,
361     0x08, 0x00, 0x17, 0x00, 0x59, 0x00, 0x00, 0x00, 0x08, 0x00, 0x18, 0x00,
362     0x5c, 0x00, 0x00, 0x00, 0x08, 0x00, 0x19, 0x00, 0x2d, 0x00, 0x00, 0x00,
363     0x08, 0x00, 0x1a, 0x00, 0x2e, 0x00, 0x00, 0x00, 0x08, 0x00, 0x1b, 0x00,
364     0x30, 0x00, 0x00, 0x00, 0x08, 0x00, 0x6f, 0x00, 0x10, 0x27, 0x00, 0x00,
365     0x04, 0x00, 0x6c, 0x00, 0x30, 0x04, 0x63, 0x00, 0x04, 0x00, 0x00, 0x00,
366     0x84, 0x00, 0x01, 0x00, 0x06, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00,
367     0x06, 0x00, 0x65, 0x00, 0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
368     0x20, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x30, 0x00, 0x00, 0x00,
369     0x06, 0x00, 0x65, 0x00, 0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
370     0x50, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x60, 0x00, 0x00, 0x00,
371     0x06, 0x00, 0x65, 0x00, 0x70, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
372     0x80, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x90, 0x00, 0x00, 0x00,
373     0x06, 0x00, 0x65, 0x00, 0xa0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
374     0xb0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xc0, 0x00, 0x00, 0x00,
375     0x06, 0x00, 0x65, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
376     0xe0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xf0, 0x00, 0x00, 0x00,
377     0x84, 0x00, 0x02, 0x00, 0x06, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00,
378     0x06, 0x00, 0x65, 0x00, 0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
379     0x20, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x30, 0x00, 0x00, 0x00,
380     0x06, 0x00, 0x65, 0x00, 0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
381     0x50, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x60, 0x00, 0x00, 0x00,
382     0x06, 0x00, 0x65, 0x00, 0x70, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
383     0x80, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x90, 0x00, 0x00, 0x00,
384     0x06, 0x00, 0x65, 0x00, 0xa0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
385     0xb0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xc0, 0x00, 0x00, 0x00,
386     0x06, 0x00, 0x65, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
387     0xe0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xf0, 0x00, 0x00, 0x00,
388     0x84, 0x00, 0x03, 0x00, 0x06, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00,
389     0x06, 0x00, 0x65, 0x00, 0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
390     0x20, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x30, 0x00, 0x00, 0x00,
391     0x06, 0x00, 0x65, 0x00, 0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
392     0x50, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x60, 0x00, 0x00, 0x00,
393     0x06, 0x00, 0x65, 0x00, 0x70, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
394     0x80, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x90, 0x00, 0x00, 0x00,
395     0x06, 0x00, 0x65, 0x00, 0xa0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
396     0xb0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xc0, 0x00, 0x00, 0x00,
397     0x06, 0x00, 0x65, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
398     0xe0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xf0, 0x00, 0x00, 0x00,
399     0x84, 0x00, 0x04, 0x00, 0x06, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00,
400     0x06, 0x00, 0x65, 0x00, 0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
401     0x20, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x30, 0x00, 0x00, 0x00,
402     0x06, 0x00, 0x65, 0x00, 0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
403     0x50, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x60, 0x00, 0x00, 0x00,
404     0x06, 0x00, 0x65, 0x00, 0x70, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
405     0x80, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x90, 0x00, 0x00, 0x00,
406     0x06, 0x00, 0x65, 0x00, 0xa0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
407     0xb0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xc0, 0x00, 0x00, 0x00,
408     0x06, 0x00, 0x65, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
409     0xe0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xf0, 0x00, 0x00, 0x00,
410     0x04, 0x00, 0x05, 0x00, 0x04, 0x00, 0x06, 0x00, 0x84, 0x00, 0x07, 0x00,
411     0x06, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
412     0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x20, 0x00, 0x00, 0x00,
413     0x06, 0x00, 0x65, 0x00, 0x30, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
414     0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x50, 0x00, 0x00, 0x00,
415     0x06, 0x00, 0x65, 0x00, 0x60, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
416     0x70, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x80, 0x00, 0x00, 0x00,
417     0x06, 0x00, 0x65, 0x00, 0x90, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
418     0xa0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xb0, 0x00, 0x00, 0x00,
419     0x06, 0x00, 0x65, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
420     0xd0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xe0, 0x00, 0x00, 0x00,
421     0x06, 0x00, 0x65, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x84, 0x00, 0x08, 0x00,
422     0x06, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
423     0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x20, 0x00, 0x00, 0x00,
424     0x06, 0x00, 0x65, 0x00, 0x30, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
425     0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x50, 0x00, 0x00, 0x00,
426     0x06, 0x00, 0x65, 0x00, 0x60, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
427     0x70, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x80, 0x00, 0x00, 0x00,
428     0x06, 0x00, 0x65, 0x00, 0x90, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
429     0xa0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xb0, 0x00, 0x00, 0x00,
430     0x06, 0x00, 0x65, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
431     0xd0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xe0, 0x00, 0x00, 0x00,
432     0x06, 0x00, 0x65, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x84, 0x00, 0x09, 0x00,
433     0x06, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
434     0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x20, 0x00, 0x00, 0x00,
435     0x06, 0x00, 0x65, 0x00, 0x30, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
436     0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x50, 0x00, 0x00, 0x00,
437     0x06, 0x00, 0x65, 0x00, 0x60, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
438     0x70, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x80, 0x00, 0x00, 0x00,
439     0x06, 0x00, 0x65, 0x00, 0x90, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
440     0xa0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xb0, 0x00, 0x00, 0x00,
441     0x06, 0x00, 0x65, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
442     0xd0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xe0, 0x00, 0x00, 0x00,
443     0x06, 0x00, 0x65, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x84, 0x00, 0x0a, 0x00,
444     0x06, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
445     0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x20, 0x00, 0x00, 0x00,
446     0x06, 0x00, 0x65, 0x00, 0x30, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
447     0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x50, 0x00, 0x00, 0x00,
448     0x06, 0x00, 0x65, 0x00, 0x60, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
449     0x70, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x80, 0x00, 0x00, 0x00,
450     0x06, 0x00, 0x65, 0x00, 0x90, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
451     0xa0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xb0, 0x00, 0x00, 0x00,
452     0x06, 0x00, 0x65, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
453     0xd0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xe0, 0x00, 0x00, 0x00,
454     0x06, 0x00, 0x65, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x40, 0x01, 0x64, 0x00,
455     0x04, 0x00, 0x00, 0x00, 0x24, 0x00, 0x01, 0x00, 0x06, 0x00, 0x65, 0x00,
456     0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xb0, 0x00, 0x00, 0x00,
457     0x06, 0x00, 0x65, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
458     0xd0, 0x00, 0x00, 0x00, 0x14, 0x00, 0x02, 0x00, 0x06, 0x00, 0x65, 0x00,
459     0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xd0, 0x00, 0x00, 0x00,
460     0x3c, 0x00, 0x03, 0x00, 0x06, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00,
461     0x06, 0x00, 0x65, 0x00, 0x20, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
462     0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xa0, 0x00, 0x00, 0x00,
463     0x06, 0x00, 0x65, 0x00, 0xb0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
464     0xc0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xd0, 0x00, 0x00, 0x00,
465     0x3c, 0x00, 0x04, 0x00, 0x06, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00,
466     0x06, 0x00, 0x65, 0x00, 0x20, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
467     0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xa0, 0x00, 0x00, 0x00,
468     0x06, 0x00, 0x65, 0x00, 0xb0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
469     0xc0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xd0, 0x00, 0x00, 0x00,
470     0x04, 0x00, 0x05, 0x00, 0x04, 0x00, 0x06, 0x00, 0x1c, 0x00, 0x07, 0x00,
471     0x06, 0x00, 0x65, 0x00, 0xb0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
472     0xc0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xd0, 0x00, 0x00, 0x00,
473     0x14, 0x00, 0x08, 0x00, 0x06, 0x00, 0x65, 0x00, 0x40, 0x00, 0x00, 0x00,
474     0x06, 0x00, 0x65, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x09, 0x00,
475     0x06, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
476     0x20, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0x40, 0x00, 0x00, 0x00,
477     0x06, 0x00, 0x65, 0x00, 0xa0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
478     0xb0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00, 0xc0, 0x00, 0x00, 0x00,
479     0x06, 0x00, 0x65, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x14, 0x00, 0x0a, 0x00,
480     0x06, 0x00, 0x65, 0x00, 0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x65, 0x00,
481     0xd0, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x76, 0x00, 0x04, 0x00, 0x02, 0x00,
482     0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x04, 0x00, 0x06, 0x00,
483     0x04, 0x00, 0x07, 0x00, 0x04, 0x00, 0x08, 0x00, 0x04, 0x00, 0x09, 0x00,
484     0x14, 0x00, 0x04, 0x00, 0x14, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
485     0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x12, 0x00,
486     0x0b, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x79, 0x00, 0x04, 0x00, 0x04, 0x00,
487     0x04, 0x00, 0x06, 0x00, 0x60, 0x00, 0x78, 0x00, 0x5c, 0x00, 0x01, 0x00,
488     0x48, 0x00, 0x01, 0x00, 0x14, 0x00, 0x01, 0x00, 0x08, 0x00, 0x01, 0x00,
489     0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 0x00, 0x04, 0x00, 0x02, 0x00,
490     0x1c, 0x00, 0x02, 0x00, 0x08, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
491     0x10, 0x00, 0x02, 0x00, 0x04, 0x00, 0x03, 0x00, 0x04, 0x00, 0x08, 0x00,
492     0x04, 0x00, 0x09, 0x00, 0x14, 0x00, 0x03, 0x00, 0x08, 0x00, 0x01, 0x00,
493     0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 0x00, 0x04, 0x00, 0x0a, 0x00,
494     0x08, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 0x00,
495     0x03, 0x00, 0x00, 0x00, 0x08, 0x00, 0x8f, 0x00, 0xe3, 0x1a, 0x00, 0x07,
496     0x1e, 0x00, 0x94, 0x00, 0x63, 0x48, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff,
497     0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
498     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0xa9, 0x00,
499     0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x0c, 0x00, 0xaa, 0x00,
500     0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40};
501 const char kIPV4Address0[] = "192.168.10.20";
502 const char kIPV4Address1[] = "1.2.3.4";
503 const char kIPV6Address0[] = "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210";
504 const char kIPV6Address1[] = "1080:0:0:0:8:800:200C:417A";
505 
506 #if !defined(DISABLE_WAKE_ON_WIFI)
507 
508 // Zero-byte pattern prefixes to match the offsetting bytes in the Ethernet
509 // frame that lie before the source IP address field.
510 const uint8_t kIPV4PatternPrefix[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
511                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
512                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
513                                       0x00, 0x00, 0x00, 0x00, 0x00};
514 const uint8_t kIPV6PatternPrefix[] = {
515     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
516     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
517 
518 // These masks have bits set to 1 to match bytes in an IP address pattern that
519 // represent the source IP address of the frame. They are padded with zero
520 // bits in front to ignore the frame offset and at the end to byte-align the
521 // mask itself.
522 const uint8_t kIPV4MaskBytes[] = {0x00, 0x00, 0x00, 0x3c};
523 const uint8_t kIPV6MaskBytes[] = {0x00, 0x00, 0xc0, 0xff, 0x3f};
524 
525 const uint8_t kIPV4Address0Bytes[] = {0xc0, 0xa8, 0x0a, 0x14};
526 const uint8_t kIPV4Address1Bytes[] = {0x01, 0x02, 0x03, 0x04};
527 
528 const uint8_t kIPV6Address0Bytes[] = {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
529                                       0x32, 0x10, 0xfe, 0xdc, 0xba, 0x98,
530                                       0x76, 0x54, 0x32, 0x10};
531 const uint8_t kIPV6Address1Bytes[] = {0x10, 0x80, 0x00, 0x00, 0x00, 0x00,
532                                       0x00, 0x00, 0x00, 0x08, 0x08, 0x00,
533                                       0x20, 0x0c, 0x41, 0x7a};
534 const char kIPV6Address2[] = "1080::8:800:200C:417A";
535 const uint8_t kIPV6Address2Bytes[] = {0x10, 0x80, 0x00, 0x00, 0x00, 0x00,
536                                       0x00, 0x00, 0x00, 0x08, 0x08, 0x00,
537                                       0x20, 0x0c, 0x41, 0x7a};
538 const char kIPV6Address3[] = "FF01::101";
539 const uint8_t kIPV6Address3Bytes[] = {0xff, 0x01, 0x00, 0x00, 0x00, 0x00,
540                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
541                                       0x00, 0x00, 0x01, 0x01};
542 const char kIPV6Address4[] = "::1";
543 const uint8_t kIPV6Address4Bytes[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
544                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
545                                       0x00, 0x00, 0x00, 0x01};
546 const char kIPV6Address5[] = "::";
547 const uint8_t kIPV6Address5Bytes[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
548                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
549                                       0x00, 0x00, 0x00, 0x00};
550 const char kIPV6Address6[] = "0:0:0:0:0:FFFF:129.144.52.38";
551 const uint8_t kIPV6Address6Bytes[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
552                                       0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
553                                       0x81, 0x90, 0x34, 0x26};
554 const char kIPV6Address7[] = "::DEDE:190.144.52.38";
555 const uint8_t kIPV6Address7Bytes[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
556                                       0x00, 0x00, 0x00, 0x00, 0xde, 0xde,
557                                       0xbe, 0x90, 0x34, 0x26};
558 
559 const uint32_t kNewWiphyNlMsg_MaxPatterns = 20;
560 const uint32_t kNewWiphyNlMsg_MaxSSIDs = 11;
561 const int kNewWiphyNlMsg_PattSupportOffset = 3300;
562 const int kNewWiphyNlMsg_WowlanTrigNetDetectAttributeOffset = 3316;
563 const int kNewWiphyNlMsg_WowlanTrigDisconnectAttributeOffset = 3268;
564 
565 const uint32_t kSSID1FreqMatches[] = {2412, 2437, 2462, 5180,
566                                       5240, 5745, 5805, 5825};
567 
568 const uint32_t kWakeReasonNlMsg_WiphyIndex = 0;
569 // NL80211_CMD_GET_WOWLAN message with nlmsg_type 0x16, which is different from
570 // kNl80211FamilyId (0x13).
571 const uint8_t kWrongMessageTypeNlMsg[] = {
572     0x14, 0x00, 0x00, 0x00, 0x16, 0x00, 0x01, 0x00, 0x01, 0x00,
573     0x00, 0x00, 0x57, 0x40, 0x00, 0x00, 0x49, 0x01, 0x00, 0x00};
574 // Bytes representing a NL80211_CMD_SET_WOWLAN reporting that the system woke
575 // up because of a reason other than wake on WiFi.
576 const uint8_t kWakeReasonUnsupportedNlMsg[] = {
577     0x30, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
578     0x00, 0x00, 0x00, 0x00, 0x4a, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
579     0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x99, 0x00, 0x01, 0x00, 0x00, 0x00,
580     0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00};
581 // Bytes representing a NL80211_CMD_SET_WOWLAN reporting that the system woke
582 // up because of a disconnect.
583 const uint8_t kWakeReasonDisconnectNlMsg[] = {
584     0x38, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
585     0x00, 0x00, 0x00, 0x00, 0x4a, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
586     0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x99, 0x00, 0x01, 0x00, 0x00, 0x00,
587     0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00,
588     0x08, 0x00, 0x75, 0x00, 0x04, 0x00, 0x02, 0x00};
589 // Bytes representing a NL80211_CMD_SET_WOWLAN reporting that the system woke
590 // up because of a a match with packet pattern index
591 // kWakeReasonPatternNlMsg_PattIndex.
592 const uint8_t kWakeReasonPatternNlMsg[] = {
593     0xac, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
594     0x00, 0x00, 0x00, 0x00, 0x4a, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
595     0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x99, 0x00, 0x01, 0x00, 0x00, 0x00,
596     0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00,
597     0x7c, 0x00, 0x75, 0x00, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
598     0x08, 0x00, 0x0d, 0x00, 0x62, 0x00, 0x00, 0x00, 0x66, 0x00, 0x0c, 0x00,
599     0x6c, 0x29, 0x95, 0x16, 0x54, 0x68, 0x6c, 0x71, 0xd9, 0x8b, 0x3c, 0x6c,
600     0x08, 0x00, 0x45, 0x00, 0x00, 0x54, 0x00, 0x00, 0x40, 0x00, 0x40, 0x01,
601     0xb7, 0xdd, 0xc0, 0xa8, 0x00, 0xfe, 0xc0, 0xa8, 0x00, 0x7d, 0x08, 0x00,
602     0x3f, 0x51, 0x28, 0x64, 0x00, 0x01, 0xb1, 0x0b, 0xd0, 0x54, 0x00, 0x00,
603     0x00, 0x00, 0x4b, 0x16, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11,
604     0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
605     0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
606     0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
607     0x36, 0x37, 0x00, 0x00};
608 const uint32_t kWakeReasonPatternNlMsg_PattIndex = 0;
609 
610 #endif  // DISABLE_WAKE_ON_WIFI
611 
612 }  // namespace
613 
614 class WakeOnWiFiTest : public ::testing::Test {
615  public:
WakeOnWiFiTest()616   WakeOnWiFiTest() : metrics_(nullptr) {}
~WakeOnWiFiTest()617   virtual ~WakeOnWiFiTest() {}
618 
SetUp()619   virtual void SetUp() {
620     Nl80211Message::SetMessageType(kNl80211FamilyId);
621     // Assume our NIC has reported its wiphy index, and that it supports wake
622     // all wake triggers.
623     wake_on_wifi_->wiphy_index_received_ = true;
624     wake_on_wifi_->wake_on_wifi_triggers_supported_.insert(
625         WakeOnWiFi::kWakeTriggerPattern);
626     wake_on_wifi_->wake_on_wifi_triggers_supported_.insert(
627         WakeOnWiFi::kWakeTriggerDisconnect);
628     wake_on_wifi_->wake_on_wifi_triggers_supported_.insert(
629         WakeOnWiFi::kWakeTriggerSSID);
630     // By default our tests assume that the NIC supports more SSIDs than
631     // whitelisted SSIDs.
632     wake_on_wifi_->wake_on_wifi_max_ssids_ = 999;
633     wake_on_wifi_->dark_resume_history_.time_ = &time_;
634 
635     ON_CALL(netlink_manager_, SendNl80211Message(_, _, _, _))
636         .WillByDefault(Return(true));
637   }
638 
SetWakeOnWiFiMaxSSIDs(uint32_t max_ssids)639   void SetWakeOnWiFiMaxSSIDs(uint32_t max_ssids) {
640     wake_on_wifi_->wake_on_wifi_max_ssids_ = max_ssids;
641   }
642 
EnableWakeOnWiFiFeaturesPacket()643   void EnableWakeOnWiFiFeaturesPacket() {
644     wake_on_wifi_->wake_on_wifi_features_enabled_ =
645         kWakeOnWiFiFeaturesEnabledPacket;
646   }
647 
EnableWakeOnWiFiFeaturesDarkConnect()648   void EnableWakeOnWiFiFeaturesDarkConnect() {
649     wake_on_wifi_->wake_on_wifi_features_enabled_ =
650         kWakeOnWiFiFeaturesEnabledDarkConnect;
651   }
652 
EnableWakeOnWiFiFeaturesPacketDarkConnect()653   void EnableWakeOnWiFiFeaturesPacketDarkConnect() {
654     wake_on_wifi_->wake_on_wifi_features_enabled_ =
655         kWakeOnWiFiFeaturesEnabledPacketDarkConnect;
656   }
657 
SetWakeOnWiFiFeaturesNotSupported()658   void SetWakeOnWiFiFeaturesNotSupported() {
659     wake_on_wifi_->wake_on_wifi_features_enabled_ =
660         kWakeOnWiFiFeaturesEnabledNotSupported;
661   }
662 
DisableWakeOnWiFiFeatures()663   void DisableWakeOnWiFiFeatures() {
664     wake_on_wifi_->wake_on_wifi_features_enabled_ =
665         kWakeOnWiFiFeaturesEnabledNone;
666   }
667 
AddWakeOnPacketConnection(const string & ip_endpoint,Error * error)668   void AddWakeOnPacketConnection(const string& ip_endpoint, Error* error) {
669     wake_on_wifi_->AddWakeOnPacketConnection(ip_endpoint, error);
670   }
671 
RemoveWakeOnPacketConnection(const string & ip_endpoint,Error * error)672   void RemoveWakeOnPacketConnection(const string& ip_endpoint, Error* error) {
673     wake_on_wifi_->RemoveWakeOnPacketConnection(ip_endpoint, error);
674   }
675 
RemoveAllWakeOnPacketConnections(Error * error)676   void RemoveAllWakeOnPacketConnections(Error* error) {
677     wake_on_wifi_->RemoveAllWakeOnPacketConnections(error);
678   }
679 
CreateIPAddressPatternAndMask(const IPAddress & ip_addr,ByteString * pattern,ByteString * mask)680   bool CreateIPAddressPatternAndMask(const IPAddress& ip_addr,
681                                      ByteString* pattern, ByteString* mask) {
682     return WakeOnWiFi::CreateIPAddressPatternAndMask(ip_addr, pattern, mask);
683   }
684 
ConfigureWiphyIndex(Nl80211Message * msg,int32_t index)685   bool ConfigureWiphyIndex(Nl80211Message* msg, int32_t index) {
686     return WakeOnWiFi::ConfigureWiphyIndex(msg, index);
687   }
688 
ConfigureDisableWakeOnWiFiMessage(SetWakeOnPacketConnMessage * msg,uint32_t wiphy_index,Error * error)689   bool ConfigureDisableWakeOnWiFiMessage(SetWakeOnPacketConnMessage* msg,
690                                          uint32_t wiphy_index, Error* error) {
691     return WakeOnWiFi::ConfigureDisableWakeOnWiFiMessage(msg, wiphy_index,
692                                                          error);
693   }
694 
WakeOnWiFiSettingsMatch(const Nl80211Message & msg,const set<WakeOnWiFi::WakeOnWiFiTrigger> & trigs,const IPAddressStore & addrs,uint32_t net_detect_scan_period_seconds,const vector<ByteString> & ssid_whitelist)695   bool WakeOnWiFiSettingsMatch(const Nl80211Message& msg,
696                                const set<WakeOnWiFi::WakeOnWiFiTrigger>& trigs,
697                                const IPAddressStore& addrs,
698                                uint32_t net_detect_scan_period_seconds,
699                                const vector<ByteString>& ssid_whitelist) {
700     return WakeOnWiFi::WakeOnWiFiSettingsMatch(
701         msg, trigs, addrs, net_detect_scan_period_seconds, ssid_whitelist);
702   }
703 
ConfigureSetWakeOnWiFiSettingsMessage(SetWakeOnPacketConnMessage * msg,const set<WakeOnWiFi::WakeOnWiFiTrigger> & trigs,const IPAddressStore & addrs,uint32_t wiphy_index,uint32_t net_detect_scan_period_seconds,const vector<ByteString> & ssid_whitelist,Error * error)704   bool ConfigureSetWakeOnWiFiSettingsMessage(
705       SetWakeOnPacketConnMessage* msg,
706       const set<WakeOnWiFi::WakeOnWiFiTrigger>& trigs,
707       const IPAddressStore& addrs, uint32_t wiphy_index,
708       uint32_t net_detect_scan_period_seconds,
709       const vector<ByteString>& ssid_whitelist, Error* error) {
710     return WakeOnWiFi::ConfigureSetWakeOnWiFiSettingsMessage(
711         msg, trigs, addrs, wiphy_index, net_detect_scan_period_seconds,
712         ssid_whitelist, error);
713   }
714 
RequestWakeOnPacketSettings()715   void RequestWakeOnPacketSettings() {
716     wake_on_wifi_->RequestWakeOnPacketSettings();
717   }
718 
VerifyWakeOnWiFiSettings(const Nl80211Message & nl80211_message)719   void VerifyWakeOnWiFiSettings(const Nl80211Message& nl80211_message) {
720     wake_on_wifi_->VerifyWakeOnWiFiSettings(nl80211_message);
721   }
722 
GetWakeOnWiFiMaxPatterns()723   size_t GetWakeOnWiFiMaxPatterns() {
724     return wake_on_wifi_->wake_on_wifi_max_patterns_;
725   }
726 
GetWakeOnWiFiMaxSSIDs()727   uint32_t GetWakeOnWiFiMaxSSIDs() {
728     return wake_on_wifi_->wake_on_wifi_max_ssids_;
729   }
730 
SetWakeOnWiFiMaxPatterns(size_t max_patterns)731   void SetWakeOnWiFiMaxPatterns(size_t max_patterns) {
732     wake_on_wifi_->wake_on_wifi_max_patterns_ = max_patterns;
733   }
734 
ApplyWakeOnWiFiSettings()735   void ApplyWakeOnWiFiSettings() { wake_on_wifi_->ApplyWakeOnWiFiSettings(); }
736 
DisableWakeOnWiFi()737   void DisableWakeOnWiFi() { wake_on_wifi_->DisableWakeOnWiFi(); }
738 
GetWakeOnWiFiTriggers()739   set<WakeOnWiFi::WakeOnWiFiTrigger>* GetWakeOnWiFiTriggers() {
740     return &wake_on_wifi_->wake_on_wifi_triggers_;
741   }
742 
GetWakeOnWiFiTriggersSupported()743   set<WakeOnWiFi::WakeOnWiFiTrigger>* GetWakeOnWiFiTriggersSupported() {
744     return &wake_on_wifi_->wake_on_wifi_triggers_supported_;
745   }
746 
ClearWakeOnWiFiTriggersSupported()747   void ClearWakeOnWiFiTriggersSupported() {
748     wake_on_wifi_->wake_on_wifi_triggers_supported_.clear();
749   }
750 
GetWakeOnPacketConnections()751   IPAddressStore* GetWakeOnPacketConnections() {
752     return &wake_on_wifi_->wake_on_packet_connections_;
753   }
754 
RetrySetWakeOnPacketConnections()755   void RetrySetWakeOnPacketConnections() {
756     wake_on_wifi_->RetrySetWakeOnPacketConnections();
757   }
758 
SetSuspendActionsDoneCallback()759   void SetSuspendActionsDoneCallback() {
760     wake_on_wifi_->suspend_actions_done_callback_ =
761         Bind(&WakeOnWiFiTest::DoneCallback, Unretained(this));
762   }
763 
ResetSuspendActionsDoneCallback()764   void ResetSuspendActionsDoneCallback() {
765     wake_on_wifi_->suspend_actions_done_callback_.Reset();
766   }
767 
SuspendActionsCallbackIsNull()768   bool SuspendActionsCallbackIsNull() {
769     return wake_on_wifi_->suspend_actions_done_callback_.is_null();
770   }
771 
RunSuspendActionsCallback(const Error & error)772   void RunSuspendActionsCallback(const Error& error) {
773     wake_on_wifi_->suspend_actions_done_callback_.Run(error);
774   }
775 
GetNumSetWakeOnPacketRetries()776   int GetNumSetWakeOnPacketRetries() {
777     return wake_on_wifi_->num_set_wake_on_packet_retries_;
778   }
779 
SetNumSetWakeOnPacketRetries(int retries)780   void SetNumSetWakeOnPacketRetries(int retries) {
781     wake_on_wifi_->num_set_wake_on_packet_retries_ = retries;
782   }
783 
OnBeforeSuspend(bool is_connected,const vector<ByteString> & ssid_whitelist,bool have_dhcp_lease,uint32_t time_to_next_lease_renewal)784   void OnBeforeSuspend(bool is_connected,
785                        const vector<ByteString>& ssid_whitelist,
786                        bool have_dhcp_lease,
787                        uint32_t time_to_next_lease_renewal) {
788     ResultCallback done_callback(
789         Bind(&WakeOnWiFiTest::DoneCallback, Unretained(this)));
790     Closure renew_dhcp_lease_callback(
791         Bind(&WakeOnWiFiTest::RenewDHCPLeaseCallback, Unretained(this)));
792     Closure remove_supplicant_networks_callback(Bind(
793         &WakeOnWiFiTest::RemoveSupplicantNetworksCallback, Unretained(this)));
794     wake_on_wifi_->OnBeforeSuspend(is_connected, ssid_whitelist, done_callback,
795                                    renew_dhcp_lease_callback,
796                                    remove_supplicant_networks_callback,
797                                    have_dhcp_lease, time_to_next_lease_renewal);
798   }
799 
OnDarkResume(bool is_connected,const vector<ByteString> & ssid_whitelist)800   void OnDarkResume(bool is_connected,
801                     const vector<ByteString>& ssid_whitelist) {
802     ResultCallback done_callback(
803         Bind(&WakeOnWiFiTest::DoneCallback, Unretained(this)));
804     Closure renew_dhcp_lease_callback(
805         Bind(&WakeOnWiFiTest::RenewDHCPLeaseCallback, Unretained(this)));
806     WakeOnWiFi::InitiateScanCallback initiate_scan_callback(
807         Bind(&WakeOnWiFiTest::InitiateScanCallback, Unretained(this)));
808     Closure remove_supplicant_networks_callback(Bind(
809         &WakeOnWiFiTest::RemoveSupplicantNetworksCallback, Unretained(this)));
810     wake_on_wifi_->OnDarkResume(
811         is_connected, ssid_whitelist, done_callback, renew_dhcp_lease_callback,
812         initiate_scan_callback, remove_supplicant_networks_callback);
813   }
814 
OnAfterResume()815   void OnAfterResume() { wake_on_wifi_->OnAfterResume(); }
816 
BeforeSuspendActions(bool is_connected,bool start_lease_renewal_timer,uint32_t time_to_next_lease_renewal)817   void BeforeSuspendActions(bool is_connected, bool start_lease_renewal_timer,
818                             uint32_t time_to_next_lease_renewal) {
819     SetDarkResumeActionsTimeOutCallback();
820     EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
821     EXPECT_CALL(metrics_,
822                 NotifyBeforeSuspendActions(is_connected, GetInDarkResume()));
823     Closure remove_supplicant_networks_callback(Bind(
824         &WakeOnWiFiTest::RemoveSupplicantNetworksCallback, Unretained(this)));
825     wake_on_wifi_->BeforeSuspendActions(is_connected, start_lease_renewal_timer,
826                                         time_to_next_lease_renewal,
827                                         remove_supplicant_networks_callback);
828     EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
829   }
830 
OnConnectedAndReachable(bool start_lease_renewal_timer,uint32_t time_to_next_lease_renewal)831   void OnConnectedAndReachable(bool start_lease_renewal_timer,
832                                uint32_t time_to_next_lease_renewal) {
833     wake_on_wifi_->OnConnectedAndReachable(start_lease_renewal_timer,
834                                            time_to_next_lease_renewal);
835   }
836 
SetInDarkResume(bool val)837   void SetInDarkResume(bool val) { wake_on_wifi_->in_dark_resume_ = val; }
838 
GetInDarkResume()839   bool GetInDarkResume() { return wake_on_wifi_->in_dark_resume_; }
840 
SetWiphyIndexReceivedToFalse()841   void SetWiphyIndexReceivedToFalse() {
842     wake_on_wifi_->wiphy_index_received_ = false;
843   }
844 
SetWiphyIndex(uint32_t wiphy_index)845   void SetWiphyIndex(uint32_t wiphy_index) {
846     wake_on_wifi_->wiphy_index_ = wiphy_index;
847   }
848 
ParseWakeOnWiFiCapabilities(const Nl80211Message & nl80211_message)849   void ParseWakeOnWiFiCapabilities(const Nl80211Message& nl80211_message) {
850     wake_on_wifi_->ParseWakeOnWiFiCapabilities(nl80211_message);
851   }
852 
SetWakeOnWiFiFeaturesEnabled(const std::string & enabled,Error * error)853   bool SetWakeOnWiFiFeaturesEnabled(const std::string& enabled, Error* error) {
854     return wake_on_wifi_->SetWakeOnWiFiFeaturesEnabled(enabled, error);
855   }
856 
GetWakeOnWiFiFeaturesEnabled()857   const string& GetWakeOnWiFiFeaturesEnabled() {
858     return wake_on_wifi_->wake_on_wifi_features_enabled_;
859   }
860 
SetDarkResumeActionsTimeOutCallback()861   void SetDarkResumeActionsTimeOutCallback() {
862     wake_on_wifi_->dark_resume_actions_timeout_callback_.Reset(Bind(
863         &WakeOnWiFiTest::DarkResumeActionsTimeoutCallback, Unretained(this)));
864   }
865 
DarkResumeActionsTimeOutCallbackIsCancelled()866   bool DarkResumeActionsTimeOutCallbackIsCancelled() {
867     return wake_on_wifi_->dark_resume_actions_timeout_callback_.IsCancelled();
868   }
869 
StartDHCPLeaseRenewalTimer()870   void StartDHCPLeaseRenewalTimer() {
871     wake_on_wifi_->dhcp_lease_renewal_timer_.Start(
872         FROM_HERE, base::TimeDelta::FromSeconds(kTimeToNextLeaseRenewalLong),
873         Bind(&WakeOnWiFiTest::OnTimerWakeDoNothing, Unretained(this)));
874   }
875 
StartWakeToScanTimer()876   void StartWakeToScanTimer() {
877     wake_on_wifi_->wake_to_scan_timer_.Start(
878         FROM_HERE, base::TimeDelta::FromSeconds(kTimeToNextLeaseRenewalLong),
879         Bind(&WakeOnWiFiTest::OnTimerWakeDoNothing, Unretained(this)));
880   }
881 
StopDHCPLeaseRenewalTimer()882   void StopDHCPLeaseRenewalTimer() {
883     wake_on_wifi_->dhcp_lease_renewal_timer_.Stop();
884   }
885 
StopWakeToScanTimer()886   void StopWakeToScanTimer() { wake_on_wifi_->wake_to_scan_timer_.Stop(); }
887 
DHCPLeaseRenewalTimerIsRunning()888   bool DHCPLeaseRenewalTimerIsRunning() {
889     return wake_on_wifi_->dhcp_lease_renewal_timer_.IsRunning();
890   }
891 
WakeToScanTimerIsRunning()892   bool WakeToScanTimerIsRunning() {
893     return wake_on_wifi_->wake_to_scan_timer_.IsRunning();
894   }
895 
SetDarkResumeActionsTimeoutMilliseconds(int64_t timeout)896   void SetDarkResumeActionsTimeoutMilliseconds(int64_t timeout) {
897     wake_on_wifi_->DarkResumeActionsTimeoutMilliseconds = timeout;
898   }
899 
InitStateForDarkResume()900   void InitStateForDarkResume() {
901     SetInDarkResume(true);
902     GetWakeOnPacketConnections()->AddUnique(IPAddress("1.1.1.1"));
903     EnableWakeOnWiFiFeaturesPacketDarkConnect();
904     SetDarkResumeActionsTimeoutMilliseconds(0);
905   }
906 
SetExpectationsDisconnectedBeforeSuspend()907   void SetExpectationsDisconnectedBeforeSuspend() {
908     EXPECT_TRUE(GetWakeOnWiFiTriggers()->empty());
909     EXPECT_CALL(*this, DoneCallback(_)).Times(0);
910     EXPECT_CALL(*this, RemoveSupplicantNetworksCallback()).Times(1);
911     EXPECT_CALL(netlink_manager_,
912                 SendNl80211Message(
913                     IsNl80211Command(kNl80211FamilyId,
914                                      SetWakeOnPacketConnMessage::kCommand),
915                     _, _, _));
916   }
917 
SetExpectationsConnectedBeforeSuspend()918   void SetExpectationsConnectedBeforeSuspend() {
919     EXPECT_TRUE(GetWakeOnWiFiTriggers()->empty());
920     EXPECT_CALL(*this, DoneCallback(_)).Times(0);
921     EXPECT_CALL(netlink_manager_,
922                 SendNl80211Message(
923                     IsNl80211Command(kNl80211FamilyId,
924                                      SetWakeOnPacketConnMessage::kCommand),
925                     _, _, _));
926   }
927 
VerifyStateConnectedBeforeSuspend()928   void VerifyStateConnectedBeforeSuspend() {
929     EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
930     EXPECT_FALSE(GetInDarkResume());
931     EXPECT_EQ(GetWakeOnWiFiTriggers()->size(), 2);
932     EXPECT_TRUE(
933         GetWakeOnWiFiTriggers()->find(WakeOnWiFi::kWakeTriggerPattern) !=
934         GetWakeOnWiFiTriggers()->end());
935     EXPECT_TRUE(
936         GetWakeOnWiFiTriggers()->find(WakeOnWiFi::kWakeTriggerDisconnect) !=
937         GetWakeOnWiFiTriggers()->end());
938   }
939 
VerifyStateDisconnectedBeforeSuspend()940   void VerifyStateDisconnectedBeforeSuspend() {
941     EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
942     EXPECT_FALSE(GetInDarkResume());
943     EXPECT_EQ(GetWakeOnWiFiTriggers()->size(), 1);
944     EXPECT_FALSE(
945         GetWakeOnWiFiTriggers()->find(WakeOnWiFi::kWakeTriggerPattern) !=
946         GetWakeOnWiFiTriggers()->end());
947     EXPECT_TRUE(GetWakeOnWiFiTriggers()->find(WakeOnWiFi::kWakeTriggerSSID) !=
948                 GetWakeOnWiFiTriggers()->end());
949   }
950 
ReportConnectedToServiceAfterWake(bool is_connected)951   void ReportConnectedToServiceAfterWake(bool is_connected) {
952     wake_on_wifi_->ReportConnectedToServiceAfterWake(is_connected);
953   }
954 
OnNoAutoConnectableServicesAfterScan(const vector<ByteString> & ssid_whitelist)955   void OnNoAutoConnectableServicesAfterScan(
956       const vector<ByteString>& ssid_whitelist) {
957     Closure remove_supplicant_networks_callback(Bind(
958         &WakeOnWiFiTest::RemoveSupplicantNetworksCallback, Unretained(this)));
959     WakeOnWiFi::InitiateScanCallback initiate_scan_callback(
960         Bind(&WakeOnWiFiTest::InitiateScanCallback, Unretained(this)));
961     wake_on_wifi_->OnNoAutoConnectableServicesAfterScan(
962         ssid_whitelist, remove_supplicant_networks_callback,
963         initiate_scan_callback);
964   }
965 
GetDarkResumeHistory()966   EventHistory* GetDarkResumeHistory() {
967     return &wake_on_wifi_->dark_resume_history_;
968   }
969 
SetNetDetectScanPeriodSeconds(uint32_t period)970   void SetNetDetectScanPeriodSeconds(uint32_t period) {
971     wake_on_wifi_->net_detect_scan_period_seconds_ = period;
972   }
973 
AddSSIDToWhitelist(const uint8_t * ssid,int num_bytes,vector<ByteString> * whitelist)974   void AddSSIDToWhitelist(const uint8_t* ssid, int num_bytes,
975                           vector<ByteString>* whitelist) {
976     vector<uint8_t> ssid_vector(ssid, ssid + num_bytes);
977     whitelist->push_back(ByteString(ssid_vector));
978   }
979 
GetWakeOnSSIDWhitelist()980   vector<ByteString>* GetWakeOnSSIDWhitelist() {
981     return &wake_on_wifi_->wake_on_ssid_whitelist_;
982   }
983 
OnWakeupReasonReceived(const NetlinkMessage & netlink_message)984   void OnWakeupReasonReceived(const NetlinkMessage& netlink_message) {
985     wake_on_wifi_->OnWakeupReasonReceived(netlink_message);
986   }
987 
ParseWakeOnSSIDResults(AttributeListConstRefPtr results_list)988   WiFi::FreqSet ParseWakeOnSSIDResults(AttributeListConstRefPtr results_list) {
989     return wake_on_wifi_->ParseWakeOnSSIDResults(results_list);
990   }
991 
GetWakeupReportMsgContext()992   NetlinkMessage::MessageContext GetWakeupReportMsgContext() {
993     NetlinkMessage::MessageContext context;
994     context.nl80211_cmd = NL80211_CMD_SET_WOWLAN;
995     context.is_broadcast = true;
996     return context;
997   }
998 
SetLastWakeReason(WakeOnWiFi::WakeOnWiFiTrigger reason)999   void SetLastWakeReason(WakeOnWiFi::WakeOnWiFiTrigger reason) {
1000     wake_on_wifi_->last_wake_reason_ = reason;
1001   }
1002 
GetLastWakeReason()1003   WakeOnWiFi::WakeOnWiFiTrigger GetLastWakeReason() {
1004     return wake_on_wifi_->last_wake_reason_;
1005   }
1006 
OnScanStarted(bool is_active_scan)1007   void OnScanStarted(bool is_active_scan) {
1008     wake_on_wifi_->OnScanStarted(is_active_scan);
1009   }
1010 
GetLastSSIDMatchFreqs()1011   const WiFi::FreqSet& GetLastSSIDMatchFreqs() {
1012     return wake_on_wifi_->last_ssid_match_freqs_;
1013   }
1014 
AddResultToLastSSIDResults()1015   void AddResultToLastSSIDResults() {
1016     wake_on_wifi_->last_ssid_match_freqs_.insert(1);
1017   }
1018 
InitiateScanInDarkResume(const WiFi::FreqSet & freqs)1019   void InitiateScanInDarkResume(const WiFi::FreqSet& freqs) {
1020     wake_on_wifi_->InitiateScanInDarkResume(
1021         Bind(&WakeOnWiFiTest::InitiateScanCallback, Unretained(this)), freqs);
1022   }
1023 
GetDarkResumeScanRetriesLeft()1024   int GetDarkResumeScanRetriesLeft() {
1025     return wake_on_wifi_->dark_resume_scan_retries_left_;
1026   }
1027 
SetDarkResumeScanRetriesLeft(int retries)1028   void SetDarkResumeScanRetriesLeft(int retries) {
1029     wake_on_wifi_->dark_resume_scan_retries_left_ = retries;
1030   }
1031 
GetTimestampBootTime(int boottime_seconds)1032   Timestamp GetTimestampBootTime(int boottime_seconds) {
1033     struct timeval monotonic = {.tv_sec = 0, .tv_usec = 0};
1034     struct timeval boottime = {.tv_sec = boottime_seconds, .tv_usec = 0};
1035     return Timestamp(monotonic, boottime, "");
1036   }
1037 
1038   MOCK_METHOD1(DoneCallback, void(const Error &));
1039   MOCK_METHOD0(RenewDHCPLeaseCallback, void(void));
1040   MOCK_METHOD1(InitiateScanCallback, void(const WiFi::FreqSet&));
1041   MOCK_METHOD0(RemoveSupplicantNetworksCallback, void(void));
1042   MOCK_METHOD0(DarkResumeActionsTimeoutCallback, void(void));
1043   MOCK_METHOD0(OnTimerWakeDoNothing, void(void));
1044   MOCK_METHOD1(RecordDarkResumeWakeReasonCallback,
1045                void(const string& wake_reason));
1046 
1047  protected:
1048   NiceMockControl control_interface_;
1049   MockMetrics metrics_;
1050   MockNetlinkManager netlink_manager_;
1051   MockTime time_;
1052   std::unique_ptr<WakeOnWiFi> wake_on_wifi_;
1053 };
1054 
1055 class WakeOnWiFiTestWithDispatcher : public WakeOnWiFiTest {
1056  public:
WakeOnWiFiTestWithDispatcher()1057   WakeOnWiFiTestWithDispatcher() : WakeOnWiFiTest() {
1058     wake_on_wifi_.reset(
1059         new WakeOnWiFi(&netlink_manager_, &dispatcher_, &metrics_,
1060                        Bind(&WakeOnWiFiTest::RecordDarkResumeWakeReasonCallback,
1061                             Unretained(this))));
1062   }
~WakeOnWiFiTestWithDispatcher()1063   virtual ~WakeOnWiFiTestWithDispatcher() {}
1064 
1065  protected:
1066   EventDispatcherForTest dispatcher_;
1067 };
1068 
1069 class WakeOnWiFiTestWithMockDispatcher : public WakeOnWiFiTest {
1070  public:
WakeOnWiFiTestWithMockDispatcher()1071   WakeOnWiFiTestWithMockDispatcher() : WakeOnWiFiTest() {
1072     wake_on_wifi_.reset(
1073         new WakeOnWiFi(&netlink_manager_, &mock_dispatcher_, &metrics_,
1074                        Bind(&WakeOnWiFiTest::RecordDarkResumeWakeReasonCallback,
1075                             Unretained(this))));
1076   }
~WakeOnWiFiTestWithMockDispatcher()1077   virtual ~WakeOnWiFiTestWithMockDispatcher() {}
1078 
1079  protected:
1080   // TODO(zqiu): message loop is needed by AlarmTimer, should restructure the
1081   // code so that it can be mocked out.
1082   base::MessageLoopForIO message_loop_;
1083   MockEventDispatcher mock_dispatcher_;
1084 };
1085 
CreatePattern(const unsigned char * prefix,size_t prefix_len,const unsigned char * addr,size_t addr_len)1086 ByteString CreatePattern(const unsigned char* prefix, size_t prefix_len,
1087                          const unsigned char* addr, size_t addr_len) {
1088   ByteString result(prefix, prefix_len);
1089   result.Append(ByteString(addr, addr_len));
1090   return result;
1091 }
1092 
1093 #if !defined(DISABLE_WAKE_ON_WIFI)
1094 
TEST_F(WakeOnWiFiTestWithMockDispatcher,CreateIPAddressPatternAndMask)1095 TEST_F(WakeOnWiFiTestWithMockDispatcher, CreateIPAddressPatternAndMask) {
1096   ByteString pattern;
1097   ByteString mask;
1098   ByteString expected_pattern;
1099 
1100   CreateIPAddressPatternAndMask(IPAddress(kIPV4Address0), &pattern, &mask);
1101   expected_pattern =
1102       CreatePattern(kIPV4PatternPrefix, sizeof(kIPV4PatternPrefix),
1103                     kIPV4Address0Bytes, sizeof(kIPV4Address0Bytes));
1104   EXPECT_TRUE(pattern.Equals(expected_pattern));
1105   EXPECT_TRUE(mask.Equals(ByteString(kIPV4MaskBytes, sizeof(kIPV4MaskBytes))));
1106 
1107   pattern.Clear();
1108   expected_pattern.Clear();
1109   mask.Clear();
1110   CreateIPAddressPatternAndMask(IPAddress(kIPV4Address1), &pattern, &mask);
1111   expected_pattern =
1112       CreatePattern(kIPV4PatternPrefix, sizeof(kIPV4PatternPrefix),
1113                     kIPV4Address1Bytes, sizeof(kIPV4Address1Bytes));
1114   EXPECT_TRUE(pattern.Equals(expected_pattern));
1115   EXPECT_TRUE(mask.Equals(ByteString(kIPV4MaskBytes, sizeof(kIPV4MaskBytes))));
1116 
1117   pattern.Clear();
1118   expected_pattern.Clear();
1119   mask.Clear();
1120   CreateIPAddressPatternAndMask(IPAddress(kIPV6Address0), &pattern, &mask);
1121   expected_pattern =
1122       CreatePattern(kIPV6PatternPrefix, sizeof(kIPV6PatternPrefix),
1123                     kIPV6Address0Bytes, sizeof(kIPV6Address0Bytes));
1124   EXPECT_TRUE(pattern.Equals(expected_pattern));
1125   EXPECT_TRUE(mask.Equals(ByteString(kIPV6MaskBytes, sizeof(kIPV6MaskBytes))));
1126 
1127   pattern.Clear();
1128   expected_pattern.Clear();
1129   mask.Clear();
1130   CreateIPAddressPatternAndMask(IPAddress(kIPV6Address1), &pattern, &mask);
1131   expected_pattern =
1132       CreatePattern(kIPV6PatternPrefix, sizeof(kIPV6PatternPrefix),
1133                     kIPV6Address1Bytes, sizeof(kIPV6Address1Bytes));
1134   EXPECT_TRUE(pattern.Equals(expected_pattern));
1135   EXPECT_TRUE(mask.Equals(ByteString(kIPV6MaskBytes, sizeof(kIPV6MaskBytes))));
1136 
1137   pattern.Clear();
1138   expected_pattern.Clear();
1139   mask.Clear();
1140   CreateIPAddressPatternAndMask(IPAddress(kIPV6Address2), &pattern, &mask);
1141   expected_pattern =
1142       CreatePattern(kIPV6PatternPrefix, sizeof(kIPV6PatternPrefix),
1143                     kIPV6Address2Bytes, sizeof(kIPV6Address2Bytes));
1144   EXPECT_TRUE(pattern.Equals(expected_pattern));
1145   EXPECT_TRUE(mask.Equals(ByteString(kIPV6MaskBytes, sizeof(kIPV6MaskBytes))));
1146 
1147   pattern.Clear();
1148   expected_pattern.Clear();
1149   mask.Clear();
1150   CreateIPAddressPatternAndMask(IPAddress(kIPV6Address3), &pattern, &mask);
1151   expected_pattern =
1152       CreatePattern(kIPV6PatternPrefix, sizeof(kIPV6PatternPrefix),
1153                     kIPV6Address3Bytes, sizeof(kIPV6Address3Bytes));
1154   EXPECT_TRUE(pattern.Equals(expected_pattern));
1155   EXPECT_TRUE(mask.Equals(ByteString(kIPV6MaskBytes, sizeof(kIPV6MaskBytes))));
1156 
1157   pattern.Clear();
1158   expected_pattern.Clear();
1159   mask.Clear();
1160   CreateIPAddressPatternAndMask(IPAddress(kIPV6Address4), &pattern, &mask);
1161   expected_pattern =
1162       CreatePattern(kIPV6PatternPrefix, sizeof(kIPV6PatternPrefix),
1163                     kIPV6Address4Bytes, sizeof(kIPV6Address4Bytes));
1164   EXPECT_TRUE(pattern.Equals(expected_pattern));
1165   EXPECT_TRUE(mask.Equals(ByteString(kIPV6MaskBytes, sizeof(kIPV6MaskBytes))));
1166 
1167   pattern.Clear();
1168   expected_pattern.Clear();
1169   mask.Clear();
1170   CreateIPAddressPatternAndMask(IPAddress(kIPV6Address5), &pattern, &mask);
1171   expected_pattern =
1172       CreatePattern(kIPV6PatternPrefix, sizeof(kIPV6PatternPrefix),
1173                     kIPV6Address5Bytes, sizeof(kIPV6Address5Bytes));
1174   EXPECT_TRUE(pattern.Equals(expected_pattern));
1175   EXPECT_TRUE(mask.Equals(ByteString(kIPV6MaskBytes, sizeof(kIPV6MaskBytes))));
1176 
1177   pattern.Clear();
1178   expected_pattern.Clear();
1179   mask.Clear();
1180   CreateIPAddressPatternAndMask(IPAddress(kIPV6Address6), &pattern, &mask);
1181   expected_pattern =
1182       CreatePattern(kIPV6PatternPrefix, sizeof(kIPV6PatternPrefix),
1183                     kIPV6Address6Bytes, sizeof(kIPV6Address6Bytes));
1184   EXPECT_TRUE(pattern.Equals(expected_pattern));
1185   EXPECT_TRUE(mask.Equals(ByteString(kIPV6MaskBytes, sizeof(kIPV6MaskBytes))));
1186 
1187   pattern.Clear();
1188   expected_pattern.Clear();
1189   mask.Clear();
1190   CreateIPAddressPatternAndMask(IPAddress(kIPV6Address7), &pattern, &mask);
1191   expected_pattern =
1192       CreatePattern(kIPV6PatternPrefix, sizeof(kIPV6PatternPrefix),
1193                     kIPV6Address7Bytes, sizeof(kIPV6Address7Bytes));
1194   EXPECT_TRUE(pattern.Equals(expected_pattern));
1195   EXPECT_TRUE(mask.Equals(ByteString(kIPV6MaskBytes, sizeof(kIPV6MaskBytes))));
1196 }
1197 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ConfigureWiphyIndex)1198 TEST_F(WakeOnWiFiTestWithMockDispatcher, ConfigureWiphyIndex) {
1199   SetWakeOnPacketConnMessage msg;
1200   uint32_t value;
1201   EXPECT_FALSE(
1202       msg.attributes()->GetU32AttributeValue(NL80211_ATTR_WIPHY, &value));
1203 
1204   ConfigureWiphyIndex(&msg, 137);
1205   EXPECT_TRUE(
1206       msg.attributes()->GetU32AttributeValue(NL80211_ATTR_WIPHY, &value));
1207   EXPECT_EQ(value, 137);
1208 }
1209 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ConfigureDisableWakeOnWiFiMessage)1210 TEST_F(WakeOnWiFiTestWithMockDispatcher, ConfigureDisableWakeOnWiFiMessage) {
1211   SetWakeOnPacketConnMessage msg;
1212   Error e;
1213   uint32_t value;
1214   EXPECT_FALSE(
1215       msg.attributes()->GetU32AttributeValue(NL80211_ATTR_WIPHY, &value));
1216 
1217   ConfigureDisableWakeOnWiFiMessage(&msg, 57, &e);
1218   EXPECT_EQ(e.type(), Error::Type::kSuccess);
1219   EXPECT_TRUE(
1220       msg.attributes()->GetU32AttributeValue(NL80211_ATTR_WIPHY, &value));
1221   EXPECT_EQ(value, 57);
1222 }
1223 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiSettingsMatch)1224 TEST_F(WakeOnWiFiTestWithMockDispatcher, WakeOnWiFiSettingsMatch) {
1225   IPAddressStore all_addresses;
1226   set<WakeOnWiFi::WakeOnWiFiTrigger> trigs;
1227   vector<ByteString> whitelist;
1228   const uint32_t interval = kNetDetectScanIntervalSeconds;
1229 
1230   GetWakeOnPacketConnMessage msg0;
1231   NetlinkPacket packet0(kResponseNoIPAddresses, sizeof(kResponseNoIPAddresses));
1232   msg0.InitFromPacket(&packet0, NetlinkMessage::MessageContext());
1233   EXPECT_TRUE(
1234       WakeOnWiFiSettingsMatch(msg0, trigs, all_addresses, interval, whitelist));
1235 
1236   trigs.insert(WakeOnWiFi::kWakeTriggerPattern);
1237   all_addresses.AddUnique(
1238       IPAddress(string(kIPV4Address0, sizeof(kIPV4Address0))));
1239   GetWakeOnPacketConnMessage msg1;
1240   NetlinkPacket packet1(kResponseIPV40, sizeof(kResponseIPV40));
1241   msg1.InitFromPacket(&packet1, NetlinkMessage::MessageContext());
1242   EXPECT_TRUE(
1243       WakeOnWiFiSettingsMatch(msg1, trigs, all_addresses, interval, whitelist));
1244   EXPECT_FALSE(
1245       WakeOnWiFiSettingsMatch(msg0, trigs, all_addresses, interval, whitelist));
1246 
1247   // Test matching of wake on disconnect trigger.
1248   trigs.insert(WakeOnWiFi::kWakeTriggerDisconnect);
1249   GetWakeOnPacketConnMessage msg2;
1250   NetlinkPacket packet2(
1251       kResponseIPV40WakeOnDisconnect, sizeof(kResponseIPV40WakeOnDisconnect));
1252   msg2.InitFromPacket(&packet2, NetlinkMessage::MessageContext());
1253   EXPECT_TRUE(
1254       WakeOnWiFiSettingsMatch(msg2, trigs, all_addresses, interval, whitelist));
1255   EXPECT_FALSE(
1256       WakeOnWiFiSettingsMatch(msg1, trigs, all_addresses, interval, whitelist));
1257   EXPECT_FALSE(
1258       WakeOnWiFiSettingsMatch(msg0, trigs, all_addresses, interval, whitelist));
1259 
1260   trigs.erase(WakeOnWiFi::kWakeTriggerDisconnect);
1261   all_addresses.AddUnique(
1262       IPAddress(string(kIPV4Address1, sizeof(kIPV4Address1))));
1263   GetWakeOnPacketConnMessage msg3;
1264   NetlinkPacket packet3(kResponseIPV401, sizeof(kResponseIPV401));
1265   msg3.InitFromPacket(&packet3, NetlinkMessage::MessageContext());
1266   EXPECT_TRUE(
1267       WakeOnWiFiSettingsMatch(msg3, trigs, all_addresses, interval, whitelist));
1268   EXPECT_FALSE(
1269       WakeOnWiFiSettingsMatch(msg2, trigs, all_addresses, interval, whitelist));
1270   EXPECT_FALSE(
1271       WakeOnWiFiSettingsMatch(msg1, trigs, all_addresses, interval, whitelist));
1272   EXPECT_FALSE(
1273       WakeOnWiFiSettingsMatch(msg0, trigs, all_addresses, interval, whitelist));
1274 
1275   all_addresses.AddUnique(
1276       IPAddress(string(kIPV6Address0, sizeof(kIPV6Address0))));
1277   GetWakeOnPacketConnMessage msg4;
1278   NetlinkPacket packet4(kResponseIPV401IPV60, sizeof(kResponseIPV401IPV60));
1279   msg4.InitFromPacket(&packet4, NetlinkMessage::MessageContext());
1280   EXPECT_TRUE(
1281       WakeOnWiFiSettingsMatch(msg4, trigs, all_addresses, interval, whitelist));
1282   EXPECT_FALSE(
1283       WakeOnWiFiSettingsMatch(msg3, trigs, all_addresses, interval, whitelist));
1284   EXPECT_FALSE(
1285       WakeOnWiFiSettingsMatch(msg2, trigs, all_addresses, interval, whitelist));
1286   EXPECT_FALSE(
1287       WakeOnWiFiSettingsMatch(msg1, trigs, all_addresses, interval, whitelist));
1288   EXPECT_FALSE(
1289       WakeOnWiFiSettingsMatch(msg0, trigs, all_addresses, interval, whitelist));
1290 
1291   all_addresses.AddUnique(
1292       IPAddress(string(kIPV6Address1, sizeof(kIPV6Address1))));
1293   GetWakeOnPacketConnMessage msg5;
1294   NetlinkPacket packet5(kResponseIPV401IPV601, sizeof(kResponseIPV401IPV601));
1295   msg5.InitFromPacket(&packet5, NetlinkMessage::MessageContext());
1296   EXPECT_TRUE(
1297       WakeOnWiFiSettingsMatch(msg5, trigs, all_addresses, interval, whitelist));
1298   EXPECT_FALSE(
1299       WakeOnWiFiSettingsMatch(msg4, trigs, all_addresses, interval, whitelist));
1300   EXPECT_FALSE(
1301       WakeOnWiFiSettingsMatch(msg3, trigs, all_addresses, interval, whitelist));
1302   EXPECT_FALSE(
1303       WakeOnWiFiSettingsMatch(msg2, trigs, all_addresses, interval, whitelist));
1304   EXPECT_FALSE(
1305       WakeOnWiFiSettingsMatch(msg1, trigs, all_addresses, interval, whitelist));
1306   EXPECT_FALSE(
1307       WakeOnWiFiSettingsMatch(msg0, trigs, all_addresses, interval, whitelist));
1308 
1309   // Test matching of wake on SSID trigger.
1310   all_addresses.Clear();
1311   trigs.clear();
1312   trigs.insert(WakeOnWiFi::kWakeTriggerSSID);
1313   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
1314   AddSSIDToWhitelist(kSSIDBytes2, sizeof(kSSIDBytes2), &whitelist);
1315   GetWakeOnPacketConnMessage msg6;
1316   NetlinkPacket packet6(kResponseWakeOnSSID, sizeof(kResponseWakeOnSSID));
1317   msg6.InitFromPacket(&packet6, NetlinkMessage::MessageContext());
1318   EXPECT_TRUE(
1319       WakeOnWiFiSettingsMatch(msg6, trigs, all_addresses, interval, whitelist));
1320   EXPECT_FALSE(
1321       WakeOnWiFiSettingsMatch(msg5, trigs, all_addresses, interval, whitelist));
1322   EXPECT_FALSE(
1323       WakeOnWiFiSettingsMatch(msg4, trigs, all_addresses, interval, whitelist));
1324   EXPECT_FALSE(
1325       WakeOnWiFiSettingsMatch(msg3, trigs, all_addresses, interval, whitelist));
1326   EXPECT_FALSE(
1327       WakeOnWiFiSettingsMatch(msg2, trigs, all_addresses, interval, whitelist));
1328   EXPECT_FALSE(
1329       WakeOnWiFiSettingsMatch(msg1, trigs, all_addresses, interval, whitelist));
1330   EXPECT_FALSE(
1331       WakeOnWiFiSettingsMatch(msg0, trigs, all_addresses, interval, whitelist));
1332 
1333   // Test that we get a mismatch if triggers are present in the message that we
1334   // don't expect.
1335   trigs.clear();
1336   EXPECT_FALSE(
1337       WakeOnWiFiSettingsMatch(msg6, trigs, all_addresses, interval, whitelist));
1338   EXPECT_FALSE(
1339       WakeOnWiFiSettingsMatch(msg5, trigs, all_addresses, interval, whitelist));
1340   EXPECT_FALSE(
1341       WakeOnWiFiSettingsMatch(msg4, trigs, all_addresses, interval, whitelist));
1342   EXPECT_FALSE(
1343       WakeOnWiFiSettingsMatch(msg3, trigs, all_addresses, interval, whitelist));
1344   EXPECT_FALSE(
1345       WakeOnWiFiSettingsMatch(msg2, trigs, all_addresses, interval, whitelist));
1346   EXPECT_FALSE(
1347       WakeOnWiFiSettingsMatch(msg1, trigs, all_addresses, interval, whitelist));
1348 }
1349 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ConfigureSetWakeOnWiFiSettingsMessage)1350 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1351        ConfigureSetWakeOnWiFiSettingsMessage) {
1352   IPAddressStore all_addresses;
1353   set<WakeOnWiFi::WakeOnWiFiTrigger> trigs;
1354   const int index = 1;  // wiphy device number
1355   vector<ByteString> whitelist;
1356   const uint32_t interval = kNetDetectScanIntervalSeconds;
1357   SetWakeOnPacketConnMessage msg0;
1358   Error e;
1359   trigs.insert(WakeOnWiFi::kWakeTriggerPattern);
1360   all_addresses.AddUnique(
1361       IPAddress(string(kIPV4Address0, sizeof(kIPV4Address0))));
1362   EXPECT_TRUE(ConfigureSetWakeOnWiFiSettingsMessage(
1363       &msg0, trigs, all_addresses, index, interval, whitelist, &e));
1364   EXPECT_TRUE(
1365       WakeOnWiFiSettingsMatch(msg0, trigs, all_addresses, interval, whitelist));
1366 
1367   SetWakeOnPacketConnMessage msg1;
1368   all_addresses.AddUnique(
1369       IPAddress(string(kIPV4Address1, sizeof(kIPV4Address1))));
1370   EXPECT_TRUE(ConfigureSetWakeOnWiFiSettingsMessage(
1371       &msg1, trigs, all_addresses, index, interval, whitelist, &e));
1372   EXPECT_TRUE(
1373       WakeOnWiFiSettingsMatch(msg1, trigs, all_addresses, interval, whitelist));
1374 
1375   SetWakeOnPacketConnMessage msg2;
1376   all_addresses.AddUnique(
1377       IPAddress(string(kIPV6Address0, sizeof(kIPV6Address0))));
1378   EXPECT_TRUE(ConfigureSetWakeOnWiFiSettingsMessage(
1379       &msg2, trigs, all_addresses, index, interval, whitelist, &e));
1380   EXPECT_TRUE(
1381       WakeOnWiFiSettingsMatch(msg2, trigs, all_addresses, interval, whitelist));
1382 
1383   SetWakeOnPacketConnMessage msg3;
1384   all_addresses.AddUnique(
1385       IPAddress(string(kIPV6Address1, sizeof(kIPV6Address1))));
1386   EXPECT_TRUE(ConfigureSetWakeOnWiFiSettingsMessage(
1387       &msg3, trigs, all_addresses, index, interval, whitelist, &e));
1388   EXPECT_TRUE(
1389       WakeOnWiFiSettingsMatch(msg3, trigs, all_addresses, interval, whitelist));
1390 
1391   SetWakeOnPacketConnMessage msg4;
1392   all_addresses.AddUnique(
1393       IPAddress(string(kIPV6Address2, sizeof(kIPV6Address2))));
1394   EXPECT_TRUE(ConfigureSetWakeOnWiFiSettingsMessage(
1395       &msg4, trigs, all_addresses, index, interval, whitelist, &e));
1396   EXPECT_TRUE(
1397       WakeOnWiFiSettingsMatch(msg4, trigs, all_addresses, interval, whitelist));
1398 
1399   SetWakeOnPacketConnMessage msg5;
1400   all_addresses.AddUnique(
1401       IPAddress(string(kIPV6Address3, sizeof(kIPV6Address3))));
1402   EXPECT_TRUE(ConfigureSetWakeOnWiFiSettingsMessage(
1403       &msg5, trigs, all_addresses, index, interval, whitelist, &e));
1404   EXPECT_TRUE(
1405       WakeOnWiFiSettingsMatch(msg5, trigs, all_addresses, interval, whitelist));
1406 
1407   SetWakeOnPacketConnMessage msg6;
1408   all_addresses.AddUnique(
1409       IPAddress(string(kIPV6Address4, sizeof(kIPV6Address4))));
1410   EXPECT_TRUE(ConfigureSetWakeOnWiFiSettingsMessage(
1411       &msg6, trigs, all_addresses, index, interval, whitelist, &e));
1412   EXPECT_TRUE(
1413       WakeOnWiFiSettingsMatch(msg6, trigs, all_addresses, interval, whitelist));
1414 
1415   SetWakeOnPacketConnMessage msg7;
1416   all_addresses.AddUnique(
1417       IPAddress(string(kIPV6Address5, sizeof(kIPV6Address5))));
1418   EXPECT_TRUE(ConfigureSetWakeOnWiFiSettingsMessage(
1419       &msg7, trigs, all_addresses, index, interval, whitelist, &e));
1420   EXPECT_TRUE(
1421       WakeOnWiFiSettingsMatch(msg7, trigs, all_addresses, interval, whitelist));
1422 
1423   SetWakeOnPacketConnMessage msg8;
1424   all_addresses.AddUnique(
1425       IPAddress(string(kIPV6Address6, sizeof(kIPV6Address6))));
1426   EXPECT_TRUE(ConfigureSetWakeOnWiFiSettingsMessage(
1427       &msg8, trigs, all_addresses, index, interval, whitelist, &e));
1428   EXPECT_TRUE(
1429       WakeOnWiFiSettingsMatch(msg8, trigs, all_addresses, interval, whitelist));
1430 
1431   SetWakeOnPacketConnMessage msg9;
1432   all_addresses.AddUnique(
1433       IPAddress(string(kIPV6Address7, sizeof(kIPV6Address7))));
1434   EXPECT_TRUE(ConfigureSetWakeOnWiFiSettingsMessage(
1435       &msg9, trigs, all_addresses, index, interval, whitelist, &e));
1436   EXPECT_TRUE(
1437       WakeOnWiFiSettingsMatch(msg9, trigs, all_addresses, interval, whitelist));
1438 
1439   SetWakeOnPacketConnMessage msg10;
1440   all_addresses.Clear();
1441   trigs.clear();
1442   trigs.insert(WakeOnWiFi::kWakeTriggerSSID);
1443   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
1444   AddSSIDToWhitelist(kSSIDBytes2, sizeof(kSSIDBytes2), &whitelist);
1445   EXPECT_TRUE(ConfigureSetWakeOnWiFiSettingsMessage(
1446       &msg10, trigs, all_addresses, index, interval, whitelist, &e));
1447   EXPECT_TRUE(WakeOnWiFiSettingsMatch(msg10, trigs, all_addresses, interval,
1448                                       whitelist));
1449 }
1450 
TEST_F(WakeOnWiFiTestWithMockDispatcher,RequestWakeOnPacketSettings)1451 TEST_F(WakeOnWiFiTestWithMockDispatcher, RequestWakeOnPacketSettings) {
1452   EXPECT_CALL(
1453       netlink_manager_,
1454       SendNl80211Message(IsNl80211Command(kNl80211FamilyId,
1455                                           GetWakeOnPacketConnMessage::kCommand),
1456                          _, _, _)).Times(1);
1457   RequestWakeOnPacketSettings();
1458 }
1459 
TEST_F(WakeOnWiFiTestWithMockDispatcher,VerifyWakeOnWiFiSettings_NoWakeOnPacketRules)1460 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1461        VerifyWakeOnWiFiSettings_NoWakeOnPacketRules) {
1462   ScopedMockLog log;
1463   // Create an Nl80211 response to a NL80211_CMD_GET_WOWLAN request
1464   // indicating that there are no wake-on-packet rules programmed into the NIC.
1465   GetWakeOnPacketConnMessage msg;
1466   NetlinkPacket packet(kResponseNoIPAddresses, sizeof(kResponseNoIPAddresses));
1467   msg.InitFromPacket(&packet, NetlinkMessage::MessageContext());
1468   // Successful verification and consequent invocation of callback.
1469   SetSuspendActionsDoneCallback();
1470   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
1471   ScopeLogger::GetInstance()->set_verbose_level(2);
1472   EXPECT_TRUE(GetWakeOnPacketConnections()->Empty());
1473   EXPECT_FALSE(SuspendActionsCallbackIsNull());
1474   EXPECT_CALL(*this, DoneCallback(ErrorTypeIs(Error::kSuccess))).Times(1);
1475   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1476   EXPECT_CALL(
1477       log, Log(_, _, HasSubstr("Wake on WiFi settings successfully verified")));
1478   EXPECT_CALL(metrics_, NotifyVerifyWakeOnWiFiSettingsResult(
1479                             Metrics::kVerifyWakeOnWiFiSettingsResultSuccess));
1480   VerifyWakeOnWiFiSettings(msg);
1481   // Suspend action callback cleared after being invoked.
1482   EXPECT_TRUE(SuspendActionsCallbackIsNull());
1483   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
1484   ScopeLogger::GetInstance()->set_verbose_level(0);
1485 
1486   // Unsuccessful verification if locally stored settings do not match.
1487   GetWakeOnPacketConnections()->AddUnique(IPAddress("1.1.1.1"));
1488   GetWakeOnWiFiTriggers()->insert(WakeOnWiFi::kWakeTriggerPattern);
1489   EXPECT_CALL(log,
1490               Log(logging::LOG_ERROR, _,
1491                   HasSubstr(
1492                       " failed: discrepancy between wake-on-packet settings on "
1493                       "NIC and those in local data structure detected")));
1494   EXPECT_CALL(metrics_, NotifyVerifyWakeOnWiFiSettingsResult(
1495                             Metrics::kVerifyWakeOnWiFiSettingsResultFailure));
1496   VerifyWakeOnWiFiSettings(msg);
1497 }
1498 
TEST_F(WakeOnWiFiTestWithMockDispatcher,VerifyWakeOnWiFiSettings_WakeOnPatternAndDisconnectRules)1499 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1500        VerifyWakeOnWiFiSettings_WakeOnPatternAndDisconnectRules) {
1501   ScopedMockLog log;
1502   // Create a non-trivial Nl80211 response to a NL80211_CMD_GET_WOWLAN request
1503   // indicating that that the NIC wakes on packets from 192.168.10.20 and on
1504   // disconnects.
1505   GetWakeOnPacketConnMessage msg;
1506   NetlinkPacket packet(
1507       kResponseIPV40WakeOnDisconnect, sizeof(kResponseIPV40WakeOnDisconnect));
1508   msg.InitFromPacket(&packet, NetlinkMessage::MessageContext());
1509   // Successful verification and consequent invocation of callback.
1510   SetSuspendActionsDoneCallback();
1511   EXPECT_FALSE(SuspendActionsCallbackIsNull());
1512   GetWakeOnPacketConnections()->AddUnique(IPAddress("192.168.10.20"));
1513   GetWakeOnWiFiTriggers()->insert(WakeOnWiFi::kWakeTriggerPattern);
1514   GetWakeOnWiFiTriggers()->insert(WakeOnWiFi::kWakeTriggerDisconnect);
1515   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
1516   ScopeLogger::GetInstance()->set_verbose_level(2);
1517   EXPECT_CALL(*this, DoneCallback(ErrorTypeIs(Error::kSuccess))).Times(1);
1518   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1519   EXPECT_CALL(
1520       log, Log(_, _, HasSubstr("Wake on WiFi settings successfully verified")));
1521   EXPECT_CALL(metrics_, NotifyVerifyWakeOnWiFiSettingsResult(
1522                             Metrics::kVerifyWakeOnWiFiSettingsResultSuccess));
1523   VerifyWakeOnWiFiSettings(msg);
1524   // Suspend action callback cleared after being invoked.
1525   EXPECT_TRUE(SuspendActionsCallbackIsNull());
1526   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
1527   ScopeLogger::GetInstance()->set_verbose_level(0);
1528 
1529   // Unsuccessful verification if locally stored settings do not match.
1530   GetWakeOnWiFiTriggers()->erase(WakeOnWiFi::kWakeTriggerDisconnect);
1531   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1532   EXPECT_CALL(log,
1533               Log(logging::LOG_ERROR, _,
1534                   HasSubstr(
1535                       " failed: discrepancy between wake-on-packet settings on "
1536                       "NIC and those in local data structure detected")));
1537   EXPECT_CALL(metrics_, NotifyVerifyWakeOnWiFiSettingsResult(
1538                             Metrics::kVerifyWakeOnWiFiSettingsResultFailure));
1539   VerifyWakeOnWiFiSettings(msg);
1540 }
1541 
TEST_F(WakeOnWiFiTestWithMockDispatcher,VerifyWakeOnWiFiSettings_WakeOnSSIDRules)1542 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1543        VerifyWakeOnWiFiSettings_WakeOnSSIDRules) {
1544   ScopedMockLog log;
1545   // Create a non-trivial Nl80211 response to a NL80211_CMD_GET_WOWLAN request
1546   // indicating that that the NIC wakes on two SSIDs represented by kSSIDBytes1
1547   // and kSSIDBytes2 and scans for them at interval
1548   // kNetDetectScanIntervalSeconds.
1549   GetWakeOnPacketConnMessage msg;
1550   NetlinkPacket packet(kResponseWakeOnSSID, sizeof(kResponseWakeOnSSID));
1551   msg.InitFromPacket(&packet, NetlinkMessage::MessageContext());
1552   // Successful verification and consequent invocation of callback.
1553   SetSuspendActionsDoneCallback();
1554   EXPECT_FALSE(SuspendActionsCallbackIsNull());
1555   GetWakeOnWiFiTriggers()->insert(WakeOnWiFi::kWakeTriggerSSID);
1556   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1),
1557                      GetWakeOnSSIDWhitelist());
1558   AddSSIDToWhitelist(kSSIDBytes2, sizeof(kSSIDBytes2),
1559                      GetWakeOnSSIDWhitelist());
1560   SetNetDetectScanPeriodSeconds(kNetDetectScanIntervalSeconds);
1561   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
1562   ScopeLogger::GetInstance()->set_verbose_level(2);
1563   EXPECT_CALL(*this, DoneCallback(ErrorTypeIs(Error::kSuccess))).Times(1);
1564   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1565   EXPECT_CALL(
1566       log, Log(_, _, HasSubstr("Wake on WiFi settings successfully verified")));
1567   EXPECT_CALL(metrics_, NotifyVerifyWakeOnWiFiSettingsResult(
1568                             Metrics::kVerifyWakeOnWiFiSettingsResultSuccess));
1569   VerifyWakeOnWiFiSettings(msg);
1570   // Suspend action callback cleared after being invoked.
1571   EXPECT_TRUE(SuspendActionsCallbackIsNull());
1572   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
1573   ScopeLogger::GetInstance()->set_verbose_level(0);
1574 }
1575 
TEST_F(WakeOnWiFiTestWithMockDispatcher,VerifyWakeOnWiFiSettingsSuccess_NoDoneCallback)1576 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1577        VerifyWakeOnWiFiSettingsSuccess_NoDoneCallback) {
1578   ScopedMockLog log;
1579   // Create an Nl80211 response to a NL80211_CMD_GET_WOWLAN request
1580   // indicating that there are no wake-on-packet rules programmed into the NIC.
1581   GetWakeOnPacketConnMessage msg;
1582   NetlinkPacket packet(kResponseNoIPAddresses, sizeof(kResponseNoIPAddresses));
1583   msg.InitFromPacket(&packet, NetlinkMessage::MessageContext());
1584   // Successful verification, but since there is no suspend action callback
1585   // set, no callback is invoked.
1586   EXPECT_TRUE(SuspendActionsCallbackIsNull());
1587   EXPECT_TRUE(GetWakeOnPacketConnections()->Empty());
1588   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
1589   ScopeLogger::GetInstance()->set_verbose_level(2);
1590   EXPECT_CALL(*this, DoneCallback(_)).Times(0);
1591   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1592   EXPECT_CALL(
1593       log, Log(_, _, HasSubstr("Wake on WiFi settings successfully verified")));
1594   EXPECT_CALL(metrics_, NotifyVerifyWakeOnWiFiSettingsResult(
1595                             Metrics::kVerifyWakeOnWiFiSettingsResultSuccess));
1596   VerifyWakeOnWiFiSettings(msg);
1597   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
1598   ScopeLogger::GetInstance()->set_verbose_level(0);
1599 }
1600 
TEST_F(WakeOnWiFiTestWithMockDispatcher,RetrySetWakeOnPacketConnections_LessThanMaxRetries)1601 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1602        RetrySetWakeOnPacketConnections_LessThanMaxRetries) {
1603   ScopedMockLog log;
1604   // Max retries not reached yet, so send Nl80211 message to program NIC again.
1605   GetWakeOnWiFiTriggers()->insert(WakeOnWiFi::kWakeTriggerDisconnect);
1606   SetNumSetWakeOnPacketRetries(WakeOnWiFi::kMaxSetWakeOnPacketRetries - 1);
1607   EXPECT_CALL(
1608       netlink_manager_,
1609       SendNl80211Message(IsNl80211Command(kNl80211FamilyId,
1610                                           SetWakeOnPacketConnMessage::kCommand),
1611                          _, _, _)).Times(1);
1612   RetrySetWakeOnPacketConnections();
1613   EXPECT_EQ(GetNumSetWakeOnPacketRetries(),
1614             WakeOnWiFi::kMaxSetWakeOnPacketRetries);
1615 }
1616 
TEST_F(WakeOnWiFiTestWithMockDispatcher,RetrySetWakeOnPacketConnections_MaxAttemptsWithCallbackSet)1617 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1618        RetrySetWakeOnPacketConnections_MaxAttemptsWithCallbackSet) {
1619   ScopedMockLog log;
1620   // Max retry attempts reached. Suspend actions done callback is set, so it
1621   // is invoked.
1622   SetNumSetWakeOnPacketRetries(WakeOnWiFi::kMaxSetWakeOnPacketRetries);
1623   SetSuspendActionsDoneCallback();
1624   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
1625   ScopeLogger::GetInstance()->set_verbose_level(3);
1626   EXPECT_FALSE(SuspendActionsCallbackIsNull());
1627   EXPECT_CALL(*this, DoneCallback(ErrorTypeIs(Error::kOperationFailed)))
1628       .Times(1);
1629   EXPECT_CALL(netlink_manager_, SendNl80211Message(_, _, _, _)).Times(0);
1630   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1631   EXPECT_CALL(log, Log(_, _, HasSubstr("max retry attempts reached")));
1632   RetrySetWakeOnPacketConnections();
1633   EXPECT_TRUE(SuspendActionsCallbackIsNull());
1634   EXPECT_EQ(GetNumSetWakeOnPacketRetries(), 0);
1635   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
1636   ScopeLogger::GetInstance()->set_verbose_level(0);
1637 }
1638 
TEST_F(WakeOnWiFiTestWithMockDispatcher,RetrySetWakeOnPacketConnections_MaxAttemptsCallbackUnset)1639 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1640        RetrySetWakeOnPacketConnections_MaxAttemptsCallbackUnset) {
1641   ScopedMockLog log;
1642   // If there is no suspend action callback set, no suspend callback should be
1643   // invoked.
1644   SetNumSetWakeOnPacketRetries(WakeOnWiFi::kMaxSetWakeOnPacketRetries);
1645   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
1646   ScopeLogger::GetInstance()->set_verbose_level(3);
1647   EXPECT_TRUE(SuspendActionsCallbackIsNull());
1648   EXPECT_CALL(*this, DoneCallback(_)).Times(0);
1649   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1650   EXPECT_CALL(log, Log(_, _, HasSubstr("max retry attempts reached")));
1651   RetrySetWakeOnPacketConnections();
1652   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
1653   ScopeLogger::GetInstance()->set_verbose_level(0);
1654 }
1655 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ParseWakeOnWiFiCapabilities_DisconnectPatternSSIDSupported)1656 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1657        ParseWakeOnWiFiCapabilities_DisconnectPatternSSIDSupported) {
1658   ClearWakeOnWiFiTriggersSupported();
1659   NewWiphyMessage msg;
1660   NetlinkPacket packet(kNewWiphyNlMsg, sizeof(kNewWiphyNlMsg));
1661   msg.InitFromPacket(&packet, NetlinkMessage::MessageContext());
1662   ParseWakeOnWiFiCapabilities(msg);
1663   EXPECT_TRUE(GetWakeOnWiFiTriggersSupported()->find(
1664                   WakeOnWiFi::kWakeTriggerDisconnect) !=
1665               GetWakeOnWiFiTriggersSupported()->end());
1666   EXPECT_TRUE(
1667       GetWakeOnWiFiTriggersSupported()->find(WakeOnWiFi::kWakeTriggerPattern) !=
1668       GetWakeOnWiFiTriggersSupported()->end());
1669   EXPECT_TRUE(
1670       GetWakeOnWiFiTriggersSupported()->find(WakeOnWiFi::kWakeTriggerSSID) !=
1671       GetWakeOnWiFiTriggersSupported()->end());
1672   EXPECT_EQ(GetWakeOnWiFiMaxPatterns(), kNewWiphyNlMsg_MaxPatterns);
1673   EXPECT_EQ(GetWakeOnWiFiMaxSSIDs(), kNewWiphyNlMsg_MaxSSIDs);
1674 }
1675 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ParseWakeOnWiFiCapabilities_UnsupportedPatternLen)1676 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1677        ParseWakeOnWiFiCapabilities_UnsupportedPatternLen) {
1678   ClearWakeOnWiFiTriggersSupported();
1679   NewWiphyMessage msg;
1680   // Modify the range of support pattern lengths to [0-1] bytes, which is less
1681   // than what we need to use  our IPV4 (30 bytes) or IPV6 (38 bytes) patterns.
1682   MutableNetlinkPacket packet(kNewWiphyNlMsg, sizeof(kNewWiphyNlMsg));
1683   struct nl80211_pattern_support* patt_support =
1684       reinterpret_cast<struct nl80211_pattern_support*>(
1685           &packet.GetMutablePayload()->GetData()[
1686               kNewWiphyNlMsg_PattSupportOffset]);
1687   patt_support->min_pattern_len = 0;
1688   patt_support->max_pattern_len = 1;
1689   msg.InitFromPacket(&packet, NetlinkMessage::MessageContext());
1690   ParseWakeOnWiFiCapabilities(msg);
1691   EXPECT_TRUE(GetWakeOnWiFiTriggersSupported()->find(
1692                   WakeOnWiFi::kWakeTriggerDisconnect) !=
1693               GetWakeOnWiFiTriggersSupported()->end());
1694   EXPECT_TRUE(
1695       GetWakeOnWiFiTriggersSupported()->find(WakeOnWiFi::kWakeTriggerSSID) !=
1696       GetWakeOnWiFiTriggersSupported()->end());
1697   // Ensure that ParseWakeOnWiFiCapabilities realizes that our IP address
1698   // patterns cannot be used given the support pattern length range reported.
1699   EXPECT_FALSE(
1700       GetWakeOnWiFiTriggersSupported()->find(WakeOnWiFi::kWakeTriggerPattern) !=
1701       GetWakeOnWiFiTriggersSupported()->end());
1702 }
1703 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ParseWakeOnWiFiCapabilities_DisconnectNotSupported)1704 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1705        ParseWakeOnWiFiCapabilities_DisconnectNotSupported) {
1706   ClearWakeOnWiFiTriggersSupported();
1707   NewWiphyMessage msg;
1708   // Change the NL80211_WOWLAN_TRIG_DISCONNECT flag attribute into the
1709   // NL80211_WOWLAN_TRIG_MAGIC_PKT flag attribute, so that this message
1710   // no longer reports wake on disconnect as a supported capability.
1711   MutableNetlinkPacket packet(kNewWiphyNlMsg, sizeof(kNewWiphyNlMsg));
1712   struct nlattr* wowlan_trig_disconnect_attr =
1713       reinterpret_cast<struct nlattr*>(
1714           &packet.GetMutablePayload()->GetData()[
1715               kNewWiphyNlMsg_WowlanTrigDisconnectAttributeOffset]);
1716   wowlan_trig_disconnect_attr->nla_type = NL80211_WOWLAN_TRIG_MAGIC_PKT;
1717   msg.InitFromPacket(&packet, NetlinkMessage::MessageContext());
1718   ParseWakeOnWiFiCapabilities(msg);
1719   EXPECT_TRUE(
1720       GetWakeOnWiFiTriggersSupported()->find(WakeOnWiFi::kWakeTriggerPattern) !=
1721       GetWakeOnWiFiTriggersSupported()->end());
1722   EXPECT_TRUE(
1723       GetWakeOnWiFiTriggersSupported()->find(WakeOnWiFi::kWakeTriggerSSID) !=
1724       GetWakeOnWiFiTriggersSupported()->end());
1725   // Ensure that ParseWakeOnWiFiCapabilities realizes that wake on disconnect
1726   // is not supported.
1727   EXPECT_FALSE(GetWakeOnWiFiTriggersSupported()->find(
1728                    WakeOnWiFi::kWakeTriggerDisconnect) !=
1729                GetWakeOnWiFiTriggersSupported()->end());
1730 }
1731 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ParseWakeOnWiFiCapabilities_SSIDNotSupported)1732 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1733        ParseWakeOnWiFiCapabilities_SSIDNotSupported) {
1734   ClearWakeOnWiFiTriggersSupported();
1735   NewWiphyMessage msg;
1736   // Change the NL80211_WOWLAN_TRIG_NET_DETECT flag attribute type to an invalid
1737   // attribute type (0), so that this message no longer reports wake on SSID
1738   // as a supported capability.
1739   MutableNetlinkPacket packet(kNewWiphyNlMsg, sizeof(kNewWiphyNlMsg));
1740   struct nlattr* wowlan_trig_net_detect_attr =
1741       reinterpret_cast<struct nlattr*>(
1742           &packet.GetMutablePayload()->GetData()[
1743               kNewWiphyNlMsg_WowlanTrigNetDetectAttributeOffset]);
1744   wowlan_trig_net_detect_attr->nla_type = 0;
1745   msg.InitFromPacket(&packet, NetlinkMessage::MessageContext());
1746   ParseWakeOnWiFiCapabilities(msg);
1747   EXPECT_TRUE(
1748       GetWakeOnWiFiTriggersSupported()->find(WakeOnWiFi::kWakeTriggerPattern) !=
1749       GetWakeOnWiFiTriggersSupported()->end());
1750   EXPECT_TRUE(GetWakeOnWiFiTriggersSupported()->find(
1751                   WakeOnWiFi::kWakeTriggerDisconnect) !=
1752               GetWakeOnWiFiTriggersSupported()->end());
1753   // Ensure that ParseWakeOnWiFiCapabilities realizes that wake on SSID is not
1754   // supported.
1755   EXPECT_FALSE(
1756       GetWakeOnWiFiTriggersSupported()->find(WakeOnWiFi::kWakeTriggerSSID) !=
1757       GetWakeOnWiFiTriggersSupported()->end());
1758 }
1759 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ApplyWakeOnWiFiSettings_WiphyIndexNotReceived)1760 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1761        ApplyWakeOnWiFiSettings_WiphyIndexNotReceived) {
1762   ScopedMockLog log;
1763   // ApplyWakeOnWiFiSettings should return immediately if the wifi interface
1764   // index has not been received when the function is called.
1765   SetWiphyIndexReceivedToFalse();
1766   EXPECT_CALL(netlink_manager_,
1767               SendNl80211Message(IsDisableWakeOnWiFiMsg(), _, _, _)).Times(0);
1768   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1769   EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
1770                        HasSubstr("Interface index not yet received")));
1771   ApplyWakeOnWiFiSettings();
1772 }
1773 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ApplyWakeOnWiFiSettings_WiphyIndexReceived)1774 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1775        ApplyWakeOnWiFiSettings_WiphyIndexReceived) {
1776   // Disable wake on WiFi if there are no wake on WiFi triggers registered.
1777   EXPECT_CALL(
1778       netlink_manager_,
1779       SendNl80211Message(IsNl80211Command(kNl80211FamilyId,
1780                                           SetWakeOnPacketConnMessage::kCommand),
1781                          _, _, _)).Times(0);
1782   EXPECT_CALL(netlink_manager_,
1783               SendNl80211Message(IsDisableWakeOnWiFiMsg(), _, _, _)).Times(1);
1784   ApplyWakeOnWiFiSettings();
1785 
1786   // Otherwise, program the NIC.
1787   IPAddress ip_addr("1.1.1.1");
1788   GetWakeOnPacketConnections()->AddUnique(ip_addr);
1789   GetWakeOnWiFiTriggers()->insert(WakeOnWiFi::kWakeTriggerPattern);
1790   EXPECT_FALSE(GetWakeOnPacketConnections()->Empty());
1791   EXPECT_CALL(
1792       netlink_manager_,
1793       SendNl80211Message(IsNl80211Command(kNl80211FamilyId,
1794                                           SetWakeOnPacketConnMessage::kCommand),
1795                          _, _, _)).Times(1);
1796   EXPECT_CALL(netlink_manager_,
1797               SendNl80211Message(IsDisableWakeOnWiFiMsg(), _, _, _)).Times(0);
1798   ApplyWakeOnWiFiSettings();
1799 }
1800 
TEST_F(WakeOnWiFiTestWithMockDispatcher,BeforeSuspendActions_ReportDoneImmediately)1801 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1802        BeforeSuspendActions_ReportDoneImmediately) {
1803   ScopedMockLog log;
1804   const bool is_connected = true;
1805   const bool start_lease_renewal_timer = true;
1806   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1),
1807                      GetWakeOnSSIDWhitelist());
1808   // If no triggers are supported, no triggers will be programmed into the NIC.
1809   ClearWakeOnWiFiTriggersSupported();
1810   SetSuspendActionsDoneCallback();
1811   SetInDarkResume(true);
1812   SetLastWakeReason(WakeOnWiFi::kWakeTriggerPattern);
1813   AddResultToLastSSIDResults();
1814   // Do not report done immediately in dark resume, since we need to program it
1815   // to disable wake on WiFi.
1816   EXPECT_CALL(*this, DoneCallback(_)).Times(0);
1817   BeforeSuspendActions(is_connected, start_lease_renewal_timer,
1818                        kTimeToNextLeaseRenewalLong);
1819   EXPECT_FALSE(GetInDarkResume());
1820   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
1821   EXPECT_TRUE(GetLastSSIDMatchFreqs().empty());
1822 
1823   SetInDarkResume(false);
1824   SetLastWakeReason(WakeOnWiFi::kWakeTriggerPattern);
1825   AddResultToLastSSIDResults();
1826   // Report done immediately on normal suspend, since wake on WiFi should
1827   // already have been disabled on the NIC on a previous resume.
1828   EXPECT_CALL(*this, DoneCallback(_)).Times(1);
1829   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1830   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
1831   ScopeLogger::GetInstance()->set_verbose_level(1);
1832   EXPECT_CALL(
1833       log,
1834       Log(_, _,
1835           HasSubstr(
1836               "No need to disable wake on WiFi on NIC in regular suspend")));
1837   BeforeSuspendActions(is_connected, start_lease_renewal_timer,
1838                        kTimeToNextLeaseRenewalLong);
1839   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
1840   EXPECT_TRUE(GetLastSSIDMatchFreqs().empty());
1841   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
1842   ScopeLogger::GetInstance()->set_verbose_level(0);
1843 }
1844 
TEST_F(WakeOnWiFiTestWithMockDispatcher,BeforeSuspendActions_FeaturesDisabledOrTriggersUnsupported)1845 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1846        BeforeSuspendActions_FeaturesDisabledOrTriggersUnsupported) {
1847   const bool is_connected = true;
1848   const bool start_lease_renewal_timer = true;
1849   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1),
1850                      GetWakeOnSSIDWhitelist());
1851   SetInDarkResume(false);
1852   SetSuspendActionsDoneCallback();
1853   // No features enabled, so no triggers programmed.
1854   DisableWakeOnWiFiFeatures();
1855   SetLastWakeReason(WakeOnWiFi::kWakeTriggerPattern);
1856   AddResultToLastSSIDResults();
1857   EXPECT_TRUE(GetWakeOnWiFiTriggers()->empty());
1858   EXPECT_CALL(*this, DoneCallback(_));
1859   BeforeSuspendActions(is_connected, start_lease_renewal_timer,
1860                        kTimeToNextLeaseRenewalLong);
1861   EXPECT_TRUE(GetWakeOnWiFiTriggers()->empty());
1862   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
1863   EXPECT_TRUE(GetLastSSIDMatchFreqs().empty());
1864 
1865   // No triggers supported, so no triggers programmed.
1866   SetSuspendActionsDoneCallback();
1867   EnableWakeOnWiFiFeaturesPacketDarkConnect();
1868   GetWakeOnWiFiTriggersSupported()->clear();
1869   SetLastWakeReason(WakeOnWiFi::kWakeTriggerPattern);
1870   AddResultToLastSSIDResults();
1871   EXPECT_TRUE(GetWakeOnWiFiTriggers()->empty());
1872   EXPECT_CALL(*this, DoneCallback(_));
1873   BeforeSuspendActions(is_connected, start_lease_renewal_timer,
1874                        kTimeToNextLeaseRenewalLong);
1875   EXPECT_TRUE(GetWakeOnWiFiTriggers()->empty());
1876   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
1877   EXPECT_TRUE(GetLastSSIDMatchFreqs().empty());
1878 
1879   // Only wake on packet feature enabled and supported.
1880   EnableWakeOnWiFiFeaturesPacket();
1881   GetWakeOnWiFiTriggersSupported()->insert(WakeOnWiFi::kWakeTriggerPattern);
1882   GetWakeOnPacketConnections()->AddUnique(IPAddress("1.1.1.1"));
1883   SetLastWakeReason(WakeOnWiFi::kWakeTriggerPattern);
1884   AddResultToLastSSIDResults();
1885   EXPECT_TRUE(GetWakeOnWiFiTriggers()->empty());
1886   BeforeSuspendActions(is_connected, start_lease_renewal_timer,
1887                        kTimeToNextLeaseRenewalLong);
1888   EXPECT_EQ(GetWakeOnWiFiTriggers()->size(), 1);
1889   EXPECT_TRUE(GetWakeOnWiFiTriggers()->find(WakeOnWiFi::kWakeTriggerPattern) !=
1890               GetWakeOnWiFiTriggers()->end());
1891   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
1892   EXPECT_TRUE(GetLastSSIDMatchFreqs().empty());
1893 
1894   // Only wake on SSID feature supported.
1895   EnableWakeOnWiFiFeaturesDarkConnect();
1896   GetWakeOnPacketConnections()->Clear();
1897   GetWakeOnWiFiTriggersSupported()->clear();
1898   GetWakeOnWiFiTriggersSupported()->insert(WakeOnWiFi::kWakeTriggerDisconnect);
1899   GetWakeOnWiFiTriggersSupported()->insert(WakeOnWiFi::kWakeTriggerSSID);
1900   GetWakeOnWiFiTriggers()->clear();
1901   SetLastWakeReason(WakeOnWiFi::kWakeTriggerPattern);
1902   AddResultToLastSSIDResults();
1903   EXPECT_TRUE(GetWakeOnWiFiTriggers()->empty());
1904   BeforeSuspendActions(is_connected, start_lease_renewal_timer,
1905                        kTimeToNextLeaseRenewalLong);
1906   EXPECT_EQ(GetWakeOnWiFiTriggers()->size(), 1);
1907   EXPECT_TRUE(
1908       GetWakeOnWiFiTriggers()->find(WakeOnWiFi::kWakeTriggerDisconnect) !=
1909       GetWakeOnWiFiTriggers()->end());
1910   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
1911   EXPECT_TRUE(GetLastSSIDMatchFreqs().empty());
1912 }
1913 
TEST_F(WakeOnWiFiTestWithMockDispatcher,BeforeSuspendActions_ConnectedBeforeSuspend)1914 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1915        BeforeSuspendActions_ConnectedBeforeSuspend) {
1916   const bool is_connected = true;
1917   const bool start_lease_renewal_timer = true;
1918   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1),
1919                      GetWakeOnSSIDWhitelist());
1920   SetSuspendActionsDoneCallback();
1921   EnableWakeOnWiFiFeaturesPacketDarkConnect();
1922   GetWakeOnPacketConnections()->AddUnique(IPAddress("1.1.1.1"));
1923 
1924   SetInDarkResume(true);
1925   GetWakeOnWiFiTriggers()->clear();
1926   EXPECT_TRUE(GetWakeOnWiFiTriggers()->empty());
1927   StartWakeToScanTimer();
1928   StopDHCPLeaseRenewalTimer();
1929   SetLastWakeReason(WakeOnWiFi::kWakeTriggerPattern);
1930   AddResultToLastSSIDResults();
1931   EXPECT_TRUE(WakeToScanTimerIsRunning());
1932   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
1933   EXPECT_CALL(*this, DoneCallback(_)).Times(0);
1934   BeforeSuspendActions(is_connected, start_lease_renewal_timer,
1935                        kTimeToNextLeaseRenewalLong);
1936   EXPECT_FALSE(GetInDarkResume());
1937   EXPECT_EQ(GetWakeOnWiFiTriggers()->size(), 2);
1938   EXPECT_TRUE(
1939       GetWakeOnWiFiTriggers()->find(WakeOnWiFi::kWakeTriggerDisconnect) !=
1940       GetWakeOnWiFiTriggers()->end());
1941   EXPECT_TRUE(GetWakeOnWiFiTriggers()->find(WakeOnWiFi::kWakeTriggerPattern) !=
1942               GetWakeOnWiFiTriggers()->end());
1943   EXPECT_TRUE(DHCPLeaseRenewalTimerIsRunning());
1944   EXPECT_FALSE(WakeToScanTimerIsRunning());
1945   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
1946   EXPECT_TRUE(GetLastSSIDMatchFreqs().empty());
1947 }
1948 
TEST_F(WakeOnWiFiTestWithMockDispatcher,BeforeSuspendActions_DisconnectedBeforeSuspend)1949 TEST_F(WakeOnWiFiTestWithMockDispatcher,
1950        BeforeSuspendActions_DisconnectedBeforeSuspend) {
1951   const bool is_connected = false;
1952   const bool start_lease_renewal_timer = true;
1953   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1),
1954                      GetWakeOnSSIDWhitelist());
1955   AddSSIDToWhitelist(kSSIDBytes2, sizeof(kSSIDBytes2),
1956                      GetWakeOnSSIDWhitelist());
1957   SetSuspendActionsDoneCallback();
1958   EnableWakeOnWiFiFeaturesPacketDarkConnect();
1959 
1960   // Do not start wake to scan timer if there are less whitelisted SSIDs (2)
1961   // than net detect SSIDs we support (10).
1962   SetInDarkResume(true);
1963   GetWakeOnWiFiTriggers()->clear();
1964   StopWakeToScanTimer();
1965   StartDHCPLeaseRenewalTimer();
1966   SetWakeOnWiFiMaxSSIDs(10);
1967   SetLastWakeReason(WakeOnWiFi::kWakeTriggerPattern);
1968   AddResultToLastSSIDResults();
1969   EXPECT_EQ(2, GetWakeOnSSIDWhitelist()->size());
1970   EXPECT_FALSE(WakeToScanTimerIsRunning());
1971   EXPECT_TRUE(DHCPLeaseRenewalTimerIsRunning());
1972   EXPECT_CALL(*this, DoneCallback(_)).Times(0);
1973   BeforeSuspendActions(is_connected, start_lease_renewal_timer,
1974                        kTimeToNextLeaseRenewalLong);
1975   EXPECT_EQ(2, GetWakeOnSSIDWhitelist()->size());
1976   EXPECT_FALSE(GetInDarkResume());
1977   EXPECT_EQ(GetWakeOnWiFiTriggers()->size(), 1);
1978   EXPECT_TRUE(GetWakeOnWiFiTriggers()->find(WakeOnWiFi::kWakeTriggerSSID) !=
1979               GetWakeOnWiFiTriggers()->end());
1980   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
1981   EXPECT_FALSE(WakeToScanTimerIsRunning());
1982   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
1983   EXPECT_TRUE(GetLastSSIDMatchFreqs().empty());
1984 
1985   // Start wake to scan timer if there are more whitelisted SSIDs (2) than
1986   // net detect SSIDs we support (1). Also, truncate the wake on SSID whitelist
1987   // so that it only contains as many SSIDs as we support (1).
1988   SetInDarkResume(true);
1989   GetWakeOnWiFiTriggers()->clear();
1990   StopWakeToScanTimer();
1991   StartDHCPLeaseRenewalTimer();
1992   SetWakeOnWiFiMaxSSIDs(1);
1993   SetLastWakeReason(WakeOnWiFi::kWakeTriggerPattern);
1994   AddResultToLastSSIDResults();
1995   EXPECT_EQ(2, GetWakeOnSSIDWhitelist()->size());
1996   EXPECT_FALSE(WakeToScanTimerIsRunning());
1997   EXPECT_TRUE(DHCPLeaseRenewalTimerIsRunning());
1998   EXPECT_CALL(*this, DoneCallback(_)).Times(0);
1999   BeforeSuspendActions(is_connected, start_lease_renewal_timer,
2000                        kTimeToNextLeaseRenewalLong);
2001   EXPECT_EQ(1, GetWakeOnSSIDWhitelist()->size());
2002   EXPECT_FALSE(GetInDarkResume());
2003   EXPECT_EQ(GetWakeOnWiFiTriggers()->size(), 1);
2004   EXPECT_TRUE(GetWakeOnWiFiTriggers()->find(WakeOnWiFi::kWakeTriggerSSID) !=
2005               GetWakeOnWiFiTriggers()->end());
2006   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
2007   EXPECT_TRUE(WakeToScanTimerIsRunning());
2008   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
2009   EXPECT_TRUE(GetLastSSIDMatchFreqs().empty());
2010 
2011   // Neither add the wake on SSID trigger nor start the wake to scan timer if
2012   // there are no whitelisted SSIDs.
2013   SetInDarkResume(true);
2014   GetWakeOnSSIDWhitelist()->clear();
2015   StopWakeToScanTimer();
2016   StartDHCPLeaseRenewalTimer();
2017   SetWakeOnWiFiMaxSSIDs(10);
2018   SetLastWakeReason(WakeOnWiFi::kWakeTriggerPattern);
2019   AddResultToLastSSIDResults();
2020   EXPECT_TRUE(GetWakeOnSSIDWhitelist()->empty());
2021   EXPECT_FALSE(WakeToScanTimerIsRunning());
2022   EXPECT_TRUE(DHCPLeaseRenewalTimerIsRunning());
2023   EXPECT_CALL(*this, DoneCallback(_)).Times(0);
2024   BeforeSuspendActions(is_connected, start_lease_renewal_timer,
2025                        kTimeToNextLeaseRenewalLong);
2026   EXPECT_TRUE(GetWakeOnSSIDWhitelist()->empty());
2027   EXPECT_FALSE(GetInDarkResume());
2028   EXPECT_TRUE(GetWakeOnWiFiTriggers()->empty());
2029   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
2030   EXPECT_FALSE(WakeToScanTimerIsRunning());
2031   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
2032   EXPECT_TRUE(GetLastSSIDMatchFreqs().empty());
2033 }
2034 
TEST_F(WakeOnWiFiTestWithMockDispatcher,DisableWakeOnWiFi_ClearsTriggers)2035 TEST_F(WakeOnWiFiTestWithMockDispatcher, DisableWakeOnWiFi_ClearsTriggers) {
2036   GetWakeOnWiFiTriggers()->insert(WakeOnWiFi::kWakeTriggerPattern);
2037   EXPECT_FALSE(GetWakeOnWiFiTriggers()->empty());
2038   DisableWakeOnWiFi();
2039   EXPECT_TRUE(GetWakeOnWiFiTriggers()->empty());
2040 }
2041 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ParseWakeOnSSIDResults)2042 TEST_F(WakeOnWiFiTestWithMockDispatcher, ParseWakeOnSSIDResults) {
2043   SetWakeOnPacketConnMessage msg;
2044   NetlinkPacket packet(kWakeReasonSSIDNlMsg, sizeof(kWakeReasonSSIDNlMsg));
2045   msg.InitFromPacket(&packet, GetWakeupReportMsgContext());
2046   AttributeListConstRefPtr triggers;
2047   ASSERT_TRUE(msg.const_attributes()->ConstGetNestedAttributeList(
2048       NL80211_ATTR_WOWLAN_TRIGGERS, &triggers));
2049   AttributeListConstRefPtr results_list;
2050   ASSERT_TRUE(triggers->ConstGetNestedAttributeList(
2051       NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS, &results_list));
2052   WiFi::FreqSet freqs = ParseWakeOnSSIDResults(results_list);
2053   EXPECT_EQ(arraysize(kSSID1FreqMatches), freqs.size());
2054   for (uint32_t freq : kSSID1FreqMatches) {
2055     EXPECT_TRUE(freqs.find(freq) != freqs.end());
2056   }
2057 }
2058 
TEST_F(WakeOnWiFiTestWithMockDispatcher,OnScanStarted_NotInDarkResume)2059 TEST_F(WakeOnWiFiTestWithMockDispatcher, OnScanStarted_NotInDarkResume) {
2060   SetInDarkResume(false);
2061   EXPECT_CALL(metrics_, NotifyScanStartedInDarkResume(_)).Times(0);
2062   OnScanStarted(false);
2063 }
2064 
TEST_F(WakeOnWiFiTestWithMockDispatcher,OnScanStarted_IgnoredWakeReasons)2065 TEST_F(WakeOnWiFiTestWithMockDispatcher, OnScanStarted_IgnoredWakeReasons) {
2066   // Do not log metrics if we entered dark resume because of wake on SSID or
2067   // wake on disconnect.
2068   SetInDarkResume(true);
2069   SetLastWakeReason(WakeOnWiFi::kWakeTriggerSSID);
2070   EXPECT_CALL(metrics_, NotifyScanStartedInDarkResume(_)).Times(0);
2071   OnScanStarted(false);
2072 
2073   SetLastWakeReason(WakeOnWiFi::kWakeTriggerDisconnect);
2074   EXPECT_CALL(metrics_, NotifyScanStartedInDarkResume(_)).Times(0);
2075   OnScanStarted(false);
2076 }
2077 
TEST_F(WakeOnWiFiTestWithMockDispatcher,OnScanStarted_LogMetrics)2078 TEST_F(WakeOnWiFiTestWithMockDispatcher, OnScanStarted_LogMetrics) {
2079   // Log metrics if we entered dark resume because of wake on pattern or an
2080   // unsupported wake reason.
2081   SetInDarkResume(true);
2082   SetLastWakeReason(WakeOnWiFi::kWakeTriggerUnsupported);
2083   EXPECT_CALL(metrics_, NotifyScanStartedInDarkResume(_));
2084   OnScanStarted(false);
2085 
2086   SetLastWakeReason(WakeOnWiFi::kWakeTriggerPattern);
2087   EXPECT_CALL(metrics_, NotifyScanStartedInDarkResume(_));
2088   OnScanStarted(false);
2089 
2090   // Log error if an active scan is launched.
2091   ScopedMockLog log;
2092   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2093   EXPECT_CALL(log,
2094               Log(logging::LOG_ERROR, _,
2095                   HasSubstr("Unexpected active scan launched in dark resume")));
2096   EXPECT_CALL(metrics_, NotifyScanStartedInDarkResume(_));
2097   OnScanStarted(true);
2098 }
2099 
TEST_F(WakeOnWiFiTestWithDispatcher,InitiateScanInDarkResume)2100 TEST_F(WakeOnWiFiTestWithDispatcher, InitiateScanInDarkResume) {
2101   WiFi::FreqSet freqs;
2102 
2103   // If we are not scanning on specific frequencies, do not enable the retry
2104   // mechanism.
2105   EXPECT_EQ(0, GetDarkResumeScanRetriesLeft());
2106   EXPECT_CALL(*this, InitiateScanCallback(freqs));
2107   InitiateScanInDarkResume(freqs);
2108   EXPECT_EQ(0, GetDarkResumeScanRetriesLeft());
2109 
2110   // Otherwise, start channel specific passive scan with retries.
2111   freqs.insert(1);
2112   EXPECT_LE(freqs.size(), WakeOnWiFi::kMaxFreqsForDarkResumeScanRetries);
2113   EXPECT_EQ(0, GetDarkResumeScanRetriesLeft());
2114   EXPECT_CALL(*this, InitiateScanCallback(freqs));
2115   InitiateScanInDarkResume(freqs);
2116   EXPECT_EQ(WakeOnWiFi::kMaxDarkResumeScanRetries,
2117             GetDarkResumeScanRetriesLeft());
2118 }
2119 
TEST_F(WakeOnWiFiTestWithMockDispatcher,AddRemoveWakeOnPacketConnection)2120 TEST_F(WakeOnWiFiTestWithMockDispatcher, AddRemoveWakeOnPacketConnection) {
2121   const string bad_ip_string("1.1");
2122   const string ip_string1("192.168.0.19");
2123   const string ip_string2("192.168.0.55");
2124   const string ip_string3("192.168.0.74");
2125   IPAddress ip_addr1(ip_string1);
2126   IPAddress ip_addr2(ip_string2);
2127   IPAddress ip_addr3(ip_string3);
2128   Error e;
2129 
2130   // Add and remove operations will fail if we provide an invalid IP address
2131   // string.
2132   EnableWakeOnWiFiFeaturesPacket();
2133   AddWakeOnPacketConnection(bad_ip_string, &e);
2134   EXPECT_EQ(e.type(), Error::kInvalidArguments);
2135   EXPECT_STREQ(e.message().c_str(),
2136                ("Invalid ip_address " + bad_ip_string).c_str());
2137   RemoveWakeOnPacketConnection(bad_ip_string, &e);
2138   EXPECT_EQ(e.type(), Error::kInvalidArguments);
2139   EXPECT_STREQ(e.message().c_str(),
2140                ("Invalid ip_address " + bad_ip_string).c_str());
2141 
2142   // Add and remove operations will fail if WiFi device does not support
2143   // pattern matching functionality, even if the feature is enabled.
2144   EnableWakeOnWiFiFeaturesPacket();
2145   ClearWakeOnWiFiTriggersSupported();
2146   AddWakeOnPacketConnection(ip_string1, &e);
2147   EXPECT_EQ(e.type(), Error::kNotSupported);
2148   EXPECT_STREQ(e.message().c_str(),
2149                "Wake on IP address patterns not supported by this WiFi device");
2150   RemoveAllWakeOnPacketConnections(&e);
2151   EXPECT_EQ(e.type(), Error::kNotSupported);
2152   EXPECT_STREQ(e.message().c_str(),
2153                "Wake on IP address patterns not supported by this WiFi device");
2154   RemoveWakeOnPacketConnection(ip_string2, &e);
2155   EXPECT_EQ(e.type(), Error::kNotSupported);
2156   EXPECT_STREQ(e.message().c_str(),
2157                "Wake on IP address patterns not supported by this WiFi device");
2158 
2159   // Add operation will fail if pattern matching is supported but the max number
2160   // of IP address patterns have already been registered.
2161   EnableWakeOnWiFiFeaturesPacketDarkConnect();
2162   GetWakeOnWiFiTriggersSupported()->insert(WakeOnWiFi::kWakeTriggerPattern);
2163   SetWakeOnWiFiMaxPatterns(1);
2164   GetWakeOnPacketConnections()->AddUnique(IPAddress(ip_string1));
2165   AddWakeOnPacketConnection(ip_string2, &e);
2166   EXPECT_EQ(e.type(), Error::kOperationFailed);
2167   EXPECT_STREQ(e.message().c_str(),
2168                "Max number of IP address patterns already registered");
2169 
2170   // Add and remove operations will still execute even when the wake on packet
2171   // feature has been disabled.
2172   GetWakeOnPacketConnections()->Clear();
2173   SetWakeOnWiFiMaxPatterns(50);
2174   DisableWakeOnWiFiFeatures();
2175   GetWakeOnWiFiTriggersSupported()->insert(WakeOnWiFi::kWakeTriggerPattern);
2176   AddWakeOnPacketConnection(ip_string1, &e);
2177   EXPECT_EQ(GetWakeOnPacketConnections()->Count(), 1);
2178   EXPECT_TRUE(GetWakeOnPacketConnections()->Contains(ip_addr1));
2179   AddWakeOnPacketConnection(ip_string2, &e);
2180   EXPECT_EQ(GetWakeOnPacketConnections()->Count(), 2);
2181   EXPECT_TRUE(GetWakeOnPacketConnections()->Contains(ip_addr2));
2182   RemoveWakeOnPacketConnection(ip_string1, &e);
2183   EXPECT_EQ(GetWakeOnPacketConnections()->Count(), 1);
2184   RemoveAllWakeOnPacketConnections(&e);
2185   EXPECT_TRUE(GetWakeOnPacketConnections()->Empty());
2186 
2187   // Normal functioning of add/remove operations when wake on WiFi features
2188   // are enabled, the NIC supports pattern matching, and the max number
2189   // of patterns have not been registered yet.
2190   EnableWakeOnWiFiFeaturesPacketDarkConnect();
2191   GetWakeOnPacketConnections()->Clear();
2192   EXPECT_TRUE(GetWakeOnPacketConnections()->Empty());
2193   AddWakeOnPacketConnection(ip_string1, &e);
2194   EXPECT_EQ(GetWakeOnPacketConnections()->Count(), 1);
2195   EXPECT_TRUE(GetWakeOnPacketConnections()->Contains(ip_addr1));
2196   EXPECT_FALSE(GetWakeOnPacketConnections()->Contains(ip_addr2));
2197   EXPECT_FALSE(GetWakeOnPacketConnections()->Contains(ip_addr3));
2198 
2199   AddWakeOnPacketConnection(ip_string2, &e);
2200   EXPECT_EQ(GetWakeOnPacketConnections()->Count(), 2);
2201   EXPECT_TRUE(GetWakeOnPacketConnections()->Contains(ip_addr1));
2202   EXPECT_TRUE(GetWakeOnPacketConnections()->Contains(ip_addr2));
2203   EXPECT_FALSE(GetWakeOnPacketConnections()->Contains(ip_addr3));
2204 
2205   AddWakeOnPacketConnection(ip_string3, &e);
2206   EXPECT_EQ(GetWakeOnPacketConnections()->Count(), 3);
2207   EXPECT_TRUE(GetWakeOnPacketConnections()->Contains(ip_addr1));
2208   EXPECT_TRUE(GetWakeOnPacketConnections()->Contains(ip_addr2));
2209   EXPECT_TRUE(GetWakeOnPacketConnections()->Contains(ip_addr3));
2210 
2211   RemoveWakeOnPacketConnection(ip_string2, &e);
2212   EXPECT_EQ(GetWakeOnPacketConnections()->Count(), 2);
2213   EXPECT_TRUE(GetWakeOnPacketConnections()->Contains(ip_addr1));
2214   EXPECT_FALSE(GetWakeOnPacketConnections()->Contains(ip_addr2));
2215   EXPECT_TRUE(GetWakeOnPacketConnections()->Contains(ip_addr3));
2216 
2217   // Remove fails if no such address is registered.
2218   RemoveWakeOnPacketConnection(ip_string2, &e);
2219   EXPECT_EQ(e.type(), Error::kNotFound);
2220   EXPECT_STREQ(e.message().c_str(),
2221                "No such IP address match registered to wake device");
2222   EXPECT_EQ(GetWakeOnPacketConnections()->Count(), 2);
2223 
2224   RemoveWakeOnPacketConnection(ip_string1, &e);
2225   EXPECT_EQ(GetWakeOnPacketConnections()->Count(), 1);
2226   EXPECT_FALSE(GetWakeOnPacketConnections()->Contains(ip_addr1));
2227   EXPECT_FALSE(GetWakeOnPacketConnections()->Contains(ip_addr2));
2228   EXPECT_TRUE(GetWakeOnPacketConnections()->Contains(ip_addr3));
2229 
2230   AddWakeOnPacketConnection(ip_string2, &e);
2231   EXPECT_EQ(GetWakeOnPacketConnections()->Count(), 2);
2232   EXPECT_FALSE(GetWakeOnPacketConnections()->Contains(ip_addr1));
2233   EXPECT_TRUE(GetWakeOnPacketConnections()->Contains(ip_addr2));
2234   EXPECT_TRUE(GetWakeOnPacketConnections()->Contains(ip_addr3));
2235 
2236   RemoveAllWakeOnPacketConnections(&e);
2237   EXPECT_EQ(GetWakeOnPacketConnections()->Count(), 0);
2238   EXPECT_FALSE(GetWakeOnPacketConnections()->Contains(ip_addr1));
2239   EXPECT_FALSE(GetWakeOnPacketConnections()->Contains(ip_addr2));
2240   EXPECT_FALSE(GetWakeOnPacketConnections()->Contains(ip_addr3));
2241 }
2242 
TEST_F(WakeOnWiFiTestWithDispatcher,OnBeforeSuspend_ClearsEventHistory)2243 TEST_F(WakeOnWiFiTestWithDispatcher, OnBeforeSuspend_ClearsEventHistory) {
2244   const int kNumEvents = WakeOnWiFi::kMaxDarkResumesPerPeriodShort - 1;
2245   vector<ByteString> whitelist;
2246   for (int i = 0; i < kNumEvents; ++i) {
2247     GetDarkResumeHistory()->RecordEvent();
2248   }
2249   EXPECT_EQ(kNumEvents, GetDarkResumeHistory()->Size());
2250   OnBeforeSuspend(true, whitelist, true, 0);
2251   EXPECT_TRUE(GetDarkResumeHistory()->Empty());
2252 }
2253 
TEST_F(WakeOnWiFiTestWithDispatcher,OnBeforeSuspend_SetsWakeOnSSIDWhitelist)2254 TEST_F(WakeOnWiFiTestWithDispatcher, OnBeforeSuspend_SetsWakeOnSSIDWhitelist) {
2255   vector<ByteString> whitelist;
2256   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2257   EXPECT_TRUE(GetWakeOnSSIDWhitelist()->empty());
2258   OnBeforeSuspend(true, whitelist, true, 0);
2259   EXPECT_FALSE(GetWakeOnSSIDWhitelist()->empty());
2260   EXPECT_EQ(1, GetWakeOnSSIDWhitelist()->size());
2261 }
2262 
TEST_F(WakeOnWiFiTestWithDispatcher,OnBeforeSuspend_SetsDoneCallback)2263 TEST_F(WakeOnWiFiTestWithDispatcher, OnBeforeSuspend_SetsDoneCallback) {
2264   vector<ByteString> whitelist;
2265   EXPECT_TRUE(SuspendActionsCallbackIsNull());
2266   OnBeforeSuspend(true, whitelist, true, 0);
2267   EXPECT_FALSE(SuspendActionsCallbackIsNull());
2268 }
2269 
TEST_F(WakeOnWiFiTestWithMockDispatcher,OnBeforeSuspend_DHCPLeaseRenewal)2270 TEST_F(WakeOnWiFiTestWithMockDispatcher, OnBeforeSuspend_DHCPLeaseRenewal) {
2271   bool is_connected;
2272   bool have_dhcp_lease;
2273   vector<ByteString> whitelist;
2274   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2275   // If we are connected the time to next lease renewal is short enough, we will
2276   // initiate DHCP lease renewal immediately.
2277   is_connected = true;
2278   have_dhcp_lease = true;
2279   EXPECT_CALL(*this, RenewDHCPLeaseCallback()).Times(1);
2280   EXPECT_CALL(mock_dispatcher_, PostTask(_)).Times(1);
2281   OnBeforeSuspend(is_connected, whitelist, have_dhcp_lease,
2282                   kTimeToNextLeaseRenewalShort);
2283 
2284   // No immediate DHCP lease renewal because we are not connected.
2285   is_connected = false;
2286   have_dhcp_lease = true;
2287   EXPECT_CALL(*this, RenewDHCPLeaseCallback()).Times(0);
2288   EXPECT_CALL(mock_dispatcher_, PostTask(_)).Times(1);
2289   OnBeforeSuspend(is_connected, whitelist, have_dhcp_lease,
2290                   kTimeToNextLeaseRenewalShort);
2291 
2292   // No immediate DHCP lease renewal because the time to the next lease renewal
2293   // is longer than the threshold.
2294   is_connected = true;
2295   have_dhcp_lease = true;
2296   EXPECT_CALL(*this, RenewDHCPLeaseCallback()).Times(0);
2297   EXPECT_CALL(mock_dispatcher_, PostTask(_)).Times(1);
2298   OnBeforeSuspend(is_connected, whitelist, have_dhcp_lease,
2299                   kTimeToNextLeaseRenewalLong);
2300 
2301   // No immediate DHCP lease renewal because we do not have a DHCP lease that
2302   // needs to be renewed.
2303   is_connected = true;
2304   have_dhcp_lease = false;
2305   EXPECT_CALL(*this, RenewDHCPLeaseCallback()).Times(0);
2306   EXPECT_CALL(mock_dispatcher_, PostTask(_)).Times(1);
2307   OnBeforeSuspend(is_connected, whitelist, have_dhcp_lease,
2308                   kTimeToNextLeaseRenewalLong);
2309 }
2310 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_ResetsDarkResumeScanRetries)2311 TEST_F(WakeOnWiFiTestWithDispatcher, OnDarkResume_ResetsDarkResumeScanRetries) {
2312   const bool is_connected = true;
2313   vector<ByteString> whitelist;
2314   SetDarkResumeScanRetriesLeft(3);
2315   EXPECT_EQ(3, GetDarkResumeScanRetriesLeft());
2316   OnDarkResume(is_connected, whitelist);
2317   EXPECT_EQ(0, GetDarkResumeScanRetriesLeft());
2318 }
2319 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_SetsWakeOnSSIDWhitelist)2320 TEST_F(WakeOnWiFiTestWithDispatcher, OnDarkResume_SetsWakeOnSSIDWhitelist) {
2321   const bool is_connected = true;
2322   vector<ByteString> whitelist;
2323   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2324   EXPECT_TRUE(GetWakeOnSSIDWhitelist()->empty());
2325   OnDarkResume(is_connected, whitelist);
2326   EXPECT_FALSE(GetWakeOnSSIDWhitelist()->empty());
2327   EXPECT_EQ(1, GetWakeOnSSIDWhitelist()->size());
2328 }
2329 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_WakeReasonUnsupported_Connected_Timeout)2330 TEST_F(WakeOnWiFiTestWithDispatcher,
2331        OnDarkResume_WakeReasonUnsupported_Connected_Timeout) {
2332   // Test that correct actions are taken if we enter OnDarkResume on an
2333   // unsupported wake trigger while connected, then timeout on suspend actions
2334   // before suspending again.
2335   const bool is_connected = true;
2336   SetLastWakeReason(WakeOnWiFi::kWakeTriggerUnsupported);
2337   vector<ByteString> whitelist;
2338   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2339   InitStateForDarkResume();
2340   EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
2341   // Renew DHCP lease if we are connected in dark resume.
2342   EXPECT_CALL(*this, RenewDHCPLeaseCallback());
2343   EXPECT_CALL(metrics_, NotifyWakeOnWiFiOnDarkResume(
2344                             WakeOnWiFi::kWakeTriggerUnsupported));
2345   OnDarkResume(is_connected, whitelist);
2346   EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
2347   // Trigger timeout callback.
2348   // Since we timeout, we are disconnected before suspend.
2349   StartDHCPLeaseRenewalTimer();
2350   SetExpectationsDisconnectedBeforeSuspend();
2351   dispatcher_.DispatchPendingEvents();
2352   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
2353   VerifyStateDisconnectedBeforeSuspend();
2354 }
2355 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_WakeReasonUnsupported_Connected_NoAutoconnectableServices)2356 TEST_F(WakeOnWiFiTestWithDispatcher,
2357        OnDarkResume_WakeReasonUnsupported_Connected_NoAutoconnectableServices) {
2358   // Test that correct actions are taken if we enter OnDarkResume on an
2359   // unsupported wake trigger while connected, then go back to suspend because
2360   // we could not find any services available for autoconnect.
2361   const bool is_connected = true;
2362   SetLastWakeReason(WakeOnWiFi::kWakeTriggerUnsupported);
2363   vector<ByteString> whitelist;
2364   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2365   InitStateForDarkResume();
2366   EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
2367   // Renew DHCP lease if we are connected in dark resume.
2368   EXPECT_CALL(*this, RenewDHCPLeaseCallback());
2369   EXPECT_CALL(metrics_, NotifyWakeOnWiFiOnDarkResume(
2370                             WakeOnWiFi::kWakeTriggerUnsupported));
2371   OnDarkResume(is_connected, whitelist);
2372   EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
2373 
2374   StartDHCPLeaseRenewalTimer();
2375   SetExpectationsDisconnectedBeforeSuspend();
2376   OnNoAutoConnectableServicesAfterScan(whitelist);
2377   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
2378   VerifyStateDisconnectedBeforeSuspend();
2379 }
2380 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_WakeReasonUnsupported_Connected_LeaseObtained)2381 TEST_F(WakeOnWiFiTestWithDispatcher,
2382        OnDarkResume_WakeReasonUnsupported_Connected_LeaseObtained) {
2383   // Test that correct actions are taken if we enter OnDarkResume on an
2384   // unsupported wake trigger while connected, then connect and obtain a DHCP
2385   // lease before suspending again.
2386   const bool is_connected = true;
2387   const bool have_dhcp_lease = true;
2388   const uint32_t time_to_next_lease_renewal = 10;
2389   SetLastWakeReason(WakeOnWiFi::kWakeTriggerUnsupported);
2390   vector<ByteString> whitelist;
2391   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2392   InitStateForDarkResume();
2393   EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
2394   // Renew DHCP lease if we are connected in dark resume.
2395   EXPECT_CALL(*this, RenewDHCPLeaseCallback());
2396   EXPECT_CALL(metrics_, NotifyWakeOnWiFiOnDarkResume(
2397                             WakeOnWiFi::kWakeTriggerUnsupported));
2398   OnDarkResume(is_connected, whitelist);
2399   EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
2400   // Lease obtained.
2401   // Since a lease is obtained, we are connected before suspend.
2402   StopDHCPLeaseRenewalTimer();
2403   StartWakeToScanTimer();
2404   SetExpectationsConnectedBeforeSuspend();
2405   OnConnectedAndReachable(have_dhcp_lease, time_to_next_lease_renewal);
2406   EXPECT_TRUE(DHCPLeaseRenewalTimerIsRunning());
2407   EXPECT_FALSE(WakeToScanTimerIsRunning());
2408   VerifyStateConnectedBeforeSuspend();
2409 }
2410 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_WakeReasonUnsupported_NotConnected_Timeout)2411 TEST_F(WakeOnWiFiTestWithDispatcher,
2412        OnDarkResume_WakeReasonUnsupported_NotConnected_Timeout) {
2413   // Test that correct actions are taken if we enter OnDarkResume on an
2414   // unsupported wake trigger while not connected, then timeout on suspend
2415   // actions before suspending again.
2416   const bool is_connected = false;
2417   SetLastWakeReason(WakeOnWiFi::kWakeTriggerUnsupported);
2418   vector<ByteString> whitelist;
2419   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2420   InitStateForDarkResume();
2421   EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
2422   // Initiate scan if we are not connected in dark resume.
2423   EXPECT_CALL(*this, RemoveSupplicantNetworksCallback());
2424   EXPECT_CALL(metrics_, NotifyDarkResumeInitiateScan());
2425   EXPECT_CALL(*this, InitiateScanCallback(_));
2426   EXPECT_CALL(metrics_, NotifyWakeOnWiFiOnDarkResume(
2427                             WakeOnWiFi::kWakeTriggerUnsupported));
2428   OnDarkResume(is_connected, whitelist);
2429   EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
2430   // Trigger timeout callback.
2431   // Since we timeout, we are disconnected before suspend.
2432   StartDHCPLeaseRenewalTimer();
2433   SetExpectationsDisconnectedBeforeSuspend();
2434   dispatcher_.DispatchPendingEvents();
2435   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
2436   VerifyStateDisconnectedBeforeSuspend();
2437 }
2438 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_WakeReasonUnsupported_NotConnected_NoAutoconnectableServices)2439 TEST_F(
2440     WakeOnWiFiTestWithDispatcher,
2441     OnDarkResume_WakeReasonUnsupported_NotConnected_NoAutoconnectableServices) {
2442   // Test that correct actions are taken if we enter OnDarkResume on an
2443   // unsupported wake trigger while not connected, then go back to suspend
2444   // because we could not find any services available for autoconnect.
2445   const bool is_connected = false;
2446   SetLastWakeReason(WakeOnWiFi::kWakeTriggerUnsupported);
2447   vector<ByteString> whitelist;
2448   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2449   InitStateForDarkResume();
2450   EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
2451   // Initiate scan if we are not connected in dark resume.
2452   EXPECT_CALL(*this, RemoveSupplicantNetworksCallback());
2453   EXPECT_CALL(metrics_, NotifyDarkResumeInitiateScan());
2454   EXPECT_CALL(*this, InitiateScanCallback(_));
2455   EXPECT_CALL(metrics_, NotifyWakeOnWiFiOnDarkResume(
2456                             WakeOnWiFi::kWakeTriggerUnsupported));
2457   OnDarkResume(is_connected, whitelist);
2458   EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
2459 
2460   StartDHCPLeaseRenewalTimer();
2461   SetExpectationsDisconnectedBeforeSuspend();
2462   OnNoAutoConnectableServicesAfterScan(whitelist);
2463   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
2464   VerifyStateDisconnectedBeforeSuspend();
2465 }
2466 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_WakeReasonUnsupported_NotConnected_LeaseObtained)2467 TEST_F(WakeOnWiFiTestWithDispatcher,
2468        OnDarkResume_WakeReasonUnsupported_NotConnected_LeaseObtained) {
2469   // Test that correct actions are taken if we enter OnDarkResume on an
2470   // unsupported wake trigger while connected, then connect and obtain a DHCP
2471   // lease before suspending again.
2472   const bool is_connected = false;
2473   const bool have_dhcp_lease = true;
2474   const uint32_t time_to_next_lease_renewal = 10;
2475   SetLastWakeReason(WakeOnWiFi::kWakeTriggerUnsupported);
2476   vector<ByteString> whitelist;
2477   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2478   InitStateForDarkResume();
2479   EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
2480   // Initiate scan if we are not connected in dark resume.
2481   EXPECT_CALL(*this, RemoveSupplicantNetworksCallback());
2482   EXPECT_CALL(metrics_, NotifyDarkResumeInitiateScan());
2483   EXPECT_CALL(*this, InitiateScanCallback(_));
2484   EXPECT_CALL(metrics_, NotifyWakeOnWiFiOnDarkResume(
2485                             WakeOnWiFi::kWakeTriggerUnsupported));
2486   OnDarkResume(is_connected, whitelist);
2487   EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
2488   // Lease obtained.
2489   // Since a lease is obtained, we are connected before suspend.
2490   StopDHCPLeaseRenewalTimer();
2491   StartWakeToScanTimer();
2492   SetExpectationsConnectedBeforeSuspend();
2493   OnConnectedAndReachable(have_dhcp_lease, time_to_next_lease_renewal);
2494   EXPECT_TRUE(DHCPLeaseRenewalTimerIsRunning());
2495   EXPECT_FALSE(WakeToScanTimerIsRunning());
2496   VerifyStateConnectedBeforeSuspend();
2497 }
2498 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_WakeReasonPattern)2499 TEST_F(WakeOnWiFiTestWithDispatcher, OnDarkResume_WakeReasonPattern) {
2500   // Test that correct actions are taken if we enter dark resume because the
2501   // system woke on a packet pattern match. We assume that we wake connected and
2502   // and go back to sleep connected if we wake on pattern.
2503   const bool is_connected = true;
2504   SetLastWakeReason(WakeOnWiFi::kWakeTriggerPattern);
2505   vector<ByteString> whitelist;
2506   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2507 
2508   InitStateForDarkResume();
2509   EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
2510   EXPECT_CALL(metrics_,
2511               NotifyWakeOnWiFiOnDarkResume(WakeOnWiFi::kWakeTriggerPattern));
2512   OnDarkResume(is_connected, whitelist);
2513   EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
2514 
2515   StartWakeToScanTimer();
2516   SetExpectationsConnectedBeforeSuspend();
2517   dispatcher_.DispatchPendingEvents();
2518   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
2519   EXPECT_FALSE(WakeToScanTimerIsRunning());
2520   VerifyStateConnectedBeforeSuspend();
2521 }
2522 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_WakeReasonDisconnect_NoAutoconnectableServices)2523 TEST_F(WakeOnWiFiTestWithDispatcher,
2524        OnDarkResume_WakeReasonDisconnect_NoAutoconnectableServices) {
2525   // Test that correct actions are taken if we enter dark resume because the
2526   // system woke on a disconnect, and go back to suspend because we could not
2527   // find any networks available for autoconnect.
2528   const bool is_connected = false;
2529   SetLastWakeReason(WakeOnWiFi::kWakeTriggerDisconnect);
2530   vector<ByteString> whitelist;
2531   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2532 
2533   InitStateForDarkResume();
2534   EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
2535   EXPECT_CALL(*this, RemoveSupplicantNetworksCallback());
2536   EXPECT_CALL(metrics_, NotifyDarkResumeInitiateScan());
2537   EXPECT_CALL(*this, InitiateScanCallback(_));
2538   EXPECT_CALL(metrics_,
2539               NotifyWakeOnWiFiOnDarkResume(WakeOnWiFi::kWakeTriggerDisconnect));
2540   OnDarkResume(is_connected, whitelist);
2541   EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
2542 
2543   StartDHCPLeaseRenewalTimer();
2544   SetExpectationsDisconnectedBeforeSuspend();
2545   OnNoAutoConnectableServicesAfterScan(whitelist);
2546   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
2547   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
2548   VerifyStateDisconnectedBeforeSuspend();
2549 }
2550 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_WakeReasonDisconnect_Timeout)2551 TEST_F(WakeOnWiFiTestWithDispatcher,
2552        OnDarkResume_WakeReasonDisconnect_Timeout) {
2553   // Test that correct actions are taken if we enter dark resume because the
2554   // system woke on a disconnect, then timeout on suspend actions before
2555   // suspending again.
2556   const bool is_connected = false;
2557   SetLastWakeReason(WakeOnWiFi::kWakeTriggerDisconnect);
2558   vector<ByteString> whitelist;
2559   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2560 
2561   InitStateForDarkResume();
2562   EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
2563   EXPECT_CALL(*this, RemoveSupplicantNetworksCallback());
2564   EXPECT_CALL(metrics_, NotifyDarkResumeInitiateScan());
2565   EXPECT_CALL(*this, InitiateScanCallback(_));
2566   EXPECT_CALL(metrics_,
2567               NotifyWakeOnWiFiOnDarkResume(WakeOnWiFi::kWakeTriggerDisconnect));
2568   OnDarkResume(is_connected, whitelist);
2569   EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
2570 
2571   StartDHCPLeaseRenewalTimer();
2572   SetExpectationsDisconnectedBeforeSuspend();
2573   dispatcher_.DispatchPendingEvents();
2574   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
2575   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
2576   VerifyStateDisconnectedBeforeSuspend();
2577 }
2578 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_WakeReasonDisconnect_LeaseObtained)2579 TEST_F(WakeOnWiFiTestWithDispatcher,
2580        OnDarkResume_WakeReasonDisconnect_LeaseObtained) {
2581   // Test that correct actions are taken if we enter dark resume because the
2582   // system woke on a disconnect, then connect and obtain a DHCP lease before
2583   // suspending again.
2584   const bool is_connected = false;
2585   const bool have_dhcp_lease = true;
2586   const uint32_t time_to_next_lease_renewal = 10;
2587   SetLastWakeReason(WakeOnWiFi::kWakeTriggerDisconnect);
2588   vector<ByteString> whitelist;
2589   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2590 
2591   InitStateForDarkResume();
2592   EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
2593   EXPECT_CALL(*this, RemoveSupplicantNetworksCallback());
2594   EXPECT_CALL(metrics_, NotifyDarkResumeInitiateScan());
2595   EXPECT_CALL(*this, InitiateScanCallback(_));
2596   EXPECT_CALL(metrics_,
2597               NotifyWakeOnWiFiOnDarkResume(WakeOnWiFi::kWakeTriggerDisconnect));
2598   OnDarkResume(is_connected, whitelist);
2599   EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
2600 
2601   StopDHCPLeaseRenewalTimer();
2602   StartWakeToScanTimer();
2603   SetExpectationsConnectedBeforeSuspend();
2604   OnConnectedAndReachable(have_dhcp_lease, time_to_next_lease_renewal);
2605   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
2606   EXPECT_TRUE(DHCPLeaseRenewalTimerIsRunning());
2607   EXPECT_FALSE(WakeToScanTimerIsRunning());
2608   VerifyStateConnectedBeforeSuspend();
2609 }
2610 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_WakeReasonSSID_NoAutoconnectableServices)2611 TEST_F(WakeOnWiFiTestWithDispatcher,
2612        OnDarkResume_WakeReasonSSID_NoAutoconnectableServices) {
2613   // Test that correct actions are taken if we enter dark resume because the
2614   // system woke on SSID, and go back to suspend because we could not find any
2615   // networks available for autoconnect.
2616   const bool is_connected = false;
2617   SetLastWakeReason(WakeOnWiFi::kWakeTriggerSSID);
2618   vector<ByteString> whitelist;
2619   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2620 
2621   InitStateForDarkResume();
2622   EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
2623   EXPECT_CALL(*this, RemoveSupplicantNetworksCallback());
2624   EXPECT_CALL(metrics_, NotifyDarkResumeInitiateScan());
2625   EXPECT_CALL(*this, InitiateScanCallback(_));
2626   EXPECT_CALL(metrics_,
2627               NotifyWakeOnWiFiOnDarkResume(WakeOnWiFi::kWakeTriggerSSID));
2628   OnDarkResume(is_connected, whitelist);
2629   EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
2630 
2631   StartDHCPLeaseRenewalTimer();
2632   SetExpectationsDisconnectedBeforeSuspend();
2633   OnNoAutoConnectableServicesAfterScan(whitelist);
2634   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
2635   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
2636   VerifyStateDisconnectedBeforeSuspend();
2637 }
2638 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_WakeReasonSSID_Timeout)2639 TEST_F(WakeOnWiFiTestWithDispatcher, OnDarkResume_WakeReasonSSID_Timeout) {
2640   // Test that correct actions are taken if we enter dark resume because the
2641   // system woke on SSID, then timeout on suspend actions before suspending
2642   // again.
2643   const bool is_connected = false;
2644   SetLastWakeReason(WakeOnWiFi::kWakeTriggerSSID);
2645   vector<ByteString> whitelist;
2646   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2647 
2648   InitStateForDarkResume();
2649   EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
2650   EXPECT_CALL(*this, RemoveSupplicantNetworksCallback());
2651   EXPECT_CALL(metrics_, NotifyDarkResumeInitiateScan());
2652   EXPECT_CALL(*this, InitiateScanCallback(GetLastSSIDMatchFreqs()));
2653   EXPECT_CALL(metrics_,
2654               NotifyWakeOnWiFiOnDarkResume(WakeOnWiFi::kWakeTriggerSSID));
2655   OnDarkResume(is_connected, whitelist);
2656   EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
2657 
2658   StartDHCPLeaseRenewalTimer();
2659   SetExpectationsDisconnectedBeforeSuspend();
2660   dispatcher_.DispatchPendingEvents();
2661   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
2662   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
2663   VerifyStateDisconnectedBeforeSuspend();
2664 }
2665 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_WakeReasonSSID_LeaseObtained)2666 TEST_F(WakeOnWiFiTestWithDispatcher,
2667        OnDarkResume_WakeReasonSSID_LeaseObtained) {
2668   // Test that correct actions are taken if we enter dark resume because the
2669   // system woke on SSID, then connect and obtain a DHCP lease before suspending
2670   // again.
2671   const bool is_connected = false;
2672   const bool have_dhcp_lease = true;
2673   const uint32_t time_to_next_lease_renewal = 10;
2674   SetLastWakeReason(WakeOnWiFi::kWakeTriggerSSID);
2675   vector<ByteString> whitelist;
2676   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2677 
2678   InitStateForDarkResume();
2679   EXPECT_TRUE(DarkResumeActionsTimeOutCallbackIsCancelled());
2680   EXPECT_CALL(*this, RemoveSupplicantNetworksCallback());
2681   EXPECT_CALL(metrics_, NotifyDarkResumeInitiateScan());
2682   EXPECT_CALL(*this, InitiateScanCallback(GetLastSSIDMatchFreqs()));
2683   EXPECT_CALL(metrics_,
2684               NotifyWakeOnWiFiOnDarkResume(WakeOnWiFi::kWakeTriggerSSID));
2685   OnDarkResume(is_connected, whitelist);
2686   EXPECT_FALSE(DarkResumeActionsTimeOutCallbackIsCancelled());
2687 
2688   StopDHCPLeaseRenewalTimer();
2689   StartWakeToScanTimer();
2690   SetExpectationsConnectedBeforeSuspend();
2691   OnConnectedAndReachable(have_dhcp_lease, time_to_next_lease_renewal);
2692   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
2693   EXPECT_TRUE(DHCPLeaseRenewalTimerIsRunning());
2694   EXPECT_FALSE(WakeToScanTimerIsRunning());
2695   VerifyStateConnectedBeforeSuspend();
2696 }
2697 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_Connected_DoNotRecordEvent)2698 TEST_F(WakeOnWiFiTestWithDispatcher, OnDarkResume_Connected_DoNotRecordEvent) {
2699   const bool is_connected = true;
2700   vector<ByteString> whitelist;
2701   EXPECT_TRUE(GetDarkResumeHistory()->Empty());
2702   OnDarkResume(is_connected, whitelist);
2703   EXPECT_TRUE(GetDarkResumeHistory()->Empty());
2704 }
2705 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_NotConnected_RecordEvent)2706 TEST_F(WakeOnWiFiTestWithDispatcher, OnDarkResume_NotConnected_RecordEvent) {
2707   const bool is_connected = false;
2708   vector<ByteString> whitelist;
2709   EXPECT_TRUE(GetDarkResumeHistory()->Empty());
2710   OnDarkResume(is_connected, whitelist);
2711   EXPECT_EQ(1, GetDarkResumeHistory()->Size());
2712 }
2713 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_NotConnected_MaxDarkResumes_ShortPeriod)2714 TEST_F(WakeOnWiFiTestWithDispatcher,
2715        OnDarkResume_NotConnected_MaxDarkResumes_ShortPeriod) {
2716   // These 3 dark resume timings are within a 1 minute interval, so as to
2717   // trigger the short throttling threshold (3 in 1 minute).
2718   const int kTimeSeconds[] = {10, 20, 30};
2719   CHECK_EQ(static_cast<const unsigned int>(
2720                WakeOnWiFi::kMaxDarkResumesPerPeriodShort),
2721            arraysize(kTimeSeconds));
2722   vector<ByteString> whitelist;
2723 
2724   // This test assumes that throttling takes place when 3 dark resumes have
2725   // been triggered in the last 1 minute.
2726   EXPECT_EQ(3, WakeOnWiFi::kMaxDarkResumesPerPeriodShort);
2727   EXPECT_EQ(1, WakeOnWiFi::kDarkResumeFrequencySamplingPeriodShortMinutes);
2728 
2729   // Wake on SSID dark resumes should be recorded in the dark resume history.
2730   const bool is_connected = false;
2731   SetLastWakeReason(WakeOnWiFi::kWakeTriggerSSID);
2732   EXPECT_TRUE(GetDarkResumeHistory()->Empty());
2733 
2734   // First two dark resumes take place at 10 and 20 seconds respectively. This
2735   // is still within the throttling threshold.
2736   for (int i = 0; i < WakeOnWiFi::kMaxDarkResumesPerPeriodShort - 1; ++i) {
2737     EXPECT_CALL(metrics_, NotifyWakeOnWiFiThrottled()).Times(0);
2738     EXPECT_CALL(time_, GetNow())
2739         .WillRepeatedly(Return(GetTimestampBootTime(kTimeSeconds[i])));
2740     OnDarkResume(is_connected, whitelist);
2741   }
2742   SetInDarkResume(false);  // this happens after BeforeSuspendActions
2743   EXPECT_EQ(WakeOnWiFi::kMaxDarkResumesPerPeriodShort - 1,
2744             GetDarkResumeHistory()->Size());
2745 
2746   // The 3rd dark resume takes place at 30 seconds, which makes 3 dark resumes
2747   // in the past minute. Disable wake on WiFi and start wake to scan timer.
2748   ResetSuspendActionsDoneCallback();
2749   StartDHCPLeaseRenewalTimer();
2750   StopWakeToScanTimer();
2751   EXPECT_TRUE(SuspendActionsCallbackIsNull());
2752   EXPECT_TRUE(DHCPLeaseRenewalTimerIsRunning());
2753   EXPECT_FALSE(WakeToScanTimerIsRunning());
2754   EXPECT_FALSE(GetDarkResumeHistory()->Empty());
2755   EXPECT_CALL(metrics_, NotifyWakeOnWiFiThrottled());
2756   EXPECT_CALL(time_, GetNow())
2757       .WillRepeatedly(Return(GetTimestampBootTime(
2758           kTimeSeconds[WakeOnWiFi::kMaxDarkResumesPerPeriodShort - 1])));
2759   OnDarkResume(is_connected, whitelist);
2760   EXPECT_FALSE(SuspendActionsCallbackIsNull());
2761   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
2762   EXPECT_TRUE(WakeToScanTimerIsRunning());
2763   EXPECT_TRUE(GetDarkResumeHistory()->Empty());
2764   EXPECT_FALSE(GetInDarkResume());
2765 }
2766 
TEST_F(WakeOnWiFiTestWithDispatcher,OnDarkResume_NotConnected_MaxDarkResumes_LongPeriod)2767 TEST_F(WakeOnWiFiTestWithDispatcher,
2768        OnDarkResume_NotConnected_MaxDarkResumes_LongPeriod) {
2769   // These 10 dark resume timings are spaced 1 minute apart so as to trigger the
2770   // long throttling threshold (10 in 10 minute) without triggering the short
2771   // throttling threshold (3 in 1 minute).
2772   const int kTimeSeconds[] = {10, 70, 130, 190, 250, 310, 370, 430, 490, 550};
2773   CHECK_EQ(
2774       static_cast<const unsigned int>(WakeOnWiFi::kMaxDarkResumesPerPeriodLong),
2775       arraysize(kTimeSeconds));
2776   vector<ByteString> whitelist;
2777 
2778   // This test assumes that throttling takes place when 3 dark resumes have been
2779   // triggered in the last 1 minute, or when 10 dark resumes have been triggered
2780   // in the last 10 minutes.
2781   EXPECT_EQ(3, WakeOnWiFi::kMaxDarkResumesPerPeriodShort);
2782   EXPECT_EQ(1, WakeOnWiFi::kDarkResumeFrequencySamplingPeriodShortMinutes);
2783   EXPECT_EQ(10, WakeOnWiFi::kMaxDarkResumesPerPeriodLong);
2784   EXPECT_EQ(10, WakeOnWiFi::kDarkResumeFrequencySamplingPeriodLongMinutes);
2785 
2786   // Wake on SSID dark resumes should be recorded in the dark resume history.
2787   const bool is_connected = false;
2788   SetLastWakeReason(WakeOnWiFi::kWakeTriggerSSID);
2789   EXPECT_TRUE(GetDarkResumeHistory()->Empty());
2790 
2791   // The first 9 dark resumes happen once per minute. This is still within the
2792   // throttling threshold.
2793   for (int i = 0; i < WakeOnWiFi::kMaxDarkResumesPerPeriodLong - 1; ++i) {
2794     EXPECT_CALL(metrics_, NotifyWakeOnWiFiThrottled()).Times(0);
2795     EXPECT_CALL(time_, GetNow())
2796         .WillRepeatedly(Return(GetTimestampBootTime(kTimeSeconds[i])));
2797     OnDarkResume(is_connected, whitelist);
2798   }
2799   SetInDarkResume(false);  // this happens after BeforeSuspendActions
2800   EXPECT_EQ(WakeOnWiFi::kMaxDarkResumesPerPeriodLong - 1,
2801             GetDarkResumeHistory()->Size());
2802 
2803   // The occurrence of the 10th dark resume makes 10 dark resumes in the past 10
2804   // minutes. Disable wake on WiFi and start wake to scan timer.
2805   ResetSuspendActionsDoneCallback();
2806   StartDHCPLeaseRenewalTimer();
2807   StopWakeToScanTimer();
2808   EXPECT_TRUE(SuspendActionsCallbackIsNull());
2809   EXPECT_TRUE(DHCPLeaseRenewalTimerIsRunning());
2810   EXPECT_FALSE(WakeToScanTimerIsRunning());
2811   EXPECT_FALSE(GetDarkResumeHistory()->Empty());
2812   EXPECT_CALL(metrics_, NotifyWakeOnWiFiThrottled());
2813   EXPECT_CALL(time_, GetNow())
2814       .WillRepeatedly(Return(GetTimestampBootTime(
2815           kTimeSeconds[WakeOnWiFi::kMaxDarkResumesPerPeriodLong - 1])));
2816   OnDarkResume(is_connected, whitelist);
2817   EXPECT_FALSE(SuspendActionsCallbackIsNull());
2818   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
2819   EXPECT_TRUE(WakeToScanTimerIsRunning());
2820   EXPECT_TRUE(GetDarkResumeHistory()->Empty());
2821   EXPECT_FALSE(GetInDarkResume());
2822   EXPECT_TRUE(GetLastSSIDMatchFreqs().empty());
2823 }
2824 
TEST_F(WakeOnWiFiTestWithMockDispatcher,OnConnectedAndReachable)2825 TEST_F(WakeOnWiFiTestWithMockDispatcher, OnConnectedAndReachable) {
2826   const bool start_lease_renewal_timer = true;
2827   ScopedMockLog log;
2828 
2829   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
2830   SetInDarkResume(true);
2831   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
2832   ScopeLogger::GetInstance()->set_verbose_level(3);
2833   EXPECT_CALL(log, Log(_, _, HasSubstr("BeforeSuspendActions")));
2834   OnConnectedAndReachable(start_lease_renewal_timer,
2835                           kTimeToNextLeaseRenewalLong);
2836 
2837   SetInDarkResume(false);
2838   EXPECT_CALL(log, Log(_, _, HasSubstr("Not in dark resume, so do nothing")));
2839   OnConnectedAndReachable(start_lease_renewal_timer,
2840                           kTimeToNextLeaseRenewalLong);
2841   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
2842   ScopeLogger::GetInstance()->set_verbose_level(0);
2843 }
2844 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabledAfterResume)2845 TEST_F(WakeOnWiFiTestWithMockDispatcher, WakeOnWiFiDisabledAfterResume) {
2846   // At least one wake on WiFi trigger supported and Wake on WiFi features
2847   // are enabled, so disable Wake on WiFi on resume.]
2848   EnableWakeOnWiFiFeaturesPacketDarkConnect();
2849   GetWakeOnWiFiTriggers()->insert(WakeOnWiFi::kWakeTriggerPattern);
2850   EXPECT_CALL(netlink_manager_,
2851               SendNl80211Message(IsDisableWakeOnWiFiMsg(), _, _, _)).Times(1);
2852   EXPECT_CALL(metrics_, NotifySuspendWithWakeOnWiFiEnabledDone()).Times(1);
2853   OnAfterResume();
2854 
2855   // No wake no WiFi triggers supported, so do nothing.
2856   ClearWakeOnWiFiTriggersSupported();
2857   EXPECT_CALL(netlink_manager_,
2858               SendNl80211Message(IsDisableWakeOnWiFiMsg(), _, _, _)).Times(0);
2859   EXPECT_CALL(metrics_, NotifySuspendWithWakeOnWiFiEnabledDone()).Times(0);
2860   OnAfterResume();
2861 
2862   // Wake on WiFi features disabled, so do nothing.
2863   GetWakeOnWiFiTriggersSupported()->insert(WakeOnWiFi::kWakeTriggerPattern);
2864   DisableWakeOnWiFiFeatures();
2865   EXPECT_CALL(netlink_manager_,
2866               SendNl80211Message(IsDisableWakeOnWiFiMsg(), _, _, _)).Times(0);
2867   EXPECT_CALL(metrics_, NotifySuspendWithWakeOnWiFiEnabledDone()).Times(0);
2868   OnAfterResume();
2869 
2870   // Both WakeOnWiFi triggers are empty and Wake on WiFi features are disabled,
2871   // so do nothing.
2872   ClearWakeOnWiFiTriggersSupported();
2873   DisableWakeOnWiFiFeatures();
2874   EXPECT_CALL(netlink_manager_,
2875               SendNl80211Message(IsDisableWakeOnWiFiMsg(), _, _, _)).Times(0);
2876   EXPECT_CALL(metrics_, NotifySuspendWithWakeOnWiFiEnabledDone()).Times(0);
2877   OnAfterResume();
2878 }
2879 
TEST_F(WakeOnWiFiTestWithMockDispatcher,SetWakeOnWiFiFeaturesEnabled)2880 TEST_F(WakeOnWiFiTestWithMockDispatcher, SetWakeOnWiFiFeaturesEnabled) {
2881   const string bad_feature("blahblah");
2882   Error e;
2883   EnableWakeOnWiFiFeaturesPacketDarkConnect();
2884   EXPECT_STREQ(GetWakeOnWiFiFeaturesEnabled().c_str(),
2885                kWakeOnWiFiFeaturesEnabledPacketDarkConnect);
2886   EXPECT_FALSE(SetWakeOnWiFiFeaturesEnabled(
2887       kWakeOnWiFiFeaturesEnabledPacketDarkConnect, &e));
2888   EXPECT_STREQ(GetWakeOnWiFiFeaturesEnabled().c_str(),
2889                kWakeOnWiFiFeaturesEnabledPacketDarkConnect);
2890 
2891   EXPECT_FALSE(SetWakeOnWiFiFeaturesEnabled(bad_feature, &e));
2892   EXPECT_EQ(e.type(), Error::kInvalidArguments);
2893   EXPECT_STREQ(e.message().c_str(), "Invalid Wake on WiFi feature");
2894   EXPECT_STREQ(GetWakeOnWiFiFeaturesEnabled().c_str(),
2895                kWakeOnWiFiFeaturesEnabledPacketDarkConnect);
2896 
2897   EXPECT_TRUE(
2898       SetWakeOnWiFiFeaturesEnabled(kWakeOnWiFiFeaturesEnabledPacket, &e));
2899   EXPECT_STREQ(GetWakeOnWiFiFeaturesEnabled().c_str(),
2900                kWakeOnWiFiFeaturesEnabledPacket);
2901 }
2902 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ReportConnectedToServiceAfterWake_WakeOnDarkConnectEnabledAndConnected)2903 TEST_F(WakeOnWiFiTestWithMockDispatcher,
2904        ReportConnectedToServiceAfterWake_WakeOnDarkConnectEnabledAndConnected) {
2905   const bool is_connected = true;
2906   EnableWakeOnWiFiFeaturesPacketDarkConnect();
2907   EXPECT_CALL(
2908       metrics_,
2909       NotifyConnectedToServiceAfterWake(
2910           Metrics::kWiFiConnetionStatusAfterWakeOnWiFiEnabledWakeConnected));
2911   ReportConnectedToServiceAfterWake(is_connected);
2912 
2913   EnableWakeOnWiFiFeaturesDarkConnect();
2914   EXPECT_CALL(
2915       metrics_,
2916       NotifyConnectedToServiceAfterWake(
2917           Metrics::kWiFiConnetionStatusAfterWakeOnWiFiEnabledWakeConnected));
2918   ReportConnectedToServiceAfterWake(is_connected);
2919 }
2920 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ReportConnectedToServiceAfterWake_WakeOnDarkConnectEnabledAndNotConnected)2921 TEST_F(
2922     WakeOnWiFiTestWithMockDispatcher,
2923     ReportConnectedToServiceAfterWake_WakeOnDarkConnectEnabledAndNotConnected) {
2924   const bool is_connected = false;
2925   EnableWakeOnWiFiFeaturesPacketDarkConnect();
2926   EXPECT_CALL(
2927       metrics_,
2928       NotifyConnectedToServiceAfterWake(
2929           Metrics::kWiFiConnetionStatusAfterWakeOnWiFiEnabledWakeNotConnected));
2930   ReportConnectedToServiceAfterWake(is_connected);
2931 
2932   EnableWakeOnWiFiFeaturesDarkConnect();
2933   EXPECT_CALL(
2934       metrics_,
2935       NotifyConnectedToServiceAfterWake(
2936           Metrics::kWiFiConnetionStatusAfterWakeOnWiFiEnabledWakeNotConnected));
2937   ReportConnectedToServiceAfterWake(is_connected);
2938 }
2939 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ReportConnectedToServiceAfterWake_WakeOnDarkConnectDisabledAndConnected)2940 TEST_F(
2941     WakeOnWiFiTestWithMockDispatcher,
2942     ReportConnectedToServiceAfterWake_WakeOnDarkConnectDisabledAndConnected) {
2943   const bool is_connected = true;
2944   EnableWakeOnWiFiFeaturesPacket();
2945   EXPECT_CALL(
2946       metrics_,
2947       NotifyConnectedToServiceAfterWake(
2948           Metrics::kWiFiConnetionStatusAfterWakeOnWiFiDisabledWakeConnected));
2949   ReportConnectedToServiceAfterWake(is_connected);
2950 
2951   DisableWakeOnWiFiFeatures();
2952   EXPECT_CALL(
2953       metrics_,
2954       NotifyConnectedToServiceAfterWake(
2955           Metrics::kWiFiConnetionStatusAfterWakeOnWiFiDisabledWakeConnected));
2956   ReportConnectedToServiceAfterWake(is_connected);
2957 }
2958 
TEST_F(WakeOnWiFiTestWithMockDispatcher,ReportConnectedToServiceAfterWake_WakeOnDarkConnectDisabledAndNotConnected)2959 TEST_F(
2960     WakeOnWiFiTestWithMockDispatcher,
2961     ReportConnectedToServiceAfterWake_WakeOnDarkConnectDisabledAndNotConnected)
2962 {
2963   const bool is_connected = false;
2964   EnableWakeOnWiFiFeaturesPacket();
2965   EXPECT_CALL(
2966       metrics_,
2967       NotifyConnectedToServiceAfterWake(
2968           Metrics::
2969               kWiFiConnetionStatusAfterWakeOnWiFiDisabledWakeNotConnected));
2970   ReportConnectedToServiceAfterWake(is_connected);
2971 
2972   DisableWakeOnWiFiFeatures();
2973   EXPECT_CALL(
2974       metrics_,
2975       NotifyConnectedToServiceAfterWake(
2976           Metrics::
2977               kWiFiConnetionStatusAfterWakeOnWiFiDisabledWakeNotConnected));
2978   ReportConnectedToServiceAfterWake(is_connected);
2979 }
2980 
TEST_F(WakeOnWiFiTestWithMockDispatcher,OnNoAutoConnectableServicesAfterScan_InDarkResume)2981 TEST_F(WakeOnWiFiTestWithMockDispatcher,
2982        OnNoAutoConnectableServicesAfterScan_InDarkResume) {
2983   vector<ByteString> whitelist;
2984   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
2985   EnableWakeOnWiFiFeaturesDarkConnect();
2986   SetInDarkResume(true);
2987 
2988   // Perform disconnect before suspend actions if we are in dark resume.
2989   GetWakeOnWiFiTriggers()->clear();
2990   StartDHCPLeaseRenewalTimer();
2991   StopWakeToScanTimer();
2992   OnNoAutoConnectableServicesAfterScan(whitelist);
2993   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
2994   EXPECT_FALSE(WakeToScanTimerIsRunning());
2995   EXPECT_EQ(GetWakeOnWiFiTriggers()->size(), 1);
2996   EXPECT_TRUE(GetWakeOnWiFiTriggers()->find(WakeOnWiFi::kWakeTriggerSSID) !=
2997               GetWakeOnWiFiTriggers()->end());
2998 }
2999 
TEST_F(WakeOnWiFiTestWithMockDispatcher,OnNoAutoConnectableServicesAfterScan_NotInDarkResume)3000 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3001        OnNoAutoConnectableServicesAfterScan_NotInDarkResume) {
3002   vector<ByteString> whitelist;
3003   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
3004   EnableWakeOnWiFiFeaturesDarkConnect();
3005   SetInDarkResume(false);
3006 
3007   // If we are not in dark resume, do nothing.
3008   GetWakeOnWiFiTriggers()->clear();
3009   StartDHCPLeaseRenewalTimer();
3010   StopWakeToScanTimer();
3011   OnNoAutoConnectableServicesAfterScan(whitelist);
3012   EXPECT_TRUE(DHCPLeaseRenewalTimerIsRunning());
3013   EXPECT_EQ(GetWakeOnWiFiTriggers()->size(), 0);
3014 }
3015 
TEST_F(WakeOnWiFiTestWithMockDispatcher,OnNoAutoConnectableServicesAfterScan_Retry)3016 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3017        OnNoAutoConnectableServicesAfterScan_Retry) {
3018   vector<ByteString> whitelist;
3019   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
3020   EnableWakeOnWiFiFeaturesDarkConnect();
3021   SetInDarkResume(true);
3022   SetDarkResumeScanRetriesLeft(1);
3023 
3024   // Perform a retry.
3025   EXPECT_EQ(1, GetDarkResumeScanRetriesLeft());
3026   EXPECT_CALL(metrics_, NotifyDarkResumeScanRetry());
3027   EXPECT_CALL(*this, InitiateScanCallback(GetLastSSIDMatchFreqs()));
3028   OnNoAutoConnectableServicesAfterScan(whitelist);
3029   EXPECT_EQ(0, GetDarkResumeScanRetriesLeft());
3030 
3031   // Still no auto-connectable services after retry. No more retries, so perform
3032   // disconnect before suspend actions.
3033   GetWakeOnWiFiTriggers()->clear();
3034   StartDHCPLeaseRenewalTimer();
3035   StopWakeToScanTimer();
3036   EXPECT_CALL(*this, InitiateScanCallback(GetLastSSIDMatchFreqs())).Times(0);
3037   OnNoAutoConnectableServicesAfterScan(whitelist);
3038   EXPECT_FALSE(DHCPLeaseRenewalTimerIsRunning());
3039   EXPECT_FALSE(WakeToScanTimerIsRunning());
3040   EXPECT_EQ(GetWakeOnWiFiTriggers()->size(), 1);
3041   EXPECT_TRUE(GetWakeOnWiFiTriggers()->find(WakeOnWiFi::kWakeTriggerSSID) !=
3042               GetWakeOnWiFiTriggers()->end());
3043 }
3044 
TEST_F(WakeOnWiFiTestWithMockDispatcher,OnWakeupReasonReceived_Unsupported)3045 TEST_F(WakeOnWiFiTestWithMockDispatcher, OnWakeupReasonReceived_Unsupported) {
3046   ScopedMockLog log;
3047   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
3048   ScopeLogger::GetInstance()->set_verbose_level(3);
3049   SetWiphyIndex(kWakeReasonNlMsg_WiphyIndex);
3050 
3051   SetWakeOnPacketConnMessage msg;
3052   NetlinkPacket packet(
3053       kWakeReasonUnsupportedNlMsg, sizeof(kWakeReasonUnsupportedNlMsg));
3054   msg.InitFromPacket(&packet, GetWakeupReportMsgContext());
3055   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
3056   EXPECT_CALL(log,
3057               Log(_, _, HasSubstr("Wakeup reason: Not wake on WiFi related")));
3058   EXPECT_CALL(metrics_, NotifyWakeupReasonReceived());
3059   EXPECT_CALL(*this, RecordDarkResumeWakeReasonCallback(_)).Times(0);
3060   OnWakeupReasonReceived(msg);
3061   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
3062 
3063   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
3064   ScopeLogger::GetInstance()->set_verbose_level(0);
3065 }
3066 
TEST_F(WakeOnWiFiTestWithMockDispatcher,OnWakeupReasonReceived_Disconnect)3067 TEST_F(WakeOnWiFiTestWithMockDispatcher, OnWakeupReasonReceived_Disconnect) {
3068   ScopedMockLog log;
3069   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
3070   ScopeLogger::GetInstance()->set_verbose_level(3);
3071   SetWiphyIndex(kWakeReasonNlMsg_WiphyIndex);
3072 
3073   SetWakeOnPacketConnMessage msg;
3074   NetlinkPacket packet(
3075       kWakeReasonDisconnectNlMsg, sizeof(kWakeReasonDisconnectNlMsg));
3076   msg.InitFromPacket(&packet, GetWakeupReportMsgContext());
3077   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
3078   EXPECT_CALL(log, Log(_, _, HasSubstr("Wakeup reason: Disconnect")));
3079   EXPECT_CALL(metrics_, NotifyWakeupReasonReceived());
3080   EXPECT_CALL(*this, RecordDarkResumeWakeReasonCallback(
3081                          WakeOnWiFi::kWakeReasonStringDisconnect));
3082   OnWakeupReasonReceived(msg);
3083   EXPECT_EQ(WakeOnWiFi::kWakeTriggerDisconnect, GetLastWakeReason());
3084 
3085   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
3086   ScopeLogger::GetInstance()->set_verbose_level(0);
3087 }
3088 
TEST_F(WakeOnWiFiTestWithMockDispatcher,OnWakeupReasonReceived_SSID)3089 TEST_F(WakeOnWiFiTestWithMockDispatcher, OnWakeupReasonReceived_SSID) {
3090   ScopedMockLog log;
3091   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
3092   ScopeLogger::GetInstance()->set_verbose_level(3);
3093   SetWiphyIndex(kWakeReasonNlMsg_WiphyIndex);
3094 
3095   SetWakeOnPacketConnMessage msg;
3096   NetlinkPacket packet(kWakeReasonSSIDNlMsg, sizeof(kWakeReasonSSIDNlMsg));
3097   msg.InitFromPacket(&packet, GetWakeupReportMsgContext());
3098   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
3099   EXPECT_CALL(log, Log(_, _, HasSubstr("Wakeup reason: SSID")));
3100   EXPECT_CALL(metrics_, NotifyWakeupReasonReceived());
3101   EXPECT_CALL(*this, RecordDarkResumeWakeReasonCallback(
3102                          WakeOnWiFi::kWakeReasonStringSSID));
3103   OnWakeupReasonReceived(msg);
3104   EXPECT_EQ(WakeOnWiFi::kWakeTriggerSSID, GetLastWakeReason());
3105   EXPECT_EQ(arraysize(kSSID1FreqMatches), GetLastSSIDMatchFreqs().size());
3106   for (uint32_t freq : kSSID1FreqMatches) {
3107     EXPECT_TRUE(GetLastSSIDMatchFreqs().find(freq) !=
3108                 GetLastSSIDMatchFreqs().end());
3109   }
3110 
3111   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
3112   ScopeLogger::GetInstance()->set_verbose_level(0);
3113 }
3114 
TEST_F(WakeOnWiFiTestWithMockDispatcher,OnWakeupReasonReceived_Pattern)3115 TEST_F(WakeOnWiFiTestWithMockDispatcher, OnWakeupReasonReceived_Pattern) {
3116   ScopedMockLog log;
3117   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
3118   ScopeLogger::GetInstance()->set_verbose_level(3);
3119   SetWiphyIndex(kWakeReasonNlMsg_WiphyIndex);
3120 
3121   SetWakeOnPacketConnMessage msg;
3122   NetlinkPacket packet(
3123       kWakeReasonPatternNlMsg, sizeof(kWakeReasonPatternNlMsg));
3124   msg.InitFromPacket(&packet, GetWakeupReportMsgContext());
3125   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
3126   EXPECT_CALL(log, Log(_, _, HasSubstr("Wakeup reason: Pattern " +
3127                                        kWakeReasonPatternNlMsg_PattIndex)));
3128   EXPECT_CALL(metrics_, NotifyWakeupReasonReceived());
3129   EXPECT_CALL(*this, RecordDarkResumeWakeReasonCallback(
3130                          WakeOnWiFi::kWakeReasonStringPattern));
3131   OnWakeupReasonReceived(msg);
3132   EXPECT_EQ(WakeOnWiFi::kWakeTriggerPattern, GetLastWakeReason());
3133 
3134   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
3135   ScopeLogger::GetInstance()->set_verbose_level(0);
3136 }
3137 
TEST_F(WakeOnWiFiTestWithMockDispatcher,OnWakeupReasonReceived_Error)3138 TEST_F(WakeOnWiFiTestWithMockDispatcher, OnWakeupReasonReceived_Error) {
3139   ScopedMockLog log;
3140   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
3141   ScopeLogger::GetInstance()->set_verbose_level(7);
3142   SetWiphyIndex(kWakeReasonNlMsg_WiphyIndex);
3143 
3144   // kWrongMessageTypeNlMsg has an nlmsg_type of 0x16, which is different from
3145   // the 0x13 (i.e. kNl80211FamilyId) that we expect in these unittests.
3146   GetWakeOnPacketConnMessage msg0;
3147   NetlinkPacket packet0(kWrongMessageTypeNlMsg, sizeof(kWrongMessageTypeNlMsg));
3148   msg0.InitFromPacket(&packet0, GetWakeupReportMsgContext());
3149   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
3150   EXPECT_CALL(log, Log(_, _, HasSubstr("Not a NL80211 Message")));
3151   EXPECT_CALL(metrics_, NotifyWakeupReasonReceived()).Times(0);
3152   EXPECT_CALL(*this, RecordDarkResumeWakeReasonCallback(_)).Times(0);
3153   OnWakeupReasonReceived(msg0);
3154   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
3155 
3156   // This message has command NL80211_CMD_GET_WOWLAN, not a
3157   // NL80211_CMD_SET_WOWLAN.
3158   GetWakeOnPacketConnMessage msg1;
3159   NetlinkPacket packet1(kResponseNoIPAddresses, sizeof(kResponseNoIPAddresses));
3160   msg1.InitFromPacket(&packet1, GetWakeupReportMsgContext());
3161   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
3162   EXPECT_CALL(log,
3163               Log(_, _, HasSubstr("Not a NL80211_CMD_SET_WOWLAN message")));
3164   EXPECT_CALL(metrics_, NotifyWakeupReasonReceived()).Times(0);
3165   EXPECT_CALL(*this, RecordDarkResumeWakeReasonCallback(_)).Times(0);
3166   OnWakeupReasonReceived(msg1);
3167   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
3168 
3169   // Valid message, but wrong wiphy index.
3170   SetWiphyIndex(kWakeReasonNlMsg_WiphyIndex + 1);
3171   SetWakeOnPacketConnMessage msg2;
3172   NetlinkPacket packet(
3173       kWakeReasonDisconnectNlMsg, sizeof(kWakeReasonDisconnectNlMsg));
3174   msg2.InitFromPacket(&packet, GetWakeupReportMsgContext());
3175   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
3176   EXPECT_CALL(
3177       log, Log(_, _, HasSubstr("Wakeup reason not meant for this interface")));
3178   EXPECT_CALL(metrics_, NotifyWakeupReasonReceived()).Times(0);
3179   EXPECT_CALL(*this, RecordDarkResumeWakeReasonCallback(_)).Times(0);
3180   OnWakeupReasonReceived(msg2);
3181   EXPECT_EQ(WakeOnWiFi::kWakeTriggerUnsupported, GetLastWakeReason());
3182 
3183   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
3184   ScopeLogger::GetInstance()->set_verbose_level(0);
3185 }
3186 
3187 #else  // DISABLE_WAKE_ON_WIFI
3188 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_AddWakeOnPacketConnection_ReturnsError)3189 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3190        WakeOnWiFiDisabled_AddWakeOnPacketConnection_ReturnsError) {
3191   DisableWakeOnWiFiFeatures();
3192   Error e;
3193   AddWakeOnPacketConnection("1.1.1.1", &e);
3194   EXPECT_EQ(e.type(), Error::kNotSupported);
3195   EXPECT_STREQ(e.message().c_str(), WakeOnWiFi::kWakeOnWiFiNotSupported);
3196 }
3197 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_RemoveWakeOnPacketConnection_ReturnsError)3198 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3199        WakeOnWiFiDisabled_RemoveWakeOnPacketConnection_ReturnsError) {
3200   DisableWakeOnWiFiFeatures();
3201   Error e;
3202   RemoveWakeOnPacketConnection("1.1.1.1", &e);
3203   EXPECT_EQ(e.type(), Error::kNotSupported);
3204   EXPECT_STREQ(e.message().c_str(), WakeOnWiFi::kWakeOnWiFiNotSupported);
3205 }
3206 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_RemoveAllWakeOnPacketConnections_ReturnsError)3207 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3208        WakeOnWiFiDisabled_RemoveAllWakeOnPacketConnections_ReturnsError) {
3209   DisableWakeOnWiFiFeatures();
3210   Error e;
3211   RemoveAllWakeOnPacketConnections(&e);
3212   EXPECT_EQ(e.type(), Error::kNotSupported);
3213   EXPECT_STREQ(e.message().c_str(), WakeOnWiFi::kWakeOnWiFiNotSupported);
3214 }
3215 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_OnBeforeSuspend_ReportsDoneImmediately)3216 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3217        WakeOnWiFiDisabled_OnBeforeSuspend_ReportsDoneImmediately) {
3218   const bool is_connected = true;
3219   const bool have_dhcp_lease = true;
3220   vector<ByteString> whitelist;
3221   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
3222   EXPECT_CALL(*this, DoneCallback(ErrorTypeIs(Error::kSuccess))).Times(1);
3223   EXPECT_CALL(*this, RenewDHCPLeaseCallback()).Times(0);
3224   OnBeforeSuspend(is_connected, whitelist, have_dhcp_lease,
3225                   kTimeToNextLeaseRenewalShort);
3226 
3227   EXPECT_CALL(*this, DoneCallback(ErrorTypeIs(Error::kSuccess))).Times(1);
3228   EXPECT_CALL(*this, RenewDHCPLeaseCallback()).Times(0);
3229   OnBeforeSuspend(is_connected, whitelist, have_dhcp_lease,
3230                   kTimeToNextLeaseRenewalLong);
3231 }
3232 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_OnDarkResume_ReportsDoneImmediately)3233 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3234        WakeOnWiFiDisabled_OnDarkResume_ReportsDoneImmediately) {
3235   const bool is_connected = true;
3236   vector<ByteString> whitelist;
3237   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
3238   EXPECT_CALL(*this, DoneCallback(ErrorTypeIs(Error::kSuccess))).Times(1);
3239   EXPECT_CALL(mock_dispatcher_, PostDelayedTask(_, _)).Times(0);
3240   EXPECT_CALL(metrics_, NotifyWakeOnWiFiOnDarkResume(_)).Times(0);
3241   OnDarkResume(is_connected, whitelist);
3242 
3243   EXPECT_CALL(*this, DoneCallback(ErrorTypeIs(Error::kSuccess))).Times(1);
3244   EXPECT_CALL(mock_dispatcher_, PostDelayedTask(_, _)).Times(0);
3245   EXPECT_CALL(metrics_, NotifyWakeOnWiFiOnDarkResume(_)).Times(0);
3246   OnDarkResume(is_connected, whitelist);
3247 }
3248 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_OnAfterResume_DoesNothing)3249 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3250        WakeOnWiFiDisabled_OnAfterResume_DoesNothing) {
3251   DisableWakeOnWiFiFeatures();
3252   EXPECT_CALL(netlink_manager_, SendNl80211Message(_, _, _, _)).Times(0);
3253   EXPECT_CALL(metrics_, NotifySuspendWithWakeOnWiFiEnabledDone()).Times(0);
3254   OnAfterResume();
3255 }
3256 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_SetWakeOnWiFiFeaturesEnabled)3257 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3258        WakeOnWiFiDisabled_SetWakeOnWiFiFeaturesEnabled) {
3259   Error e;
3260   SetWakeOnWiFiFeaturesNotSupported();
3261   EXPECT_STREQ(GetWakeOnWiFiFeaturesEnabled().c_str(),
3262                kWakeOnWiFiFeaturesEnabledNotSupported);
3263   EXPECT_FALSE(
3264       SetWakeOnWiFiFeaturesEnabled(kWakeOnWiFiFeaturesEnabledNotSupported, &e));
3265   EXPECT_STREQ(GetWakeOnWiFiFeaturesEnabled().c_str(),
3266                kWakeOnWiFiFeaturesEnabledNotSupported);
3267   EXPECT_EQ(e.type(), Error::kNotSupported);
3268   EXPECT_STREQ(e.message().c_str(), WakeOnWiFi::kWakeOnWiFiNotSupported);
3269 
3270   EXPECT_FALSE(
3271       SetWakeOnWiFiFeaturesEnabled(kWakeOnWiFiFeaturesEnabledPacket, &e));
3272   EXPECT_STREQ(GetWakeOnWiFiFeaturesEnabled().c_str(),
3273                kWakeOnWiFiFeaturesEnabledNotSupported);
3274   EXPECT_EQ(e.type(), Error::kNotSupported);
3275   EXPECT_STREQ(e.message().c_str(), WakeOnWiFi::kWakeOnWiFiNotSupported);
3276 }
3277 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_OnConnectedAndReachable)3278 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3279        WakeOnWiFiDisabled_OnConnectedAndReachable) {
3280   ScopedMockLog log;
3281   const bool start_lease_renewal_timer = true;
3282   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
3283   ScopeLogger::GetInstance()->set_verbose_level(3);
3284 
3285   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
3286   SetInDarkResume(true);
3287   EXPECT_CALL(
3288       log, Log(_, _, HasSubstr("Wake on WiFi not supported, so do nothing")));
3289   OnConnectedAndReachable(start_lease_renewal_timer,
3290                           kTimeToNextLeaseRenewalLong);
3291 
3292   SetInDarkResume(false);
3293   EXPECT_CALL(log, Log(_, _, HasSubstr("Not in dark resume, so do nothing")));
3294   OnConnectedAndReachable(start_lease_renewal_timer,
3295                           kTimeToNextLeaseRenewalLong);
3296   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
3297   ScopeLogger::GetInstance()->set_verbose_level(0);
3298 }
3299 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_ReportConnectedToServiceAfterWakeAndConnected)3300 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3301        WakeOnWiFiDisabled_ReportConnectedToServiceAfterWakeAndConnected) {
3302   const bool is_connected = true;
3303   EXPECT_CALL(
3304       metrics_,
3305       NotifyConnectedToServiceAfterWake(
3306           Metrics::kWiFiConnetionStatusAfterWakeOnWiFiDisabledWakeConnected));
3307   ReportConnectedToServiceAfterWake(is_connected);
3308 }
3309 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_ReportConnectedToServiceAfterWakeAndNotConnected)3310 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3311        WakeOnWiFiDisabled_ReportConnectedToServiceAfterWakeAndNotConnected) {
3312   const bool is_connected = false;
3313   EXPECT_CALL(
3314       metrics_,
3315       NotifyConnectedToServiceAfterWake(
3316           Metrics::
3317               kWiFiConnetionStatusAfterWakeOnWiFiDisabledWakeNotConnected));
3318   ReportConnectedToServiceAfterWake(is_connected);
3319 }
3320 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_OnNoAutoConnectableServicesAfterScan)3321 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3322        WakeOnWiFiDisabled_OnNoAutoConnectableServicesAfterScan) {
3323   vector<ByteString> whitelist;
3324   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
3325 
3326   // Do nothing (i.e. do not invoke WakeOnWiFi::BeforeSuspendActions) if wake
3327   // on WiFi is not supported, whether or not we are in dark resume.
3328   SetInDarkResume(true);
3329   GetWakeOnWiFiTriggers()->clear();
3330   StartDHCPLeaseRenewalTimer();
3331   StopWakeToScanTimer();
3332   OnNoAutoConnectableServicesAfterScan(whitelist);
3333   EXPECT_FALSE(WakeToScanTimerIsRunning());
3334   EXPECT_TRUE(DHCPLeaseRenewalTimerIsRunning());
3335   EXPECT_EQ(GetWakeOnWiFiTriggers()->size(), 0);
3336 
3337   SetInDarkResume(false);
3338   GetWakeOnWiFiTriggers()->clear();
3339   StartDHCPLeaseRenewalTimer();
3340   StopWakeToScanTimer();
3341   OnNoAutoConnectableServicesAfterScan(whitelist);
3342   EXPECT_FALSE(WakeToScanTimerIsRunning());
3343   EXPECT_TRUE(DHCPLeaseRenewalTimerIsRunning());
3344   EXPECT_EQ(GetWakeOnWiFiTriggers()->size(), 0);
3345 }
3346 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_OnWakeupReasonReceived_DoesNothing)3347 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3348        WakeOnWiFiDisabled_OnWakeupReasonReceived_DoesNothing) {
3349   ScopedMockLog log;
3350   ScopeLogger::GetInstance()->EnableScopesByName("wifi");
3351   ScopeLogger::GetInstance()->set_verbose_level(7);
3352 
3353   SetWakeOnPacketConnMessage msg;
3354   NetlinkPacket packet(kWakeReasonSSIDNlMsg, sizeof(kWakeReasonSSIDNlMsg));
3355   msg.InitFromPacket(&packet, GetWakeupReportMsgContext());
3356   EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
3357   EXPECT_CALL(
3358       log, Log(_, _, HasSubstr("Wake on WiFi not supported, so do nothing")));
3359   EXPECT_CALL(metrics_, NotifyWakeupReasonReceived()).Times(0);
3360   EXPECT_CALL(*this, RecordDarkResumeWakeReasonCallback(_)).Times(0);
3361   OnWakeupReasonReceived(msg);
3362 
3363   ScopeLogger::GetInstance()->EnableScopesByName("-wifi");
3364   ScopeLogger::GetInstance()->set_verbose_level(0);
3365 }
3366 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_ConfigureSetWakeOnWiFiSettingsMessage_ReturnsFalse)3367 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3368        WakeOnWiFiDisabled_ConfigureSetWakeOnWiFiSettingsMessage_ReturnsFalse) {
3369   IPAddressStore no_addresses;
3370   IPAddressStore one_address;
3371   const char kIPv4AddrString[] = "1.1.1.1";
3372   one_address.AddUnique(
3373       IPAddress(string(kIPv4AddrString, sizeof(kIPv4AddrString))));
3374   set<WakeOnWiFi::WakeOnWiFiTrigger> no_trigs;
3375   set<WakeOnWiFi::WakeOnWiFiTrigger> one_trig;
3376   one_trig.insert(WakeOnWiFi::kWakeTriggerPattern);
3377   const int index = 1;  // wiphy device number
3378   vector<ByteString> whitelist;
3379   const uint32_t interval = kNetDetectScanIntervalSeconds;
3380   SetWakeOnPacketConnMessage msg;
3381   Error e;
3382   EXPECT_FALSE(ConfigureSetWakeOnWiFiSettingsMessage(
3383       &msg, no_trigs, no_addresses, index, interval, whitelist, &e));
3384   EXPECT_FALSE(ConfigureSetWakeOnWiFiSettingsMessage(
3385       &msg, one_trig, one_address, index, interval, whitelist, &e));
3386 }
3387 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_WakeOnWiFiSettingsMatch_ReturnsFalse)3388 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3389        WakeOnWiFiDisabled_WakeOnWiFiSettingsMatch_ReturnsFalse) {
3390   // Test that WakeOnWiFi::WakeOnWiFiSettingsMatch unconditionally returns false
3391   // when wake-on-WiFi is disabled by testing it against several cases where we
3392   // expect it to return true.
3393   IPAddressStore all_addresses;
3394   set<WakeOnWiFi::WakeOnWiFiTrigger> trigs;
3395   vector<ByteString> whitelist;
3396   const uint32_t interval = kNetDetectScanIntervalSeconds;
3397 
3398   GetWakeOnPacketConnMessage msg0;
3399   NetlinkPacket packet0(kResponseNoIPAddresses, sizeof(kResponseNoIPAddresses));
3400   msg0.InitFromPacket(&packet0, NetlinkMessage::MessageContext());
3401   EXPECT_FALSE(
3402       WakeOnWiFiSettingsMatch(msg0, trigs, all_addresses, interval, whitelist));
3403 
3404   trigs.insert(WakeOnWiFi::kWakeTriggerPattern);
3405   all_addresses.AddUnique(
3406       IPAddress(string(kIPV4Address0, sizeof(kIPV4Address0))));
3407   GetWakeOnPacketConnMessage msg1;
3408   NetlinkPacket packet1(kResponseIPV40, sizeof(kResponseIPV40));
3409   msg1.InitFromPacket(&packet1, NetlinkMessage::MessageContext());
3410   EXPECT_FALSE(
3411       WakeOnWiFiSettingsMatch(msg1, trigs, all_addresses, interval, whitelist));
3412 
3413   trigs.insert(WakeOnWiFi::kWakeTriggerDisconnect);
3414   GetWakeOnPacketConnMessage msg2;
3415   NetlinkPacket packet2(kResponseIPV40WakeOnDisconnect,
3416                         sizeof(kResponseIPV40WakeOnDisconnect));
3417   msg2.InitFromPacket(&packet2, NetlinkMessage::MessageContext());
3418   EXPECT_FALSE(
3419       WakeOnWiFiSettingsMatch(msg2, trigs, all_addresses, interval, whitelist));
3420 
3421   trigs.erase(WakeOnWiFi::kWakeTriggerDisconnect);
3422   all_addresses.AddUnique(
3423       IPAddress(string(kIPV4Address1, sizeof(kIPV4Address1))));
3424   GetWakeOnPacketConnMessage msg3;
3425   NetlinkPacket packet3(kResponseIPV401, sizeof(kResponseIPV401));
3426   msg3.InitFromPacket(&packet3, NetlinkMessage::MessageContext());
3427   EXPECT_FALSE(
3428       WakeOnWiFiSettingsMatch(msg3, trigs, all_addresses, interval, whitelist));
3429 
3430   all_addresses.AddUnique(
3431       IPAddress(string(kIPV6Address0, sizeof(kIPV6Address0))));
3432   GetWakeOnPacketConnMessage msg4;
3433   NetlinkPacket packet4(kResponseIPV401IPV60, sizeof(kResponseIPV401IPV60));
3434   msg4.InitFromPacket(&packet4, NetlinkMessage::MessageContext());
3435   EXPECT_FALSE(
3436       WakeOnWiFiSettingsMatch(msg4, trigs, all_addresses, interval, whitelist));
3437 
3438   all_addresses.AddUnique(
3439       IPAddress(string(kIPV6Address1, sizeof(kIPV6Address1))));
3440   GetWakeOnPacketConnMessage msg5;
3441   NetlinkPacket packet5(kResponseIPV401IPV601, sizeof(kResponseIPV401IPV601));
3442   msg5.InitFromPacket(&packet5, NetlinkMessage::MessageContext());
3443   EXPECT_FALSE(
3444       WakeOnWiFiSettingsMatch(msg5, trigs, all_addresses, interval, whitelist));
3445 
3446   all_addresses.Clear();
3447   trigs.clear();
3448   trigs.insert(WakeOnWiFi::kWakeTriggerSSID);
3449   AddSSIDToWhitelist(kSSIDBytes1, sizeof(kSSIDBytes1), &whitelist);
3450   AddSSIDToWhitelist(kSSIDBytes2, sizeof(kSSIDBytes2), &whitelist);
3451   GetWakeOnPacketConnMessage msg6;
3452   NetlinkPacket packet6(kResponseWakeOnSSID, sizeof(kResponseWakeOnSSID));
3453   msg6.InitFromPacket(&packet6, NetlinkMessage::MessageContext());
3454   EXPECT_FALSE(
3455       WakeOnWiFiSettingsMatch(msg6, trigs, all_addresses, interval, whitelist));
3456 }
3457 
TEST_F(WakeOnWiFiTestWithMockDispatcher,WakeOnWiFiDisabled_ParseWakeOnWiFiCapabilities_DoesNothing)3458 TEST_F(WakeOnWiFiTestWithMockDispatcher,
3459        WakeOnWiFiDisabled_ParseWakeOnWiFiCapabilities_DoesNothing) {
3460   // |kNewWiphyNlMsg| should indicate that the NIC supports wake on pattern
3461   // (on up to |kNewWiphyNlMsg_MaxPatterns| registered patterns),
3462   // supports wake on SSID (on up to |kNewWiphyNlMsg_MaxSSIDs| SSIDs), and
3463   // supports wake on disconnect. Test that
3464   // WakeOnWiFi::ParseWakeOnWiFiCapabilities does nothing and does not parse
3465   // these capabilities when wake-on-WiFi is disabled.
3466   ClearWakeOnWiFiTriggersSupported();
3467   SetWakeOnWiFiMaxSSIDs(0);
3468   NewWiphyMessage msg;
3469   NetlinkPacket packet(kNewWiphyNlMsg, sizeof(kNewWiphyNlMsg));
3470   msg.InitFromPacket(&packet, NetlinkMessage::MessageContext());
3471   ParseWakeOnWiFiCapabilities(msg);
3472   EXPECT_TRUE(GetWakeOnWiFiTriggersSupported()->empty());
3473   EXPECT_EQ(0, GetWakeOnWiFiMaxPatterns());
3474   EXPECT_EQ(0, GetWakeOnWiFiMaxSSIDs());
3475 }
3476 
3477 #endif  // DISABLE_WAKE_ON_WIFI
3478 
3479 }  // namespace shill
3480