A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-link-change-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Gabriel Ferreira <gabrielcarvfer@gmail.com>
7 */
8
9#include "ns3/callback.h"
10#include "ns3/csma-channel.h"
11#include "ns3/csma-net-device.h"
12#include "ns3/mac48-address.h"
13#include "ns3/node.h"
14#include "ns3/simulator.h"
15#include "ns3/test.h"
16
17using namespace ns3;
18
19/**
20 * @ingroup csma-test
21 * @ingroup tests
22 *
23 * @brief Verify that detaching a CsmaNetDevice from its channel fires the
24 * link-change callback and marks the link down (@issueid{234}).
25 */
27{
28 public:
30
31 private:
32 void DoRun() override;
33 /// Link-change callback; counts invocations.
34 void LinkChange();
35 uint32_t m_linkChangeCount{0}; //!< number of link-change notifications
36};
37
39 : TestCase("CsmaChannel::Detach fires the link-change callback (issue #234)")
40{
41}
42
43void
48
49void
51{
52 // --- Detach by device pointer ---
53 {
58 dev->SetAddress(Mac48Address::Allocate());
59 node->AddDevice(dev);
60
61 dev->Attach(channel);
62 NS_TEST_ASSERT_MSG_EQ(dev->IsLinkUp(), true, "link should be up after Attach");
63
64 dev->AddLinkChangeCallback(MakeCallback(&CsmaLinkChangeTestCase::LinkChange, this));
65
66 bool detached = channel->Detach(dev);
67 NS_TEST_ASSERT_MSG_EQ(detached, true, "Detach(device) should succeed");
69 1,
70 "link-change callback should fire on Detach(device)");
71 NS_TEST_ASSERT_MSG_EQ(dev->IsLinkUp(), false, "link should be down after Detach(device)");
72 }
73
74 // --- Detach by device id ---
75 {
80 dev->SetAddress(Mac48Address::Allocate());
81 node->AddDevice(dev);
82
83 dev->Attach(channel);
84 dev->AddLinkChangeCallback(MakeCallback(&CsmaLinkChangeTestCase::LinkChange, this));
85
86 // The first (and only) attached device has channel device id 0.
87 bool detached = channel->Detach(uint32_t(0));
88 NS_TEST_ASSERT_MSG_EQ(detached, true, "Detach(deviceId) should succeed");
90 1,
91 "link-change callback should fire on Detach(deviceId)");
92 NS_TEST_ASSERT_MSG_EQ(dev->IsLinkUp(), false, "link should be down after Detach(deviceId)");
93 }
94
95 // The nodes register themselves with the NodeList on construction; this
96 // clears it and disposes them along with their devices and channels.
98}
99
100/**
101 * @ingroup csma-test
102 * @ingroup tests
103 *
104 * @brief CSMA link-change test suite.
105 */
107{
108 public:
114};
115
116static CsmaLinkChangeTestSuite g_csmaLinkChangeTestSuite; //!< Static variable for test init
static Mac48Address Allocate()
Allocate a new Mac48Address.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
@ UNIT
This test suite implements a Unit Test.
Definition test.h:1273
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:690
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:133
Every class exported by the ns3 library is enclosed in the ns3 namespace.