File: test_pngchunks.py

package info (click to toggle)
pngtools 1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 928 kB
  • sloc: ansic: 871; sh: 684; python: 558; makefile: 32
file content (117 lines) | stat: -rw-r--r-- 3,923 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""Tests for the pngchunks tool."""

from tests import base


class TestPngchunks(base.PngtoolsTestCase):
    """Test pngchunks chunk listing."""

    def test_lists_ihdr(self):
        """Output contains an IHDR chunk."""
        result = self.run_tool(
            'pngchunks', [self.sample_path('input.png')]
        )
        self.assertEqual(0, result.returncode)
        self.assertIn('[IHDR]', result.stdout)

    def test_lists_iend(self):
        """Output contains an IEND chunk."""
        result = self.run_tool(
            'pngchunks', [self.sample_path('input.png')]
        )
        self.assertIn('[IEND]', result.stdout)

    def test_lists_idat(self):
        """Output contains at least one IDAT chunk."""
        result = self.run_tool(
            'pngchunks', [self.sample_path('input.png')]
        )
        self.assertIn('[IDAT]', result.stdout)

    def test_ihdr_dimensions(self):
        """IHDR chunk shows correct width and height."""
        result = self.run_tool(
            'pngchunks', [self.sample_path('input.png')]
        )
        self.assertIn('IHDR Width: 256', result.stdout)
        self.assertIn('IHDR Height: 256', result.stdout)

    def test_ihdr_bitdepth(self):
        """IHDR chunk shows correct bitdepth."""
        result = self.run_tool(
            'pngchunks', [self.sample_path('input.png')]
        )
        self.assertIn('IHDR Bitdepth: 8', result.stdout)

    def test_chunk_properties_critical(self):
        """IHDR is identified as critical and public."""
        result = self.run_tool(
            'pngchunks', [self.sample_path('input.png')]
        )
        # The line after IHDR should describe its properties
        self.assertIn('Critical, public', result.stdout)

    def test_ancillary_chunks(self):
        """input.png has ancillary chunks like bKGD or pHYs."""
        result = self.run_tool(
            'pngchunks', [self.sample_path('input.png')]
        )
        # input.png is known to have bKGD, pHYs, tIME
        has_ancillary = (
            '[bKGD]' in result.stdout
            or '[pHYs]' in result.stdout
            or '[tIME]' in result.stdout
        )
        self.assertTrue(
            has_ancillary,
            'Expected ancillary chunks in output'
        )

    def test_compression_algorithm(self):
        """IHDR compression algorithm is identified as Deflate."""
        result = self.run_tool(
            'pngchunks', [self.sample_path('input.png')]
        )
        self.assertIn(
            'Compression algorithm is Deflate', result.stdout
        )

    def test_chunk_crc(self):
        """Each chunk has a CRC value displayed."""
        result = self.run_tool(
            'pngchunks', [self.sample_path('input.png')]
        )
        self.assertIn('Chunk CRC:', result.stdout)

    def test_iend_no_data(self):
        """IEND chunk reports containing no data."""
        result = self.run_tool(
            'pngchunks', [self.sample_path('input.png')]
        )
        self.assertIn('IEND contains no data', result.stdout)


class TestPngchunksErrors(base.PngtoolsTestCase):
    """Test pngchunks error handling."""

    def test_no_arguments(self):
        """No arguments prints usage and exits 1."""
        result = self.run_tool('pngchunks')
        self.assertEqual(1, result.returncode)
        self.assertIn('Usage:', result.stderr)

    def test_missing_file(self):
        """Missing file prints error and exits 1."""
        result = self.run_tool(
            'pngchunks', ['/nonexistent/file.png']
        )
        self.assertEqual(1, result.returncode)
        self.assertIn('Could not open', result.stderr)

    def test_invalid_file(self):
        """Non-PNG file prints error and exits 1."""
        result = self.run_tool(
            'pngchunks', [self.invalid_file]
        )
        self.assertEqual(1, result.returncode)
        self.assertIn('not a PNG file', result.stderr)