-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccelerometer_test.cc
451 lines (367 loc) · 16 KB
/
accelerometer_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <base/files/file_util.h>
#include <gtest/gtest.h>
#include <libmems/common_types.h>
#include <libmems/iio_context.h>
#include <libmems/iio_device.h>
#include <libmems/iio_device_impl.h>
#include <libmems/test_fakes.h>
#include "mems_setup/configuration.h"
#include "mems_setup/delegate.h"
#include "mems_setup/sensor_location.h"
#include "mems_setup/test_fakes.h"
#include "mems_setup/test_helper.h"
using libmems::fakes::FakeIioChannel;
using libmems::fakes::FakeIioDevice;
using mems_setup::testing::SensorTestBase;
namespace mems_setup {
namespace {
static gid_t kChronosGroupId = 666;
static gid_t kIioserviceGroupId = 777;
static gid_t kPowerGroupId = 999;
constexpr int kDeviceId = 1;
constexpr char kTriggerString[] = "trigger";
constexpr char kHwfifoTimeoutString[] = "buffer/hwfifo_timeout";
constexpr char kFlushString[] = "flush";
#if USE_IIOSERVICE
constexpr char kDevString[] = "/dev/";
#endif // USE_IIOSERVICE
class AccelerometerTest : public SensorTestBase {
public:
AccelerometerTest()
: SensorTestBase("cros-ec-accel", kDeviceId, SensorKind::ACCELEROMETER) {
mock_delegate_->AddGroup("chronos", kChronosGroupId);
mock_delegate_->AddGroup(Configuration::GetGroupNameForSysfs(),
kIioserviceGroupId);
std::string dev_name = libmems::IioDeviceImpl::GetStringFromId(kDeviceId);
// /sys/bus/iio/devices/iio:device1
base::FilePath sys_dev_path = mock_device_->GetPath();
// Create the file to set the trigger in |AddSysfsTrigger|.
mock_delegate_->CreateFile(sys_dev_path.Append(kTriggerString));
// Create the files to set permissions and ownership for test.
mock_delegate_->CreateFile(sys_dev_path.Append(kHwfifoTimeoutString));
mock_delegate_->CreateFile(sys_dev_path.Append(kFlushString));
}
void CheckPermissionsAndOwnershipForFile(const base::FilePath& path,
int permission) {
uid_t user;
gid_t group;
EXPECT_TRUE(mock_delegate_->GetOwnership(path, &user, &group));
EXPECT_EQ(group, kIioserviceGroupId);
EXPECT_EQ(permission, mock_delegate_->GetPermissions(path));
}
};
TEST_F(AccelerometerTest, CheckPermissionsAndOwnership) {
SetSingleSensor(kBaseSensorLocation);
ConfigureVpd({{"in_accel_x_base_calibbias", "100"}});
EXPECT_TRUE(GetConfiguration()->Configure());
std::string dev_name = libmems::IioDeviceImpl::GetStringFromId(kDeviceId);
// /sys/bus/iio/devices/iio:device1
base::FilePath sys_dev_path = mock_device_->GetPath();
CheckPermissionsAndOwnershipForFile(sys_dev_path.Append(kHwfifoTimeoutString),
base::FILE_PERMISSION_WRITE_BY_GROUP |
base::FILE_PERMISSION_READ_BY_GROUP);
CheckPermissionsAndOwnershipForFile(sys_dev_path.Append(kFlushString),
base::FILE_PERMISSION_WRITE_BY_GROUP);
#if USE_IIOSERVICE
// /dev/iio:deviceX
base::FilePath dev_path = base::FilePath(kDevString).Append(dev_name.c_str());
CheckPermissionsAndOwnershipForFile(dev_path,
base::FILE_PERMISSION_WRITE_BY_GROUP |
base::FILE_PERMISSION_READ_BY_GROUP);
#endif // USE_IIOSERVICE
}
#if USE_IIOSERVICE
TEST_F(AccelerometerTest, FrequencyReset) {
SetSingleSensor(kBaseSensorLocation);
ConfigureVpd({{"in_accel_x_base_calibbias", "100"}});
EXPECT_TRUE(GetConfiguration()->Configure());
auto frequency_opt =
mock_device_->ReadDoubleAttribute(libmems::kSamplingFrequencyAttr);
EXPECT_TRUE(frequency_opt.has_value());
EXPECT_EQ(frequency_opt.value(), 0.0);
}
#endif // USE_IIOSERVICE
TEST_F(AccelerometerTest, CheckClock) {
SetSingleSensor(kBaseSensorLocation);
ConfigureVpd({{"in_accel_x_base_calibbias", "100"}});
EXPECT_TRUE(GetConfiguration()->Configure());
auto attr_opt = mock_device_->ReadStringAttribute("current_timestamp_clock");
EXPECT_TRUE(attr_opt.has_value());
EXPECT_EQ(attr_opt.value(), "boottime");
}
TEST_F(AccelerometerTest, MissingVpd) {
SetSingleSensor(kBaseSensorLocation);
ConfigureVpd({{"in_accel_x_base_calibbias", "100"}});
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_TRUE(mock_device_->GetChannel("accel_x")
->ReadNumberAttribute("calibbias")
.has_value());
EXPECT_EQ(100, mock_device_->GetChannel("accel_x")
->ReadNumberAttribute("calibbias")
.value());
EXPECT_FALSE(mock_device_->GetChannel("accel_y")
->ReadNumberAttribute("calibbias")
.has_value());
EXPECT_FALSE(mock_device_->GetChannel("accel_z")
->ReadNumberAttribute("calibbias")
.has_value());
}
TEST_F(AccelerometerTest, NotNumericVpd) {
SetSingleSensor(kBaseSensorLocation);
ConfigureVpd({{"in_accel_x_base_calibbias", "blah"},
{"in_accel_y_base_calibbias", "100"}});
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_FALSE(mock_device_->GetChannel("accel_x")
->ReadNumberAttribute("calibbias")
.has_value());
EXPECT_TRUE(mock_device_->GetChannel("accel_y")
->ReadNumberAttribute("calibbias")
.has_value());
EXPECT_EQ(100, mock_device_->GetChannel("accel_y")
->ReadNumberAttribute("calibbias")
.value());
EXPECT_FALSE(mock_device_->GetChannel("accel_z")
->ReadNumberAttribute("calibbias")
.has_value());
}
TEST_F(AccelerometerTest, VpdOutOfRange) {
SetSingleSensor(kBaseSensorLocation);
ConfigureVpd({{"in_accel_x_base_calibbias", "104"}, // just above .100g.
{"in_accel_y_base_calibbias", "100"},
{"in_accel_z_base_calibbias", "85"}});
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_FALSE(mock_device_->GetChannel("accel_x")
->ReadNumberAttribute("calibbias")
.has_value());
EXPECT_FALSE(mock_device_->GetChannel("accel_y")
->ReadNumberAttribute("calibbias")
.has_value());
EXPECT_FALSE(mock_device_->GetChannel("accel_z")
->ReadNumberAttribute("calibbias")
.has_value());
}
TEST_F(AccelerometerTest, CalibscaleData) {
SetSingleSensor(kBaseSensorLocation);
ConfigureVpd({{"in_accel_x_base_calibscale", "5"},
{"in_accel_y_base_calibscale", "6"},
{"in_accel_z_base_calibscale", "7"}});
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_TRUE(mock_device_->GetChannel("accel_x")
->ReadNumberAttribute("calibscale")
.has_value());
EXPECT_TRUE(mock_device_->GetChannel("accel_y")
->ReadNumberAttribute("calibscale")
.has_value());
EXPECT_TRUE(mock_device_->GetChannel("accel_z")
->ReadNumberAttribute("calibscale")
.has_value());
EXPECT_EQ(5, mock_device_->GetChannel("accel_x")
->ReadNumberAttribute("calibscale")
.value());
EXPECT_EQ(6, mock_device_->GetChannel("accel_y")
->ReadNumberAttribute("calibscale")
.value());
EXPECT_EQ(7, mock_device_->GetChannel("accel_z")
->ReadNumberAttribute("calibscale")
.value());
}
TEST_F(AccelerometerTest, CalibscaleZeroData) {
SetSingleSensor(kBaseSensorLocation);
ConfigureVpd({{"in_accel_x_base_calibscale", "5"},
{"in_accel_y_base_calibscale", "6"},
{"in_accel_z_base_calibscale", "0"}});
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_TRUE(mock_device_->GetChannel("accel_x")
->ReadNumberAttribute("calibscale")
.has_value());
EXPECT_TRUE(mock_device_->GetChannel("accel_y")
->ReadNumberAttribute("calibscale")
.has_value());
EXPECT_TRUE(mock_device_->GetChannel("accel_z")
->ReadNumberAttribute("calibscale")
.has_value());
EXPECT_EQ(5, mock_device_->GetChannel("accel_x")
->ReadNumberAttribute("calibscale")
.value());
EXPECT_EQ(6, mock_device_->GetChannel("accel_y")
->ReadNumberAttribute("calibscale")
.value());
EXPECT_EQ(0, mock_device_->GetChannel("accel_z")
->ReadNumberAttribute("calibscale")
.value());
}
TEST_F(AccelerometerTest, NotLoadingTriggerModule) {
SetSingleSensor(kBaseSensorLocation);
ConfigureVpd({{"in_accel_x_base_calibbias", "50"},
{"in_accel_y_base_calibbias", "100"},
{"in_accel_z_base_calibbias", "85"}});
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_EQ(0, mock_delegate_->GetNumModulesProbed());
}
TEST_F(AccelerometerTest, MultipleSensorDevice) {
SetSharedSensor();
ConfigureVpd({{"in_accel_x_base_calibbias", "50"},
{"in_accel_y_base_calibbias", "100"},
{"in_accel_z_base_calibbias", "85"},
{"in_accel_y_lid_calibbias", "27"}});
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_TRUE(mock_device_->GetChannel("accel_x_base")
->ReadNumberAttribute("calibbias")
.has_value());
EXPECT_TRUE(mock_device_->GetChannel("accel_y_base")
->ReadNumberAttribute("calibbias")
.has_value());
EXPECT_TRUE(mock_device_->GetChannel("accel_z_base")
->ReadNumberAttribute("calibbias")
.has_value());
EXPECT_EQ(50, mock_device_->GetChannel("accel_x_base")
->ReadNumberAttribute("calibbias")
.value());
EXPECT_EQ(100, mock_device_->GetChannel("accel_y_base")
->ReadNumberAttribute("calibbias")
.value());
EXPECT_EQ(85, mock_device_->GetChannel("accel_z_base")
->ReadNumberAttribute("calibbias")
.value());
EXPECT_FALSE(mock_device_->GetChannel("accel_x_lid")
->ReadNumberAttribute("calibbias")
.has_value());
EXPECT_TRUE(mock_device_->GetChannel("accel_y_lid")
->ReadNumberAttribute("calibbias")
.has_value());
EXPECT_EQ(27, mock_device_->GetChannel("accel_y_lid")
->ReadNumberAttribute("calibbias")
.value());
EXPECT_FALSE(mock_device_->GetChannel("accel_z_lid")
->ReadNumberAttribute("calibbias")
.has_value());
}
TEST_F(AccelerometerTest, TriggerPermissions) {
SetSingleSensor(kLidSensorLocation);
EXPECT_TRUE(GetConfiguration()->Configure());
base::FilePath trigger_now = mock_trigger1_->GetPath().Append("trigger_now");
EXPECT_NE(0, mock_delegate_->GetPermissions(trigger_now) &
base::FILE_PERMISSION_WRITE_BY_GROUP);
gid_t gid = 0;
mock_delegate_->GetOwnership(trigger_now, nullptr, &gid);
EXPECT_EQ(kChronosGroupId, gid);
}
#if !USE_IIOSERVICE
TEST_F(AccelerometerTest, SingleSensorEnableChannels) {
SetSingleSensor(kLidSensorLocation);
EXPECT_TRUE(GetConfiguration()->Configure());
for (auto channel : mock_device_->GetAllChannels()) {
if (strcmp(channel->GetId(), "calibration") == 0)
continue;
EXPECT_EQ(static_cast<FakeIioChannel*>(channel)->IsScanElementsEnabled(),
0 != strcmp(channel->GetId(), "timestamp"));
}
}
TEST_F(AccelerometerTest, MultipleSensorEnableChannels) {
SetSharedSensor();
EXPECT_TRUE(GetConfiguration()->Configure());
for (auto channel : mock_device_->GetAllChannels()) {
if (strcmp(channel->GetId(), "calibration") == 0)
continue;
EXPECT_EQ(static_cast<FakeIioChannel*>(channel)->IsScanElementsEnabled(),
0 != strcmp(channel->GetId(), "timestamp"));
}
}
TEST_F(AccelerometerTest, BufferEnabled) {
SetSingleSensor(kLidSensorLocation);
EXPECT_FALSE(mock_device_->IsBufferEnabled());
EXPECT_TRUE(GetConfiguration()->Configure());
size_t accel_buffer_len = 0;
EXPECT_TRUE(mock_device_->IsBufferEnabled(&accel_buffer_len));
EXPECT_EQ(1, accel_buffer_len);
}
#endif
TEST_F(AccelerometerTest, SingleSensorKbWakeAnglePermissions) {
base::FilePath kb_path("/sys/class/chromeos/cros_ec/kb_wake_angle");
SetSingleSensor(kLidSensorLocation);
mock_delegate_->CreateFile(kb_path);
mock_delegate_->AddGroup("power", kPowerGroupId);
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_NE(0, mock_delegate_->GetPermissions(kb_path) &
base::FILE_PERMISSION_WRITE_BY_GROUP);
gid_t gid = 0;
mock_delegate_->GetOwnership(kb_path, nullptr, &gid);
EXPECT_EQ(kPowerGroupId, gid);
}
TEST_F(AccelerometerTest, SharedSensorKbWakeAnglePermissions) {
base::FilePath kb_path = mock_device_->GetPath().Append("in_angl_offset");
SetSharedSensor();
mock_delegate_->CreateFile(kb_path);
mock_delegate_->AddGroup("power", kPowerGroupId);
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_NE(0, mock_delegate_->GetPermissions(kb_path) &
base::FILE_PERMISSION_WRITE_BY_GROUP);
gid_t gid = 0;
mock_delegate_->GetOwnership(kb_path, nullptr, &gid);
EXPECT_EQ(kPowerGroupId, gid);
}
TEST_F(AccelerometerTest, OkWithSysfstrigDefined) {
SetSingleSensor(kLidSensorLocation);
mock_sysfs_trigger_->AddMockTrigger();
EXPECT_TRUE(GetConfiguration()->Configure());
}
TEST_F(AccelerometerTest, SetRangeNoGyroLid) {
SetSingleSensor(kLidSensorLocation);
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_EQ(4, mock_device_->ReadNumberAttribute("scale").value());
}
TEST_F(AccelerometerTest, SetRangeNoGyroLidOld) {
SetSharedSensor();
SetSingleSensor(kBaseSensorLocation);
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_NE(4, mock_device_->ReadNumberAttribute("scale").value_or(0));
}
TEST_F(AccelerometerTest, SetRangeGyroBaseBase) {
auto mock_gyro =
std::make_unique<FakeIioDevice>(mock_context_.get(), "cros-ec-gyro", 2);
mock_gyro->WriteStringAttribute("location", kBaseSensorLocation);
mock_context_->AddDevice(std::move(mock_gyro));
SetSingleSensor(kBaseSensorLocation);
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_EQ(4, mock_device_->ReadNumberAttribute("scale").value());
}
TEST_F(AccelerometerTest, SetRangeGyroBaseLid) {
auto mock_gyro =
std::make_unique<FakeIioDevice>(mock_context_.get(), "cros-ec-gyro", 2);
mock_gyro->WriteStringAttribute("location", kBaseSensorLocation);
mock_context_->AddDevice(std::move(mock_gyro));
SetSingleSensor(kLidSensorLocation);
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_EQ(2, mock_device_->ReadNumberAttribute("scale").value());
}
TEST_F(AccelerometerTest, SetRangeMultipleGyroLid) {
auto mock_gyro1 =
std::make_unique<FakeIioDevice>(mock_context_.get(), "cros-ec-gyro", 2);
mock_gyro1->WriteStringAttribute("location", kBaseSensorLocation);
mock_context_->AddDevice(std::move(mock_gyro1));
auto mock_gyro2 =
std::make_unique<FakeIioDevice>(mock_context_.get(), "cros-ec-gyro", 3);
mock_gyro2->WriteStringAttribute("location", kLidSensorLocation);
mock_context_->AddDevice(std::move(mock_gyro2));
SetSingleSensor(kLidSensorLocation);
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_EQ(4, mock_device_->ReadNumberAttribute("scale").value());
}
TEST_F(AccelerometerTest, SetRangeMultipleGyroBase) {
auto mock_gyro1 =
std::make_unique<FakeIioDevice>(mock_context_.get(), "cros-ec-gyro", 2);
mock_gyro1->WriteStringAttribute("location", kBaseSensorLocation);
mock_context_->AddDevice(std::move(mock_gyro1));
auto mock_gyro2 =
std::make_unique<FakeIioDevice>(mock_context_.get(), "cros-ec-gyro", 3);
mock_gyro2->WriteStringAttribute("location", kLidSensorLocation);
mock_context_->AddDevice(std::move(mock_gyro2));
SetSingleSensor(kBaseSensorLocation);
EXPECT_TRUE(GetConfiguration()->Configure());
EXPECT_EQ(2, mock_device_->ReadNumberAttribute("scale").value());
}
} // namespace
} // namespace mems_setup