summaryrefslogtreecommitdiff
path: root/meta-google/recipes-google/networking/google-usb-network/usb_network_test.sh
blob: e5894338bda77c3cc591997d6150ade9852c0d9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/bin/bash
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


TEMPDIRS=""
TEMPFILES=""
# Script under test
SUT=$PWD/usb_network.sh

TEST_STATUS="OK"

test_setup() {
    echo -n "Testing $1 ..."
    FAKE_CONFIGFS=$(mktemp -d)
    TEMPDIRS="${TEMPDIRS} ${FAKE_CONFIGFS}"
    FAKE_GADGETFS=$FAKE_CONFIGFS/usb_gadget
    mkdir -p "$FAKE_GADGETFS"
}

test_teardown() {
    echo ${TEST_STATUS}
    if test -n "$TEMPDIRS"; then
        rm -rf ${TEMPDIRS}
    fi
    if test -n "$TEMPFILES"; then
        rm -f $TEMPFILES
    fi
}

test_fail() {
    echo -n " $@ " >&2
    TEST_STATUS="FAIL"

    test_teardown
    exit 1
}

check_file_content() {
    local filename="$1"
    local expected_content="$2"

    if ! test -f "${filename}"; then
        test_fail "File ${filename} does not exist!"
    fi

    local actual_content=$(cat ${filename})
    if [[ $expected_content != $actual_content ]]; then
        test_fail "Expected ${expected_content}, got ${actual_content}"
    fi
}

test_gadget_creation_with_defaults() {
    local extra_args=()
    local gadget_dir="$1"
    if [[ $gadget_dir == "" ]]; then
        gadget_dir="g1";
    else
        extra_args=(${extra_args} --gadget-dir-name "${gadget_dir}")
    fi
    local product_name="Souvenier BMC"
    local product_id="0xcafe"
    local host_mac="ab:cd:ef:10:11:12"
    local dev_mac="12:11:10:ef:cd:ab"
    local bind_device="f80002000.udc"
    CONFIGFS_HOME=${FAKE_CONFIGFS} ${SUT} --product-id "${product_id}" \
        --product-name "${product_name}" \
        --host-mac "${host_mac}" \
        --dev-mac "${dev_mac}" \
        --bind-device "${bind_device}" \
        ${extra_args[@]}

    if test $? -ne 0; then
        test_fail "${SUT} failed"
    fi

    if ! test -d "${FAKE_GADGETFS}/${gadget_dir}"; then
        test_fail "Gadget was not created!"
    fi

    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idVendor "0x18d1"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idProduct "${product_id}"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/manufacturer "Google"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/product "${product_name}"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/MaxPower "100"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/strings/0x409/configuration "ECM"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0/dev_addr "${dev_mac}"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0/host_addr "${host_mac}"

    if ! test -d ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0; then
        test_fail "Function directory was not created"
    fi

    local func_link="${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/ecm.usb0"
    if ! test -L "${func_link}"; then
        test_fail "Symlink to the function was not created in the config"
    fi

    local link_dest=$(realpath ${func_link})
    if [[ $link_dest != ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0 ]]; then
        test_fail "Symlink points to the wrong file/dir"
    fi

    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/UDC "${bind_device}"
}

test_gadget_creation_with_override() {
    mkdir -p ${FAKE_GADGETFS}/g1/{strings,configs,functions}
    touch ${FAKE_GADGETFS}/g1/{idVendor,idProduct}

    test_gadget_creation_with_defaults
}

test_gadget_stopping() {
    local extra_args=()
    local gadget_dir="$1"
    local iface_name="$2"
    if [[ $gadget_dir == "" ]]; then
        gadget_dir="g1";
    else
        extra_args=(${extra_args} --gadget-dir-name "${gadget_dir}")
    fi

    if [[ $iface_name == "" ]]; then
        iface_name="usb0";
    else
        extra_args=(${extra_args} --iface-name "${iface_name}")
    fi

    CONFIGFS_HOME=${FAKE_CONFIGFS} ${SUT} ${extra_args[@]} stop

    if test -d "${FAKE_GADGETFS}/${gadget_dir}"; then
        test_fail "Gadget was not removed!"
    fi
}

test_gadget_creation_no_macs() {
    local gadget_dir="g1";
    local product_name="Souvenier BMC"
    local product_id="0xcafe"
    local bind_device="f80002000.udc"
    CONFIGFS_HOME=${FAKE_CONFIGFS} ${SUT} --product-id "${product_id}" \
        --product-name "${product_name}" \
        --bind-device "${bind_device}"

    if test $? -ne 0; then
        test_fail "${SUT} failed"
    fi

    if ! test -d "${FAKE_GADGETFS}/${gadget_dir}"; then
        test_fail "Gadget was not created!"
    fi

    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idVendor "0x18d1"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idProduct "${product_id}"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/manufacturer "Google"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/product "${product_name}"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/MaxPower "100"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/strings/0x409/configuration "ECM"

    if test -e ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0/dev_addr; then
        test_fail "dev_addr should not be set"
    fi

    if test -e ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0/host_addr; then
        test_fail "host_addr should not be set"
    fi

    local func_link="${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/ecm.usb0"
    if ! test -L "${func_link}"; then
        test_fail "Symlink to the function was not created in the config"
    fi

    local link_dest=$(realpath ${func_link})
    if [[ $link_dest != ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.usb0 ]]; then
        test_fail "Symlink points to the wrong file/dir"
    fi

    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/UDC "${bind_device}"
}

test_gadget_creation_alt_iface() {
    local gadget_dir="g1";
    local product_name="Souvenier BMC"
    local product_id="0xcafe"
    local bind_device="f80002000.udc"
    local iface_name="iface0"
    CONFIGFS_HOME=${FAKE_CONFIGFS} ${SUT} --product-id "${product_id}" \
        --product-name "${product_name}" \
        --bind-device "${bind_device}" \
        --iface-name "${iface_name}"

    if test $? -ne 0; then
        test_fail "${SUT} failed"
    fi

    if ! test -d "${FAKE_GADGETFS}/${gadget_dir}"; then
        test_fail "Gadget was not created!"
    fi

    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idVendor "0x18d1"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idProduct "${product_id}"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/manufacturer "Google"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/product "${product_name}"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/MaxPower "100"
    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/strings/0x409/configuration "ECM"

    if ! test -d ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.${iface_name}; then
        test_fail "Function directory was not created"
    fi

    if test -e ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.${iface_name}/dev_addr; then
        test_fail "dev_addr should not be set"
    fi

    if test -e ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.${iface_name}/host_addr; then
        test_fail "host_addr should not be set"
    fi

    local func_link="${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/ecm.${iface_name}"
    if ! test -L "${func_link}"; then
        test_fail "Symlink to the function was not created in the config"
    fi

    local link_dest=$(realpath ${func_link})
    if [[ $link_dest != ${FAKE_GADGETFS}/${gadget_dir}/functions/ecm.${iface_name} ]]; then
        test_fail "Symlink points to the wrong file/dir"
    fi

    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/UDC "${bind_device}"
}


# -------------------------------------------------------------------
test_setup "Device Creation"

test_gadget_creation_with_defaults

test_teardown
# -------------------------------------------------------------------

# -------------------------------------------------------------------
test_setup "Device Creation With Override"

test_gadget_creation_with_override

test_teardown
# -------------------------------------------------------------------

# -------------------------------------------------------------------
test_setup "Test Device Stop"

test_gadget_creation_with_defaults
test_gadget_stopping

test_teardown
# -------------------------------------------------------------------

# -------------------------------------------------------------------
test_setup "Device Creation/Stopping, Alternative Name"

test_gadget_creation_with_defaults "gAlt"
test_gadget_stopping "gAlt"

test_teardown
# -------------------------------------------------------------------

# -------------------------------------------------------------------
test_setup "Device Creation without MAC Addrs"

test_gadget_creation_no_macs

test_teardown
# -------------------------------------------------------------------

# -------------------------------------------------------------------
test_setup "Device Creation/Stopping, Alternative Interface"

test_gadget_creation_alt_iface

test_teardown
# -------------------------------------------------------------------

echo "SUCCESS!"