From 854ca2ed345f1af4fe8441900ac65babe2d2ec2f Mon Sep 17 00:00:00 2001 From: Chappidi Yoga Satwik Date: Wed, 17 Feb 2021 01:56:41 +0530 Subject: [PATCH] core (fixes #349): Warn about Config::SetDefault() of deprecated attribute --- src/core/model/config.cc | 2 ++ src/core/test/attribute-test-suite.cc | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/core/model/config.cc b/src/core/model/config.cc index fe46b8f29..50ed341fd 100644 --- a/src/core/model/config.cc +++ b/src/core/model/config.cc @@ -870,6 +870,8 @@ bool SetDefaultFailSafe (std::string fullName, const AttributeValue &value) { return false; } + struct TypeId::AttributeInformation info; + tid.LookupAttributeByName(paramName, &info); for (uint32_t j = 0; j < tid.GetAttributeN (); j++) { struct TypeId::AttributeInformation tmp = tid.GetAttribute (j); diff --git a/src/core/test/attribute-test-suite.cc b/src/core/test/attribute-test-suite.cc index 50e1441cb..b97373459 100644 --- a/src/core/test/attribute-test-suite.cc +++ b/src/core/test/attribute-test-suite.cc @@ -228,6 +228,12 @@ public: TimeValue (Seconds (-2)), MakeTimeAccessor (&AttributeObjectTest::m_timeWithBounds), MakeTimeChecker (Seconds (-5), Seconds (10))) + .AddAttribute ("TestDeprecated", "help text", + BooleanValue (false), + MakeBooleanAccessor (&AttributeObjectTest::m_boolTestDeprecated), + MakeBooleanChecker (), + TypeId::DEPRECATED, + "DEPRECATED test working.") ; return tid; @@ -320,6 +326,7 @@ private: bool m_boolTestA; bool m_boolTest; + bool m_boolTestDeprecated; int16_t m_int16; int16_t m_int16WithBounds; int16_t m_int16SetGet; @@ -424,6 +431,20 @@ AttributeTestCase::DoRun (void) ok = CheckGetCodePaths (p, "TestBoolName", "true", BooleanValue (true)); NS_TEST_ASSERT_MSG_EQ (ok, true, "Attribute not set properly by default value"); + std::string expected ("Attribute 'TestDeprecated' is deprecated: DEPRECATED test working.\n"); + // Temporarily redirect std::cerr to a stringstream + std::stringstream buffer; + std::streambuf *oldBuffer = std::cerr.rdbuf (buffer.rdbuf()); + // Cause the deprecation warning to be sent to the stringstream + Config::SetDefault ("ns3::AttributeObjectTest::TestDeprecated", BooleanValue (true)); + + // Compare the obtained actual string with the expected string. + NS_TEST_ASSERT_MSG_EQ (buffer.str (), expected, "Deprecated attribute not working"); + // Restore cerr to its original stream buffer + std::cerr.rdbuf (oldBuffer); + + + // // Set the default value of the BooleanValue the other way and create an object. // The new default value should stick.