• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
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 <memory>
18  
19  #include <gtest/gtest.h>
20  
21  #include "wificond/net/kernel-header-latest/nl80211.h"
22  #include "wificond/net/nl80211_attribute.h"
23  
24  namespace android {
25  namespace wificond {
26  
27  namespace {
28  
29  const uint32_t kU8Value1 = 200;
30  const uint32_t kU16Value1 = 5000;
31  const uint32_t kU32Value1 = 250000;
32  const uint32_t kU32Value2 = 500000;
33  const std::string kIFName = "wlan0";
34  const uint8_t kMacAddress[] = {
35      0xc0, 0x3f, 0x0e, 0x77, 0xe8, 0x7f
36  };
37  
38  // This header contains invalid buffer length
39  const uint8_t kBrokenBuffer[] = {
40      0xff, 0xff, // nla_len = 0xffff
41      0x01, 0x11, // nla_type
42      0x15, 0x12, // payload
43      0x00, 0x00  // padding
44  };
45  const uint8_t kValidU32AttrBuffer[] = {
46      0x08, 0x00, // nla_len = 8
47      0x01, 0x00, // nla_type
48      0xf1, 0x12, 0x12, 0x2a // payload
49  };
50  const uint8_t kBufferContainsStringWithTrailingZero[] = {
51      0x0a, 0x00, // nla_len = 10
52      0x01, 0x00, // nla_type
53      'w', 'l', 'a', 'n', '0', '\0',
54      0x00, 0x00  // padding
55  };
56  const uint8_t kBufferContainsStringWithTrailingZeros[] = {
57      0x0c, 0x00, // nla_len = 12
58      0x01, 0x00, // nla_type
59      'w', 'l', 'a', 'n', '0', '\0', '\0', '\0'
60  };
61  const uint8_t kBufferContainsStringWithoutTrailingZero[] = {
62      0x09, 0x00, // nla_len = 9
63      0x01, 0x00, // nla_type
64      'w', 'l', 'a', 'n', '0',
65      0x00, 0x00, 0x00  // padding
66  };
67  
68  const uint8_t kBufferContainsListOfAttributes[] = {
69      0x28, 0x00, // nla_len = 40
70      0x01, 0x00, // nla_type
71      // List of attributes:
72      // They have attribute id from 0 to N.
73      0x0a, 0x00, // nla_len = 10
74      0x00, 0x00, // nla_type = 0
75      'f', 'i', 'r', 's', 't','\0',
76      0x00, 0x00,  // padding
77      0x0b, 0x00, // nla_len = 11
78      0x01, 0x00, // nla_type = 1
79      's', 'e', 'c', 'o', 'n', 'd','\0',
80      0x00, // padding
81      0x0a, 0x00, // nla_len = 10
82      0x02, 0x00, // nla_type = 2
83      't', 'h', 'i', 'r', 'd','\0',
84      0x00, 0x00, // padding
85  };
86  
87  const uint8_t kBufferContainsListOfNestedAttributes[] = {
88      0x28, 0x00, // nla_len = 40
89      0x01, 0x00, // nla_type
90  
91      // List of nested attributes:
92      // They have attribute id from 0 to N.
93  
94      // Nested attribute 1:
95      0x0c, 0x00, // nla_len = 12
96      0x00, 0x00, // nla_type = 0
97          0x06, 0x00, // nla_len = 6
98          0x01, 0x00, // nla_type
99          0x05, 0x00, // uint16_t attribute with value 5
100          0x00, 0x00, // padding
101  
102      // Nested attribute 2:
103      0x0c, 0x00, // nla_len = 12
104      0x01, 0x00, // nla_type = 1
105          0x08, 0x00, // nla_len = 8
106          0x01, 0x00, // nla_type
107          0x0a, 0x00,
108          0x00, 0x00, // uint32_t attribute with value 10
109  
110      // Nested attribute 3:
111      0x0c, 0x00, // nla_len = 12
112      0x02, 0x00, // nla_type = 2
113          0x05, 0x00, // nla_len = 5
114          0x01, 0x00, // nla_type
115          0x08, 0x00, // uint8_t attribute with value 8
116          0x00, 0x00, // padding
117  };
118  
119  }  // namespace
120  
TEST(NL80211AttributeTest,U8AttributesSeriallizeCorrectly)121  TEST(NL80211AttributeTest,U8AttributesSeriallizeCorrectly) {
122    NL80211Attr<uint8_t> u8_attr(1, kU8Value1);
123    EXPECT_EQ(u8_attr.GetValue(), kU8Value1);
124  }
125  
TEST(NL80211AttributeTest,U16AttributesSeriallizeCorrectly)126  TEST(NL80211AttributeTest,U16AttributesSeriallizeCorrectly) {
127    NL80211Attr<uint16_t> u16_attr(1, kU16Value1);
128    EXPECT_EQ(u16_attr.GetValue(), kU16Value1);
129  }
130  
TEST(NL80211AttributeTest,U32AttributesSeriallizeCorrectly)131  TEST(NL80211AttributeTest,U32AttributesSeriallizeCorrectly) {
132    NL80211Attr<uint32_t> u32_attr(1, kU32Value1);
133    EXPECT_EQ(u32_attr.GetValue(), kU32Value1);
134  }
135  
TEST(NL80211AttributeTest,StringAttributesSeriallizeCorrectly)136  TEST(NL80211AttributeTest,StringAttributesSeriallizeCorrectly) {
137    NL80211Attr<std::string> str_attr(1, kIFName);
138    EXPECT_EQ(str_attr.GetValue(), kIFName);
139  }
140  
TEST(NL80211AttributeTest,ByteVectorsSeriallizeCorrectly)141  TEST(NL80211AttributeTest, ByteVectorsSeriallizeCorrectly) {
142    std::vector<uint8_t> mac_address(
143        kMacAddress,
144        kMacAddress + sizeof(kMacAddress));
145    NL80211Attr<std::vector<uint8_t>> byte_vector_attr(1, mac_address);
146    EXPECT_EQ(byte_vector_attr.GetValue(), mac_address);
147  }
148  
TEST(NL80211AttributeTest,CanGetNestedAttributes)149  TEST(NL80211AttributeTest, CanGetNestedAttributes) {
150    NL80211NestedAttr nested_attr(1);
151    NL80211Attr<uint32_t> u32_attr_1(1, kU32Value1);
152    NL80211Attr<uint32_t> u32_attr_2(2, kU32Value2);
153  
154    nested_attr.AddAttribute(u32_attr_1);
155    nested_attr.AddAttribute(u32_attr_2);
156  
157    EXPECT_TRUE(nested_attr.HasAttribute(1));
158    EXPECT_TRUE(nested_attr.HasAttribute(2));
159  
160    uint32_t attr_value;
161    EXPECT_TRUE(nested_attr.GetAttributeValue(1, &attr_value));
162    EXPECT_EQ(attr_value, kU32Value1);
163    EXPECT_TRUE(nested_attr.GetAttributeValue(2, &attr_value));
164    EXPECT_EQ(attr_value, kU32Value2);
165  }
166  
TEST(NL80211AttributeTest,CannotGetDoubleNestedAttributes)167  TEST(NL80211AttributeTest, CannotGetDoubleNestedAttributes) {
168    NL80211NestedAttr nested_attr(1);
169    NL80211NestedAttr deeper_nested_attr(2);
170    NL80211Attr<uint32_t> u32_attr_1(3, kU32Value1);
171  
172    deeper_nested_attr.AddAttribute(u32_attr_1);
173    nested_attr.AddAttribute(deeper_nested_attr);
174  
175    EXPECT_FALSE(nested_attr.HasAttribute(3));
176  }
177  
TEST(NL80211AttributeTest,CannotGetMissingAttribute)178  TEST(NL80211AttributeTest, CannotGetMissingAttribute) {
179    NL80211NestedAttr nested_attr(1);
180    NL80211Attr<uint32_t> u32_attr_1(1, kU32Value1);
181  
182    nested_attr.AddAttribute(u32_attr_1);
183  
184    uint32_t attr_value;
185    EXPECT_FALSE(nested_attr.HasAttribute(2));
186    EXPECT_FALSE(nested_attr.GetAttributeValue(2, &attr_value));
187  }
188  
TEST(NL80211AttributeTest,CannotGetAttributeWithWrongType)189  TEST(NL80211AttributeTest, CannotGetAttributeWithWrongType) {
190    NL80211NestedAttr nested_attr(1);
191    NL80211Attr<uint32_t> u32_attr_1(1, kU32Value1);
192  
193    nested_attr.AddAttribute(u32_attr_1);
194  
195    uint16_t attr_value;
196    EXPECT_TRUE(nested_attr.HasAttribute(1));
197    EXPECT_FALSE(nested_attr.GetAttributeValue(1, &attr_value));
198  }
199  
200  
TEST(NL80211AttributeTest,InvalidU32AttributeWithEmptyBuffer)201  TEST(NL80211AttributeTest, InvalidU32AttributeWithEmptyBuffer) {
202    std::vector<uint8_t> buffer;
203    NL80211Attr<uint32_t> invalid_attr(buffer);
204    EXPECT_FALSE(invalid_attr.IsValid());
205  }
206  
TEST(NL80211AttributeTest,InvalidU32AttributeWithBrokenBuffer)207  TEST(NL80211AttributeTest, InvalidU32AttributeWithBrokenBuffer) {
208    std::vector<uint8_t> buffer(
209        kBrokenBuffer,
210        kBrokenBuffer + sizeof(kBrokenBuffer));
211    NL80211Attr<uint32_t> invalid_attr(buffer);
212    EXPECT_FALSE(invalid_attr.IsValid());
213  }
214  
TEST(NL80211AttributeTest,InvalidU16AttributeWithU32Buffer)215  TEST(NL80211AttributeTest, InvalidU16AttributeWithU32Buffer) {
216    std::vector<uint8_t> buffer(
217        kValidU32AttrBuffer,
218        kValidU32AttrBuffer + sizeof(kValidU32AttrBuffer));
219    NL80211Attr<uint32_t> valid_attr(buffer);
220    NL80211Attr<uint16_t> invalid_attr(buffer);
221    EXPECT_TRUE(valid_attr.IsValid());
222    EXPECT_FALSE(invalid_attr.IsValid());
223  }
224  
TEST(NL80211AttributeTest,InitStringAttributeWithTrailingZeroFromBuffer)225  TEST(NL80211AttributeTest, InitStringAttributeWithTrailingZeroFromBuffer) {
226    std::vector<uint8_t> buffer(
227        kBufferContainsStringWithTrailingZero,
228        kBufferContainsStringWithTrailingZero +
229            sizeof(kBufferContainsStringWithTrailingZero));
230    NL80211Attr<std::string> str_attr(buffer);
231    EXPECT_EQ("wlan0", str_attr.GetValue());
232  }
233  
TEST(NL80211AttributeTest,InitStringAttributeWithTrailingZerosFromBuffer)234  TEST(NL80211AttributeTest, InitStringAttributeWithTrailingZerosFromBuffer) {
235    std::vector<uint8_t> buffer(
236        kBufferContainsStringWithTrailingZeros,
237        kBufferContainsStringWithTrailingZeros +
238            sizeof(kBufferContainsStringWithTrailingZeros));
239    NL80211Attr<std::string> str_attr(buffer);
240    EXPECT_EQ("wlan0", str_attr.GetValue());
241  }
242  
TEST(NL80211AttributeTest,InitStringAttributeWithoutTrailingZeroFromBuffer)243  TEST(NL80211AttributeTest, InitStringAttributeWithoutTrailingZeroFromBuffer) {
244    std::vector<uint8_t> buffer(
245        kBufferContainsStringWithoutTrailingZero,
246        kBufferContainsStringWithoutTrailingZero +
247            sizeof(kBufferContainsStringWithoutTrailingZero));
248    NL80211Attr<std::string> str_attr(buffer);
249    EXPECT_EQ("wlan0", str_attr.GetValue());
250  }
251  
TEST(NL80211AttributeTest,GetListOfStringsFromBuffer)252  TEST(NL80211AttributeTest, GetListOfStringsFromBuffer) {
253    std::vector<uint8_t> buffer(
254        kBufferContainsListOfAttributes,
255        kBufferContainsListOfAttributes +
256            sizeof(kBufferContainsListOfAttributes));
257    std::vector<std::string> strs;
258    std::vector<std::string> expected_strs = {"first", "second", "third"};
259    NL80211NestedAttr nested_attr(buffer);
260    nested_attr.GetListOfAttributeValues(&strs);
261    EXPECT_EQ(expected_strs, strs);
262  }
263  
TEST(NL80211AttributeTest,GetListOfAttributesFromBuffer)264  TEST(NL80211AttributeTest, GetListOfAttributesFromBuffer) {
265    std::vector<uint8_t> buffer(
266        kBufferContainsListOfAttributes,
267        kBufferContainsListOfAttributes +
268            sizeof(kBufferContainsListOfAttributes));
269  
270    std::vector<NL80211Attr<std::string>> attrs;
271    NL80211NestedAttr attr(buffer);
272    EXPECT_TRUE(attr.GetListOfAttributes(&attrs));
273    EXPECT_TRUE(attrs.size() == 3);
274    ASSERT_EQ(0, attrs[0].GetAttributeId());
275    ASSERT_EQ(1, attrs[1].GetAttributeId());
276    ASSERT_EQ(2, attrs[2].GetAttributeId());
277    ASSERT_EQ("first", attrs[0].GetValue());
278    ASSERT_EQ("second", attrs[1].GetValue());
279    ASSERT_EQ("third", attrs[2].GetValue());
280  }
281  
TEST(NL80211AttributeTest,GetListOfNestedAttributesFromBuffer)282  TEST(NL80211AttributeTest, GetListOfNestedAttributesFromBuffer) {
283    std::vector<uint8_t> buffer(
284        kBufferContainsListOfNestedAttributes,
285        kBufferContainsListOfNestedAttributes +
286            sizeof(kBufferContainsListOfNestedAttributes));
287    std::vector<NL80211NestedAttr> nested_attrs;
288    NL80211NestedAttr attr(buffer);
289    EXPECT_TRUE(attr.GetListOfNestedAttributes(&nested_attrs));
290    EXPECT_TRUE(nested_attrs.size() == 3);
291    uint16_t value1 = 0;
292    uint32_t value2 = 0;
293    uint8_t value3 = 0;
294    ASSERT_TRUE(nested_attrs[0].GetAttributeValue(1, &value1));
295    ASSERT_TRUE(nested_attrs[1].GetAttributeValue(1, &value2));
296    ASSERT_TRUE(nested_attrs[2].GetAttributeValue(1, &value3));
297    EXPECT_TRUE(value1 == 5);
298    EXPECT_TRUE(value2 == 10);
299    EXPECT_TRUE(value3 == 8);
300  }
301  
TEST(NL80211AttributeTest,MergeAttributes)302  TEST(NL80211AttributeTest, MergeAttributes) {
303    NL80211Attr<std::vector<uint8_t>> attr1(1, {'a', 'b', 'c'});
304    NL80211Attr<std::vector<uint8_t>> attr2(1, {'d', 'e'});
305    ASSERT_TRUE(attr1.Merge(attr2));
306    std::vector<uint8_t> expected_value{{'a', 'b', 'c', 'd', 'e'}};
307    EXPECT_EQ(expected_value, attr1.GetValue());
308  }
309  
TEST(NL80211AttributeTest,CannotMergeInvalidAttributeWithBrokenBuffer)310  TEST(NL80211AttributeTest, CannotMergeInvalidAttributeWithBrokenBuffer) {
311    NL80211Attr<std::vector<uint8_t>> valid_attr(1, {'a', 'b', 'c'});
312    std::vector<uint8_t> broken_buffer(
313        kBrokenBuffer,
314        kBrokenBuffer + sizeof(kBrokenBuffer));
315    NL80211Attr<std::vector<uint8_t>> invalid_attr(broken_buffer);
316    EXPECT_FALSE(valid_attr.Merge(invalid_attr));
317  }
318  
TEST(NL80211AttributeTest,CannotMergeAttributesWithDifferentIds)319  TEST(NL80211AttributeTest, CannotMergeAttributesWithDifferentIds) {
320    NL80211Attr<std::vector<uint8_t>> attr1(1, {'a', 'b', 'c'});
321    NL80211Attr<std::vector<uint8_t>> attr2(2, {'d', 'e', 'f'});
322    EXPECT_FALSE(attr1.Merge(attr2));
323  }
324  
TEST(NL80211AttributeTest,MergeNestedAttributes)325  TEST(NL80211AttributeTest, MergeNestedAttributes) {
326    NL80211NestedAttr nested_attr1(0);
327    NL80211NestedAttr nested_attr2(0);
328    NL80211Attr<uint32_t> uint32_attr1(1, kU32Value1);
329    NL80211Attr<uint32_t> uint32_attr2(2, kU32Value2);
330    nested_attr1.AddAttribute(uint32_attr1);
331    nested_attr2.AddAttribute(uint32_attr2);
332    ASSERT_TRUE(nested_attr1.Merge(nested_attr2));
333  
334    uint32_t value1, value2;
335    EXPECT_TRUE(nested_attr1.GetAttributeValue(1, &value1));
336    EXPECT_TRUE(value1 == kU32Value1);
337    EXPECT_TRUE(nested_attr1.GetAttributeValue(2, &value2));
338    EXPECT_TRUE(value2 == kU32Value2);
339  }
340  
341  }  // namespace wificond
342  }  // namespace android
343