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