Skip to content

Commit 147004c

Browse files
Unit tests for NetworkCommissioning cluster using Ethernet and bugfixes. (project-chip#42189)
* Add unit tests for `NetworkCommisioning` cluster for ethernet * Bug fixes `Init` was called on the cluster then instead `Deinit` was called `Shutdown` Write attribute on InterfaceEnabled with value of false when not supported didn't return INVALID_ACTION status code * Restyled by whitespace * Restyled by clang-format * Restyled by gn * Resolving reviews and suggestions --------- Co-authored-by: Restyled.io <[email protected]>
1 parent fe7d07a commit 147004c

File tree

7 files changed

+358
-11
lines changed

7 files changed

+358
-11
lines changed

src/app/clusters/network-commissioning/CodegenInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ CHIP_ERROR Instance::Init()
4646
void Instance::Shutdown()
4747
{
4848
TEMPORARY_RETURN_IGNORED CodegenDataModelProvider::Instance().Registry().Unregister(&mCluster.Cluster());
49-
mCluster.Cluster().Shutdown();
49+
mCluster.Cluster().Deinit();
5050
}
5151

5252
} // namespace NetworkCommissioning

src/app/clusters/network-commissioning/NetworkCommissioningCluster.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ void NetworkCommissioningCluster::SetLastConnectErrorValue(Attributes::LastConne
261261
{
262262
if (mLastConnectErrorValue.Update(connectErrorValue))
263263
{
264-
NotifyAttributeChanged(Attributes::LastConnectErrorValue::TypeInfo::GetAttributeId());
264+
NotifyAttributeChanged(Attributes::LastConnectErrorValue::Id);
265265
}
266266
}
267267

@@ -273,12 +273,12 @@ void NetworkCommissioningCluster::SetLastNetworkId(ByteSpan lastNetworkId)
273273

274274
memcpy(mLastNetworkID, lastNetworkId.data(), lastNetworkId.size());
275275
mLastNetworkIDLen = static_cast<uint8_t>(lastNetworkId.size());
276-
NotifyAttributeChanged(Attributes::LastNetworkID::TypeInfo::GetAttributeId());
276+
NotifyAttributeChanged(Attributes::LastNetworkID::Id);
277277
}
278278

279279
void NetworkCommissioningCluster::ReportNetworksListChanged()
280280
{
281-
NotifyAttributeChanged(Attributes::Networks::TypeInfo::GetAttributeId());
281+
NotifyAttributeChanged(Attributes::Networks::Id);
282282
}
283283

284284
void NetworkCommissioningCluster::OnNetworkingStatusChange(Status aCommissioningError, Optional<ByteSpan> aNetworkId,
@@ -1081,10 +1081,18 @@ DataModel::ActionReturnStatus NetworkCommissioningCluster::WriteAttribute(const
10811081
{
10821082
bool value;
10831083
ReturnErrorOnFailure(decoder.Decode(value));
1084-
return NotifyAttributeChangedIfSuccess(request.path.mAttributeId, SetInterfaceEnabled(value));
1084+
CHIP_ERROR err = SetInterfaceEnabled(value);
1085+
1086+
// Spec. 11.9.6.5 -- "If not supported, a write to this attribute with a value of false SHALL fail with a status of
1087+
// INVALID_ACTION."
1088+
if (err == CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE)
1089+
{
1090+
return Protocols::InteractionModel::Status::InvalidAction;
1091+
}
1092+
return NotifyAttributeChangedIfSuccess(request.path.mAttributeId, err);
10851093
}
10861094

1087-
return Protocols::InteractionModel::Status::InvalidAction;
1095+
return Protocols::InteractionModel::Status::UnsupportedWrite;
10881096
}
10891097

10901098
std::optional<DataModel::ActionReturnStatus> NetworkCommissioningCluster::InvokeCommand(const DataModel::InvokeRequest & request,

src/app/clusters/network-commissioning/tests/BUILD.gn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ chip_test_suite("tests") {
2323

2424
test_sources = [
2525
"TestNetworkCommissioningCluster.cpp",
26+
"TestNetworkCommissioningClusterEthernet.cpp",
2627
"TestThreadResponse.cpp",
2728
"TestWifiResponse.cpp",
2829
]
2930

30-
sources = [ "FakeWifiDriver.h" ]
31+
sources = [ "FakeDrivers.h" ]
3132

3233
cflags = [ "-Wconversion" ]
3334

src/app/clusters/network-commissioning/tests/FakeWifiDriver.h renamed to src/app/clusters/network-commissioning/tests/FakeDrivers.h

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,109 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
#include "clusters/NetworkCommissioning/Enums.h"
17-
#include "lib/core/CHIPError.h"
16+
#include <clusters/NetworkCommissioning/Attributes.h>
17+
#include <clusters/NetworkCommissioning/Enums.h>
18+
#include <lib/core/CHIPError.h>
19+
#include <lib/support/Span.h>
1820
#include <platform/NetworkCommissioning.h>
1921
#include <protocols/interaction_model/StatusCode.h>
2022

2123
namespace chip {
2224
namespace Testing {
2325

26+
class FakeEthernetDriver : public DeviceLayer::NetworkCommissioning::EthernetDriver
27+
{
28+
public:
29+
class FakeNetworkIterator : public DeviceLayer::NetworkCommissioning::NetworkIterator
30+
{
31+
public:
32+
void Set(Span<DeviceLayer::NetworkCommissioning::Network> networks)
33+
{
34+
mNetworks = networks;
35+
currentindex = 0;
36+
}
37+
bool Next(DeviceLayer::NetworkCommissioning::Network & item) override
38+
{
39+
VerifyOrReturnValue(currentindex < mNetworks.size(), false);
40+
item = mNetworks[currentindex++];
41+
return true;
42+
}
43+
size_t Count() override { return mNetworks.size(); }
44+
void Release() override {}
45+
46+
private:
47+
Span<DeviceLayer::NetworkCommissioning::Network> mNetworks;
48+
size_t currentindex = 0;
49+
};
50+
51+
CHIP_ERROR Init(NetworkStatusChangeCallback * networkStatusChangeCallback) override
52+
{
53+
mNetworkStatusChangeCallback = networkStatusChangeCallback;
54+
return CHIP_NO_ERROR;
55+
}
56+
57+
uint8_t GetMaxNetworks() override { return 1; };
58+
59+
void SetNetwork(ByteSpan interfaceName)
60+
{
61+
auto span = MutableByteSpan{ mNetwork.networkID, sizeof(mNetwork.networkID) };
62+
SuccessOrDie(CopySpanToMutableSpan(interfaceName, span));
63+
mNetwork.networkIDLen = static_cast<uint8_t>(interfaceName.size());
64+
mNetwork.connected = true;
65+
mNetworkCount = 1;
66+
67+
mNetworkStatusChangeCallback->OnNetworkingStatusChange(
68+
app::Clusters::NetworkCommissioning::NetworkCommissioningStatusEnum::kSuccess, Optional{ interfaceName },
69+
testErrorValue);
70+
}
71+
72+
void SetNetworkConnected(bool connected) { mNetwork.connected = connected; }
73+
74+
void SetNoNetwork() { mNetworkCount = 0; }
75+
76+
DeviceLayer::NetworkCommissioning::NetworkIterator * GetNetworks() override
77+
{
78+
if (mNetworkCount == 0)
79+
{
80+
mNetworkIterator.Set({});
81+
}
82+
else
83+
{
84+
mNetworkIterator.Set({ &mNetwork, 1 });
85+
}
86+
87+
return &mNetworkIterator;
88+
};
89+
90+
void EnableDisabling(bool enabledAllowed) { mSetEnabledAllowed = enabledAllowed; }
91+
92+
CHIP_ERROR SetEnabled(bool enabled) override
93+
{
94+
if (GetEnabled() == enabled)
95+
{
96+
return CHIP_NO_ERROR;
97+
}
98+
if (mSetEnabledAllowed)
99+
{
100+
mEnabled = enabled;
101+
return CHIP_NO_ERROR;
102+
}
103+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
104+
}
105+
106+
bool GetEnabled() override { return mEnabled; };
107+
108+
Optional<int32_t> testErrorValue{ 123 };
109+
110+
private:
111+
NetworkStatusChangeCallback * mNetworkStatusChangeCallback;
112+
FakeNetworkIterator mNetworkIterator;
113+
DeviceLayer::NetworkCommissioning::Network mNetwork;
114+
size_t mNetworkCount = 0;
115+
bool mSetEnabledAllowed = false;
116+
bool mEnabled = true;
117+
};
118+
24119
class FakeWiFiDriver : public DeviceLayer::NetworkCommissioning::WiFiDriver
25120
{
26121
public:

src/app/clusters/network-commissioning/tests/TestNetworkCommissioningCluster.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include <lib/support/ReadOnlyBuffer.h>
3737
#include <platform/NetworkCommissioning.h>
3838

39-
#include "FakeWifiDriver.h"
39+
#include "FakeDrivers.h"
4040

4141
namespace {
4242

@@ -114,7 +114,7 @@ TEST_F(TestNetworkCommissioningCluster, TestNotifyOnEnableInterface)
114114
{
115115
WriteOperation writeOp(kRootEndpointId, NetworkCommissioning::Id, InterfaceEnabled::Id);
116116
writeOp.SetSubjectDescriptor(kAdminSubjectDescriptor);
117-
AttributeValueDecoder decoder = writeOp.DecoderFor(true);
117+
AttributeValueDecoder decoder = writeOp.DecoderFor(false);
118118

119119
// no notification if enable fails
120120
context.ChangeListener().DirtyList().clear();

0 commit comments

Comments
 (0)