1 // Copyright 2014 The Android Open Source Project
2 //
3 // This software is licensed under the terms of the GNU General Public
4 // License version 2, as published by the Free Software Foundation, and
5 // may be copied, distributed, and modified under those terms.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License for more details.
11
12 #include "android/avd/util.h"
13 #include "android/utils/file_data.h"
14
15 #include <gtest/gtest.h>
16
TEST(AvdUtil,emulator_getBackendSuffix)17 TEST(AvdUtil, emulator_getBackendSuffix) {
18 EXPECT_STREQ("arm", emulator_getBackendSuffix("arm"));
19 EXPECT_STREQ("x86", emulator_getBackendSuffix("x86"));
20 EXPECT_STREQ("x86", emulator_getBackendSuffix("x86_64"));
21 EXPECT_STREQ("mips", emulator_getBackendSuffix("mips"));
22 EXPECT_STREQ("arm", emulator_getBackendSuffix("arm64"));
23
24 // TODO(digit): Add support for these CPU architectures to the emulator
25 // to change these to EXPECT_STREQ() calls.
26 EXPECT_FALSE(emulator_getBackendSuffix("mips64"));
27
28 EXPECT_FALSE(emulator_getBackendSuffix(NULL));
29 EXPECT_FALSE(emulator_getBackendSuffix("dummy"));
30 }
31
TEST(AvdUtil,propertyFile_getInt)32 TEST(AvdUtil, propertyFile_getInt) {
33 FileData fd;
34
35 const char* testFile =
36 "nineteen=19\n"
37 "int_min=-2147483648\n"
38 "int_max=2147483647\n"
39 "invalid=2147483648\n"
40 "invalid2=-2147483649\n"
41 "invalid3=bar\n"
42 "empty=\n";
43
44 EXPECT_EQ(0,fileData_initFromMemory(&fd, testFile, strlen(testFile)));
45
46 const int kDefault = 1138;
47 SearchResult kSearchResultGarbage = (SearchResult)0xdeadbeef;
48 SearchResult searchResult = kSearchResultGarbage;
49
50 EXPECT_EQ(kDefault,propertyFile_getInt(&fd, "invalid", kDefault, &searchResult));
51 EXPECT_EQ(RESULT_INVALID,searchResult);
52
53 searchResult = kSearchResultGarbage;
54 EXPECT_EQ(kDefault,propertyFile_getInt(&fd, "invalid2", kDefault, &searchResult));
55 EXPECT_EQ(RESULT_INVALID,searchResult);
56
57 searchResult = kSearchResultGarbage;
58 EXPECT_EQ(kDefault,propertyFile_getInt(&fd, "invalid3", kDefault, &searchResult));
59 EXPECT_EQ(RESULT_INVALID,searchResult);
60
61 searchResult = kSearchResultGarbage;
62 EXPECT_EQ(kDefault,propertyFile_getInt(&fd, "bar", kDefault, &searchResult));
63 EXPECT_EQ(RESULT_NOT_FOUND,searchResult);
64
65 searchResult = kSearchResultGarbage;
66 EXPECT_EQ(kDefault,propertyFile_getInt(&fd, "empty", kDefault, &searchResult));
67 EXPECT_EQ(RESULT_INVALID,searchResult);
68
69 searchResult = kSearchResultGarbage;
70 EXPECT_EQ(19,propertyFile_getInt(&fd, "nineteen", kDefault, &searchResult));
71 EXPECT_EQ(RESULT_FOUND,searchResult);
72
73 // check that null "searchResult" parameter is supported
74 EXPECT_EQ(kDefault,propertyFile_getInt(&fd, "bar", kDefault, NULL));
75 EXPECT_EQ(kDefault,propertyFile_getInt(&fd, "invalid", kDefault, NULL));
76 EXPECT_EQ(19,propertyFile_getInt(&fd, "nineteen", kDefault, NULL));
77 }
78
TEST(AvdUtil,propertyFile_getApiLevel)79 TEST(AvdUtil, propertyFile_getApiLevel) {
80 FileData fd;
81
82 const char* emptyFile =
83 "\n";
84
85 const char* testFile19 =
86 "ro.build.version.sdk=19\n";
87
88 const char* testFileBogus =
89 "ro.build.version.sdk=bogus\n";
90
91 EXPECT_EQ(0,fileData_initFromMemory(&fd, emptyFile, strlen(emptyFile)));
92 EXPECT_EQ(10000,propertyFile_getApiLevel(&fd));
93
94 EXPECT_EQ(0,fileData_initFromMemory(&fd, testFile19, strlen(testFile19)));
95 EXPECT_EQ(19,propertyFile_getApiLevel(&fd));
96
97 EXPECT_EQ(0,fileData_initFromMemory(&fd, testFileBogus, strlen(testFileBogus)));
98 EXPECT_EQ(3,propertyFile_getApiLevel(&fd));
99 }
100
TEST(AvdUtil,propertyFile_getAdbdCommunicationMode)101 TEST(AvdUtil, propertyFile_getAdbdCommunicationMode) {
102 FileData fd;
103
104 const char* emptyFile =
105 "\n";
106
107 const char* valueIsZero =
108 "ro.adb.qemud=0";
109
110 const char* valueIsOne =
111 "ro.adb.qemud=1";
112
113 const char* valueIsBogus =
114 "ro.adb.qemud=bogus";
115
116 // Empty file -> assume 1
117 EXPECT_EQ(0, fileData_initFromMemory(&fd, emptyFile, strlen(emptyFile)));
118 EXPECT_EQ(1, propertyFile_getAdbdCommunicationMode(&fd));
119
120 EXPECT_EQ(0, fileData_initFromMemory(&fd, valueIsZero, strlen(valueIsZero)));
121 EXPECT_EQ(0, propertyFile_getAdbdCommunicationMode(&fd));
122
123 EXPECT_EQ(0, fileData_initFromMemory(&fd, valueIsOne, strlen(valueIsOne)));
124 EXPECT_EQ(1, propertyFile_getAdbdCommunicationMode(&fd));
125
126 // BOGUS -> 1
127 EXPECT_EQ(0, fileData_initFromMemory(&fd, valueIsBogus, strlen(valueIsBogus)));
128 EXPECT_EQ(1, propertyFile_getAdbdCommunicationMode(&fd));
129 }
130
131