Commit 6a55ddd7 authored by Benjamin Lerman's avatar Benjamin Lerman

mojo: Utility class to parse .data file.

This add an utility class to parse .data file. It will be used to run
the validation tests in java.

R=ppi@chromium.org, yzshen@chromium.org

Review URL: https://codereview.chromium.org/508033004

Cr-Commit-Position: refs/heads/master@{#292588}
parent 6cf10194
......@@ -53,6 +53,7 @@ if (is_android) {
generate_jni("jni_headers") {
sources = [
"android/javatests/src/org/chromium/mojo/MojoTestCase.java",
"android/javatests/src/org/chromium/mojo/bindings/ValidationTestUtil.java",
"android/system/src/org/chromium/mojo/system/impl/CoreImpl.java",
"services/native_viewport/android/src/org/chromium/mojo/PlatformViewportAndroid.java",
]
......
......@@ -7,6 +7,7 @@
#include "base/android/jni_registrar.h"
#include "base/android/library_loader/library_loader_hooks.h"
#include "mojo/android/javatests/mojo_test_case.h"
#include "mojo/android/javatests/validation_test_util.h"
#include "mojo/android/system/core_impl.h"
namespace {
......@@ -14,6 +15,7 @@ namespace {
base::android::RegistrationMethod kMojoRegisteredMethods[] = {
{ "CoreImpl", mojo::android::RegisterCoreImpl },
{ "MojoTestCase", mojo::android::RegisterMojoTestCase },
{ "ValidationTestUtil", mojo::android::RegisterValidationTestUtil },
};
bool RegisterMojoJni(JNIEnv* env) {
......
// 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 org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* Utility class for testing message validation. The file format used to describe a message is
* described in The format is described in
* mojo/public/cpp/bindings/tests/validation_test_input_parser.h
*/
@JNINamespace("mojo::android")
public class ValidationTestUtil {
/**
* Content of a '.data' file.
*/
public static class Data {
private final ByteBuffer mData;
private final int mHandlesCount;
private final String mErrorMessage;
public ByteBuffer getData() {
return mData;
}
public int getHandlesCount() {
return mHandlesCount;
}
public String getErrorMessage() {
return mErrorMessage;
}
private Data(ByteBuffer data, int handlesCount, String errorMessage) {
this.mData = data;
this.mHandlesCount = handlesCount;
this.mErrorMessage = errorMessage;
}
}
/**
* Parse a '.data' file.
*/
public static Data parseData(String dataAsString) {
return nativeParseData(dataAsString);
}
private static native Data nativeParseData(String dataAsString);
@CalledByNative
private static Data buildData(ByteBuffer data, int handlesCount, String errorMessage) {
ByteBuffer copiedData = null;
if (data != null) {
copiedData = ByteBuffer.allocateDirect(data.limit());
copiedData.order(ByteOrder.nativeOrder());
copiedData.put(data);
copiedData.flip();
}
return new Data(copiedData, handlesCount, errorMessage);
}
}
// 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.test.suitebuilder.annotation.SmallTest;
import org.chromium.mojo.MojoTestCase;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* Testing {@link ValidationTestUtil}.
*/
public class ValidationTestUtilTest extends MojoTestCase {
/**
* Check that the input parser is correct on a given input.
*/
public static void checkInputParser(
String input, boolean isInputValid, ByteBuffer expectedData, int expectedHandlesCount) {
ValidationTestUtil.Data data = ValidationTestUtil.parseData(input);
if (isInputValid) {
assertNull(data.getErrorMessage());
assertEquals(expectedData, data.getData());
assertEquals(expectedHandlesCount, data.getHandlesCount());
} else {
assertNotNull(data.getErrorMessage());
assertNull(data.getData());
}
}
/**
* Testing {@link ValidationTestUtil#parseData(String)}.
*/
@SmallTest
public void testCorrectMessageParsing() {
{
// Test empty input.
String input = "";
ByteBuffer expected = ByteBuffer.allocateDirect(0);
expected.order(ByteOrder.nativeOrder());
checkInputParser(input, true, expected, 0);
}
{
// Test input that only consists of comments and whitespaces.
String input = " \t // hello world \n\r \t// the answer is 42 ";
ByteBuffer expected = ByteBuffer.allocateDirect(0);
expected.order(ByteOrder.nativeOrder());
checkInputParser(input, true, expected, 0);
}
{
String input = "[u1]0x10// hello world !! \n\r \t [u2]65535 \n" +
"[u4]65536 [u8]0xFFFFFFFFFFFFFFFF 0 0Xff";
ByteBuffer expected = ByteBuffer.allocateDirect(17);
expected.order(ByteOrder.nativeOrder());
expected.put((byte) 0x10);
expected.putShort((short) 65535);
expected.putInt(65536);
expected.putLong(-1);
expected.put((byte) 0);
expected.put((byte) 0xff);
expected.flip();
checkInputParser(input, true, expected, 0);
}
{
String input = "[s8]-0x800 [s1]-128\t[s2]+0 [s4]-40";
ByteBuffer expected = ByteBuffer.allocateDirect(15);
expected.order(ByteOrder.nativeOrder());
expected.putLong(-0x800);
expected.put((byte) -128);
expected.putShort((short) 0);
expected.putInt(-40);
expected.flip();
checkInputParser(input, true, expected, 0);
}
{
String input = "[b]00001011 [b]10000000 // hello world\r [b]00000000";
ByteBuffer expected = ByteBuffer.allocateDirect(3);
expected.order(ByteOrder.nativeOrder());
expected.put((byte) 11);
expected.put((byte) 128);
expected.put((byte) 0);
expected.flip();
checkInputParser(input, true, expected, 0);
}
{
String input = "[f]+.3e9 [d]-10.03";
ByteBuffer expected = ByteBuffer.allocateDirect(12);
expected.order(ByteOrder.nativeOrder());
expected.putFloat(+.3e9f);
expected.putDouble(-10.03);
expected.flip();
checkInputParser(input, true, expected, 0);
}
{
String input = "[dist4]foo 0 [dist8]bar 0 [anchr]foo [anchr]bar";
ByteBuffer expected = ByteBuffer.allocateDirect(14);
expected.order(ByteOrder.nativeOrder());
expected.putInt(14);
expected.put((byte) 0);
expected.putLong(9);
expected.put((byte) 0);
expected.flip();
checkInputParser(input, true, expected, 0);
}
{
String input = "// This message has handles! \n[handles]50 [u8]2";
ByteBuffer expected = ByteBuffer.allocateDirect(8);
expected.order(ByteOrder.nativeOrder());
expected.putLong(2);
expected.flip();
checkInputParser(input, true, expected, 50);
}
// Test some failure cases.
{
String error_inputs[] = {
"/ hello world",
"[u1]x",
"[u2]-1000",
"[u1]0x100",
"[s2]-0x8001",
"[b]1",
"[b]1111111k",
"[dist4]unmatched",
"[anchr]hello [dist8]hello",
"[dist4]a [dist4]a [anchr]a",
"[dist4]a [anchr]a [dist4]a [anchr]a",
"0 [handles]50"
};
for (String input : error_inputs) {
ByteBuffer expected = ByteBuffer.allocateDirect(0);
expected.order(ByteOrder.nativeOrder());
checkInputParser(input, false, expected, 0);
}
}
}
}
// 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.
#include "mojo/android/javatests/validation_test_util.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/test/test_support_android.h"
#include "jni/ValidationTestUtil_jni.h"
#include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h"
namespace mojo {
namespace android {
bool RegisterValidationTestUtil(JNIEnv* env) {
return RegisterNativesImpl(env);
}
jobject ParseData(JNIEnv* env, jclass jcaller, jstring data_as_string) {
std::string input =
base::android::ConvertJavaStringToUTF8(env, data_as_string);
std::vector<uint8_t> data;
size_t num_handles;
std::string error_message;
if (!test::ParseValidationTestInput(
input, &data, &num_handles, &error_message)) {
ScopedJavaLocalRef<jstring> j_error_message =
base::android::ConvertUTF8ToJavaString(env, error_message);
return Java_ValidationTestUtil_buildData(
env, NULL, 0, j_error_message.obj()).Release();
}
jobject byte_buffer =
env->NewDirectByteBuffer(&data[0], data.size());
return Java_ValidationTestUtil_buildData(env, byte_buffer, num_handles, NULL)
.Release();
}
} // namespace android
} // namespace mojo
// 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.
#ifndef MOJO_ANDROID_JAVATESTS_VALIDATION_TEST_UTIL_H_
#define MOJO_ANDROID_JAVATESTS_VALIDATION_TEST_UTIL_H_
#include <jni.h>
#include "base/android/jni_android.h"
namespace mojo {
namespace android {
JNI_EXPORT bool RegisterValidationTestUtil(JNIEnv* env);
} // namespace android
} // namespace mojo
#endif // MOJO_SYSTEM_ANDROID_JAVATESTS_VALIDATION_TEST_UTIL_H_
......@@ -509,6 +509,7 @@
],
'sources': [
'android/javatests/src/org/chromium/mojo/MojoTestCase.java',
'android/javatests/src/org/chromium/mojo/bindings/ValidationTestUtil.java',
'android/system/src/org/chromium/mojo/system/impl/CoreImpl.java',
'services/native_viewport/android/src/org/chromium/mojo/PlatformViewportAndroid.java',
'shell/android/apk/src/org/chromium/mojo_shell_apk/MojoMain.java',
......@@ -563,6 +564,7 @@
'../base/base.gyp:test_support_base',
'libmojo_system_java',
'mojo_jni_headers',
'mojo_public_bindings_test_utils',
],
'defines': [
'UNIT_TEST' # As exported from testing/gtest.gyp:gtest.
......@@ -571,6 +573,8 @@
'android/javatests/mojo_test_case.cc',
'android/javatests/mojo_test_case.h',
'android/javatests/init_library.cc',
'android/javatests/validation_test_util.cc',
'android/javatests/validation_test_util.h',
],
},
{
......
......@@ -50,6 +50,18 @@
'public/cpp/test_support/test_utils.h',
],
},
{
# GN version: //mojo/public/cpp/bindings/tests:mojo_public_bindings_test_utils
'target_name': 'mojo_public_bindings_test_utils',
'type': 'static_library',
'dependencies': [
'../base/base.gyp:base',
],
'sources': [
'public/cpp/bindings/tests/validation_test_input_parser.cc',
'public/cpp/bindings/tests/validation_test_input_parser.h',
],
},
# TODO(vtl): Reorganize the mojo_public_*_unittests.
{
# GN version: //mojo/public/cpp/bindings/tests:mojo_public_bindings_unittests
......@@ -61,6 +73,7 @@
'mojo_environment_standalone',
'mojo_public_test_utils',
'mojo_run_all_unittests',
'mojo_public_bindings_test_utils',
'mojo_public_test_interfaces',
'mojo_utility',
],
......@@ -78,8 +91,6 @@
'public/cpp/bindings/tests/string_unittest.cc',
'public/cpp/bindings/tests/struct_unittest.cc',
'public/cpp/bindings/tests/type_conversion_unittest.cc',
'public/cpp/bindings/tests/validation_test_input_parser.cc',
'public/cpp/bindings/tests/validation_test_input_parser.h',
'public/cpp/bindings/tests/validation_unittest.cc',
],
},
......
......@@ -12,6 +12,7 @@ test("mojo_public_bindings_unittests") {
"//mojo/public/cpp/utility",
"//mojo/public/interfaces/bindings/tests:test_interfaces",
"//testing/gtest",
":mojo_public_bindings_test_utils",
]
sources = [
......@@ -28,8 +29,13 @@ test("mojo_public_bindings_unittests") {
"string_unittest.cc",
"struct_unittest.cc",
"type_conversion_unittest.cc",
"validation_unittest.cc",
]
}
source_set("mojo_public_bindings_test_utils") {
sources = [
"validation_test_input_parser.cc",
"validation_test_input_parser.h",
"validation_unittest.cc",
]
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment