KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_embedded_file_compress.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <magic_enum.hpp>
21#include <magic_enum_iostream.hpp>
22#include <boost/test/unit_test.hpp>
23#include <mmh3_hash.h>
24#include <embedded_files.h>
25
26#include <wx/wfstream.h>
27
28#include <random>
29using magic_enum::iostream_operators::operator<<;
30
31BOOST_AUTO_TEST_SUITE( EmbeddedFiles )
32
33BOOST_AUTO_TEST_CASE( CompressAndEncode_OK )
34{
36 file.name = "test_file";
37 std::string data = "Hello, World!";
38 file.decompressedData.assign(data.begin(), data.end());
39
41 hash.add( file.decompressedData );
42 file.data_hash = hash.digest().ToString();
43
46}
47
48BOOST_AUTO_TEST_CASE( DecompressAndDecode_OK )
49{
51 file.name = "test_file";
52 std::string data = "Hello, World!";
53 file.decompressedData.assign( data.begin(), data.end() );
54
56 hash.add( file.decompressedData );
57 file.data_hash = hash.digest().ToString();
58
61
64
65 // Create a large test data
66 data.clear();
67 data.reserve( 13 * 100000 + 1 );
68
69 for( int i = 0; i < 100000; ++i )
70 data += "Hello, World!";
71
72 file.decompressedData.assign( data.begin(), data.end() );
73
74 hash.reset();
75 hash.add( file.decompressedData );
76 file.data_hash = hash.digest().ToString();
77
80
83
84 // Create a sequential test dataset
85 data.clear();
86 data.reserve( 100000 );
87
88 for( int i = 0; i < 100000; ++i )
89 data += static_cast<char>( i % 256 );
90
91 file.decompressedData.assign( data.begin(), data.end() );
92 hash.reset();
93 hash.add( file.decompressedData );
94 file.data_hash = hash.digest().ToString();
95
98
101
102 // Create a random test dataset with a known seed
103 data.clear();
104 data.reserve( 100000 );
105
106 std::mt19937 rng;
107 rng.seed( 0 );
108
109 for( int i = 0; i < 100000; ++i )
110 data += static_cast<char>( rng() % 256 );
111
112 file.decompressedData.assign( data.begin(), data.end() );
113 hash.reset();
114 hash.add( file.decompressedData );
115 file.data_hash = hash.digest().ToString();
116
119
122
123}
124
125BOOST_AUTO_TEST_CASE( DecompressAndDecode_ChecksumError )
126{
128 file.name = "test_file";
129 std::string data = "Hello, World!";
130 file.decompressedData.assign(data.begin(), data.end());
131
134
135 // Modify the checksum
136 file.data_hash[0] = 'x';
137
140}
141
142
143BOOST_AUTO_TEST_CASE( ComputeFileHash_MatchesEmbeddedHash )
144{
145 // Create a temp file with known content
146 wxFileName tempFile = wxFileName::CreateTempFileName( wxS( "kicad_embed_test" ) );
147 std::string data = "Test file content for hash computation";
148
149 {
150 wxFFileOutputStream out( tempFile.GetFullPath() );
151 BOOST_REQUIRE( out.IsOk() );
152 out.Write( data.data(), data.size() );
153 }
154
155 // Compute hash via ComputeFileHash
156 std::string computedHash;
159 BOOST_CHECK( !computedHash.empty() );
160
161 // Embed the same file and verify hashes match
162 EMBEDDED_FILES files;
163 EMBEDDED_FILES::EMBEDDED_FILE* embedded = files.AddFile( tempFile, false );
164 BOOST_REQUIRE( embedded != nullptr );
165 BOOST_CHECK_EQUAL( computedHash, embedded->data_hash );
166
167 // Clean up
168 wxRemoveFile( tempFile.GetFullPath() );
169}
170
171
172BOOST_AUTO_TEST_CASE( ComputeFileHash_DifferentContent )
173{
174 // Create two temp files with different content
175 wxFileName tempFile1 = wxFileName::CreateTempFileName( wxS( "kicad_embed_test1" ) );
176 wxFileName tempFile2 = wxFileName::CreateTempFileName( wxS( "kicad_embed_test2" ) );
177 std::string data1 = "Content version 1";
178 std::string data2 = "Content version 2";
179
180 {
181 wxFFileOutputStream out1( tempFile1.GetFullPath() );
182 BOOST_REQUIRE( out1.IsOk() );
183 out1.Write( data1.data(), data1.size() );
184 }
185
186 {
187 wxFFileOutputStream out2( tempFile2.GetFullPath() );
188 BOOST_REQUIRE( out2.IsOk() );
189 out2.Write( data2.data(), data2.size() );
190 }
191
192 // Compute hashes for both files
193 std::string hash1, hash2;
198
199 // Hashes should be different
200 BOOST_CHECK_NE( hash1, hash2 );
201
202 // Clean up
203 wxRemoveFile( tempFile1.GetFullPath() );
204 wxRemoveFile( tempFile2.GetFullPath() );
205}
206
207
208BOOST_AUTO_TEST_CASE( ComputeFileHash_NonExistentFile )
209{
210 wxFileName nonExistent( wxS( "/nonexistent/path/file.txt" ) );
211 std::string hash;
212
215 BOOST_CHECK( hash.empty() );
216}
217
218
@ FILE_NOT_FOUND
File not found on disk.
@ CHECKSUM_ERROR
Checksum in file does not match data.
static RETURN_CODE DecompressAndDecode(EMBEDDED_FILE &aFile)
Takes data from the #compressedEncodedData buffer and Base64 decodes it.
static RETURN_CODE ComputeFileHash(const wxFileName &aFileName, std::string &aHash)
Compute the hash of a file on disk without fully embedding it.
EMBEDDED_FILE * AddFile(const wxFileName &aName, bool aOverwrite)
Load a file from disk and adds it to the collection.
static uint32_t Seed()
static RETURN_CODE CompressAndEncode(EMBEDDED_FILE &aFile)
Take data from the #decompressedData buffer and compresses it using ZSTD into the #compressedEncodedD...
A streaming C++ equivalent for MurmurHash3_x64_128.
Definition mmh3_hash.h:60
FORCE_INLINE void add(const std::string &input)
Definition mmh3_hash.h:95
FORCE_INLINE HASH_128 digest()
Definition mmh3_hash.h:114
FORCE_INLINE void reset(uint32_t aSeed=0)
Definition mmh3_hash.h:66
static boost::mt19937 rng
Definition kiid.cpp:52
std::vector< char > decompressedData
std::string ToString() const
Definition hash_128.h:47
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(CompressAndEncode_OK)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")