Skip to content

Commit d85ec89

Browse files
authored
[test] avoid FRIEND_TEST which is discouraged (#346)
This commit removes usage of the discouraged FRIEND_TEST and use a test peer instead to access private methods and variables.
1 parent 299e537 commit d85ec89

4 files changed

Lines changed: 75 additions & 10 deletions

File tree

src/library/commissioner_impl.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <chrono>
3333
#include <cstdint>
3434
#include <functional>
35-
#include <gtest/gtest_prod.h>
3635
#include <map>
3736
#include <string>
3837
#include <vector>
@@ -57,6 +56,8 @@ namespace ot {
5756

5857
namespace commissioner {
5958

59+
class CommissionerTestPeer;
60+
6061
// This is the implementation of the Commissioner interface with
6162
// event-driven model. The commissioner is not running unless the
6263
// the event loop has been started. Starting the event loop will
@@ -75,6 +76,7 @@ namespace commissioner {
7576
class CommissionerImpl : public Commissioner
7677
{
7778
friend class JoinerSession;
79+
friend class CommissionerTestPeer;
7880

7981
public:
8082
explicit CommissionerImpl(CommissionerHandler &aHandler, struct event_base *aEventBase);
@@ -299,8 +301,6 @@ class CommissionerImpl : public Commissioner
299301

300302
coap::Resource mResourceDiagAns;
301303
NetDiagData mDiagAnsTlvs;
302-
303-
FRIEND_TEST(CommissionerSafeTestProxyMode, ShouldBeAbleToSendToJoinerIfJoinerSessionExists);
304304
};
305305

306306
/*

src/library/commissioner_safe.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
#include <event2/event.h>
3838
#include <event2/util.h>
39-
#include <gtest/gtest_prod.h>
4039
#include <stdint.h>
4140

4241
#include <functional>
@@ -58,6 +57,8 @@ namespace ot {
5857

5958
namespace commissioner {
6059

60+
class CommissionerTestPeer;
61+
6162
/**
6263
* This class implements the Commissioner interface.
6364
*
@@ -71,6 +72,8 @@ namespace commissioner {
7172
*/
7273
class CommissionerSafe : public Commissioner
7374
{
75+
friend class CommissionerTestPeer;
76+
7477
public:
7578
explicit CommissionerSafe(CommissionerHandler &aHandler)
7679
: mHandler(aHandler)
@@ -241,9 +244,6 @@ class CommissionerSafe : public Commissioner
241244

242245
// The even loop thread running in background.
243246
std::thread mEventThread;
244-
245-
FRIEND_TEST(CommissionerSafeTestProxyMode, ShouldBeAbleToReceiveJoinerMessage);
246-
FRIEND_TEST(CommissionerSafeTestProxyMode, ShouldBeAbleToSendToJoinerIfJoinerSessionExists);
247247
};
248248

249249
} // namespace commissioner

src/library/commissioner_safe_test.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "commissioner/error.hpp"
4848
#include "common/address.hpp"
4949
#include "library/commissioner_safe.hpp"
50+
#include "library/commissioner_test_peer.hpp"
5051
#include "library/joiner_session.hpp"
5152

5253
using testing::ContainerEq;
@@ -99,7 +100,8 @@ TEST(CommissionerSafeTestProxyMode, ShouldBeAbleToReceiveJoinerMessage)
99100

100101
EXPECT_CALL(mockHandler, OnJoinerMessage(ContainerEq(joinerId), 5400, ContainerEq(payload))).Times(1);
101102

102-
JoinerSession session(*static_cast<CommissionerSafe &>(*commissioner).mImpl, joinerId, std::string(), 12345, 0x1200,
103+
auto &commissionerSafe = static_cast<CommissionerSafe &>(*commissioner);
104+
JoinerSession session(*CommissionerTestPeer::GetImpl(commissionerSafe), joinerId, std::string(), 12345, 0x1200,
103105
Address(), 12344, Address(), 12343);
104106
session.RecvJoinerDtlsRecords(payload, 5400);
105107
sleep(1);
@@ -124,8 +126,9 @@ TEST(CommissionerSafeTestProxyMode, ShouldBeAbleToSendToJoinerIfJoinerSessionExi
124126
ByteArray joinerId{1, 2, 3, 4, 5, 6, 7, 8};
125127
ByteArray payload{1, 2, 3};
126128

127-
auto impl = static_cast<CommissionerSafe &>(*commissioner).mImpl;
128-
impl->mJoinerSessions.emplace(
129+
auto &commissionerSafe = static_cast<CommissionerSafe &>(*commissioner);
130+
auto impl = CommissionerTestPeer::GetImpl(commissionerSafe);
131+
CommissionerTestPeer::GetJoinerSessions(*impl).emplace(
129132
std::piecewise_construct, std::forward_as_tuple(joinerId),
130133
std::forward_as_tuple(*impl, joinerId, std::string(), 12345, 0x1200, Address(), 12344, Address(), 12343));
131134

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2026, The OpenThread Commissioner Authors.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* 3. Neither the name of the copyright holder nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
/**
30+
* @file
31+
* This file defines a test peer for Commissioner classes to access private members in tests.
32+
*/
33+
34+
#ifndef OT_COMM_LIBRARY_COMMISSIONER_TEST_PEER_HPP_
35+
#define OT_COMM_LIBRARY_COMMISSIONER_TEST_PEER_HPP_
36+
37+
#include <map>
38+
#include <memory>
39+
40+
#include "library/commissioner_impl.hpp"
41+
#include "library/commissioner_safe.hpp"
42+
43+
namespace ot {
44+
45+
namespace commissioner {
46+
47+
class CommissionerTestPeer
48+
{
49+
public:
50+
static std::shared_ptr<CommissionerImpl> &GetImpl(CommissionerSafe &aSafe) { return aSafe.mImpl; }
51+
52+
static std::map<ByteArray, JoinerSession> &GetJoinerSessions(CommissionerImpl &aImpl)
53+
{
54+
return aImpl.mJoinerSessions;
55+
}
56+
};
57+
58+
} // namespace commissioner
59+
60+
} // namespace ot
61+
62+
#endif // OT_COMM_LIBRARY_COMMISSIONER_TEST_PEER_HPP_

0 commit comments

Comments
 (0)