From 05139500fb44c1ff574756b3a5d10d46674c236d Mon Sep 17 00:00:00 2001 From: Federico Bond Date: Thu, 1 Dec 2016 10:55:02 -0300 Subject: Warn about using msg.value in non-payable function --- test/libsolidity/SolidityNameAndTypeResolution.cpp | 90 +++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 11c38114..a4e601f7 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -89,8 +90,12 @@ parseAnalyseAndReturnError(string const& _source, bool _reportWarnings = false, TypeChecker typeChecker(errors); bool success = typeChecker.checkTypeRequirements(*contract); BOOST_CHECK(success || !errors.empty()); - } + if (success) + { + StaticAnalyzer staticAnalyzer(errors); + staticAnalyzer.analyze(*sourceUnit); + } if (errors.size() > 1 && !_allowMultipleErrors) BOOST_FAIL("Multiple errors found"); for (auto const& currentError: errors) @@ -189,6 +194,14 @@ CHECK_ERROR_OR_WARNING(text, Warning, substring, true, false) // [checkSuccess(text)] asserts that the compilation down to typechecking succeeds. #define CHECK_SUCCESS(text) do { BOOST_CHECK(success((text))); } while(0) +#define CHECK_SUCCESS_NO_WARNINGS(text) \ +do \ +{ \ + auto sourceAndError = parseAnalyseAndReturnError((text), true); \ + BOOST_CHECK(sourceAndError.second == nullptr); \ +} \ +while(0) + BOOST_AUTO_TEST_SUITE(SolidityNameAndTypeResolution) @@ -4777,6 +4790,81 @@ BOOST_AUTO_TEST_CASE(invalid_mobile_type) CHECK_ERROR(text, TypeError, ""); } +BOOST_AUTO_TEST_CASE(warns_msg_value_in_non_payable_public_function) +{ + char const* text = R"( + contract C { + function f() { + msg.value; + } + } + )"; + CHECK_WARNING(text, "\"msg.value\" used in non-payable function. Do you want to add the \"payable\" modifier to this function?"); +} + +BOOST_AUTO_TEST_CASE(does_not_warn_msg_value_in_payable_function) +{ + char const* text = R"( + contract C { + function f() payable { + msg.value; + } + } + )"; + CHECK_SUCCESS_NO_WARNINGS(text); +} + +BOOST_AUTO_TEST_CASE(does_not_warn_msg_value_in_internal_function) +{ + char const* text = R"( + contract C { + function f() internal { + msg.value; + } + } + )"; + CHECK_SUCCESS_NO_WARNINGS(text); +} + +BOOST_AUTO_TEST_CASE(does_not_warn_msg_value_in_library) +{ + char const* text = R"( + library C { + function f() { + msg.value; + } + } + )"; + CHECK_SUCCESS_NO_WARNINGS(text); +} + +BOOST_AUTO_TEST_CASE(does_not_warn_non_magic_msg_value) +{ + char const* text = R"( + contract C { + struct msg { + uint256 value; + } + + function f() { + msg.value; + } + } + )"; + CHECK_SUCCESS_NO_WARNINGS(text); +} + +BOOST_AUTO_TEST_CASE(does_not_warn_msg_value_in_modifier_following_non_payable_public_function) +{ + char const* text = R"( + contract c { + function f() { } + modifier m() { msg.value; _; } + } + )"; + CHECK_SUCCESS_NO_WARNINGS(text); +} + BOOST_AUTO_TEST_SUITE_END() } -- cgit