Tag Parser  6.4.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
testfilecheck.cpp
Go to the documentation of this file.
1 #include "../caseinsensitivecomparer.h"
2 
3 #include <c++utilities/tests/testutils.h>
4 #include <c++utilities/conversion/stringbuilder.h>
5 #include <c++utilities/io/catchiofailure.h>
6 
7 #include <cppunit/extensions/HelperMacros.h>
8 #include <cppunit/TestFixture.h>
9 
10 #include <openssl/sha.h>
11 
12 #include <fstream>
13 
14 using namespace std;
15 using namespace ConversionUtilities;
16 using namespace IoUtilities;
17 using namespace TestUtilities;
18 using namespace Media;
19 
20 using namespace CPPUNIT_NS;
21 
26 {
27  char checksum[65];
28  bool operator ==(const Sha256Checksum &other) const;
29 };
30 
35 {
36  for(const char *i1 = checksum, *i2 = other.checksum, *end = checksum + sizeof(checksum); i1 != end; ++i1, ++i2) {
37  if(CaseInsensitiveCharComparer::toLower(static_cast<unsigned char>(*i1))
38  != CaseInsensitiveCharComparer::toLower(static_cast<unsigned char>(*i2))) {
39  return false;
40  }
41  }
42  return true;
43 }
44 
45 ostream &operator<<(ostream &os, const Sha256Checksum &checksum)
46 {
47  os << checksum.checksum;
48  return os;
49 }
50 
54 struct TestFile
55 {
56  const char *path;
58  Sha256Checksum computeSha256Sum() const;
59  void verifyChecksum() const;
60 } const testFiles[] = {
61  {"matroska_wave1/logo3_256x256.png", {"810b9172607e281d9a3969018c7d6521de240cc3688fecf598444e666aa6b4dc"}},
62  {"matroska_wave1/test1.mkv", {"0996a309ff2095910b9d30d5253b044d637154297ddf7d0bda7f3adedf5addc1"}},
63  {"matroska_wave1/test2.mkv", {"5b53d306e56f9bda6e80c3fbd9f3ccd20cc885770449d1fc0b5bec35c71d61e2"}},
64  {"matroska_wave1/test3.mkv", {"1722b0d93a6ef1a14dd513bd031cd5901c233b45aa3e3c87be0b0d7348d7d1b5"}},
65  {"matroska_wave1/test4.mkv", {"43df750a2a01a37949791b717051b41522081a266b71d113be4b713063843699"}},
66  {"matroska_wave1/test5.mkv", {"92acdc33bb0b5d7a4d9b0d6ca792230a78c786a30179dc9999cee41c28642842"}},
67  {"matroska_wave1/test6.mkv", {"7cad84b434116e023d340dd584ac833b93f03fb1bd7ea2727fa45de50af0abb9"}},
68  {"matroska_wave1/test7.mkv", {"95b21c92ad5a4fe00914ff5009e2a64f12fd4c5fb9cb1c3c888ab50bf0ffe483"}},
69  {"matroska_wave1/test8.mkv", {"9dddcd1550b814dae44d62e2b9f27c0eca31d5e190df2220cbf7492e3d6c63da"}},
70  {"mtx-test-data/mkv/handbrake-chapters-2.mkv", {"eccc55f3b59a77086b3ffb914525d312c7886eae34e3933352dea2f6f6a1974c"}},
71  {"mtx-test-data/mkv/tags.mkv", {"4330019afc3d846600c1ded38158fcac081297f4e56c749251c236f4871e0287"}},
72  {"mkv/nested-tags.xml", {"85cfcc94920f114e52fd1aa3df24706cd2710626e065a2c8c55dd209ec8dc8ce"}},
73  {"mp4/test1.m4a", {"4f16e0a22525bd13ba859431406d7f5991e0b4f155c51e10e5f32b0c97034b36"}},
74  {"mtx-test-data/aac/he-aacv2-ps.m4a", {"be54be0ae45b0184583ced8a84a881a1652a449feb7f6a917e11f60efabb68ac"}},
75  {"mtx-test-data/alac/othertest-itunes.m4a", {"5e9c64cde00902211533fbe38aaa67ef5f79a945e1d717951b78b4bbf9ff84e8"}},
76  {"mtx-test-data/mp3/id3-tag-and-xing-header.mp3", {"4a9187b05dc74d32e5a3de53494fde9db8c6c25d46082f86de6f424ad28daacf"}},
77  {"mtx-test-data/mp4/10-DanseMacabreOp.40.m4a", {"30c915d5656de049d66fd70b0966a33faf038af42365a2bb973e5c2fc0ba2038"}},
78  {"mtx-test-data/mp4/1080p-DTS-HD-7.1.mp4", {"fbf929bf8300fc6e53c5c5b7fde4ed2a427fef2d4fd093511c672083039abbf1"}},
79  {"mtx-test-data/mp4/dash/dragon-age-inquisition-H1LkM6IVlm4-video.mp4", {"864891f4510f3fa9c49c19e671171cec08ceb331362cf7161419b957be090d47"}},
80  {"mtx-test-data/ogg/qt4dance_medium.ogg", {"0b5429da9713be171c6ae0da69621261e8d5ddc9db3da872e5ade1a1c883decd"}},
81  {"mtx-test-data/opus/v-opus.ogg", {"e12adece4dbcccf2471b61c3ebd7c6576dee351d85809ab6f01d6f324d65b417"}},
82 };
83 
88 {
89  // init sha256 hashing
90  SHA256_CTX sha256;
91  SHA256_Init(&sha256);
92 
93  // read and hash file
94  {
95  ifstream file;
96  file.exceptions(ios_base::eofbit | ios_base::failbit | ios_base::badbit);
97  file.open(testFilePath(path), ios_base::in | ios_base::binary);
98 
99  char readBuffer[4096];
100  try {
101  for(;;) {
102  file.read(readBuffer, sizeof(readBuffer));
103  SHA256_Update(&sha256, readBuffer, static_cast<size_t>(file.gcount()));
104  }
105  } catch(...) {
106  catchIoFailure();
107  if(file.eof() && !file.bad()) {
108  SHA256_Update(&sha256, readBuffer, static_cast<size_t>(file.gcount()));
109  } else {
110  throw;
111  }
112  }
113  }
114 
115  // compute final hash
116  unsigned char hash[SHA256_DIGEST_LENGTH];
117  SHA256_Final(hash, &sha256);
118 
119  // convert to "hex string"
120  Sha256Checksum hexString;
121  char *hexStringIterator = hexString.checksum;
122  for(unsigned char hashNumber : hash) {
123  const string digits = numberToString(hashNumber, 16);
124  *(hexStringIterator++) = digits.size() < 2 ? '0' : digits.front();
125  *(hexStringIterator++) = digits.back();
126  }
127  *hexStringIterator = '\0';
128  return hexString;
129 }
130 
135 {
136  CPPUNIT_ASSERT_EQUAL_MESSAGE(argsToString("integrity of testfile \"", path, '\"'),
137  expectedSha256sum,
138  computeSha256Sum());
139 }
140 
145 class TestFileCheck : public TestFixture
146 {
147  CPPUNIT_TEST_SUITE(TestFileCheck);
148  CPPUNIT_TEST(verifyChecksums);
149  CPPUNIT_TEST_SUITE_END();
150 
151 private:
152  void verifyChecksums();
153 
154 };
155 
157 
158 void TestFileCheck::verifyChecksums()
159 {
160  for(const TestFile &testFile : testFiles) {
161  testFile.verifyChecksum();
162  }
163 }
CPPUNIT_TEST_SUITE_REGISTRATION(TestFileCheck)
Sha256Checksum expectedSha256sum
The TestFileCheck class verifies integrity of all testfiles used in the testsuite of tagparser or tag...
STL namespace.
const char * path
Sha256Checksum computeSha256Sum() const
Computes the SHA-256 checksums for the file using OpenSSL.
Contains utility classes helping to read and write streams.
The TestFile struct holds the path (relative to testfile dir) and checksum of a test file...
bool operator==(const Sha256Checksum &other) const
Returns whether the current instance equals other ignoring the case.
constexpr bool operator==(byte lhs, FlacMetaDataBlockType type)
Definition: flacmetadata.h:28
The Sha256Checksum struct holds the "hex string representation" of a SHA-256 checksum.
void verifyChecksum() const
Checks whether the expected SHA-256 checksum matches the actual checksum.
ostream & operator<<(ostream &os, const Sha256Checksum &checksum)
Contains all classes and functions of the TagInfo library.
Definition: exceptions.h:9
struct TestFile testFiles[]