Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: device/usb/usb_descriptors_unittest.cc

Issue 1707453002: Reland of Parse USB interface association descriptors. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/usb/usb_descriptors.cc ('k') | device/usb/usb_device_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "device/usb/mock_usb_device_handle.h" 9 #include "device/usb/mock_usb_device_handle.h"
10 #include "device/usb/usb_descriptors.h" 10 #include "device/usb/usb_descriptors.h"
(...skipping 14 matching lines...) Expand all
25 void ExpectStringDescriptors( 25 void ExpectStringDescriptors(
26 scoped_ptr<std::map<uint8_t, base::string16>> string_map) { 26 scoped_ptr<std::map<uint8_t, base::string16>> string_map) {
27 EXPECT_EQ(3u, string_map->size()); 27 EXPECT_EQ(3u, string_map->size());
28 EXPECT_EQ(base::ASCIIToUTF16("String 1"), (*string_map)[1]); 28 EXPECT_EQ(base::ASCIIToUTF16("String 1"), (*string_map)[1]);
29 EXPECT_EQ(base::ASCIIToUTF16("String 2"), (*string_map)[2]); 29 EXPECT_EQ(base::ASCIIToUTF16("String 2"), (*string_map)[2]);
30 EXPECT_EQ(base::ASCIIToUTF16("String 3"), (*string_map)[3]); 30 EXPECT_EQ(base::ASCIIToUTF16("String 3"), (*string_map)[3]);
31 } 31 }
32 32
33 class UsbDescriptorsTest : public ::testing::Test {}; 33 class UsbDescriptorsTest : public ::testing::Test {};
34 34
35 TEST_F(UsbDescriptorsTest, NoInterfaceAssociations) {
36 UsbConfigDescriptor config(1, false, false, 0);
37 config.interfaces.emplace_back(0, 0, 255, 255, 255);
38 config.interfaces.emplace_back(0, 1, 255, 255, 255);
39 config.interfaces.emplace_back(1, 0, 255, 255, 255);
40 config.AssignFirstInterfaceNumbers();
41
42 EXPECT_EQ(0, config.interfaces[0].first_interface);
43 EXPECT_EQ(0, config.interfaces[1].first_interface);
44 EXPECT_EQ(1, config.interfaces[2].first_interface);
45 }
46
47 TEST_F(UsbDescriptorsTest, InterfaceAssociations) {
48 // Links interfaces 0 and 1 into a single function.
49 static const uint8_t kIAD1[] = {0x08, 0x0b, 0x00, 0x02,
50 0xff, 0xff, 0xff, 0x00};
51 // Only references a single interface, 2.
52 static const uint8_t kIAD2[] = {0x08, 0x0b, 0x02, 0x01,
53 0xff, 0xff, 0xff, 0x00};
54 // Malformed. References interface 3 but bInterfaceCount is 0.
55 static const uint8_t kIAD3[] = {0x08, 0x0b, 0x03, 0x00,
56 0xff, 0xff, 0xff, 0x00};
57 // Links interfaces 4 and 5 into a single function.
58 static const uint8_t kIAD4[] = {0x08, 0x0b, 0x04, 0x02,
59 0xff, 0xff, 0xff, 0x00};
60
61 UsbConfigDescriptor config(1, false, false, 0);
62 config.extra_data.assign(kIAD1, kIAD1 + sizeof(kIAD1));
63 config.extra_data.insert(config.extra_data.end(), kIAD2,
64 kIAD2 + sizeof(kIAD2));
65 config.interfaces.emplace_back(0, 0, 255, 255, 255);
66 config.interfaces.emplace_back(1, 0, 255, 255, 255);
67 UsbInterfaceDescriptor iface1a(1, 1, 255, 255, 255);
68 iface1a.extra_data.assign(kIAD3, kIAD3 + sizeof(kIAD3));
69 config.interfaces.push_back(std::move(iface1a));
70 config.interfaces.emplace_back(2, 0, 255, 255, 255);
71 config.interfaces.emplace_back(3, 0, 255, 255, 255);
72 UsbInterfaceDescriptor iface4(4, 0, 255, 255, 255);
73 iface4.extra_data.assign(kIAD4, kIAD4 + sizeof(kIAD4));
74 config.interfaces.push_back(std::move(iface4));
75 config.interfaces.emplace_back(5, 0, 255, 255, 255);
76 config.AssignFirstInterfaceNumbers();
77
78 // Interfaces 0 and 1 (plus 1's alternate) are a single function.
79 EXPECT_EQ(0, config.interfaces[0].interface_number);
80 EXPECT_EQ(0, config.interfaces[0].first_interface);
81 EXPECT_EQ(1, config.interfaces[1].interface_number);
82 EXPECT_EQ(0, config.interfaces[1].first_interface);
83 EXPECT_EQ(1, config.interfaces[2].interface_number);
84 EXPECT_EQ(0, config.interfaces[2].first_interface);
85
86 // Interfaces 2 and 3 are their own functions.
87 EXPECT_EQ(2, config.interfaces[3].interface_number);
88 EXPECT_EQ(2, config.interfaces[3].first_interface);
89 EXPECT_EQ(3, config.interfaces[4].interface_number);
90 EXPECT_EQ(3, config.interfaces[4].first_interface);
91
92 // Interfaces 4 and 5 are a single function.
93 EXPECT_EQ(4, config.interfaces[5].interface_number);
94 EXPECT_EQ(4, config.interfaces[5].first_interface);
95 EXPECT_EQ(5, config.interfaces[6].interface_number);
96 EXPECT_EQ(4, config.interfaces[6].first_interface);
97 }
98
99 TEST_F(UsbDescriptorsTest, CorruptInterfaceAssociations) {
100 {
101 // Descriptor is too short.
102 static const uint8_t kIAD[] = {0x01};
103 UsbConfigDescriptor config(1, false, false, 0);
104 config.extra_data.assign(kIAD, kIAD + sizeof(kIAD));
105 config.AssignFirstInterfaceNumbers();
106 }
107 {
108 // Descriptor is too long.
109 static const uint8_t kIAD[] = {0x09, 0x0b, 0x00, 0x00,
110 0x00, 0x00, 0x00, 0x00};
111 UsbConfigDescriptor config(1, false, false, 0);
112 config.extra_data.assign(kIAD, kIAD + sizeof(kIAD));
113 config.AssignFirstInterfaceNumbers();
114 }
115 {
116 // References an undefined interface.
117 static const uint8_t kIAD[] = {0x08, 0x0b, 0x07, 0x00,
118 0xff, 0xff, 0xff, 0x00};
119 UsbConfigDescriptor config(1, false, false, 0);
120 config.interfaces.emplace_back(0, 0, 255, 255, 255);
121 config.extra_data.assign(kIAD, kIAD + sizeof(kIAD));
122 config.AssignFirstInterfaceNumbers();
123
124 EXPECT_EQ(0, config.interfaces[0].interface_number);
125 EXPECT_EQ(0, config.interfaces[0].first_interface);
126 }
127 }
128
35 TEST_F(UsbDescriptorsTest, StringDescriptor) { 129 TEST_F(UsbDescriptorsTest, StringDescriptor) {
36 static const uint8_t kBuffer[] = {0x1a, 0x03, 'H', 0, 'e', 0, 'l', 0, 'l', 0, 130 static const uint8_t kBuffer[] = {0x1a, 0x03, 'H', 0, 'e', 0, 'l', 0, 'l', 0,
37 'o', 0, ' ', 0, 'w', 0, 'o', 0, 'r', 0, 131 'o', 0, ' ', 0, 'w', 0, 'o', 0, 'r', 0,
38 'l', 0, 'd', 0, '!', 0}; 132 'l', 0, 'd', 0, '!', 0};
39 base::string16 string; 133 base::string16 string;
40 ASSERT_TRUE(ParseUsbStringDescriptor( 134 ASSERT_TRUE(ParseUsbStringDescriptor(
41 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &string)); 135 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &string));
42 EXPECT_EQ(base::ASCIIToUTF16("Hello world!"), string); 136 EXPECT_EQ(base::ASCIIToUTF16("Hello world!"), string);
43 } 137 }
44 138
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 _, _, _)) 220 _, _, _))
127 .WillOnce(InvokeCallback(kStringDescriptor3, sizeof(kStringDescriptor3))); 221 .WillOnce(InvokeCallback(kStringDescriptor3, sizeof(kStringDescriptor3)));
128 222
129 ReadUsbStringDescriptors(device_handle, std::move(string_map), 223 ReadUsbStringDescriptors(device_handle, std::move(string_map),
130 base::Bind(&ExpectStringDescriptors)); 224 base::Bind(&ExpectStringDescriptors));
131 } 225 }
132 226
133 } // namespace 227 } // namespace
134 228
135 } // namespace device 229 } // namespace device
OLDNEW
« no previous file with comments | « device/usb/usb_descriptors.cc ('k') | device/usb/usb_device_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698