aboutsummaryrefslogtreecommitdiff
path: root/mojo/android/javatests/src/org/chromium/mojo/bindings/SerializationTest.java
blob: 2c17e3a19464f89da94a23af677131deed5a8a84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.mojo.bindings;

import android.support.test.filters.SmallTest;

import junit.framework.TestCase;

import org.chromium.mojo.HandleMock;
import org.chromium.mojo.bindings.test.mojom.mojo.Struct1;
import org.chromium.mojo.bindings.test.mojom.mojo.Struct2;
import org.chromium.mojo.bindings.test.mojom.mojo.Struct3;
import org.chromium.mojo.bindings.test.mojom.mojo.Struct4;
import org.chromium.mojo.bindings.test.mojom.mojo.Struct5;
import org.chromium.mojo.bindings.test.mojom.mojo.Struct6;
import org.chromium.mojo.bindings.test.mojom.mojo.StructOfNullables;

import java.nio.ByteBuffer;

/**
 * Tests for the serialization logic of the generated structs, using structs defined in
 * mojo/public/interfaces/bindings/tests/serialization_test_structs.mojom .
 */
public class SerializationTest extends TestCase {

    private static void assertThrowsSerializationException(Struct struct) {
        try {
            struct.serialize(null);
            fail("Serialization of invalid struct should have thrown an exception.");
        } catch (SerializationException ex) {
            // Expected.
        }
    }

    /**
     * Verifies that serializing a struct with an invalid handle of a non-nullable type throws an
     * exception.
     */
    @SmallTest
    public void testHandle() {
        Struct2 struct = new Struct2();
        assertFalse(struct.hdl.isValid());
        assertThrowsSerializationException(struct);

        // Make the struct valid and verify that it serializes without an exception.
        struct.hdl = new HandleMock();
        struct.serialize(null);
    }

    /**
     * Verifies that serializing a struct with a null struct pointer throws an exception.
     */
    @SmallTest
    public void testStructPointer() {
        Struct3 struct = new Struct3();
        assertNull(struct.struct1);
        assertThrowsSerializationException(struct);

        // Make the struct valid and verify that it serializes without an exception.
        struct.struct1 = new Struct1();
        struct.serialize(null);
    }

    /**
     * Verifies that serializing a struct with an array of structs throws an exception when the
     * struct is invalid.
     */
    @SmallTest
    public void testStructArray() {
        Struct4 struct = new Struct4();
        assertNull(struct.data);
        assertThrowsSerializationException(struct);

        // Create the (1-element) array but have the element null.
        struct.data = new Struct1[1];
        assertThrowsSerializationException(struct);

        // Create the array element, struct should serialize now.
        struct.data[0] = new Struct1();
        struct.serialize(null);
    }

    /**
     * Verifies that serializing a struct with a fixed-size array of incorrect length throws an
     * exception.
     */
    @SmallTest
    public void testFixedSizeArray() {
        Struct5 struct = new Struct5();
        assertNull(struct.pair);
        assertThrowsSerializationException(struct);

        // Create the (1-element) array, 2-element array is required.
        struct.pair = new Struct1[1];
        struct.pair[0] = new Struct1();
        assertThrowsSerializationException(struct);

        // Create the array of a correct size, struct should serialize now.
        struct.pair = new Struct1[2];
        struct.pair[0] = new Struct1();
        struct.pair[1] = new Struct1();
        struct.serialize(null);
    }

    /**
     * Verifies that serializing a struct with a null string throws an exception.
     */
    @SmallTest
    public void testString() {
        Struct6 struct = new Struct6();
        assertNull(struct.str);
        assertThrowsSerializationException(struct);

        // Make the struct valid and verify that it serializes without an exception.
        struct.str = "";
        struct.serialize(null);
    }

    /**
     * Verifies that a struct with an invalid nullable handle, null nullable struct pointer and null
     * nullable string serializes without an exception.
     */
    @SmallTest
    public void testNullableFields() {
        StructOfNullables struct = new StructOfNullables();
        assertFalse(struct.hdl.isValid());
        assertNull(struct.struct1);
        assertNull(struct.str);
        struct.serialize(null);
    }

    /**
     * Verifies that a struct can be serialized to and deserialized from a ByteBuffer.
     */
    @SmallTest
    public void testByteBufferSerialization() {
        Struct1 input = new Struct1();
        input.i = 0x7F;

        ByteBuffer buf = input.serialize();

        byte[] expected_raw_bytes = {16, 0, 0, 0, 0, 0, 0, 0, 0x7F, 0, 0, 0, 0, 0, 0, 0};
        ByteBuffer expected_buf = ByteBuffer.wrap(expected_raw_bytes);
        assertEquals(expected_buf, buf);

        Struct1 output = Struct1.deserialize(buf);
        assertEquals(0x7F, output.i);
    }

    /**
     * Verifies that a struct with handles cannot be serialized to a ByteBuffer.
     */
    @SmallTest
    public void testByteBufferSerializationWithHandles() {
        StructOfNullables struct = new StructOfNullables();
        assertFalse(struct.hdl.isValid());
        assertNull(struct.struct1);
        assertNull(struct.str);

        // It is okay to serialize invalid handles.
        struct.serialize();

        struct.hdl = new HandleMock();

        try {
            struct.serialize();
            fail("Serializing a struct with handles to a ByteBuffer should have thrown an "
                    + "exception.");
        } catch (UnsupportedOperationException ex) {
            // Expected.
        }
    }
}