modified usage of sapi:✌️:Array

This commit is contained in:
root 2020-09-08 14:21:57 +00:00
parent 298aa457c1
commit ecbd169616
3 changed files with 46 additions and 18 deletions

View File

@ -24,7 +24,10 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) {
std::vector<uint8_t> image(GenerateValues());
// Encode the image.
sapi::v::Array<uint8_t> sapi_image(image.data(), kImgLen);
sapi::v::Array<uint8_t> sapi_image(kImgLen);
CHECK(std::copy(image.begin(), image.end(), sapi_image.GetData()))
<< "could not copy values";
sapi::v::ConstCStr sapi_filename("/output/out_generated1.png");
sapi::StatusOr<unsigned int> result = api.lodepng_encode32_file(
@ -59,14 +62,17 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) {
sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
sapi_pixels.SetRemote(sapi_image_ptr.GetValue());
CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok()) << "Error during transfer from sandboxee";
CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok())
<< "Error during transfer from sandboxee";
// Now, we can compare the values.
CHECK(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(),
sapi_pixels.GetData() + kImgLen)) << "Values differ";
sapi_pixels.GetData() + kImgLen))
<< "Values differ";
// Free the memory allocated inside the sandbox.
CHECK(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()).ok()) << "Could not free memory inside sandboxed process";
CHECK(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()).ok())
<< "Could not free memory inside sandboxed process";
}
void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
@ -74,7 +80,10 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
std::vector<uint8_t> image(GenerateValues());
// Encode the image into memory first.
sapi::v::Array<uint8_t> sapi_image(image.data(), kImgLen);
sapi::v::Array<uint8_t> sapi_image(kImgLen);
CHECK(std::copy(image.begin(), image.end(), sapi_image.GetData()))
<< "could not copy values";
sapi::v::ConstCStr sapi_filename("/output/out_generated2.png");
sapi::v::ULLong sapi_pngsize;
@ -94,7 +103,8 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
sapi::v::Array<uint8_t> sapi_png_array(sapi_pngsize.GetValue());
sapi_png_array.SetRemote(sapi_png_ptr.GetValue());
CHECK(sandbox.TransferFromSandboxee(&sapi_png_array).ok()) << "Error during transfer from sandboxee";
CHECK(sandbox.TransferFromSandboxee(&sapi_png_array).ok())
<< "Error during transfer from sandboxee";
// Write the image into the file (from memory).
result =
@ -117,13 +127,15 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
CHECK(result.ok()) << "load_file call failed";
CHECK(!result.value()) << "Unexpected result from load_file call";
CHECK(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue()) << "Png sizes differ";
CHECK(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue())
<< "Png sizes differ";
// Transfer the png array.
sapi::v::Array<uint8_t> sapi_png_array2(sapi_pngsize2.GetValue());
sapi_png_array2.SetRemote(sapi_png_ptr2.GetValue());
CHECK(sandbox.TransferFromSandboxee(&sapi_png_array2).ok()) << "Error during transfer from sandboxee";
CHECK(sandbox.TransferFromSandboxee(&sapi_png_array2).ok())
<< "Error during transfer from sandboxee";
// After the file is loaded, decode it so we have access to the values
// directly.
@ -142,16 +154,21 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
sapi_pixels.SetRemote(sapi_png_ptr3.GetValue());
CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok()) << "Error during transfer from sandboxee";
CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok())
<< "Error during transfer from sandboxee";
// Compare the values.
CHECK(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(),
sapi_pixels.GetData() + kImgLen)) << "Values differ";
sapi_pixels.GetData() + kImgLen))
<< "Values differ";
// Free the memory allocated inside the sandbox.
CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr.GetValue()).ok()) << "Could not free memory inside sandboxed process";
CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr2.GetValue()).ok()) << "Could not free memory inside sandboxed process";
CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr3.GetValue()).ok()) << "Could not free memory inside sandboxed process";
CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr.GetValue()).ok())
<< "Could not free memory inside sandboxed process";
CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr2.GetValue()).ok())
<< "Could not free memory inside sandboxed process";
CHECK(sandbox.GetRpcChannel()->Free(sapi_png_ptr3.GetValue()).ok())
<< "Could not free memory inside sandboxed process";
}
int main(int argc, char *argv[]) {

View File

@ -67,7 +67,11 @@ TEST(LodePngTest, EncodeDecodeOneStep) {
std::vector<uint8_t> image(GenerateValues());
sapi::v::Array<uint8_t> sapi_image(image.data(), kImgLen);
sapi::v::Array<uint8_t> sapi_image(kImgLen);
EXPECT_THAT(std::copy(image.begin(), image.end(), sapi_image.GetData()),
IsTrue())
<< "could not copy values";
sapi::v::ConstCStr sapi_filename("/output/out_generated1.png");
SAPI_ASSERT_OK_AND_ASSIGN(
@ -101,7 +105,8 @@ TEST(LodePngTest, EncodeDecodeOneStep) {
IsTrue())
<< "Values differ";
EXPECT_THAT(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()), IsOk()) << "Could not free memory inside sandboxed process";
EXPECT_THAT(sandbox.GetRpcChannel()->Free(sapi_image_ptr.GetValue()), IsOk())
<< "Could not free memory inside sandboxed process";
EXPECT_THAT(sandbox2::file_util::fileops::DeleteRecursively(images_path),
IsTrue())
@ -123,7 +128,11 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) {
std::vector<uint8_t> image(GenerateValues());
sapi::v::Array<uint8_t> sapi_image(image.data(), kImgLen);
sapi::v::Array<uint8_t> sapi_image(kImgLen);
EXPECT_THAT(std::copy(image.begin(), image.end(), sapi_image.GetData()),
IsTrue())
<< "could not copy values";
sapi::v::ConstCStr sapi_filename("/output/out_generated2.png");
sapi::v::ULLong sapi_pngsize;

View File

@ -44,7 +44,8 @@ void EncodeDecodeOneStep(const std::string &images_path) {
CHECK(height2 == kHeight) << "Heights differ";
// Now, we can compare the values.
CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen)) << "Values differ";
CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen))
<< "Values differ";
free(image2);
}
@ -87,7 +88,8 @@ void EncodeDecodeTwoSteps(const std::string &images_path) {
CHECK(height2 == kHeight) << "Heights differ";
// Compare the values.
CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen)) << "Values differ";
CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen))
<< "Values differ";
free(png);
free(png2);