| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <fstream> | 5 #include <fstream> |
| 6 #include <iostream> | 6 #include <iostream> |
| 7 | 7 |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/platform_test.h" | 10 #include "base/platform_test.h" |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 std::string source_buffer_; | 175 std::string source_buffer_; |
| 176 | 176 |
| 177 scoped_ptr<bz_stream> bzip2_data_stream_; | 177 scoped_ptr<bz_stream> bzip2_data_stream_; |
| 178 char* bzip2_encode_buffer_; | 178 char* bzip2_encode_buffer_; |
| 179 int bzip2_encode_len_; | 179 int bzip2_encode_len_; |
| 180 }; | 180 }; |
| 181 | 181 |
| 182 // Basic scenario: decoding bzip2 data with big enough buffer. | 182 // Basic scenario: decoding bzip2 data with big enough buffer. |
| 183 TEST_F(BZip2FilterUnitTest, DecodeBZip2) { | 183 TEST_F(BZip2FilterUnitTest, DecodeBZip2) { |
| 184 // Decode the compressed data with filter | 184 // Decode the compressed data with filter |
| 185 std::vector<std::string> filters; | 185 std::vector<Filter::FilterType> filter_types; |
| 186 filters.push_back("bzip2"); | 186 filter_types.push_back(Filter::FILTER_TYPE_BZIP2); |
| 187 scoped_ptr<Filter> filter( | 187 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kDefaultBufferSize)); |
| 188 Filter::Factory(filters, kApplicationOctetStream, kDefaultBufferSize)); | |
| 189 ASSERT_TRUE(filter.get()); | 188 ASSERT_TRUE(filter.get()); |
| 190 memcpy(filter->stream_buffer(), bzip2_encode_buffer_, bzip2_encode_len_); | 189 memcpy(filter->stream_buffer(), bzip2_encode_buffer_, bzip2_encode_len_); |
| 191 filter->FlushStreamBuffer(bzip2_encode_len_); | 190 filter->FlushStreamBuffer(bzip2_encode_len_); |
| 192 | 191 |
| 193 char bzip2_decode_buffer[kDefaultBufferSize]; | 192 char bzip2_decode_buffer[kDefaultBufferSize]; |
| 194 int bzip2_decode_size = kDefaultBufferSize; | 193 int bzip2_decode_size = kDefaultBufferSize; |
| 195 Filter::FilterStatus result = | 194 Filter::FilterStatus result = |
| 196 filter->ReadData(bzip2_decode_buffer, &bzip2_decode_size); | 195 filter->ReadData(bzip2_decode_buffer, &bzip2_decode_size); |
| 197 ASSERT_EQ(Filter::FILTER_DONE, result); | 196 ASSERT_EQ(Filter::FILTER_DONE, result); |
| 198 | 197 |
| 199 // Compare the decoding result with source data | 198 // Compare the decoding result with source data |
| 200 EXPECT_TRUE(bzip2_decode_size == source_len()); | 199 EXPECT_TRUE(bzip2_decode_size == source_len()); |
| 201 EXPECT_EQ(memcmp(source_buffer(), bzip2_decode_buffer, source_len()), 0); | 200 EXPECT_EQ(memcmp(source_buffer(), bzip2_decode_buffer, source_len()), 0); |
| 202 } | 201 } |
| 203 | 202 |
| 204 // Tests we can call filter repeatedly to get all the data decoded. | 203 // Tests we can call filter repeatedly to get all the data decoded. |
| 205 // To do that, we create a filter with a small buffer that can not hold all | 204 // To do that, we create a filter with a small buffer that can not hold all |
| 206 // the input data. | 205 // the input data. |
| 207 TEST_F(BZip2FilterUnitTest, DecodeWithSmallInputBuffer) { | 206 TEST_F(BZip2FilterUnitTest, DecodeWithSmallInputBuffer) { |
| 208 std::vector<std::string> filters; | 207 std::vector<Filter::FilterType> filter_types; |
| 209 filters.push_back("bzip2"); | 208 filter_types.push_back(Filter::FILTER_TYPE_BZIP2); |
| 210 scoped_ptr<Filter> filter( | 209 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kSmallBufferSize)); |
| 211 Filter::Factory(filters, kApplicationOctetStream, kSmallBufferSize)); | |
| 212 ASSERT_TRUE(filter.get()); | 210 ASSERT_TRUE(filter.get()); |
| 213 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(), | 211 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(), |
| 214 bzip2_encode_buffer_, bzip2_encode_len_, | 212 bzip2_encode_buffer_, bzip2_encode_len_, |
| 215 kDefaultBufferSize, false); | 213 kDefaultBufferSize, false); |
| 216 } | 214 } |
| 217 | 215 |
| 218 // Tests we can decode when caller has small buffer to read out from filter. | 216 // Tests we can decode when caller has small buffer to read out from filter. |
| 219 TEST_F(BZip2FilterUnitTest, DecodeWithSmallOutputBuffer) { | 217 TEST_F(BZip2FilterUnitTest, DecodeWithSmallOutputBuffer) { |
| 220 std::vector<std::string> filters; | 218 std::vector<Filter::FilterType> filter_types; |
| 221 filters.push_back("bzip2"); | 219 filter_types.push_back(Filter::FILTER_TYPE_BZIP2); |
| 222 scoped_ptr<Filter> filter( | 220 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kDefaultBufferSize)); |
| 223 Filter::Factory(filters, kApplicationOctetStream, kDefaultBufferSize)); | |
| 224 ASSERT_TRUE(filter.get()); | 221 ASSERT_TRUE(filter.get()); |
| 225 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(), | 222 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(), |
| 226 bzip2_encode_buffer_, bzip2_encode_len_, | 223 bzip2_encode_buffer_, bzip2_encode_len_, |
| 227 kSmallBufferSize, false); | 224 kSmallBufferSize, false); |
| 228 } | 225 } |
| 229 | 226 |
| 230 // Tests we can still decode with just 1 byte buffer in the filter. | 227 // Tests we can still decode with just 1 byte buffer in the filter. |
| 231 // The purpose of this tests are two: (1) Verify filter can parse partial BZip2 | 228 // The purpose of this tests are two: (1) Verify filter can parse partial BZip2 |
| 232 // header correctly. (2) Sometimes the filter will consume input without | 229 // header correctly. (2) Sometimes the filter will consume input without |
| 233 // generating output. Verify filter can handle it correctly. | 230 // generating output. Verify filter can handle it correctly. |
| 234 TEST_F(BZip2FilterUnitTest, DecodeWithOneByteInputBuffer) { | 231 TEST_F(BZip2FilterUnitTest, DecodeWithOneByteInputBuffer) { |
| 235 std::vector<std::string> filters; | 232 std::vector<Filter::FilterType> filter_types; |
| 236 filters.push_back("bzip2"); | 233 filter_types.push_back(Filter::FILTER_TYPE_BZIP2); |
| 237 scoped_ptr<Filter> filter( | 234 scoped_ptr<Filter> filter(Filter::Factory(filter_types, 1)); |
| 238 Filter::Factory(filters, kApplicationOctetStream, 1)); | |
| 239 ASSERT_TRUE(filter.get()); | 235 ASSERT_TRUE(filter.get()); |
| 240 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(), | 236 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(), |
| 241 bzip2_encode_buffer_, bzip2_encode_len_, | 237 bzip2_encode_buffer_, bzip2_encode_len_, |
| 242 kDefaultBufferSize, false); | 238 kDefaultBufferSize, false); |
| 243 } | 239 } |
| 244 | 240 |
| 245 // Tests we can still decode with just 1 byte buffer in the filter and just 1 | 241 // Tests we can still decode with just 1 byte buffer in the filter and just 1 |
| 246 // byte buffer in the caller. | 242 // byte buffer in the caller. |
| 247 TEST_F(BZip2FilterUnitTest, DecodeWithOneByteInputAndOutputBuffer) { | 243 TEST_F(BZip2FilterUnitTest, DecodeWithOneByteInputAndOutputBuffer) { |
| 248 std::vector<std::string> filters; | 244 std::vector<Filter::FilterType> filter_types; |
| 249 filters.push_back("bzip2"); | 245 filter_types.push_back(Filter::FILTER_TYPE_BZIP2); |
| 250 scoped_ptr<Filter> filter( | 246 scoped_ptr<Filter> filter(Filter::Factory(filter_types, 1)); |
| 251 Filter::Factory(filters, kApplicationOctetStream, 1)); | |
| 252 ASSERT_TRUE(filter.get()); | 247 ASSERT_TRUE(filter.get()); |
| 253 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(), | 248 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(), |
| 254 bzip2_encode_buffer_, bzip2_encode_len_, 1, false); | 249 bzip2_encode_buffer_, bzip2_encode_len_, 1, false); |
| 255 } | 250 } |
| 256 | 251 |
| 257 // Decoding bzip2 stream with corrupted data. | 252 // Decoding bzip2 stream with corrupted data. |
| 258 TEST_F(BZip2FilterUnitTest, DecodeCorruptedData) { | 253 TEST_F(BZip2FilterUnitTest, DecodeCorruptedData) { |
| 259 char corrupt_data[kDefaultBufferSize]; | 254 char corrupt_data[kDefaultBufferSize]; |
| 260 int corrupt_data_len = bzip2_encode_len_; | 255 int corrupt_data_len = bzip2_encode_len_; |
| 261 memcpy(corrupt_data, bzip2_encode_buffer_, bzip2_encode_len_); | 256 memcpy(corrupt_data, bzip2_encode_buffer_, bzip2_encode_len_); |
| 262 | 257 |
| 263 char corrupt_decode_buffer[kDefaultBufferSize]; | 258 char corrupt_decode_buffer[kDefaultBufferSize]; |
| 264 int corrupt_decode_size = kDefaultBufferSize; | 259 int corrupt_decode_size = kDefaultBufferSize; |
| 265 | 260 |
| 266 // Decode the correct data with filter | 261 // Decode the correct data with filter |
| 267 std::vector<std::string> filters; | 262 std::vector<Filter::FilterType> filter_types; |
| 268 filters.push_back("bzip2"); | 263 filter_types.push_back(Filter::FILTER_TYPE_BZIP2); |
| 269 scoped_ptr<Filter> filter1( | 264 scoped_ptr<Filter> filter1(Filter::Factory(filter_types, kDefaultBufferSize)); |
| 270 Filter::Factory(filters, kApplicationOctetStream, kDefaultBufferSize)); | |
| 271 ASSERT_TRUE(filter1.get()); | 265 ASSERT_TRUE(filter1.get()); |
| 272 | 266 |
| 273 Filter::FilterStatus code = DecodeAllWithFilter(filter1.get(), | 267 Filter::FilterStatus code = DecodeAllWithFilter(filter1.get(), |
| 274 corrupt_data, | 268 corrupt_data, |
| 275 corrupt_data_len, | 269 corrupt_data_len, |
| 276 corrupt_decode_buffer, | 270 corrupt_decode_buffer, |
| 277 &corrupt_decode_size); | 271 &corrupt_decode_size); |
| 278 | 272 |
| 279 // Expect failures | 273 // Expect failures |
| 280 EXPECT_TRUE(code == Filter::FILTER_DONE); | 274 EXPECT_TRUE(code == Filter::FILTER_DONE); |
| 281 | 275 |
| 282 // Decode the corrupted data with filter | 276 // Decode the corrupted data with filter |
| 283 scoped_ptr<Filter> filter2( | 277 scoped_ptr<Filter> filter2(Filter::Factory(filter_types, kDefaultBufferSize)); |
| 284 Filter::Factory(filters, kApplicationOctetStream, kDefaultBufferSize)); | |
| 285 ASSERT_TRUE(filter2.get()); | 278 ASSERT_TRUE(filter2.get()); |
| 286 | 279 |
| 287 int pos = corrupt_data_len / 2; | 280 int pos = corrupt_data_len / 2; |
| 288 corrupt_data[pos] = !corrupt_data[pos]; | 281 corrupt_data[pos] = !corrupt_data[pos]; |
| 289 | 282 |
| 290 code = DecodeAllWithFilter(filter2.get(), | 283 code = DecodeAllWithFilter(filter2.get(), |
| 291 corrupt_data, | 284 corrupt_data, |
| 292 corrupt_data_len, | 285 corrupt_data_len, |
| 293 corrupt_decode_buffer, | 286 corrupt_decode_buffer, |
| 294 &corrupt_decode_size); | 287 &corrupt_decode_size); |
| 295 | 288 |
| 296 // Expect failures | 289 // Expect failures |
| 297 EXPECT_TRUE(code != Filter::FILTER_DONE); | 290 EXPECT_TRUE(code != Filter::FILTER_DONE); |
| 298 } | 291 } |
| 299 | 292 |
| 300 // Decoding bzip2 stream with missing data. | 293 // Decoding bzip2 stream with missing data. |
| 301 TEST_F(BZip2FilterUnitTest, DecodeMissingData) { | 294 TEST_F(BZip2FilterUnitTest, DecodeMissingData) { |
| 302 char corrupt_data[kDefaultBufferSize]; | 295 char corrupt_data[kDefaultBufferSize]; |
| 303 int corrupt_data_len = bzip2_encode_len_; | 296 int corrupt_data_len = bzip2_encode_len_; |
| 304 memcpy(corrupt_data, bzip2_encode_buffer_, bzip2_encode_len_); | 297 memcpy(corrupt_data, bzip2_encode_buffer_, bzip2_encode_len_); |
| 305 | 298 |
| 306 int pos = corrupt_data_len / 2; | 299 int pos = corrupt_data_len / 2; |
| 307 int len = corrupt_data_len - pos - 1; | 300 int len = corrupt_data_len - pos - 1; |
| 308 memmove(&corrupt_data[pos], &corrupt_data[pos+1], len); | 301 memmove(&corrupt_data[pos], &corrupt_data[pos+1], len); |
| 309 --corrupt_data_len; | 302 --corrupt_data_len; |
| 310 | 303 |
| 311 // Decode the corrupted data with filter | 304 // Decode the corrupted data with filter |
| 312 std::vector<std::string> filters; | 305 std::vector<Filter::FilterType> filter_types; |
| 313 filters.push_back("bzip2"); | 306 filter_types.push_back(Filter::FILTER_TYPE_BZIP2); |
| 314 scoped_ptr<Filter> filter( | 307 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kDefaultBufferSize)); |
| 315 Filter::Factory(filters, kApplicationOctetStream, kDefaultBufferSize)); | |
| 316 ASSERT_TRUE(filter.get()); | 308 ASSERT_TRUE(filter.get()); |
| 317 char corrupt_decode_buffer[kDefaultBufferSize]; | 309 char corrupt_decode_buffer[kDefaultBufferSize]; |
| 318 int corrupt_decode_size = kDefaultBufferSize; | 310 int corrupt_decode_size = kDefaultBufferSize; |
| 319 | 311 |
| 320 Filter::FilterStatus code = DecodeAllWithFilter(filter.get(), | 312 Filter::FilterStatus code = DecodeAllWithFilter(filter.get(), |
| 321 corrupt_data, | 313 corrupt_data, |
| 322 corrupt_data_len, | 314 corrupt_data_len, |
| 323 corrupt_decode_buffer, | 315 corrupt_decode_buffer, |
| 324 &corrupt_decode_size); | 316 &corrupt_decode_size); |
| 325 // Expect failures | 317 // Expect failures |
| 326 EXPECT_TRUE(code != Filter::FILTER_DONE); | 318 EXPECT_TRUE(code != Filter::FILTER_DONE); |
| 327 } | 319 } |
| 328 | 320 |
| 329 // Decoding bzip2 stream with corrupted header. | 321 // Decoding bzip2 stream with corrupted header. |
| 330 TEST_F(BZip2FilterUnitTest, DecodeCorruptedHeader) { | 322 TEST_F(BZip2FilterUnitTest, DecodeCorruptedHeader) { |
| 331 char corrupt_data[kDefaultBufferSize]; | 323 char corrupt_data[kDefaultBufferSize]; |
| 332 int corrupt_data_len = bzip2_encode_len_; | 324 int corrupt_data_len = bzip2_encode_len_; |
| 333 memcpy(corrupt_data, bzip2_encode_buffer_, bzip2_encode_len_); | 325 memcpy(corrupt_data, bzip2_encode_buffer_, bzip2_encode_len_); |
| 334 | 326 |
| 335 corrupt_data[2] = !corrupt_data[2]; | 327 corrupt_data[2] = !corrupt_data[2]; |
| 336 | 328 |
| 337 // Decode the corrupted data with filter | 329 // Decode the corrupted data with filter |
| 338 std::vector<std::string> filters; | 330 std::vector<Filter::FilterType> filter_types; |
| 339 filters.push_back("bzip2"); | 331 filter_types.push_back(Filter::FILTER_TYPE_BZIP2); |
| 340 scoped_ptr<Filter> filter( | 332 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kDefaultBufferSize)); |
| 341 Filter::Factory(filters, kApplicationOctetStream, kDefaultBufferSize)); | |
| 342 ASSERT_TRUE(filter.get()); | 333 ASSERT_TRUE(filter.get()); |
| 343 char corrupt_decode_buffer[kDefaultBufferSize]; | 334 char corrupt_decode_buffer[kDefaultBufferSize]; |
| 344 int corrupt_decode_size = kDefaultBufferSize; | 335 int corrupt_decode_size = kDefaultBufferSize; |
| 345 | 336 |
| 346 Filter::FilterStatus code = DecodeAllWithFilter(filter.get(), | 337 Filter::FilterStatus code = DecodeAllWithFilter(filter.get(), |
| 347 corrupt_data, | 338 corrupt_data, |
| 348 corrupt_data_len, | 339 corrupt_data_len, |
| 349 corrupt_decode_buffer, | 340 corrupt_decode_buffer, |
| 350 &corrupt_decode_size); | 341 &corrupt_decode_size); |
| 351 | 342 |
| 352 // Expect failures | 343 // Expect failures |
| 353 EXPECT_TRUE(code == Filter::FILTER_ERROR); | 344 EXPECT_TRUE(code == Filter::FILTER_ERROR); |
| 354 } | 345 } |
| 355 | 346 |
| 356 // Tests we can decode all compress data and get extra data which is | 347 // Tests we can decode all compress data and get extra data which is |
| 357 // appended to compress data stream by some server when it finish | 348 // appended to compress data stream by some server when it finish |
| 358 // sending compress data. | 349 // sending compress data. |
| 359 TEST_F(BZip2FilterUnitTest, DecodeWithExtraDataAndSmallOutputBuffer) { | 350 TEST_F(BZip2FilterUnitTest, DecodeWithExtraDataAndSmallOutputBuffer) { |
| 360 char more_data[kDefaultBufferSize + kExtraDataBufferSize]; | 351 char more_data[kDefaultBufferSize + kExtraDataBufferSize]; |
| 361 int more_data_len = bzip2_encode_len_ + kExtraDataBufferSize; | 352 int more_data_len = bzip2_encode_len_ + kExtraDataBufferSize; |
| 362 memcpy(more_data, bzip2_encode_buffer_, bzip2_encode_len_); | 353 memcpy(more_data, bzip2_encode_buffer_, bzip2_encode_len_); |
| 363 memcpy(more_data + bzip2_encode_len_, kExtraData, kExtraDataBufferSize); | 354 memcpy(more_data + bzip2_encode_len_, kExtraData, kExtraDataBufferSize); |
| 364 | 355 |
| 365 std::vector<std::string> filters; | 356 std::vector<Filter::FilterType> filter_types; |
| 366 filters.push_back("bzip2"); | 357 filter_types.push_back(Filter::FILTER_TYPE_BZIP2); |
| 367 scoped_ptr<Filter> filter( | 358 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kDefaultBufferSize)); |
| 368 Filter::Factory(filters, kApplicationOctetStream, kDefaultBufferSize)); | |
| 369 ASSERT_TRUE(filter.get()); | 359 ASSERT_TRUE(filter.get()); |
| 370 DecodeAndCompareWithFilter(filter.get(), | 360 DecodeAndCompareWithFilter(filter.get(), |
| 371 source_buffer(), | 361 source_buffer(), |
| 372 source_len() + kExtraDataBufferSize, | 362 source_len() + kExtraDataBufferSize, |
| 373 more_data, | 363 more_data, |
| 374 more_data_len, | 364 more_data_len, |
| 375 kSmallBufferSize, | 365 kSmallBufferSize, |
| 376 true); | 366 true); |
| 377 } | 367 } |
| 378 | 368 |
| 379 TEST_F(BZip2FilterUnitTest, DecodeWithExtraDataAndSmallInputBuffer) { | 369 TEST_F(BZip2FilterUnitTest, DecodeWithExtraDataAndSmallInputBuffer) { |
| 380 char more_data[kDefaultBufferSize + kExtraDataBufferSize]; | 370 char more_data[kDefaultBufferSize + kExtraDataBufferSize]; |
| 381 int more_data_len = bzip2_encode_len_ + kExtraDataBufferSize; | 371 int more_data_len = bzip2_encode_len_ + kExtraDataBufferSize; |
| 382 memcpy(more_data, bzip2_encode_buffer_, bzip2_encode_len_); | 372 memcpy(more_data, bzip2_encode_buffer_, bzip2_encode_len_); |
| 383 memcpy(more_data + bzip2_encode_len_, kExtraData, kExtraDataBufferSize); | 373 memcpy(more_data + bzip2_encode_len_, kExtraData, kExtraDataBufferSize); |
| 384 | 374 |
| 385 std::vector<std::string> filters; | 375 std::vector<Filter::FilterType> filter_types; |
| 386 filters.push_back("bzip2"); | 376 filter_types.push_back(Filter::FILTER_TYPE_BZIP2); |
| 387 scoped_ptr<Filter> filter( | 377 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kSmallBufferSize)); |
| 388 Filter::Factory(filters, kApplicationOctetStream, kSmallBufferSize)); | |
| 389 ASSERT_TRUE(filter.get()); | 378 ASSERT_TRUE(filter.get()); |
| 390 DecodeAndCompareWithFilter(filter.get(), | 379 DecodeAndCompareWithFilter(filter.get(), |
| 391 source_buffer(), | 380 source_buffer(), |
| 392 source_len() + kExtraDataBufferSize, | 381 source_len() + kExtraDataBufferSize, |
| 393 more_data, | 382 more_data, |
| 394 more_data_len, | 383 more_data_len, |
| 395 kDefaultBufferSize, | 384 kDefaultBufferSize, |
| 396 true); | 385 true); |
| 397 } | 386 } |
| 398 | 387 |
| 399 } // namespace | 388 } // namespace |
| OLD | NEW |