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.test.suitebuilder.annotation.SmallTest;
8
9import org.chromium.mojo.MojoTestCase;
10
11import java.nio.ByteBuffer;
12import java.nio.ByteOrder;
13
14/**
15 * Testing {@link ValidationTestUtil}.
16 */
17public class ValidationTestUtilTest extends MojoTestCase {
18
19    /**
20     * Check that the input parser is correct on a given input.
21     */
22    public static void checkInputParser(
23            String input, boolean isInputValid, ByteBuffer expectedData, int expectedHandlesCount) {
24        ValidationTestUtil.Data data = ValidationTestUtil.parseData(input);
25        if (isInputValid) {
26            assertNull(data.getErrorMessage());
27            assertEquals(expectedData, data.getData());
28            assertEquals(expectedHandlesCount, data.getHandlesCount());
29        } else {
30            assertNotNull(data.getErrorMessage());
31            assertNull(data.getData());
32        }
33    }
34
35    /**
36     * Testing {@link ValidationTestUtil#parseData(String)}.
37     */
38    @SmallTest
39    public void testCorrectMessageParsing() {
40        {
41            // Test empty input.
42            String input = "";
43            ByteBuffer expected = ByteBuffer.allocateDirect(0);
44            expected.order(ByteOrder.nativeOrder());
45
46            checkInputParser(input, true, expected, 0);
47        }
48        {
49            // Test input that only consists of comments and whitespaces.
50            String input = "    \t  // hello world \n\r \t// the answer is 42   ";
51            ByteBuffer expected = ByteBuffer.allocateDirect(0);
52            expected.order(ByteOrder.nativeOrder());
53
54            checkInputParser(input, true, expected, 0);
55        }
56        {
57            String input = "[u1]0x10// hello world !! \n\r  \t [u2]65535 \n" +
58                    "[u4]65536 [u8]0xFFFFFFFFFFFFFFFF 0 0Xff";
59            ByteBuffer expected = ByteBuffer.allocateDirect(17);
60            expected.order(ByteOrder.nativeOrder());
61            expected.put((byte) 0x10);
62            expected.putShort((short) 65535);
63            expected.putInt(65536);
64            expected.putLong(-1);
65            expected.put((byte) 0);
66            expected.put((byte) 0xff);
67            expected.flip();
68
69            checkInputParser(input, true, expected, 0);
70        }
71        {
72            String input = "[s8]-0x800 [s1]-128\t[s2]+0 [s4]-40";
73            ByteBuffer expected = ByteBuffer.allocateDirect(15);
74            expected.order(ByteOrder.nativeOrder());
75            expected.putLong(-0x800);
76            expected.put((byte) -128);
77            expected.putShort((short) 0);
78            expected.putInt(-40);
79            expected.flip();
80
81            checkInputParser(input, true, expected, 0);
82        }
83        {
84            String input = "[b]00001011 [b]10000000  // hello world\r [b]00000000";
85            ByteBuffer expected = ByteBuffer.allocateDirect(3);
86            expected.order(ByteOrder.nativeOrder());
87            expected.put((byte) 11);
88            expected.put((byte) 128);
89            expected.put((byte) 0);
90            expected.flip();
91
92            checkInputParser(input, true, expected, 0);
93        }
94        {
95            String input = "[f]+.3e9 [d]-10.03";
96            ByteBuffer expected = ByteBuffer.allocateDirect(12);
97            expected.order(ByteOrder.nativeOrder());
98            expected.putFloat(+.3e9f);
99            expected.putDouble(-10.03);
100            expected.flip();
101
102            checkInputParser(input, true, expected, 0);
103        }
104        {
105            String input = "[dist4]foo 0 [dist8]bar 0 [anchr]foo [anchr]bar";
106            ByteBuffer expected = ByteBuffer.allocateDirect(14);
107            expected.order(ByteOrder.nativeOrder());
108            expected.putInt(14);
109            expected.put((byte) 0);
110            expected.putLong(9);
111            expected.put((byte) 0);
112            expected.flip();
113
114            checkInputParser(input, true, expected, 0);
115        }
116        {
117            String input = "// This message has handles! \n[handles]50 [u8]2";
118            ByteBuffer expected = ByteBuffer.allocateDirect(8);
119            expected.order(ByteOrder.nativeOrder());
120            expected.putLong(2);
121            expected.flip();
122
123            checkInputParser(input, true, expected, 50);
124        }
125
126        // Test some failure cases.
127        {
128            String error_inputs[] = {
129                "/ hello world",
130                "[u1]x",
131                "[u2]-1000",
132                "[u1]0x100",
133                "[s2]-0x8001",
134                "[b]1",
135                "[b]1111111k",
136                "[dist4]unmatched",
137                "[anchr]hello [dist8]hello",
138                "[dist4]a [dist4]a [anchr]a",
139                "[dist4]a [anchr]a [dist4]a [anchr]a",
140                "0 [handles]50"
141            };
142
143            for (String input : error_inputs) {
144                ByteBuffer expected = ByteBuffer.allocateDirect(0);
145                expected.order(ByteOrder.nativeOrder());
146                checkInputParser(input, false, expected, 0);
147            }
148        }
149
150    }
151}
152