blob: 92d7e39d6ed8e4a62df4ba000ea9bc18383dfd97 [file] [log] [blame]
Pavel Maltsev7cb0bf32016-12-02 16:52:39 -08001/*
2 * Copyright (C) 2016 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 <gtest/gtest.h>
18#include <memory>
19#include <fstream>
20#include <unordered_set>
21
22#include "vehicle_hal_manager/AccessControlConfigParser.h"
23
24namespace android {
25namespace hardware {
26namespace vehicle {
27namespace V2_0 {
28
29namespace {
30
31class AccessControlConfigParserTest : public ::testing::Test {
32protected:
33 void SetUp() override {
34 std::vector<VehicleProperty> supportedProperties {
35 VehicleProperty::HVAC_FAN_SPEED,
36 VehicleProperty::HVAC_FAN_DIRECTION,
37 };
38 parser.reset(new AccessControlConfigParser(supportedProperties));
39 }
40public:
41 PropertyAclMap aclMap;
42 std::unique_ptr<AccessControlConfigParser> parser;
43};
44
45TEST_F(AccessControlConfigParserTest, basicParsing) {
46 std::stringstream file;
47 file << "S:0x0500 1000 RW" << std::endl;
48
49 ASSERT_TRUE(parser->parseFromStream(&file, &aclMap));
50
51 ASSERT_EQ(1, aclMap.size());
52 auto it = aclMap.find(VehicleProperty::HVAC_FAN_SPEED);
53 ASSERT_NE(aclMap.end(), it);
54 ASSERT_EQ(VehiclePropertyAccess::READ_WRITE, it->second.access);
55 ASSERT_EQ(VehicleProperty::HVAC_FAN_SPEED, it->second.propId);
56 ASSERT_EQ(1000u, it->second.uid);
57}
58
59TEST_F(AccessControlConfigParserTest, multipleUids) {
60 std::stringstream file;
61 file << "Set AID_AUDIO 1004" << std::endl
62 << "Set AID_SYSTEM 1000" << std::endl
63 << "S:0x0500 AID_SYSTEM RW" << std::endl
64 << "S:0x0500 AID_AUDIO RW" << std::endl
65 << "S:0x0500 0xbeef R" << std::endl; // Read-only.
66
67 std::unordered_set<unsigned> expectedUids {1000, 1004, 0xbeef};
68
69 ASSERT_TRUE(parser->parseFromStream(&file, &aclMap));
70
71 auto range = aclMap.equal_range(VehicleProperty::HVAC_FAN_SPEED);
72 for (auto it = range.first; it != range.second; ++it) {
73 auto& acl = it->second;
74
75 ASSERT_EQ(1, expectedUids.count(acl.uid))
76 << " uid: " << std::hex << acl.uid;
77
78 if (acl.uid == 0xbeef) {
79 ASSERT_EQ(VehiclePropertyAccess::READ, acl.access);
80 } else {
81 ASSERT_EQ(VehiclePropertyAccess::READ_WRITE, acl.access);
82 }
83 }
84}
85
86TEST_F(AccessControlConfigParserTest, fileContainsJunk) {
87 std::stringstream file;
88 file << "This string will be ignored with warning in the log" << std::endl
89 << "# However comments are quit legitimate" << std::endl
90 << "S:0x0500 0xbeef R # YAY" << std::endl;
91
92 ASSERT_FALSE(parser->parseFromStream(&file, &aclMap));
93
94 ASSERT_EQ(1, aclMap.size());
95 auto it = aclMap.find(VehicleProperty::HVAC_FAN_SPEED);
96 ASSERT_NE(aclMap.end(), it);
97 ASSERT_EQ(VehiclePropertyAccess::READ, it->second.access);
98 ASSERT_EQ(VehicleProperty::HVAC_FAN_SPEED, it->second.propId);
99 ASSERT_EQ(0xbeef, it->second.uid);
100}
101
102TEST_F(AccessControlConfigParserTest, badIntegerFormat) {
103 std::stringstream file;
104 file << "S:0x0500 A12 RW " << std::endl;
105
106 ASSERT_FALSE(parser->parseFromStream(&file, &aclMap));
107 ASSERT_EQ(0, aclMap.size());
108}
109
110TEST_F(AccessControlConfigParserTest, ignoreNotSupportedProperties) {
111 std::stringstream file;
112 file << "S:0x0666 1000 RW " << std::endl;
113
114 ASSERT_FALSE(parser->parseFromStream(&file, &aclMap));
115 ASSERT_EQ(0, aclMap.size());
116}
117
118TEST_F(AccessControlConfigParserTest, multipleCalls) {
119 std::stringstream configFile;
120 configFile << "S:0x0500 1000 RW" << std::endl;
121
122 ASSERT_TRUE(parser->parseFromStream(&configFile, &aclMap));
123 ASSERT_EQ(1, aclMap.size());
124
125 std::stringstream configFile2;
126 configFile2 << "S:0x0501 1004 RW" << std::endl;
127 ASSERT_TRUE(parser->parseFromStream(&configFile2, &aclMap));
128 ASSERT_EQ(2, aclMap.size());
129
130 auto it = aclMap.find(VehicleProperty::HVAC_FAN_SPEED);
131 ASSERT_NE(aclMap.end(), it);
132 ASSERT_EQ(VehiclePropertyAccess::READ_WRITE, it->second.access);
133 ASSERT_EQ(VehicleProperty::HVAC_FAN_SPEED, it->second.propId);
134 ASSERT_EQ(1000u, it->second.uid);
135
136 it = aclMap.find(VehicleProperty::HVAC_FAN_DIRECTION);
137 ASSERT_NE(aclMap.end(), it);
138 ASSERT_EQ(VehiclePropertyAccess::READ_WRITE, it->second.access);
139 ASSERT_EQ(VehicleProperty::HVAC_FAN_DIRECTION, it->second.propId);
140 ASSERT_EQ(1004u, it->second.uid);
141}
142
143
144} // namespace anonymous
145
146} // namespace V2_0
147} // namespace vehicle
148} // namespace hardware
149} // namespace android