int test_certchainchecker(int argc, char *argv[]) {

        PKIX_UInt32 actualMinorVersion;
        PKIX_PL_OID *bcOID = NULL;
        PKIX_PL_OID *ncOID = NULL;
        PKIX_PL_OID *cpOID = NULL;
        PKIX_PL_OID *pmOID = NULL;
        PKIX_PL_OID *pcOID = NULL;
        PKIX_PL_OID *iaOID = NULL;
        PKIX_CertChainChecker *dummyChecker = NULL;
        PKIX_List *supportedExtensions = NULL;
        PKIX_PL_Object *initialState = NULL;
        PKIX_UInt32 j = 0;

        PKIX_TEST_STD_VARS();

        startTests("CertChainChecker");

        PKIX_TEST_EXPECT_NO_ERROR(
            PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));

        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create
                (&supportedExtensions, plContext));

        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_OID_Create
                (PKIX_BASICCONSTRAINTS_OID, &bcOID, plContext));
        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
                (supportedExtensions, (PKIX_PL_Object *)bcOID, plContext));

        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_OID_Create
                (PKIX_NAMECONSTRAINTS_OID, &ncOID, plContext));
        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
                (supportedExtensions, (PKIX_PL_Object *)ncOID, plContext));

        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_OID_Create
                (PKIX_CERTIFICATEPOLICIES_OID, &cpOID, plContext));
        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
                (supportedExtensions, (PKIX_PL_Object *)cpOID, plContext));

        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_OID_Create
                (PKIX_POLICYMAPPINGS_OID, &pmOID, plContext));
        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
                (supportedExtensions, (PKIX_PL_Object *)pmOID, plContext));

        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_OID_Create
                (PKIX_POLICYCONSTRAINTS_OID, &pcOID, plContext));
        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
                (supportedExtensions, (PKIX_PL_Object *)pcOID, plContext));

        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_OID_Create
                (PKIX_INHIBITANYPOLICY_OID, &iaOID, plContext));
        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
                (supportedExtensions, (PKIX_PL_Object *)iaOID, plContext));

        PKIX_TEST_DECREF_BC(bcOID);
        PKIX_TEST_DECREF_BC(ncOID);
        PKIX_TEST_DECREF_BC(cpOID);
        PKIX_TEST_DECREF_BC(pmOID);
        PKIX_TEST_DECREF_BC(pcOID);
        PKIX_TEST_DECREF_BC(iaOID);

        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_IncRef
                ((PKIX_PL_Object *)supportedExtensions, plContext));

        initialState = (PKIX_PL_Object *)supportedExtensions;

        subTest("CertChainChecker_Create");
        PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertChainChecker_Create
                (dummyChecker_Check, /* PKIX_CertChainChecker_CheckCallback */
                PKIX_FALSE,          /* forwardCheckingSupported */
                PKIX_FALSE,          /* forwardDirectionExpected */
                supportedExtensions,
                NULL,                /* PKIX_PL_Object *initialState */
                &dummyChecker,
                plContext));

        PKIX_TEST_EXPECT_NO_ERROR
                (PKIX_CertChainChecker_SetCertChainCheckerState
                (dummyChecker, initialState, plContext));

        test_CertChainChecker_Duplicate(dummyChecker);

        subTest("CertChainChecker_Destroy");
        PKIX_TEST_DECREF_BC(dummyChecker);

cleanup:

        PKIX_TEST_DECREF_AC(dummyChecker);
        PKIX_TEST_DECREF_AC(initialState);
        PKIX_TEST_DECREF_AC(supportedExtensions);

        PKIX_Shutdown(plContext);

        PKIX_TEST_RETURN();

        endTests("CertChainChecker");

        return (0);
}
/*
 * FUNCTION: pkix_NameConstraintsChecker_Check
 * (see comments for PKIX_CertChainChecker_CheckCallback in pkix_checker.h)
 */
static PKIX_Error *
pkix_NameConstraintsChecker_Check(
        PKIX_CertChainChecker *checker,
        PKIX_PL_Cert *cert,
        PKIX_List *unresolvedCriticalExtensions,
        void **pNBIOContext,
        void *plContext)
{
        pkix_NameConstraintsCheckerState *state = NULL;
        PKIX_PL_CertNameConstraints *nameConstraints = NULL;
        PKIX_PL_CertNameConstraints *mergedNameConstraints = NULL;
        PKIX_Boolean selfIssued = PKIX_FALSE;
        PKIX_Boolean lastCert = PKIX_FALSE;

        PKIX_ENTER(CERTCHAINCHECKER, "pkix_NameConstraintsChecker_Check");
        PKIX_NULLCHECK_THREE(checker, cert, pNBIOContext);

        *pNBIOContext = NULL; /* we never block on pending I/O */

        PKIX_CHECK(PKIX_CertChainChecker_GetCertChainCheckerState
                    (checker, (PKIX_PL_Object **)&state, plContext),
                    PKIX_CERTCHAINCHECKERGETCERTCHAINCHECKERSTATEFAILED);

        state->certsRemaining--;
        lastCert = state->certsRemaining == 0;

        /* Get status of self issued */
        PKIX_CHECK(pkix_IsCertSelfIssued(cert, &selfIssued, plContext),
                    PKIX_ISCERTSELFISSUEDFAILED);

        /* Check on non self-issued and if so only for last cert */
        if (selfIssued == PKIX_FALSE ||
            (selfIssued == PKIX_TRUE && lastCert)) {
                PKIX_CHECK(PKIX_PL_Cert_CheckNameConstraints
                    (cert, state->nameConstraints, lastCert,
                      plContext),
                    PKIX_CERTCHECKNAMECONSTRAINTSFAILED);
        }

        if (!lastCert) {

            PKIX_CHECK(PKIX_PL_Cert_GetNameConstraints
                    (cert, &nameConstraints, plContext),
                    PKIX_CERTGETNAMECONSTRAINTSFAILED);

            /* Merge with previous name constraints kept in state */

            if (nameConstraints != NULL) {

                if (state->nameConstraints == NULL) {

                        state->nameConstraints = nameConstraints;

                } else {

                        PKIX_CHECK(PKIX_PL_Cert_MergeNameConstraints
                                (nameConstraints,
                                state->nameConstraints,
                                &mergedNameConstraints,
                                plContext),
                                PKIX_CERTMERGENAMECONSTRAINTSFAILED);

                        PKIX_DECREF(nameConstraints);
                        PKIX_DECREF(state->nameConstraints);

                        state->nameConstraints = mergedNameConstraints;
                }

                /* Remove Name Constraints Extension OID from list */
                if (unresolvedCriticalExtensions != NULL) {
                        PKIX_CHECK(pkix_List_Remove
                                    (unresolvedCriticalExtensions,
                                    (PKIX_PL_Object *)state->nameConstraintsOID,
                                    plContext),
                                    PKIX_LISTREMOVEFAILED);
                }
            }
        }

        PKIX_CHECK(PKIX_CertChainChecker_SetCertChainCheckerState
                    (checker, (PKIX_PL_Object *)state, plContext),
                    PKIX_CERTCHAINCHECKERSETCERTCHAINCHECKERSTATEFAILED);

cleanup:

        PKIX_DECREF(state);

        PKIX_RETURN(CERTCHAINCHECKER);
}