|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// Author: jcrowell@google.com (Jeffrey Crowell) |
| 18 | + |
| 19 | +#include "pagespeed/kernel/util/brotli_inflater.h" |
| 20 | + |
| 21 | +#include <cstddef> |
| 22 | +#include "pagespeed/kernel/base/gtest.h" |
| 23 | +#include "pagespeed/kernel/base/message_handler_test_base.h" |
| 24 | +#include "pagespeed/kernel/base/null_mutex.h" |
| 25 | +#include "pagespeed/kernel/util/simple_random.h" |
| 26 | +#include "pagespeed/kernel/base/stack_buffer.h" |
| 27 | +#include "pagespeed/kernel/base/string.h" |
| 28 | +#include "pagespeed/kernel/base/string_util.h" |
| 29 | +#include "pagespeed/kernel/base/string_writer.h" |
| 30 | + |
| 31 | +namespace net_instaweb { |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +// Generated with command line tool "bro". |
| 36 | +static const char kHello[] = "hello\n"; |
| 37 | +static const char kHelloBrotli[] = "\x8b\x02\x80\x68\x65\x6c\x6c\x6f\x0a\x03"; |
| 38 | +// Use the highest quality by default (11). |
| 39 | +static const int kCompressionLevel = 11; |
| 40 | + |
| 41 | +// Writer that returns false on Write(). |
| 42 | +class StringWriterReturningFalse : public StringWriter { |
| 43 | + public: |
| 44 | + explicit StringWriterReturningFalse(GoogleString* str) : StringWriter(str) {} |
| 45 | + bool Write(const StringPiece& str, |
| 46 | + net_instaweb::MessageHandler* message_handler) { |
| 47 | + StringWriter::Write(str, message_handler); |
| 48 | + return false; |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +TEST(BrotliInflater, TestBrotliDecompress) { |
| 53 | + // Compress and decompress a simple string. |
| 54 | + GoogleString decompressed; |
| 55 | + TestMessageHandler handler; |
| 56 | + StringWriter decompress_writer(&decompressed); |
| 57 | + StringPiece compressed(kHelloBrotli, sizeof(kHelloBrotli)); |
| 58 | + EXPECT_TRUE( |
| 59 | + BrotliInflater::Decompress(compressed, &handler, &decompress_writer)); |
| 60 | + EXPECT_STREQ(kHello, decompressed); |
| 61 | + EXPECT_EQ(0, handler.messages().size()); |
| 62 | +} |
| 63 | + |
| 64 | +TEST(BrotliInflater, TestFailedWriteBrotliDecompress) { |
| 65 | + // Use a writer that returns failure on write. |
| 66 | + GoogleString decompressed; |
| 67 | + TestMessageHandler handler; |
| 68 | + StringWriterReturningFalse decompress_writer(&decompressed); |
| 69 | + StringPiece compressed(kHelloBrotli, sizeof(kHelloBrotli)); |
| 70 | + EXPECT_FALSE( |
| 71 | + BrotliInflater::Decompress(compressed, &handler, &decompress_writer)); |
| 72 | + EXPECT_EQ(0, handler.messages().size()); |
| 73 | +} |
| 74 | + |
| 75 | +TEST(BrotliInflater, TestCorruptInputBrotliDecompress) { |
| 76 | + // Take "hello\n" but replace the first 2 bytes with "AB", so it will not be |
| 77 | + // valid brotli. |
| 78 | + const char kHelloBrotliCorrupt[] = "AB\x80\x68\x65\x6c\x6c\x6f\x0a\x03"; |
| 79 | + GoogleString decompressed; |
| 80 | + TestMessageHandler handler; |
| 81 | + StringWriterReturningFalse decompress_writer(&decompressed); |
| 82 | + StringPiece compressed(kHelloBrotliCorrupt, sizeof(kHelloBrotliCorrupt)); |
| 83 | + EXPECT_FALSE( |
| 84 | + BrotliInflater::Decompress(compressed, &handler, &decompress_writer)); |
| 85 | + EXPECT_STREQ("Error: BROTLI_RESULT_ERROR", handler.messages()[0]); |
| 86 | +} |
| 87 | + |
| 88 | +TEST(BrotliInflater, TestTruncatedInputBrotliDecompress) { |
| 89 | + // Take "hello\n" but truncate the string, so it will not be valid brotli. |
| 90 | + const char kHelloBrotliCorrupt[] = "\x8b\x02\x80\x68\x65\x6c\x6c\x6f"; |
| 91 | + GoogleString decompressed; |
| 92 | + TestMessageHandler handler; |
| 93 | + StringWriterReturningFalse decompress_writer(&decompressed); |
| 94 | + StringPiece compressed(kHelloBrotliCorrupt, sizeof(kHelloBrotliCorrupt)); |
| 95 | + EXPECT_FALSE( |
| 96 | + BrotliInflater::Decompress(compressed, &handler, &decompress_writer)); |
| 97 | + EXPECT_STREQ("Warning: BROTLI_RESULT_NEEDS_MORE_INPUT", |
| 98 | + handler.messages()[0]); |
| 99 | +} |
| 100 | + |
| 101 | +TEST(BrotliInflater, TestCompressDecompressSmallString) { |
| 102 | + // Compress and decompress a simple string, ensure that it remains unchanged. |
| 103 | + StringPiece payload(kHello); |
| 104 | + TestMessageHandler handler; |
| 105 | + GoogleString compressed, decompressed; |
| 106 | + StringWriter compressed_writer(&compressed); |
| 107 | + EXPECT_TRUE(BrotliInflater::Compress(payload, kCompressionLevel, &handler, |
| 108 | + &compressed_writer)); |
| 109 | + StringWriter decompressed_writer(&decompressed); |
| 110 | + EXPECT_TRUE( |
| 111 | + BrotliInflater::Decompress(compressed, &handler, &decompressed_writer)); |
| 112 | + EXPECT_STREQ(payload, decompressed); |
| 113 | + EXPECT_EQ(0, handler.messages().size()); |
| 114 | +} |
| 115 | + |
| 116 | +TEST(BrotliInflater, TestCompressDecompressLargeString) { |
| 117 | + // Compress and decompress a long string of repeated characters that will |
| 118 | + // exceed the buffer size. |
| 119 | + TestMessageHandler handler; |
| 120 | + GoogleString value(5 * kStackBufferSize, 'A'); |
| 121 | + StringPiece payload(value); |
| 122 | + GoogleString compressed, decompressed; |
| 123 | + StringWriter compressed_writer(&compressed); |
| 124 | + EXPECT_TRUE(BrotliInflater::Compress(payload, kCompressionLevel, &handler, |
| 125 | + &compressed_writer)); |
| 126 | + StringWriter decompressed_writer(&decompressed); |
| 127 | + EXPECT_TRUE( |
| 128 | + BrotliInflater::Decompress(compressed, &handler, &decompressed_writer)); |
| 129 | + EXPECT_STREQ(payload, decompressed); |
| 130 | + EXPECT_EQ(0, handler.messages().size()); |
| 131 | +} |
| 132 | + |
| 133 | +TEST(BrotliInflater, TestCompressDecompressLargeStringWithPoorCompression) { |
| 134 | + // Compress and decompress a long string of random characters. |
| 135 | + TestMessageHandler handler; |
| 136 | + SimpleRandom random(new NullMutex); |
| 137 | + GoogleString value = random.GenerateHighEntropyString(5 * kStackBufferSize); |
| 138 | + StringPiece payload(value); |
| 139 | + GoogleString compressed, decompressed; |
| 140 | + StringWriter compressed_writer(&compressed); |
| 141 | + EXPECT_TRUE(BrotliInflater::Compress(payload, kCompressionLevel, &handler, |
| 142 | + &compressed_writer)); |
| 143 | + StringWriter decompressed_writer(&decompressed); |
| 144 | + EXPECT_TRUE( |
| 145 | + BrotliInflater::Decompress(compressed, &handler, &decompressed_writer)); |
| 146 | + EXPECT_STREQ(payload, decompressed); |
| 147 | + EXPECT_EQ(0, handler.messages().size()); |
| 148 | +} |
| 149 | + |
| 150 | +} // namespace |
| 151 | + |
| 152 | +} // namespace net_instaweb |
0 commit comments