Home | History | Annotate | Download | only in tests
      1 // Copyright (c) 2013 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 // A simple library that provides JNI_OnLoad() and JNI_OnUnload() hooks.
      6 // Used by test_java_vm.cpp
      7 
      8 #include <jni.h>
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 
     12 #define VARNAME "TEST_VAR"
     13 
     14 extern "C" int JNI_OnLoad(JavaVM* vm, void* reserved) {
     15   printf("%s: Entering\n", __FUNCTION__);
     16   const char* env = getenv(VARNAME);
     17   if (!env || strcmp(env, "INIT")) {
     18     fprintf(stderr,
     19             "%s: Env variable %s has invalid value: %s (expected INIT)\n",
     20             __FUNCTION__,
     21             VARNAME,
     22             env);
     23     exit(1);
     24   }
     25   setenv(VARNAME, "LOADED", 1);
     26   printf("%s: Exiting\n", __FUNCTION__);
     27   return JNI_VERSION_1_4;
     28 }
     29 
     30 extern "C" void JNI_OnUnload(JavaVM* vm, void* reserved) {
     31   printf("%s: Entering\n", __FUNCTION__);
     32   const char* env = getenv(VARNAME);
     33   if (!env || strcmp(env, "LOADED")) {
     34     fprintf(stderr,
     35             "%s: Env variable %s has invalid value: %s (expected LOADED)\n",
     36             __FUNCTION__,
     37             VARNAME,
     38             env);
     39     exit(1);
     40   }
     41   setenv(VARNAME, "UNLOADED", 1);
     42   printf("%s: Exiting\n", __FUNCTION__);
     43 }
     44