2014-10-21 06:16:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "gmp-test-decryptor.h"
|
|
|
|
#include "gmp-test-storage.h"
|
2014-11-03 10:02:30 +00:00
|
|
|
#include "gmp-test-output-protection.h"
|
2014-10-21 06:16:19 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <istream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <sstream>
|
2014-12-04 20:32:00 +00:00
|
|
|
#include <set>
|
2014-10-21 06:16:19 +00:00
|
|
|
|
2014-10-28 22:43:18 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-10-21 06:16:19 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
FakeDecryptor* FakeDecryptor::sInstance = nullptr;
|
2014-12-04 20:32:00 +00:00
|
|
|
extern GMPPlatformAPI* g_platform_api; // Defined in gmp-fake.cpp
|
2014-10-21 06:16:19 +00:00
|
|
|
|
2014-12-04 20:32:00 +00:00
|
|
|
class GMPMutexAutoLock
|
2014-10-21 06:16:19 +00:00
|
|
|
{
|
2014-12-04 20:32:00 +00:00
|
|
|
public:
|
|
|
|
explicit GMPMutexAutoLock(GMPMutex* aMutex) : mMutex(aMutex) {
|
|
|
|
mMutex->Acquire();
|
|
|
|
}
|
|
|
|
~GMPMutexAutoLock() {
|
|
|
|
mMutex->Release();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
GMPMutex* const mMutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestManager {
|
|
|
|
public:
|
|
|
|
TestManager() : mMutex(CreateMutex()) {}
|
|
|
|
|
|
|
|
// Register a test with the test manager.
|
|
|
|
void BeginTest(const string& aTestID) {
|
|
|
|
GMPMutexAutoLock lock(mMutex);
|
|
|
|
auto found = mTestIDs.find(aTestID);
|
|
|
|
if (found == mTestIDs.end()) {
|
|
|
|
mTestIDs.insert(aTestID);
|
|
|
|
} else {
|
|
|
|
Error("FAIL BeginTest test already existed: " + aTestID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify the test manager that the test is finished. If all tests are done,
|
|
|
|
// test manager will send "test-storage complete" to notify the parent that
|
|
|
|
// all tests are finished and also delete itself.
|
|
|
|
void EndTest(const string& aTestID) {
|
|
|
|
bool isEmpty = false;
|
|
|
|
{
|
|
|
|
GMPMutexAutoLock lock(mMutex);
|
|
|
|
auto found = mTestIDs.find(aTestID);
|
|
|
|
if (found != mTestIDs.end()) {
|
|
|
|
mTestIDs.erase(aTestID);
|
|
|
|
isEmpty = mTestIDs.empty();
|
|
|
|
} else {
|
|
|
|
Error("FAIL EndTest test not existed: " + aTestID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isEmpty) {
|
|
|
|
Finish();
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~TestManager() {
|
|
|
|
mMutex->Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Error(const string& msg) {
|
|
|
|
FakeDecryptor::Message(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Finish() {
|
2014-10-21 06:16:19 +00:00
|
|
|
FakeDecryptor::Message("test-storage complete");
|
|
|
|
}
|
2014-12-04 20:32:00 +00:00
|
|
|
|
|
|
|
static GMPMutex* CreateMutex() {
|
|
|
|
GMPMutex* mutex = nullptr;
|
|
|
|
g_platform_api->createmutex(&mutex);
|
|
|
|
return mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPMutex* const mMutex;
|
|
|
|
set<string> mTestIDs;
|
|
|
|
};
|
2014-10-21 06:16:19 +00:00
|
|
|
|
2014-11-14 08:39:39 +00:00
|
|
|
FakeDecryptor::FakeDecryptor(GMPDecryptorHost* aHost)
|
2014-10-23 07:37:55 +00:00
|
|
|
: mCallback(nullptr)
|
2014-11-14 08:39:39 +00:00
|
|
|
, mHost(aHost)
|
2014-10-21 06:16:19 +00:00
|
|
|
{
|
2014-10-28 22:43:18 +00:00
|
|
|
MOZ_ASSERT(!sInstance);
|
2014-10-21 06:16:19 +00:00
|
|
|
sInstance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FakeDecryptor::DecryptingComplete()
|
|
|
|
{
|
|
|
|
sInstance = nullptr;
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FakeDecryptor::Message(const std::string& aMessage)
|
|
|
|
{
|
2014-10-28 22:43:18 +00:00
|
|
|
MOZ_ASSERT(sInstance);
|
2014-10-21 06:16:19 +00:00
|
|
|
const static std::string sid("fake-session-id");
|
|
|
|
sInstance->mCallback->SessionMessage(sid.c_str(), sid.size(),
|
2015-01-14 22:25:47 +00:00
|
|
|
kGMPLicenseRequest,
|
|
|
|
(const uint8_t*)aMessage.c_str(), aMessage.size());
|
2014-10-21 06:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
Tokenize(const std::string& aString)
|
|
|
|
{
|
|
|
|
std::stringstream strstr(aString);
|
|
|
|
std::istream_iterator<std::string> it(strstr), end;
|
|
|
|
return std::vector<std::string>(it, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const string TruncateRecordId = "truncate-record-id";
|
|
|
|
static const string TruncateRecordData = "I will soon be truncated";
|
|
|
|
|
|
|
|
class ReadThenTask : public GMPTask {
|
|
|
|
public:
|
|
|
|
ReadThenTask(string aId, ReadContinuation* aThen)
|
|
|
|
: mId(aId)
|
|
|
|
, mThen(aThen)
|
|
|
|
{}
|
2015-03-21 16:28:04 +00:00
|
|
|
void Run() override {
|
2014-10-21 06:16:19 +00:00
|
|
|
ReadRecord(mId, mThen);
|
|
|
|
}
|
2015-03-21 16:28:04 +00:00
|
|
|
void Destroy() override {
|
2014-10-21 06:16:19 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
string mId;
|
2014-10-23 07:37:55 +00:00
|
|
|
ReadContinuation* mThen;
|
2014-10-21 06:16:19 +00:00
|
|
|
};
|
|
|
|
|
2014-12-03 12:34:00 +00:00
|
|
|
class SendMessageTask : public GMPTask {
|
|
|
|
public:
|
2014-12-10 22:49:09 +00:00
|
|
|
explicit SendMessageTask(const string& aMessage,
|
|
|
|
TestManager* aTestManager = nullptr,
|
|
|
|
const string& aTestID = "")
|
2014-12-04 20:32:00 +00:00
|
|
|
: mMessage(aMessage), mTestmanager(aTestManager), mTestID(aTestID) {}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
void Run() override {
|
2014-12-03 12:34:00 +00:00
|
|
|
FakeDecryptor::Message(mMessage);
|
2014-12-04 20:32:00 +00:00
|
|
|
if (mTestmanager) {
|
|
|
|
mTestmanager->EndTest(mTestID);
|
|
|
|
}
|
2014-12-03 12:34:00 +00:00
|
|
|
}
|
2014-12-04 20:32:00 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
void Destroy() override {
|
2014-12-03 12:34:00 +00:00
|
|
|
delete this;
|
|
|
|
}
|
2014-12-04 20:32:00 +00:00
|
|
|
|
|
|
|
private:
|
2014-12-03 12:34:00 +00:00
|
|
|
string mMessage;
|
2014-12-04 20:32:00 +00:00
|
|
|
TestManager* const mTestmanager;
|
|
|
|
const string mTestID;
|
2014-12-03 12:34:00 +00:00
|
|
|
};
|
|
|
|
|
2014-10-21 06:16:19 +00:00
|
|
|
class TestEmptyContinuation : public ReadContinuation {
|
|
|
|
public:
|
2014-12-04 20:32:00 +00:00
|
|
|
TestEmptyContinuation(TestManager* aTestManager, const string& aTestID)
|
|
|
|
: mTestmanager(aTestManager), mTestID(aTestID) {}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
void ReadComplete(GMPErr aErr, const std::string& aData) override {
|
2014-10-21 06:16:19 +00:00
|
|
|
if (aData != "") {
|
|
|
|
FakeDecryptor::Message("FAIL TestEmptyContinuation record was not truncated");
|
|
|
|
}
|
2014-12-04 20:32:00 +00:00
|
|
|
mTestmanager->EndTest(mTestID);
|
2014-10-21 06:16:19 +00:00
|
|
|
delete this;
|
|
|
|
}
|
2014-12-04 20:32:00 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
TestManager* const mTestmanager;
|
|
|
|
const string mTestID;
|
2014-10-21 06:16:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class TruncateContinuation : public ReadContinuation {
|
|
|
|
public:
|
2014-12-04 20:34:00 +00:00
|
|
|
TruncateContinuation(const string& aID,
|
|
|
|
TestManager* aTestManager,
|
|
|
|
const string& aTestID)
|
|
|
|
: mID(aID), mTestmanager(aTestManager), mTestID(aTestID) {}
|
2014-12-04 20:32:00 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
void ReadComplete(GMPErr aErr, const std::string& aData) override {
|
2014-10-21 06:16:19 +00:00
|
|
|
if (aData != TruncateRecordData) {
|
|
|
|
FakeDecryptor::Message("FAIL TruncateContinuation read data doesn't match written data");
|
|
|
|
}
|
2014-12-04 20:32:00 +00:00
|
|
|
auto cont = new TestEmptyContinuation(mTestmanager, mTestID);
|
|
|
|
auto msg = "FAIL in TruncateContinuation write.";
|
|
|
|
auto failTask = new SendMessageTask(msg, mTestmanager, mTestID);
|
2014-12-04 20:34:00 +00:00
|
|
|
WriteRecord(mID, nullptr, 0, new ReadThenTask(mID, cont), failTask);
|
2014-10-21 06:16:19 +00:00
|
|
|
delete this;
|
|
|
|
}
|
2014-12-04 20:32:00 +00:00
|
|
|
|
|
|
|
private:
|
2014-12-04 20:34:00 +00:00
|
|
|
const string mID;
|
2014-12-04 20:32:00 +00:00
|
|
|
TestManager* const mTestmanager;
|
|
|
|
const string mTestID;
|
2014-10-21 06:16:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class VerifyAndFinishContinuation : public ReadContinuation {
|
|
|
|
public:
|
2014-12-04 20:32:00 +00:00
|
|
|
explicit VerifyAndFinishContinuation(string aValue,
|
|
|
|
TestManager* aTestManager,
|
|
|
|
const string& aTestID)
|
|
|
|
: mValue(aValue), mTestmanager(aTestManager), mTestID(aTestID) {}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
void ReadComplete(GMPErr aErr, const std::string& aData) override {
|
2014-10-21 06:16:19 +00:00
|
|
|
if (aData != mValue) {
|
|
|
|
FakeDecryptor::Message("FAIL VerifyAndFinishContinuation read data doesn't match expected data");
|
|
|
|
}
|
2014-12-04 20:32:00 +00:00
|
|
|
mTestmanager->EndTest(mTestID);
|
2014-10-21 06:16:19 +00:00
|
|
|
delete this;
|
|
|
|
}
|
2014-12-04 20:32:00 +00:00
|
|
|
|
|
|
|
private:
|
2014-10-21 06:16:19 +00:00
|
|
|
string mValue;
|
2014-12-04 20:32:00 +00:00
|
|
|
TestManager* const mTestmanager;
|
|
|
|
const string mTestID;
|
2014-10-21 06:16:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class VerifyAndOverwriteContinuation : public ReadContinuation {
|
|
|
|
public:
|
2014-12-04 20:32:00 +00:00
|
|
|
VerifyAndOverwriteContinuation(string aId, string aValue, string aOverwrite,
|
|
|
|
TestManager* aTestManager, const string& aTestID)
|
2014-10-21 06:16:19 +00:00
|
|
|
: mId(aId)
|
|
|
|
, mValue(aValue)
|
|
|
|
, mOverwrite(aOverwrite)
|
2014-12-04 20:32:00 +00:00
|
|
|
, mTestmanager(aTestManager)
|
|
|
|
, mTestID(aTestID)
|
2014-10-21 06:16:19 +00:00
|
|
|
{}
|
2014-12-04 20:32:00 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
void ReadComplete(GMPErr aErr, const std::string& aData) override {
|
2014-10-21 06:16:19 +00:00
|
|
|
if (aData != mValue) {
|
|
|
|
FakeDecryptor::Message("FAIL VerifyAndOverwriteContinuation read data doesn't match expected data");
|
|
|
|
}
|
2014-12-04 20:32:00 +00:00
|
|
|
auto cont = new VerifyAndFinishContinuation(mOverwrite, mTestmanager, mTestID);
|
|
|
|
auto msg = "FAIL in VerifyAndOverwriteContinuation write.";
|
|
|
|
auto failTask = new SendMessageTask(msg, mTestmanager, mTestID);
|
|
|
|
WriteRecord(mId, mOverwrite, new ReadThenTask(mId, cont), failTask);
|
2014-10-21 06:16:19 +00:00
|
|
|
delete this;
|
|
|
|
}
|
2014-12-04 20:32:00 +00:00
|
|
|
|
|
|
|
private:
|
2014-10-21 06:16:19 +00:00
|
|
|
string mId;
|
|
|
|
string mValue;
|
|
|
|
string mOverwrite;
|
2014-12-04 20:32:00 +00:00
|
|
|
TestManager* const mTestmanager;
|
|
|
|
const string mTestID;
|
2014-10-21 06:16:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const string OpenAgainRecordId = "open-again-record-id";
|
|
|
|
|
|
|
|
class OpenedSecondTimeContinuation : public OpenContinuation {
|
|
|
|
public:
|
2014-12-04 20:32:00 +00:00
|
|
|
explicit OpenedSecondTimeContinuation(GMPRecord* aRecord,
|
|
|
|
TestManager* aTestManager,
|
|
|
|
const string& aTestID)
|
2015-02-10 17:19:00 +00:00
|
|
|
: mRecord(aRecord), mTestmanager(aTestManager), mTestID(aTestID) {
|
|
|
|
MOZ_ASSERT(aRecord);
|
|
|
|
}
|
2014-10-21 06:16:19 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual void OpenComplete(GMPErr aStatus, GMPRecord* aRecord) override {
|
2014-10-21 06:16:19 +00:00
|
|
|
if (GMP_SUCCEEDED(aStatus)) {
|
|
|
|
FakeDecryptor::Message("FAIL OpenSecondTimeContinuation should not be able to re-open record.");
|
|
|
|
}
|
2015-02-10 17:19:00 +00:00
|
|
|
if (aRecord) {
|
|
|
|
aRecord->Close();
|
|
|
|
}
|
2014-10-21 06:16:19 +00:00
|
|
|
// Succeeded, open should have failed.
|
2014-12-04 20:32:00 +00:00
|
|
|
mTestmanager->EndTest(mTestID);
|
2014-10-21 06:16:19 +00:00
|
|
|
mRecord->Close();
|
|
|
|
}
|
2014-12-04 20:32:00 +00:00
|
|
|
|
|
|
|
private:
|
2014-10-21 06:16:19 +00:00
|
|
|
GMPRecord* mRecord;
|
2014-12-04 20:32:00 +00:00
|
|
|
TestManager* const mTestmanager;
|
|
|
|
const string mTestID;
|
2014-10-21 06:16:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class OpenedFirstTimeContinuation : public OpenContinuation {
|
|
|
|
public:
|
2014-12-04 20:34:00 +00:00
|
|
|
OpenedFirstTimeContinuation(const string& aID,
|
|
|
|
TestManager* aTestManager,
|
|
|
|
const string& aTestID)
|
|
|
|
: mID(aID), mTestmanager(aTestManager), mTestID(aTestID) {}
|
2014-12-04 20:32:00 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual void OpenComplete(GMPErr aStatus, GMPRecord* aRecord) override {
|
2014-10-21 06:16:19 +00:00
|
|
|
if (GMP_FAILED(aStatus)) {
|
|
|
|
FakeDecryptor::Message("FAIL OpenAgainContinuation to open record initially.");
|
2014-12-04 20:32:00 +00:00
|
|
|
mTestmanager->EndTest(mTestID);
|
2015-02-10 17:19:00 +00:00
|
|
|
if (aRecord) {
|
|
|
|
aRecord->Close();
|
|
|
|
}
|
2014-10-21 06:16:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-04 20:32:00 +00:00
|
|
|
auto cont = new OpenedSecondTimeContinuation(aRecord, mTestmanager, mTestID);
|
2014-12-04 20:34:00 +00:00
|
|
|
GMPOpenRecord(mID, cont);
|
2014-10-21 06:16:19 +00:00
|
|
|
}
|
2014-12-04 20:32:00 +00:00
|
|
|
|
|
|
|
private:
|
2014-12-04 20:34:00 +00:00
|
|
|
const string mID;
|
2014-12-04 20:32:00 +00:00
|
|
|
TestManager* const mTestmanager;
|
|
|
|
const string mTestID;
|
2014-10-21 06:16:19 +00:00
|
|
|
};
|
|
|
|
|
2014-12-04 20:34:00 +00:00
|
|
|
static void
|
|
|
|
DoTestStorage(const string& aPrefix, TestManager* aTestManager)
|
2014-10-21 06:16:19 +00:00
|
|
|
{
|
|
|
|
// Basic I/O tests. We run three cases concurrently. The tests, like
|
|
|
|
// GMPStorage run asynchronously. When they've all passed, we send
|
|
|
|
// a message back to the parent process, or a failure message if not.
|
|
|
|
|
|
|
|
// Test 1: Basic I/O test, and test that writing 0 bytes in a record
|
|
|
|
// deletes record.
|
|
|
|
//
|
|
|
|
// Write data to truncate record, then
|
|
|
|
// read data, verify that we read what we wrote, then
|
|
|
|
// write 0 bytes to truncate record, then
|
|
|
|
// read data, verify that 0 bytes was read
|
2014-12-04 20:34:00 +00:00
|
|
|
const string id1 = aPrefix + TruncateRecordId;
|
|
|
|
const string testID1 = aPrefix + "write-test-1";
|
|
|
|
aTestManager->BeginTest(testID1);
|
|
|
|
auto cont1 = new TruncateContinuation(id1, aTestManager, testID1);
|
2014-12-04 20:32:00 +00:00
|
|
|
auto msg1 = "FAIL in TestStorage writing TruncateRecord.";
|
2014-12-04 20:34:00 +00:00
|
|
|
auto failTask1 = new SendMessageTask(msg1, aTestManager, testID1);
|
|
|
|
WriteRecord(id1, TruncateRecordData,
|
|
|
|
new ReadThenTask(id1, cont1), failTask1);
|
2014-10-21 06:16:19 +00:00
|
|
|
|
|
|
|
// Test 2: Test that overwriting a record with a shorter record truncates
|
|
|
|
// the record to the shorter record.
|
|
|
|
//
|
|
|
|
// Write record, then
|
|
|
|
// read and verify record, then
|
|
|
|
// write a shorter record to same record.
|
|
|
|
// read and verify
|
2014-12-04 20:34:00 +00:00
|
|
|
string id2 = aPrefix + "record1";
|
2014-10-21 06:16:19 +00:00
|
|
|
string record1 = "This is the first write to a record.";
|
|
|
|
string overwrite = "A shorter record";
|
2014-12-04 20:34:00 +00:00
|
|
|
const string testID2 = aPrefix + "write-test-2";
|
|
|
|
aTestManager->BeginTest(testID2);
|
|
|
|
auto task2 = new VerifyAndOverwriteContinuation(id2, record1, overwrite,
|
|
|
|
aTestManager, testID2);
|
2014-12-04 20:32:00 +00:00
|
|
|
auto msg2 = "FAIL in TestStorage writing record1.";
|
2014-12-04 20:34:00 +00:00
|
|
|
auto failTask2 = new SendMessageTask(msg2, aTestManager, testID2);
|
|
|
|
WriteRecord(id2, record1, new ReadThenTask(id2, task2), failTask2);
|
2014-10-21 06:16:19 +00:00
|
|
|
|
|
|
|
// Test 3: Test that opening a record while it's already open fails.
|
|
|
|
//
|
|
|
|
// Open record1, then
|
|
|
|
// open record1, should fail.
|
2014-12-04 20:32:00 +00:00
|
|
|
// close record1
|
2014-12-04 20:34:00 +00:00
|
|
|
const string id3 = aPrefix + OpenAgainRecordId;
|
|
|
|
const string testID3 = aPrefix + "open-test-1";
|
|
|
|
aTestManager->BeginTest(testID3);
|
|
|
|
auto task3 = new OpenedFirstTimeContinuation(id3, aTestManager, testID3);
|
|
|
|
GMPOpenRecord(id3, task3);
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestStorageTask : public GMPTask {
|
|
|
|
public:
|
|
|
|
TestStorageTask(const string& aPrefix, TestManager* aTestManager)
|
|
|
|
: mPrefix(aPrefix), mTestManager(aTestManager) {}
|
|
|
|
virtual void Destroy() { delete this; }
|
|
|
|
virtual void Run() {
|
|
|
|
DoTestStorage(mPrefix, mTestManager);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
const string mPrefix;
|
|
|
|
TestManager* const mTestManager;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
FakeDecryptor::TestStorage()
|
|
|
|
{
|
|
|
|
TestManager* testManager = new TestManager();
|
|
|
|
GMPThread* thread1 = nullptr;
|
|
|
|
GMPThread* thread2 = nullptr;
|
|
|
|
|
|
|
|
// Main thread tests.
|
|
|
|
DoTestStorage("mt1-", testManager);
|
|
|
|
DoTestStorage("mt2-", testManager);
|
|
|
|
|
|
|
|
// Off-main-thread tests.
|
|
|
|
if (GMP_SUCCEEDED(g_platform_api->createthread(&thread1))) {
|
|
|
|
thread1->Post(new TestStorageTask("thread1-", testManager));
|
|
|
|
} else {
|
|
|
|
FakeDecryptor::Message("FAIL to create thread1 for storage tests");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GMP_SUCCEEDED(g_platform_api->createthread(&thread2))) {
|
|
|
|
thread2->Post(new TestStorageTask("thread2-", testManager));
|
|
|
|
} else {
|
|
|
|
FakeDecryptor::Message("FAIL to create thread2 for storage tests");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thread1) {
|
|
|
|
thread1->Join();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thread2) {
|
|
|
|
thread2->Join();
|
|
|
|
}
|
2014-10-21 06:16:19 +00:00
|
|
|
|
2014-12-04 20:32:00 +00:00
|
|
|
// Note: Once all tests finish, TestManager will dispatch "test-pass" message,
|
2014-10-21 06:16:19 +00:00
|
|
|
// which ends the test for the parent.
|
|
|
|
}
|
|
|
|
|
|
|
|
class ReportWritten : public GMPTask {
|
|
|
|
public:
|
|
|
|
ReportWritten(const string& aRecordId, const string& aValue)
|
|
|
|
: mRecordId(aRecordId)
|
|
|
|
, mValue(aValue)
|
|
|
|
{}
|
2015-03-21 16:28:04 +00:00
|
|
|
void Run() override {
|
2014-10-21 06:16:19 +00:00
|
|
|
FakeDecryptor::Message("stored " + mRecordId + " " + mValue);
|
|
|
|
}
|
2015-03-21 16:28:04 +00:00
|
|
|
void Destroy() override {
|
2014-10-21 06:16:19 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
const string mRecordId;
|
|
|
|
const string mValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReportReadStatusContinuation : public ReadContinuation {
|
|
|
|
public:
|
2014-10-10 21:28:35 +00:00
|
|
|
explicit ReportReadStatusContinuation(const string& aRecordId)
|
2014-10-21 06:16:19 +00:00
|
|
|
: mRecordId(aRecordId)
|
|
|
|
{}
|
2015-03-21 16:28:04 +00:00
|
|
|
void ReadComplete(GMPErr aErr, const std::string& aData) override {
|
2014-10-21 06:16:19 +00:00
|
|
|
if (GMP_FAILED(aErr)) {
|
|
|
|
FakeDecryptor::Message("retrieve " + mRecordId + " failed");
|
|
|
|
} else {
|
|
|
|
stringstream ss;
|
|
|
|
ss << aData.size();
|
|
|
|
string len;
|
|
|
|
ss >> len;
|
|
|
|
FakeDecryptor::Message("retrieve " + mRecordId + " succeeded (length " +
|
|
|
|
len + " bytes)");
|
|
|
|
}
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
string mRecordId;
|
|
|
|
};
|
|
|
|
|
2014-10-23 07:37:55 +00:00
|
|
|
class ReportReadRecordContinuation : public ReadContinuation {
|
|
|
|
public:
|
2014-10-10 21:28:35 +00:00
|
|
|
explicit ReportReadRecordContinuation(const string& aRecordId)
|
2014-10-23 07:37:55 +00:00
|
|
|
: mRecordId(aRecordId)
|
|
|
|
{}
|
2015-03-21 16:28:04 +00:00
|
|
|
void ReadComplete(GMPErr aErr, const std::string& aData) override {
|
2014-10-23 07:37:55 +00:00
|
|
|
if (GMP_FAILED(aErr)) {
|
|
|
|
FakeDecryptor::Message("retrieved " + mRecordId + " failed");
|
|
|
|
} else {
|
|
|
|
FakeDecryptor::Message("retrieved " + mRecordId + " " + aData);
|
|
|
|
}
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
string mRecordId;
|
|
|
|
};
|
|
|
|
|
2014-11-20 23:25:12 +00:00
|
|
|
static void
|
|
|
|
RecvGMPRecordIterator(GMPRecordIterator* aRecordIterator,
|
|
|
|
void* aUserArg,
|
|
|
|
GMPErr aStatus)
|
|
|
|
{
|
|
|
|
FakeDecryptor* decryptor = reinterpret_cast<FakeDecryptor*>(aUserArg);
|
|
|
|
decryptor->ProcessRecordNames(aRecordIterator, aStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FakeDecryptor::ProcessRecordNames(GMPRecordIterator* aRecordIterator,
|
|
|
|
GMPErr aStatus)
|
|
|
|
{
|
|
|
|
if (sInstance != this) {
|
|
|
|
FakeDecryptor::Message("Error aUserArg was not passed through GetRecordIterator");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (GMP_FAILED(aStatus)) {
|
|
|
|
FakeDecryptor::Message("Error GetRecordIterator failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string response("record-names ");
|
|
|
|
bool first = true;
|
|
|
|
const char* name = nullptr;
|
|
|
|
uint32_t len = 0;
|
|
|
|
while (GMP_SUCCEEDED(aRecordIterator->GetName(&name, &len))) {
|
|
|
|
std::string s(name, name+len);
|
|
|
|
if (!first) {
|
|
|
|
response += ",";
|
|
|
|
} else {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
response += s;
|
|
|
|
aRecordIterator->NextRecord();
|
|
|
|
}
|
|
|
|
aRecordIterator->Close();
|
|
|
|
FakeDecryptor::Message(response);
|
|
|
|
}
|
|
|
|
|
2014-10-23 07:37:55 +00:00
|
|
|
enum ShutdownMode {
|
|
|
|
ShutdownNormal,
|
|
|
|
ShutdownTimeout,
|
|
|
|
ShutdownStoreToken
|
|
|
|
};
|
|
|
|
|
|
|
|
static ShutdownMode sShutdownMode = ShutdownNormal;
|
|
|
|
static string sShutdownToken = "";
|
|
|
|
|
2014-10-21 06:16:19 +00:00
|
|
|
void
|
|
|
|
FakeDecryptor::UpdateSession(uint32_t aPromiseId,
|
|
|
|
const char* aSessionId,
|
|
|
|
uint32_t aSessionIdLength,
|
|
|
|
const uint8_t* aResponse,
|
|
|
|
uint32_t aResponseSize)
|
|
|
|
{
|
|
|
|
std::string response((const char*)aResponse, (const char*)(aResponse)+aResponseSize);
|
|
|
|
std::vector<std::string> tokens = Tokenize(response);
|
|
|
|
const string& task = tokens[0];
|
|
|
|
if (task == "test-storage") {
|
|
|
|
TestStorage();
|
|
|
|
} else if (task == "store") {
|
|
|
|
// send "stored record" message on complete.
|
|
|
|
const string& id = tokens[1];
|
|
|
|
const string& value = tokens[2];
|
|
|
|
WriteRecord(id,
|
|
|
|
value,
|
2014-12-03 12:34:00 +00:00
|
|
|
new ReportWritten(id, value),
|
|
|
|
new SendMessageTask("FAIL in writing record."));
|
2014-10-21 06:16:19 +00:00
|
|
|
} else if (task == "retrieve") {
|
|
|
|
const string& id = tokens[1];
|
|
|
|
ReadRecord(id, new ReportReadStatusContinuation(id));
|
2014-10-23 07:37:55 +00:00
|
|
|
} else if (task == "shutdown-mode") {
|
|
|
|
const string& mode = tokens[1];
|
|
|
|
if (mode == "timeout") {
|
|
|
|
sShutdownMode = ShutdownTimeout;
|
|
|
|
} else if (mode == "token") {
|
|
|
|
sShutdownMode = ShutdownStoreToken;
|
|
|
|
sShutdownToken = tokens[2];
|
|
|
|
Message("shutdown-token received " + sShutdownToken);
|
|
|
|
}
|
|
|
|
} else if (task == "retrieve-shutdown-token") {
|
|
|
|
ReadRecord("shutdown-token", new ReportReadRecordContinuation("shutdown-token"));
|
2014-11-03 10:02:30 +00:00
|
|
|
} else if (task == "test-op-apis") {
|
|
|
|
mozilla::gmptest::TestOuputProtectionAPIs();
|
2014-11-14 08:39:39 +00:00
|
|
|
} else if (task == "retrieve-plugin-voucher") {
|
|
|
|
const uint8_t* rawVoucher = nullptr;
|
|
|
|
uint32_t length = 0;
|
|
|
|
mHost->GetPluginVoucher(&rawVoucher, &length);
|
|
|
|
std::string voucher((const char*)rawVoucher, (const char*)(rawVoucher + length));
|
|
|
|
Message("retrieved plugin-voucher: " + voucher);
|
2014-11-20 23:25:12 +00:00
|
|
|
} else if (task == "retrieve-record-names") {
|
|
|
|
GMPEnumRecordNames(&RecvGMPRecordIterator, this);
|
2014-10-23 07:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CompleteShutdownTask : public GMPTask {
|
|
|
|
public:
|
2014-10-10 21:28:35 +00:00
|
|
|
explicit CompleteShutdownTask(GMPAsyncShutdownHost* aHost)
|
2014-10-23 07:37:55 +00:00
|
|
|
: mHost(aHost)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void Run() {
|
|
|
|
mHost->ShutdownComplete();
|
|
|
|
}
|
|
|
|
virtual void Destroy() { delete this; }
|
|
|
|
GMPAsyncShutdownHost* mHost;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
TestAsyncShutdown::BeginShutdown() {
|
|
|
|
switch (sShutdownMode) {
|
|
|
|
case ShutdownNormal:
|
|
|
|
mHost->ShutdownComplete();
|
|
|
|
break;
|
|
|
|
case ShutdownTimeout:
|
|
|
|
// Don't do anything; wait for timeout, Gecko should kill
|
|
|
|
// the plugin and recover.
|
|
|
|
break;
|
|
|
|
case ShutdownStoreToken:
|
|
|
|
// Store message, then shutdown.
|
|
|
|
WriteRecord("shutdown-token",
|
|
|
|
sShutdownToken,
|
2014-12-03 12:34:00 +00:00
|
|
|
new CompleteShutdownTask(mHost),
|
|
|
|
new SendMessageTask("FAIL writing shutdown-token."));
|
2014-10-23 07:37:55 +00:00
|
|
|
break;
|
2014-10-21 06:16:19 +00:00
|
|
|
}
|
|
|
|
}
|