Skip to content

Commit

Permalink
Merge pull request #26 from Liftric/fix/memory-leaks
Browse files Browse the repository at this point in the history
Fix: memory leaks
  • Loading branch information
gaebel committed Jun 14, 2021
2 parents f5e9d41 + 94b2c31 commit 2bbb190
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 307 deletions.
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,26 @@ sourceSets {
#### Android

```kotlin
val store = KVault(context = Context)
val store = KVault(context)
```

#### iOS

```kotlin
val store = KVault(serviceName = "credentials", accessGroup = null)
val store = KVault(serviceName = null, accessGroup = null)
```

| Parameter | Description |
| :----------- | :---------------------------------- |
| serviceName | Used to categories objects. |
| accessGroup | Used to share objects between apps. |

### Setting

```kotlin
val stringStoredSuccessfully: Boolean = store.set(key = "PASSWORD", value = "546hfbfzzeujfdbfdz")
val intStoredSuccessfully: Boolean = store.set(key = "SECRET", value = 45678765)
val floatStoredSuccessfully: Boolean = store.set(key = "HEIGHT", value = 1.79)
val stringStored: Boolean = store.set(key = "LEET", stringValue = "1337")
val intStored: Boolean = store.set(key = "ANSWER", intValue = 42)
val floatStored: Boolean = store.set(key = "PI", floatValue = 3.14)
```

#### Supported Types
Expand All @@ -52,32 +57,32 @@ sourceSets {
### Getting

```kotlin
val stringValue: String? = store.string(forKey = "PASSWORD")
val intValue: Int? = store.int(forKey = "SECRET")
val stringValue: String? = store.string(forKey = "PASSWORD")
val intValue: Int? = store.int(forKey = "SECRET")
```

To check if an object is in the Keychain you can also use:

```kotlin
val existsObject: Boolean = store.existsObject(forKey = "PASSWORD")
val existsObject: Boolean = store.existsObject(forKey = "PASSWORD")
```

### Deleting

#### Single object

```kotlin
val isRemoved: Boolean = store.removeObject(forKey = "PASSWORD")
val isRemoved: Boolean = store.removeObject(forKey = "PASSWORD")
```

#### All objects

##### iOS

⚠️ If the service name and the access group are null it will delete all objects that are in the apps Keychain.
⚠️ If the service name and the access group are not null, it will only delete the objects that match the query.

```kotlin
store.clear()
val isCleared: Boolean = store.clear()
```

## License
Expand Down
18 changes: 1 addition & 17 deletions iostests/Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,15 @@
//

import UIKit
import kvault

@main
class AppDelegate: UIResponder, UIApplicationDelegate {



func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}


}

52 changes: 52 additions & 0 deletions iostests/Sources/DebugViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// DebugViewController.swift
// TestApp
//
// Created by Jan Gaebel on 11.06.21.
//

import UIKit
import kvault

class DebugViewController: UIViewController {

let button = UIButton()

override func viewDidLoad() {
super.viewDidLoad()

title = "test"
view.backgroundColor = .white
view.addSubview(button)

button.translatesAutoresizingMaskIntoConstraints = false
button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
button.setTitle("DEBUG", for: .normal)
button.setTitleColor(.red, for: .normal)

button.addTarget(self, action: #selector(debug), for: .touchUpInside)
}

@objc func debug() {
let vault = KVault(serviceName: "DEBUG", accessGroup: nil)
self.performOperations(on: vault)
}

private func performOperations(on vault: KVault) {
vault.set(key: "A", boolValue: true)
vault.set(key: "B", intValue: 1)
vault.set(key: "C", floatValue: 1)
vault.set(key: "D", doubleValue: 1)
vault.set(key: "E", longValue: 1)
vault.set(key: "F", stringValue: "T")
_ = vault.existsObject(forKey: "A")
_ = vault.bool(forKey: "A")
_ = vault.int(forKey: "B")
_ = vault.float(forKey: "C")
_ = vault.double(forKey: "D")
_ = vault.long(forKey: "E")
vault.deleteObject(forKey: "F")
vault.clear()
}
}
41 changes: 6 additions & 35 deletions iostests/Sources/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,13 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
}

func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}

func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}

func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UINavigationController(rootViewController: DebugViewController())
self.window = window
window.makeKeyAndVisible()
}
}

func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}

func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}


}

4 changes: 4 additions & 0 deletions iostests/TestApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
3D20E9072659290E0063F320 /* kvault.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3D20E9062659290E0063F320 /* kvault.framework */; };
3D20E9082659290E0063F320 /* kvault.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3D20E9062659290E0063F320 /* kvault.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
3D20E90B265929B90063F320 /* Keychain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D20E90A265929B90063F320 /* Keychain.swift */; };
3D4BC93E2673E8EF00D12CC3 /* DebugViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D4BC93D2673E8EF00D12CC3 /* DebugViewController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -50,6 +51,7 @@
3D20E9062659290E0063F320 /* kvault.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = kvault.framework; path = ../kvault/build/bin/iosX64/debugFramework/kvault.framework; sourceTree = "<group>"; };
3D20E90A265929B90063F320 /* Keychain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Keychain.swift; sourceTree = "<group>"; };
3D20E90C26592A440063F320 /* TestApp.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = TestApp.entitlements; sourceTree = "<group>"; };
3D4BC93D2673E8EF00D12CC3 /* DebugViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebugViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -96,6 +98,7 @@
children = (
3D20E8D5265928990063F320 /* AppDelegate.swift */,
3D20E8D7265928990063F320 /* SceneDelegate.swift */,
3D4BC93D2673E8EF00D12CC3 /* DebugViewController.swift */,
3D20E90A265929B90063F320 /* Keychain.swift */,
3D20E8E32659289C0063F320 /* Info.plist */,
);
Expand Down Expand Up @@ -219,6 +222,7 @@
files = (
3D20E90B265929B90063F320 /* Keychain.swift in Sources */,
3D20E8D6265928990063F320 /* AppDelegate.swift in Sources */,
3D4BC93E2673E8EF00D12CC3 /* DebugViewController.swift in Sources */,
3D20E8D8265928990063F320 /* SceneDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
30 changes: 15 additions & 15 deletions iostests/Tests/KeychainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class KeychainTests: XCTestCase {
]

testData.forEach {
sut.set(key: $0.key, value_____: $0.value)
sut.set(key: $0.key, stringValue: $0.value)
XCTAssertNotNil(sut.string(forKey: $0.key), "\($0.key) should not be null")
XCTAssertEqual($0.value, sut.string(forKey: $0.key), "\($0.key) should resolve \($0.value)")
XCTAssertNotEqual(sut.string(forKey: $0.key),
Expand All @@ -55,7 +55,7 @@ class KeychainTests: XCTestCase {
]

testData.forEach {
sut.set(key: $0.key, value___: $0.value)
sut.set(key: $0.key, intValue: $0.value)
XCTAssertNotNil(sut.int(forKey: $0.key)?.int32Value, "\($0.key) should not be null")
XCTAssertEqual($0.value, sut.int(forKey: $0.key)?.int32Value, "\($0.key) should resolve \($0.value)")
XCTAssertNotEqual(sut.int(forKey: $0.key)?.int32Value, 1337,
Expand All @@ -75,7 +75,7 @@ class KeychainTests: XCTestCase {
]

testData.forEach {
sut.set(key: $0.key, value____: $0.value)
sut.set(key: $0.key, longValue: $0.value)
XCTAssertNotNil(sut.long(forKey: $0.key)?.int64Value, "\($0.key) should not be null")
XCTAssertEqual($0.value, sut.long(forKey: $0.key)?.int64Value, "\($0.key) should resolve \($0.value)")
XCTAssertNotEqual(sut.long(forKey: $0.key)?.int64Value, 1337,
Expand All @@ -95,7 +95,7 @@ class KeychainTests: XCTestCase {
]

testData.forEach {
sut.set(key: $0.key, value__: $0.value)
sut.set(key: $0.key, floatValue: $0.value)
XCTAssertNotNil(sut.float(forKey: $0.key)?.floatValue, "\($0.key) should not be null")
XCTAssertEqual($0.value, sut.float(forKey: $0.key)?.floatValue, "\($0.key) should resolve \($0.value)")
XCTAssertNotEqual(sut.float(forKey: $0.key)?.floatValue, 31337.31337,
Expand All @@ -115,7 +115,7 @@ class KeychainTests: XCTestCase {
]

testData.forEach {
sut.set(key: $0.key, value_: $0.value)
sut.set(key: $0.key, doubleValue: $0.value)
XCTAssertNotNil(sut.double(forKey: $0.key)?.doubleValue, "\($0.key) should not be null")
XCTAssertEqual($0.value, sut.double(forKey: $0.key)?.doubleValue, "\($0.key) should resolve \($0.value)")
XCTAssertNotEqual(sut.double(forKey: $0.key)?.doubleValue, 31337.31337,
Expand All @@ -134,7 +134,7 @@ class KeychainTests: XCTestCase {
]

testData.forEach {
sut.set(key: $0.key, value: $0.value)
sut.set(key: $0.key, boolValue: $0.value)
XCTAssertNotNil(sut.bool(forKey: $0.key)?.boolValue, "\($0.key) should not be null")
XCTAssertEqual($0.value, sut.bool(forKey: $0.key)?.boolValue, "\($0.key) should resolve \($0.value)")
}
Expand All @@ -146,23 +146,23 @@ class KeychainTests: XCTestCase {

func testExistsObject() {
XCTAssertFalse(sut.existsObject(forKey: "blank"), "Blank should not exist")
sut.set(key: "blank", value_____: "124")
sut.set(key: "bla", value___: 1)
sut.set(key: "blank", stringValue: "124")
sut.set(key: "bla", intValue: 1)
XCTAssertTrue(sut.existsObject(forKey: "blank"), "Blank should exist")
}

func testDeleteObject() {
XCTAssertFalse(sut.existsObject(forKey: "blank"), "Blank should not exist")
sut.set(key: "blank", value_____: "123")
sut.set(key: "blank", stringValue: "123")
XCTAssertTrue(sut.existsObject(forKey: "blank"), "Blank should exist")
sut.deleteObject(forKey: "blank")
XCTAssertFalse(sut.existsObject(forKey: "blank"), "Blank should not exist anymore")
}

func testOverwrite() {
sut.set(key: "keyX", value_____: "dummyX")
sut.set(key: "keyX", stringValue: "dummyX")
XCTAssertEqual("dummyX", sut.string(forKey: "keyX"))
sut.set(key: "keyX", value_____: "dummyXY")
sut.set(key: "keyX", stringValue: "dummyXY")
XCTAssertEqual("dummyXY", sut.string(forKey: "keyX"))
XCTAssertNotEqual("dummyX", sut.string(forKey: "keyX"))
}
Expand All @@ -171,7 +171,7 @@ class KeychainTests: XCTestCase {
let keys = ["key1", "key2", "key3", "key4", "key5"]

keys.forEach {
sut.set(key: $0, value_____: "dummy")
sut.set(key: $0, stringValue: "dummy")
XCTAssertTrue(sut.existsObject(forKey: $0), "\($0) should exist")
}

Expand All @@ -189,7 +189,7 @@ class KeychainTests: XCTestCase {

[sut, sut2].forEach { sut in
keys.forEach {
sut.set(key: $0, value_____: "dummy")
sut.set(key: $0, stringValue: "dummy")
XCTAssertTrue(sut.existsObject(forKey: $0), "\($0) should exist")
}
}
Expand All @@ -205,7 +205,7 @@ class KeychainTests: XCTestCase {
}

keys.forEach {
sut2.set(key: $0, value_____: "dummy")
sut2.set(key: $0, stringValue: "dummy")
XCTAssertTrue(sut2.existsObject(forKey: $0), "\($0) should exist")
}

Expand All @@ -227,7 +227,7 @@ class KeychainTests: XCTestCase {

[sut, sut2].forEach { sut in
keys.forEach {
sut.set(key: $0, value_____: "dummy")
sut.set(key: $0, stringValue: "dummy")
XCTAssertTrue(sut.existsObject(forKey: $0), "\($0) should exist")
}
}
Expand Down
1 change: 1 addition & 0 deletions kvault/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ kotlin {
}
}
}

tasks {
val iosX64Test by existing(KotlinNativeSimulatorTest::class) {
filter.excludeTestsMatching("com.liftric.kvault.KVaultTest")
Expand Down
Loading

0 comments on commit 2bbb190

Please sign in to comment.