Skip to content

Commit 89fb70e

Browse files
happyCoder92copybara-github
authored andcommitted
Test zlib sandbox against unsandboxed
PiperOrigin-RevId: 806255941 Change-Id: I445b954ea10d5d926d7b6d7d63dd2c3226ea12e1
1 parent 4d40c63 commit 89fb70e

3 files changed

Lines changed: 115 additions & 5 deletions

File tree

sandboxed_api/examples/zlib/BUILD

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,28 @@ cc_binary(
5555
],
5656
)
5757

58+
cc_binary(
59+
name = "unsandboxed_zlib",
60+
srcs = ["unsandboxed_zlib.cc"],
61+
copts = sapi_platform_copts(),
62+
deps = [
63+
"@abseil-cpp//absl/base:core_headers",
64+
"@abseil-cpp//absl/base:log_severity",
65+
"@abseil-cpp//absl/flags:parse",
66+
"@abseil-cpp//absl/log",
67+
"@abseil-cpp//absl/log:globals",
68+
"@abseil-cpp//absl/log:initialize",
69+
"@zlib",
70+
],
71+
)
72+
5873
# Tests input/output conversion of zlib and compares with golden files
5974
sh_test(
6075
name = "main_zlib_test",
6176
srcs = ["main_zlib_test.sh"],
62-
data = [":main_zlib"],
77+
data = [
78+
":main_zlib",
79+
":unsandboxed_zlib",
80+
],
6381
tags = ["notsan"],
6482
)

sandboxed_api/examples/zlib/main_zlib_test.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ die() {
2424
[[ -n "$COVERAGE" ]] && exit 0
2525

2626
BIN=$TEST_SRCDIR/com_google_sandboxed_api/sandboxed_api/examples/zlib/main_zlib
27+
UNSANDBOXED_BIN=$TEST_SRCDIR/com_google_sandboxed_api/sandboxed_api/examples/zlib/unsandboxed_zlib
2728
TESTDATA="$TEST_SRCDIR/com_google_sandboxed_api/sandboxed_api/examples/zlib/testdata"
2829

2930
echo "aaaa" | "$BIN" || die 'FAILED: it should have exited with 0'
3031

31-
echo "This is a test string" | "$BIN" | \
32-
sha256sum --status -c \
33-
<(echo '030ac8adfa86cd729493f25333642ca605463c8d37fe6fa822e3a43829872b31 -') || \
34-
die 'FAILED: it should match the golden SHA256'
32+
EXPECTED_SUM=$(echo "This is a test string" | "$UNSANDBOXED_BIN" | \
33+
sha256sum | cut -d " " -f 1)
34+
SUM=$(echo "This is a test string" | "$BIN" | \
35+
sha256sum | cut -d " " -f 1)
36+
[ "$SUM" == "$EXPECTED_SUM" ] || die "FAILED: SHA256 mismatch:
37+
actual: $SUM
38+
expected: $EXPECTED_SUM"
3539

3640
echo 'PASS'
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <cassert>
16+
#include <cstdio>
17+
#include <cstdlib>
18+
19+
#include "absl/base/log_severity.h"
20+
#include "absl/flags/parse.h"
21+
#include "absl/log/globals.h"
22+
#include "absl/log/initialize.h"
23+
#include "absl/log/log.h"
24+
#include "zlib.h"
25+
26+
int main(int argc, char* argv[]) {
27+
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
28+
absl::ParseCommandLine(argc, argv);
29+
absl::InitializeLog();
30+
31+
int ret;
32+
int flush;
33+
unsigned have;
34+
z_stream strm = {};
35+
36+
constexpr int kChunk = 16384;
37+
unsigned char in[kChunk] = {0};
38+
unsigned char out[kChunk] = {0};
39+
40+
const char* kZlibVersion = "1.2.11";
41+
// Allocate deflate state.
42+
43+
ret = deflateInit_(&strm, Z_DEFAULT_COMPRESSION, kZlibVersion, sizeof(strm));
44+
45+
if (ret != Z_OK) {
46+
return ret;
47+
}
48+
49+
LOG(INFO) << "Starting compression";
50+
51+
// Compress until end of file.
52+
do {
53+
strm.avail_in = fread(in, 1, kChunk, stdin);
54+
if (ferror(stdin)) {
55+
LOG(INFO) << "Error reading from stdin";
56+
deflateEnd(&strm);
57+
return Z_ERRNO;
58+
}
59+
flush = feof(stdin) ? Z_FINISH : Z_NO_FLUSH;
60+
strm.next_in = in;
61+
62+
// Run deflate() on input until output buffer not full, finish compression
63+
// if all of source has been read in.
64+
do {
65+
strm.avail_out = kChunk;
66+
strm.next_out = out;
67+
68+
ret = deflate(&strm, flush); // no bad return value.
69+
assert(ret != Z_STREAM_ERROR); // state not clobbered.
70+
have = kChunk - strm.avail_out;
71+
72+
if (fwrite(out, 1, have, stdout) != have || ferror(stdout)) {
73+
// Not really necessary as strm did not change from last transfer.
74+
deflateEnd(&strm);
75+
return Z_ERRNO;
76+
}
77+
} while (strm.avail_out == 0);
78+
assert(strm.avail_in == 0); // all input will be used.
79+
80+
// done when last data in file processed.
81+
} while (flush != Z_FINISH);
82+
assert(ret == Z_STREAM_END); // stream will be complete.
83+
84+
// clean up and return.
85+
deflateEnd(&strm);
86+
87+
return EXIT_SUCCESS;
88+
}

0 commit comments

Comments
 (0)