tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

cert_unittest.cc (1743B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "gtest/gtest.h"
      8 
      9 #include "nss.h"
     10 #include "secerr.h"
     11 #include "pk11pub.h"
     12 #include "nss_scoped_ptrs.h"
     13 
     14 namespace nss_test {
     15 
     16 class CertTest : public ::testing::Test {};
     17 
     18 // Tests CERT_GetCertificateDer for the certs we have.
     19 TEST_F(CertTest, GetCertDer) {
     20  // Listing all the certs should get us the default trust anchors.
     21  ScopedCERTCertList certs(PK11_ListCerts(PK11CertListAll, nullptr));
     22  ASSERT_FALSE(PR_CLIST_IS_EMPTY(&certs->list));
     23 
     24  for (PRCList* cursor = PR_NEXT_LINK(&certs->list); cursor != &certs->list;
     25       cursor = PR_NEXT_LINK(cursor)) {
     26    CERTCertListNode* node = (CERTCertListNode*)cursor;
     27    SECItem der;
     28    ASSERT_EQ(SECSuccess, CERT_GetCertificateDer(node->cert, &der));
     29    ASSERT_EQ(0, SECITEM_CompareItem(&der, &node->cert->derCert));
     30  }
     31 }
     32 
     33 TEST_F(CertTest, GetCertDerBad) {
     34  EXPECT_EQ(SECFailure, CERT_GetCertificateDer(nullptr, nullptr));
     35  EXPECT_EQ(SEC_ERROR_INVALID_ARGS, PORT_GetError());
     36 
     37  ScopedCERTCertList certs(PK11_ListCerts(PK11CertListAll, nullptr));
     38  ASSERT_FALSE(PR_CLIST_IS_EMPTY(&certs->list));
     39  CERTCertListNode* node = (CERTCertListNode*)PR_NEXT_LINK(&certs->list);
     40  EXPECT_EQ(SECFailure, CERT_GetCertificateDer(node->cert, nullptr));
     41  EXPECT_EQ(SEC_ERROR_INVALID_ARGS, PORT_GetError());
     42 
     43  SECItem der;
     44  EXPECT_EQ(SECFailure, CERT_GetCertificateDer(nullptr, &der));
     45  EXPECT_EQ(SEC_ERROR_INVALID_ARGS, PORT_GetError());
     46 }
     47 }  // namespace nss_test