1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.mojo.bindings;
6 
7 import androidx.test.filters.SmallTest;
8 
9 import org.junit.Assert;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 
13 import org.chromium.base.test.BaseJUnit4ClassRunner;
14 import org.chromium.mojo.HandleMock;
15 import org.chromium.mojo.bindings.test.mojom.mojo.Struct1;
16 import org.chromium.mojo.bindings.test.mojom.mojo.Struct2;
17 import org.chromium.mojo.bindings.test.mojom.mojo.Struct3;
18 import org.chromium.mojo.bindings.test.mojom.mojo.Struct4;
19 import org.chromium.mojo.bindings.test.mojom.mojo.Struct5;
20 import org.chromium.mojo.bindings.test.mojom.mojo.Struct6;
21 import org.chromium.mojo.bindings.test.mojom.mojo.StructOfNullables;
22 
23 import java.nio.ByteBuffer;
24 
25 /**
26  * Tests for the serialization logic of the generated structs, using structs defined in
27  * mojo/public/interfaces/bindings/tests/serialization_test_structs.mojom .
28  */
29 @RunWith(BaseJUnit4ClassRunner.class)
30 public class SerializationTest {
assertThrowsSerializationException(Struct struct)31     private static void assertThrowsSerializationException(Struct struct) {
32         try {
33             struct.serialize(null);
34             Assert.fail("Serialization of invalid struct should have thrown an exception.");
35         } catch (SerializationException ex) {
36             // Expected.
37         }
38     }
39 
40     /**
41      * Verifies that serializing a struct with an invalid handle of a non-nullable type throws an
42      * exception.
43      */
44     @Test
45     @SmallTest
testHandle()46     public void testHandle() {
47         Struct2 struct = new Struct2();
48         Assert.assertFalse(struct.hdl.isValid());
49         assertThrowsSerializationException(struct);
50 
51         // Make the struct valid and verify that it serializes without an exception.
52         struct.hdl = new HandleMock();
53         struct.serialize(null);
54     }
55 
56     /**
57      * Verifies that serializing a struct with a null struct pointer throws an exception.
58      */
59     @Test
60     @SmallTest
testStructPointer()61     public void testStructPointer() {
62         Struct3 struct = new Struct3();
63         Assert.assertNull(struct.struct1);
64         assertThrowsSerializationException(struct);
65 
66         // Make the struct valid and verify that it serializes without an exception.
67         struct.struct1 = new Struct1();
68         struct.serialize(null);
69     }
70 
71     /**
72      * Verifies that serializing a struct with an array of structs throws an exception when the
73      * struct is invalid.
74      */
75     @Test
76     @SmallTest
testStructArray()77     public void testStructArray() {
78         Struct4 struct = new Struct4();
79         Assert.assertNull(struct.data);
80         assertThrowsSerializationException(struct);
81 
82         // Create the (1-element) array but have the element null.
83         struct.data = new Struct1[1];
84         assertThrowsSerializationException(struct);
85 
86         // Create the array element, struct should serialize now.
87         struct.data[0] = new Struct1();
88         struct.serialize(null);
89     }
90 
91     /**
92      * Verifies that serializing a struct with a fixed-size array of incorrect length throws an
93      * exception.
94      */
95     @Test
96     @SmallTest
testFixedSizeArray()97     public void testFixedSizeArray() {
98         Struct5 struct = new Struct5();
99         Assert.assertNull(struct.pair);
100         assertThrowsSerializationException(struct);
101 
102         // Create the (1-element) array, 2-element array is required.
103         struct.pair = new Struct1[1];
104         struct.pair[0] = new Struct1();
105         assertThrowsSerializationException(struct);
106 
107         // Create the array of a correct size, struct should serialize now.
108         struct.pair = new Struct1[2];
109         struct.pair[0] = new Struct1();
110         struct.pair[1] = new Struct1();
111         struct.serialize(null);
112     }
113 
114     /**
115      * Verifies that serializing a struct with a null string throws an exception.
116      */
117     @Test
118     @SmallTest
testString()119     public void testString() {
120         Struct6 struct = new Struct6();
121         Assert.assertNull(struct.str);
122         assertThrowsSerializationException(struct);
123 
124         // Make the struct valid and verify that it serializes without an exception.
125         struct.str = "";
126         struct.serialize(null);
127     }
128 
129     /**
130      * Verifies that a struct with an invalid nullable handle, null nullable struct pointer and null
131      * nullable string serializes without an exception.
132      */
133     @Test
134     @SmallTest
testNullableFields()135     public void testNullableFields() {
136         StructOfNullables struct = new StructOfNullables();
137         Assert.assertFalse(struct.hdl.isValid());
138         Assert.assertNull(struct.struct1);
139         Assert.assertNull(struct.str);
140         struct.serialize(null);
141     }
142 
143     /**
144      * Verifies that a struct can be serialized to and deserialized from a ByteBuffer.
145      */
146     @Test
147     @SmallTest
testByteBufferSerialization()148     public void testByteBufferSerialization() {
149         Struct1 input = new Struct1();
150         input.i = 0x7F;
151 
152         ByteBuffer buf = input.serialize();
153 
154         byte[] expected_raw_bytes = {16, 0, 0, 0, 0, 0, 0, 0, 0x7F, 0, 0, 0, 0, 0, 0, 0};
155         ByteBuffer expected_buf = ByteBuffer.wrap(expected_raw_bytes);
156         Assert.assertEquals(expected_buf, buf);
157 
158         Struct1 output = Struct1.deserialize(buf);
159         Assert.assertEquals(0x7F, output.i);
160     }
161 
162     /**
163      * Verifies that a struct with handles cannot be serialized to a ByteBuffer.
164      */
165     @Test
166     @SmallTest
testByteBufferSerializationWithHandles()167     public void testByteBufferSerializationWithHandles() {
168         StructOfNullables struct = new StructOfNullables();
169         Assert.assertFalse(struct.hdl.isValid());
170         Assert.assertNull(struct.struct1);
171         Assert.assertNull(struct.str);
172 
173         // It is okay to serialize invalid handles.
174         struct.serialize();
175 
176         struct.hdl = new HandleMock();
177 
178         try {
179             struct.serialize();
180             Assert.fail("Serializing a struct with handles to a ByteBuffer should have thrown an "
181                     + "exception.");
182         } catch (UnsupportedOperationException ex) {
183             // Expected.
184         }
185     }
186 }
187