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 #include "mojo/android/javatests/validation_test_util.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include "base/android/jni_android.h"
11 #include "base/android/jni_string.h"
12 #include "base/android/scoped_java_ref.h"
13 #include "base/test/test_support_android.h"
14 #include "jni/ValidationTestUtil_jni.h"
15 #include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h"
16
17 using base::android::JavaParamRef;
18 using base::android::ScopedJavaLocalRef;
19
20 namespace mojo {
21 namespace android {
22
RegisterValidationTestUtil(JNIEnv * env)23 bool RegisterValidationTestUtil(JNIEnv* env) {
24 return RegisterNativesImpl(env);
25 }
26
ParseData(JNIEnv * env,const JavaParamRef<jclass> & jcaller,const JavaParamRef<jstring> & data_as_string)27 ScopedJavaLocalRef<jobject> ParseData(
28 JNIEnv* env,
29 const JavaParamRef<jclass>& jcaller,
30 const JavaParamRef<jstring>& data_as_string) {
31 std::string input =
32 base::android::ConvertJavaStringToUTF8(env, data_as_string);
33 std::vector<uint8_t> data;
34 size_t num_handles;
35 std::string error_message;
36 if (!test::ParseValidationTestInput(
37 input, &data, &num_handles, &error_message)) {
38 ScopedJavaLocalRef<jstring> j_error_message =
39 base::android::ConvertUTF8ToJavaString(env, error_message);
40 return Java_ValidationTestUtil_buildData(env, nullptr, 0, j_error_message);
41 }
42 void* data_ptr = &data[0];
43 if (!data_ptr) {
44 DCHECK(!data.size());
45 data_ptr = &data;
46 }
47 jobject byte_buffer =
48 env->NewDirectByteBuffer(data_ptr, data.size());
49 return Java_ValidationTestUtil_buildData(env, byte_buffer, num_handles,
50 nullptr);
51 }
52
53 } // namespace android
54 } // namespace mojo
55