Improve OEMCrypto unit tests

This change updates the unit tests to have more comments so that it is
more clear what went wrong if a test fails.

Also, some utility code has been changed to make it easier to support
new platforms and read/write locks.

Also, the reference code has had some refactoring added to make it
easier for Widevine to test CDM code.  There should be no
functionality differences in the reference code.

Also, in the main API doc, there was an obsolete paragraph in the
description of the threading model.  This paragraph has been removed.
This commit is contained in:
Fred Gylys-Colwell
2019-03-15 13:18:26 -07:00
parent e7d6da8d24
commit 88d6b53ba3
31 changed files with 686 additions and 283 deletions

12
util/src/dllmain.cpp Normal file
View File

@@ -0,0 +1,12 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine Master
// License Agreement.
//
// dllmain - A dummy DllMain method for Windows DLLs.
//
#include <windows.h>
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
LPVOID lpReserved) {
return TRUE;
}

21
util/src/platform.cpp Normal file
View File

@@ -0,0 +1,21 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine Master
// License Agreement.
#include "platform.h"
#include "stdlib.h"
#ifdef _WIN32
int setenv(const char* key, const char* value, int overwrite) {
if (!overwrite) {
size_t size;
errno_t err = getenv_s(&size, nullptr, 0, key);
if (err != 0 || size != 0)
return err; // Return 0 if it exists, but don't change.
}
return _putenv_s(key, value);
}
#endif

60
util/src/rw_lock.cpp Normal file
View File

@@ -0,0 +1,60 @@
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine Master
// License Agreement.
#include "rw_lock.h"
#include "log.h"
namespace wvcdm {
shared_mutex::~shared_mutex() {
if (reader_count_ > 0) {
LOGE("shared_mutex destroyed with active readers!");
}
if (has_writer_) {
LOGE("shared_mutex destroyed with an active writer!");
}
}
void shared_mutex::lock_shared() {
std::unique_lock<std::mutex> lock(mutex_);
while (has_writer_) {
condition_variable_.wait(lock);
}
++reader_count_;
}
void shared_mutex::unlock_shared() {
std::unique_lock<std::mutex> lock(mutex_);
--reader_count_;
if (reader_count_ == 0) {
condition_variable_.notify_all();
}
}
bool shared_mutex::lock_implementation(bool abort_if_unavailable) {
std::unique_lock<std::mutex> lock(mutex_);
while (reader_count_ > 0 || has_writer_) {
if (abort_if_unavailable) return false;
condition_variable_.wait(lock);
}
has_writer_ = true;
return true;
}
void shared_mutex::unlock() {
std::unique_lock<std::mutex> lock(mutex_);
has_writer_ = false;
condition_variable_.notify_all();
}
} // namespace wvcdm

View File

@@ -4,7 +4,6 @@
#include "string_conversions.h"
#include <arpa/inet.h>
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
@@ -13,6 +12,7 @@
#include <vector>
#include "log.h"
#include "platform.h"
namespace wvcdm {