• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020, The Android Open Source Project
3  *
4  * Licensed under the Apache License, is(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 package hidl2aidl.tests;
18 
19 import static org.hamcrest.core.Is.is;
20 import static org.junit.Assert.assertThat;
21 import static org.junit.Assert.fail;
22 
23 import hidl2aidl.test.Translate;
24 import java.util.ArrayList;
25 import org.junit.Test;
26 import org.junit.runners.JUnit4;
27 
28 public class TranslateJavaTest {
29     @Test
OnlyIn10()30     public void OnlyIn10() {
31         hidl2aidl.test.OnlyIn10 dest;
32         hidl2aidl.test.V1_0.OnlyIn10 source = new hidl2aidl.test.V1_0.OnlyIn10();
33         source.str = "Hello";
34         dest = Translate.h2aTranslate(source);
35         assertThat(source.str, is(dest.str));
36     }
37 
38     @Test
OnlyIn11()39     public void OnlyIn11() {
40         hidl2aidl.test.OnlyIn11 dest;
41         hidl2aidl.test.V1_1.OnlyIn11 source = new hidl2aidl.test.V1_1.OnlyIn11();
42         source.str = 12;
43         dest = Translate.h2aTranslate(source);
44         assertThat(source.str, is(dest.str));
45     }
46 
47     @Test
OverrideMe()48     public void OverrideMe() {
49         hidl2aidl.test.OverrideMe dest;
50         hidl2aidl.test.V1_1.OverrideMe source = new hidl2aidl.test.V1_1.OverrideMe();
51         source.a = "World";
52         dest = Translate.h2aTranslate(source);
53         assertThat(source.a, is(dest.a));
54     }
55 
56     @Test
Outer()57     public void Outer() {
58         hidl2aidl.test.Outer dest;
59         hidl2aidl.test.V1_1.Outer source = new hidl2aidl.test.V1_1.Outer();
60         source.a = 12;
61         source.v1_0.inner.a = 16;
62         dest = Translate.h2aTranslate(source);
63         assertThat(source.v1_0.inner.a, is(dest.inner.a));
64     }
65 
66     @Test
OuterInner()67     public void OuterInner() {
68         hidl2aidl.test.Outer.Inner dest;
69         hidl2aidl.test.V1_0.Outer.Inner source = new hidl2aidl.test.V1_0.Outer.Inner();
70         source.a = 12;
71         dest = Translate.h2aTranslate(source);
72         assertThat(source.a, is(dest.a));
73     }
74 
75     @Test
NameCollision()76     public void NameCollision() {
77         hidl2aidl.test.NameCollision dest;
78         hidl2aidl.test.V1_2.NameCollision source = new hidl2aidl.test.V1_2.NameCollision();
79         source.reference.reference.a = 12;
80         source.reference.b = "Fancy";
81         source.c = "Car";
82         dest = Translate.h2aTranslate(source);
83         assertThat(source.reference.reference.a, is(dest.a));
84         assertThat(source.reference.b, is(dest.b));
85         assertThat(source.c, is(dest.c));
86     }
87 
88     @Test
IFooBigStruct()89     public void IFooBigStruct() {
90         hidl2aidl.test.IFoo.BigStruct dest;
91         hidl2aidl.test.V1_1.IFoo.BigStruct source = new hidl2aidl.test.V1_1.IFoo.BigStruct();
92         source.type = 12;
93         source.value = 16;
94         dest = Translate.h2aTranslate(source);
95         assertThat(source.type, is(dest.type));
96         assertThat(source.value, is(dest.value));
97     }
98 
99     @Test
IBarInner()100     public void IBarInner() {
101         hidl2aidl.test.IBar.Inner dest;
102         hidl2aidl.test.V1_0.IBar.Inner source = new hidl2aidl.test.V1_0.IBar.Inner();
103         source.a = 0x70000000;
104         dest = Translate.h2aTranslate(source);
105         assertThat(source.a, is(dest.a));
106     }
107 
108     @Test
UnsignedToSignedTooLarge()109     public void UnsignedToSignedTooLarge() {
110         hidl2aidl.test.IBar.Inner dest;
111         hidl2aidl.test.V1_0.IBar.Inner source = new hidl2aidl.test.V1_0.IBar.Inner();
112         // source.a is uint32_t, dest.a is int32_t
113         source.a = -1;
114         try {
115             dest = Translate.h2aTranslate(source);
116             fail("Expected an exception to be thrown for out of bounds unsigned to signed translation");
117         } catch (RuntimeException e) {
118             String message = e.getMessage();
119             assertThat("Unsafe conversion between signed and unsigned scalars for field: in.a",
120                     is(message));
121         }
122     }
123 
124     @Test
SafeUnionBarByte()125     public void SafeUnionBarByte() {
126         hidl2aidl.test.SafeUnionBar dest;
127         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
128         source.a((byte) 8);
129         assertThat(hidl2aidl.test.V1_2.SafeUnionBar.hidl_discriminator.a,
130                 is(source.getDiscriminator()));
131         dest = Translate.h2aTranslate(source);
132         assertThat(source.a(), is(dest.getA()));
133     }
134 
135     @Test
SafeUnionBarInt64()136     public void SafeUnionBarInt64() {
137         hidl2aidl.test.SafeUnionBar dest;
138         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
139         source.b(25000);
140         dest = Translate.h2aTranslate(source);
141         assertThat(source.b(), is(dest.getB()));
142     }
143 
144     @Test
SafeUnionBarInnerStructBar()145     public void SafeUnionBarInnerStructBar() {
146         hidl2aidl.test.SafeUnionBar dest;
147         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
148         hidl2aidl.test.V1_2.SafeUnionBar.InnerStructBar inner =
149                 new hidl2aidl.test.V1_2.SafeUnionBar.InnerStructBar();
150         inner.x = 8;
151         inner.z = 12;
152         source.innerStructBar(inner);
153         dest = Translate.h2aTranslate(source);
154         assertThat(source.innerStructBar().x, is(dest.getInnerStructBar().x));
155         assertThat(source.innerStructBar().z, is(dest.getInnerStructBar().z));
156     }
157 
158     @Test
SafeUnionBarOnlyIn11()159     public void SafeUnionBarOnlyIn11() {
160         hidl2aidl.test.SafeUnionBar dest;
161         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
162         hidl2aidl.test.V1_1.OnlyIn11 onlyIn11 = new hidl2aidl.test.V1_1.OnlyIn11();
163         onlyIn11.str = 12;
164         source.c(onlyIn11);
165         dest = Translate.h2aTranslate(source);
166         assertThat(source.c().str, is(dest.getC().str));
167     }
168 
169     @Test
SafeUnionBarString()170     public void SafeUnionBarString() {
171         hidl2aidl.test.SafeUnionBar dest;
172         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
173         source.d("Hello world!");
174         dest = Translate.h2aTranslate(source);
175         assertThat(source.d(), is(dest.getD()));
176     }
177 
178     @Test
SafeUnionBarFloat()179     public void SafeUnionBarFloat() {
180         hidl2aidl.test.SafeUnionBar dest;
181         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
182         source.e(3.5f);
183         dest = Translate.h2aTranslate(source);
184         assertThat(source.e(), is(dest.getE()));
185     }
186 
187     @Test
SafeUnionBarDouble()188     public void SafeUnionBarDouble() {
189         hidl2aidl.test.SafeUnionBar dest;
190         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
191         source.f(3e10);
192         dest = Translate.h2aTranslate(source);
193         assertThat(source.f(), is(dest.getF()));
194     }
195 
196     @Test
SafeUnionBarBitfield()197     public void SafeUnionBarBitfield() {
198         hidl2aidl.test.SafeUnionBar dest;
199         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
200         source.g(hidl2aidl.test.V1_2.FooFlag.THIRD);
201         dest = Translate.h2aTranslate(source);
202         assertThat(source.g(), is(dest.getG()));
203     }
204 
205     @Test
SafeUnionBarEnum()206     public void SafeUnionBarEnum() {
207         hidl2aidl.test.SafeUnionBar dest;
208         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
209         source.h(hidl2aidl.test.V1_1.Value.B);
210         dest = Translate.h2aTranslate(source);
211         assertThat(source.h(), is(dest.getH()));
212     }
213 
214     @Test
SafeUnionBarChar()215     public void SafeUnionBarChar() {
216         hidl2aidl.test.SafeUnionBar dest;
217         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
218         source.i((short) 12);
219         dest = Translate.h2aTranslate(source);
220         assertThat(source.i(), is((short) dest.getI()));
221     }
222 
223     @Test
SafeUnionBarNegativeChar()224     public void SafeUnionBarNegativeChar() {
225         hidl2aidl.test.SafeUnionBar dest;
226         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
227         source.i((short) -1);
228         try {
229             dest = Translate.h2aTranslate(source);
230             fail("Expected an exception to be thrown for out of bounds signed to unsigned translation");
231         } catch (RuntimeException e) {
232             String message = e.getMessage();
233             assertThat("Unsafe conversion between signed and unsigned scalars for field: in.i()",
234                     is(message));
235         }
236     }
237 
238     @Test
SafeUnionBarVecByte()239     public void SafeUnionBarVecByte() {
240         hidl2aidl.test.SafeUnionBar dest;
241         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
242         source.j(new ArrayList<Byte>());
243         source.j().add((byte) 12);
244         source.j().add((byte) 6);
245         dest = Translate.h2aTranslate(source);
246         assertThat(source.j().get(0), is((byte) dest.getJ()[0]));
247         assertThat(source.j().get(1), is((byte) dest.getJ()[1]));
248     }
249 
250     @Test
SafeUnionBarVecLong()251     public void SafeUnionBarVecLong() {
252         hidl2aidl.test.SafeUnionBar dest;
253         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
254         source.k(new ArrayList<Integer>());
255         source.k().add(hidl2aidl.test.Value.A);
256         source.k().add(hidl2aidl.test.Value.B);
257         dest = Translate.h2aTranslate(source);
258         assertThat(source.k().get(0), is(dest.getK()[0]));
259         assertThat(source.k().get(1), is(dest.getK()[1]));
260     }
261 
262     @Test
SafeUnionBarArrayByte()263     public void SafeUnionBarArrayByte() {
264         hidl2aidl.test.SafeUnionBar dest;
265         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
266         source.l(new byte[2]);
267         source.l()[0] = (byte) 12;
268         source.l()[1] = (byte) 6;
269         dest = Translate.h2aTranslate(source);
270         assertThat(source.l()[0], is(dest.getL()[0]));
271         assertThat(source.l()[1], is(dest.getL()[1]));
272     }
273 
274     @Test
SafeUnionBarRepeatedFloat()275     public void SafeUnionBarRepeatedFloat() {
276         hidl2aidl.test.SafeUnionBar dest;
277         hidl2aidl.test.V1_2.SafeUnionBar source = new hidl2aidl.test.V1_2.SafeUnionBar();
278         source.m(3.5f);
279         dest = Translate.h2aTranslate(source);
280         assertThat(source.m(), is(dest.getM()));
281     }
282 
283     @Test
ArrayFoo()284     public void ArrayFoo() {
285         hidl2aidl.test.ArrayFoo dest;
286         hidl2aidl.test.V1_2.ArrayFoo source = new hidl2aidl.test.V1_2.ArrayFoo();
287         source.a[0] = 42;
288         source.a[0] = 42;
289         assertThat(source.e.length, is(0));
290         dest = Translate.h2aTranslate(source);
291         assertThat(source.a[0], is(dest.a[0]));
292     }
293 
294     @Test
ArrayFooEmpty()295     public void ArrayFooEmpty() {
296         hidl2aidl.test.ArrayFoo dest;
297         hidl2aidl.test.V1_2.ArrayFoo source = new hidl2aidl.test.V1_2.ArrayFoo();
298         dest = Translate.h2aTranslate(source);
299         assertThat(source.a.length, is(dest.a.length));
300     }
301 
302     @Test
ArrayFooEnum()303     public void ArrayFooEnum() {
304         hidl2aidl.test.ArrayFoo dest;
305         hidl2aidl.test.V1_2.ArrayFoo source = new hidl2aidl.test.V1_2.ArrayFoo();
306         source.c[0] = hidl2aidl.test.Value.A;
307         source.c[1] = hidl2aidl.test.Value.B;
308         dest = Translate.h2aTranslate(source);
309         assertThat(source.c[0], is(dest.c[0]));
310         assertThat(source.c[1], is(dest.c[1]));
311     }
312 
313     @Test
ArrayFooString()314     public void ArrayFooString() {
315         hidl2aidl.test.ArrayFoo dest;
316         hidl2aidl.test.V1_2.ArrayFoo source = new hidl2aidl.test.V1_2.ArrayFoo();
317         source.d[0] = "hello";
318         source.d[1] = "world";
319         dest = Translate.h2aTranslate(source);
320         assertThat(source.d[0], is(dest.d[0]));
321         assertThat(source.d[1], is(dest.d[1]));
322     }
323 
324     @Test
VectorFoo()325     public void VectorFoo() {
326         hidl2aidl.test.VectorFoo dest;
327         hidl2aidl.test.V1_2.VectorFoo source = new hidl2aidl.test.V1_2.VectorFoo();
328         source.a.add((byte) 42);
329         source.a.add((byte) 8);
330         dest = Translate.h2aTranslate(source);
331         assertThat(source.a.get(0), is(dest.a[0]));
332         assertThat(source.a.get(1), is(dest.a[1]));
333     }
334 
335     @Test
VectorFooEmpty()336     public void VectorFooEmpty() {
337         hidl2aidl.test.VectorFoo dest;
338         hidl2aidl.test.V1_2.VectorFoo source = new hidl2aidl.test.V1_2.VectorFoo();
339         dest = Translate.h2aTranslate(source);
340         assertThat(source.a.size(), is(dest.a.length));
341     }
342 
343     @Test
VectorFooUnsigned()344     public void VectorFooUnsigned() {
345         hidl2aidl.test.VectorFoo dest;
346         hidl2aidl.test.V1_2.VectorFoo source = new hidl2aidl.test.V1_2.VectorFoo();
347         source.b.add(12);
348         source.b.add(0xf0000000);
349         try {
350             dest = Translate.h2aTranslate(source);
351             fail("Expected an exception to be thrown for out of bounds unsigned to signed translation");
352         } catch (RuntimeException e) {
353             String message = e.getMessage();
354             assertThat(
355                     "Unsafe conversion between signed and unsigned scalars for field: in.b.get(i)",
356                     is(message));
357         }
358     }
359 
360     @Test
VectorFooEnum()361     public void VectorFooEnum() {
362         hidl2aidl.test.VectorFoo dest;
363         hidl2aidl.test.V1_2.VectorFoo source = new hidl2aidl.test.V1_2.VectorFoo();
364         source.c.add(hidl2aidl.test.Value.A);
365         source.c.add(hidl2aidl.test.Value.B);
366         dest = Translate.h2aTranslate(source);
367         assertThat(source.c.get(0), is(dest.c[0]));
368         assertThat(source.c.get(1), is(dest.c[1]));
369     }
370 
371     @Test
VectorFooString()372     public void VectorFooString() {
373         hidl2aidl.test.VectorFoo dest;
374         hidl2aidl.test.V1_2.VectorFoo source = new hidl2aidl.test.V1_2.VectorFoo();
375         source.d.add("hello");
376         source.d.add("world");
377         dest = Translate.h2aTranslate(source);
378         assertThat(source.d.get(0), is(dest.d[0]));
379         assertThat(source.d.get(1), is(dest.d[1]));
380     }
381 
382     @Test
ExtensionArrayFoo()383     public void ExtensionArrayFoo() {
384         hidl2aidl.test.extension.ArrayFoo dest;
385         hidl2aidl.test.extension.V1_2.ArrayFoo source =
386                 new hidl2aidl.test.extension.V1_2.ArrayFoo();
387         source.e[0] = 12;
388         source.e[1] = 2;
389         dest = hidl2aidl.test.extension.Translate.h2aTranslate(source);
390         assertThat(source.e[0], is(dest.e[0]));
391         assertThat(source.e[1], is(dest.e[1]));
392     }
393 }
394