File indexing completed on 2024-05-05 17:52:11

0001 /*
0002     Copyright Hannah von Reth <vonreth@kde.org>
0003 
0004     Redistribution and use in source and binary forms, with or without
0005     modification, are permitted provided that the following conditions
0006     are met:
0007     1. Redistributions of source code must retain the above copyright
0008        notice, this list of conditions and the following disclaimer.
0009     2. Redistributions in binary form must reproduce the above copyright
0010        notice, this list of conditions and the following disclaimer in the
0011        documentation and/or other materials provided with the distribution.
0012 
0013     THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0014     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0015     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0016     ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0017     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0018     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0019     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0020     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0021     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0022     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0023     SUCH DAMAGE.
0024 */
0025 
0026 #include "test_config.h"
0027 #include "kshimdata.h"
0028 
0029 #include "nlohmann/json.hpp"
0030 
0031 namespace {
0032 
0033 // dump_args create a json file that we parse
0034 auto readArgs()
0035 {
0036     nlohmann::json json;
0037     std::ifstream in;
0038     in.open("dump_args.json");
0039     in >> json;
0040     in.close();
0041 
0042     return json["args"].get<std::vector<std::string>>();
0043 }
0044 }
0045 int main()
0046 {
0047     KLog::setLoggingEnabled(true);
0048     KLog::setStdLoggingEnabled(true);
0049 
0050     const auto kshimgen =
0051             (KShimTest::binaryDir() / KSTRING("kshimgen"s)).replace_extension(KShimLib::exeSuffix);
0052 
0053     const auto dump_args =
0054             (KShimTest::binaryDir() / KSTRING("dump_args"s)).replace_extension(KShimLib::exeSuffix);
0055 
0056 
0057     {
0058         // test whether the args are correctly passed
0059         kLog << "Test: args";
0060         const auto test =
0061                 (std::filesystem::current_path() / KSTRING("test"s)).replace_extension(KShimLib::exeSuffix);
0062 
0063 
0064         TEST_EQ(KShimLib::run(KShimData(kshimgen),
0065                               { KSTRING("--create"s), KSTRING("test"s), dump_args.native() }),
0066                 0);
0067 
0068         TEST_EQ(KShimLib::run(test, { KSTRING("-h"s) }), 0);
0069 
0070         auto args = readArgs();
0071         TEST_EQ(std::filesystem::path(args[0]).filename(), dump_args.filename());
0072         TEST_EQ(args[1], "-h");
0073     }
0074 
0075     {
0076         // test that arg0 is the name of the test
0077         kLog << "Test: --keep-arg0";
0078         const auto test =
0079                 (std::filesystem::current_path() / KSTRING("test2"s)).replace_extension(KShimLib::exeSuffix);
0080 
0081         TEST_EQ(KShimLib::run(KShimData(kshimgen),
0082                               { KSTRING("--create"s), KSTRING("test2"s), dump_args.native(),
0083                                 KSTRING("--keep-argv0"s) }),
0084                 0);
0085         TEST_EQ(KShimLib::run(test, { KSTRING("-h"s) }), 0);
0086 
0087         auto args = readArgs();
0088         TEST_EQ(std::filesystem::path(args[0]), test);
0089         TEST_EQ(args[1], "-h");
0090     }
0091 
0092     kLog << "End";
0093 }