#include <Regressions/SOSTestDevice.h>
#include <Regressions/SOSTestDataSource.h>
#include "secd_regressions.h"
#include "SecdTestKeychainUtilities.h"
#include <SecureObjectSync/SOSEngine.h>
#include <SecureObjectSync/SOSPeer.h>
#include <Security/SecBase64.h>
#include <Security/SecItem.h>
#include <Security/SecItemPriv.h>
#include <corecrypto/ccsha2.h>
#include <securityd/SecItemServer.h>
#include <securityd/SecItemDataSource.h>
#include <utilities/SecCFWrappers.h>
#include <utilities/SecIOFormat.h>
#include <utilities/SecFileLocations.h>
#include <AssertMacros.h>
#include <stdint.h>
static int kTestTestCount = 1286;
__unused static bool SOSCircleHandleCircleWithLock(SOSEngineRef engine, CFStringRef myID, CFDataRef message, CFErrorRef *error) {
CFMutableArrayRef trustedPeers = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
CFMutableArrayRef untrustedPeers = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
CFStringRef peerID = NULL;
const uint8_t expected[20] = { 0xea, 0x6c, 0x01, 0x4d,
0xc7, 0x2d, 0x6f, 0x8c,
0xcd, 0x1e, 0xd9, 0x2a,
0xce, 0x1d, 0x41, 0xf0,
0xd8, 0xde, 0x89, 0x57 };
const char resultSize = sizeof(expected);
CFDataRef coder = CFDataCreate(kCFAllocatorDefault, expected, resultSize);
CFArrayForEachC(SOSEngineGetPeerIDs(engine), peerID){
CFArrayAppendValue(trustedPeers, peerID);
SOSEngineSetCoderData(engine, peerID, coder, error);
};
CFReleaseNull(coder);
CFShow(trustedPeers);
SOSEngineCircleChanged_locked(engine, myID,trustedPeers, untrustedPeers);
peerID = (CFStringRef)CFArrayGetValueAtIndex(trustedPeers, 0);
CFArrayAppendValue(untrustedPeers, peerID);
CFArrayRemoveAllValue(trustedPeers, peerID);
SOSEngineCircleChanged_locked(engine, myID, trustedPeers, untrustedPeers);
CFArrayAppendValue(trustedPeers, peerID);
CFArrayRemoveAllValue(untrustedPeers, peerID);
return true;
}
static void testsync3(const char *name, const char *test_directive, const char *test_reason) {
__block int iteration=0;
SOSTestDeviceListTestSync(name, test_directive, test_reason, 0, false, ^bool(SOSTestDeviceRef source, SOSTestDeviceRef dest) {
iteration++;
if (iteration == 12 || iteration == 13) {
pass("pre-rcv %@", dest);
}
if (iteration == 19) {
pass("pre-send %@", source);
}
return false;
}, ^bool(SOSTestDeviceRef source, SOSTestDeviceRef dest, SOSMessageRef message) {
if (iteration == 10) {
pass("pre-add %@", source);
SOSTestDeviceAddRemoteGenericItem(source, CFSTR("test_account"), CFSTR("test service"));
pass("post-add %@", source);
return true; } else if (iteration == 12 || iteration == 15) {
pass("post-rcv %@", dest);
}
return false;
}, CFSTR("AAA"), CFSTR("BBB"), CFSTR("CCC"), NULL);
}
static void testsync2(const char *name, const char *test_directive, const char *test_reason, void (^aliceInit)(SOSDataSourceRef ds), void (^bobInit)(SOSDataSourceRef ds), CFStringRef msg, ...) {
__block int iteration=0;
SOSTestDeviceListTestSync(name, test_directive, test_reason, kSOSPeerVersion, false, ^bool(SOSTestDeviceRef source, SOSTestDeviceRef dest) {
if (iteration == 96) {
pass("%@ before message", source);
}
return false;
}, ^bool(SOSTestDeviceRef source, SOSTestDeviceRef dest, SOSMessageRef message) {
iteration++;
if (iteration == 60) {
pass("%@ before addition", source);
SOSTestDeviceAddRemoteGenericItem(source, CFSTR("test_account"), CFSTR("test service"));
pass("%@ after addition", source);
return true;
}
return false;
}, CFSTR("alice"), CFSTR("bob"), CFSTR("claire"), CFSTR("dave"),CFSTR("edward"), CFSTR("frank"), CFSTR("gary"), NULL);
}
static void testsync(const char *name, const char *test_directive, const char *test_reason, void (^aliceInit)(SOSDataSourceRef ds), void (^bobInit)(SOSDataSourceRef ds), ...) {
__block int msg_index = 0;
__block int last_msg_index = 0;
va_list args;
va_start(args, bobInit);
CFArrayRef messages = CFArrayCreateForVC(kCFAllocatorDefault, &kCFTypeArrayCallBacks, args);
SOSTestDeviceListTestSync(name, test_directive, test_reason, 0, false,
^bool(SOSTestDeviceRef source, SOSTestDeviceRef dest) {
if (msg_index == 0) {
aliceInit(source->ds);
bobInit(dest->ds);
return true;
}
return false;
}, ^bool(SOSTestDeviceRef source, SOSTestDeviceRef dest, SOSMessageRef message) {
CFStringRef hexMsg = msg_index < CFArrayGetCount(messages) ? (CFStringRef)CFArrayGetValueAtIndex(messages, msg_index) : 0;
if (message) {
msg_index++;
CFStringRef messageDigestStr = SOSMessageCopyDigestHex(message);
if (hexMsg) {
if (CFEqual(messageDigestStr, hexMsg)) {
pass("%s %@ handled message [%d] %@", name, SOSEngineGetMyID(dest->ds->engine), msg_index, message);
} else {
fail("%s %@ received message [%d] digest %@ != %@ %@", name, SOSEngineGetMyID(dest->ds->engine), msg_index, messageDigestStr, hexMsg, message);
}
last_msg_index = msg_index;
} else {
fail("%s %@ sent extra message [%d] with digest %@: %@", name, SOSEngineGetMyID(source->ds->engine), msg_index, messageDigestStr, message);
}
CFReleaseSafe(messageDigestStr);
}
return false;
}, CFSTR("alice"), CFSTR("bob"), NULL);
if (msg_index < CFArrayGetCount(messages)) {
fail("%s nothing sent expecting message [%d] digest %@", name, msg_index, CFArrayGetValueAtIndex(messages, msg_index));
} else if (last_msg_index < msg_index) {
fail("%s exchanged %d messages not in expected list", name, msg_index - last_msg_index);
}
}
#if 0
static void testsyncempty(void) {
CFMutableArrayRef deviceIDs = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
for (int deviceIX=0; deviceIX < 10; ++deviceIX) {
CFStringRef deviceID = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%c"), 'A' + deviceIX);
CFArrayAppendValue(deviceIDs, deviceID);
CFReleaseSafe(deviceID);
if (deviceIX > 0) {
for (CFIndex version = 0; version < 3; version += 2) {
CFMutableDictionaryRef testDevices = SOSTestDeviceListCreate(false, version, deviceIDs);
SOSTestDeviceListSync("syncempty", test_directive, test_reason, testDevices, NULL, NULL);
SOSTestDeviceListInSync("syncempty", test_directive, test_reason, testDevices);
CFReleaseSafe(testDevices);
}
}
}
CFReleaseSafe(deviceIDs);
}
#endif
static CFIndex syncmany_add(int iteration) {
if (iteration % 7 < 3 && iteration < 10)
return iteration % 17 + 200;
return 0;
}
static void testsyncmany(const char *name, const char *test_directive, const char *test_reason, int devFirst, int devCount, int version, CFIndex (*should_add)(int iteration)) {
CFMutableArrayRef deviceIDs = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
for (int deviceIX=0; deviceIX < devCount; ++deviceIX) {
CFStringRef deviceID = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%c"), 'A' + deviceIX);
CFArrayAppendValue(deviceIDs, deviceID);
CFReleaseSafe(deviceID);
if (deviceIX >= devFirst) {
CFMutableDictionaryRef testDevices = SOSTestDeviceListCreate(false, version, deviceIDs);
__block int iteration = 0;
SOSTestDeviceListSync(name, test_directive, test_reason, testDevices, ^bool(SOSTestDeviceRef source, SOSTestDeviceRef dest) {
bool didAdd = false;
iteration++;
CFIndex toAdd = should_add(iteration);
if (toAdd) {
CFStringRef account = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("item%d"), iteration);
didAdd = SOSTestDeviceAddGenericItems(source, toAdd, account, CFSTR("testsyncmany"));
CFReleaseSafe(account);
}
if (iteration == 279 || iteration == 459)
pass("pre-send[%d] %@", iteration, source);
return didAdd;
}, ^bool(SOSTestDeviceRef source, SOSTestDeviceRef dest, SOSMessageRef message) {
if (iteration == 262)
pass("post-rcv[%d] %@", iteration, dest);
if (iteration == 272 || iteration == 279)
pass("post-send[%d] %@", iteration, source);
return false;
});
SOSTestDeviceListInSync(name, test_directive, test_reason, testDevices);
CFReleaseSafe(testDevices);
}
}
CFReleaseSafe(deviceIDs);
}
static void testsync2p(void) {
__block int iteration = 0;
SOSTestDeviceListTestSync("testsync2p", test_directive, test_reason, 0, false, ^bool(SOSTestDeviceRef source, SOSTestDeviceRef dest) {
iteration++;
if (iteration <= 10) {
CFStringRef account = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("item%d"), iteration);
SOSTestDeviceAddGenericItem(source, account, CFSTR("testsync2p"));
CFReleaseSafe(account);
return true;
}
return false;
}, ^bool(SOSTestDeviceRef source, SOSTestDeviceRef dest, SOSMessageRef message) {
return false;
}, CFSTR("Atestsync2p"), CFSTR("Btestsync2p"), NULL);
}
static void synctests(void) {
#if 0
CFDictionaryRef item = CFDictionaryCreateForCFTypes
(0,
kSecClass, kSecClassGenericPassword,
kSecAttrAccessible, kSecAttrAccessibleWhenUnlocked,
kSecAttrSynchronizable, kCFBooleanTrue,
kSecAttrService, CFSTR("service"),
kSecAttrAccount, CFSTR("account"),
NULL);
SecItemAdd(item, NULL);
CFReleaseSafe(item);
#endif
SKIP:
{
#ifdef NO_SERVER
WithPathInKeychainDirectory(CFSTR("keychain-2-debug.db"), ^(const char *keychain_path) {
unlink(keychain_path);
});
WithPathInKeychainDirectory(CFSTR("keychain-2.db"), ^(const char *keychain_path) {
unlink(keychain_path);
});
void kc_dbhandle_reset(void);
kc_dbhandle_reset();
#else
skip("Keychain not reset", kTestTestCount, false);
#endif
testsync3("secd_70_engine3", test_directive, test_reason);
testsync("secd_70_engine", test_directive, test_reason,
^ (SOSDataSourceRef dataSource) {},
^ (SOSDataSourceRef dataSource) {},
CFSTR("2AF312E092D67308A0083DFFBF2B6B754B967864"),
CFSTR("2AF312E092D67308A0083DFFBF2B6B754B967864"),
CFSTR("2AF312E092D67308A0083DFFBF2B6B754B967864"),
CFSTR("2AF312E092D67308A0083DFFBF2B6B754B967864"),
NULL);
testsync("secd_70_engine-alice1", test_directive, test_reason,
^ (SOSDataSourceRef dataSource) {
__block CFErrorRef error = NULL;
SOSObjectRef object = SOSDataSourceCreateGenericItem(dataSource, CFSTR("test_account"), CFSTR("test service"));
ok(SOSDataSourceWith(dataSource, &error, ^(SOSTransactionRef txn, bool *commit) {
ok(SOSDataSourceMergeObject(dataSource, txn, object, NULL, &error), "dataSource %@ added object %@", SOSEngineGetMyID(dataSource->engine), error);
}), "ds transaction failed %@", error);
CFReleaseSafe(object);
CFReleaseNull(error);
},
^ (SOSDataSourceRef dataSource) {},
CFSTR("DDDB2DCEB7B36F0757F400251ECD11E377A0DCE8"),
CFSTR("B2777CC898AE381B3F375B27E4FD9757F6CE9948"),
CFSTR("CB67BF9ECF00DC7664834DE7A2D7CC1523D25341"),
CFSTR("ADAA3ACE75ED516CB91893413EE9CC9ED04CA47B"),
NULL);
testsync("secd_70_engine-alice1bob1", test_directive, test_reason,
^ (SOSDataSourceRef dataSource) {
#if 0
CFErrorRef error = NULL;
CFDictionaryRef object = SOSDataSourceCreateGenericItem(dataSource, CFSTR("test_account"), CFSTR("test service"));
ok(SOSDataSourceMergeObject(dataSource, object, NULL, &error), "dataSource %@ added object %@", SOSEngineGetMyID(dataSource->engine), error);
CFReleaseSafe(object);
CFReleaseNull(error);
#endif
},
^ (SOSDataSourceRef dataSource) {
__block CFErrorRef error = NULL;
SOSObjectRef object = SOSDataSourceCreateGenericItem(dataSource, CFSTR("test_account"), CFSTR("test service"));
ok(SOSDataSourceWith(dataSource, &error, ^(SOSTransactionRef txn, bool *commit) {
ok(SOSDataSourceMergeObject(dataSource, txn, object, NULL, &error), "dataSource %@ added object %@", SOSEngineGetMyID(dataSource->engine), error);
}), "ds transaction failed %@", error);
CFReleaseSafe(object);
CFReleaseNull(error);
},
CFSTR("2AF312E092D67308A0083DFFBF2B6B754B967864"),
CFSTR("CB67BF9ECF00DC7664834DE7A2D7CC1523D25341"),
CFSTR("ADAA3ACE75ED516CB91893413EE9CC9ED04CA47B"),
NULL);
testsync("secd_70_engine-alice1bob2", test_directive, test_reason,
^ (SOSDataSourceRef dataSource) {
#if 0
CFErrorRef error = NULL;
SOSObjectRef object = SOSDataSourceCreateGenericItem(dataSource, CFSTR("test_account"), CFSTR("test service"));
ok(SOSDataSourceMergeObject(dataSource, object, NULL, &error), "dataSource %@ added object %@", SOSEngineGetMyID(dataSource->engine), error);
CFReleaseSafe(object);
CFReleaseNull(error);
#endif
},
^ (SOSDataSourceRef dataSource) {
__block CFErrorRef error = NULL;
__block SOSObjectRef object = SOSDataSourceCreateGenericItem(dataSource, CFSTR("test_account"), CFSTR("test service"));
ok(SOSDataSourceWith(dataSource, &error, ^(SOSTransactionRef txn, bool *commit) {
ok(SOSDataSourceMergeObject(dataSource, txn, object, NULL, &error), "dataSource %@ added object %@", SOSEngineGetMyID(dataSource->engine), error);
CFReleaseSafe(object);
CFReleaseNull(error);
object = SOSDataSourceCreateGenericItem(dataSource, CFSTR("account1"), CFSTR("service1"));
ok(SOSDataSourceMergeObject(dataSource, txn, object, NULL, &error), "dataSource %@ added object %@", SOSEngineGetMyID(dataSource->engine), error);
CFReleaseSafe(object);
}), "ds transaction failed %@", error);
CFReleaseNull(error);
},
CFSTR("2AF312E092D67308A0083DFFBF2B6B754B967864"),
CFSTR("270EB3953B2E1E295F668CFC27CBB7137991A4BE"),
CFSTR("D1B3944E3084425F41B2C2EA0BE82170E10AA37D"),
NULL);
TODO: {
todo("<rdar://problem/14049022> Test case in sd-70-engine fails due to need for RowID");
testsync("secd_70_engine-update", test_directive, test_reason,
^ (SOSDataSourceRef dataSource) {
__block CFErrorRef error = NULL;
const char *password = "password1";
CFDataRef data = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)password, strlen(password));
SOSObjectRef object_to_find = SOSDataSourceCreateGenericItemWithData(dataSource, CFSTR("test_account"), CFSTR("test service"), true, NULL);
SOSObjectRef object = SOSDataSourceCopyObject(dataSource, object_to_find, &error);
SOSObjectRef old_object = NULL;
SKIP: {
skip("no object", 1, ok(object, "Finding object %@, error: %@", object_to_find, error));
CFReleaseNull(data);
old_object = SOSDataSourceCreateGenericItem(dataSource, CFSTR("test_account"), CFSTR("test service"));
ok(SOSDataSourceWith(dataSource, &error, ^(SOSTransactionRef txn, bool *commit) {
ok(SOSDataSourceMergeObject(dataSource, txn, object, NULL, &error), "dataSource %@ update object %@", SOSEngineGetMyID(dataSource->engine), error);
}), "ds transaction failed %@", error);
}
CFReleaseSafe(data);
CFReleaseSafe(old_object);
CFReleaseSafe(object);
CFReleaseNull(error);
},
^ (SOSDataSourceRef dataSource) {
__block CFErrorRef error = NULL;
__block SOSObjectRef object = SOSDataSourceCreateGenericItem(dataSource, CFSTR("test_account"), CFSTR("test service"));
ok(SOSDataSourceWith(dataSource, &error, ^(SOSTransactionRef txn, bool *commit) {
ok(SOSDataSourceMergeObject(dataSource, txn, object, NULL, &error), "dataSource %@ added object %@", SOSEngineGetMyID(dataSource->engine), error);
CFReleaseSafe(object);
CFReleaseNull(error);
object = SOSDataSourceCreateGenericItem(dataSource, CFSTR("account1"), CFSTR("service1"));
ok(SOSDataSourceMergeObject(dataSource, txn, object, NULL, &error), "dataSource %@ added object %@", SOSEngineGetMyID(dataSource->engine), error);
CFReleaseSafe(object);
}), "ds transaction failed %@", error);
CFReleaseNull(error);
},
CFSTR("2AF312E092D67308A0083DFFBF2B6B754B967864"),
CFSTR("270EB3953B2E1E295F668CFC27CBB7137991A4BE"),
CFSTR("D1B3944E3084425F41B2C2EA0BE82170E10AA37D"),
NULL);
}
testsync("secd_70_engine-foreign-add", test_directive, test_reason,
^ (SOSDataSourceRef dataSource) {
},
^ (SOSDataSourceRef dataSource) {
__block CFErrorRef error = NULL;
const char *password = "password1";
CFDataRef data = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)password, strlen(password));
__block SOSObjectRef object = SOSDataSourceCreateGenericItemWithData(dataSource, CFSTR("test_account"), CFSTR("test service"), false, data);
CFReleaseSafe(data);
ok(SOSDataSourceWith(dataSource, &error, ^(SOSTransactionRef txn, bool *commit) {
ok(SOSDataSourceMergeObject(dataSource, txn, object, NULL, &error), "dataSource %@ added object %@", SOSEngineGetMyID(dataSource->engine), error);
CFReleaseSafe(object);
CFReleaseNull(error);
object = SOSDataSourceCreateGenericItem(dataSource, CFSTR("account1"), CFSTR("service1"));
ok(SOSDataSourceMergeObject(dataSource, txn, object, NULL, &error), "dataSource %@ added object %@", SOSEngineGetMyID(dataSource->engine), error);
CFReleaseSafe(object);
}), "ds transaction failed %@", error);
CFReleaseNull(error);
},
CFSTR("2AF312E092D67308A0083DFFBF2B6B754B967864"),
CFSTR("769F63675CEE9CB968BFD9CA48DB9079BFCAFB6C"),
CFSTR("818C24B9BC495940836B9C8F76517C838CEFFA98"),
NULL);
}
testsync2("secd_70_engine2", test_directive, test_reason,
^ (SOSDataSourceRef dataSource) {},
^ (SOSDataSourceRef dataSource) {},
CFSTR("2AF312E092D67308A0083DFFBF2B6B754B967864"),
CFSTR("2AF312E092D67308A0083DFFBF2B6B754B967864"),
CFSTR("2AF312E092D67308A0083DFFBF2B6B754B967864"),
CFSTR("2AF312E092D67308A0083DFFBF2B6B754B967864"),
CFSTR("2AF312E092D67308A0083DFFBF2B6B754B967864"),
NULL);
TODO: {
todo("testsync fails with 10 peers and staggered adds");
testsyncmany("syncmany", test_directive, test_reason, 9, 10, 0, syncmany_add);
}
testsyncmany("v2syncmany", test_directive, test_reason, 9, 10, 2, syncmany_add);
testsync2p();
}
int secd_70_engine(int argc, char *const *argv)
{
plan_tests(kTestTestCount);
secd_test_setup_temp_keychain("secd_70_engine", NULL);
synctests();
return 0;
}