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 org.chromium.base.annotations.CalledByNative; 8 import org.chromium.base.annotations.JNINamespace; 9 10 import java.nio.ByteBuffer; 11 import 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") 19 public class ValidationTestUtil { 20 /** 21 * Content of a '.data' file. 22 */ 23 public static class Data { 24 private final ByteBuffer mData; 25 private final int mHandlesCount; 26 private final String mErrorMessage; 27 getData()28 public ByteBuffer getData() { 29 return mData; 30 } 31 getHandlesCount()32 public int getHandlesCount() { 33 return mHandlesCount; 34 } 35 getErrorMessage()36 public String getErrorMessage() { 37 return mErrorMessage; 38 } 39 Data(ByteBuffer data, int handlesCount, String errorMessage)40 private Data(ByteBuffer data, int handlesCount, String errorMessage) { 41 this.mData = data; 42 this.mHandlesCount = handlesCount; 43 this.mErrorMessage = errorMessage; 44 } 45 } 46 47 /** 48 * Parse a '.data' file. 49 */ parseData(String dataAsString)50 public static Data parseData(String dataAsString) { 51 return nativeParseData(dataAsString); 52 } 53 nativeParseData(String dataAsString)54 private static native Data nativeParseData(String dataAsString); 55 56 @CalledByNative buildData(ByteBuffer data, int handlesCount, String errorMessage)57 private static Data buildData(ByteBuffer data, int handlesCount, String errorMessage) { 58 ByteBuffer copiedData = null; 59 if (data != null) { 60 copiedData = ByteBuffer.allocateDirect(data.limit()); 61 copiedData.order(ByteOrder.LITTLE_ENDIAN); 62 copiedData.put(data); 63 copiedData.flip(); 64 } 65 return new Data(copiedData, handlesCount, errorMessage); 66 } 67 } 68