[CRYPT32_WINETEST]
[reactos.git] / rostests / winetests / crypt32 / cert.c
index d3b362c..8239af8 100644 (file)
@@ -18,9 +18,9 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-#include <assert.h>
 #include <stdio.h>
 #include <stdarg.h>
+
 #include <windef.h>
 #include <winbase.h>
 #include <winreg.h>
@@ -487,6 +487,8 @@ static void testCertProperties(void)
     size = sizeof(hashProperty);
     ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
      hashProperty, &size);
+    ok(ret, "CertGetCertificateContextProperty failed: %08x\n",
+     GetLastError());
     ok(!memcmp(hashProperty, hash, sizeof(hash)), "Unexpected hash\n");
     /* Delete the (bogus) hash, and get the real one */
     ret = CertSetCertificateContextProperty(context, CERT_HASH_PROP_ID, 0,
@@ -632,6 +634,51 @@ static void testCertProperties(void)
     CertFreeCertificateContext(context);
 }
 
+static void testCreateCert(void)
+{
+    PCCERT_CONTEXT cert, enumCert;
+    DWORD count, size;
+    BOOL ret;
+
+    SetLastError(0xdeadbeef);
+    cert = CertCreateCertificateContext(0, NULL, 0);
+    ok(!cert && GetLastError() == E_INVALIDARG,
+     "expected E_INVALIDARG, got %08x\n", GetLastError());
+    SetLastError(0xdeadbeef);
+    cert = CertCreateCertificateContext(0, selfSignedCert,
+     sizeof(selfSignedCert));
+    ok(!cert && GetLastError() == E_INVALIDARG,
+     "expected E_INVALIDARG, got %08x\n", GetLastError());
+    SetLastError(0xdeadbeef);
+    cert = CertCreateCertificateContext(X509_ASN_ENCODING, NULL, 0);
+    ok(!cert &&
+     (GetLastError() == CRYPT_E_ASN1_EOD ||
+     broken(GetLastError() == OSS_MORE_INPUT /* NT4 */)),
+     "expected CRYPT_E_ASN1_EOD, got %08x\n", GetLastError());
+
+    cert = CertCreateCertificateContext(X509_ASN_ENCODING,
+     selfSignedCert, sizeof(selfSignedCert));
+    ok(cert != NULL, "creating cert failed: %08x\n", GetLastError());
+    /* Even in-memory certs are expected to have a store associated with them */
+    todo_wine
+    ok(cert->hCertStore != NULL, "expected created cert to have a store\n");
+    /* The cert doesn't have the archived property set (which would imply it
+     * doesn't show up in enumerations.)
+     */
+    size = 0;
+    ret = CertGetCertificateContextProperty(cert, CERT_ARCHIVED_PROP_ID,
+     NULL, &size);
+    ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
+       "expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
+    /* Strangely, enumerating the certs in the store finds none. */
+    enumCert = NULL;
+    count = 0;
+    while ((enumCert = CertEnumCertificatesInStore(cert->hCertStore, enumCert)))
+        count++;
+    ok(!count, "expected 0, got %d\n", count);
+    CertFreeCertificateContext(cert);
+}
+
 static void testDupCert(void)
 {
     HCERTSTORE store;
@@ -1514,11 +1561,13 @@ static void testGetIssuerCert(void)
     /* With only the child certificate, no issuer will be found */
     ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
      chain7_1, sizeof(chain7_1), CERT_STORE_ADD_ALWAYS, &child);
+    ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
     parent = CertGetIssuerCertificateFromStore(store, child, NULL, &flags);
     ok(parent == NULL, "Expected no issuer\n");
     /* Adding an issuer allows one (and only one) issuer to be found */
     ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
      chain10_1, sizeof(chain10_1), CERT_STORE_ADD_ALWAYS, &cert1);
+    ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
     parent = CertGetIssuerCertificateFromStore(store, child, NULL, &flags);
     ok(parent == cert1, "Expected cert1 to be the issuer\n");
     parent = CertGetIssuerCertificateFromStore(store, child, parent, &flags);
@@ -1529,6 +1578,7 @@ static void testGetIssuerCert(void)
      */
     ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
      chain10_0, sizeof(chain10_0), CERT_STORE_ADD_ALWAYS, &cert2);
+    ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
     parent = CertGetIssuerCertificateFromStore(store, child, NULL, &flags);
     ok(parent == cert2, "Expected cert2 to be the first issuer\n");
     parent = CertGetIssuerCertificateFromStore(store, child, parent, &flags);
@@ -1548,11 +1598,13 @@ static void testGetIssuerCert(void)
     /* With only the child certificate, no issuer will be found */
     ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
      chain7_1, sizeof(chain7_1), CERT_STORE_ADD_ALWAYS, &child);
+    ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
     parent = CertGetIssuerCertificateFromStore(store, child, NULL, &flags);
     ok(parent == NULL, "Expected no issuer\n");
     /* Adding an issuer allows one (and only one) issuer to be found */
     ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
      chain10_0, sizeof(chain10_0), CERT_STORE_ADD_ALWAYS, &cert1);
+    ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
     parent = CertGetIssuerCertificateFromStore(store, child, NULL, &flags);
     ok(parent == cert1, "Expected cert1 to be the issuer\n");
     parent = CertGetIssuerCertificateFromStore(store, child, parent, &flags);
@@ -1563,6 +1615,7 @@ static void testGetIssuerCert(void)
      */
     ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
      chain10_1, sizeof(chain10_1), CERT_STORE_ADD_ALWAYS, &cert2);
+    ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
     parent = CertGetIssuerCertificateFromStore(store, child, NULL, &flags);
     ok(parent == cert2, "Expected cert2 to be the first issuer\n");
     parent = CertGetIssuerCertificateFromStore(store, child, parent, &flags);
@@ -1826,6 +1879,7 @@ static void testCertSigs(void)
     CryptReleaseContext(csp, 0);
     ret = pCryptAcquireContextA(&csp, cspNameA, MS_DEF_PROV_A, PROV_RSA_FULL,
      CRYPT_DELETEKEYSET);
+    ok(ret, "CryptAcquireContext failed: %08x\n", GetLastError());
 }
 
 static const BYTE md5SignedEmptyCert[] = {
@@ -2014,6 +2068,8 @@ static void testCreateSelfSignCert(void)
     CryptReleaseContext(csp, 0);
     ret = pCryptAcquireContextA(&csp, cspNameA, MS_DEF_PROV_A, PROV_RSA_FULL,
      CRYPT_DELETEKEYSET);
+    ok(ret, "CryptAcquireContext failed: %08x\n", GetLastError());
+
 
     /* do the same test with AT_KEYEXCHANGE  and key info*/
     memset(&info,0,sizeof(info));
@@ -2082,7 +2138,7 @@ static void testIntendedKeyUsage(void)
     if (0)
     {
         /* Crash */
-        ret = CertGetIntendedKeyUsage(0, NULL, NULL, 0);
+        CertGetIntendedKeyUsage(0, NULL, NULL, 0);
     }
     ret = CertGetIntendedKeyUsage(0, &info, NULL, 0);
     ok(!ret, "expected failure\n");
@@ -2092,6 +2148,7 @@ static void testIntendedKeyUsage(void)
     ok(!ret, "expected failure\n");
     ret = CertGetIntendedKeyUsage(X509_ASN_ENCODING, &info, usage_bytes,
      sizeof(usage_bytes));
+    ok(!ret, "expected failure\n");
     info.cExtension = 1;
     info.rgExtension = &ext;
     ret = CertGetIntendedKeyUsage(X509_ASN_ENCODING, &info, NULL, 0);
@@ -2250,6 +2307,8 @@ static void testKeyUsage(void)
         SetLastError(0xbaadcafe);
         size = sizeof(buf);
         ret = CertGetEnhancedKeyUsage(context, 0, pUsage, &size);
+        ok(ret || broken(!ret && GetLastError() == CRYPT_E_NOT_FOUND /* NT4 */),
+        "CertGetEnhancedKeyUsage failed: %08x\n", GetLastError());
         ok(GetLastError() == CRYPT_E_NOT_FOUND,
          "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
 
@@ -2655,9 +2714,8 @@ static void testIsRDNAttrsInCertificateName(void)
     if (0)
     {
         /* Crash */
-        ret = CertIsRDNAttrsInCertificateName(0, 0, NULL, NULL);
-        ret = CertIsRDNAttrsInCertificateName(X509_ASN_ENCODING, 0, &name,
-         NULL);
+        CertIsRDNAttrsInCertificateName(0, 0, NULL, NULL);
+        CertIsRDNAttrsInCertificateName(X509_ASN_ENCODING, 0, &name, NULL);
     }
     SetLastError(0xdeadbeef);
     ret = CertIsRDNAttrsInCertificateName(0, 0, &name, NULL);
@@ -2931,7 +2989,7 @@ static void testHashToBeSigned(void)
     /* Crash */
     if (0)
     {
-        ret = CryptHashToBeSigned(0, 0, NULL, 0, NULL, NULL);
+        CryptHashToBeSigned(0, 0, NULL, 0, NULL, NULL);
     }
     SetLastError(0xdeadbeef);
     ret = CryptHashToBeSigned(0, 0, NULL, 0, NULL, &size);
@@ -2972,6 +3030,9 @@ static void testHashToBeSigned(void)
 
     ret = CryptHashToBeSigned(0, X509_ASN_ENCODING, md5SignedEmptyCert,
      sizeof(md5SignedEmptyCert), hash, &size);
+    ok(ret || broken(!ret && GetLastError() == NTE_BAD_ALGID) /* NT4 */,
+     "CryptHashToBeSigned failed: %08x\n", GetLastError());
+
     ok(!memcmp(hash, md5SignedEmptyCertHash, size), "unexpected value\n");
 }
 
@@ -2980,9 +3041,9 @@ static void testCompareCert(void)
     CERT_INFO info1 = { 0 }, info2 = { 0 };
     BOOL ret;
 
-    /* Crashes
-    ret = CertCompareCertificate(X509_ASN_ENCODING, NULL, NULL);
-     */
+    /* Crashes */
+    if (0)
+        CertCompareCertificate(X509_ASN_ENCODING, NULL, NULL);
 
     /* Certs with the same issuer and serial number are equal, even if they
      * differ in other respects (like subject).
@@ -3618,6 +3679,7 @@ START_TEST(cert)
 
     testAddCert();
     testCertProperties();
+    testCreateCert();
     testDupCert();
     testFindCert();
     testGetSubjectCert();