Example #1
0
int
main(int argc, char* argv[])
{
  {
    NS_InitXPCOM2(nullptr, nullptr, nullptr);
    nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
    if (!prefs) {
      return -1;
    }
    // When NSS initializes, it attempts to get some localized strings.
    // As a result, OS X and Windows flip out if this isn't set.
    // (This isn't done automatically since this test doesn't have a
    // lot of the other boilerplate components that would otherwise
    // keep the certificate db alive longer than we want it to.)
    nsresult rv = prefs->SetBoolPref("intl.locale.matchOS", true);
    if (NS_FAILED(rv)) {
      return -1;
    }
    nsCOMPtr<nsIX509CertDB> certdb(do_GetService(NS_X509CERTDB_CONTRACTID));
    if (!certdb) {
      return -1;
    }
  } // this scopes the nsCOMPtrs
  // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
  NS_ShutdownXPCOM(nullptr);
  return 0;
}
Example #2
0
int
main(int argc, char* argv[])
{
  {
    NS_InitXPCOM2(nullptr, nullptr, nullptr);
    nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
    if (!prefs) {
      return -1;
    }
    // When NSS initializes, it attempts to get some localized strings.
    // As a result, Android flips out if this isn't set.
    nsresult rv = prefs->SetBoolPref("intl.locale.matchOS", true);
    if (NS_FAILED(rv)) {
      return -1;
    }
    nsCOMPtr<nsIX509CertDB> certdb(do_GetService(NS_X509CERTDB_CONTRACTID));
    if (!certdb) {
      return -1;
    }
  } // this scopes the nsCOMPtrs
  // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
  NS_ShutdownXPCOM(nullptr);
  return 0;
}
Example #3
0
int
main(int argc, char* argv[])
{
  ScopedXPCOM xpcom("TestCertDB");
  if (xpcom.failed()) {
    fail("couldn't initialize XPCOM");
    return 1;
  }
  {
    nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
    if (!prefs) {
      fail("couldn't get nsIPrefBranch");
      return 1;
    }
    // When PSM initializes, it attempts to get some localized strings.
    // As a result, Android flips out if this isn't set.
    nsresult rv = prefs->SetBoolPref("intl.locale.matchOS", true);
    if (NS_FAILED(rv)) {
      fail("couldn't set pref 'intl.locale.matchOS'");
      return 1;
    }
    nsCOMPtr<nsIX509CertDB> certdb(do_GetService(NS_X509CERTDB_CONTRACTID));
    if (!certdb) {
      fail("couldn't get certdb");
      return 1;
    }
    nsCOMPtr<nsIX509CertList> certList;
    rv = certdb->GetCerts(getter_AddRefs(certList));
    if (NS_FAILED(rv)) {
      fail("couldn't get list of certificates");
      return 1;
    }
    nsCOMPtr<nsISimpleEnumerator> enumerator;
    rv = certList->GetEnumerator(getter_AddRefs(enumerator));
    if (NS_FAILED(rv)) {
      fail("couldn't enumerate certificate list");
      return 1;
    }
    bool foundBuiltIn = false;
    bool hasMore = false;
    while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore) {
      nsCOMPtr<nsISupports> supports;
      if (NS_FAILED(enumerator->GetNext(getter_AddRefs(supports)))) {
        fail("couldn't get next certificate");
        return 1;
      }
      nsCOMPtr<nsIX509Cert> cert(do_QueryInterface(supports));
      if (!cert) {
        fail("couldn't QI to nsIX509Cert");
        return 1;
      }
      if (NS_FAILED(cert->GetIsBuiltInRoot(&foundBuiltIn))) {
        fail("GetIsBuiltInRoot failed");
        return 1;
      }
      if (foundBuiltIn) {
        break;
      }
    }
    if (foundBuiltIn) {
      passed("successfully loaded at least one built-in certificate");
    } else {
      fail("didn't load any built-in certificates");
      return 1;
    }
  } // this scopes the nsCOMPtrs
  // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
  return 0;
}