2 * Copyright (c) 2008,2010,2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <CoreFoundation/CoreFoundation.h>
26 #include <Security/SecBase.h>
27 #include <Security/SecItem.h>
28 #include <Security/SecItemPriv.h>
29 #include <Security/SecInternal.h>
30 #include <utilities/SecCFWrappers.h>
31 #include <utilities/SecFileLocations.h>
37 #include "secd_regressions.h"
39 /* TODO: This test needs to be updated. It was originally created to test upgrades from DB prior to the introduction of versionning, circa 2008.
40 We don't support upgrading from that old of keychain, but this test should be upgraded to test upgrades from v5 to v6 keychain, or more current
43 const char *create_db_sql
=
45 "CREATE TABLE genp(cdat REAL,mdat REAL,desc BLOB,icmt BLOB,crtr INTEGER,type INTEGER,scrp INTEGER,labl BLOB,alis BLOB,invi INTEGER,nega INTEGER,cusi INTEGER,prot BLOB,acct BLOB NOT NULL DEFAULT '',svce BLOB NOT NULL DEFAULT '',gena BLOB,data BLOB,PRIMARY KEY(acct,svce));"
46 "INSERT INTO \"genp\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'4087574952','EnhancedVoicemail',NULL,X'34F32095A0ED6F32637629114439CE38E6FF39ADB591E761D20ED23F9FACF639258DA4F12454FD4D0189C0D39AAA9227');"
47 "INSERT INTO \"genp\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'TlalocNet','AirPort',NULL,X'52E24441994D93D18F344DDF6A7F1F6EC43A63BCEB5F89B02FEBEEAAE108BB4933EAE73A0FB615F693C70BCFBCF034BE74BDF0280ECBEB357EEFA3B7EF03060B');"
48 "INSERT INTO \"genp\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'weasels','AirPort',NULL,X'3FAD49851913382FBC92C9EB90D90D82A74B1DABB5F726648898B2FA2FBA405AA0B9D95D9837BBFF0F9B7C29954973249AA066F9F8AA68D79552970C687A7DA6');"
49 "CREATE TABLE inet(cdat REAL,mdat REAL,desc BLOB,icmt BLOB,crtr INTEGER,type INTEGER,scrp INTEGER,labl BLOB,alis BLOB,invi INTEGER,nega INTEGER,cusi INTEGER,prot BLOB,acct BLOB NOT NULL DEFAULT '',sdmn BLOB NOT NULL DEFAULT '',srvr BLOB NOT NULL DEFAULT '',ptcl INTEGER NOT NULL DEFAULT 0,atyp BLOB NOT NULL DEFAULT '',port INTEGER NOT NULL DEFAULT 0,path BLOB NOT NULL DEFAULT '',data BLOB,PRIMARY KEY(acct,sdmn,srvr,ptcl,atyp,port,path));"
50 "INSERT INTO \"inet\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'mb.7766@gmail.com','','imap.gmail.com','imap','',143,'',X'0029D7AFBF0000E0E386C8654070569B2DF1D7DC2D641AA29223297EC9E8AD86ED91CA6DEE3D2DA0FABD8F05DE5A7AD4CC46B134A211472B6DE50595EACAC149');"
51 "INSERT INTO \"inet\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'brouwer','','phonehome.apple.com','imap','',143,'',X'BB373BAE840427C5E1247540ADA559AB14DF3788906B786498A8E1CFF4B4C596634E4A4C7F9C55EA1B646163AFCDADA8');"
52 "INSERT INTO \"inet\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'mb.7766@gmail.com','','smtp.gmail.com','smtp','',25,'',X'042C08A4AECD3957822F531A602734F07B89DABA3BA6629ECEFE10E264C12635F83EFBB1707C6B39FB20CCE0200D8997B690FBB0B92911BFE9B2D1E05B1CD5F5');"
53 "INSERT INTO \"inet\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'brouwer','','phonehome.apple.com','smtp','',25,'',X'25B0775265ADC808B8AFB2F2602C44B13F5ECC1F04B1D5E6EAE1B803446F3A817CCF8401416FE673CE366E25FACF5C55');"
54 "CREATE TABLE cert(ctyp INTEGER NOT NULL DEFAULT 0,cenc INTEGER,labl BLOB,alis BLOB,subj BLOB,issr BLOB NOT NULL DEFAULT '',slnr BLOB NOT NULL DEFAULT '',skid BLOB,pkhh BLOB,data BLOB,PRIMARY KEY(ctyp,issr,slnr));"
55 "CREATE TABLE keys(kcls INTEGER NOT NULL DEFAULT 0,labl BLOB,alis BLOB,perm INTEGER,priv INTEGER,modi INTEGER,klbl BLOB NOT NULL DEFAULT '',atag BLOB NOT NULL DEFAULT '',crtr INTEGER NOT NULL DEFAULT 0,type INTEGER NOT NULL DEFAULT 0,bsiz INTEGER NOT NULL DEFAULT 0,esiz INTEGER NOT NULL DEFAULT 0,sdat REAL NOT NULL DEFAULT 0,edat REAL NOT NULL DEFAULT 0,sens INTEGER,asen INTEGER,extr INTEGER,next INTEGER,encr INTEGER,decr INTEGER,drve INTEGER,sign INTEGER,vrfy INTEGER,snrc INTEGER,vyrc INTEGER,wrap INTEGER,unwp INTEGER,data BLOB,PRIMARY KEY(kcls,klbl,atag,crtr,type,bsiz,esiz,sdat,edat));"
56 "CREATE INDEX ialis ON cert(alis);"
57 "CREATE INDEX isubj ON cert(subj);"
58 "CREATE INDEX iskid ON cert(skid);"
59 "CREATE INDEX ipkhh ON cert(pkhh);"
60 "CREATE INDEX ikcls ON keys(kcls);"
61 "CREATE INDEX iklbl ON keys(klbl);"
62 "CREATE INDEX iencr ON keys(encr);"
63 "CREATE INDEX idecr ON keys(decr);"
64 "CREATE INDEX idrve ON keys(drve);"
65 "CREATE INDEX isign ON keys(sign);"
66 "CREATE INDEX ivrfy ON keys(vrfy);"
67 "CREATE INDEX iwrap ON keys(wrap);"
68 "CREATE INDEX iunwp ON keys(unwp);"
71 #include "SecdTestKeychainUtilities.h"
73 #include <securityd/SecItemServer.h>
74 #include <utilities/SecCFWrappers.h>
76 /* Test basic add delete update copy matching stuff. */
77 static void tests(void)
79 /* custom keychain dir */
80 secd_test_setup_temp_keychain("secd_30_keychain_upgrade", ^{
81 CFStringRef keychain_path_cf
= __SecKeychainCopyPath();
83 CFStringPerformWithCString(keychain_path_cf
, ^(const char *keychain_path
) {
84 /* Create a new keychain sqlite db */
86 is(sqlite3_open(keychain_path
, &db
), SQLITE_OK
, "create keychain");
87 is(sqlite3_exec(db
, create_db_sql
, NULL
, NULL
, NULL
), SQLITE_OK
,
92 CFReleaseSafe(keychain_path_cf
);
96 CFNumberRef eighty
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &v_eighty
);
97 const char *v_data
= "test";
98 CFDataRef pwdata
= CFDataCreate(NULL
, (UInt8
*)v_data
, strlen(v_data
));
99 CFMutableDictionaryRef query
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
100 CFDictionaryAddValue(query
, kSecClass
, kSecClassInternetPassword
);
101 CFDictionaryAddValue(query
, kSecAttrServer
, CFSTR("members.spamcop.net"));
102 CFDictionaryAddValue(query
, kSecAttrAccount
, CFSTR("smith"));
103 CFDictionaryAddValue(query
, kSecAttrPort
, eighty
);
104 CFDictionaryAddValue(query
, kSecAttrProtocol
, kSecAttrProtocolHTTP
);
105 CFDictionaryAddValue(query
, kSecAttrAuthenticationType
, kSecAttrAuthenticationTypeDefault
);
106 CFDictionaryAddValue(query
, kSecValueData
, pwdata
);
107 ok_status(SecItemAdd(query
, NULL
), "add internet password");
108 is_status(SecItemAdd(query
, NULL
), errSecDuplicateItem
,
109 "add internet password again");
111 ok_status(SecItemCopyMatching(query
, NULL
), "Found the item we added");
113 ok_status(SecItemDelete(query
), "Deleted the item we added");
115 CFReleaseSafe(eighty
);
116 CFReleaseSafe(pwdata
);
117 CFReleaseSafe(query
);
123 int secd_30_keychain_upgrade(int argc
, char *const *argv
)
125 plan_tests(6 + kSecdTestSetupTestCount
);