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