Skip to content

Commit c19425f

Browse files
committed
Add unit tests for PngChunk::keyTXTChunk
1 parent ad948e8 commit c19425f

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

src/pngchunk_int.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,25 @@ namespace Exiv2 {
103103

104104
DataBuf PngChunk::keyTXTChunk(const DataBuf& data, bool stripHeader)
105105
{
106-
// From a tEXt, zTXt, or iTXt chunk,
107-
// we get the key, it's a null terminated string at the chunk start
106+
// From a tEXt, zTXt, or iTXt chunk, we get the keyword which is null terminated.
108107
const int offset = stripHeader ? 8 : 0;
109-
if (data.size() <= offset) throw Error(kerFailedToReadImageData);
108+
if (data.size() <= offset)
109+
throw Error(kerFailedToReadImageData);
110110
const byte *key = data.c_data(offset);
111111

112-
// Find null string at end of key.
112+
// Find null chatecter at end of keyword.
113113
int keysize=0;
114114
while (key[keysize] != 0)
115115
{
116116
keysize++;
117117
// look if keysize is valid.
118118
if (keysize+offset >= data.size())
119119
throw Error(kerFailedToReadImageData);
120+
/// \todo move conditional out of the loop
120121
}
121122

122123
return DataBuf(key, keysize);
123-
124-
} // PngChunk::keyTXTChunk
124+
}
125125

126126
DataBuf PngChunk::parseTXTChunk(const DataBuf& data,
127127
int keysize,

src/pngimage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,14 +712,14 @@ namespace Exiv2 {
712712
std::cout << "Exiv2::PngImage::doWriteMetadata: strip " << szChunk
713713
<< " chunk (length: " << dataOffset << ")" << std::endl;
714714
#endif
715-
}
716-
else
715+
} else
717716
{
718717
#ifdef EXIV2_DEBUG_MESSAGES
719718
std::cout << "Exiv2::PngImage::doWriteMetadata: write " << szChunk
720719
<< " chunk (length: " << dataOffset << ")" << std::endl;
721720
#endif
722-
if (outIo.write(chunkBuf.c_data(), chunkBuf.size()) != chunkBuf.size()) throw Error(kerImageWriteFailed);
721+
if (outIo.write(chunkBuf.c_data(), chunkBuf.size()) != chunkBuf.size())
722+
throw Error(kerImageWriteFailed);
723723
}
724724
} else {
725725
// Write all others chunk as well.

unitTests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_executable(unit_tests
1212
test_futils.cpp
1313
test_helper_functions.cpp
1414
test_image_int.cpp
15+
test_pngimage.cpp
1516
test_safe_op.cpp
1617
test_slice.cpp
1718
test_tiffheader.cpp

unitTests/test_pngimage.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2004-2022 Exiv2 authors
3+
* This program is part of the Exiv2 distribution.
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU General Public License
7+
* as published by the Free Software Foundation; either version 2
8+
* of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
#include <exiv2/pngimage.hpp>
21+
#include "pngchunk_int.hpp" // This is not part of the public API
22+
23+
#include <gtest/gtest.h>
24+
25+
#include <array>
26+
#include <algorithm>
27+
28+
using namespace Exiv2;
29+
30+
TEST(PngChunk, keyTxtChunkExtractsKeywordCorrectlyInPresenceOfNullChar)
31+
{
32+
// The following data is: '\0\0"AzTXtRaw profile type exif\0\0x'
33+
std::array<std::uint8_t, 32> data{0x00, 0x00, 0x22, 0x41, 0x7a, 0x54, 0x58, 0x74,
34+
0x52, 0x61, 0x77, 0x20, 0x70, 0x72, 0x6f, 0x66,
35+
0x69, 0x6c, 0x65, 0x20, 0x74, 0x79, 0x70, 0x65,
36+
0x20, 0x65, 0x78, 0x69, 0x66, 0x00, 0x00, 0x78};
37+
38+
DataBuf chunkBuf(data.data(), static_cast<long>(data.size()));
39+
DataBuf key = Internal::PngChunk::keyTXTChunk(chunkBuf, true);
40+
ASSERT_EQ(21, key.size());
41+
42+
ASSERT_TRUE(std::equal(key.data(), key.data()+key.size(), data.data()+8, data.data()+8+key.size()));
43+
}
44+
45+
46+
TEST(PngChunk, keyTxtChunkThrowsExceptionWhenThereIsNoNullChar)
47+
{
48+
// The following data is: '\0\0"AzTXtRaw profile type exifx'
49+
std::array<std::uint8_t, 30> data{0x00, 0x00, 0x22, 0x41, 0x7a, 0x54, 0x58, 0x74,
50+
0x52, 0x61, 0x77, 0x20, 0x70, 0x72, 0x6f, 0x66,
51+
0x69, 0x6c, 0x65, 0x20, 0x74, 0x79, 0x70, 0x65,
52+
0x20, 0x65, 0x78, 0x69, 0x66, 0x78};
53+
54+
DataBuf chunkBuf(data.data(), static_cast<long>(data.size()));
55+
ASSERT_THROW(Internal::PngChunk::keyTXTChunk(chunkBuf, true), Exiv2::Error);
56+
}

0 commit comments

Comments
 (0)