Skip to content

Commit ddf5645

Browse files
committed
Add new tests in PngChunk & PngImage for increasing coverage
1 parent ec21f65 commit ddf5645

File tree

1 file changed

+166
-8
lines changed

1 file changed

+166
-8
lines changed

unitTests/test_pngimage.cpp

Lines changed: 166 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@
2424

2525
#include <array>
2626
#include <algorithm>
27+
#include <memory>
28+
#include <sstream>
2729

2830
using namespace Exiv2;
2931

3032
TEST(PngChunk, keyTxtChunkExtractsKeywordCorrectlyInPresenceOfNullChar)
3133
{
3234
// 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};
35+
const std::array<std::uint8_t, 32> data{0x00, 0x00, 0x22, 0x41, 0x7a, 0x54, 0x58, 0x74,
36+
0x52, 0x61, 0x77, 0x20, 0x70, 0x72, 0x6f, 0x66,
37+
0x69, 0x6c, 0x65, 0x20, 0x74, 0x79, 0x70, 0x65,
38+
0x20, 0x65, 0x78, 0x69, 0x66, 0x00, 0x00, 0x78};
3739

3840
DataBuf chunkBuf(data.data(), static_cast<long>(data.size()));
3941
DataBuf key = Internal::PngChunk::keyTXTChunk(chunkBuf, true);
@@ -46,11 +48,167 @@ TEST(PngChunk, keyTxtChunkExtractsKeywordCorrectlyInPresenceOfNullChar)
4648
TEST(PngChunk, keyTxtChunkThrowsExceptionWhenThereIsNoNullChar)
4749
{
4850
// 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};
51+
const std::array<std::uint8_t, 30> data{0x00, 0x00, 0x22, 0x41, 0x7a, 0x54, 0x58, 0x74,
52+
0x52, 0x61, 0x77, 0x20, 0x70, 0x72, 0x6f, 0x66,
53+
0x69, 0x6c, 0x65, 0x20, 0x74, 0x79, 0x70, 0x65,
54+
0x20, 0x65, 0x78, 0x69, 0x66, 0x78};
5355

5456
DataBuf chunkBuf(data.data(), static_cast<long>(data.size()));
5557
ASSERT_THROW(Internal::PngChunk::keyTXTChunk(chunkBuf, true), Exiv2::Error);
5658
}
59+
60+
TEST(PngChunk, keyTxtChunkThrowsIfSizeIsNotEnough)
61+
{
62+
const std::array<std::uint8_t, 4> data{0x00, 0x00, 0x22, 0x41};
63+
DataBuf chunkBuf(data.data(), static_cast<long>(data.size()));
64+
ASSERT_THROW(Internal::PngChunk::keyTXTChunk(chunkBuf, true), Exiv2::Error);
65+
66+
DataBuf emptyChunk(data.data(), 0);
67+
ASSERT_THROW(Internal::PngChunk::keyTXTChunk(emptyChunk, false), Exiv2::Error);
68+
}
69+
70+
71+
TEST(PngImage, canBeCreatedFromScratch)
72+
{
73+
auto memIo = std::make_unique<MemIo>();
74+
const bool create {true};
75+
ASSERT_NO_THROW(PngImage png(std::move(memIo), create));
76+
}
77+
78+
TEST(PngImage, canBeOpenedEvenWithAnEmptyMemIo)
79+
{
80+
auto memIo = std::make_unique<MemIo>();
81+
const bool create {false};
82+
ASSERT_NO_THROW(PngImage png(std::move(memIo), create));
83+
}
84+
85+
TEST(PngImage, mimeTypeIsPng)
86+
{
87+
auto memIo = std::make_unique<MemIo>();
88+
const bool create {true};
89+
PngImage png(std::move(memIo), create);
90+
91+
ASSERT_EQ("image/png", png.mimeType());
92+
}
93+
94+
TEST(PngImage, printStructurePrintsNothingWithKpsNone)
95+
{
96+
auto memIo = std::make_unique<MemIo>();
97+
const bool create {true};
98+
PngImage png(std::move(memIo), create);
99+
100+
std::ostringstream stream;
101+
png.printStructure(stream, Exiv2::kpsNone, 1);
102+
103+
ASSERT_TRUE(stream.str().empty());
104+
}
105+
106+
TEST(PngImage, printStructurePrintsDataWithKpsBasic)
107+
{
108+
auto memIo = std::make_unique<MemIo>();
109+
const bool create {true};
110+
PngImage png(std::move(memIo), create);
111+
112+
std::ostringstream stream;
113+
png.printStructure(stream, Exiv2::kpsBasic, 1);
114+
115+
ASSERT_FALSE(stream.str().empty());
116+
}
117+
118+
TEST(PngImage, cannotReadMetadataFromEmptyIo)
119+
{
120+
auto memIo = std::make_unique<MemIo>();
121+
const bool create {false};
122+
PngImage png(std::move(memIo), create);
123+
124+
try {
125+
png.readMetadata();
126+
FAIL();
127+
} catch (const Exiv2::Error& e) {
128+
ASSERT_EQ(kerNotAnImage, e.code());
129+
ASSERT_STREQ("This does not look like a PNG image", e.what());
130+
}
131+
}
132+
133+
TEST(PngImage, cannotReadMetadataFromIoWhichCannotBeOpened)
134+
{
135+
auto memIo = std::make_unique<FileIo>("NonExistingPath.png");
136+
const bool create {false};
137+
PngImage png(std::move(memIo), create);
138+
139+
try {
140+
png.readMetadata();
141+
FAIL();
142+
} catch (const Exiv2::Error& e) {
143+
ASSERT_EQ(kerDataSourceOpenFailed, e.code());
144+
}
145+
}
146+
147+
TEST(PngImage, cannotWriteMetadataToEmptyIo)
148+
{
149+
auto memIo = std::make_unique<MemIo>();
150+
const bool create {false};
151+
PngImage png(std::move(memIo), create);
152+
153+
try {
154+
png.writeMetadata();
155+
FAIL();
156+
} catch (const Exiv2::Error& e) {
157+
ASSERT_EQ(kerNoImageInInputData, e.code());
158+
}
159+
}
160+
161+
TEST(PngImage, cannotWriteMetadataToIoWhichCannotBeOpened)
162+
{
163+
auto memIo = std::make_unique<FileIo>("NonExistingPath.png");
164+
const bool create {false};
165+
PngImage png(std::move(memIo), create);
166+
167+
try {
168+
png.readMetadata();
169+
FAIL();
170+
} catch (const Exiv2::Error& e) {
171+
ASSERT_EQ(kerDataSourceOpenFailed, e.code());
172+
}
173+
}
174+
175+
176+
TEST(isPngType, withValidSignatureReturnsTrue)
177+
{
178+
const unsigned char pngSignature[8] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
179+
MemIo memIo(pngSignature, 8);
180+
ASSERT_TRUE(isPngType(memIo, false));
181+
}
182+
183+
TEST(isPngType, withInvalidSignatureReturnsFalse)
184+
{
185+
const unsigned char pngSignature[8] = { 0x69, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
186+
MemIo memIo(pngSignature, 8);
187+
ASSERT_FALSE(isPngType(memIo, false));
188+
}
189+
190+
TEST(isPngType, withShorterDataReturnsFalse)
191+
{
192+
const unsigned char pngSignature[6] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A};
193+
MemIo memIo(pngSignature, 6);
194+
ASSERT_FALSE(isPngType(memIo, false));
195+
}
196+
197+
TEST(isPngType, withEmptyDataReturnsFalse)
198+
{
199+
MemIo memIo;
200+
ASSERT_FALSE(isPngType(memIo, false));
201+
}
202+
203+
TEST(isPngType, withMemIoInErroneousStatusThrows)
204+
{
205+
MemIo memIo;
206+
memIo.getb();
207+
208+
try {
209+
isPngType(memIo, false);
210+
FAIL();
211+
} catch (const Exiv2::Error& e) {
212+
ASSERT_EQ(kerInputDataReadFailed, e.code());
213+
}
214+
}

0 commit comments

Comments
 (0)