mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
WIP added an encoding use case where the image is generated
This commit is contained in:
parent
044814f4ed
commit
100d7125aa
|
@ -8,4 +8,5 @@ TODO
|
|||
- include abseil flags for unit testing (done, should be changed in main.cc also)
|
||||
- improve tests (images, generating images etc.)
|
||||
- clear redundant includes
|
||||
- check if security policy can be stricter
|
||||
- check if security policy can be stricter
|
||||
- use addDirectoryAt instead of addDirectory
|
|
@ -50,6 +50,8 @@ bool cmp_images32(const std::string &f1, const std::string &f2) {
|
|||
return false;
|
||||
}
|
||||
|
||||
std::cout << "width height = " << width1 << " " << height1 << std::endl;
|
||||
|
||||
for (int i = 0; i < width1 * height1; ++i) {
|
||||
if (image1[i] != image2[i]) {
|
||||
std::cerr << "PIXELS DIFFER AT i = " << i << std::endl;
|
||||
|
|
|
@ -51,7 +51,7 @@ void decode_and_encode32(SapiLodepngSandbox &sandbox, LodepngApi &api,
|
|||
// address to which the previous variable points. To do that, we have to
|
||||
// transfer the data from the sandbox memory to this process's memory.
|
||||
sapi::v::RemotePtr remote_out_ptr(reinterpret_cast<void *>(image.GetValue()));
|
||||
sapi::v::Array<char> out_img(width.GetValue() * height.GetValue());
|
||||
sapi::v::Array<unsigned char> out_img(width.GetValue() * height.GetValue());
|
||||
out_img.SetRemote(remote_out_ptr.GetValue());
|
||||
|
||||
if (!sandbox.TransferFromSandboxee(&out_img).ok()) {
|
||||
|
@ -59,7 +59,7 @@ void decode_and_encode32(SapiLodepngSandbox &sandbox, LodepngApi &api,
|
|||
exit(1);
|
||||
}
|
||||
|
||||
// now the pixels are available at out_img.GetData()
|
||||
// now the values are available at out_img.GetData()
|
||||
|
||||
// when calling the encoding function, we need only an unsigned char *
|
||||
// (instead of **) so we can simply use the sapi::v::Array defined before
|
||||
|
@ -78,6 +78,37 @@ void decode_and_encode32(SapiLodepngSandbox &sandbox, LodepngApi &api,
|
|||
// which is why the solution in which data is transferred around is used.
|
||||
}
|
||||
|
||||
void test2(SapiLodepngSandbox &sandbox, LodepngApi &api, const std::string &images_path) {
|
||||
|
||||
srand(time(NULL)); // maybe use something else
|
||||
// int width = 1024, height = 1024;
|
||||
unsigned int width = 512, height = 512;
|
||||
unsigned char *image = (unsigned char*)malloc(width * height * 4);
|
||||
// for (int i = 0; i < width * height; ++i) {
|
||||
// image[i] = rand() % 256;
|
||||
// }
|
||||
|
||||
for(int y = 0; y < height; y++)
|
||||
for(int x = 0; x < width; x++) {
|
||||
image[4 * width * y + 4 * x + 0] = 255 * !(x & y);
|
||||
image[4 * width * y + 4 * x + 1] = x ^ y;
|
||||
image[4 * width * y + 4 * x + 2] = x | y;
|
||||
image[4 * width * y + 4 * x + 3] = 255;
|
||||
}
|
||||
|
||||
sapi::v::Array<unsigned char> image_(image, width * height);
|
||||
sapi::v::UInt width_(width), height_(height);
|
||||
std::string filename = images_path + "/out/generate_and_encode_one_step1.png";
|
||||
sapi::v::ConstCStr filename_(filename.c_str());
|
||||
|
||||
sandbox.Allocate(&image_).IgnoreError();
|
||||
sandbox.TransferToSandboxee(&image_).IgnoreError();
|
||||
|
||||
auto res = api.lodepng_encode32_file(filename_.PtrBefore(), image_.PtrBefore(), width_.GetValue(), height_.GetValue()).value();
|
||||
std::cout << "res = " << res << std::endl;
|
||||
free(image);
|
||||
}
|
||||
|
||||
// compares the pixels of the f1 and f2 png files.
|
||||
bool cmp_images32(SapiLodepngSandbox &sandbox, LodepngApi &api,
|
||||
const std::string &f1, const std::string &f2) {
|
||||
|
@ -144,8 +175,6 @@ bool cmp_images32(SapiLodepngSandbox &sandbox, LodepngApi &api,
|
|||
// equal, then encoding and decoding worked.
|
||||
void test1(SapiLodepngSandbox &sandbox, LodepngApi &api,
|
||||
const std::string &images_path) {
|
||||
std::cout << "test1" << std::endl;
|
||||
|
||||
std::string filename1 = images_path + "/test1.png";
|
||||
std::string filename2 = images_path + "/out/test1_1out.png";
|
||||
std::string filename3 = images_path + "/out/test1_2out.png";
|
||||
|
@ -186,6 +215,6 @@ int main(int argc, char *argv[]) {
|
|||
LodepngApi api(&sandbox);
|
||||
|
||||
test1(sandbox, api, images_path);
|
||||
|
||||
test2(sandbox, api, images_path);
|
||||
return 0;
|
||||
}
|
|
@ -21,6 +21,8 @@
|
|||
#include "lodepng_sapi.sapi.h"
|
||||
#include "sandbox.h"
|
||||
#include "sandboxed_api/util/flag.h"
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
// defining the flag does not work as intended (always has the default value)
|
||||
// ignore for now
|
||||
|
@ -29,10 +31,41 @@ ABSL_FLAG(string, images_path, std::filesystem::current_path().string(),
|
|||
|
||||
namespace {
|
||||
|
||||
// TODO find how to not use it like this.
|
||||
std::string images_path = "/usr/local/google/home/amedar/internship/sandboxed-api/oss-internship-2020/sapi_lodepng/test_images";
|
||||
|
||||
|
||||
TEST(addition, basic) {
|
||||
EXPECT_EQ(2, 1 + 1);
|
||||
// std::cout << "flag=" << std::string(absl::GetFlag(FLAGS_images_path))
|
||||
// << std::endl;
|
||||
}
|
||||
|
||||
TEST(initSandbox, basic) {
|
||||
SapiLodepngSandbox sandbox(images_path);
|
||||
ASSERT_TRUE(sandbox.Init().ok());
|
||||
}
|
||||
|
||||
TEST(encode32, generate_and_encode_one_step) {
|
||||
// randomly generate pixels of an image and encode it into a file
|
||||
SapiLodepngSandbox sandbox(images_path);
|
||||
ASSERT_TRUE(sandbox.Init().ok());
|
||||
LodepngApi api(&sandbox);
|
||||
|
||||
|
||||
srand(time(NULL)); // maybe use something else
|
||||
int width = 1024, height = 1024;
|
||||
unsigned char *image = (unsigned char*)malloc(width * height);
|
||||
for (int i = 0; i < width * height; ++i) {
|
||||
image[i] = rand() % 256;
|
||||
}
|
||||
|
||||
sapi::v::Array<unsigned char> image_(image, width * height);
|
||||
sapi::v::UInt width_(width), height_(height);
|
||||
std::string filename = images_path + " /out/generate_and_encode_one_step1.png";
|
||||
sapi::v::ConstCStr filename_(filename.c_str());
|
||||
ASSERT_TRUE(api.lodepng_encode32_file(filename_.PtrBefore(), image_.PtrBefore(), width_.GetValue(), height_.GetValue()).ok());
|
||||
|
||||
}
|
||||
|
||||
} // namespace
|
Binary file not shown.
After Width: | Height: | Size: 594 KiB |
Loading…
Reference in New Issue
Block a user