Bug 1072024 - GTest for cross-origin separation of Gecko Media Plugins r=cpearce
authorEdwin Flores <eflores@mozilla.com>
Tue, 14 Oct 2014 11:04:59 +1300
changeset 210240 282fb5ca6310148bfbbdd4e47220c0033f849e13
parent 210239 3a3027f77f625bab53a9d1f7e300090904f70021
child 210241 bdba7bcc32dcd68b647bfe997cdd0c917d347b00
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewerscpearce
bugs1072024
milestone35.0a1
Bug 1072024 - GTest for cross-origin separation of Gecko Media Plugins r=cpearce
content/media/gtest/TestGMPCrossOrigin.cpp
content/media/gtest/moz.build
testing/gtest/rungtests.py
new file mode 100644
--- /dev/null
+++ b/content/media/gtest/TestGMPCrossOrigin.cpp
@@ -0,0 +1,123 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "gtest/gtest.h"
+
+#include "mozilla/StaticPtr.h"
+#include "GMPVideoDecoderProxy.h"
+#include "GMPVideoEncoderProxy.h"
+#include "GMPService.h"
+
+#include "nsAppDirectoryServiceDefs.h"
+#include "nsIFile.h"
+#include "nsISimpleEnumerator.h"
+
+using namespace mozilla;
+using namespace mozilla::gmp;
+
+struct GMPTestRunner
+{
+  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(GMPTestRunner)
+
+  void DoTest(void (GMPTestRunner::*aTestMethod)());
+  void RunTestGMPTestCodec();
+  void RunTestGMPCrossOrigin();
+
+private:
+  ~GMPTestRunner() { }
+};
+
+void
+GMPTestRunner::RunTestGMPTestCodec()
+{
+  nsRefPtr<GeckoMediaPluginService> service =
+    GeckoMediaPluginService::GetGeckoMediaPluginService();
+
+  GMPVideoHost* host = nullptr;
+  GMPVideoDecoderProxy* decoder = nullptr;
+  GMPVideoDecoderProxy* decoder2 = nullptr;
+  GMPVideoEncoderProxy* encoder = nullptr;
+
+  nsTArray<nsCString> tags;
+  tags.AppendElement(NS_LITERAL_CSTRING("h264"));
+
+  service->GetGMPVideoDecoder(&tags, NS_LITERAL_STRING("o"), &host, &decoder2);
+  service->GetGMPVideoDecoder(&tags, NS_LITERAL_STRING(""), &host, &decoder);
+
+  service->GetGMPVideoEncoder(&tags, NS_LITERAL_STRING(""), &host, &encoder);
+
+  EXPECT_TRUE(host);
+  EXPECT_TRUE(decoder);
+  EXPECT_TRUE(decoder2);
+  EXPECT_TRUE(encoder);
+
+  if (decoder) decoder->Close();
+  if (decoder2) decoder2->Close();
+  if (encoder) encoder->Close();
+}
+
+void
+GMPTestRunner::RunTestGMPCrossOrigin()
+{
+  nsRefPtr<GeckoMediaPluginService> service =
+    GeckoMediaPluginService::GetGeckoMediaPluginService();
+
+  GMPVideoHost* host = nullptr;
+  nsTArray<nsCString> tags;
+  tags.AppendElement(NS_LITERAL_CSTRING("h264"));
+
+  GMPVideoDecoderProxy* decoder1 = nullptr;
+  GMPVideoDecoderProxy* decoder2 = nullptr;
+  GMPVideoEncoderProxy* encoder1 = nullptr;
+  GMPVideoEncoderProxy* encoder2 = nullptr;
+
+  service->GetGMPVideoDecoder(&tags, NS_LITERAL_STRING("origin1"), &host, &decoder1);
+  service->GetGMPVideoDecoder(&tags, NS_LITERAL_STRING("origin2"), &host, &decoder2);
+  EXPECT_TRUE(!!decoder1 && !!decoder2 &&
+              decoder1->ParentID() != decoder2->ParentID());
+
+  service->GetGMPVideoEncoder(&tags, NS_LITERAL_STRING("origin1"), &host, &encoder1);
+  service->GetGMPVideoEncoder(&tags, NS_LITERAL_STRING("origin2"), &host, &encoder2);
+  EXPECT_TRUE(!!encoder1 && !!encoder2 &&
+              encoder1->ParentID() != encoder2->ParentID());
+
+  if (decoder2) decoder2->Close();
+  if (encoder2) encoder2->Close();
+
+  service->GetGMPVideoDecoder(&tags, NS_LITERAL_STRING("origin1"), &host, &decoder2);
+  EXPECT_TRUE(!!decoder1 && !!decoder2 &&
+              decoder1->ParentID() == decoder2->ParentID());
+
+  service->GetGMPVideoEncoder(&tags, NS_LITERAL_STRING("origin1"), &host, &encoder2);
+  EXPECT_TRUE(!!encoder1 && !!encoder2 &&
+              encoder1->ParentID() == encoder2->ParentID());
+
+  if (decoder1) decoder1->Close();
+  if (decoder2) decoder2->Close();
+  if (encoder1) encoder1->Close();
+  if (encoder2) encoder2->Close();
+}
+
+void
+GMPTestRunner::DoTest(void (GMPTestRunner::*aTestMethod)())
+{
+  nsRefPtr<GeckoMediaPluginService> service =
+    GeckoMediaPluginService::GetGeckoMediaPluginService();
+  nsCOMPtr<nsIThread> thread;
+
+  service->GetThread(getter_AddRefs(thread));
+  thread->Dispatch(NS_NewRunnableMethod(this, aTestMethod), NS_DISPATCH_SYNC);
+}
+
+TEST(GeckoMediaPlugins, GMPTestCodec) {
+  nsRefPtr<GMPTestRunner> runner = new GMPTestRunner();
+  runner->DoTest(&GMPTestRunner::RunTestGMPTestCodec);
+}
+
+TEST(GeckoMediaPlugins, GMPCrossOrigin) {
+  nsRefPtr<GMPTestRunner> runner = new GMPTestRunner();
+  runner->DoTest(&GMPTestRunner::RunTestGMPCrossOrigin);
+}
--- a/content/media/gtest/moz.build
+++ b/content/media/gtest/moz.build
@@ -1,16 +1,17 @@
 # -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 UNIFIED_SOURCES += [
     'TestAudioCompactor.cpp',
+    'TestGMPCrossOrigin.cpp',
     'TestTrackEncoder.cpp',
     'TestVideoSegment.cpp',
     'TestWebMBuffered.cpp'
 ]
 
 TEST_HARNESS_FILES.gtest += [
     'test.webm'
 ]
@@ -20,13 +21,14 @@ if CONFIG['MOZ_WEBM_ENCODER']:
                         'TestVorbisTrackEncoder.cpp',
                         'TestWebMWriter.cpp',
 ]
 
 include('/ipc/chromium/chromium-config.mozbuild')
 
 LOCAL_INCLUDES += [
     '/content/media/encoder',
+    '/content/media/gmp',
 ]
 
 FINAL_LIBRARY = 'xul-gtest'
 
 FAIL_ON_WARNINGS = True
--- a/testing/gtest/rungtests.py
+++ b/testing/gtest/rungtests.py
@@ -62,16 +62,17 @@ class GTests(object):
         return result
 
     def build_core_environment(self, env = {}):
         """
         Add environment variables likely to be used across all platforms, including remote systems.
         """
         env["MOZILLA_FIVE_HOME"] = self.xre_path
         env["MOZ_XRE_DIR"] = self.xre_path
+        env["MOZ_GMP_PATH"] = os.path.join(self.xre_path, "gmp-fake", "1.0")
         env["XPCOM_DEBUG_BREAK"] = "stack-and-abort"
         env["MOZ_CRASHREPORTER_NO_REPORT"] = "1"
         env["MOZ_CRASHREPORTER"] = "1"
         env["MOZ_RUN_GTEST"] = "1"
         # Normally we run with GTest default output, override this to use the TBPL test format.
         env["MOZ_TBPL_PARSER"] = "1"
         return env