#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('decode-levels');

my $n_tests = 14;

# image-streams.pdf is the output of examples/pdf-create.
# examples/pdf-create validates the actual image data.
# image-streams-small.pdf was manually created by editing
# pdf-create.cc to reduce width and height to 40x8 and ignoring
# errors. Its purpose was to get a small file with images with
# different filters for fuzz testing.
foreach my $l (qw(none generalized specialized all))
{
    $td->runtest("image-streams: $l",
                 {$td->COMMAND =>
                      "qpdf image-streams.pdf --compress-streams=n" .
                      " --decode-level=$l a.pdf"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("check image-streams: $l",
                 {$td->COMMAND => "test_driver 39 a.pdf"},
                 {$td->FILE => "image-streams-$l.out", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
}

# C API
$td->runtest("image-streams: C",
             {$td->COMMAND => "qpdf-ctest 20 image-streams.pdf '' a.pdf"},
             {$td->STRING => "C test 20 done\n", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check image-streams: C",
             {$td->COMMAND => "test_driver 39 a.pdf"},
             {$td->FILE => "image-streams-specialized.out",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

# Bad JPEG data
$td->runtest("check finds bad jpeg data",
             {$td->COMMAND => "qpdf --check bad-jpeg.pdf"},
             {$td->FILE => "bad-jpeg-check.out",
              $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);
$td->runtest("precheck detects bad jpeg data",
             {$td->COMMAND => "qpdf --static-id --decode-level=all" .
                  " bad-jpeg.pdf a.pdf"},
             {$td->FILE => "bad-jpeg.out", $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check file",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "bad-jpeg-out.pdf"});
$td->runtest("get data",
             {$td->COMMAND => "qpdf --show-object=6" .
                  " --filtered-stream-data bad-jpeg.pdf"},
             {$td->FILE => "bad-jpeg-show.out", $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);

cleanup();
$td->report($n_tests);
