Skip to content

Commit

Permalink
Darwin: mark copyPublicKey as CF_RETURNS_RETAINED (project-chip#3…
Browse files Browse the repository at this point in the history
…7022)

for correct memory management when called from Swift.  added a test that shows how the annotation affects typical callees.

Co-authored-by: Andrei Litvin <[email protected]>
  • Loading branch information
kiel-apple and andy31415 authored Jan 10, 2025
1 parent 44bb573 commit db02354
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/MTRKeypair.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ MTR_AVAILABLE(ios(16.1), macos(13.0), watchos(9.1), tvos(16.1))
/**
* @brief Returns a copy of the public key for the keypair.
*/
- (SecKeyRef)copyPublicKey MTR_AVAILABLE(ios(18.4), macos(15.4), watchos(11.4), tvos(18.4));
- (SecKeyRef)copyPublicKey CF_RETURNS_RETAINED MTR_AVAILABLE(ios(18.4), macos(15.4), watchos(11.4), tvos(18.4));

/**
* @brief Returns public key for the keypair without adding a reference. DEPRECATED - please use copyPublicKey, otherwise this will leak.
Expand Down
54 changes: 54 additions & 0 deletions src/darwin/Framework/CHIPTests/MTRSwiftCertificateTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) 2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation

class MTRSwiftCertificateTests : XCTestCase {
func testGenerateIntermediateCert() {
let rootKeys = MTRTestKeys()
do {
let rootCert = try MTRCertificates.createRootCertificate(rootKeys, issuerID: nil, fabricID: nil)
XCTAssertNotNil(rootCert)

let intermediateKeys = MTRTestKeys()
XCTAssertNotNil(intermediateKeys)

// NB: `copyPublicKey` returns Unmanaged<SecKey> if `copyPublicKey` lacks CF_RETURNS_RETAINED annotation.
let intermediatePublicKey = intermediateKeys.copyPublicKey()
XCTAssertNotNil(intermediatePublicKey)

let intermediateCert = try MTRCertificates.createIntermediateCertificate(rootKeys, rootCertificate: rootCert, intermediatePublicKey: intermediatePublicKey, issuerID: nil as NSNumber?, fabricID: nil)
XCTAssertNotNil(intermediateCert)

guard let tlvCert = MTRCertificates.convertX509Certificate(intermediateCert) else {
XCTFail()
return
}
XCTAssertNotNil(tlvCert)

guard let derCert = MTRCertificates.convertMatterCertificate(tlvCert) else {
XCTFail()
return
}
XCTAssertNotNil(derCert)

XCTAssertEqual(intermediateCert, derCert)

} catch {
XCTFail()
}
}
}
4 changes: 4 additions & 0 deletions src/darwin/Framework/Matter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@
9B5CCB5D2C6EC890009DD99B /* MTRDevice_XPC.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B5CCB5A2C6EC890009DD99B /* MTRDevice_XPC.h */; };
9B5CCB602C6EE29E009DD99B /* MTRDeviceControllerXPCParameters.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9B5CCB5F2C6EE29E009DD99B /* MTRDeviceControllerXPCParameters.mm */; };
9B5CCB612C6EE29E009DD99B /* MTRDeviceControllerXPCParameters.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B5CCB5E2C6EE29E009DD99B /* MTRDeviceControllerXPCParameters.h */; };
9BC9E5872D3099FF00784A21 /* MTRSwiftCertificateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BC9E5862D3099FF00784A21 /* MTRSwiftCertificateTests.swift */; };
9BDA2A062C5D9AF800A32BDD /* MTRDevice_Concrete.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9BDA2A052C5D9AF800A32BDD /* MTRDevice_Concrete.mm */; };
9BDA2A082C5D9AFB00A32BDD /* MTRDevice_Concrete.h in Headers */ = {isa = PBXBuildFile; fileRef = 9BDA2A072C5D9AFB00A32BDD /* MTRDevice_Concrete.h */; };
9BFE5D502C6D3075007D4319 /* MTRDeviceController_XPC.h in Headers */ = {isa = PBXBuildFile; fileRef = 9BFE5D4E2C6D3075007D4319 /* MTRDeviceController_XPC.h */; };
Expand Down Expand Up @@ -835,6 +836,7 @@
9B5CCB5B2C6EC890009DD99B /* MTRDevice_XPC.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRDevice_XPC.mm; sourceTree = "<group>"; };
9B5CCB5E2C6EE29E009DD99B /* MTRDeviceControllerXPCParameters.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRDeviceControllerXPCParameters.h; sourceTree = "<group>"; };
9B5CCB5F2C6EE29E009DD99B /* MTRDeviceControllerXPCParameters.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRDeviceControllerXPCParameters.mm; sourceTree = "<group>"; };
9BC9E5862D3099FF00784A21 /* MTRSwiftCertificateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MTRSwiftCertificateTests.swift; sourceTree = "<group>"; };
9BDA2A052C5D9AF800A32BDD /* MTRDevice_Concrete.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRDevice_Concrete.mm; sourceTree = "<group>"; };
9BDA2A072C5D9AFB00A32BDD /* MTRDevice_Concrete.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRDevice_Concrete.h; sourceTree = "<group>"; };
9BFE5D4E2C6D3075007D4319 /* MTRDeviceController_XPC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRDeviceController_XPC.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1634,6 +1636,7 @@
3D4733AE2BDF1B80003DC19B /* MTRSetupPayloadTests.m */,
519498312A25581C00B3BABE /* MTRSetupPayloadInitializationTests.m */,
51E0FC0F2ACBBF230001E197 /* MTRSwiftDeviceTests.swift */,
9BC9E5862D3099FF00784A21 /* MTRSwiftCertificateTests.swift */,
5143851D2A65885500EDC8E6 /* MTRSwiftPairingTests.swift */,
997DED1926955D0200975E97 /* MTRThreadOperationalDatasetTests.mm */,
5A7947DD27BEC3F500434CF2 /* MTRXPCListenerSampleTests.m */,
Expand Down Expand Up @@ -2344,6 +2347,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
9BC9E5872D3099FF00784A21 /* MTRSwiftCertificateTests.swift in Sources */,
51742B4E29CB6B88009974FE /* MTRPairingTests.m in Sources */,
5131BF662BE2E1B000D5D6BC /* MTRTestCase.mm in Sources */,
51669AF02913204400F4AA36 /* MTRBackwardsCompatTests.m in Sources */,
Expand Down

0 comments on commit db02354

Please sign in to comment.