Codestyle update

This commit is contained in:
Bohdan Tyshchenko 2020-08-12 14:09:40 -07:00
parent 6f7110dd4f
commit 47fd491e20
11 changed files with 253 additions and 101 deletions

View File

@ -1,3 +1,17 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load(
"@com_google_sandboxed_api//sandboxed_api/bazel:sapi.bzl",
"sapi_library",

View File

View File

@ -1,3 +1,25 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <sys/stat.h>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include "guetzli/jpeg_data_reader.h"
#include "guetzli/quality.h"
#include "guetzli_entry_points.h"
@ -5,17 +27,10 @@
#include "sandboxed_api/sandbox2/util/fileops.h"
#include "sandboxed_api/util/statusor.h"
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <sys/stat.h>
#include <vector>
namespace {
constexpr int kBytesPerPixel = 350;
constexpr int kLowestMemusageMB = 100; // in MB
constexpr int kLowestMemusageMB = 100;
struct GuetzliInitData {
std::string in_data;
@ -26,7 +41,7 @@ struct GuetzliInitData {
template<typename T>
void CopyMemoryToLenVal(const T* data, size_t size,
sapi::LenValStruct* out_data) {
free(out_data->data); // Not sure about this
free(out_data->data); // Not sure about this
out_data->size = size;
T* new_out = static_cast<T*>(malloc(size));
memcpy(new_out, data, size);
@ -49,7 +64,6 @@ sapi::StatusOr<std::string> ReadFromFd(int fd) {
status = read(fd, buf.get(), fsize);
if (status < 0) {
lseek(fd, 0, SEEK_SET);
return absl::FailedPreconditionError(
"Error reading input from fd"
);

View File

@ -1,4 +1,19 @@
#pragma once
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GUETZLI_SANDBOXED_GUETZLI_ENTRY_POINTS_H_
#define GUETZLI_SANDBOXED_GUETZLI_ENTRY_POINTS_H_
#include "guetzli/processor.h"
#include "sandboxed_api/lenval_core.h"
@ -16,3 +31,5 @@ extern "C" bool ProcessJpeg(const ProcessingParams* processing_params,
extern "C" bool ProcessRgb(const ProcessingParams* processing_params,
sapi::LenValStruct* output);
extern "C" bool WriteDataToFd(int fd, sapi::LenValStruct* data);
#endif // GUETZLI_SANDBOXED_GUETZLI_ENTRY_POINTS_H_

View File

@ -1,13 +1,29 @@
#pragma once
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GUETZLI_SANDBOXED_GUETZLI_SANDBOX_H_
#define GUETZLI_SANDBOXED_GUETZLI_SANDBOX_H_
#include <libgen.h>
#include <syscall.h>
#include "guetzli_sapi.sapi.h"
#include "sandboxed_api/sandbox2/policy.h"
#include "sandboxed_api/sandbox2/policybuilder.h"
#include "sandboxed_api/util/flag.h"
#include "guetzli_sapi.sapi.h"
namespace guetzli {
namespace sandbox {
@ -32,5 +48,7 @@ class GuetzliSapiSandbox : public GuetzliSandbox {
}
};
} // namespace sandbox
} // namespace guetzli
} // namespace sandbox
} // namespace guetzli
#endif // GUETZLI_SANDBOXED_GUETZLI_SANDBOX_H_

View File

@ -1,19 +1,34 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <iostream>
#include "guetzli_transaction.h"
#include "sandboxed_api/sandbox2/util/fileops.h"
#include "sandboxed_api/util/statusor.h"
#include "guetzli_transaction.h"
namespace {
constexpr int kDefaultJPEGQuality = 95;
constexpr int kDefaultMemlimitMB = 6000; // in MB
constexpr int kDefaultMemlimitMB = 6000;
void TerminateHandler() {
fprintf(stderr, "Unhandled exception. Most likely insufficient memory available.\n"
@ -124,7 +139,7 @@ int main(int argc, const char** argv) {
}
}
else {
fprintf(stderr, "%s\n", result.ToString().c_str()); // Use cerr instead ?
fprintf(stderr, "%s\n", result.ToString().c_str()); // Use cerr instead ?
return 1;
}

View File

@ -1,3 +1,17 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "guetzli_transaction.h"
#include <iostream>
@ -45,8 +59,8 @@ absl::Status GuetzliTransaction::Init() {
"Error receiving remote FD: remote output fd is set to -1");
}
in_fd_.OwnLocalFd(false); // FDCloser will close local fd
out_fd_.OwnLocalFd(false); // FDCloser will close local fd
in_fd_.OwnLocalFd(false); // FDCloser will close local fd
out_fd_.OwnLocalFd(false); // FDCloser will close local fd
return absl::OkStatus();
}
@ -62,14 +76,14 @@ absl::Status GuetzliTransaction::Main() {
params_.memlimit_mb
};
auto result_status = image_type_ == ImageType::JPEG ?
auto result_status = image_type_ == ImageType::kJpeg ?
api.ProcessJpeg(processing_params.PtrBefore(), output.PtrBoth()) :
api.ProcessRgb(processing_params.PtrBefore(), output.PtrBoth());
if (!result_status.value_or(false)) {
std::stringstream error_stream;
error_stream << "Error processing "
<< (image_type_ == ImageType::JPEG ? "jpeg" : "rgb") << " data"
<< (image_type_ == ImageType::kJpeg ? "jpeg" : "rgb") << " data"
<< std::endl;
return absl::FailedPreconditionError(
@ -113,8 +127,8 @@ sapi::StatusOr<ImageType> GuetzliTransaction::GetImageTypeFromFd(int fd) const {
}
return memcmp(read_buf, kPNGMagicBytes, sizeof(kPNGMagicBytes)) == 0 ?
ImageType::PNG : ImageType::JPEG;
ImageType::kPng : ImageType::kJpeg;
}
} // namespace sandbox
} // namespace guetzli
} // namespace sandbox
} // namespace guetzli

View File

@ -1,21 +1,34 @@
#pragma once
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GUETZLI_SANDBOXED_GUETZLI_TRANSACTION_H_
#define GUETZLI_SANDBOXED_GUETZLI_TRANSACTION_H_
#include <libgen.h>
#include <syscall.h>
#include "guetzli_sandbox.h"
#include "sandboxed_api/transaction.h"
#include "sandboxed_api/vars.h"
#include "guetzli_sandbox.h"
namespace guetzli {
namespace sandbox {
constexpr int kDefaultTransactionRetryCount = 0;
constexpr uint64_t kMpixPixels = 1'000'000;
enum class ImageType {
JPEG,
PNG
kJpeg,
kPng
};
struct TransactionParams {
@ -55,8 +68,13 @@ class GuetzliTransaction : public sapi::Transaction {
const TransactionParams params_;
sapi::v::Fd in_fd_;
sapi::v::Fd out_fd_;
ImageType image_type_ = ImageType::JPEG;
ImageType image_type_ = ImageType::kJpeg;
static const int kDefaultTransactionRetryCount = 0;
static const uint64_t kMpixPixels = 1'000'000;
};
} // namespace sandbox
} // namespace guetzli
} // namespace sandbox
} // namespace guetzli
#endif // GUETZLI_SANDBOXED_GUETZLI_TRANSACTION_H_

View File

@ -1,3 +1,17 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cc_test(
name = "transaction_tests",
srcs = ["guetzli_transaction_test.cc"],

View File

@ -1,16 +1,32 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syscall.h>
#include <algorithm>
#include <fstream>
#include <memory>
#include <sstream>
#include "gtest/gtest.h"
#include "guetzli_sandbox.h"
#include "sandboxed_api/sandbox2/util/fileops.h"
#include "sandboxed_api/vars.h"
#include <fstream>
#include <memory>
#include <syscall.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sstream>
#include <algorithm>
#include "guetzli_sandbox.h"
namespace guetzli {
namespace sandbox {
@ -18,23 +34,23 @@ namespace tests {
namespace {
constexpr const char* IN_PNG_FILENAME = "bees.png";
constexpr const char* IN_JPG_FILENAME = "nature.jpg";
constexpr const char* PNG_REFERENCE_FILENAME = "bees_reference.jpg";
constexpr const char* JPG_REFERENCE_FILENAME = "nature_reference.jpg";
constexpr const char* kInPngFilename = "bees.png";
constexpr const char* kInJpegFilename = "nature.jpg";
constexpr const char* kPngReferenceFilename = "bees_reference.jpg";
constexpr const char* kJpegReferenceFIlename = "nature_reference.jpg";
constexpr int PNG_EXPECTED_SIZE = 38'625;
constexpr int JPG_EXPECTED_SIZE = 10'816;
constexpr int kPngExpectedSize = 38'625;
constexpr int kJpegExpectedSize = 10'816;
constexpr int DEFAULT_QUALITY_TARGET = 95;
constexpr int DEFAULT_MEMLIMIT_MB = 6000;
constexpr int kDefaultQualityTarget = 95;
constexpr int kDefaultMemlimitMb = 6000;
constexpr const char* RELATIVE_PATH_TO_TESTDATA =
constexpr const char* kRelativePathToTestdata =
"/guetzli_sandboxed/tests/testdata/";
std::string GetPathToInputFile(const char* filename) {
return std::string(getenv("TEST_SRCDIR"))
+ std::string(RELATIVE_PATH_TO_TESTDATA)
+ std::string(kRelativePathToTestdata)
+ std::string(filename);
}
@ -78,7 +94,7 @@ protected:
// This test can take up to few minutes depending on your hardware
TEST_F(GuetzliSapiTest, ProcessRGB) {
sapi::v::Fd in_fd(open(GetPathToInputFile(IN_PNG_FILENAME).c_str(),
sapi::v::Fd in_fd(open(GetPathToInputFile(kInPngFilename).c_str(),
O_RDONLY));
ASSERT_TRUE(in_fd.GetValue() != -1) << "Error opening input file";
ASSERT_EQ(api_->sandbox()->TransferToSandboxee(&in_fd), absl::OkStatus())
@ -87,17 +103,17 @@ TEST_F(GuetzliSapiTest, ProcessRGB) {
sapi::v::Struct<ProcessingParams> processing_params;
*processing_params.mutable_data() = {in_fd.GetRemoteFd(),
0,
DEFAULT_QUALITY_TARGET,
DEFAULT_MEMLIMIT_MB
kDefaultQualityTarget,
kDefaultMemlimitMb
};
sapi::v::LenVal output(0);
auto processing_result = api_->ProcessRgb(processing_params.PtrBefore(),
output.PtrBoth());
ASSERT_TRUE(processing_result.value_or(false)) << "Error processing rgb data";
ASSERT_EQ(output.GetDataSize(), PNG_EXPECTED_SIZE)
ASSERT_EQ(output.GetDataSize(), kPngExpectedSize)
<< "Incorrect result data size";
std::string reference_data =
ReadFromFile(GetPathToInputFile(PNG_REFERENCE_FILENAME));
ReadFromFile(GetPathToInputFile(kPngReferenceFilename));
ASSERT_EQ(output.GetDataSize(), reference_data.size())
<< "Incorrect result data size";
ASSERT_TRUE(CompareBytesInLenValAndContainer(output, reference_data))
@ -106,7 +122,7 @@ TEST_F(GuetzliSapiTest, ProcessRGB) {
// This test can take up to few minutes depending on your hardware
TEST_F(GuetzliSapiTest, ProcessJpeg) {
sapi::v::Fd in_fd(open(GetPathToInputFile(IN_JPG_FILENAME).c_str(),
sapi::v::Fd in_fd(open(GetPathToInputFile(kInJpegFilename).c_str(),
O_RDONLY));
ASSERT_TRUE(in_fd.GetValue() != -1) << "Error opening input file";
ASSERT_EQ(api_->sandbox()->TransferToSandboxee(&in_fd), absl::OkStatus())
@ -115,27 +131,23 @@ TEST_F(GuetzliSapiTest, ProcessJpeg) {
sapi::v::Struct<ProcessingParams> processing_params;
*processing_params.mutable_data() = {in_fd.GetRemoteFd(),
0,
DEFAULT_QUALITY_TARGET,
DEFAULT_MEMLIMIT_MB
kDefaultQualityTarget,
kDefaultMemlimitMb
};
sapi::v::LenVal output(0);
auto processing_result = api_->ProcessJpeg(processing_params.PtrBefore(),
output.PtrBoth());
ASSERT_TRUE(processing_result.value_or(false)) << "Error processing jpg data";
ASSERT_EQ(output.GetDataSize(), JPG_EXPECTED_SIZE)
ASSERT_EQ(output.GetDataSize(), kJpegExpectedSize)
<< "Incorrect result data size";
std::string reference_data =
ReadFromFile(GetPathToInputFile(JPG_REFERENCE_FILENAME));
ReadFromFile(GetPathToInputFile(kJpegReferenceFIlename));
ASSERT_EQ(output.GetDataSize(), reference_data.size())
<< "Incorrect result data size";
ASSERT_TRUE(CompareBytesInLenValAndContainer(output, reference_data))
<< "Processed data doesn't match reference output";
}
// TEST_F(GuetzliSapiTest, WriteDataToFd) {
// sapi::v::Fd fd(open(".", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR));
// }
} // namespace tests
} // namespace sandbox
} // namespace guetzli
} // namespace tests
} // namespace sandbox
} // namespace guetzli

View File

@ -1,14 +1,30 @@
#include "gtest/gtest.h"
#include "guetzli_transaction.h"
#include "sandboxed_api/sandbox2/util/fileops.h"
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fstream>
#include <sstream>
#include <memory>
#include <sstream>
#include "gtest/gtest.h"
#include "sandboxed_api/sandbox2/util/fileops.h"
#include "guetzli_transaction.h"
namespace guetzli {
namespace sandbox {
@ -16,23 +32,23 @@ namespace tests {
namespace {
constexpr const char* IN_PNG_FILENAME = "bees.png";
constexpr const char* IN_JPG_FILENAME = "nature.jpg";
constexpr const char* PNG_REFERENCE_FILENAME = "bees_reference.jpg";
constexpr const char* JPG_REFERENCE_FILENAME = "nature_reference.jpg";
constexpr const char* kInPngFilename = "bees.png";
constexpr const char* kInJpegFilename = "nature.jpg";
constexpr const char* kPngReferenceFilename = "bees_reference.jpg";
constexpr const char* kJpegReferenceFIlename = "nature_reference.jpg";
constexpr int PNG_EXPECTED_SIZE = 38'625;
constexpr int JPG_EXPECTED_SIZE = 10'816;
constexpr int kPngExpectedSize = 38'625;
constexpr int kJpegExpectedSize = 10'816;
constexpr int DEFAULT_QUALITY_TARGET = 95;
constexpr int DEFAULT_MEMLIMIT_MB = 6000;
constexpr int kDefaultQualityTarget = 95;
constexpr int kDefaultMemlimitMb = 6000;
constexpr const char* RELATIVE_PATH_TO_TESTDATA =
constexpr const char* kRelativePathToTestdata =
"/guetzli_sandboxed/tests/testdata/";
std::string GetPathToInputFile(const char* filename) {
return std::string(getenv("TEST_SRCDIR"))
+ std::string(RELATIVE_PATH_TO_TESTDATA)
+ std::string(kRelativePathToTestdata)
+ std::string(filename);
}
@ -48,11 +64,11 @@ std::string ReadFromFile(const std::string& filename) {
return result.str();
}
} // namespace
} // namespace
TEST(GuetzliTransactionTest, TestTransactionJpg) {
sandbox2::file_util::fileops::FDCloser in_fd_closer(
open(GetPathToInputFile(IN_JPG_FILENAME).c_str(), O_RDONLY));
open(GetPathToInputFile(kInJpegFilename).c_str(), O_RDONLY));
ASSERT_TRUE(in_fd_closer.get() != -1) << "Error opening input jpg file";
sandbox2::file_util::fileops::FDCloser out_fd_closer(
open(".", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR));
@ -61,8 +77,8 @@ TEST(GuetzliTransactionTest, TestTransactionJpg) {
in_fd_closer.get(),
out_fd_closer.get(),
0,
DEFAULT_QUALITY_TARGET,
DEFAULT_MEMLIMIT_MB
kDefaultQualityTarget,
kDefaultMemlimitMb
};
{
GuetzliTransaction transaction(std::move(params));
@ -72,7 +88,7 @@ TEST(GuetzliTransactionTest, TestTransactionJpg) {
}
ASSERT_TRUE(fcntl(out_fd_closer.get(), F_GETFD) != -1 || errno != EBADF)
<< "Local output fd closed";
auto reference_data = ReadFromFile(GetPathToInputFile(JPG_REFERENCE_FILENAME));
auto reference_data = ReadFromFile(GetPathToInputFile(kJpegReferenceFIlename));
auto output_size = lseek(out_fd_closer.get(), 0, SEEK_END);
ASSERT_EQ(reference_data.size(), output_size)
<< "Different sizes of reference and returned data";
@ -90,7 +106,7 @@ TEST(GuetzliTransactionTest, TestTransactionJpg) {
TEST(GuetzliTransactionTest, TestTransactionPng) {
sandbox2::file_util::fileops::FDCloser in_fd_closer(
open(GetPathToInputFile(IN_PNG_FILENAME).c_str(), O_RDONLY));
open(GetPathToInputFile(kInPngFilename).c_str(), O_RDONLY));
ASSERT_TRUE(in_fd_closer.get() != -1) << "Error opening input png file";
sandbox2::file_util::fileops::FDCloser out_fd_closer(
open(".", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR));
@ -99,8 +115,8 @@ TEST(GuetzliTransactionTest, TestTransactionPng) {
in_fd_closer.get(),
out_fd_closer.get(),
0,
DEFAULT_QUALITY_TARGET,
DEFAULT_MEMLIMIT_MB
kDefaultQualityTarget,
kDefaultMemlimitMb
};
{
GuetzliTransaction transaction(std::move(params));
@ -110,7 +126,7 @@ TEST(GuetzliTransactionTest, TestTransactionPng) {
}
ASSERT_TRUE(fcntl(out_fd_closer.get(), F_GETFD) != -1 || errno != EBADF)
<< "Local output fd closed";
auto reference_data = ReadFromFile(GetPathToInputFile(PNG_REFERENCE_FILENAME));
auto reference_data = ReadFromFile(GetPathToInputFile(kPngReferenceFilename));
auto output_size = lseek(out_fd_closer.get(), 0, SEEK_END);
ASSERT_EQ(reference_data.size(), output_size)
<< "Different sizes of reference and returned data";
@ -126,6 +142,6 @@ TEST(GuetzliTransactionTest, TestTransactionPng) {
<< "Returned data doesn't match refernce";
}
} // namespace tests
} // namespace sandbox
} // namespace guetzli
} // namespace tests
} // namespace sandbox
} // namespace guetzli