27#include <wx/filename.h>
29#include <wx/stdpaths.h>
42const char* c_opensshEncrypted =
"-----BEGIN OPENSSH PRIVATE KEY-----\n"
43 "b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABAxS2G8mY\n"
44 "+85LKkVcFtJi1GAAAAGAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAAIKB0oSTjiyvxC+qh\n"
45 "cJ2zEnPDzZWArHcl90v2Se1SkmoGAAAAkKKrIYipMtR+JUEsuoYSmnfD0b/ng57Fq2uj25\n"
46 "oHDRqrDyPKS0xqQJa9+IQvPeM+E3G1fAGvsOYTQXsLJzXuQDiLX3vceEuN9AmJkA9Naea4\n"
47 "qAPy8CTlBx/ktdd4YgnYXBETi6Ab1f/h7LAVlLJTOc9Ta9HilKzO8Vl5p3EUS1PWkfGGqe\n"
48 "QLo1/ryfJjC3ZZbA==\n"
49 "-----END OPENSSH PRIVATE KEY-----\n";
52const char* c_opensshPlain =
"-----BEGIN OPENSSH PRIVATE KEY-----\n"
53 "b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW\n"
54 "QyNTUxOQAAACD4Xd9qfxjO+vpBXO94Otoa0kg/p5H8kMYhN6eFe7v8MgAAAIjYZhWO2GYV\n"
55 "jgAAAAtzc2gtZWQyNTUxOQAAACD4Xd9qfxjO+vpBXO94Otoa0kg/p5H8kMYhN6eFe7v8Mg\n"
56 "AAAEBNlyz61Kiufy1a3WO6i2I7vB+7W2iQc8R81H0qrBheSfhd32p/GM76+kFc73g62hrS\n"
57 "SD+nkfyQxiE3p4V7u/wyAAAAAXQBAgME\n"
58 "-----END OPENSSH PRIVATE KEY-----\n";
61const char* c_opensshTruncated =
"-----BEGIN OPENSSH PRIVATE KEY-----\n"
63 "-----END OPENSSH PRIVATE KEY-----\n";
66const char* c_pemEncrypted =
"-----BEGIN EC PRIVATE KEY-----\n"
67 "Proc-Type: 4,ENCRYPTED\n"
68 "DEK-Info: AES-128-CBC,10297828DB5031A5C22276D18E077B61\n"
70 "rHfBqMZtByvcsRb7zgNtkSjhFgnRY3jpyyagjM8jxNoEDRaSSHNSugON80psxytE\n"
71 "HBTGShcJbDXOC7EER7QZOTjM/d+3DB+FEL4EwnJxgnVfWN/5p9hP3CoJLgeItkQV\n"
72 "POqt3mQ1V8AnJibunyn13YrczHM39IFruBB9JvNKKG+i/7yf4Jy4jCB9nicH7TC+\n"
73 "hQCu/mOMde02ziwULJiWIU3EALSYo6JhDJxTRIqUr7TDGXEAPcxpQEvMDz+HAgzY\n"
74 "9zTU6LC/CSbHQNjHYSvgDY4zRDFWxeHgNeQVfqrlOPGGE8NC5HCeo5i1r+rSVPn+\n"
75 "kzqXcGpwN682lM/oJV4ix3her0jf59RnAPKYwyWXfhUwYFtzFwroOoquFi6+HHVT\n"
76 "LlzJacbVVj/GTBOdHBSsu8cfiVLymHhEEzQCyIYY05DeHFe8GiSwDDdvMCPppX38\n"
77 "/R3PnNaEYwOEUK4o50nsWEfPHQw8kZaUkiUcX7uIyV4=\n"
78 "-----END EC PRIVATE KEY-----\n";
81const char* c_pemPlain =
"-----BEGIN EC PRIVATE KEY-----\n"
82 "MIIBaAIBAQQgGuajOowPzHaTJKkj1W+RQS0p87K8bEkZbgbw5pmVArmggfowgfcC\n"
83 "AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////\n"
84 "MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57Pr\n"
85 "vVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEE\n"
86 "axfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54W\n"
87 "K84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8\n"
88 "YyVRAgEBoUQDQgAELhPMgsc2xVYFBrKzqXq8CBRS8t9BBCnznM96FcZFv1MvdbIK\n"
89 "yn1nix57Bwq0VoyooErjaZ3SpXl47JlJpIsm4A==\n"
90 "-----END EC PRIVATE KEY-----\n";
93const char* c_pkcs8Encrypted =
"-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
94 "MIIBzzBJBgkqhkiG9w0BBQ0wPDAbBgkqhkiG9w0BBQwwDgQImaRUWsA/SFECAggA\n"
95 "MB0GCWCGSAFlAwQBAgQQiywEwYp7En3NzOemv0ek9QSCAYAIh56t3GB2r1S/B7Cy\n"
96 "UFS4qn1DvGDuhubEl6cw3n8Y1PnWj6ryq6O9Pw/DKdnKonzVN+PVaofXg26ZPLdR\n"
97 "q+J7fmcMPIEC0DqV/6T+oGqWp2iaiTZ11BBOoyBkd8ev59c/TxngOWlbJvvmGhEF\n"
98 "mOiCwXKoKjzU8kSVbmL+vFe1thZjrCaihL79JsZE6Ws5bd7yAn6grPBPI8sBexNi\n"
99 "fERG81T9Nu2mxCEd7zkNAoK0cZBnUwFvxDama18wMvXRYzEHETCUCWevoNFdcwo/\n"
100 "IkGgLciMxtSBBOXyWiVvAzTloSGHa5wafptTsgyTc1HKZBTR0ZTNvBX37aziggWE\n"
101 "u4fMHcHlAJwP4UXgCACtgcfktVhjohgNQdBm5RB95qEUdNAQDQSe4Ro27HfT0YyR\n"
102 "AHZYNXziRjZHsO6wqGarcxdZkH1Cy8Ca9FJr3uRWWr+8CWIjE9nDEkFbXUyQGOPq\n"
103 "sTAC7FpnQHVczHcaa45jXYaPmU/iNFnPkO2DzxWADe/1xIg=\n"
104 "-----END ENCRYPTED PRIVATE KEY-----\n";
109 GitInitGuard() { git_libgit2_init(); }
110 ~GitInitGuard() { git_libgit2_shutdown(); }
120 auto now = std::chrono::steady_clock::now().time_since_epoch();
121 long long ticks = std::chrono::duration_cast<std::chrono::nanoseconds>( now ).count();
123 wxString base = wxFileName::GetTempDir();
125 fn.AssignDir( base );
126 fn.AppendDir( wxString::Format(
"kicad-qa-git-%lld-%p", ticks,
this ) );
127 wxFileName::Mkdir( fn.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
134 char* resolved = realpath( fn.GetPath().utf8_string().c_str(),
nullptr );
138 path = wxString::FromUTF8( resolved );
152 if( !
path.IsEmpty() )
153 wxFileName::Rmdir(
path, wxPATH_RMDIR_RECURSIVE );
159git_repository* makeRepoWithCommit(
const wxString& aRepoPath,
const wxString& aFileName,
160 const std::string& aFileContents )
162 git_repository* repo =
nullptr;
163 git_repository_init_options init_opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
164 init_opts.flags = GIT_REPOSITORY_INIT_MKPATH;
165 init_opts.initial_head =
"main";
167 BOOST_REQUIRE_EQUAL( git_repository_init_ext( &repo, aRepoPath.utf8_string().c_str(),
172 wxFileName filePath( aRepoPath, aFileName );
174 std::ofstream f( filePath.GetFullPath().utf8_string() );
179 git_config* cfg =
nullptr;
180 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
182 git_config_set_string( cfg,
"user.name",
"QA Test" );
186 git_index*
index =
nullptr;
187 BOOST_REQUIRE_EQUAL( git_repository_index( &
index, repo ), GIT_OK );
189 BOOST_REQUIRE_EQUAL( git_index_add_bypath(
index, aFileName.utf8_string().c_str() ), GIT_OK );
190 BOOST_REQUIRE_EQUAL( git_index_write(
index ), GIT_OK );
193 BOOST_REQUIRE_EQUAL( git_index_write_tree( &tree_oid,
index ), GIT_OK );
195 git_tree* tree =
nullptr;
196 BOOST_REQUIRE_EQUAL( git_tree_lookup( &tree, repo, &tree_oid ), GIT_OK );
199 git_signature* sig =
nullptr;
200 BOOST_REQUIRE_EQUAL( git_signature_now( &sig,
"QA Test",
"[email protected]" ), GIT_OK );
204 BOOST_REQUIRE_EQUAL( git_commit_create( &commit_oid, repo,
"HEAD", sig, sig,
nullptr,
205 "initial", tree, 0,
nullptr ),
226 git_repository* repo = makeRepoWithCommit( tmp.
path,
"file.txt",
"data\n" );
232 BOOST_CHECK( !root.IsEmpty() );
233 BOOST_CHECK_MESSAGE( !root.Contains( wxS(
"/.git" ) ) && !root.Contains( wxS(
"\\.git" ) ),
234 "GetGitRootDirectory should return the working directory, got: "
235 + root.ToStdString() );
239 rootFn.AssignDir( root );
240 rootFn.MakeAbsolute();
242 tmpFn.AssignDir( tmp.
path );
243 tmpFn.MakeAbsolute();
244 BOOST_CHECK_EQUAL( rootFn.GetFullPath().ToStdString(), tmpFn.GetFullPath().ToStdString() );
256 git_repository* repo = makeRepoWithCommit( tmp.
path,
"file.txt",
"data\n" );
264 git_remote* remote =
nullptr;
265 BOOST_REQUIRE_EQUAL( git_remote_create( &remote, repo,
"github",
279 git_repository* repo = makeRepoWithCommit( tmp.
path,
"file.txt",
"data\n" );
282 git_remote* remote =
nullptr;
283 BOOST_REQUIRE_EQUAL( git_remote_create( &remote, repo,
"origin",
301 git_repository* repo = makeRepoWithCommit( tmp.
path,
"file.txt",
"data\n" );
307 BOOST_CHECK_MESSAGE( local.empty(),
308 "Expected no AHEAD files when no upstream is configured, got "
309 + std::to_string( local.size() ) );
310 BOOST_CHECK_MESSAGE( remote.empty(),
311 "Expected no BEHIND files when no upstream is configured, got "
312 + std::to_string( remote.size() ) );
329 git_repository* repo = makeRepoWithCommit( tmp.
path,
"untouched.txt",
"stable\n" );
334 wxFileName extra( tmp.
path, wxS(
"second.txt" ) );
335 std::ofstream f( extra.GetFullPath().utf8_string() );
339 git_index*
index =
nullptr;
340 BOOST_REQUIRE_EQUAL( git_repository_index( &
index, repo ), GIT_OK );
342 BOOST_REQUIRE_EQUAL( git_index_add_bypath(
index,
"second.txt" ), GIT_OK );
343 BOOST_REQUIRE_EQUAL( git_index_write(
index ), GIT_OK );
346 BOOST_REQUIRE_EQUAL( git_index_write_tree( &newTreeOid,
index ), GIT_OK );
348 git_tree* newTree =
nullptr;
349 BOOST_REQUIRE_EQUAL( git_tree_lookup( &newTree, repo, &newTreeOid ), GIT_OK );
352 git_reference* head =
nullptr;
353 BOOST_REQUIRE_EQUAL( git_repository_head( &head, repo ), GIT_OK );
356 git_commit* parentCommit =
nullptr;
358 git_commit_lookup( &parentCommit, repo, git_reference_target( head ) ), GIT_OK );
361 git_signature* sig =
nullptr;
362 BOOST_REQUIRE_EQUAL( git_signature_now( &sig,
"QA Test",
"[email protected]" ), GIT_OK );
365 const git_commit* parents[1] = { parentCommit };
366 git_oid secondCommitOid;
367 BOOST_REQUIRE_EQUAL( git_commit_create( &secondCommitOid, repo,
"HEAD", sig, sig,
nullptr,
368 "second", newTree, 1, parents ),
372 git_treebuilder* tb =
nullptr;
373 BOOST_REQUIRE_EQUAL( git_treebuilder_new( &tb, repo,
nullptr ), GIT_OK );
376 const std::string upstreamData =
"upstream\n";
377 BOOST_REQUIRE_EQUAL( git_blob_create_from_buffer( &blobOid, repo, upstreamData.data(),
378 upstreamData.size() ),
380 BOOST_REQUIRE_EQUAL( git_treebuilder_insert(
nullptr, tb,
"remote_only.txt", &blobOid,
384 git_oid remoteTreeOid;
385 BOOST_REQUIRE_EQUAL( git_treebuilder_write( &remoteTreeOid, tb ), GIT_OK );
386 git_treebuilder_free( tb );
388 git_tree* remoteTree =
nullptr;
389 BOOST_REQUIRE_EQUAL( git_tree_lookup( &remoteTree, repo, &remoteTreeOid ), GIT_OK );
392 git_oid remoteCommitOid;
393 BOOST_REQUIRE_EQUAL( git_commit_create( &remoteCommitOid, repo,
nullptr, sig, sig,
nullptr,
394 "upstream root", remoteTree, 0,
nullptr ),
397 git_reference* remoteRef =
nullptr;
398 BOOST_REQUIRE_EQUAL( git_reference_create( &remoteRef, repo,
"refs/remotes/origin/main",
399 &remoteCommitOid,
true,
nullptr ),
403 git_config* cfg =
nullptr;
404 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
406 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.remote",
"origin" ),
408 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.merge",
"refs/heads/main" ),
419 BOOST_CHECK_MESSAGE( local.find( wxS(
"untouched.txt" ) ) == local.end(),
420 "untouched.txt should not be reported as AHEAD when the local and "
421 "remote histories share no ancestor" );
422 BOOST_CHECK_MESSAGE( remote.find( wxS(
"untouched.txt" ) ) == remote.end(),
423 "untouched.txt should not be reported as BEHIND when the local "
424 "and remote histories share no ancestor" );
437 git_repository* repo = makeRepoWithCommit( tmp.
path,
"untouched.txt",
"stable\n" );
441 wxFileName extra( tmp.
path, wxS(
"touched.txt" ) );
442 std::ofstream f( extra.GetFullPath().utf8_string() );
446 git_index*
index =
nullptr;
447 BOOST_REQUIRE_EQUAL( git_repository_index( &
index, repo ), GIT_OK );
449 BOOST_REQUIRE_EQUAL( git_index_add_bypath(
index,
"touched.txt" ), GIT_OK );
450 BOOST_REQUIRE_EQUAL( git_index_write(
index ), GIT_OK );
453 BOOST_REQUIRE_EQUAL( git_index_write_tree( &treeOid,
index ), GIT_OK );
455 git_tree* tree =
nullptr;
456 BOOST_REQUIRE_EQUAL( git_tree_lookup( &tree, repo, &treeOid ), GIT_OK );
459 git_reference* head =
nullptr;
460 BOOST_REQUIRE_EQUAL( git_repository_head( &head, repo ), GIT_OK );
463 git_commit* parent =
nullptr;
464 BOOST_REQUIRE_EQUAL( git_commit_lookup( &parent, repo, git_reference_target( head ) ),
468 git_signature* sig =
nullptr;
469 BOOST_REQUIRE_EQUAL( git_signature_now( &sig,
"QA Test",
"[email protected]" ), GIT_OK );
472 const git_commit* parents[1] = { parent };
474 BOOST_REQUIRE_EQUAL( git_commit_create( &base_oid, repo,
"HEAD", sig, sig,
nullptr,
475 "add touched", tree, 1, parents ),
480 git_remote* origin =
nullptr;
481 BOOST_REQUIRE_EQUAL( git_remote_create( &origin, repo,
"origin",
486 git_reference* upstream_ref =
nullptr;
487 BOOST_REQUIRE_EQUAL( git_reference_create( &upstream_ref, repo,
"refs/remotes/origin/main",
488 &base_oid,
true,
nullptr ),
492 git_config* cfg =
nullptr;
493 BOOST_REQUIRE_EQUAL( git_repository_config( &cfg, repo ), GIT_OK );
495 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.remote",
"origin" ),
497 BOOST_REQUIRE_EQUAL( git_config_set_string( cfg,
"branch.main.merge",
"refs/heads/main" ),
502 wxFileName extra( tmp.
path, wxS(
"touched.txt" ) );
503 std::ofstream f( extra.GetFullPath().utf8_string() );
507 BOOST_REQUIRE_EQUAL( git_index_add_bypath(
index,
"touched.txt" ), GIT_OK );
508 BOOST_REQUIRE_EQUAL( git_index_write(
index ), GIT_OK );
511 BOOST_REQUIRE_EQUAL( git_index_write_tree( &newTreeOid,
index ), GIT_OK );
513 git_tree* newTree =
nullptr;
514 BOOST_REQUIRE_EQUAL( git_tree_lookup( &newTree, repo, &newTreeOid ), GIT_OK );
517 git_commit* baseCommit =
nullptr;
518 BOOST_REQUIRE_EQUAL( git_commit_lookup( &baseCommit, repo, &base_oid ), GIT_OK );
521 const git_commit* aheadParents[1] = { baseCommit };
523 BOOST_REQUIRE_EQUAL( git_commit_create( &aheadOid, repo,
"HEAD", sig, sig,
nullptr,
524 "modify touched", newTree, 1, aheadParents ),
530 BOOST_CHECK( remote.empty() );
531 BOOST_CHECK_MESSAGE( local.find( wxS(
"touched.txt" ) ) != local.end(),
532 "touched.txt should be reported as AHEAD" );
533 BOOST_CHECK_MESSAGE( local.find( wxS(
"untouched.txt" ) ) == local.end(),
534 "untouched.txt should NOT be reported as AHEAD - this was the "
535 "regression in issue 21576" );
542 "A passphrase-protected OpenSSH key must be reported as encrypted" );
545 "An OpenSSH key with cipher \"none\" must be reported as unencrypted" );
548 "An OpenSSH key with an unreadable body must not be reported as encrypted" );
555 "A PEM key with Proc-Type: 4,ENCRYPTED must be reported as encrypted" );
558 "A PEM key with no Proc-Type must be reported as unencrypted" );
565 "A PKCS#8 key headed ENCRYPTED PRIVATE KEY must be reported as encrypted" );
574 "A public key is not an encrypted private key" );
wxString GetGitRootDirectory() const
std::pair< std::set< wxString >, std::set< wxString > > GetDifferentFiles() const
Return a pair of sets of files that differ locally from the remote repository The first set is files ...
bool HasPushAndPullRemote() const
std::unique_ptr< git_tree, decltype([](git_tree *aTree) { git_tree_free(aTree); })> GitTreePtr
A unique pointer for git_tree objects with automatic cleanup.
bool IsPrivateKeyEncrypted(const wxString &aKeyText)
Report whether an SSH private key is protected by a passphrase.
std::unique_ptr< git_repository, decltype([](git_repository *aRepo) { git_repository_free(aRepo); })> GitRepositoryPtr
A unique pointer for git_repository objects with automatic cleanup.
std::unique_ptr< git_commit, decltype([](git_commit *aCommit) { git_commit_free(aCommit); })> GitCommitPtr
A unique pointer for git_commit objects with automatic cleanup.
std::unique_ptr< git_config, decltype([](git_config *aConfig) { git_config_free(aConfig); })> GitConfigPtr
A unique pointer for git_config objects with automatic cleanup.
std::unique_ptr< git_reference, decltype([](git_reference *aRef) { git_reference_free(aRef); })> GitReferencePtr
A unique pointer for git_reference objects with automatic cleanup.
std::unique_ptr< git_signature, decltype([](git_signature *aSignature) { git_signature_free(aSignature); })> GitSignaturePtr
A unique pointer for git_signature objects with automatic cleanup.
std::unique_ptr< git_index, decltype([](git_index *aIndex) { git_index_free(aIndex); })> GitIndexPtr
A unique pointer for git_index objects with automatic cleanup.
std::unique_ptr< git_remote, decltype([](git_remote *aRemote) { git_remote_free(aRemote); })> GitRemotePtr
A unique pointer for git_remote objects with automatic cleanup.
Scoped temporary directory used by the tests below.
ScopedTempDir(const wxString &aTag)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(GitRootDirectoryReturnsWorkdir)
BOOST_CHECK_EQUAL(result, "25.4")