-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperipheral_test.cc
59 lines (47 loc) · 1.82 KB
/
peripheral_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
// Copyright 2021 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "typecd/peripheral.h"
#include <string>
#include <base/files/scoped_temp_dir.h>
#include <base/strings/stringprintf.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "typecd/test_constants.h"
#include "typecd/test_utils.h"
namespace typecd {
class PeripheralTest : public ::testing::Test {
protected:
void SetUp() override {
ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
temp_dir_ = scoped_temp_dir_.GetPath();
}
public:
base::FilePath temp_dir_;
base::ScopedTempDir scoped_temp_dir_;
};
TEST_F(PeripheralTest, CheckPDRevision) {
// Set up fake sysfs paths.
auto path = temp_dir_.Append(std::string("port0-fake_periheral"));
ASSERT_TRUE(base::CreateDirectory(path));
Peripheral p(path);
EXPECT_EQ(p.GetPDRevision(), PDRevision::kNone);
// Gibberish PD revision values should be rejected.
auto pd_rev = base::StringPrintf("a.f");
ASSERT_TRUE(base::WriteFile(path.Append("usb_power_delivery_revision"),
pd_rev.c_str(), pd_rev.length()));
p.UpdatePDRevision();
EXPECT_EQ(p.GetPDRevision(), PDRevision::kNone);
pd_rev = base::StringPrintf("!*(&#@$>SC(&(*)(#@>C>");
ASSERT_TRUE(base::WriteFile(path.Append("usb_power_delivery_revision"),
pd_rev.c_str(), pd_rev.length()));
p.UpdatePDRevision();
EXPECT_EQ(p.GetPDRevision(), PDRevision::kNone);
// Legitimate PD revision values should be parsed correctly.
pd_rev = base::StringPrintf("3.0");
ASSERT_TRUE(base::WriteFile(path.Append("usb_power_delivery_revision"),
pd_rev.c_str(), pd_rev.length()));
p.UpdatePDRevision();
EXPECT_EQ(p.GetPDRevision(), PDRevision::k30);
}
} // namespace typecd