Skip to content

Commit be0f1c8

Browse files
RobertLauferElektrobitcarneywebMichael Carney
authored
* Issue CppMicroServices#184: added check for bundle name matching manifest value Causes test failures... not exactly sure how to resolve them. * Updated test cases to work properly with new restrictions * added test for mismatched bundle name and bundle.symbolic_name * responded to comments and refactored some code. Refactored code is around the use of the option::Option array. Instead of a std::unique_ptr to an allocated array of option::Option object, use a std::vector and pass a reference to the vector to checkSanity(). This should avoid warnings about pointer arithmetic and is also more appropriate code for the use case. --------- Co-authored-by: carneyweb <mike@carneyweb.com> Co-authored-by: Michael Carney <carney@macmini.mynetworksettings.com>
1 parent 4687f17 commit be0f1c8

2 files changed

Lines changed: 72 additions & 32 deletions

File tree

framework/test/gtest/ResourceCompilerTest.cpp

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ namespace
7272
protected:
7373
std::string rcbinpath;
7474
std::string tempdir;
75-
76-
const std::string manifest_json = R"({
77-
"bundle.symbolic_name" : "main",
78-
"bundle.version" : "0.1.0",
79-
"bundle.activator" : true
80-
})";
8175
};
8276

8377
// A zip file containing only a manifest.
@@ -370,15 +364,26 @@ TEST_F(ResourceCompilerTest, testEscapePath)
370364

371365
TEST_F(ResourceCompilerTest, testManifestAdd)
372366
{
367+
const std::string manifest_json = R"({
368+
"bundle.symbolic_name" : "mybundle",
369+
"bundle.version" : "0.1.0",
370+
"bundle.activator" : true
371+
})";
373372
createDirHierarchy(tempdir, manifest_json);
374373

375-
std::ostringstream cmd;
376-
cmd << rcbinpath;
377-
cmd << " --bundle-name "
378-
<< "mybundle";
379-
cmd << " --out-file \"" << tempdir << "Example.zip\"";
380-
cmd << " --manifest-add \"" << tempdir << "manifest.json\"";
374+
std::ostringstream badcmd;
375+
badcmd << rcbinpath
376+
<< " --bundle-name mismatched_bundle_name"
377+
<< " --out-file \"" << tempdir << "Example.zip\""
378+
<< " --manifest-add \"" << tempdir << "manifest.json\"";
379+
// Test that invoking command with --bundle-name different from that in the manifest fails.
380+
ASSERT_NE(EXIT_SUCCESS, runExecutable(badcmd.str()));
381381

382+
std::ostringstream cmd;
383+
cmd << rcbinpath
384+
<< " --bundle-name mybundle"
385+
<< " --out-file \"" << tempdir << "Example.zip\""
386+
<< " --manifest-add \"" << tempdir << "manifest.json\"";
382387
// Test that Cmdline invocation in testManifestAdd returns 0
383388
ASSERT_EQ(EXIT_SUCCESS, runExecutable(cmd.str()));
384389

@@ -549,10 +554,16 @@ TEST_F(ResourceCompilerTest, testZipAddTwice)
549554
*/
550555
TEST_F(ResourceCompilerTest, testBundleManifestZipAdd)
551556
{
557+
const std::string manifest_json = R"({
558+
"bundle.symbolic_name" : "anotherbundle",
559+
"bundle.version" : "0.1.0",
560+
"bundle.activator" : true
561+
})";
562+
createManifestFile(tempdir, manifest_json, "manifest2.json");
563+
552564
std::ostringstream cmd;
553565
cmd << rcbinpath;
554-
cmd << " --bundle-name anotherbundle ";
555-
cmd << " --manifest-add \"" << tempdir << "manifest.json\" ";
566+
cmd << " --manifest-add \"" << tempdir << "manifest2.json\" ";
556567
cmd << " --bundle-file \"" << tempdir << "sample1.dll\" ";
557568
cmd << " --zip-add \"" << tempdir << "tomerge.zip\" ";
558569
cmd << " --zip-add \"" << tempdir << "Example2.zip\"";
@@ -645,7 +656,7 @@ TEST_F(ResourceCompilerTest, testDuplicateManifestFileAdd)
645656
{
646657
std::ostringstream cmd;
647658
cmd << rcbinpath;
648-
cmd << " --bundle-name multiple_dups ";
659+
cmd << " --bundle-name mybundle ";
649660
cmd << " --manifest-add \"" << tempdir << "manifest.json\" ";
650661
cmd << " --manifest-add \"" << tempdir << "manifest.json\" ";
651662
cmd << " --out-file \"" << tempdir << "testDuplicateManifestFileAdd.zip\" ";
@@ -857,6 +868,7 @@ TEST_F(ResourceCompilerTest, testManifestAddWithInvalidJSON)
857868

858869
TEST_F(ResourceCompilerTest, testUnicodeBundleFile)
859870
{
871+
860872
std::ostringstream cmd;
861873
cmd << rcbinpath;
862874
cmd << " --bundle-file \"" << tempdir << u8"foobar/myArchive00.zip\"";
@@ -915,13 +927,20 @@ TEST_F(ResourceCompilerTest, testUnicodeZipAdd)
915927

916928
TEST_F(ResourceCompilerTest, testUnicodeManifestAdd)
917929
{
930+
const std::string manifest_json = u8R"({
931+
"bundle.symbolic_name" : "myunicodebundle02-V",
932+
"bundle.version" : "0.1.0",
933+
"bundle.activator" : true
934+
})";
935+
createManifestFile(tempdir, manifest_json, "manifest3.json");
936+
918937
auto origdir = util::GetCurrentWorkingDirectory();
919938
ChangeDirectory(tempdir);
920939
std::ostringstream cmd;
921940
cmd << rcbinpath;
922941
cmd << " --bundle-file " << u8"foobar/myArchive05.zip";
923942
cmd << " --out-file unicodeResAdd02.zip";
924-
cmd << " --manifest-add " << u8"foobar/manifest.json";
943+
cmd << " --manifest-add " << u8"manifest3.json";
925944
cmd << " --bundle-name myunicodebundle02";
926945
cmd << "-V";
927946

@@ -941,21 +960,21 @@ TEST_F(ResourceCompilerTest, testManifestAddWithJSONComments)
941960
std::string jsonCommentSyntax(R"(
942961
{ /* no, no, no. */
943962
"bundle.name": "foo",
963+
"bundle.symbolic_name": "mybundle",
944964
"bundle.version" : "1.0.2",
945965
"bundle.description" : "This bundle shouldn't have comments!",
946966
"authors" : ["John Doe", "Douglas Reynolds", "Daniel Cannady"],
947967
"rating" : 5 // no comments allowed
948968
}
949969
)");
950970

951-
createManifestFile(tempdir, jsonCommentSyntax);
971+
createManifestFile(tempdir, jsonCommentSyntax, "manifest4.json");
952972

953973
std::ostringstream cmd;
954974
cmd << rcbinpath;
955-
cmd << " --bundle-name "
956-
<< "mybundle";
975+
cmd << " --bundle-name mybundle";
957976
cmd << " --out-file \"" << tempdir << "json_comment_syntax.zip\"";
958-
cmd << " --manifest-add \"" << tempdir << "manifest.json\"";
977+
cmd << " --manifest-add \"" << tempdir << "manifest4.json\"";
959978
// Test embedding a manifest containing JSON comments.
960979
ASSERT_EQ(EXIT_SUCCESS, runExecutable(cmd.str()));
961980
}
@@ -1320,7 +1339,8 @@ TEST_F(ResourceCompilerTest, testMultipleManifestConcatenation)
13201339
TEST_F(ResourceCompilerTest, testManifestWithNullTerminator)
13211340
{
13221341
const std::string manifest_json = R"({
1323-
"test" : {
1342+
"bundle.symbolic_name" : "main",
1343+
"test" : {
13241344
"bar" : "baz\\0bar",
13251345
"foo\\0bar" : [1, 2, 5, 7]
13261346
}})";

tools/rc/ResourceCompiler.cpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <fstream>
3030
#include <iostream>
3131
#include <list>
32-
#include <memory>
3332
#include <set>
3433
#include <stdexcept>
3534
#include <string>
@@ -416,6 +415,28 @@ ZipArchive::CheckAndAddToArchivedNames(std::string const& archiveEntry)
416415
void
417416
ZipArchive::AddManifestFile(Json::Value const& manifest)
418417
{
418+
// Check to make sure that the bundleName passed on the command line and the
419+
// bundle.symbolic_name in the manifest file match. If the bundleName is not passed in on the
420+
// command line, use the name specified in the manifest. If there's a mismatch or if no
421+
// bundleName is supplied in either location, it's an error.
422+
auto bname = manifest.get("bundle.symbolic_name", "");
423+
if (bname.isString()) {
424+
auto bnameStr = bname.asString();
425+
if (!bundleName.empty()) {
426+
if (bnameStr != bundleName) {
427+
throw std::runtime_error("Bundle name in manifest "
428+
+ bnameStr
429+
+ " does not match value supplied on command line "
430+
+ bundleName);
431+
}
432+
}
433+
else {
434+
bundleName = bnameStr;
435+
}
436+
}
437+
if (bundleName.empty()) {
438+
throw std::runtime_error("Bundle name is required. Make sure that \"bundle.symbolic_name\" is set in the manifest.json file.");
439+
}
419440
std::string styledManifestJson(manifest.toStyledString());
420441
std::string archiveEntry(bundleName + "/manifest.json");
421442

@@ -745,7 +766,7 @@ const option::Descriptor usage[] = {
745766

746767
// Check invalid invocations and errors
747768
static int
748-
checkSanity(option::Parser& parse, option::Option* options)
769+
checkSanity(option::Parser& parse, std::vector<option::Option>& options)
749770
{
750771
int return_code = EXIT_SUCCESS;
751772

@@ -792,17 +813,16 @@ checkSanity(option::Parser& parse, option::Option* options)
792813
return_code = EXIT_FAILURE;
793814
}
794815

795-
// If either --manifest-add or --res-add is given, --bundle-name must also be given.
796-
if ((options[MANIFESTADD] || options[RESADD]) && !options[BUNDLENAME])
816+
// If --res-add is given, --bundle-name must also be given.
817+
if (options[RESADD] && !options[BUNDLENAME])
797818
{
798-
std::cerr << "If either --manifest-add or --res-add is provided, "
799-
"--bundle-name must be provided."
819+
std::cerr << "If --res-add is provided, --bundle-name must be provided."
800820
<< std::endl;
801821
return_code = EXIT_FAILURE;
802822
}
803823

804824
// Generate a warning that --bundle-name is not necessary in following invocation.
805-
if (options[BUNDLENAME] && !options[MANIFESTADD] && !options[RESADD] && return_code != EXIT_FAILURE)
825+
if (options[BUNDLENAME] && !options[RESADD] && return_code != EXIT_FAILURE)
806826
{
807827
std::clog << "Warning: --bundle-name option is unnecessary here." << std::endl;
808828
}
@@ -835,17 +855,17 @@ main(int argc, char** argv)
835855
argc -= (argc > 0);
836856
argv += (argc > 0); // skip program name argv[0]
837857
option::Stats stats(usage, argc, argv);
838-
std::unique_ptr<option::Option[]> options(new option::Option[stats.options_max]);
839-
std::unique_ptr<option::Option[]> buffer(new option::Option[stats.buffer_max]);
840-
option::Parser parse(true, usage, argc, argv, options.get(), buffer.get());
858+
std::vector<option::Option> options { stats.options_max };
859+
std::vector<option::Option> buffer { stats.buffer_max };
860+
option::Parser parse(true, usage, argc, argv, options.data(), buffer.data());
841861

842862
if (argc == 0 || options[HELP])
843863
{
844864
option::printUsage(std::clog, usage);
845865
return return_code;
846866
}
847867

848-
return_code = checkSanity(parse, options.get());
868+
return_code = checkSanity(parse, options);
849869
if (return_code == EXIT_FAILURE)
850870
{
851871
return return_code;

0 commit comments

Comments
 (0)