This source file includes following definitions.
- ReadEntireFile
- WriteEntireBuffer
- MapAndCheckResults
- Init
- RunTests
- TestBadParameters
- TestMap
- TestPartialRegions
#include "ppapi/tests/test_file_mapping.h"
#include <string.h>
#include <limits>
#include <string>
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/ppb_file_io.h"
#include "ppapi/c/ppb_file_mapping.h"
#include "ppapi/cpp/file_io.h"
#include "ppapi/cpp/file_ref.h"
#include "ppapi/cpp/file_system.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/tests/test_utils.h"
REGISTER_TEST_CASE(FileMapping);
namespace {
int32_t ReadEntireFile(PP_Instance instance,
pp::FileIO* file_io,
int32_t offset,
std::string* data,
CallbackType callback_type) {
TestCompletionCallback callback(instance, callback_type);
char buf[256];
int32_t read_offset = offset;
for (;;) {
callback.WaitForResult(
file_io->Read(read_offset, buf, sizeof(buf), callback.GetCallback()));
if (callback.result() < 0)
return callback.result();
if (callback.result() == 0)
break;
read_offset += callback.result();
data->append(buf, callback.result());
}
return PP_OK;
}
int32_t WriteEntireBuffer(PP_Instance instance,
pp::FileIO* file_io,
int32_t offset,
const std::string& data,
CallbackType callback_type) {
TestCompletionCallback callback(instance, callback_type);
int32_t write_offset = offset;
const char* buf = data.c_str();
int32_t size = data.size();
while (write_offset < offset + size) {
callback.WaitForResult(file_io->Write(write_offset,
&buf[write_offset - offset],
size - write_offset + offset,
callback.GetCallback()));
if (callback.result() < 0)
return callback.result();
if (callback.result() == 0)
return PP_ERROR_FAILED;
write_offset += callback.result();
}
callback.WaitForResult(file_io->Flush(callback.GetCallback()));
return callback.result();
}
}
std::string TestFileMapping::MapAndCheckResults(uint32_t prot,
uint32_t flags) {
TestCompletionCallback callback(instance_->pp_instance(), callback_type());
pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
pp::FileRef file_ref(file_system, "/mapped_file");
callback.WaitForResult(file_system.Open(1024, callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
const int64_t page_size =
file_mapping_if_->GetMapPageSize(instance_->pp_instance());
const int64_t kNumPages = 4;
std::string file_contents((page_size * (kNumPages - 1)) + 128, 'a');
pp::FileIO file_io(instance_);
callback.WaitForResult(file_io.Open(file_ref,
PP_FILEOPENFLAG_CREATE |
PP_FILEOPENFLAG_TRUNCATE |
PP_FILEOPENFLAG_READ |
PP_FILEOPENFLAG_WRITE,
callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
&file_io,
0,
file_contents,
callback_type()));
void* address = NULL;
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
kNumPages * page_size,
prot,
flags,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
ASSERT_NE(NULL, address);
if (prot & PP_FILEMAPPROTECTION_READ) {
std::string mapped_data(static_cast<char*>(address), file_contents.size());
ASSERT_EQ(file_contents, mapped_data);
const std::string file_contents2(file_contents.size(), 'x');
ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
&file_io,
0,
file_contents2,
callback_type()));
std::string mapped_data2(static_cast<char*>(address), file_contents.size());
if (flags & PP_FILEMAPFLAG_SHARED)
ASSERT_EQ(file_contents2, mapped_data2);
}
if (prot & PP_FILEMAPPROTECTION_WRITE) {
std::string old_file_contents;
ASSERT_EQ(PP_OK, ReadEntireFile(instance_->pp_instance(),
&file_io,
0,
&old_file_contents,
callback_type()));
memset(address, 'y', file_contents.size());
std::string mapped_data3(file_contents.size(), 'y');
callback.WaitForResult(
file_mapping_if_->Unmap(
instance_->pp_instance(), address, file_contents.size(),
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
std::string new_file_contents;
ASSERT_EQ(PP_OK, ReadEntireFile(instance_->pp_instance(),
&file_io,
0,
&new_file_contents,
callback_type()));
ASSERT_NE(mapped_data3, old_file_contents);
if (flags & PP_FILEMAPFLAG_SHARED)
ASSERT_EQ(mapped_data3, new_file_contents);
else
ASSERT_EQ(old_file_contents, new_file_contents);
} else {
callback.WaitForResult(
file_mapping_if_->Unmap(
instance_->pp_instance(), address, file_contents.size(),
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
}
PASS();
}
bool TestFileMapping::Init() {
file_mapping_if_ = static_cast<const PPB_FileMapping_0_1*>(
pp::Module::Get()->GetBrowserInterface(PPB_FILEMAPPING_INTERFACE_0_1));
return !!file_mapping_if_ && CheckTestingInterface() &&
EnsureRunningOverHTTP();
}
void TestFileMapping::RunTests(const std::string& filter) {
RUN_CALLBACK_TEST(TestFileMapping, BadParameters, filter);
RUN_CALLBACK_TEST(TestFileMapping, Map, filter);
RUN_CALLBACK_TEST(TestFileMapping, PartialRegions, filter);
}
std::string TestFileMapping::TestBadParameters() {
TestCompletionCallback callback(instance_->pp_instance(), callback_type());
pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
pp::FileRef file_ref(file_system, "/mapped_file");
callback.WaitForResult(file_system.Open(1024, callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
const int64_t page_size =
file_mapping_if_->GetMapPageSize(instance_->pp_instance());
std::string file_contents(page_size, 'a');
pp::FileIO file_io(instance_);
callback.WaitForResult(file_io.Open(file_ref,
PP_FILEOPENFLAG_CREATE |
PP_FILEOPENFLAG_TRUNCATE |
PP_FILEOPENFLAG_READ |
PP_FILEOPENFLAG_WRITE,
callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
&file_io,
0,
file_contents,
callback_type()));
void* address = NULL;
callback.WaitForResult(
file_mapping_if_->Map(
PP_Instance(0xbadbad),
file_io.pp_resource(),
page_size,
PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
PP_Resource(0xbadbad),
page_size,
PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
std::numeric_limits<int64_t>::max(),
PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_NOMEMORY, callback.result());
ASSERT_EQ(NULL, address);
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
-1,
PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
page_size,
~PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
page_size,
PP_FILEMAPPROTECTION_READ,
0,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
page_size,
PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_SHARED | PP_FILEMAPFLAG_PRIVATE,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
page_size,
PP_FILEMAPPROTECTION_READ,
~PP_FILEMAPFLAG_SHARED,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
page_size,
PP_FILEMAPPROTECTION_READ,
~PP_FILEMAPFLAG_SHARED,
1,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
callback.WaitForResult(
file_mapping_if_->Unmap(
instance_->pp_instance(),
NULL,
page_size,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
callback.WaitForResult(
file_mapping_if_->Unmap(
instance_->pp_instance(),
reinterpret_cast<const void*>(0xdeadbeef),
page_size,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
PASS();
}
std::string TestFileMapping::TestMap() {
ASSERT_SUBTEST_SUCCESS(MapAndCheckResults(PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_SHARED));
ASSERT_SUBTEST_SUCCESS(MapAndCheckResults(PP_FILEMAPPROTECTION_WRITE,
PP_FILEMAPFLAG_SHARED));
ASSERT_SUBTEST_SUCCESS(
MapAndCheckResults(PP_FILEMAPPROTECTION_WRITE | PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_SHARED));
ASSERT_SUBTEST_SUCCESS(MapAndCheckResults(PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE));
ASSERT_SUBTEST_SUCCESS(MapAndCheckResults(PP_FILEMAPPROTECTION_WRITE,
PP_FILEMAPFLAG_PRIVATE));
ASSERT_SUBTEST_SUCCESS(
MapAndCheckResults(PP_FILEMAPPROTECTION_WRITE | PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE));
PASS();
}
std::string TestFileMapping::TestPartialRegions() {
TestCompletionCallback callback(instance_->pp_instance(), callback_type());
pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
pp::FileRef file_ref1(file_system, "/mapped_file1");
pp::FileRef file_ref2(file_system, "/mapped_file2");
callback.WaitForResult(file_system.Open(1024, callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
const int64_t page_size =
file_mapping_if_->GetMapPageSize(instance_->pp_instance());
const int64_t kNumPages = 3;
std::string file_contents1(kNumPages * page_size, 'a');
pp::FileIO file_io1(instance_);
callback.WaitForResult(file_io1.Open(file_ref1,
PP_FILEOPENFLAG_CREATE |
PP_FILEOPENFLAG_TRUNCATE |
PP_FILEOPENFLAG_READ |
PP_FILEOPENFLAG_WRITE,
callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
&file_io1,
0,
file_contents1,
callback_type()));
void* address = NULL;
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io1.pp_resource(),
kNumPages * page_size,
PP_FILEMAPPROTECTION_WRITE | PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_SHARED,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
ASSERT_NE(NULL, address);
void* address_of_middle_page =
reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(address) + page_size);
callback.WaitForResult(
file_mapping_if_->Unmap(
instance_->pp_instance(),
address_of_middle_page,
page_size,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
pp::FileIO file_io2(instance_);
callback.WaitForResult(file_io2.Open(file_ref2,
PP_FILEOPENFLAG_CREATE |
PP_FILEOPENFLAG_TRUNCATE |
PP_FILEOPENFLAG_READ |
PP_FILEOPENFLAG_WRITE,
callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
std::string file_contents2(page_size, 'b');
ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
&file_io2,
0,
file_contents2,
callback_type()));
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io2.pp_resource(),
page_size,
PP_FILEMAPPROTECTION_WRITE | PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_SHARED | PP_FILEMAPFLAG_FIXED,
0,
&address_of_middle_page,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
memset(address, 'c', kNumPages * page_size);
callback.WaitForResult(
file_mapping_if_->Unmap(
instance_->pp_instance(), address, kNumPages * page_size,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
std::string expected_file_contents1 = std::string(page_size, 'c') +
std::string(page_size, 'a') +
std::string(page_size, 'c');
std::string new_file_contents1;
ASSERT_EQ(PP_OK, ReadEntireFile(instance_->pp_instance(),
&file_io1,
0,
&new_file_contents1,
callback_type()));
ASSERT_EQ(expected_file_contents1, new_file_contents1);
std::string expected_file_contents2 = std::string(page_size, 'c');
std::string new_file_contents2;
ASSERT_EQ(PP_OK, ReadEntireFile(instance_->pp_instance(),
&file_io2,
0,
&new_file_contents2,
callback_type()));
ASSERT_EQ(expected_file_contents2, new_file_contents2);
PASS();
}