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 <gtest/gtest.h>
17 
18 #include <cstdio>
19 #include <string>
20 
21 #include "UsbIpUtils.h"
22 
23 /* No virtual USB devices are attached. */
24 static const std::string EMPTY_STATUS =
25     "hub port sta spd dev      sockfd local_busid\n"
26     "hs  0000 004 000 00000000 000000 0-0\n"
27     "hs  0001 004 000 00000000 000000 0-0\n"
28     "hs  0002 004 000 00000000 000000 0-0\n"
29     "hs  0003 004 000 00000000 000000 0-0\n"
30     "ss  0004 004 000 00000000 000000 0-0\n"
31     "ss  0005 004 000 00000000 000000 0-0\n"
32     "ss  0006 004 000 00000000 000000 0-0\n"
33     "ss  0007 004 000 00000000 000000 0-0";
34 
35 /* A single high speed an super speed device are attached. */
36 static const std::string PORTS_ALLOCATED =
37     "hub port sta spd dev      sockfd local_busid\n"
38     "hs  0000 006 003 00010003 000003 4-1\n"
39     "hs  0001 004 000 00000000 000000 0-0\n"
40     "hs  0002 004 000 00000000 000000 0-0\n"
41     "hs  0003 004 000 00000000 000000 0-0\n"
42     "ss  0004 006 004 00010004 000004 5-1\n"
43     "ss  0005 004 000 00000000 000000 0-0\n"
44     "ss  0006 004 000 00000000 000000 0-0\n"
45     "ss  0007 004 000 00000000 000000 0-0";
46 
47 /* All USB device ports are allocated. */
48 static const std::string NONE_AVAILABLE =
49     "hub port sta spd dev      sockfd local_busid\n"
50     "hs  0000 006 003 00010001 000003 4-1\n"
51     "hs  0001 006 003 00010002 000004 4-2\n"
52     "hs  0002 006 003 00010003 000005 4-3\n"
53     "hs  0003 006 003 00010004 000006 4-4\n"
54     "ss  0004 006 004 00010005 000007 5-1\n"
55     "ss  0005 006 004 00010006 000008 5-2\n"
56     "ss  0006 006 004 00010007 000009 5-3\n"
57     "ss  0007 006 004 00010008 000010 5-4";
58 
59 /*
60  * Returns a file pointer associated with a std::string.
61  * NOTE: User should call fclose on the pointer when done.
62  */
get_fp_from_string(const std::string & test_input)63 static FILE *get_fp_from_string(const std::string &test_input) {
64     return fmemopen((void *)test_input.c_str(), test_input.size(), "r");
65 }
66 
TEST(UsbIpTest,ReturnsFirstHighSpeedPort)67 TEST(UsbIpTest, ReturnsFirstHighSpeedPort) {
68     FILE *file = get_fp_from_string(EMPTY_STATUS);
69     ASSERT_EQ(get_free_vhci_port(file, USBIP_SPEED_HIGH), 0);
70     fclose(file);
71 }
72 
TEST(UsbIpTest,ReturnsFirstSuperSpeedPort)73 TEST(UsbIpTest, ReturnsFirstSuperSpeedPort) {
74     FILE *file = get_fp_from_string(EMPTY_STATUS);
75     ASSERT_EQ(get_free_vhci_port(file, USBIP_SPEED_SUPER), 4);
76     fclose(file);
77 }
78 
TEST(UsbIpTest,ReturnsFirstFreeHighSpeedPort)79 TEST(UsbIpTest, ReturnsFirstFreeHighSpeedPort) {
80     FILE *file = get_fp_from_string(PORTS_ALLOCATED);
81     ASSERT_EQ(get_free_vhci_port(file, USBIP_SPEED_HIGH), 1);
82     fclose(file);
83 }
84 
TEST(UsbIpTest,ReturnsFirstFreeSuperSpeedPort)85 TEST(UsbIpTest, ReturnsFirstFreeSuperSpeedPort) {
86     FILE *file = get_fp_from_string(PORTS_ALLOCATED);
87     ASSERT_EQ(get_free_vhci_port(file, USBIP_SPEED_SUPER), 5);
88     fclose(file);
89 }
90 
TEST(UsbIpTest,AllHighSpeedPortsAllocatted)91 TEST(UsbIpTest, AllHighSpeedPortsAllocatted) {
92     FILE *file = get_fp_from_string(NONE_AVAILABLE);
93     ASSERT_EQ(get_free_vhci_port(file, USBIP_SPEED_HIGH), -1);
94     fclose(file);
95 }
96 
TEST(UsbIpTest,AllSuperSpeedPortsAllocated)97 TEST(UsbIpTest, AllSuperSpeedPortsAllocated) {
98     FILE *file = get_fp_from_string(NONE_AVAILABLE);
99     ASSERT_EQ(get_free_vhci_port(file, USBIP_SPEED_SUPER), -1);
100     fclose(file);
101 }
102