|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2024 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <gtest/gtest.h> |
| 20 | + |
| 21 | +#include <controller/CommissioningWindowOpener.h> |
| 22 | +#include <lib/core/CHIPError.h> |
| 23 | +#include <lib/core/ErrorStr.h> |
| 24 | +#include <lib/support/logging/CHIPLogging.h> |
| 25 | + |
| 26 | +using namespace chip; |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +class MockDeviceController : public Controller::DeviceController |
| 31 | +{ |
| 32 | +public: |
| 33 | + CHIP_ERROR |
| 34 | + GetConnectedDevice(NodeId peerNodeId, Callback::Callback<OnDeviceConnected> * onConnection, |
| 35 | + Callback::Callback<OnDeviceConnectionFailure> * onFailure, |
| 36 | + TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload) override |
| 37 | + { |
| 38 | + return CHIP_NO_ERROR; |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +// Valid crypto values from src/protocols/secure_channel/tests/TestPASESession.cpp |
| 43 | +constexpr uint32_t sTestSpake2p01_PinCode = 20202021; |
| 44 | +constexpr uint32_t sTestSpake2p01_IterationCount = 1000; |
| 45 | +constexpr uint8_t sTestSpake2p01_Salt[] = { 0x53, 0x50, 0x41, 0x4B, 0x45, 0x32, 0x50, 0x20, |
| 46 | + 0x4B, 0x65, 0x79, 0x20, 0x53, 0x61, 0x6C, 0x74 }; |
| 47 | +constexpr Crypto::Spake2pVerifierSerialized sTestSpake2p01_SerializedVerifier = { |
| 48 | + 0xB9, 0x61, 0x70, 0xAA, 0xE8, 0x03, 0x34, 0x68, 0x84, 0x72, 0x4F, 0xE9, 0xA3, 0xB2, 0x87, 0xC3, 0x03, 0x30, 0xC2, 0xA6, |
| 49 | + 0x60, 0x37, 0x5D, 0x17, 0xBB, 0x20, 0x5A, 0x8C, 0xF1, 0xAE, 0xCB, 0x35, 0x04, 0x57, 0xF8, 0xAB, 0x79, 0xEE, 0x25, 0x3A, |
| 50 | + 0xB6, 0xA8, 0xE4, 0x6B, 0xB0, 0x9E, 0x54, 0x3A, 0xE4, 0x22, 0x73, 0x6D, 0xE5, 0x01, 0xE3, 0xDB, 0x37, 0xD4, 0x41, 0xFE, |
| 51 | + 0x34, 0x49, 0x20, 0xD0, 0x95, 0x48, 0xE4, 0xC1, 0x82, 0x40, 0x63, 0x0C, 0x4F, 0xF4, 0x91, 0x3C, 0x53, 0x51, 0x38, 0x39, |
| 52 | + 0xB7, 0xC0, 0x7F, 0xCC, 0x06, 0x27, 0xA1, 0xB8, 0x57, 0x3A, 0x14, 0x9F, 0xCD, 0x1F, 0xA4, 0x66, 0xCF |
| 53 | +}; |
| 54 | + |
| 55 | +static void OCWPasscodeCallback(void * context, NodeId deviceId, CHIP_ERROR status, SetupPayload payload) {} |
| 56 | +static void OCWVerifierCallback(void * context, NodeId deviceId, CHIP_ERROR status) {} |
| 57 | + |
| 58 | +class TestCommissioningWindowOpener : public ::testing::Test |
| 59 | +{ |
| 60 | +public: |
| 61 | + static void SetUpTestSuite() { ASSERT_EQ(Platform::MemoryInit(), CHIP_NO_ERROR); } |
| 62 | + static void TearDownTestSuite() { Platform::MemoryShutdown(); } |
| 63 | + |
| 64 | +protected: |
| 65 | + // Initialize with a null pointer for now, replace with a valid controller pointer if available |
| 66 | + MockDeviceController mockController; |
| 67 | + Controller::CommissioningWindowOpener opener = Controller::CommissioningWindowOpener(&mockController); |
| 68 | +}; |
| 69 | + |
| 70 | +TEST_F(TestCommissioningWindowOpener, OpenCommissioningWindowVerifier_Success) |
| 71 | +{ |
| 72 | + Callback::Callback<Controller::OnOpenCommissioningWindowWithVerifier> callback(OCWVerifierCallback, this); |
| 73 | + |
| 74 | + CHIP_ERROR err = opener.OpenCommissioningWindow(Controller::CommissioningWindowVerifierParams() |
| 75 | + .SetNodeId(0x1234) |
| 76 | + .SetTimeout(300) |
| 77 | + .SetIteration(sTestSpake2p01_IterationCount) |
| 78 | + .SetDiscriminator(3840) |
| 79 | + .SetSalt(ByteSpan(sTestSpake2p01_Salt)) |
| 80 | + .SetVerifier(ByteSpan(sTestSpake2p01_SerializedVerifier)), |
| 81 | + &callback); |
| 82 | + EXPECT_EQ(err, CHIP_NO_ERROR); |
| 83 | +} |
| 84 | + |
| 85 | +TEST_F(TestCommissioningWindowOpener, OpenCommissioningWindowVerifier_Failure_NoSalt) |
| 86 | +{ |
| 87 | + Callback::Callback<Controller::OnOpenCommissioningWindowWithVerifier> callback(OCWVerifierCallback, this); |
| 88 | + |
| 89 | + CHIP_ERROR err = opener.OpenCommissioningWindow(Controller::CommissioningWindowVerifierParams() |
| 90 | + .SetNodeId(0x1234) |
| 91 | + .SetTimeout(300) |
| 92 | + .SetIteration(sTestSpake2p01_IterationCount) |
| 93 | + .SetDiscriminator(3840) |
| 94 | + .SetVerifier(ByteSpan(sTestSpake2p01_SerializedVerifier)), |
| 95 | + &callback); |
| 96 | + EXPECT_EQ(err, CHIP_ERROR_INVALID_ARGUMENT); |
| 97 | +} |
| 98 | + |
| 99 | +TEST_F(TestCommissioningWindowOpener, OpenCommissioningWindowVerifier_Failure_NoVerifier) |
| 100 | +{ |
| 101 | + Callback::Callback<Controller::OnOpenCommissioningWindowWithVerifier> callback(OCWVerifierCallback, this); |
| 102 | + |
| 103 | + CHIP_ERROR err = opener.OpenCommissioningWindow(Controller::CommissioningWindowVerifierParams() |
| 104 | + .SetNodeId(0x1234) |
| 105 | + .SetTimeout(300) |
| 106 | + .SetIteration(sTestSpake2p01_IterationCount) |
| 107 | + .SetDiscriminator(3840) |
| 108 | + .SetSalt(ByteSpan(sTestSpake2p01_Salt)), |
| 109 | + &callback); |
| 110 | + EXPECT_EQ(err, CHIP_ERROR_INVALID_ARGUMENT); |
| 111 | +} |
| 112 | + |
| 113 | +TEST_F(TestCommissioningWindowOpener, OpenCommissioningWindowVerifier_Failure_InvalidIteration) |
| 114 | +{ |
| 115 | + Callback::Callback<Controller::OnOpenCommissioningWindowWithVerifier> callback(OCWVerifierCallback, this); |
| 116 | + |
| 117 | + CHIP_ERROR err = opener.OpenCommissioningWindow(Controller::CommissioningWindowVerifierParams() |
| 118 | + .SetNodeId(0x1234) |
| 119 | + .SetTimeout(300) |
| 120 | + .SetIteration(0) |
| 121 | + .SetDiscriminator(3840) |
| 122 | + .SetSalt(ByteSpan(sTestSpake2p01_Salt)) |
| 123 | + .SetVerifier(ByteSpan(sTestSpake2p01_SerializedVerifier)), |
| 124 | + &callback); |
| 125 | + EXPECT_EQ(err, CHIP_ERROR_INVALID_ARGUMENT); |
| 126 | +} |
| 127 | + |
| 128 | +TEST_F(TestCommissioningWindowOpener, OpenCommissioningWindowPasscode_Success) |
| 129 | +{ |
| 130 | + SetupPayload ignored; |
| 131 | + Callback::Callback<Controller::OnOpenCommissioningWindow> callback(OCWPasscodeCallback, this); |
| 132 | + CHIP_ERROR err = opener.OpenCommissioningWindow(Controller::CommissioningWindowPasscodeParams() |
| 133 | + .SetNodeId(0x1234) |
| 134 | + .SetTimeout(300) |
| 135 | + .SetIteration(sTestSpake2p01_IterationCount) |
| 136 | + .SetDiscriminator(3840) |
| 137 | + .SetSetupPIN(sTestSpake2p01_PinCode) |
| 138 | + .SetReadVIDPIDAttributes(true) |
| 139 | + .SetSalt(ByteSpan(sTestSpake2p01_Salt)), |
| 140 | + &callback, ignored); |
| 141 | + EXPECT_EQ(err, CHIP_NO_ERROR); |
| 142 | +} |
| 143 | + |
| 144 | +TEST_F(TestCommissioningWindowOpener, OpenCommissioningWindowPasscode_Success_NoPin) |
| 145 | +{ |
| 146 | + SetupPayload ignored; |
| 147 | + Callback::Callback<Controller::OnOpenCommissioningWindow> callback(OCWPasscodeCallback, this); |
| 148 | + CHIP_ERROR err = opener.OpenCommissioningWindow(Controller::CommissioningWindowPasscodeParams() |
| 149 | + .SetNodeId(0x1234) |
| 150 | + .SetTimeout(300) |
| 151 | + .SetIteration(sTestSpake2p01_IterationCount) |
| 152 | + .SetDiscriminator(3840) |
| 153 | + .SetSalt(ByteSpan(sTestSpake2p01_Salt)), |
| 154 | + &callback, ignored); |
| 155 | + EXPECT_EQ(err, CHIP_NO_ERROR); |
| 156 | +} |
| 157 | + |
| 158 | +TEST_F(TestCommissioningWindowOpener, OpenCommissioningWindowPasscode_Success_NoSalt) |
| 159 | +{ |
| 160 | + SetupPayload ignored; |
| 161 | + Callback::Callback<Controller::OnOpenCommissioningWindow> callback(OCWPasscodeCallback, this); |
| 162 | + CHIP_ERROR err = opener.OpenCommissioningWindow(Controller::CommissioningWindowPasscodeParams() |
| 163 | + .SetNodeId(0x1234) |
| 164 | + .SetTimeout(300) |
| 165 | + .SetIteration(sTestSpake2p01_IterationCount) |
| 166 | + .SetDiscriminator(3840) |
| 167 | + .SetSetupPIN(sTestSpake2p01_PinCode), |
| 168 | + &callback, ignored); |
| 169 | + EXPECT_EQ(err, CHIP_NO_ERROR); |
| 170 | +} |
| 171 | + |
| 172 | +TEST_F(TestCommissioningWindowOpener, OpenCommissioningWindowPasscode_Failure_InvalidIteration) |
| 173 | +{ |
| 174 | + SetupPayload ignored; |
| 175 | + Callback::Callback<Controller::OnOpenCommissioningWindow> callback(OCWPasscodeCallback, this); |
| 176 | + CHIP_ERROR err = opener.OpenCommissioningWindow( |
| 177 | + Controller::CommissioningWindowPasscodeParams().SetNodeId(0x1234).SetTimeout(300).SetIteration(0).SetDiscriminator(3840), |
| 178 | + &callback, ignored); |
| 179 | + EXPECT_EQ(err, CHIP_ERROR_INVALID_ARGUMENT); |
| 180 | +} |
| 181 | + |
| 182 | +// Add more test cases as needed to cover different scenarios |
| 183 | +} // namespace |
0 commit comments