2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2014-01-07 18:32:23 +00:00
|
|
|
|
2014-10-09 23:33:56 +00:00
|
|
|
#include "packager/media/base/test/fake_prng.h"
|
2014-01-07 18:32:23 +00:00
|
|
|
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
2015-07-22 23:40:45 +00:00
|
|
|
#include "packager/base/files/file_util.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/media/test/test_data_util.h"
|
2014-01-07 18:32:23 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
FILE* g_rand_source_fp = NULL;
|
|
|
|
|
|
|
|
const char kFakePrngDataFile[] = "fake_prng_data.bin";
|
|
|
|
|
|
|
|
// RAND_bytes and RAND_pseudorand implementation.
|
2016-08-02 17:41:03 +00:00
|
|
|
int FakeBytes(uint8_t* buf, size_t num) {
|
2014-01-07 18:32:23 +00:00
|
|
|
DCHECK(buf);
|
|
|
|
DCHECK(g_rand_source_fp);
|
|
|
|
|
2014-03-21 17:26:49 +00:00
|
|
|
if (fread(buf, 1, num, g_rand_source_fp) < static_cast<size_t>(num)) {
|
2014-01-07 18:32:23 +00:00
|
|
|
LOG(ERROR) << "Ran out of fake PRNG data";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const RAND_METHOD kFakeRandMethod = {NULL, // RAND_seed function.
|
|
|
|
FakeBytes, // RAND_bytes function.
|
|
|
|
NULL, // RAND_cleanup function.
|
|
|
|
NULL, // RAND_add function.
|
|
|
|
FakeBytes, // RAND_pseudorand function.
|
|
|
|
NULL}; // RAND_status function.
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-01-07 18:32:23 +00:00
|
|
|
namespace media {
|
|
|
|
namespace fake_prng {
|
|
|
|
|
|
|
|
bool StartFakePrng() {
|
|
|
|
if (g_rand_source_fp) {
|
|
|
|
LOG(ERROR) << "Fake PRNG already started.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open deterministic random data file and set the OpenSSL PRNG.
|
|
|
|
g_rand_source_fp =
|
2014-02-26 23:55:01 +00:00
|
|
|
base::OpenFile(GetTestDataFilePath(kFakePrngDataFile), "rb");
|
2014-01-07 18:32:23 +00:00
|
|
|
if (!g_rand_source_fp) {
|
|
|
|
LOG(ERROR) << "Cannot open " << kFakePrngDataFile;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
RAND_set_rand_method(&kFakeRandMethod);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StopFakePrng() {
|
|
|
|
if (g_rand_source_fp) {
|
2014-02-26 23:55:01 +00:00
|
|
|
base::CloseFile(g_rand_source_fp);
|
2014-01-07 18:32:23 +00:00
|
|
|
g_rand_source_fp = NULL;
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Fake PRNG not started.";
|
|
|
|
}
|
|
|
|
RAND_set_rand_method(RAND_SSLeay());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fake_prng
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|