summaryrefslogtreecommitdiffstats
path: root/security/sandbox/common/test/SandboxTest.cpp
blob: 5996dabb13b92bb593a1fb9f6cb641def443b986 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* -*- 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 https://mozilla.org/MPL/2.0/. */

#include "SandboxTest.h"

#include "mozilla/Components.h"
#include "SandboxTestingParent.h"
#include "SandboxTestingChild.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/gfx/GPUProcessManager.h"
#include "mozilla/gfx/GPUChild.h"
#include "mozilla/ipc/Endpoint.h"

using namespace mozilla;
using namespace mozilla::ipc;
using namespace mozilla::dom;

namespace mozilla {

NS_IMPL_ISUPPORTS(SandboxTest, mozISandboxTest)

GeckoProcessType GeckoProcessStringToType(const nsCString& aString) {
  for (GeckoProcessType type = GeckoProcessType(0);
       type < GeckoProcessType::GeckoProcessType_End;
       type = GeckoProcessType(type + 1)) {
    if (aString == kGeckoProcessTypeString[type]) {
      return type;
    }
  }
  return GeckoProcessType::GeckoProcessType_Invalid;
}

// Set up tests on remote process connected to the given actor.
// The actor must handle the InitSandboxTesting message.
template <typename Actor>
SandboxTestingParent* InitializeSandboxTestingActors(Actor* aActor) {
  Endpoint<PSandboxTestingParent> sandboxTestingParentEnd;
  Endpoint<PSandboxTestingChild> sandboxTestingChildEnd;
  nsresult rv = PSandboxTesting::CreateEndpoints(
      base::GetCurrentProcId(), aActor->OtherPid(), &sandboxTestingParentEnd,
      &sandboxTestingChildEnd);
  if (NS_FAILED(rv)) {
    return nullptr;
  }

  Unused << aActor->SendInitSandboxTesting(std::move(sandboxTestingChildEnd));
  return SandboxTestingParent::Create(std::move(sandboxTestingParentEnd));
}

NS_IMETHODIMP
SandboxTest::StartTests(const nsTArray<nsCString>& aProcessesList) {
  for (auto processTypeName : aProcessesList) {
    GeckoProcessType type = GeckoProcessStringToType(processTypeName);
    if (type == GeckoProcessType::GeckoProcessType_Invalid) {
      return NS_ERROR_ILLEGAL_VALUE;
    }

    switch (type) {
      case GeckoProcessType_Content: {
        nsTArray<ContentParent*> parents;
        ContentParent::GetAll(parents);
        MOZ_ASSERT(parents.Length() > 0);
        mSandboxTestingParents[type] =
            InitializeSandboxTestingActors(parents[0]);
        break;
      }

      case GeckoProcessType_GPU: {
        gfx::GPUProcessManager* gpuProc = gfx::GPUProcessManager::Get();
        gfx::GPUChild* gpuChild = gpuProc ? gpuProc->GetGPUChild() : nullptr;
        if (!gpuChild) {
          // There is no GPU process for this OS.  Report test done.
          nsCOMPtr<nsIObserverService> observerService =
              mozilla::services::GetObserverService();
          MOZ_RELEASE_ASSERT(observerService);
          observerService->NotifyObservers(nullptr, "sandbox-test-done", 0);
          return NS_OK;
        }

        mSandboxTestingParents[type] = InitializeSandboxTestingActors(gpuChild);
        break;
      }

      default:
        MOZ_ASSERT_UNREACHABLE(
            "SandboxTest does not yet support this process type");
        return NS_ERROR_ILLEGAL_VALUE;
    }

    if (!mSandboxTestingParents[type]) {
      return NS_ERROR_FAILURE;
    }
  }
  return NS_OK;
}

NS_IMETHODIMP
SandboxTest::FinishTests() {
  for (SandboxTestingParent* stp : mSandboxTestingParents) {
    SandboxTestingParent::Destroy(stp);
  }
  return NS_OK;
}

}  // namespace mozilla

NS_IMPL_COMPONENT_FACTORY(mozISandboxTest) {
  return MakeAndAddRef<SandboxTest>().downcast<nsISupports>();
}