secd-62-account-backup.c [plain text]
#include <Security/SecBase.h>
#include <Security/SecItem.h>
#include <CoreFoundation/CFDictionary.h>
#include <Security/SecureObjectSync/SOSAccount.h>
#include <Security/SecureObjectSync/SOSCloudCircle.h>
#include <Security/SecureObjectSync/SOSInternal.h>
#include <Security/SecureObjectSync/SOSUserKeygen.h>
#include <Security/SecureObjectSync/SOSTransport.h>
#include <Security/SecureObjectSync/SOSViews.h>
#include <stdlib.h>
#include <unistd.h>
#include "secd_regressions.h"
#include "SOSTestDataSource.h"
#include "SOSRegressionUtilities.h"
#include <utilities/SecCFWrappers.h>
#include <Security/SecKeyPriv.h>
#include <securityd/SOSCloudCircleServer.h>
#if !TARGET_IPHONE_SIMULATOR
#include "SOSAccountTesting.h"
#endif
#include "SecdTestKeychainUtilities.h"
#if !TARGET_IPHONE_SIMULATOR
static CFDataRef CopyBackupKeyForString(CFStringRef string, CFErrorRef *error)
{
__block CFDataRef result = NULL;
CFStringPerformWithUTF8CFData(string, ^(CFDataRef stringAsData) {
result = SOSCopyDeviceBackupPublicKey(stringAsData, error);
});
return result;
}
static int kTestTestCount = 114;
#else
static int kTestTestCount = 1;
#endif
static void tests(void)
{
#if !TARGET_IPHONE_SIMULATOR
__block CFErrorRef error = NULL;
CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
CFStringRef cfaccount = CFSTR("test@test.org");
CFStringRef kTestView1 = CFSTR("TestView1");
CFStringRef kTestView2 = CFSTR("TestView2");
CFMutableSetRef testViews = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
CFSetAddValue(testViews, kTestView1);
SOSViewsSetTestViewsSet(testViews);
CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
SOSAccountRef alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
SOSAccountRef bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
CFDataRef alice_backup_key = CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error);
CFDataRef bob_backup_key = CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error);
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
CFReleaseNull(error);
ok(SOSAccountResetToOffering(alice_account, &error), "Reset to offering (%@)", error);
CFReleaseNull(error);
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
CFReleaseNull(error);
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
ok(SOSAccountJoinCircles(bob_account, &error), "Bob Applies (%@)", error);
CFReleaseNull(error);
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
{
CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
CFReleaseNull(error);
CFReleaseNull(applicants);
}
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
CFReleaseNull(peers);
is(SOSAccountUpdateView(alice_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
CFReleaseNull(error);
is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
CFReleaseNull(error);
ok(SOSAccountSetBackupPublicKey(alice_account, alice_backup_key, &error), "Set backup public key, alice (%@)", error);
CFReleaseNull(error);
ok(SOSAccountSetBackupPublicKey(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
CFReleaseNull(error);
ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice is in backup before sync?");
ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Is bob in the backup after sync? - 1");
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 4, "updates");
ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice is in backup after sync?");
ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "IS bob in the backup after sync");
ok(SOSAccountLeaveCircle(bob_account, &error), "Bob Leaves (%@)", error);
CFReleaseNull(error);
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Bob left the circle, Alice is not in the backup");
ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), kTestView1), "Bob is still in the backup!");
ok(SOSAccountJoinCircles(bob_account, &error));
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
{
CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
CFReleaseNull(error);
CFReleaseNull(applicants);
}
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
CFReleaseNull(error);
ok(SOSAccountSetBackupPublicKey(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
ok(SOSAccountRemoveBackupPublickey(bob_account, &error), "Removing Bob's backup key (%@)", error);
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is in the backup - should not be so!");
ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), kTestView1), "Bob is up to date in the backup!");
ok(SOSAccountSetBackupPublicKey(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
CFReleaseNull(error);
is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
ok(SOSAccountStartNewBackup(bob_account, kTestView1, &error), "Setting new backup public key for bob account failed: (%@)", error);
ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is not in the backup");
ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), kTestView1), "Bob is up to date in the backup - should not be so!");
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key should be in the backup");
ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Alice is in the backup");
ok(SOSAccountResetToEmpty(alice_account, &error), "Reset circle to empty");
CFReleaseNull(error);
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
ok(SOSAccountIsBackupRingEmpty(bob_account, kTestView1), "Bob should not be in the backup");
ok(SOSAccountIsBackupRingEmpty(alice_account, kTestView1), "Alice should not be in the backup");
CFReleaseNull(bob_account);
CFReleaseNull(alice_account);
CFReleaseNull(cfpassword);
SOSViewsSetTestViewsSet(NULL);
SOSUnregisterAllTransportMessages();
SOSUnregisterAllTransportCircles();
SOSUnregisterAllTransportKeyParameters();
CFArrayRemoveAllValues(key_transports);
CFArrayRemoveAllValues(circle_transports);
CFArrayRemoveAllValues(message_transports);
CFReleaseNull(testViews);
CFReleaseNull(kTestView1);
CFReleaseNull(kTestView2);
#endif
}
int secd_62_account_backup(int argc, char *const *argv)
{
plan_tests(kTestTestCount);
secd_test_setup_temp_keychain(__FUNCTION__, NULL);
tests();
return 0;
}