This repository was archived by the owner on Feb 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 293
Initial support for some scoped types #1250
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /// Copyright 2023 Google LLC | ||
| /// | ||
| /// 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 | ||
| /// | ||
| /// https://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. | ||
|
|
||
| #ifndef SANTA__COMMON__SCOPEDCFTYPEREF_H | ||
| #define SANTA__COMMON__SCOPEDCFTYPEREF_H | ||
|
|
||
| #include <CoreFoundation/CoreFoundation.h> | ||
|
|
||
| #include "Source/common/ScopedTypeRef.h" | ||
|
|
||
| namespace santa::common { | ||
|
|
||
| template <typename CFT> | ||
| using ScopedCFTypeRef = ScopedTypeRef<CFT, (CFT)NULL, CFRetain, CFRelease>; | ||
|
|
||
| } // namespace santa::common | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /// Copyright 2023 Google LLC | ||
| /// | ||
| /// 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 | ||
| /// | ||
| /// https://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. | ||
|
|
||
| #include <CoreFoundation/CoreFoundation.h> | ||
| #include <Security/Security.h> | ||
| #import <XCTest/XCTest.h> | ||
| #include "XCTest/XCTest.h" | ||
|
|
||
| #include "Source/common/ScopedCFTypeRef.h" | ||
|
|
||
| using santa::common::ScopedCFTypeRef; | ||
|
|
||
| @interface ScopedCFTypeRefTest : XCTestCase | ||
| @end | ||
|
|
||
| @implementation ScopedCFTypeRefTest | ||
|
|
||
| - (void)testDefaultConstruction { | ||
| // Default construction creates wraps a NULL object | ||
| ScopedCFTypeRef<CFNumberRef> scopedRef; | ||
| XCTAssertFalse(scopedRef.Unsafe()); | ||
| } | ||
|
|
||
| - (void)testOperatorBool { | ||
| // Operator bool is `false` when object is null | ||
| { | ||
| ScopedCFTypeRef<CFNumberRef> scopedNullRef; | ||
| XCTAssertFalse(scopedNullRef.Unsafe()); | ||
| XCTAssertFalse(scopedNullRef); | ||
| } | ||
|
|
||
| // Operator bool is `true` when object is NOT null | ||
| { | ||
| int x = 123; | ||
| CFNumberRef numRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &x); | ||
|
|
||
| ScopedCFTypeRef<CFNumberRef> scopedNumRef = ScopedCFTypeRef<CFNumberRef>::Assume(numRef); | ||
| XCTAssertTrue(scopedNumRef.Unsafe()); | ||
| XCTAssertTrue(scopedNumRef); | ||
| } | ||
| } | ||
|
|
||
| // Note that CFMutableArray is used for testing, even when subtypes aren't | ||
| // needed, because it is never optimized into immortal constant values, unlike | ||
| // other types. | ||
| - (void)testAssume { | ||
| int want = 123; | ||
| int got = 0; | ||
| CFMutableArrayRef array = CFArrayCreateMutable(nullptr, /*capacity=*/0, &kCFTypeArrayCallBacks); | ||
|
|
||
| // Baseline state, initial retain count is 1 after object creation | ||
| XCTAssertEqual(1, CFGetRetainCount(array)); | ||
|
|
||
| CFNumberRef numRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &want); | ||
| CFArrayAppendValue(array, numRef); | ||
| CFRelease(numRef); | ||
|
|
||
| XCTAssertEqual(1, CFArrayGetCount(array)); | ||
|
|
||
| { | ||
| ScopedCFTypeRef<CFMutableArrayRef> scopedArray = | ||
| ScopedCFTypeRef<CFMutableArrayRef>::Assume(array); | ||
|
|
||
| // Ensure ownership was taken, and retain count remains unchanged | ||
| XCTAssertTrue(scopedArray.Unsafe()); | ||
| XCTAssertEqual(1, CFGetRetainCount(scopedArray.Unsafe())); | ||
|
|
||
| // Make sure the object contains expected contents | ||
| CFMutableArrayRef ref = scopedArray.Unsafe(); | ||
| XCTAssertEqual(1, CFArrayGetCount(ref)); | ||
| XCTAssertTrue( | ||
| CFNumberGetValue((CFNumberRef)CFArrayGetValueAtIndex(ref, 0), kCFNumberIntType, &got)); | ||
| XCTAssertEqual(want, got); | ||
| } | ||
| } | ||
|
|
||
| // Note that CFMutableArray is used for testing, even when subtypes aren't | ||
| // needed, because it is never optimized into immortal constant values, unlike | ||
| // other types. | ||
| - (void)testRetain { | ||
| int want = 123; | ||
| int got = 0; | ||
| CFMutableArrayRef array = CFArrayCreateMutable(nullptr, /*capacity=*/0, &kCFTypeArrayCallBacks); | ||
|
|
||
| // Baseline state, initial retain count is 1 after object creation | ||
| XCTAssertEqual(1, CFGetRetainCount(array)); | ||
|
|
||
| CFNumberRef numRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &want); | ||
| CFArrayAppendValue(array, numRef); | ||
| CFRelease(numRef); | ||
|
|
||
| XCTAssertEqual(1, CFArrayGetCount(array)); | ||
|
|
||
| { | ||
| ScopedCFTypeRef<CFMutableArrayRef> scopedArray = | ||
| ScopedCFTypeRef<CFMutableArrayRef>::Retain(array); | ||
|
|
||
| // Ensure ownership was taken, and retain count was incremented | ||
| XCTAssertTrue(scopedArray.Unsafe()); | ||
| XCTAssertEqual(2, CFGetRetainCount(scopedArray.Unsafe())); | ||
|
|
||
| // Make sure the object contains expected contents | ||
| CFMutableArrayRef ref = scopedArray.Unsafe(); | ||
| XCTAssertEqual(1, CFArrayGetCount(ref)); | ||
| XCTAssertTrue( | ||
| CFNumberGetValue((CFNumberRef)CFArrayGetValueAtIndex(ref, 0), kCFNumberIntType, &got)); | ||
| XCTAssertEqual(want, got); | ||
| } | ||
|
|
||
| // The original `array` object should still be valid due to the extra retain. | ||
| // Ensure the retain count has decreased since `scopedArray` went out of scope | ||
| XCTAssertEqual(1, CFArrayGetCount(array)); | ||
| } | ||
|
|
||
| - (void)testInto { | ||
| ScopedCFTypeRef<CFURLRef> scopedURLRef = | ||
| ScopedCFTypeRef<CFURLRef>::Assume(CFURLCreateWithFileSystemPath( | ||
| kCFAllocatorDefault, CFSTR("/usr/bin/true"), kCFURLPOSIXPathStyle, YES)); | ||
|
|
||
| ScopedCFTypeRef<SecStaticCodeRef> scopedCodeRef; | ||
| XCTAssertFalse(scopedCodeRef); | ||
|
|
||
| SecStaticCodeCreateWithPath(scopedURLRef.Unsafe(), kSecCSDefaultFlags, | ||
| scopedCodeRef.InitializeInto()); | ||
|
|
||
| // Ensure the scoped object was initialized | ||
| XCTAssertTrue(scopedCodeRef); | ||
| } | ||
|
|
||
| @end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /// Copyright 2023 Google LLC | ||
| /// | ||
| /// 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 | ||
| /// | ||
| /// https://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. | ||
|
|
||
| #ifndef SANTA__COMMON__SCOPEDIOOBJECTREF_H | ||
| #define SANTA__COMMON__SCOPEDIOOBJECTREF_H | ||
|
|
||
| #include <IOKit/IOKitLib.h> | ||
|
|
||
| #include "Source/common/ScopedTypeRef.h" | ||
|
|
||
| namespace santa::common { | ||
|
|
||
| template <typename IOT> | ||
| using ScopedIOObjectRef = | ||
| ScopedTypeRef<IOT, (IOT)IO_OBJECT_NULL, IOObjectRetain, IOObjectRelease>; | ||
|
|
||
| } | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /// Copyright 2023 Google LLC | ||
| /// | ||
| /// 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 | ||
| /// | ||
| /// https://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. | ||
|
|
||
| #include <CoreFoundation/CoreFoundation.h> | ||
| #include <IOKit/IOKitLib.h> | ||
| #include <IOKit/usb/IOUSBLib.h> | ||
| #import <XCTest/XCTest.h> | ||
|
|
||
| #include "Source/common/ScopedIOObjectRef.h" | ||
| #include "Source/santad/Logs/EndpointSecurity/Serializers/Utilities.h" | ||
|
|
||
| using santa::common::ScopedIOObjectRef; | ||
| using santa::santad::logs::endpoint_security::serializers::Utilities::GetDefaultIOKitCommsPort; | ||
|
|
||
| @interface ScopedIOObjectRefTest : XCTestCase | ||
| @end | ||
|
|
||
| @implementation ScopedIOObjectRefTest | ||
|
|
||
| - (void)testDefaultConstruction { | ||
| // Default construction creates wraps a NULL object | ||
| ScopedIOObjectRef<io_object_t> scopedRef; | ||
| XCTAssertFalse(scopedRef.Unsafe()); | ||
| } | ||
|
|
||
| - (void)testOperatorBool { | ||
| // Operator bool is `false` when object is null | ||
| { | ||
| ScopedIOObjectRef<io_object_t> scopedNullRef; | ||
| XCTAssertFalse(scopedNullRef.Unsafe()); | ||
| XCTAssertFalse(scopedNullRef); | ||
| } | ||
|
|
||
| // Operator bool is `true` when object is NOT null | ||
| { | ||
| CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName); | ||
| XCTAssertNotEqual((CFMutableDictionaryRef)NULL, matchingDict); | ||
|
|
||
| io_service_t service = IOServiceGetMatchingService(GetDefaultIOKitCommsPort(), matchingDict); | ||
|
|
||
| ScopedIOObjectRef<io_service_t> scopedServiceRef = | ||
| ScopedIOObjectRef<io_service_t>::Assume(service); | ||
|
|
||
| XCTAssertTrue(scopedServiceRef.Unsafe()); | ||
| XCTAssertTrue(scopedServiceRef); | ||
| } | ||
| } | ||
|
|
||
| - (void)testAssume { | ||
| CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName); | ||
| XCTAssertNotEqual((CFMutableDictionaryRef)NULL, matchingDict); | ||
|
|
||
| io_service_t service = IOServiceGetMatchingService(GetDefaultIOKitCommsPort(), matchingDict); | ||
|
|
||
| // Baseline state, initial retain count is 1 after object creation | ||
| XCTAssertEqual(1, IOObjectGetUserRetainCount(service)); | ||
| XCTAssertNotEqual(IO_OBJECT_NULL, service); | ||
|
|
||
| { | ||
| ScopedIOObjectRef<io_service_t> scopedIORef = ScopedIOObjectRef<io_service_t>::Assume(service); | ||
|
|
||
| // Ensure ownership was taken, and retain count remains unchanged | ||
| XCTAssertTrue(scopedIORef.Unsafe()); | ||
| XCTAssertEqual(1, IOObjectGetUserRetainCount(scopedIORef.Unsafe())); | ||
| XCTAssertNotEqual(IO_OBJECT_NULL, scopedIORef.Unsafe()); | ||
| } | ||
| } | ||
|
|
||
| - (void)testRetain { | ||
| CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName); | ||
| XCTAssertNotEqual((CFMutableDictionaryRef)NULL, matchingDict); | ||
|
|
||
| io_service_t service = IOServiceGetMatchingService(GetDefaultIOKitCommsPort(), matchingDict); | ||
|
|
||
| // Baseline state, initial retain count is 1 after object creation | ||
| XCTAssertEqual(1, IOObjectGetUserRetainCount(service)); | ||
| XCTAssertNotEqual(IO_OBJECT_NULL, service); | ||
|
|
||
| { | ||
| ScopedIOObjectRef<io_service_t> scopedIORef = ScopedIOObjectRef<io_service_t>::Retain(service); | ||
|
|
||
| // Ensure ownership was taken, and retain count was incremented | ||
| XCTAssertTrue(scopedIORef.Unsafe()); | ||
| XCTAssertEqual(2, IOObjectGetUserRetainCount(scopedIORef.Unsafe())); | ||
| XCTAssertNotEqual(IO_OBJECT_NULL, scopedIORef.Unsafe()); | ||
| } | ||
|
|
||
| // The original `service` object should still be valid due to the extra retain. | ||
| // Ensure the retain count has decreased since `scopedIORef` went out of scope. | ||
| XCTAssertEqual(1, IOObjectGetUserRetainCount(service)); | ||
| } | ||
|
|
||
| @end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.