• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace mojo {
18 namespace android {
19 
RegisterValidationTestUtil(JNIEnv * env)20 bool RegisterValidationTestUtil(JNIEnv* env) {
21   return RegisterNativesImpl(env);
22 }
23 
ParseData(JNIEnv * env,const JavaParamRef<jclass> & jcaller,const JavaParamRef<jstring> & data_as_string)24 ScopedJavaLocalRef<jobject> ParseData(
25     JNIEnv* env,
26     const JavaParamRef<jclass>& jcaller,
27     const JavaParamRef<jstring>& data_as_string) {
28   std::string input =
29       base::android::ConvertJavaStringToUTF8(env, data_as_string);
30   std::vector<uint8_t> data;
31   size_t num_handles;
32   std::string error_message;
33   if (!test::ParseValidationTestInput(
34           input, &data, &num_handles, &error_message)) {
35     ScopedJavaLocalRef<jstring> j_error_message =
36         base::android::ConvertUTF8ToJavaString(env, error_message);
37     return Java_ValidationTestUtil_buildData(env, NULL, 0,
38                                              j_error_message.obj());
39   }
40   void* data_ptr = &data[0];
41   if (!data_ptr) {
42     DCHECK(!data.size());
43     data_ptr = &data;
44   }
45   jobject byte_buffer =
46       env->NewDirectByteBuffer(data_ptr, data.size());
47   return Java_ValidationTestUtil_buildData(env, byte_buffer, num_handles, NULL);
48 }
49 
50 }  // namespace android
51 }  // namespace mojo
52