aboutsummaryrefslogtreecommitdiffstats
path: root/test/libsolidity
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2016-12-11 23:46:06 +0800
committerGitHub <noreply@github.com>2016-12-11 23:46:06 +0800
commitd2b8bdd0163ba097e86bbce8d9770256bcda7a3b (patch)
tree823b740b0917f32640d5c3a79a1e75c631089464 /test/libsolidity
parent4508f713247359b8f9cba4867c7c77c64a001807 (diff)
parent05139500fb44c1ff574756b3a5d10d46674c236d (diff)
downloaddexon-solidity-d2b8bdd0163ba097e86bbce8d9770256bcda7a3b.tar.gz
dexon-solidity-d2b8bdd0163ba097e86bbce8d9770256bcda7a3b.tar.zst
dexon-solidity-d2b8bdd0163ba097e86bbce8d9770256bcda7a3b.zip
Merge pull request #1464 from federicobond/warn-msgvalue
Warn about using msg.value in non-payable function
Diffstat (limited to 'test/libsolidity')
-rw-r--r--test/libsolidity/SolidityNameAndTypeResolution.cpp90
1 files changed, 89 insertions, 1 deletions
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 <libsolidity/parsing/Scanner.h>
#include <libsolidity/parsing/Parser.h>
#include <libsolidity/analysis/NameAndTypeResolver.h>
+#include <libsolidity/analysis/StaticAnalyzer.h>
#include <libsolidity/analysis/SyntaxChecker.h>
#include <libsolidity/interface/Exceptions.h>
#include <libsolidity/analysis/GlobalContext.h>
@@ -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()
}