1 /*
2 * Copyright (C) 2020 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
17 #include <hidl2aidl/test/extension/translate-cpp.h>
18 #include <hidl2aidl/test/translate-cpp.h>
19
20 #include <gtest/gtest.h>
21
22 class Hidl2aidlTranslateTest : public ::testing::Test {};
23
24 namespace android {
25
TEST_F(Hidl2aidlTranslateTest,OnlyIn10)26 TEST_F(Hidl2aidlTranslateTest, OnlyIn10) {
27 hidl2aidl::test::OnlyIn10 dest;
28 hidl2aidl::test::V1_0::OnlyIn10 source;
29 source.str = "Hello";
30 ASSERT_TRUE(h2a::translate(source, &dest));
31 EXPECT_EQ(source.str, String8(dest.str).c_str());
32 }
33
TEST_F(Hidl2aidlTranslateTest,OnlyIn11)34 TEST_F(Hidl2aidlTranslateTest, OnlyIn11) {
35 hidl2aidl::test::OnlyIn11 dest;
36 hidl2aidl::test::V1_1::OnlyIn11 source;
37 source.str = 12;
38 ASSERT_TRUE(h2a::translate(source, &dest));
39 EXPECT_EQ(source.str, dest.str);
40 }
41
TEST_F(Hidl2aidlTranslateTest,OverrideMe)42 TEST_F(Hidl2aidlTranslateTest, OverrideMe) {
43 hidl2aidl::test::OverrideMe dest;
44 hidl2aidl::test::V1_1::OverrideMe source;
45 source.a = "World";
46 ASSERT_TRUE(h2a::translate(source, &dest));
47 EXPECT_EQ(source.a, String8(dest.a).c_str());
48 }
49
TEST_F(Hidl2aidlTranslateTest,Outer)50 TEST_F(Hidl2aidlTranslateTest, Outer) {
51 hidl2aidl::test::Outer dest;
52 hidl2aidl::test::V1_1::Outer source;
53 source.a = 12;
54 source.v1_0.inner.a = 16;
55 ASSERT_TRUE(h2a::translate(source, &dest));
56 EXPECT_EQ(source.a, dest.a);
57 EXPECT_EQ(source.v1_0.inner.a, dest.inner.a);
58 }
59
TEST_F(Hidl2aidlTranslateTest,OuterInner)60 TEST_F(Hidl2aidlTranslateTest, OuterInner) {
61 hidl2aidl::test::OuterInner dest;
62 hidl2aidl::test::V1_0::Outer::Inner source;
63 source.a = 12;
64 ASSERT_TRUE(h2a::translate(source, &dest));
65 EXPECT_EQ(source.a, dest.a);
66 }
67
TEST_F(Hidl2aidlTranslateTest,NameCollision)68 TEST_F(Hidl2aidlTranslateTest, NameCollision) {
69 hidl2aidl::test::NameCollision dest;
70 hidl2aidl::test::V1_2::NameCollision source;
71 source.reference.reference.a = 12;
72 source.reference.b = "Fancy";
73 source.c = "Car";
74 ASSERT_TRUE(h2a::translate(source, &dest));
75 EXPECT_EQ(source.reference.reference.a, dest.a);
76 EXPECT_EQ(source.reference.b, String8(dest.b).c_str());
77 EXPECT_EQ(source.c, String8(dest.c).c_str());
78 }
79
TEST_F(Hidl2aidlTranslateTest,IFooBigStruct)80 TEST_F(Hidl2aidlTranslateTest, IFooBigStruct) {
81 hidl2aidl::test::IFooBigStruct dest;
82 hidl2aidl::test::V1_1::IFoo::BigStruct source;
83 source.type = 12;
84 source.value = 16;
85 ASSERT_TRUE(h2a::translate(source, &dest));
86 EXPECT_EQ(source.type, dest.type);
87 EXPECT_EQ(source.value, dest.value);
88 }
89
TEST_F(Hidl2aidlTranslateTest,IBarInner)90 TEST_F(Hidl2aidlTranslateTest, IBarInner) {
91 hidl2aidl::test::IBarInner dest;
92 hidl2aidl::test::V1_0::IBar::Inner source;
93 source.a = 0x70000000;
94 ASSERT_TRUE(h2a::translate(source, &dest));
95 EXPECT_EQ(static_cast<int32_t>(source.a), dest.a);
96 }
97
TEST_F(Hidl2aidlTranslateTest,UnsignedToSignedTooLarge)98 TEST_F(Hidl2aidlTranslateTest, UnsignedToSignedTooLarge) {
99 hidl2aidl::test::IBarInner dest;
100 hidl2aidl::test::V1_0::IBar::Inner source;
101 // source.a is uint32_t, dest.a is int32_t
102 source.a = 0xf0000000;
103 ASSERT_FALSE(h2a::translate(source, &dest));
104 EXPECT_NE(static_cast<int32_t>(source.a), dest.a);
105 }
106
TEST_F(Hidl2aidlTranslateTest,SafeUnionBarByte)107 TEST_F(Hidl2aidlTranslateTest, SafeUnionBarByte) {
108 hidl2aidl::test::SafeUnionBar dest;
109 hidl2aidl::test::V1_2::SafeUnionBar source;
110 source.a(8);
111 ASSERT_TRUE(h2a::translate(source, &dest));
112 EXPECT_EQ(source.a(), dest.get<hidl2aidl::test::SafeUnionBar::a>());
113 }
114
TEST_F(Hidl2aidlTranslateTest,SafeUnionBarInt64)115 TEST_F(Hidl2aidlTranslateTest, SafeUnionBarInt64) {
116 hidl2aidl::test::SafeUnionBar dest;
117 hidl2aidl::test::V1_2::SafeUnionBar source;
118 source.b(25000);
119 ASSERT_TRUE(h2a::translate(source, &dest));
120 EXPECT_EQ(static_cast<int64_t>(source.b()), dest.get<hidl2aidl::test::SafeUnionBar::b>());
121 }
122
TEST_F(Hidl2aidlTranslateTest,SafeUnionBarInnerStructBar)123 TEST_F(Hidl2aidlTranslateTest, SafeUnionBarInnerStructBar) {
124 hidl2aidl::test::SafeUnionBar dest;
125 hidl2aidl::test::V1_2::SafeUnionBar source;
126 hidl2aidl::test::V1_2::SafeUnionBar::InnerStructBar inner;
127 inner.x = 8;
128 inner.z = 12;
129 source.innerStructBar(inner);
130 ASSERT_TRUE(h2a::translate(source, &dest));
131 EXPECT_EQ(source.innerStructBar().x,
132 dest.get<hidl2aidl::test::SafeUnionBar::innerStructBar>().x);
133 EXPECT_EQ(source.innerStructBar().z,
134 dest.get<hidl2aidl::test::SafeUnionBar::innerStructBar>().z);
135 }
136
TEST_F(Hidl2aidlTranslateTest,SafeUnionBarOnlyIn11)137 TEST_F(Hidl2aidlTranslateTest, SafeUnionBarOnlyIn11) {
138 hidl2aidl::test::SafeUnionBar dest;
139 hidl2aidl::test::V1_2::SafeUnionBar source;
140 hidl2aidl::test::V1_1::OnlyIn11 onlyIn11;
141 onlyIn11.str = 12;
142 source.c(onlyIn11);
143 ASSERT_TRUE(h2a::translate(source, &dest));
144 EXPECT_EQ(source.c().str, dest.get<hidl2aidl::test::SafeUnionBar::c>().str);
145 }
146
TEST_F(Hidl2aidlTranslateTest,SafeUnionBarString)147 TEST_F(Hidl2aidlTranslateTest, SafeUnionBarString) {
148 hidl2aidl::test::SafeUnionBar dest;
149 hidl2aidl::test::V1_2::SafeUnionBar source;
150 source.d("Hello world!");
151 ASSERT_TRUE(h2a::translate(source, &dest));
152 EXPECT_EQ(source.d(), String8(dest.get<hidl2aidl::test::SafeUnionBar::d>()).c_str());
153 }
154
TEST_F(Hidl2aidlTranslateTest,SafeUnionBarFloat)155 TEST_F(Hidl2aidlTranslateTest, SafeUnionBarFloat) {
156 hidl2aidl::test::SafeUnionBar dest;
157 hidl2aidl::test::V1_2::SafeUnionBar source;
158 source.e(3.5f);
159 ASSERT_TRUE(h2a::translate(source, &dest));
160 EXPECT_EQ(source.e(), dest.get<hidl2aidl::test::SafeUnionBar::e>());
161 }
162
TEST_F(Hidl2aidlTranslateTest,SafeUnionBarDouble)163 TEST_F(Hidl2aidlTranslateTest, SafeUnionBarDouble) {
164 hidl2aidl::test::SafeUnionBar dest;
165 hidl2aidl::test::V1_2::SafeUnionBar source;
166 source.f(3e10);
167 ASSERT_TRUE(h2a::translate(source, &dest));
168 EXPECT_EQ(source.f(), dest.get<hidl2aidl::test::SafeUnionBar::f>());
169 }
170
TEST_F(Hidl2aidlTranslateTest,SafeUnionBarBitfield)171 TEST_F(Hidl2aidlTranslateTest, SafeUnionBarBitfield) {
172 hidl2aidl::test::SafeUnionBar dest;
173 hidl2aidl::test::V1_2::SafeUnionBar source;
174 ::android::hardware::hidl_bitfield<::hidl2aidl::test::V1_2::FooFlag> bits(0);
175 bits |= hidl2aidl::test::V1_2::FooFlag::THIRD;
176 source.g(bits);
177 ASSERT_TRUE(h2a::translate(source, &dest));
178 EXPECT_EQ(static_cast<hidl2aidl::test::FooFlag>(source.g()),
179 dest.get<hidl2aidl::test::SafeUnionBar::g>());
180 }
181
TEST_F(Hidl2aidlTranslateTest,SafeUnionBarEnum)182 TEST_F(Hidl2aidlTranslateTest, SafeUnionBarEnum) {
183 hidl2aidl::test::SafeUnionBar dest;
184 hidl2aidl::test::V1_2::SafeUnionBar source;
185 source.h(hidl2aidl::test::V1_1::Value::B);
186 ASSERT_TRUE(h2a::translate(source, &dest));
187 EXPECT_EQ(static_cast<hidl2aidl::test::Value>(source.h()),
188 dest.get<hidl2aidl::test::SafeUnionBar::h>());
189 }
190
TEST_F(Hidl2aidlTranslateTest,SafeUnionBarChar16)191 TEST_F(Hidl2aidlTranslateTest, SafeUnionBarChar16) {
192 hidl2aidl::test::SafeUnionBar dest;
193 hidl2aidl::test::V1_2::SafeUnionBar source;
194 source.i(12);
195 ASSERT_TRUE(h2a::translate(source, &dest));
196 EXPECT_EQ(source.i(), dest.get<hidl2aidl::test::SafeUnionBar::i>());
197 }
198
TEST_F(Hidl2aidlTranslateTest,ArrayFoo)199 TEST_F(Hidl2aidlTranslateTest, ArrayFoo) {
200 hidl2aidl::test::ArrayFoo dest;
201 hidl2aidl::test::V1_2::ArrayFoo source;
202 source.a[0] = 42;
203 source.a[1] = 8;
204 ASSERT_TRUE(h2a::translate(source, &dest));
205 ASSERT_EQ(12u, dest.a.size());
206 EXPECT_EQ(source.a[0], dest.a[0]);
207 EXPECT_EQ(source.a[1], dest.a[1]);
208 EXPECT_EQ(source.a[2], dest.a[2]);
209 EXPECT_EQ(source.a[11], dest.a[11]);
210 }
211
TEST_F(Hidl2aidlTranslateTest,ArrayFooEmpty)212 TEST_F(Hidl2aidlTranslateTest, ArrayFooEmpty) {
213 hidl2aidl::test::ArrayFoo dest;
214 hidl2aidl::test::V1_2::ArrayFoo source;
215 ASSERT_TRUE(h2a::translate(source, &dest));
216 EXPECT_EQ(12u, dest.a.size());
217 EXPECT_EQ(0, dest.a[0]);
218 }
219
TEST_F(Hidl2aidlTranslateTest,ArrayFooEnum)220 TEST_F(Hidl2aidlTranslateTest, ArrayFooEnum) {
221 hidl2aidl::test::ArrayFoo dest;
222 hidl2aidl::test::V1_2::ArrayFoo source;
223 source.c[0] = hidl2aidl::test::V1_1::Value::A;
224 source.c[1] = hidl2aidl::test::V1_1::Value::B;
225 ASSERT_TRUE(h2a::translate(source, &dest));
226 EXPECT_EQ(static_cast<hidl2aidl::test::Value>(source.c[0]), dest.c[0]);
227 EXPECT_EQ(static_cast<hidl2aidl::test::Value>(source.c[1]), dest.c[1]);
228 }
229
TEST_F(Hidl2aidlTranslateTest,ArrayFooString)230 TEST_F(Hidl2aidlTranslateTest, ArrayFooString) {
231 hidl2aidl::test::ArrayFoo dest;
232 hidl2aidl::test::V1_2::ArrayFoo source;
233 source.d[0] = "hello";
234 source.d[1] = "world";
235 ASSERT_TRUE(h2a::translate(source, &dest));
236 EXPECT_EQ(source.d[0], String8(dest.d[0]).c_str());
237 EXPECT_EQ(source.d[1], String8(dest.d[1]).c_str());
238 }
239
TEST_F(Hidl2aidlTranslateTest,VectorFoo)240 TEST_F(Hidl2aidlTranslateTest, VectorFoo) {
241 hidl2aidl::test::VectorFoo dest;
242 hidl2aidl::test::V1_2::VectorFoo source;
243 source.a = {42, 8};
244 ASSERT_TRUE(h2a::translate(source, &dest));
245 EXPECT_EQ(source.a[0], dest.a[0]);
246 EXPECT_EQ(source.a[1], dest.a[1]);
247 }
248
TEST_F(Hidl2aidlTranslateTest,VectorFooEmpty)249 TEST_F(Hidl2aidlTranslateTest, VectorFooEmpty) {
250 hidl2aidl::test::VectorFoo dest;
251 hidl2aidl::test::V1_2::VectorFoo source;
252 ASSERT_TRUE(h2a::translate(source, &dest));
253 EXPECT_EQ(0u, dest.a.size());
254 }
255
TEST_F(Hidl2aidlTranslateTest,VectorFooUnsigned)256 TEST_F(Hidl2aidlTranslateTest, VectorFooUnsigned) {
257 hidl2aidl::test::VectorFoo dest;
258 hidl2aidl::test::V1_2::VectorFoo source;
259 // source.a is uint32_t, dest.a is int32_t
260 source.b = {12, 0xf0000000};
261 ASSERT_FALSE(h2a::translate(source, &dest));
262 }
263
TEST_F(Hidl2aidlTranslateTest,VectorFooEnum)264 TEST_F(Hidl2aidlTranslateTest, VectorFooEnum) {
265 hidl2aidl::test::VectorFoo dest;
266 hidl2aidl::test::V1_2::VectorFoo source;
267 source.c = {hidl2aidl::test::V1_1::Value::A, hidl2aidl::test::V1_1::Value::B};
268 ASSERT_TRUE(h2a::translate(source, &dest));
269 EXPECT_EQ(static_cast<hidl2aidl::test::Value>(source.c[0]), dest.c[0]);
270 EXPECT_EQ(static_cast<hidl2aidl::test::Value>(source.c[1]), dest.c[1]);
271 }
272
TEST_F(Hidl2aidlTranslateTest,VectorFooString)273 TEST_F(Hidl2aidlTranslateTest, VectorFooString) {
274 hidl2aidl::test::VectorFoo dest;
275 hidl2aidl::test::V1_2::VectorFoo source;
276 source.d = {"hello", "world"};
277 ASSERT_TRUE(h2a::translate(source, &dest));
278 EXPECT_EQ(source.d[0], String8(dest.d[0]).c_str());
279 EXPECT_EQ(source.d[1], String8(dest.d[1]).c_str());
280 }
281
TEST_F(Hidl2aidlTranslateTest,ExtensionArrayFoo)282 TEST_F(Hidl2aidlTranslateTest, ExtensionArrayFoo) {
283 hidl2aidl::test::extension::ArrayFoo dest;
284 hidl2aidl::test::extension::V1_2::ArrayFoo source;
285 source.e[0] = 12;
286 source.e[1] = 2;
287 ASSERT_TRUE(h2a::translate(source, &dest));
288 EXPECT_EQ(source.e[0], dest.e[0]);
289 EXPECT_EQ(source.e[1], dest.e[1]);
290 }
291
292 } // namespace android
293