[ Merge of http://go/wvgerrit/209590 ] The filestore unit tests have not been updated in a while, and contained several test statements which could crash the test when failed (accessing elements in a vector or characters in a string without proper size checks). Other parts of the tests had non-obvious purposes without detailed knowledge of how the file system works on the different platforms. Significant parts of the tests have been refactored to include better checking and to add error messages to explain the expectations. Several of the tests have been documented, and the FileSystem header has been updated to explain what the API does. Bug: 376533901 Test: file_store_unittest on Oriole Change-Id: I5af9fd2a2ed01aa6186026761c9e0814604ec610
143 lines
5.2 KiB
C++
143 lines
5.2 KiB
C++
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
|
// source code may only be used and distributed under the Widevine License
|
|
// Agreement.
|
|
//
|
|
// File - Platform independent interface for a File class
|
|
//
|
|
#ifndef WVCDM_UTIL_FILE_STORE_H_
|
|
#define WVCDM_UTIL_FILE_STORE_H_
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "disallow_copy_and_assign.h"
|
|
#include "platform.h"
|
|
#include "util_common.h"
|
|
|
|
namespace wvutil {
|
|
|
|
// Fixed filename for ATSC DRM certificate pre-installed
|
|
// on ATSC devices for ATSC licenses.
|
|
static const std::string kAtscCertificateFileName = "atsccert.bin";
|
|
// General filename for either global or unmapped app-origin
|
|
// DRM certificates.
|
|
static const std::string kCertificateFileName = "cert1.bin";
|
|
// File extension for DRM and OEM certificate files.
|
|
static const std::string kCertificateFileNameExt = ".bin";
|
|
// Filename prefix for mapped (scoped) DRM certificate filenames
|
|
// specific to a particular app-origin.
|
|
static const std::string kScopedCertificateFilenamePrefix = "cert1_";
|
|
// TODO(b/376533901): Replace this constant with
|
|
// kScopedCertificateFilenamePrefix in source code..
|
|
static const std::string kCertificateFileNamePrefix =
|
|
kScopedCertificateFilenamePrefix;
|
|
// Legacy general filename for either global or unmapped app-origin
|
|
// DRM certificates.
|
|
static const std::string kLegacyCertificateFileName = "cert.bin";
|
|
// Legacy filename prefix for mapped (scoped) DRM certificate filenames
|
|
// specific to a particular app-origin.
|
|
static const std::string kLegacyScopedCertificateFilenamePrefix = "cert";
|
|
// TODO(b/376533901): Replace this constant with
|
|
// kLegacyScopedCertificateFilenamePrefix in source code..
|
|
static const std::string kLegacyCertificateFileNamePrefix =
|
|
kLegacyScopedCertificateFilenamePrefix;
|
|
// Filename for global OEM certificates.
|
|
static const std::string kOemCertificateFileName = "oemcert.bin";
|
|
|
|
// File interface. The implementation is platform dependent.
|
|
class File {
|
|
public:
|
|
File() {}
|
|
virtual ~File() {}
|
|
virtual ssize_t Read(char* buffer, size_t bytes) = 0;
|
|
virtual ssize_t Write(const char* buffer, size_t bytes) = 0;
|
|
|
|
CORE_DISALLOW_COPY_AND_ASSIGN(File);
|
|
};
|
|
|
|
// File system base class. The implementation is platform dependent.
|
|
class FileSystem {
|
|
public:
|
|
FileSystem();
|
|
FileSystem(const std::string& origin, void* extra_data);
|
|
virtual ~FileSystem();
|
|
|
|
// Concreate implementation of FileSystem.
|
|
// Depending on the platform, this may be vendor or Widevine implemented.
|
|
class Impl;
|
|
|
|
// Flags for calls to Open.
|
|
static constexpr int kNoFlags = 0;
|
|
// Create file if does not already exist, open file if it does exist.
|
|
static constexpr int kCreate = (1 << 0);
|
|
// Open file as read-only; typically should not be used with kCreate.
|
|
static constexpr int kReadOnly = (1 << 1);
|
|
// Open file and truncated. May be used with kCreate; should not
|
|
// be used with kReadOnly.
|
|
static constexpr int kTruncate = (1 << 2);
|
|
|
|
virtual std::unique_ptr<File> Open(const std::string& file_path, int flags);
|
|
|
|
// Checks if the |path| exists. The |path| may be a file or directory.
|
|
// Return true if an entry in the file system exists; false otherwise.
|
|
virtual bool Exists(const std::string& path);
|
|
// Same as above, except the optional parameter of |errno_value| should
|
|
// be set to 0 or the value of C errno when attempting to check
|
|
// the existence of a file.
|
|
virtual bool Exists(const std::string& path, int* errno_value);
|
|
|
|
// Removes the specified |path|.
|
|
//
|
|
// If |path| is a regular file, the file should be removed.
|
|
// If |path| is a directory, both the directory and the directory
|
|
// contents should be removed.
|
|
//
|
|
// Implementation must support a |path| containing a single wildcard
|
|
// character in the filename component of the path.
|
|
//
|
|
// Return value:
|
|
// - true : File/directory was removed, or file/directory did not exist
|
|
// - false : File/directory could not be removed, or other error.
|
|
virtual bool Remove(const std::string& path);
|
|
|
|
// Obtain the size of a file in bytes. |file_path| must be a file,
|
|
// and not a directory.
|
|
//
|
|
// Return value:
|
|
// - non-negative : size of file in bytes if file exists
|
|
// - negative : file does not exist, or error occurred.
|
|
virtual ssize_t FileSize(const std::string& file_path);
|
|
|
|
// Return the entries stored at |dir_path| (includes both files
|
|
// and directories).
|
|
//
|
|
// Return value:
|
|
// - true : Directory exists, and directory entry names are stored
|
|
// in |names|; |names| may be empty if directory was empty.
|
|
// - false : Directory does not exist, |dir_path| is not a directory,
|
|
// or error was encountered.
|
|
virtual bool List(const std::string& dir_path,
|
|
std::vector<std::string>* names);
|
|
|
|
const std::string& origin() const { return origin_; }
|
|
void set_origin(const std::string& origin);
|
|
|
|
const std::string& identifier() const { return identifier_; }
|
|
void set_identifier(const std::string& identifier);
|
|
bool IsGlobal() const { return identifier_.empty(); }
|
|
|
|
private:
|
|
std::unique_ptr<FileSystem::Impl> impl_;
|
|
std::string origin_;
|
|
std::string identifier_;
|
|
|
|
CORE_DISALLOW_COPY_AND_ASSIGN(FileSystem);
|
|
};
|
|
|
|
} // namespace wvutil
|
|
|
|
#endif // WVCDM_UTIL_FILE_STORE_H_
|