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 org.chromium.base.CalledByNative;
8import org.chromium.base.JNINamespace;
9
10import java.nio.ByteBuffer;
11import java.nio.ByteOrder;
12
13/**
14 * Utility class for testing message validation. The file format used to describe a message is
15 * described in The format is described in
16 * mojo/public/cpp/bindings/tests/validation_test_input_parser.h
17 */
18@JNINamespace("mojo::android")
19public class ValidationTestUtil {
20
21    /**
22     * Content of a '.data' file.
23     */
24    public static class Data {
25        private final ByteBuffer mData;
26        private final int mHandlesCount;
27        private final String mErrorMessage;
28
29        public ByteBuffer getData() {
30            return mData;
31        }
32
33        public int getHandlesCount() {
34            return mHandlesCount;
35        }
36
37        public String getErrorMessage() {
38            return mErrorMessage;
39        }
40
41        private Data(ByteBuffer data, int handlesCount, String errorMessage) {
42            this.mData = data;
43            this.mHandlesCount = handlesCount;
44            this.mErrorMessage = errorMessage;
45        }
46    }
47
48    /**
49     * Parse a '.data' file.
50     */
51    public static Data parseData(String dataAsString) {
52        return nativeParseData(dataAsString);
53    }
54
55    private static native Data nativeParseData(String dataAsString);
56
57    @CalledByNative
58    private static Data buildData(ByteBuffer data, int handlesCount, String errorMessage) {
59        ByteBuffer copiedData = null;
60        if (data != null) {
61            copiedData = ByteBuffer.allocateDirect(data.limit());
62            copiedData.order(ByteOrder.nativeOrder());
63            copiedData.put(data);
64            copiedData.flip();
65        }
66        return new Data(copiedData, handlesCount, errorMessage);
67    }
68}
69