1 /*
2 * Copyright (c) 2024, The OpenThread 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 #include <gmock/gmock.h>
30 #include <gtest/gtest.h>
31
32 #include <openthread/border_agent.h>
33 #include <openthread/dataset.h>
34 #include <openthread/dataset_ftd.h>
35 #include <openthread/instance.h>
36 #include <openthread/ip6.h>
37 #include <openthread/thread.h>
38 #include <openthread/platform/time.h>
39 #include "gmock/gmock.h"
40
41 #include "fake_platform.hpp"
42
43 using namespace ot;
44 using ::testing::AnyNumber;
45 using ::testing::AtLeast;
46 using ::testing::MockFunction;
47 using ::testing::Truly;
48
49 template <typename R, typename... A> class MockCallback : public testing::MockFunction<R(A...)>
50 {
51 public:
CallWithContext(A...aArgs,void * aContext)52 static R CallWithContext(A... aArgs, void *aContext)
53 {
54 return static_cast<MockCallback *>(aContext)->Call(aArgs...);
55 };
56 };
57
TEST(otDatasetSetActiveTlvs,shouldTriggerStateCallbackOnSuccess)58 TEST(otDatasetSetActiveTlvs, shouldTriggerStateCallbackOnSuccess)
59 {
60 FakePlatform fakePlatform;
61
62 typedef MockCallback<void, otChangedFlags> MockStateCallback;
63
64 MockStateCallback mockStateCallback;
65
66 EXPECT_CALL(mockStateCallback, Call).Times(AnyNumber());
67
68 EXPECT_CALL(mockStateCallback, Call(Truly([](otChangedFlags aChangedFlags) -> bool {
69 return (aChangedFlags & OT_CHANGED_ACTIVE_DATASET);
70 })))
71 .Times(AtLeast(1));
72
73 otError error = otSetStateChangedCallback(FakePlatform::CurrentInstance(), MockStateCallback::CallWithContext,
74 &mockStateCallback);
75 assert(error == OT_ERROR_NONE);
76
77 otOperationalDataset dataset;
78 otOperationalDatasetTlvs datasetTlvs;
79
80 error = otDatasetCreateNewNetwork(FakePlatform::CurrentInstance(), &dataset);
81 assert(error == OT_ERROR_NONE);
82
83 otDatasetConvertToTlvs(&dataset, &datasetTlvs);
84 error = otDatasetSetActiveTlvs(FakePlatform::CurrentInstance(), &datasetTlvs);
85 assert(error == OT_ERROR_NONE);
86
87 fakePlatform.GoInMs(10000);
88 }
89