Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests] Include more information in asserts in MonoTouchFixtures.Security.RecordTest.DeskCase_83099_InmutableDictionary. #21354

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions tests/monotouch-test/Security/RecordTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,16 @@ public void DeskCase_83099_InmutableDictionary ()
var testUsername = "testusername";

//TEST 1: Save a keychain value
var test1 = SaveUserPassword (testUsername, "testValue1");
Assert.IsTrue (test1, "Password could not be saved to keychain");
var test1 = SaveUserPassword (testUsername, "testValue1", out var queryCode, out var addCode, out var updateCode);
Assert.IsTrue (test1, $"Password could not be saved to keychain. queryCode: {queryCode} addCode: {addCode} updateCode: {updateCode}");

//TEST 2: Get the saved keychain value
var test2 = GetUserPassword (testUsername);
Assert.IsTrue (StringUtil.StringsEqual (test2, "testValue1", false));

//TEST 3: Update the keychain value
var test3 = SaveUserPassword (testUsername, "testValue2");
Assert.IsTrue (test3, "Password could not be saved to keychain");
var test3 = SaveUserPassword (testUsername, "testValue2", out queryCode, out addCode, out updateCode);
Assert.IsTrue (test3, "Password could not be saved to keychain. queryCode: {queryCode} addCode: {addCode} updateCode: {updateCode}");

//TEST 4: Get the updated keychain value
var test4 = GetUserPassword (testUsername);
Expand Down Expand Up @@ -353,27 +353,28 @@ public static string GetUserPassword (string username)
return password;
}

public static bool SaveUserPassword (string username, string password)
public static bool SaveUserPassword (string username, string password, out SecStatusCode queryCode, out SecStatusCode addCode, out SecStatusCode updateCode)
{
addCode = (SecStatusCode) (-1); // pick a value that doesn't already exist in SecStatusCode
updateCode = (SecStatusCode) (-1); // pick a value that doesn't already exist in SecStatusCode
var success = false;
var searchRecord = CreateSecRecord (SecKind.InternetPassword,
server: "Test1",
account: username.ToLower ()
);
SecStatusCode queryCode;
var record = SecKeyChain.QueryAsRecord (searchRecord, out queryCode);
if (queryCode == SecStatusCode.ItemNotFound) {
record = CreateSecRecord (SecKind.InternetPassword,
server: "Test1",
account: username.ToLower (),
valueData: NSData.FromString (password)
);
var addCode = SecKeyChain.Add (record);
addCode = SecKeyChain.Add (record);
success = (addCode == SecStatusCode.Success);
}
if (queryCode == SecStatusCode.Success && record is not null) {
record.ValueData = NSData.FromString (password);
var updateCode = SecKeyChain.Update (searchRecord, record);
updateCode = SecKeyChain.Update (searchRecord, record);
success = (updateCode == SecStatusCode.Success);
}
return success;
Expand Down