/hg/icedtea-web: Clean up NP_Initialize; add more C++ unit tests

adomurad at icedtea.classpath.org adomurad at icedtea.classpath.org
Fri Dec 21 12:21:42 PST 2012


changeset b9733f552c79 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=b9733f552c79
author: Adam Domurad <adomurad at redhat.com>
date: Fri Dec 21 15:21:42 2012 -0500

	Clean up NP_Initialize; add more C++ unit tests


diffstat:

 ChangeLog                                   |   10 ++
 plugin/icedteanp/IcedTeaNPPlugin.cc         |  137 +++++++--------------------
 tests/cpp-unit-tests/IcedTeaNPPluginTest.cc |   92 ++++++++++++++++++
 3 files changed, 141 insertions(+), 98 deletions(-)

diffs (347 lines):

diff -r 01b5bc425cd4 -r b9733f552c79 ChangeLog
--- a/ChangeLog	Fri Dec 21 18:06:53 2012 +0100
+++ b/ChangeLog	Fri Dec 21 15:21:42 2012 -0500
@@ -1,3 +1,13 @@
+2012-12-21  Adam Domurad  <adomurad at redhat.com>
+
+	* plugin/icedteanp/IcedTeaNPPlugin.cc: Remove need for 'goto' in 
+	(NP_Initialize). Check TMPDIR environment variable for possible data 
+	directory. Expose some previously static variables/functions for unit 
+	testing purposes. Reduce need for explicit allocations for strings 
+	'data_directory' and 'appletviewer_executable'.
+	* tests/cpp-unit-tests/IcedTeaNPPluginTest.cc: 
+	Add some basic tests for functions in IcedTeaNPPlugin.cc.
+
 2012-12-21  Jiri Vanek  <jvanek at redhat.com>
 
 	* netx/net/sourceforge/jnlp/runtime/ApplicationInstance.java
diff -r 01b5bc425cd4 -r b9733f552c79 plugin/icedteanp/IcedTeaNPPlugin.cc
--- a/plugin/icedteanp/IcedTeaNPPlugin.cc	Fri Dec 21 18:06:53 2012 +0100
+++ b/plugin/icedteanp/IcedTeaNPPlugin.cc	Fri Dec 21 15:21:42 2012 -0500
@@ -154,10 +154,10 @@
 #endif
 
 // Data directory for plugin.
-static gchar* data_directory = NULL;
+static std::string data_directory;
 
 // Fully-qualified appletviewer executable.
-static gchar* appletviewer_executable = NULL;
+static const char* appletviewer_executable = ICEDTEA_WEB_JRE "/bin/java";
 
 // Applet viewer input channel (needs to be static because it is used in plugin_in_pipe_callback)
 static GIOChannel* in_from_appletviewer = NULL;
@@ -243,7 +243,7 @@
                                                int width, int height,
                                                char* url);
 /* Returns JVM options set in itw-settings */
-static std::vector<std::string*>* get_jvm_args();
+std::vector<std::string*>* get_jvm_args();
 
 // Global instance counter.
 // Mutex to protect plugin_instance_counter.
@@ -469,7 +469,7 @@
 
   // in_pipe_name
   in_pipe_name = g_strdup_printf ("%s/%d-icedteanp-appletviewer-to-plugin",
-                                         data_directory, getpid());
+                                         data_directory.c_str(), getpid());
   if (!in_pipe_name)
     {
       PLUGIN_ERROR ("Failed to create input pipe name.");
@@ -496,7 +496,7 @@
 
   // out_pipe_name
   out_pipe_name = g_strdup_printf ("%s/%d-icedteanp-plugin-to-appletviewer",
-                                         data_directory, getpid());
+                                         data_directory.c_str(), getpid());
 
   if (!out_pipe_name)
     {
@@ -1671,7 +1671,7 @@
 /*
  * Returns JVM options set in itw-settings
  */
-static std::vector<std::string*>*
+std::vector<std::string*>*
 get_jvm_args()
 {
   std::vector < std::string> commands;
@@ -1695,6 +1695,7 @@
   {
     PLUGIN_ERROR("Failed to get JVM arguments set for plugin.");
     output = NULL;
+    return NULL;
   }
 
   tokenOutput = IcedTeaPluginUtilities::strSplit(output, " \n");
@@ -2146,91 +2147,67 @@
   if (initialized)
     return NPERR_NO_ERROR;
 
+  NPError np_error = NPERR_NO_ERROR;
+
   // Make sure the plugin data directory exists, creating it if
   // necessary.
-  data_directory = g_strconcat (P_tmpdir, NULL);
-  if (!data_directory)
+
+  const char* tmpdir_env = getenv("TMPDIR");
+  if (tmpdir_env != NULL && g_file_test (tmpdir_env,
+                    (GFileTest) (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
     {
-      PLUGIN_ERROR ("Failed to create data directory name.");
-      return NPERR_OUT_OF_MEMORY_ERROR;
+      data_directory = tmpdir_env;
     }
-  NPError np_error = NPERR_NO_ERROR;
-  gchar* filename = NULL;
-
-  // If P_tmpdir does not exist, try /tmp directly
-
-  if (!g_file_test (data_directory,
+  else if (g_file_test (P_tmpdir,
+                    (GFileTest) (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
+    {
+      data_directory = P_tmpdir;
+    }
+  else
+    {
+      // If TMPDIR and P_tmpdir do not exist, try /tmp directly
+      data_directory = "/tmp";
+    }
+
+  data_directory += "/icedteaplugin-";
+  if (getenv("USER") != NULL)
+      data_directory += getenv("USER");
+
+  // Now create a icedteaplugin subdir
+  if (!g_file_test (data_directory.c_str(),
                     (GFileTest) (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
     {
       int file_error = 0;
 
-      data_directory = g_strconcat ("/tmp", NULL);
-        if (!data_directory)
-          {
-            PLUGIN_ERROR ("Failed to create data directory name.");
-            return NPERR_OUT_OF_MEMORY_ERROR;
-          }
-
-    }
-
-  data_directory = g_strconcat (data_directory, "/icedteaplugin-", getenv("USER"), NULL);
-
-  if (!data_directory)
-  {
-      PLUGIN_ERROR ("Failed to create data directory name.");
-      return NPERR_OUT_OF_MEMORY_ERROR;
-  }
-
-  // Now create a icedteaplugin subdir
-  if (!g_file_test (data_directory,
-                    (GFileTest) (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
-    {
-      int file_error = 0;
-
-      file_error = g_mkdir (data_directory, 0700);
+      file_error = g_mkdir (data_directory.c_str(), 0700);
       if (file_error != 0)
         {
           PLUGIN_ERROR_THREE ("Failed to create data directory",
-                          data_directory,
+                          data_directory.c_str(),
                           strerror (errno));
-          np_error = NPERR_GENERIC_ERROR;
-          goto cleanup_data_directory;
+          return NPERR_GENERIC_ERROR;
         }
     }
 
 
-  // If data directory doesn't exit by this point, bail
-  if (!g_file_test (data_directory,
+  // If data directory doesn't exist by this point, bail
+  if (!g_file_test (data_directory.c_str(),
                     (GFileTest) (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
     {
       PLUGIN_ERROR_THREE ("Temp directory does not exist: ",
-                          data_directory,
+                          data_directory.c_str(),
                           strerror (errno));
-
-      np_error = NPERR_GENERIC_ERROR;
-            goto cleanup_data_directory;
-
+      return NPERR_GENERIC_ERROR;
     }
 
   // Set appletviewer_executable.
-  filename = g_strdup(ICEDTEA_WEB_JRE);
-  appletviewer_executable = g_strdup_printf ("%s/bin/java",
-                                             filename);
   PLUGIN_DEBUG("Executing java at %s\n", appletviewer_executable);
-  if (!appletviewer_executable)
-    {
-      PLUGIN_ERROR ("Failed to create appletviewer executable name.");
-      np_error = NPERR_OUT_OF_MEMORY_ERROR;
-      goto cleanup_filename;
-    }
-
   np_error = plugin_test_appletviewer ();
   if (np_error != NPERR_NO_ERROR)
     {
       plugin_display_failure_dialog ();
-      goto cleanup_appletviewer_executable;
+      return np_error;
     }
-  g_free (filename);
 
   initialized = true;
 
@@ -2266,30 +2243,6 @@
   PLUGIN_DEBUG ("NP_Initialize return\n");
 
   return NPERR_NO_ERROR;
-
- cleanup_appletviewer_executable:
-  if (appletviewer_executable)
-    {
-      g_free (appletviewer_executable);
-      appletviewer_executable = NULL;
-    }
-
- cleanup_filename:
-  if (filename)
-    {
-      g_free (filename);
-      filename = NULL;
-    }
-
- cleanup_data_directory:
-  if (data_directory)
-    {
-      g_free (data_directory);
-      data_directory = NULL;
-    }
-
-
-  return np_error;
 }
 
 // Returns a string describing the MIME type that this plugin
@@ -2358,18 +2311,6 @@
       plugin_instance_mutex = NULL;
     }
 
-  if (data_directory)
-    {
-      g_free (data_directory);
-      data_directory = NULL;
-    }
-
-  if (appletviewer_executable)
-    {
-      g_free (appletviewer_executable);
-      appletviewer_executable = NULL;
-    }
-
   // stop the appletviewer
   plugin_stop_appletviewer();
 
diff -r 01b5bc425cd4 -r b9733f552c79 tests/cpp-unit-tests/IcedTeaNPPluginTest.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cpp-unit-tests/IcedTeaNPPluginTest.cc	Fri Dec 21 15:21:42 2012 -0500
@@ -0,0 +1,92 @@
+/* Copyright (C) 2012 Red Hat
+
+ This file is part of IcedTea.
+
+ IcedTea is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ IcedTea is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with IcedTea; see the file COPYING.  If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301 USA.
+
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library.  Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library with independent modules to produce an
+ executable, regardless of the license terms of these independent
+ modules, and to copy and distribute the resulting executable under
+ terms of your choice, provided that you also meet, for each linked
+ independent module, the terms and conditions of the license of that
+ module.  An independent module is a module which is not derived from
+ or based on this library.  If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so.  If you do not wish to do so, delete this
+ exception statement from your version. */
+
+#include <cstdio>
+
+#include <npapi.h>
+
+#include <UnitTest++.h>
+
+#include "IcedTeaNPPlugin.h"
+#include "IcedTeaPluginUtils.h"
+
+TEST(NP_GetMIMEDescription) {
+	std::string MIME_type = NP_GetMIMEDescription();
+	CHECK(MIME_type.find("application/x-java-applet") != std::string::npos);
+	CHECK(MIME_type.find("application/x-java-vm") != std::string::npos);
+}
+
+/* Not normally exposed */
+std::vector<std::string*>* get_jvm_args();
+extern gchar* in_pipe_name;
+extern gchar* out_pipe_name;
+
+TEST(get_jvm_args) {
+	in_pipe_name = (gchar*)"inpipe";
+	out_pipe_name = (gchar*)"outpipe";
+
+	std::vector<std::string*>* args = get_jvm_args();
+	CHECK(args != NULL);
+
+	IcedTeaPluginUtilities::freeStringPtrVector(args);
+}
+
+TEST(NP_GetValue) {
+  void* __unused = NULL;
+  gchar* char_value = NULL;
+
+  /* test plugin name */ {
+	CHECK_EQUAL(NPERR_NO_ERROR, 
+		NP_GetValue(__unused, NPPVpluginNameString, &char_value));
+	CHECK(std::string(char_value).find(PLUGIN_NAME) != std::string::npos);
+	g_free(char_value);
+	char_value = NULL;
+  }
+  /* test plugin desc */ {
+	CHECK_EQUAL(NPERR_NO_ERROR, 
+		NP_GetValue(__unused, NPPVpluginDescriptionString, &char_value));
+	CHECK(std::string(char_value).find("executes Java applets") != std::string::npos);
+	g_free(char_value);
+	char_value = NULL;
+  }
+  /* test plugin unknown */ {
+    printf("NOTE: Next error expected\n"); // the following will print an error message
+	CHECK_EQUAL(NPERR_GENERIC_ERROR, 
+		NP_GetValue(__unused, NPPVformValue, &char_value));
+	g_free(char_value);
+	char_value = NULL;
+  }
+}



More information about the distro-pkg-dev mailing list