secd-63-account-resurrection.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 <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>
#include "SOSAccountTesting.h"
#include "SecdTestKeychainUtilities.h"
static int kTestTestCount = 112;
typedef void (^stir_block)(int expected_iterations);
typedef int (^execute_block)();
static void stirBetween(stir_block stir, ...) {
va_list va;
va_start(va, stir);
execute_block execute = NULL;
while ((execute = va_arg(va, execute_block)) != NULL)
stir(execute());
}
static void VerifyCountAndAcceptAllApplicants(SOSAccountRef account, int expected)
{
CFErrorRef error = NULL;
CFArrayRef applicants = SOSAccountCopyApplicants(account, &error);
SKIP: {
skip("Empty applicant array", 2, applicants);
is(CFArrayGetCount(applicants), expected, "Applicants: %@ (%@)", applicants, error);
CFReleaseNull(error);
ok(SOSAccountAcceptApplicants(account , applicants, &error), "Accepting all (%@)", error);
CFReleaseNull(error);
}
CFReleaseNull(applicants);
}
static void tests(void)
{
__block CFErrorRef error = NULL;
CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
CFStringRef cfaccount = CFSTR("test@test.org");
CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
const CFStringRef data_source_name = CFSTR("TestSource");
SOSAccountRef alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), data_source_name);
SOSAccountRef bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), data_source_name);
SOSAccountRef carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), data_source_name);
SOSAccountRef alice_resurrected = NULL;
__block CFDataRef frozen_alice = NULL;
stirBetween(^(int expected){
is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), expected, "stirring");
}, ^{
ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account , cfaccount, cfpassword, &error), "bob credential setting (%@)", error);
return 1;
}, ^{
ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account , cfaccount, cfpassword, &error), "alice credential setting (%@)", error);
CFReleaseNull(error);
ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "carole credential setting (%@)", error);
CFReleaseNull(error);
ok(SOSAccountResetToOffering(alice_account , &error), "Reset to offering (%@)", error);
CFReleaseNull(error);
return 2;
}, ^{
ok(SOSAccountJoinCircles(bob_account , &error), "Bob Applies (%@)", error);
CFReleaseNull(error);
return 2;
}, ^{
VerifyCountAndAcceptAllApplicants(alice_account, 1);
return 3;
}, ^{
accounts_agree("bob&alice pair", bob_account, alice_account);
is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSNeverLeftCircle, "Bob affirms he hasn't left.");
CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
CFReleaseNull(peers);
return 1;
}, ^{
frozen_alice = SOSAccountCopyEncodedData(alice_account, kCFAllocatorNull, &error);
ok(frozen_alice, "Copy encoded %@", error);
CFReleaseNull(error);
SOSAccountPurgePrivateCredential(alice_account);
ok(SOSAccountLeaveCircle(alice_account , &error), "Alice Leaves (%@)", error);
CFReleaseNull(error);
return 3;
}, ^{
accounts_agree("Alice bails", bob_account, alice_account);
{
CFArrayRef concurring = SOSAccountCopyConcurringPeers(alice_account, &error);
ok(concurring && CFArrayGetCount(concurring) == 2, "See two concurring %@ (%@)", concurring, error);
CFReleaseNull(error);
CFReleaseNull(concurring);
}
return 1;
},
NULL);
alice_resurrected = CreateAccountForLocalChangesFromData(frozen_alice, CFSTR("Alice risen"), data_source_name);
ProcessChangesUntilNoChange(changes, alice_resurrected, bob_account, carole_account, NULL);
stirBetween(^(int expected){
is(ProcessChangesUntilNoChange(changes, alice_resurrected, bob_account, carole_account, NULL), expected, "stirring");
}, ^{
ok(SOSAccountAssertUserCredentialsAndUpdate(alice_resurrected, cfaccount, cfpassword, &error), "alice_resurrected credential setting (%@)", error);
CFReleaseNull(error);
return 1;
}, ^{
ok(!SOSAccountIsInCircle(alice_resurrected, &error), "Ressurrected not in circle: %@", error);
CFReleaseNull(error);
ok(SOSAccountJoinCircles(alice_resurrected, &error), "Risen-alice Applies (%@)", error);
CFReleaseNull(error);
return 2;
}, ^{
VerifyCountAndAcceptAllApplicants(bob_account, 1);
return 3;
},
NULL);
CFReleaseNull(bob_account);
CFReleaseNull(alice_account);
CFReleaseNull(carole_account);
CFReleaseNull(alice_resurrected);
CFReleaseNull(frozen_alice);
SOSUnregisterAllTransportMessages();
SOSUnregisterAllTransportCircles();
SOSUnregisterAllTransportKeyParameters();
CFArrayRemoveAllValues(key_transports);
CFArrayRemoveAllValues(circle_transports);
CFArrayRemoveAllValues(message_transports);
}
int secd_63_account_resurrection(int argc, char *const *argv)
{
plan_tests(kTestTestCount);
secd_test_setup_temp_keychain(__FUNCTION__, NULL);
tests();
return 0;
}