Cleaned up file_store_unittest.cpp
[ 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
This commit is contained in:
@@ -7,7 +7,8 @@
|
||||
#ifndef WVCDM_UTIL_FILE_STORE_H_
|
||||
#define WVCDM_UTIL_FILE_STORE_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <stddef.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -18,16 +19,35 @@
|
||||
|
||||
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";
|
||||
static const std::string kCertificateFileNamePrefix = "cert1_";
|
||||
// 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";
|
||||
static const std::string kLegacyCertificateFileNamePrefix = "cert";
|
||||
// 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";
|
||||
static const std::string kOemCertificateFileNamePrefix = "oemcert_";
|
||||
|
||||
// File class. The implementation is platform dependent.
|
||||
// File interface. The implementation is platform dependent.
|
||||
class File {
|
||||
public:
|
||||
File() {}
|
||||
@@ -35,35 +55,70 @@ class File {
|
||||
virtual ssize_t Read(char* buffer, size_t bytes) = 0;
|
||||
virtual ssize_t Write(const char* buffer, size_t bytes) = 0;
|
||||
|
||||
friend class FileSystem;
|
||||
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;
|
||||
|
||||
// defines as bit flag
|
||||
enum OpenFlags {
|
||||
kNoFlags = 0,
|
||||
kCreate = 1,
|
||||
kReadOnly = 2, // defaults to read and write access
|
||||
kTruncate = 4
|
||||
};
|
||||
// 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);
|
||||
|
||||
virtual bool Exists(const std::string& file_path);
|
||||
virtual bool Exists(const std::string& file_path, int* errno_value);
|
||||
virtual bool Remove(const std::string& file_path);
|
||||
// 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 filenames stored at dir_path.
|
||||
// dir_path will be stripped from the returned names.
|
||||
// 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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user