blob: e5894338bda77c3cc591997d6150ade9852c0d9f [file] [log] [blame]
Maksym Sloyko96342732021-08-03 21:54:28 +00001#!/bin/bash
2# Copyright 2021 Google LLC
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
17TEMPDIRS=""
18TEMPFILES=""
19# Script under test
20SUT=$PWD/usb_network.sh
21
22TEST_STATUS="OK"
23
24test_setup() {
25 echo -n "Testing $1 ..."
26 FAKE_CONFIGFS=$(mktemp -d)
27 TEMPDIRS="${TEMPDIRS} ${FAKE_CONFIGFS}"
28 FAKE_GADGETFS=$FAKE_CONFIGFS/usb_gadget
29 mkdir -p "$FAKE_GADGETFS"
30}
31
32test_teardown() {
33 echo ${TEST_STATUS}
34 if test -n "$TEMPDIRS"; then
35 rm -rf ${TEMPDIRS}
36 fi
37 if test -n "$TEMPFILES"; then
38 rm -f $TEMPFILES
39 fi
40}
41
42test_fail() {
43 echo -n " $@ " >&2
44 TEST_STATUS="FAIL"
45
46 test_teardown
47 exit 1
48}
49
50check_file_content() {
51 local filename="$1"
52 local expected_content="$2"
53
54 if ! test -f "${filename}"; then
55 test_fail "File ${filename} does not exist!"
56 fi
57
58 local actual_content=$(cat ${filename})
59 if [[ $expected_content != $actual_content ]]; then
60 test_fail "Expected ${expected_content}, got ${actual_content}"
61 fi
62}
63
64test_gadget_creation_with_defaults() {
65 local extra_args=()
66 local gadget_dir="$1"
67 if [[ $gadget_dir == "" ]]; then
68 gadget_dir="g1";
69 else
70 extra_args=(${extra_args} --gadget-dir-name "${gadget_dir}")
71 fi
72 local product_name="Souvenier BMC"
73 local product_id="0xcafe"
74 local host_mac="ab:cd:ef:10:11:12"
75 local dev_mac="12:11:10:ef:cd:ab"
76 local bind_device="f80002000.udc"
77 CONFIGFS_HOME=${FAKE_CONFIGFS} ${SUT} --product-id "${product_id}" \
78 --product-name "${product_name}" \
79 --host-mac "${host_mac}" \
80 --dev-mac "${dev_mac}" \
81 --bind-device "${bind_device}" \
82 ${extra_args[@]}
83
84 if test $? -ne 0; then
85 test_fail "${SUT} failed"
86 fi
87
88 if ! test -d "${FAKE_GADGETFS}/${gadget_dir}"; then
89 test_fail "Gadget was not created!"
90 fi
91
92 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idVendor "0x18d1"
93 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idProduct "${product_id}"
94 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/manufacturer "Google"
95 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/product "${product_name}"
96 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/MaxPower "100"
97 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/strings/0x409/configuration "ECM"
98 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0/dev_addr "${dev_mac}"
99 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0/host_addr "${host_mac}"
100
101 if ! test -d ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0; then
102 test_fail "Function directory was not created"
103 fi
104
105 local func_link="${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/ecm.usb0"
106 if ! test -L "${func_link}"; then
107 test_fail "Symlink to the function was not created in the config"
108 fi
109
110 local link_dest=$(realpath ${func_link})
111 if [[ $link_dest != ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0 ]]; then
112 test_fail "Symlink points to the wrong file/dir"
113 fi
114
115 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/UDC "${bind_device}"
116}
117
118test_gadget_creation_with_override() {
119 mkdir -p ${FAKE_GADGETFS}/g1/{strings,configs,functions}
120 touch ${FAKE_GADGETFS}/g1/{idVendor,idProduct}
121
122 test_gadget_creation_with_defaults
123}
124
125test_gadget_stopping() {
126 local extra_args=()
127 local gadget_dir="$1"
128 local iface_name="$2"
129 if [[ $gadget_dir == "" ]]; then
130 gadget_dir="g1";
131 else
132 extra_args=(${extra_args} --gadget-dir-name "${gadget_dir}")
133 fi
134
135 if [[ $iface_name == "" ]]; then
136 iface_name="usb0";
137 else
138 extra_args=(${extra_args} --iface-name "${iface_name}")
139 fi
140
141 CONFIGFS_HOME=${FAKE_CONFIGFS} ${SUT} ${extra_args[@]} stop
142
143 if test -d "${FAKE_GADGETFS}/${gadget_dir}"; then
144 test_fail "Gadget was not removed!"
145 fi
146}
147
148test_gadget_creation_no_macs() {
149 local gadget_dir="g1";
150 local product_name="Souvenier BMC"
151 local product_id="0xcafe"
152 local bind_device="f80002000.udc"
153 CONFIGFS_HOME=${FAKE_CONFIGFS} ${SUT} --product-id "${product_id}" \
154 --product-name "${product_name}" \
155 --bind-device "${bind_device}"
156
157 if test $? -ne 0; then
158 test_fail "${SUT} failed"
159 fi
160
161 if ! test -d "${FAKE_GADGETFS}/${gadget_dir}"; then
162 test_fail "Gadget was not created!"
163 fi
164
165 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idVendor "0x18d1"
166 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idProduct "${product_id}"
167 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/manufacturer "Google"
168 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/product "${product_name}"
169 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/MaxPower "100"
170 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/strings/0x409/configuration "ECM"
171
172 if test -e ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0/dev_addr; then
173 test_fail "dev_addr should not be set"
174 fi
175
176 if test -e ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0/host_addr; then
177 test_fail "host_addr should not be set"
178 fi
179
180 local func_link="${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/ecm.usb0"
181 if ! test -L "${func_link}"; then
182 test_fail "Symlink to the function was not created in the config"
183 fi
184
185 local link_dest=$(realpath ${func_link})
186 if [[ $link_dest != ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0 ]]; then
187 test_fail "Symlink points to the wrong file/dir"
188 fi
189
190 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/UDC "${bind_device}"
191}
192
193test_gadget_creation_alt_iface() {
194 local gadget_dir="g1";
195 local product_name="Souvenier BMC"
196 local product_id="0xcafe"
197 local bind_device="f80002000.udc"
198 local iface_name="iface0"
199 CONFIGFS_HOME=${FAKE_CONFIGFS} ${SUT} --product-id "${product_id}" \
200 --product-name "${product_name}" \
201 --bind-device "${bind_device}" \
202 --iface-name "${iface_name}"
203
204 if test $? -ne 0; then
205 test_fail "${SUT} failed"
206 fi
207
208 if ! test -d "${FAKE_GADGETFS}/${gadget_dir}"; then
209 test_fail "Gadget was not created!"
210 fi
211
212 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idVendor "0x18d1"
213 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idProduct "${product_id}"
214 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/manufacturer "Google"
215 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/product "${product_name}"
216 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/MaxPower "100"
217 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/strings/0x409/configuration "ECM"
218
219 if ! test -d ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.${iface_name}; then
220 test_fail "Function directory was not created"
221 fi
222
223 if test -e ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.${iface_name}/dev_addr; then
224 test_fail "dev_addr should not be set"
225 fi
226
227 if test -e ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.${iface_name}/host_addr; then
228 test_fail "host_addr should not be set"
229 fi
230
231 local func_link="${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/ecm.${iface_name}"
232 if ! test -L "${func_link}"; then
233 test_fail "Symlink to the function was not created in the config"
234 fi
235
236 local link_dest=$(realpath ${func_link})
237 if [[ $link_dest != ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.${iface_name} ]]; then
238 test_fail "Symlink points to the wrong file/dir"
239 fi
240
241 check_file_content ${FAKE_GADGETFS}/${gadget_dir}/UDC "${bind_device}"
242}
243
244
245# -------------------------------------------------------------------
246test_setup "Device Creation"
247
248test_gadget_creation_with_defaults
249
250test_teardown
251# -------------------------------------------------------------------
252
253# -------------------------------------------------------------------
254test_setup "Device Creation With Override"
255
256test_gadget_creation_with_override
257
258test_teardown
259# -------------------------------------------------------------------
260
261# -------------------------------------------------------------------
262test_setup "Test Device Stop"
263
264test_gadget_creation_with_defaults
265test_gadget_stopping
266
267test_teardown
268# -------------------------------------------------------------------
269
270# -------------------------------------------------------------------
271test_setup "Device Creation/Stopping, Alternative Name"
272
273test_gadget_creation_with_defaults "gAlt"
274test_gadget_stopping "gAlt"
275
276test_teardown
277# -------------------------------------------------------------------
278
279# -------------------------------------------------------------------
280test_setup "Device Creation without MAC Addrs"
281
282test_gadget_creation_no_macs
283
284test_teardown
285# -------------------------------------------------------------------
286
287# -------------------------------------------------------------------
288test_setup "Device Creation/Stopping, Alternative Interface"
289
290test_gadget_creation_alt_iface
291
292test_teardown
293# -------------------------------------------------------------------
294
295echo "SUCCESS!"