2016-08-19 17:57:21 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2016-08-19 17:57:21 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2016-08-19 17:57:21 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2016-08-19 17:57:21 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2016-08-19 17:57:21 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2016-08-19 17:57:21 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <chris@ethereum.org>
|
|
|
|
* @date 2016
|
|
|
|
* Unit tests for the semantic versioning matcher.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <tuple>
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/Scanner.h>
|
2019-04-17 11:02:30 +00:00
|
|
|
#include <liblangutil/SemVerHandler.h>
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2022-03-15 18:53:29 +00:00
|
|
|
#include <test/libsolidity/util/SoltestErrors.h>
|
2016-08-19 17:57:21 +00:00
|
|
|
|
2020-01-14 14:55:31 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity::langutil;
|
2016-08-19 17:57:21 +00:00
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::frontend::test
|
2016-08-19 17:57:21 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(SemVerMatcher)
|
|
|
|
|
2020-12-08 20:06:10 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
SemVerMatchExpression parseExpression(std::string const& _input)
|
2016-08-19 17:57:21 +00:00
|
|
|
{
|
2021-07-14 10:53:39 +00:00
|
|
|
CharStream stream(_input, "");
|
|
|
|
Scanner scanner{stream};
|
2023-08-30 00:08:06 +00:00
|
|
|
std::vector<std::string> literals;
|
|
|
|
std::vector<Token> tokens;
|
2016-08-19 17:57:21 +00:00
|
|
|
while (scanner.currentToken() != Token::EOS)
|
|
|
|
{
|
|
|
|
auto token = scanner.currentToken();
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string literal = scanner.currentLiteral();
|
2018-10-22 14:48:21 +00:00
|
|
|
if (literal.empty() && TokenTraits::toString(token))
|
|
|
|
literal = TokenTraits::toString(token);
|
2016-08-19 17:57:21 +00:00
|
|
|
literals.push_back(literal);
|
|
|
|
tokens.push_back(token);
|
|
|
|
scanner.next();
|
|
|
|
}
|
|
|
|
|
2022-03-15 18:53:29 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
auto matchExpression = SemVerMatchExpressionParser(tokens, literals).parse();
|
|
|
|
|
|
|
|
BOOST_CHECK_MESSAGE(
|
|
|
|
matchExpression.isValid(),
|
|
|
|
"Expression \"" + _input + "\" did not parse properly."
|
|
|
|
);
|
|
|
|
|
|
|
|
return matchExpression;
|
|
|
|
}
|
|
|
|
catch (SemVerError const&)
|
|
|
|
{
|
|
|
|
// Ignored, since a test case should have a parsable version
|
|
|
|
soltestAssert(false);
|
|
|
|
}
|
2022-11-25 20:36:53 +00:00
|
|
|
|
|
|
|
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
|
|
|
|
util::unreachable();
|
2022-03-15 18:53:29 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 17:57:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 18:53:29 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(exception_on_invalid_version_in_semverversion_constructor)
|
|
|
|
{
|
|
|
|
BOOST_CHECK_EXCEPTION(
|
|
|
|
SemVerVersion version("1.2"),
|
|
|
|
SemVerError,
|
|
|
|
[&](auto const& _exception) { BOOST_TEST(_exception.what() == "Invalid versionString: 1.2"); return true; }
|
|
|
|
);
|
|
|
|
|
|
|
|
BOOST_CHECK_EXCEPTION(
|
|
|
|
SemVerVersion version("-1.2.0"),
|
|
|
|
SemVerError,
|
|
|
|
[&](auto const& _exception) { BOOST_TEST(_exception.what() == "Invalid versionString: -1.2.0"); return true; }
|
|
|
|
);
|
2020-12-08 20:06:10 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 17:57:21 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(positive_range)
|
|
|
|
{
|
|
|
|
// Positive range tests
|
2023-08-30 00:08:06 +00:00
|
|
|
std::vector<std::pair<std::string, std::string>> tests = {
|
2016-08-19 17:57:21 +00:00
|
|
|
{"*", "1.2.3-foo"},
|
|
|
|
{"1.0.0 - 2.0.0", "1.2.3"},
|
|
|
|
{"1.0.0", "1.0.0"},
|
2019-10-29 00:46:04 +00:00
|
|
|
{"1.0", "1.0.0"},
|
|
|
|
{"1", "1.0.0"},
|
2016-08-19 17:57:21 +00:00
|
|
|
{">=*", "0.2.4"},
|
|
|
|
{"*", "1.2.3"},
|
|
|
|
{">=1.0.0", "1.0.0"},
|
|
|
|
{">=1.0.0", "1.0.1"},
|
|
|
|
{">=1.0.0", "1.1.0"},
|
|
|
|
{">1.0.0", "1.0.1"},
|
|
|
|
{">1.0.0", "1.1.0"},
|
|
|
|
{"<=2.0.0", "2.0.0"},
|
|
|
|
{"<=2.0.0", "1.9999.9999"},
|
|
|
|
{"<=2.0.0", "0.2.9"},
|
|
|
|
{"<2.0.0", "1.9999.9999"},
|
|
|
|
{"<2.0.0", "0.2.9"},
|
2019-10-29 00:46:04 +00:00
|
|
|
{"<1.0", "1.0.0-pre"},
|
|
|
|
{"<1", "1.0.0-pre"},
|
2016-08-19 17:57:21 +00:00
|
|
|
{">= 1.0.0", "1.0.0"},
|
|
|
|
{">= 1.0.0", "1.0.1"},
|
|
|
|
{">= 1.0.0", "1.1.0"},
|
|
|
|
{"> 1.0.0", "1.0.1"},
|
|
|
|
{"> 1.0.0", "1.1.0"},
|
|
|
|
{"<= 2.0.0", "2.0.0"},
|
|
|
|
{"<= 2.0.0", "1.9999.9999"},
|
|
|
|
{"<= 2.0.0", "0.2.9"},
|
|
|
|
{"< 2.0.0", "1.9999.9999"},
|
|
|
|
{"<\t2.0.0", "0.2.9"},
|
|
|
|
{">=0.1.97", "0.1.97"},
|
|
|
|
{"0.1.20 || 1.2.4", "1.2.4"},
|
|
|
|
{">=0.2.3 || <0.0.1", "0.0.0"},
|
|
|
|
{">=0.2.3 || <0.0.1", "0.2.3"},
|
|
|
|
{">=0.2.3 || <0.0.1", "0.2.4"},
|
|
|
|
{"\"2.x.x\"", "2.1.3"},
|
|
|
|
{"1.2.x", "1.2.3"},
|
|
|
|
{"\"1.2.x\" || \"2.x\"", "2.1.3"},
|
|
|
|
{"\"1.2.x\" || \"2.x\"", "1.2.3"},
|
|
|
|
{"x", "1.2.3"},
|
|
|
|
{"2.*.*", "2.1.3"},
|
|
|
|
{"1.2.*", "1.2.3"},
|
|
|
|
{"1.2.* || 2.*", "2.1.3"},
|
|
|
|
{"1.2.* || 2.*", "1.2.3"},
|
|
|
|
{"*", "1.2.3"},
|
|
|
|
{"2", "2.1.2"},
|
|
|
|
{"2.3", "2.3.1"},
|
|
|
|
{"~2.4", "2.4.0"}, // >=2.4.0 <2.5.0
|
|
|
|
{"~2.4", "2.4.5"},
|
|
|
|
{"~1", "1.2.3"}, // >=1.0.0 <2.0.0
|
|
|
|
{"~1.0", "1.0.2"}, // >=1.0.0 <1.1.0,
|
|
|
|
{"~ 1.0", "1.0.2"},
|
|
|
|
{"~ 1.0.3", "1.0.12"},
|
|
|
|
{">=1", "1.0.0"},
|
|
|
|
{">= 1", "1.0.0"},
|
|
|
|
{"<1.2", "1.1.1"},
|
|
|
|
{"< 1.2", "1.1.1"},
|
|
|
|
{"=0.7.x", "0.7.2"},
|
|
|
|
{"<=0.7.x", "0.7.2"},
|
|
|
|
{">=0.7.x", "0.7.2"},
|
|
|
|
{"<=0.7.x", "0.6.2"},
|
|
|
|
{"~1.2.1 >=1.2.3", "1.2.3"},
|
|
|
|
{"~1.2.1 =1.2.3", "1.2.3"},
|
|
|
|
{"~1.2.1 1.2.3", "1.2.3"},
|
|
|
|
{"~1.2.1 >=1.2.3 1.2.3", "1.2.3"},
|
|
|
|
{"~1.2.1 1.2.3 >=1.2.3", "1.2.3"},
|
|
|
|
{">=\"1.2.1\" 1.2.3", "1.2.3"},
|
|
|
|
{"1.2.3 >=1.2.1", "1.2.3"},
|
|
|
|
{">=1.2.3 >=1.2.1", "1.2.3"},
|
|
|
|
{">=1.2.1 >=1.2.3", "1.2.3"},
|
|
|
|
{">=1.2", "1.2.8"},
|
|
|
|
{"^1.2.3", "1.8.1"},
|
|
|
|
{"^0.1.2", "0.1.2"},
|
|
|
|
{"^0.1", "0.1.2"},
|
|
|
|
{"^1.2", "1.4.2"},
|
2019-10-29 00:46:04 +00:00
|
|
|
{"^1.2", "1.2.0"},
|
|
|
|
{"^1", "1.2.0"},
|
2016-08-19 17:57:21 +00:00
|
|
|
{"<=1.2.3", "1.2.3-beta"},
|
|
|
|
{">1.2", "1.3.0-beta"},
|
|
|
|
{"<1.2.3", "1.2.3-beta"},
|
2019-12-09 10:58:27 +00:00
|
|
|
{"^1.2 ^1", "1.4.2"},
|
|
|
|
{"^0", "0.5.1"},
|
|
|
|
{"^0", "0.1.1"},
|
2016-08-19 17:57:21 +00:00
|
|
|
};
|
|
|
|
for (auto const& t: tests)
|
|
|
|
{
|
|
|
|
SemVerVersion version(t.second);
|
2022-03-15 18:53:29 +00:00
|
|
|
SemVerMatchExpression matchExpression = parseExpression(t.first);
|
2016-08-19 17:57:21 +00:00
|
|
|
BOOST_CHECK_MESSAGE(
|
2022-03-15 18:53:29 +00:00
|
|
|
matchExpression.matches(version),
|
2016-08-19 17:57:21 +00:00
|
|
|
"Version \"" + t.second + "\" did not satisfy expression \"" + t.first + "\""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(negative_range)
|
|
|
|
{
|
2019-12-09 10:58:27 +00:00
|
|
|
// Negative range tests
|
2023-08-30 00:08:06 +00:00
|
|
|
std::vector<std::pair<std::string, std::string>> tests = {
|
2021-10-12 13:13:35 +00:00
|
|
|
{"^0^1", "0.0.0"},
|
|
|
|
{"^0^1", "1.0.0"},
|
2016-08-19 17:57:21 +00:00
|
|
|
{"1.0.0 - 2.0.0", "2.2.3"},
|
2019-10-29 00:46:04 +00:00
|
|
|
{"1.0", "1.0.0-pre"},
|
|
|
|
{"1", "1.0.0-pre"},
|
2016-08-19 17:57:21 +00:00
|
|
|
{"^1.2.3", "1.2.3-pre"},
|
|
|
|
{"^1.2", "1.2.0-pre"},
|
2019-12-09 10:58:27 +00:00
|
|
|
{"^1.2", "1.2.1-pre"},
|
2016-08-19 17:57:21 +00:00
|
|
|
{"^1.2.3", "1.2.3-beta"},
|
|
|
|
{"=0.7.x", "0.7.0-asdf"},
|
|
|
|
{">=0.7.x", "0.7.0-asdf"},
|
|
|
|
{"1.0.0", "1.0.1"},
|
|
|
|
{">=1.0.0", "0.0.0"},
|
|
|
|
{">=1.0.0", "0.0.1"},
|
|
|
|
{">=1.0.0", "0.1.0"},
|
|
|
|
{">1.0.0", "0.0.1"},
|
|
|
|
{">1.0.0", "0.1.0"},
|
|
|
|
{"<=2.0.0", "3.0.0"},
|
|
|
|
{"<=2.0.0", "2.9999.9999"},
|
|
|
|
{"<=2.0.0", "2.2.9"},
|
|
|
|
{"<2.0.0", "2.9999.9999"},
|
|
|
|
{"<2.0.0", "2.2.9"},
|
|
|
|
{">=0.1.97", "0.1.93"},
|
|
|
|
{"0.1.20 || 1.2.4", "1.2.3"},
|
|
|
|
{">=0.2.3 || <0.0.1", "0.0.3"},
|
|
|
|
{">=0.2.3 || <0.0.1", "0.2.2"},
|
|
|
|
{"\"2.x.x\"", "1.1.3"},
|
|
|
|
{"\"2.x.x\"", "3.1.3"},
|
|
|
|
{"1.2.x", "1.3.3"},
|
|
|
|
{"\"1.2.x\" || \"2.x\"", "3.1.3"},
|
|
|
|
{"\"1.2.x\" || \"2.x\"", "1.1.3"},
|
|
|
|
{"2.*.*", "1.1.3"},
|
|
|
|
{"2.*.*", "3.1.3"},
|
|
|
|
{"1.2.*", "1.3.3"},
|
|
|
|
{"1.2.* || 2.*", "3.1.3"},
|
|
|
|
{"1.2.* || 2.*", "1.1.3"},
|
|
|
|
{"2", "1.1.2"},
|
|
|
|
{"2.3", "2.4.1"},
|
|
|
|
{"~2.4", "2.5.0"}, // >=2.4.0 <2.5.0
|
|
|
|
{"~2.4", "2.3.9"},
|
|
|
|
{"~1", "0.2.3"}, // >=1.0.0 <2.0.0
|
|
|
|
{"~1.0", "1.1.0"}, // >=1.0.0 <1.1.0
|
|
|
|
{"<1", "1.0.0"},
|
|
|
|
{">=1.2", "1.1.1"},
|
|
|
|
{"=0.7.x", "0.8.2"},
|
|
|
|
{">=0.7.x", "0.6.2"},
|
|
|
|
{"<0.7.x", "0.7.2"},
|
|
|
|
{"=1.2.3", "1.2.3-beta"},
|
|
|
|
{">1.2", "1.2.8"},
|
|
|
|
{"^1.2.3", "2.0.0-alpha"},
|
2019-12-09 10:58:27 +00:00
|
|
|
{"^0.6", "0.6.2-alpha"},
|
|
|
|
{"^0.6", "0.6.0-alpha"},
|
|
|
|
{"^1.2", "1.2.1-pre"},
|
2016-08-19 17:57:21 +00:00
|
|
|
{"^1.2.3", "1.2.2"},
|
2019-12-09 10:58:27 +00:00
|
|
|
{"^1", "1.2.0-pre"},
|
|
|
|
{"^1", "1.2.0-pre"},
|
|
|
|
{"^1.2", "1.1.9"},
|
|
|
|
{"^0", "0.5.1-pre"},
|
|
|
|
{"^0", "0.0.0-pre"},
|
|
|
|
{"^0", "1.0.0"},
|
2016-08-19 17:57:21 +00:00
|
|
|
};
|
|
|
|
for (auto const& t: tests)
|
|
|
|
{
|
|
|
|
SemVerVersion version(t.second);
|
2022-03-15 18:53:29 +00:00
|
|
|
auto matchExpression = parseExpression(t.first);
|
2016-08-19 17:57:21 +00:00
|
|
|
BOOST_CHECK_MESSAGE(
|
2022-03-15 18:53:29 +00:00
|
|
|
!matchExpression.matches(version),
|
2016-08-19 17:57:21 +00:00
|
|
|
"Version \"" + t.second + "\" did satisfy expression \"" + t.first + "\" " +
|
|
|
|
"(although it should not)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
} // end namespaces
|