nl80211_attribute_unittest.cpp 11 KB

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