text:
"""
Bayesian Degree Corrected Stochastic Block Model
roughly based on Infinite-degree-corrected stochastic block model by Herlau et. al.,
but with a fixed number of cluster sizes and a different update equation for the collapsed Gibbs sampler;
see accompanying documentation
"""

import numpy as np
import sys

sys.path.append("/home/victor/Documents/community_detection/MCMC")
from cgs_llhds import diri_multi_llhd
from multi_sbm_helpers import comp_edge_cts, softmax
from dcsbm_helpers import GD, BD, samp_shape_post_step, samp_rate_post_step, samp_gam_post_step

class gen_data:
    def __init__(self, n, phis, eta):
        """
        :param n: number of vertices in each community
        :param phis: list of probability distributions, phis[l] should be length n[l] and sum to 1
        :param eta: symmetric matrix, eta[k,l] is expected number of edges between vertex in k and vertex in l
        """

        self.n = n
        self.n_vert = sum(n)
        self.n_comm = len(n)
        self.phis = phis
        self.eta = eta

        z = np.repeat(0, self.n_vert)
        acc = 0
        for l in range(self.n_comm - 1):
            acc += self.n[l]
            z[acc: acc + self.n[l + 1]] = l + 1

        self.z = z

        phi = np.repeat(0., self.n_vert)
        phi[0:self.n[0]] = phis[0]
        acc = 0
        for l in range(self.n_comm - 1):
            acc += self.n[l]
            phi[acc: acc + self.n[l + 1]] = phis[l + 1]

        self.phi = phi

        self.A = self.sampleA()

    def sampleA(self):
        """
        Sample an adjacency matrix conditional on all other parameters
        :return ndarray, float. Sampled adjacency matrix
        """
        A = np.zeros([self.n_vert, self.n_vert])

        for i in range(self.n_vert):
            for j in range(i + 1, self.n_vert):
                thetai = self.n[self.z[i]] * self.phi[i]
                thetaj = self.n[self.z[j]] * self.phi[j]
                A[i, j] = np.random.poisson(thetai * thetaj * self.eta[self.z[i], self.z[j]])
                A[j, i] = A[i, j]

        return A

class gen_data_hypers(gen_data):
    "Sample a graph from the Bayesian DCSBM"

    def __init__(self, n_vert, n_comm, alpha, kap, lam, gam):
        """
        :param n_vert: number of vertices in the graph
        :param n_comm: number of communities
        :param alpha: dirichlet prior parameter for community memberships
        :param kap: scalar, gamma dist param
        :param lam: scalar, gamma dist param
        :param gam: scalar, param for deg correction dirichlet dist. Basic block model recovered in gamma->inf limit
        """

        self.n_vert = n_vert
        self.n_comm = n_comm
        self.alpha = alpha
        self.kap = kap
        self.lam = lam
        self.gam = gam

        self.n = np.random.multinomial(n_vert, np.random.dirichlet(np.repeat(alpha, n_comm)))

        z = np.repeat(0,n_vert)
        acc = 0
        for l in range(n_comm-1):
            acc += self.n[l]
            z[acc : acc + self.n[l + 1]] = l+1

        self.z=z

        phi_ls = [np.random.dirichlet(np.repeat(gam, nl)) for nl in self.n]
        phi = np.repeat(0.,self.n_vert)
        phi[0:self.n[0]] = phi_ls[0]
        acc = 0
        for l in range(n_comm-1):
            acc += self.n[l]
            phi[acc : acc + self.n[l + 1]] = phi_ls[l+1]

        self.phis = phi_ls
        self.phi = phi

        eta = np.random.gamma(kap,1./lam,[n_comm,n_comm])
        for k in range(n_comm):
            for l in range(k+1,n_comm):
                eta[k,l] = eta[l,k]
        self.eta = eta

        self.A = self.sampleA()

class cgs:
    """
    Collapsed Gibbs sampler for the Bayesian DCSBM
    """

    def __init__(self, A, z, n_comm, alpha, kap, lam, gam):
        """
        :param A: adjacency matrix of (multi) graph
        :param z: community identities of vertices
        :param n_comm: number of communities (in case some communities are empty at init)
        :param alpha: dirichlet prior parameter for community memberships
        :param kap: scalar, gamma dist param
        :param lam: scalar, gamma dist param
        :param gam: scalar, param for deg correction dirichlet dist. Basic block model recovered in gamma->inf limit
        """

        self.A = A
        self.z = z
        self.n_comm = n_comm
        self.alpha = 1.*alpha
        self.kap = 1.*kap
        self.lam = 1.*lam
        self.gam = 1.*gam

        '''
        compute initial values of sufficient stats
        '''
        # comm_idxs[k] is list of indices of vertices in community k
        self.comm_idxs = []
        for k in range(n_comm):
            self.comm_idxs.append([i for i, zi in enumerate(z) if zi == k])  # vertices in community k

        # n[k] is number of vertices in community k
        self.n = np.array([members.__len__() for members in self.comm_idxs])

        # edge_cts[k,l] is number of edges between community k and community l, not counting self edges
        # warning: not counting self edges!
        self.edge_cts = comp_edge_cts(self.A, self.comm_idxs)

        self.n_vert = A.shape[0]

        # initial assignment for (latent) self-edges of the graph - shouldn't matter too much
        self.diags = np.repeat(0,self.n_vert)

        self.degs = np.sum(A,axis=1) # degree of each vertex

        # [vectors of the form [1 0 0 0], [0 1 0 0], etc., used to call diri-multi-llhd
        self._comm_indicators = [np.identity(n_comm, int)[j,:] for j in range(n_comm)]

    def sample_diags(self):
        """
        Sample the self edges of the graph, which are treated as latent variables.
        Scheme for doing this is to sample eta, phi | z,A and then A_ii | eta,phi,z
        The relevant posterior distributions are derived in the Bayesian DCSBM paper
        """

        eta_diags = np.zeros(self.n_vert ) # eta_diags[v] = eta_z[v]z[v]
        theta = np.zeros(self.n_vert ) # theta_l = n_l * phi_l

        for l in range(self.n_comm):
            #eta
            kap_post = self.kap + self.edge_cts[l,l] + np.sum(self.diags[self.comm_idxs[l]])
            # kap_post = self.kap + self.edge_cts[l,l]
            lam_post = self.lam + self.n[l]**2 /2.

            # numpy uses a different convention for the parameterization of the gamma distribution
            eta_diags[self.comm_idxs[l]] = np.random.gamma(kap_post, 1./lam_post)

            # phi
            gam_post = self.gam + np.sum(self.A[self.comm_idxs[l],:],axis=1) + 2*self.diags[self.comm_idxs[l]]
            theta[self.comm_idxs[l]] = self.n[l] * np.random.dirichlet(gam_post)

        self.diags = np.random.poisson(0.5 * theta**2 * eta_diags)

    """
    Functions to update zs
    """

    def remove_vertex(self,v):
        """
        removes vertex v from the sufficient stats
        :param v: integer, index of vertex to be removed
        """
        edges_to_comm = [np.sum(self.A[v, comm]) for comm in self.comm_idxs] # vv: double checked this, it's fine
        self.comm_idxs[self.z[v]].remove(v)

        self.n[self.z[v]] -= 1

        self.edge_cts[self.z[v], :] -= edges_to_comm
        self.edge_cts[:, self.z[v]] = self.edge_cts[self.z[v], :]

        # set label to invalid value. Should have no effect (except to throw error if invoked before being reassinged)
        self.z[v] = self.n_vert+1

    def add_vertex(self,v,k):
        """
        adds vertex v to community k, updating the sufficient stats of the SBM
        :param v: integer, index of vertex to be added
        :param k: integer, index of community to add vertex to
        """

        # warning! this doesn't include self edges
        edges_to_comm = [np.sum(self.A[v, comm]) for comm in self.comm_idxs]

        self.z[v] = k

        self.comm_idxs[self.z[v]].append(v)
        self.n[k] += 1

        self.edge_cts[self.z[v], :] += edges_to_comm
        self.edge_cts[:, self.z[v]] = self.edge_cts[self.z[v], :]

    def comm_llhd(self, v):
        """
        computes a length n vector q such that q(k)-q(l) = log(P(A|z_-v,z_v =k)) - log(P(A|z_-v,z_v = l))
        See associated documentation for derivation of these equations
        :param v: id of vertex with community identities to be computed
        :return: length n vector q such that q(k)-q(l) = log(P(A|z_-v,z_v =k)) - log(P(A|z_-v,z_v = l))
        """

        # dv_cts[m] is number of edges from v to community m, ignoring the self edges of (the communityless) v
        dv_cts = np.array([np.sum(self.A[v,comm]) for comm in self.comm_idxs])

        tot_trials = 1.*np.outer(self.n, self.n) # number of pairs of vertices between comm k and l
        tot_trials[np.diag_indices_from(tot_trials)] = self.n ** 2 / 2.  # possible connections to comm [l,l] is smaller

        log_comm_prob = np.zeros(self.n_comm)
        for k in range(self.n_comm):
            # SBM component
            edge_add = np.zeros(self.n_comm)
            tot_ct_add = np.zeros(self.n_comm)

            edge_add += dv_cts
            edge_add[k] += self.diags[v]

            tot_ct_add += self.n
            tot_ct_add[k] += 0.5

            log_comm_prob[k] += np.sum(GD(self.edge_cts[k,:]+self.kap, tot_trials[k,:]+self.lam,edge_add,tot_ct_add))

            #degree correction
            if self.n[k]!=0:
                deg_term = np.repeat(self.gam, self.n[k])+self.degs[self.comm_idxs[k]]
                log_comm_prob[k] += BD(deg_term, self.degs[v]+self.diags[v]+self.gam) \
                                    - BD(np.repeat(self.gam,self.n[k]),self.gam)
                log_comm_prob[k] += (np.sum(self.edge_cts[k,:])+self.edge_cts[k,k]+dv_cts[k])*np.log(1.+1./self.n[k]) \
                                    + (self.degs[v]+self.diags[v])*np.log(self.n[k]+1)

        return log_comm_prob

    def log_comm_prior(self):
        """
        :return: log dirichlent multinomial values (prior for z), for pass to softmax
        """
        return np.array([diri_multi_llhd(obs=comm_indic, alphas=self.alpha+self.n) for comm_indic in self._comm_indicators])

    def update_z(self, v):
        """
        Runs a single step of the collapsed gibbs sampler to resample the community identity of v
        :param v: integer, a vertex in the graph
        """

        # sample new self edges of adjacency matrix
        # it's really irritating that this is required
        self.sample_diags()

        # add in the contribution from the self edges to the sufficient stats
        # self_edges[l] is number of self edges in community l
        self_edges = np.array([np.sum(self.diags[comm_idx]) for comm_idx in self.comm_idxs])
        self.edge_cts[np.diag_indices_from(self.edge_cts)] += self_edges
        self.degs += self.diags

        '''
        remove current vertex from sufficient stats
        '''
        self.remove_vertex(v)

        '''
        sample the new index conditional on all other community assignments
        using P(z_v = k | A, z_-v) \propto P( A | z_v = k, z_-v) * P(z_v = k | z_-v)
        '''

        # P( A | z_v = k, z_-v) contribution
        log_comm_prob = self.comm_llhd(v)

        # P(z_v = k | z_-v) contribution
        log_comm_prob += self.log_comm_prior()

        # exponentiate and sample the new label
        comm_prob = softmax(log_comm_prob)
        new_comm = np.random.multinomial(1, comm_prob).nonzero()[0][0]

        '''
        add the vertex back into the sufficient stats
        '''
        self.add_vertex(v,new_comm)

        # clean up the self edges
        self.edge_cts[np.diag_indices_from(self.edge_cts)] -= self_edges
        self.degs -= self.diags

    def update_zs(self):
        """
        Update the community indicators in all of the models
        """
        vertex_order = range(self.n_vert)
        np.random.shuffle(vertex_order)
        for v in vertex_order:
            self.update_z(v)

    """
    Functions to update higher level parameters (kap,lam,gam)
    """

    def update_NB_params(self):
        kap = np.copy(self.kap)
        lam = np.copy(self.lam)

        tot_trials = 1. * np.outer(self.n, self.n)  # number of pairs of vertices between comm k and l
        tot_trials[np.diag_indices_from(tot_trials)] = self.n ** 2 / 2.  # possible connections to comm [l,l] is smaller

        # empty communities carry no information (but do cause divide by 0 errors -_-)
        ttm = np.ma.masked_values(tot_trials,0)
        em = np.ma.masked_array(self.edge_cts, ttm.mask)

        # count each community pair only once
        unique_pairs = np.triu_indices(self.n_comm)

        # update kappa
        # key observation is that e_lm ~ NB(kap, 1/(1+lam/n_lm))
        # so we can use augmented conjugate update of Zhou&Carin 2012
        ps = ttm[unique_pairs] / (ttm[unique_pairs] + lam)
        kap = samp_shape_post_step(em[unique_pairs].compressed(), kap, ps.compressed(), 0.1, 0.1)

        # update lambda
        # simple independent MH sampler
        lam = samp_rate_post_step(em[unique_pairs].compressed(), ttm[unique_pairs].compressed(), kap, lam)
        self.kap = kap
        self.lam = lam

    def update_gam(self):
        # metropolis-hasting update of gamma
        gam = np.copy(self.gam)

        terms = self.degs + 2*self.diags
        gam = samp_gam_post_step(terms, self.comm_idxs, gam)

        self.gam = gam


text:
package org.kaaproject.kaa.server.common.nosql.mongo.dao.model;

import java.util.Random;

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;

import org.junit.Assert;
import org.junit.Test;
import org.kaaproject.kaa.common.dto.EndpointGroupStateDto;

public class EndpointGroupStateTest {

    public static final Random RANDOM = new Random();
    public static final int LIMIT = 1000000;

    @Test
    public void EndpointStateTest(){
        EndpointGroupStateDto state = new EndpointGroupStateDto();

        String groupId = String.valueOf(RANDOM.nextInt(LIMIT));
        String filterId = String.valueOf(RANDOM.nextInt(LIMIT));
        String configId = String.valueOf(RANDOM.nextInt(LIMIT));
        state.setConfigurationId(configId);
        state.setProfileFilterId(filterId);
        state.setEndpointGroupId(groupId);

        EndpointGroupState stateOne = new EndpointGroupState(state);
        EndpointGroupState stateTwo = new EndpointGroupState();
        stateTwo.setConfigurationId(configId);
        stateTwo.setProfileFilterId(filterId);
        stateTwo.setEndpointGroupId(groupId);
        Assert.assertEquals(stateOne, stateTwo);
        Assert.assertEquals(stateOne.hashCode(), stateTwo.hashCode());
        Assert.assertEquals(stateOne.toDto(), stateTwo.toDto());
        Assert.assertEquals(stateOne.toString(), stateTwo.toString());
    }

    @Test
    public void hashCodeEqualsTest(){
        EqualsVerifier.forClass(EndpointGroupState.class).suppress(Warning.NONFINAL_FIELDS).withRedefinedSuperclass().verify();
    }

}

redpajama_set_name: RedPajamaGithub


text:
/**
 *
 */
$(function() {

   interactionCategoryDefaults(InteractionCategories)

   $('.help_query').each(function() { // Notice the .each() loop, discussed below
        $(this).qtip({
            content: {
                text: $(this).next('div').html()
            },
    		position: {
    			my: 'top center',
    			at: 'bottom center'
    		},
    		style: {
    			classes: 'qtip-bootstrap',
    			tip: {
    				width: 10,
    				height: 8
    			}
    		},
            show: 'click',
            hide: 'unfocus'
        });
    });

    $( "#min_interaction_score" ).slider({

		step: 0.01,
        min: 0,
        max: 1,
        value: 0,
        change:function( event, ui ) {
          $("#search_min_interaction_score").val(ui.value);
        },
        slide: function( event, ui ) {
          $("#min_interaction_score_handle").html(ui.value);
        }
      });

    $("#min_interaction_score_handle").html($( "#min_interaction_score" ).slider( "value" ));
    $("#min_interaction_score").draggable();

    var tissueArray = ['adipose_subcutaneous', 'adipose_visceral_omentum', 'adrenal_gland', 'artery_aorta', 'artery_coronary', 'artery_tibial', 'brain_0', 'brain_1', 'brain_2', 'breast_mammary_tissue', 'colon_sigmoid', 'colon_transverse', 'esophagus_gastroesophageal_junction', 'esophagus_mucosa', 'esophagus_muscularis', 'heart_atrial_appendage', 'heart_left_ventricle', 'kidney_cortex', 'liver', 'lung', 'minor_salivary_gland', 'muscle_skeletal', 'nerve_tibial', 'ovary', 'pancreas', 'pituitary', 'prostate', 'skin', 'small_intestine_terminal_ileum', 'spleen', 'stomach', 'testis', 'thyroid', 'uterus', 'vagina', 'whole_blood'];

    tissueArray.forEach(function(tissue){

        $( "#" + tissue ).slider({

    		step: 0.01,
            min: 0,
            max: 22,
            value: 0,
            change:function( event, ui ) {
              $("#search_" + tissue).val(ui.value);
            },
            slide: function( event, ui ) {
              $("#" + tissue + "_handle").html(ui.value);
            }
          });

        $("#" + tissue + "_handle").html($( "#" + tissue ).slider( "value" ));
        $("#" + tissue).draggable();

    });

    var seperator = '';
    var invalidTerms = [];

	$("#search_identifier").autocomplete({
        	source: function(request, response){

        		var commaSpaceCount = (request.term.match(/,\s/g) || []).length;
				var commaCount = (request.term.match(/,/g) || []).length;
				var semicolonCount = (request.term.match(/;/g) || []).length;
				var newLineCount = (request.term.match(/\n/g) || []).length;
				var tabCount = (request.term.match(/\t/g) || []).length;
				var spaceCount = (request.term.match(/\s/g) || []).length;

				var seperatorCountArray = [commaSpaceCount, commaCount, semicolonCount, newLineCount, tabCount, spaceCount];

				var maxValue = Math.max.apply(this, seperatorCountArray);

				// Get the index of the max value, through the built in function inArray
				var index = $.inArray(maxValue,seperatorCountArray);

				var seperatorArray = [', ', ',', ';', "\n", "\t", " "];

				seperator = seperatorArray[index];

				request.term = request.term.replace(/\n/g, ",");
				request.term = request.term.replace(/\t/g, ",");
				request.term = request.term.replace(/\s/g, ",");
				request.term = request.term.replace(/%20/g, ",");
				request.term = request.term.replace(/ /g, ",");

				urlAutocomplete = Url + 'app.php/autocomplete/' + request.term;

				$.getJSON(urlAutocomplete, function(data){
					response(data.autocomplete);
				});

		},
		select: function(event,ui) {
		    this.value=ui.item.value;
		    $(this).trigger('change');
		    return false;
		},
		minLength: 2,

	}).autocomplete( "instance" )._renderItem = function( ul, item) {

		item.value = item.value.replace(/,/g, seperator);

	  return $( "
" )
	    .attr( "data-value", item.value)
	    .append( item.label )
	    .appendTo( ul );
	};

	$( "#search_identifier" ).on("change keyup paste", validateSearchTerms);

	$( "#remove_invalid_terms" ).on("click", function(){

		var invalidTerms = $('#invalid_terms_array').attr('data');

		var searchIdentifierValue = $("#search_identifier").val();

		var invalidTermsArray = invalidTerms.split(",");

		var commaSpaceCount = (searchIdentifierValue.match(/,\s/g) || []).length;
		var commaCount = (searchIdentifierValue.match(/,/g) || []).length;
		var semicolonCount = (searchIdentifierValue.match(/;/g) || []).length;
		var newLineCount = (searchIdentifierValue.match(/\n/g) || []).length;
		var tabCount = (searchIdentifierValue.match(/\t/g) || []).length;
		var spaceCount = (searchIdentifierValue.match(/\s/g) || []).length;

		var seperatorCountArray = [commaSpaceCount, commaCount, semicolonCount, newLineCount, tabCount, spaceCount];

		var maxValue = Math.max.apply(this, seperatorCountArray);

		var index = $.inArray(maxValue,seperatorCountArray);

		var seperatorArray = [', ', ',', ';', "\n", "\t", " "];

		var seperator = seperatorArray[index];

		searchIdentifierValue = searchIdentifierValue.replace(/\n/g, ",");
		searchIdentifierValue = searchIdentifierValue.replace(/\t/g, ",");
		searchIdentifierValue = searchIdentifierValue.replace(/\s/g, ",");
		searchIdentifierValue = searchIdentifierValue.replace(/%20/g, ",");
		searchIdentifierValue = searchIdentifierValue.replace(/ /g, ",");

		$.each(invalidTermsArray, function( index, value ){

			if(value != ''){
			var regex = new RegExp(value,"g");

			searchIdentifierValue = searchIdentifierValue.replace(regex, "");
			}
		});

		var searchIdentifierValueArray = searchIdentifierValue.split(",");

		searchIdentifierValueArray = cleanArray(searchIdentifierValueArray);

		searchIdentifierValue = searchIdentifierValueArray.join(seperator);

		$("#search_identifier").val(searchIdentifierValue);

		validateSearchTerms();

	});

    $( "#example_1" ).on("click", function(){
    	$( "#search_query_query" ).prop('checked', false);
    	$( "#search_text_output" ).prop('checked', false);
    	$( "#search_published" ).prop('checked', true);
       	$( "#search_validated" ).prop('checked', true);
		$( "#search_verified" ).prop('checked', true);
		$( "#search_literature" ).prop('checked', true);
		$("#advanced_search").collapse("hide");
    });

    $( "#example_2" ).on("click", function(){
    	$( "#search_query_query" ).prop('checked', true);
    	$( "#search_text_output" ).prop('checked', false);
    	$( "#search_published" ).prop('checked', true);
       	$( "#search_validated" ).prop('checked', true);
		$( "#search_verified" ).prop('checked', true);
		$( "#search_literature" ).prop('checked', true);
		$("#advanced_search").collapse("show");

    });

    $( "#example_3" ).on("click", function(){
    	$( "#search_query_query" ).prop('checked', false);
    	$( "#search_text_output" ).prop('checked', true);
    	$( "#search_published" ).prop('checked', true);
       	$( "#search_validated" ).prop('checked', true);
		$( "#search_verified" ).prop('checked', true);
		$( "#search_literature" ).prop('checked', true);
		$("#advanced_search").collapse("show");

    });

    $("#advanced_search").on("hide.bs.collapse", function(){
	    $("#advanced").html('Advanced');

	});
	  $("#advanced_search").on("show.bs.collapse", function(){
	    $("#advanced").html('Basic');

	  });

});

function interactionCategoryDefaults(InteractionCategories){

	$.each(InteractionCategories, function (index, interaction_category) {
		var selected_by_default = interaction_category[2];
		if(selected_by_default == 0){
			var id = 'search_' + interaction_category[1]
			$('#' + id).prop('checked', false);
		}
	});
}

function cleanArray(actual) {
	  var newArray = new Array();
	  for (var i = 0; i < actual.length; i++) {
	    if (actual[i]) {
	      newArray.push(actual[i]);
	    }
	  }
	  return newArray;
	}

function getSeperator(searchIdentifierValue) {

	var commaSpaceCount = (searchIdentifierValue.match(/,\s/g) || []).length;
	var commaCount = (searchIdentifierValue.match(/,/g) || []).length;
	var semicolonCount = (searchIdentifierValue.match(/;/g) || []).length;
	var newLineCount = (searchIdentifierValue.match(/\n/g) || []).length;
	var tabCount = (searchIdentifierValue.match(/\t/g) || []).length;
	var spaceCount = (searchIdentifierValue.match(/\s/g) || []).length;

	var seperatorCountArray = [commaSpaceCount, commaCount, semicolonCount, newLineCount, tabCount, spaceCount];

	var maxValue = Math.max.apply(this, seperatorCountArray);

	var index = $.inArray(maxValue,seperatorCountArray);

	var seperatorArray = [', ', ',', ';', "\n", "\t", " "];

	var seperator = seperatorArray[index];

	return seperator;
}

function validateSearchTerms() {

	var searchIdentifierValue = "/" + $("#search_identifier").val();

	searchIdentifierValue = searchIdentifierValue.replace(/\n/g, ",");
	searchIdentifierValue = searchIdentifierValue.replace(/\t/g, ",");
	searchIdentifierValue = searchIdentifierValue.replace(/\s/g, ",");
	searchIdentifierValue = searchIdentifierValue.replace(/%20/g, ",");
	searchIdentifierValue = searchIdentifierValue.replace(/ /g, ",");

	var urlTermValidation = '';

	if(searchIdentifierValue == '/'){

		urlTermValidation = Url + 'app.php/term_validation';
	}else{

		urlTermValidation = Url + 'app.php/term_validation' + searchIdentifierValue;
	}

	var termCountArray = searchIdentifierValue.split(",");
	var termCount = termCountArray.length;

	$.getJSON(urlTermValidation, function(data){

		var invalidTermsString = '';
		var invalidTerms = '';
		$.each(data, function (index, value) {

			invalidTermsString  = invalidTermsString + ',' + value;

			invalidTerms = invalidTerms + '
' + value  + '
';
		});

		var invalidTermCount = data.length;
		var validTermCount = termCount - invalidTermCount;

		invalidTermsString = invalidTermsString.replace(/^,/, "");
		if(invalidTerms){

			$("#all_terms_valid").addClass('hidden');
			$(".search_terms_not_found").removeClass('hidden');
			$("#invalid_terms_array").removeClass('hidden');
			$("#remove_invalid_terms").removeClass('hidden');
			$("#invalid_terms_array").attr('data', invalidTermsString);
			$("#invalid_terms_array").html(invalidTerms);
			$(".number_present_terms").html(validTermCount);
			$(".number_not_present_terms").html(invalidTermCount);

		}else{

			if($("#search_identifier").val() != ''){

				$("#all_terms_valid").removeClass('hidden');
			}

			$(".search_terms_not_found").addClass('hidden');
			$("#invalid_terms_array").addClass('hidden');
			$("#remove_invalid_terms").addClass('hidden');
			$(".number_present_terms").html(validTermCount);

		}

	});
}


text:
package java.rmi;

/**
 * An RMISecurityException signals that a security exception
 * has occurred during the execution of one of
 * java.rmi.RMISecurityManager's methods.
 *
 * @author  Roger Riggs
 * @since   JDK1.1
 * @deprecated Use {@link SecurityException} instead.
 * Application code should never directly reference this class, and
 * RMISecurityManager no longer throws this subclass of
 * java.lang.SecurityException.
 */
@Deprecated
public class RMISecurityException extends SecurityException {

    /* indicate compatibility with JDK 1.1.x version of class */
     private static final long serialVersionUID = -8433406075740433514L;

    /**
     * Construct an RMISecurityException with a detail message.
     * @param name the detail message
     * @since JDK1.1
     * @deprecated no replacement
     */
    @Deprecated
    public RMISecurityException(String name) {
        super(name);
    }

    /**
     * Construct an RMISecurityException with a detail message.
     * @param name the detail message
     * @param arg ignored
     * @since JDK1.1
     * @deprecated no replacement
     */
    @Deprecated
    public RMISecurityException(String name, String arg) {
        this(name);
    }
}


text:
#import <UIKit/UIKit.h>
#import "IconDownloader.h"
#import <ArcGIS/ArcGIS.h>

@interface ContentViewControllerBase : UITableViewController <IconDownloaderDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) AGSPortal *portal;
@property (nonatomic, strong) NSMutableDictionary *currentIconDownloads;

//This is the array of search responses.
@property (nonatomic, strong) NSMutableArray *searchResponseArray;

@property (nonatomic) BOOL doneLoading;

- (NSInteger) numberOfRowsInSection:(NSInteger)section;
- (id)contentForRowAtIndex:(NSIndexPath *)indexPath;
- (void)loadMoreResults:(NSInteger)section;
- (void)startIconDownload:(id)contentType forIndexPath:(NSIndexPath *)indexPath withSize:(CGSize)size;

@end


text:
"""These are filesystem related flows."""

import fnmatch
import re
import stat

from grr.lib import aff4
from grr.lib import flow
from grr.lib import rdfvalue

from grr.lib.aff4_objects import aff4_grr
from grr.proto import flows_pb2

def CreateAFF4Object(stat_response, client_id, token, sync=False):
  """This creates a File or a Directory from a stat response."""

  stat_response.aff4path = aff4.AFF4Object.VFSGRRClient.PathspecToURN(
      stat_response.pathspec, client_id)

  if stat.S_ISDIR(stat_response.st_mode):
    fd = aff4.FACTORY.Create(stat_response.aff4path, "VFSDirectory",
                             mode="w", token=token)
  else:
    fd = aff4.FACTORY.Create(stat_response.aff4path, "VFSFile", mode="w",
                             token=token)

  fd.Set(fd.Schema.STAT(stat_response))
  fd.Set(fd.Schema.PATHSPEC(stat_response.pathspec))
  fd.Close(sync=sync)

class ListDirectoryArgs(rdfvalue.RDFProtoStruct):
  protobuf = flows_pb2.ListDirectoryArgs

class ListDirectory(flow.GRRFlow):
  """List files in a directory."""

  category = "/Filesystem/"
  args_type = ListDirectoryArgs
  behaviours = flow.GRRFlow.behaviours + "ADVANCED"

  @flow.StateHandler(next_state=["List", "Stat"])
  def Start(self):
    """Issue a request to list the directory."""
    self.CallClient("StatFile", pathspec=self.state.args.pathspec,
                    next_state="Stat")

    # We use data to pass the path to the callback:
    self.CallClient("ListDirectory", pathspec=self.state.args.pathspec,
                    next_state="List")

  @flow.StateHandler()
  def Stat(self, responses):
    """Save stat information on the directory."""
    # Did it work?
    if not responses.success:
      self.Error("Could not stat directory: %s" % responses.status)

    else:
      # Keep the stat response for later.
      self.state.Register("stat", rdfvalue.StatEntry(responses.First()))
      self.state.Register("directory_pathspec", self.state.stat.pathspec)

      # The full path of the object is the combination of the client_id and the
      # path.
      self.state.Register("urn", aff4.AFF4Object.VFSGRRClient.PathspecToURN(
          self.state.directory_pathspec, self.client_id))

  @flow.StateHandler()
  def List(self, responses):
    """Collect the directory listing and store in the datastore."""
    if not responses.success:
      raise flow.FlowError(str(responses.status))

    self.Status("Listed %s", self.state.urn)

    # The AFF4 object is opened for writing with an asyncronous close for speed.
    fd = aff4.FACTORY.Create(self.state.urn, "VFSDirectory", mode="w",
                             token=self.token)

    fd.Set(fd.Schema.PATHSPEC(self.state.directory_pathspec))
    fd.Set(fd.Schema.STAT(self.state.stat))

    fd.Close(sync=False)

    for st in responses:
      st = rdfvalue.StatEntry(st)
      CreateAFF4Object(st, self.client_id, self.token, sync=False)
      self.SendReply(st)  # Send Stats to parent flows.

    aff4.FACTORY.Flush()

  @flow.StateHandler()
  def End(self):
    if self.state.urn:
      self.Notify("ViewObject", self.state.urn,
                  u"Listed {0}".format(self.state.args.pathspec))

class IteratedListDirectory(ListDirectory):
  """A Flow to retrieve a directory listing using an iterator.

  This flow is an example for how to use the iterated client actions. Normally
  you do not need to call this flow - a ListDirectory flow is enough.
  """

  category = None

  @flow.StateHandler(next_state="List")
  def Start(self):
    """Issue a request to list the directory."""
    self.state.Register("responses", [])
    self.state.Register("urn", None)

    # We use data to pass the path to the callback:
    self.state.Register("request", rdfvalue.ListDirRequest(
        pathspec=self.state.args.pathspec))

    # For this example we will use a really small number to force many round
    # trips with the client. This is a performance killer.
    self.state.request.iterator.number = 50

    self.CallClient("IteratedListDirectory", self.state.request,
                    next_state="List")

  @flow.StateHandler(next_state="List")
  def List(self, responses):
    """Collect the directory listing and store in the data store."""
    if not responses.success:
      raise flow.FlowError(str(responses.status))

    if responses:
      for response in responses:
        self.state.responses.append(response)

      self.state.request.iterator = responses.iterator
      self.CallClient("IteratedListDirectory", self.state.request,
                      next_state="List")
    else:
      self.StoreDirectory()

  def StoreDirectory(self):
    """Store the content of the directory listing in the AFF4 object."""
    # The full path of the object is the combination of the client_id and the
    # path.
    if not self.state.responses: return

    directory_pathspec = self.state.responses[0].pathspec.Dirname()

    urn = aff4.AFF4Object.VFSGRRClient.PathspecToURN(
        directory_pathspec, self.client_id)

    # First dir we get back is the main urn.
    if not self.state.urn: self.state.urn = urn

    fd = aff4.FACTORY.Create(urn, "VFSDirectory", token=self.token)
    fd.Close(sync=False)

    for st in self.state.responses:
      st = rdfvalue.StatEntry(st)
      CreateAFF4Object(st, self.client_id, self.token)
      self.SendReply(st)  # Send Stats to parent flows.

  @flow.StateHandler()
  def End(self):
    self.Notify("ViewObject", self.state.urn, "List of {0} completed.".format(
        self.state.args.pathspec))

class RecursiveListDirectoryArgs(rdfvalue.RDFProtoStruct):
  protobuf = flows_pb2.RecursiveListDirectoryArgs

class RecursiveListDirectory(flow.GRRFlow):
  """Recursively list directory on the client."""

  category = "/Filesystem/"

  args_type = RecursiveListDirectoryArgs

  @flow.StateHandler(next_state="ProcessDirectory")
  def Start(self):
    """List the initial directory."""
    # The first directory we listed.
    self.state.Register("first_directory", None)

    self.state.Register("dir_count", 0)
    self.state.Register("file_count", 0)

    self.CallClient("ListDirectory", pathspec=self.state.args.pathspec,
                    next_state="ProcessDirectory")

  @flow.StateHandler(next_state="ProcessDirectory")
  def ProcessDirectory(self, responses):
    """Recursively list the directory, and add to the timeline."""
    if responses.success:
      response = responses.First()

      if response is None:
        return

      directory_pathspec = response.pathspec.Dirname()

      urn = aff4.AFF4Object.VFSGRRClient.PathspecToURN(
          directory_pathspec, self.client_id)

      self.StoreDirectory(responses)

      # If the urn is too deep we quit to prevent recursion errors.
      if self.state.first_directory is None:
        self.state.first_directory = urn

      else:
        relative_name = urn.RelativeName(self.state.first_directory) or ""
        if len(relative_name.split("/")) >= self.state.args.max_depth:
          self.Log("Exceeded maximum path depth at %s.",
                   urn.RelativeName(self.state.first_directory))
          return

      for stat_response in responses:
        # Queue a list directory for each directory here, but do not follow
        # symlinks.
        if (not stat_response.symlink and
            stat.S_ISDIR(stat_response.st_mode)):
          self.CallClient("ListDirectory", pathspec=stat_response.pathspec,
                          next_state="ProcessDirectory")
          self.state.dir_count += 1
          if self.state.dir_count % 100 == 0:   # Log every 100 directories
            self.Status("Reading %s. (%d nodes, %d directories done)",
                        urn.RelativeName(self.state.first_directory),
                        self.state.file_count, self.state.dir_count)

      self.state.file_count += len(responses)

  def StoreDirectory(self, responses):
    """Stores all stat responses."""
    for st in responses:
      st = rdfvalue.StatEntry(st)
      CreateAFF4Object(st, self.client_id, self.token)
      self.SendReply(st)  # Send Stats to parent flows.

  @flow.StateHandler()
  def End(self):
    status_text = "Recursive Directory Listing complete %d nodes, %d dirs"
    urn = aff4.AFF4Object.VFSGRRClient.PathspecToURN(self.state.args.pathspec,
                                                     self.client_id)
    self.Notify("ViewObject", self.state.first_directory or urn,
                status_text % (self.state.file_count, self.state.dir_count))
    self.Status(status_text, self.state.file_count, self.state.dir_count)

class UpdateSparseImageChunksArgs(rdfvalue.RDFProtoStruct):
  protobuf = flows_pb2.UpdateSparseImageChunksArgs

class UpdateSparseImageChunks(flow.GRRFlow):
  """Updates a list of chunks of a sparse image from the client."""

  category = "/Filesystem/"
  args_type = UpdateSparseImageChunksArgs

  def GetBufferForChunk(self, chunk):
    chunk_offset = chunk * self.state.chunksize
    request = rdfvalue.BufferReference(pathspec=self.state.pathspec,
                                       length=self.state.chunksize,
                                       offset=chunk_offset)
    return request

  @flow.StateHandler(next_state="UpdateChunk")
  def Start(self):

    fd = aff4.FACTORY.Open(self.state.args.file_urn, token=self.token,
                           aff4_type="AFF4SparseImage", mode="rw")

    pathspec = fd.Get(fd.Schema.PATHSPEC)
    self.state.Register("pathspec", pathspec)
    self.state.Register("fd", fd)
    self.state.Register("chunksize", fd.chunksize)
    self.state.Register("missing_chunks", self.state.args.chunks_to_fetch)

    if self.state.missing_chunks:
      # TODO(user): At the moment we aren't using deduplication, since
      # we're transferring every chunk from the client, regardless of whether
      # it's a known blob. We should do something similar to MultiGetFile here
      # instead.
      chunk = self.state.missing_chunks.Pop(0)
      request = self.GetBufferForChunk(chunk)
      self.CallClient("TransferBuffer", request, next_state="UpdateChunk")

  @flow.StateHandler(next_state="UpdateChunk")
  def UpdateChunk(self, responses):
    if not responses.success:
      raise IOError("Error running TransferBuffer: %s" % responses.status)

    response = responses.First()

    if len(self.state.missing_chunks) >= 1:
      chunk_number = response.offset / self.state.chunksize
      self.state.fd.AddBlob(blob_hash=response.data,
                            length=response.length,
                            chunk_number=chunk_number)

      self.state.fd.Flush()

      next_chunk = self.state.missing_chunks.Pop(0)
      request = self.GetBufferForChunk(next_chunk)
      self.CallClient("TransferBuffer", request, next_state="UpdateChunk")

class FetchBufferForSparseImageArgs(rdfvalue.RDFProtoStruct):
  protobuf = flows_pb2.FetchBufferForSparseImageArgs

class FetchBufferForSparseImage(flow.GRRFlow):
  """Reads data from a client-side file, specified by a length and offset.

  This data is written to an AFF4SparseImage object. Note that
  more data than is requested may be read since we align reads to chunks.
  """

  category = "/Filesystem/"
  args_type = FetchBufferForSparseImageArgs

  @flow.StateHandler(next_state="TransferBuffer")
  def Start(self):

    urn = self.state.args.file_urn

    fd = aff4.FACTORY.Open(urn, token=self.token, aff4_type="AFF4SparseImage",
                           mode="rw")
    self.state.Register("fd", fd)

    pathspec = fd.Get(fd.Schema.PATHSPEC)

    # Use the object's chunk size, in case it's different to the class-wide
    # chunk size.
    chunksize = fd.chunksize
    self.state.Register("pathspec", pathspec)
    self.state.Register("chunksize", chunksize)

    # Make sure we always read a whole number of chunks.
    new_length, new_offset = self.AlignToChunks(self.state.args.length,
                                                self.state.args.offset,
                                                chunksize)

    # Remember where we're up to in reading the file, and how much we have left
    # to read.
    self.state.Register("bytes_left_to_read", new_length)
    self.state.Register("current_offset", new_offset)

    # Always read one chunk at a time.
    request = rdfvalue.BufferReference(pathspec=self.state.pathspec,
                                       length=self.state.chunksize,
                                       offset=self.state.current_offset)

    # Remember where we're up to, and that we're about to read one chunk.
    self.state.bytes_left_to_read -= chunksize
    self.state.current_offset += chunksize

    self.CallClient("TransferBuffer", request,
                    next_state="TransferBuffer")

  @flow.StateHandler(next_state="TransferBuffer")
  def TransferBuffer(self, responses):
    # Did it work?
    if not responses.success:
      raise IOError("Error running TransferBuffer: %s" % responses.status)

    response = responses.First()

    # Write the data we got from the client to the file.
    sparse_image = self.state.fd
    chunk_number = response.offset / sparse_image.chunksize
    sparse_image.AddBlob(blob_hash=response.data,
                         length=response.length,
                         chunk_number=chunk_number)
    sparse_image.Flush()

    length_to_read = min(self.state.chunksize,
                         self.state.bytes_left_to_read)

    if length_to_read:
      request = rdfvalue.BufferReference(pathspec=self.state.pathspec,
                                         length=length_to_read,
                                         offset=self.state.current_offset)

      # TODO(user): Again, this is going to be too slow, since we're
      # waiting for a client response every time we request a buffer. We need to
      # queue up multiple reads.
      self.CallClient("TransferBuffer", request,
                      next_state="TransferBuffer")

      # Move our offset along the file by how much we read.
      self.state.current_offset += length_to_read
      # Remember how much more we need to read.
      self.state.bytes_left_to_read = max(
          0,
          self.state.bytes_left_to_read - length_to_read)

  @staticmethod
  def AlignToChunks(length, offset, chunksize):
    """Make sure that if we are reading part of a chunk, we read all of it.

    Args:
      length: How much data to read.
      offset: Where in the file to read it from.
      chunksize: How big the chunks in the file are.

    Returns:
      A (length, offset) tuple, containing the length and offset
      required to read all affected chunks.
    """
    start_chunk = offset / chunksize
    end_chunk = (offset + length) / chunksize
    # If we happened to round down to the beginning of the end chunk, make sure
    # to read to the end of it (which is the beginning of the next chunk).
    if (offset + length) % chunksize != 0:
      end_chunk += 1

    # New offset starts from the beginning of the start chunk.
    new_offset = start_chunk * chunksize
    new_length = (end_chunk * chunksize) - new_offset

    return new_length, new_offset

class MakeNewAFF4SparseImageArgs(rdfvalue.RDFProtoStruct):
  protobuf = flows_pb2.MakeNewAFF4SparseImageArgs

class MakeNewAFF4SparseImage(flow.GRRFlow):
  """Gets a new file from the client, possibly as an AFF4SparseImage.

  If the filesize is >= the size threshold, then we get the file as an empty
  AFF4SparseImage, otherwise we just call GetFile, which gets the complete file.

  We do the check to see if the file is big enough to get as an AFF4SparseImage
  in this flow so we don't need to do another round trip to the client.

  Args:
    pathspec: Pathspec of the file to look at.
    size_threshold: If the file is bigger than this size, we'll get it as an
    empty AFF4SparseImage, otherwise we'll just download the whole file as
    usual with GetFile.
  """

  category = "/Filesystem/"
  args_type = MakeNewAFF4SparseImageArgs

  @flow.StateHandler(next_state="ProcessStat")
  def Start(self):
    self.CallClient("StatFile", pathspec=self.state.args.pathspec,
                    next_state="ProcessStat")

  @flow.StateHandler(next_state="End")
  def ProcessStat(self, responses):
    # Did it work?
    if not responses.success:
      # It's better to raise rather than merely logging since it will
      # make it to the flow's protobuf and users can
      # inspect the reason this flow failed.
      raise IOError("Could not stat file: %s" % responses.status)

    client_stat = responses.First()

    # Update the pathspec to the one we got from the client.
    self.state.Register("pathspec", client_stat.pathspec)

    # If the file was big enough, we'll store it as an AFF4SparseImage
    if client_stat.st_size > self.state.args.size_threshold:
      urn = aff4_grr.VFSGRRClient.PathspecToURN(self.state.pathspec,
                                                self.client_id)

      fd = aff4.FACTORY.Open(urn, token=self.token)

      # TODO(user) When we can check the last update time of the
      # contents of a file, raise if the contents have been updated before here.

      fd = aff4.FACTORY.Create(urn, aff4_type="AFF4SparseImage",
                               token=self.token, mode="rw")
      fd.Set(fd.Schema.PATHSPEC, self.state.pathspec)
      fd.Set(fd.Schema.STAT, client_stat)
      fd.Flush()
    else:
      # Otherwise, just get the whole file.
      self.CallFlow("MultiGetFile", pathspecs=[self.state.pathspec],
                    next_state="End")

  @flow.StateHandler()
  def End(self, responses):
    # Check that the GetFile flow worked.
    if not responses.success:
      raise IOError("Could not get file: %s" % responses.status)

class GlobArgs(rdfvalue.RDFProtoStruct):
  protobuf = flows_pb2.GlobArgs

  def Validate(self):
    """Ensure that the glob paths are valid."""
    self.paths.Validate()

class GlobMixin(object):
  """A MixIn to implement the glob functionality."""

  def GlobForPaths(self, paths, pathtype="OS", root_path=None,
                   no_file_type_check=False):
    """Starts the Glob.

    This is the main entry point for this flow mixin.

    First we convert the pattern into regex components, and then we
    interpolate each component. Finally, we generate a cartesian product of all
    combinations.

    Args:
      paths: A list of GlobExpression instances.
      pathtype: The pathtype to use for creating pathspecs.
      root_path: A pathspec where to start searching from.
      no_file_type_check: Work with all kinds of files - not only with regular
                          ones.
    """
    patterns = []

    if not paths:
      # Nothing to do.
      return

    client = aff4.FACTORY.Open(self.client_id, token=self.token)

    self.state.Register("pathtype", pathtype)
    self.state.Register("root_path", root_path)
    self.state.Register("no_file_type_check", no_file_type_check)

    # Transform the patterns by substitution of client attributes. When the
    # client has multiple values for an attribute, this generates multiple
    # copies of the pattern, one for each variation. e.g.:
    # /home/%%Usernames%%/* -> [ /home/user1/*, /home/user2/* ]
    for path in paths:
      patterns.extend(path.Interpolate(client=client))

    # Expand each glob pattern into a list of components. A component is either
    # a wildcard or a literal component.
    # e.g. /usr/lib/*.exe -> ['/usr/lib', '.*.exe']

    # We build a tree for each component such that duplicated components are
    # merged. We do not need to reissue the same client requests for the same
    # components. For example, the patterns:
    # '/home/%%Usernames%%*' -> {'/home/': {
    #      'syslog.*\\Z(?ms)': {}, 'test.*\\Z(?ms)': {}}}
    # Note: The component tree contains serialized pathspecs in dicts.
    for pattern in patterns:
      # The root node.
      node = self.state.component_tree

      for component in self.ConvertGlobIntoPathComponents(pattern):
        node = node.setdefault(component.SerializeToString(), {})

    root_path = self.state.component_tree.keys()[0]
    self.CallStateInline(messages=[None], next_state="ProcessEntry",
                         request_data=dict(component_path=[root_path]))

  def GlobReportMatch(self, stat_response):
    """Called when we've found a matching a StatEntry."""
    # By default write the stat_response to the AFF4 VFS.
    CreateAFF4Object(stat_response, self.client_id, self.token)

  # A regex indicating if there are shell globs in this path.
  GLOB_MAGIC_CHECK = re.compile("[*?[]")

  # Maximum number of files to inspect in a single directory
  FILE_MAX_PER_DIR = 100000

  def ConvertGlobIntoPathComponents(self, pattern):
    """Converts a glob pattern into a list of pathspec components.

    Wildcards are also converted to regular expressions. The pathspec components
    do not span directories, and are marked as a regex or a literal component.

    We also support recursion into directories using the ** notation.  For
    example, /home/**2/foo.txt will find all files named foo.txt recursed 2
    directories deep. If the directory depth is omitted, it defaults to 3.

    Example:
     /home/test/* -> ['home', 'test', '.*\\Z(?ms)']

    Args:
      pattern: A glob expression with wildcards.

    Returns:
      A list of PathSpec instances for each component.

    Raises:
      ValueError: If the glob is invalid.
    """

    components = []
    for path_component in pattern.split("/"):
      # A ** in the path component means recurse into directories that match the
      # pattern.
      m = rdfvalue.GlobExpression.RECURSION_REGEX.search(path_component)
      if m:
        path_component = path_component.replace(m.group(0), "*")

        component = rdfvalue.PathSpec(
            path=fnmatch.translate(path_component),
            pathtype=self.state.pathtype,
            path_options=rdfvalue.PathSpec.Options.RECURSIVE)

        # Allow the user to override the recursion depth.
        if m.group(1):
          component.recursion_depth = int(m.group(1))

      elif self.GLOB_MAGIC_CHECK.search(path_component):
        component = rdfvalue.PathSpec(
            path=fnmatch.translate(path_component),
            pathtype=self.state.pathtype,
            path_options=rdfvalue.PathSpec.Options.REGEX)
      else:
        pathtype = self.state.pathtype
        # TODO(user): This is a backwards compatibility hack. Remove when
        # all clients reach 3.0.0.2.
        if (pathtype == rdfvalue.PathSpec.PathType.TSK and
            re.match("^.:$", path_component)):
          path_component = "%s\\" % path_component
        component = rdfvalue.PathSpec(
            path=path_component,
            pathtype=pathtype,
            path_options=rdfvalue.PathSpec.Options.CASE_INSENSITIVE)

      components.append(component)

    return components

  @flow.StateHandler()
  def Start(self, **_):
    super(GlobMixin, self).Start()
    self.state.Register("component_tree", {})

  def FindNode(self, component_path):
    """Find the node in the component_tree from component_path.

    Args:
      component_path: A list of components which reference a node in the
        component tree. This allows us to resume processing in the tree.

    Returns:
      A node in the component_tree.
    """
    # Find the node that the component path is referring to.
    node = self.state.component_tree
    for component in component_path:
      node = node[component]

    return node

  def _MatchPath(self, pathspec, response):
    """Check if the responses matches the pathspec (considering options)."""
    to_match = response.pathspec.Basename()
    if pathspec.path_options == rdfvalue.PathSpec.Options.CASE_INSENSITIVE:
      return to_match.lower() == pathspec.path.lower()
    elif pathspec.path_options == rdfvalue.PathSpec.Options.CASE_LITERAL:
      return to_match == pathspec.path
    elif pathspec.path_options == rdfvalue.PathSpec.Options.REGEX:
      return bool(re.match(pathspec.path, to_match, flags=re.IGNORECASE))
    elif pathspec.path_options == rdfvalue.PathSpec.Options.RECURSIVE:
      return True
    raise ValueError("Unknown Pathspec type.")

  @flow.StateHandler(next_state="ProcessEntry")
  def ProcessEntry(self, responses):
    """Process the responses from the client."""
    if not responses.success:
      return

    # If we get a response with an unfinished iterator then we missed some
    # files. Call Find on the client until we're done.
    if (responses.iterator and responses.iterator.state !=
        responses.iterator.State.FINISHED):
      findspec = rdfvalue.FindSpec(responses.request.request.args)
      findspec.iterator = responses.iterator
      self.CallClient("Find", findspec,
                      next_state="ProcessEntry",
                      request_data=responses.request_data)

    # The Find client action does not return a StatEntry but a
    # FindSpec. Normalize to a StatEntry.
    stat_responses = [r.hit if isinstance(r, rdfvalue.FindSpec) else r
                      for r in responses]

    # If this was a pure path matching call without any regex / recursion, we
    # know exactly which node in the component tree we have to process next and
    # get it from the component_path. If this was a regex match though, we
    # sent the client a combined regex that matches all nodes in order to save
    # round trips and client processing time. In that case we only get the
    # base node and have to check for all subnodes if the response actually
    # matches that subnode before we continue processing.
    component_path = responses.request_data.get("component_path")
    if component_path is not None:

      for response in stat_responses:
        self._ProcessResponse(response, [component_path])

    else:
      # This is a combined match.
      base_path = responses.request_data["base_path"]
      base_node = self.FindNode(base_path)
      for response in stat_responses:
        matching_components = []
        for next_node in base_node.keys():
          pathspec = rdfvalue.PathSpec(next_node)

          if self._MatchPath(pathspec, response):
            matching_path = base_path + [next_node]
            matching_components.append(matching_path)

        if matching_components:
          self._ProcessResponse(response, matching_components,
                                base_wildcard=True)

  def _GetBasePathspec(self, response):
    if response:
      return response.pathspec.Copy()
    else:
      root_path = self.state.root_path
      if root_path:
        return root_path.Copy()
    return None

  def _ProcessResponse(self, response, component_paths, base_wildcard=False):
    for component_path in component_paths:
      regexes_to_get = []
      recursions_to_get = {}

      node = self.FindNode(component_path)

      if not node:
        # Node is empty representing a leaf node - we found a hit - report it.
        self.GlobReportMatch(response)
        return

      # There are further components in the tree - iterate over them.
      for component_str, next_node in node.items():
        component = rdfvalue.PathSpec(component_str)
        next_component = component_path + [component_str]

        # If we reach this point, we are instructed to go deeper into the
        # directory structure. We only want to actually do this if
        # - the last response was a proper directory,
        # - or it was a file (an image) that was explicitly given meaning
        #   no wildcards or groupings,
        # - or no_file_type_check was set.
        #
        # This reduces the number of TSK opens on the client that may
        # sometimes lead to instabilities due to bugs in the library.

        if response and (
            not (stat.S_ISDIR(response.st_mode) or
                 not base_wildcard or
                 self.state.no_file_type_check)):
          continue

        if component.path_options == component.Options.RECURSIVE:
          recursions_to_get.setdefault(
              component.recursion_depth, []).append(component)
        elif component.path_options == component.Options.REGEX:
          regexes_to_get.append(component)

        elif component.path_options == component.Options.CASE_INSENSITIVE:
          # Here we need to create the next pathspec by appending the current
          # component to what we already have. If we don't have anything yet, we
          # fall back to the root path. If there is no root path either, the
          # current component becomes the new base.
          base_pathspec = self._GetBasePathspec(response)
          if base_pathspec:
            pathspec = base_pathspec.Append(component)
          else:
            pathspec = component

          if not next_node:
            # Check for the existence of the last node.
            request = rdfvalue.ListDirRequest(pathspec=pathspec)

            if (response and
                (response.st_mode == 0 or not stat.S_ISREG(response.st_mode))):
              # If next node is empty, this node is a leaf node, we
              # therefore must stat it to check that it is there.
              self.CallClient(
                  "StatFile", request, next_state="ProcessEntry",
                  request_data=dict(component_path=next_component))
          else:
            # There is no need to go back to the client for intermediate
            # paths in the prefix tree, just emulate this by recursively
            # calling this state inline.
            self.CallStateInline(
                [rdfvalue.StatEntry(pathspec=pathspec)],
                next_state="ProcessEntry",
                request_data=dict(component_path=next_component))

      if recursions_to_get or regexes_to_get:
        # Recursions or regexes need a base pathspec to operate on. If we
        # have neither a response or a root path, we send a default pathspec
        # that opens the root with pathtype "OS".
        base_pathspec = self._GetBasePathspec(response)
        if not base_pathspec:
          base_pathspec = rdfvalue.PathSpec(path="/", pathtype="OS")

        for depth, recursions in recursions_to_get.iteritems():
          path_regex = "(?i)^" + "$|^".join(
              set([c.path for c in recursions])) + "$"

          findspec = rdfvalue.FindSpec(pathspec=base_pathspec,
                                       cross_devs=True,
                                       max_depth=depth,
                                       path_regex=path_regex)

          findspec.iterator.number = self.FILE_MAX_PER_DIR
          self.CallClient("Find", findspec,
                          next_state="ProcessEntry",
                          request_data=dict(base_path=component_path))

        if regexes_to_get:
          path_regex = "(?i)^" + "$|^".join(
              set([c.path for c in regexes_to_get])) + "$"
          findspec = rdfvalue.FindSpec(pathspec=base_pathspec,
                                       max_depth=1,
                                       path_regex=path_regex)

          findspec.iterator.number = self.FILE_MAX_PER_DIR
          self.CallClient("Find", findspec,
                          next_state="ProcessEntry",
                          request_data=dict(base_path=component_path))

class Glob(GlobMixin, flow.GRRFlow):
  """Glob the filesystem for patterns.

  Returns:
    StatResponse messages, one for each matching file.
  """

  category = "/Filesystem/"
  behaviours = flow.GRRFlow.behaviours + "ADVANCED"
  args_type = GlobArgs

  @flow.StateHandler(next_state="ProcessEntry")
  def Start(self):
    """Starts the Glob.

    First we convert the pattern into regex components, and then we
    interpolate each component. Finally, we generate a cartesian product of all
    combinations.
    """
    super(Glob, self).Start()
    self.GlobForPaths(self.args.paths, pathtype=self.args.pathtype,
                      root_path=self.args.root_path,
                      no_file_type_check=self.args.no_file_type_check)

  def GlobReportMatch(self, stat_response):
    """Called when we've found a matching a StatEntry."""
    super(Glob, self).GlobReportMatch(stat_response)
    self.SendReply(stat_response)

class DiskVolumeInfoArgs(rdfvalue.RDFProtoStruct):
  protobuf = flows_pb2.DiskVolumeInfoArgs

def PathHasDriveLetter(path):
  """Check path for windows drive letter.

  Use 1:2 to avoid raising on single character paths.

  Args:
    path: path string
  Returns:
    True if this path has a drive letter.
  """
  return path[1:2] == ":"

class DiskVolumeInfo(flow.GRRFlow):
  """Get disk volume info for a given path.

  On linux and OS X we call StatFS on each path and return the results. For
  windows we collect all the volume information and filter it using the drive
  letters in the supplied path list.
  """
  args_type = DiskVolumeInfoArgs
  category = "/Filesystem/"
  behaviours = flow.GRRFlow.behaviours + "ADVANCED"

  @flow.StateHandler(next_state=["CollectVolumeInfo", "StoreSystemRoot"])
  def Start(self):
    client = aff4.FACTORY.Open(self.client_id, token=self.token)
    self.state.Register("system", client.Get(client.Schema.SYSTEM))
    self.state.Register("drive_letters", set())
    self.state.Register("system_root_required", False)

    if self.state.system == "Windows":
      # Handle the case where a path is specified without the drive letter by
      # collecting systemroot and making sure we report the disk usage for it.
      for path in self.args.path_list:
        if PathHasDriveLetter(path):
          self.state.drive_letters.add(path[0:2])
        else:
          self.state.system_root_required = True
      if self.state.system_root_required:
        self.CallFlow(
            "ArtifactCollectorFlow",
            artifact_list=["SystemRoot"],
            next_state="StoreSystemRoot")
        return

    self.CallStateInline(next_state="CollectVolumeInfo")

  @flow.StateHandler(next_state=["CollectVolumeInfo"])
  def StoreSystemRoot(self, responses):
    if not responses.success or not responses.First():
      if self.state.drive_letters:
        # We have at least one path that already has a drive letter so we'll log
        # rather than raise.
        self.Log("Error collecting SystemRoot artifact: %s", responses.status)
      else:
        raise flow.FlowError(
            "Error collecting SystemRoot artifact: %s" % responses.status)

    drive = str(responses.First())[0:2]
    if drive:
      self.state.drive_letters.add(drive)
    else:
      self.Log("Bad result for systemdrive: %s", responses.First())

    self.CallStateInline(next_state="CollectVolumeInfo")

  @flow.StateHandler(next_state=["ProcessVolumes", "ProcessWindowsVolumes"])
  def CollectVolumeInfo(self, unused_responses):
    if self.state.system == "Windows":
      # No dependencies for WMI
      deps = rdfvalue.ArtifactCollectorFlowArgs.Dependency.IGNORE_DEPS
      self.CallFlow(
          "ArtifactCollectorFlow",
          artifact_list=["WMILogicalDisks"],
          next_state="ProcessWindowsVolumes",
          dependencies=deps,
          store_results_in_aff4=True)
    else:
      self.CallClient("StatFS",
                      rdfvalue.StatFSRequest(
                          path_list=self.args.path_list,
                          pathtype=self.args.pathtype),
                      next_state="ProcessVolumes")

  @flow.StateHandler()
  def ProcessWindowsVolumes(self, responses):
    if not responses.success:
      self.Log("Error running WMILogicalDisks artifact: %s", responses.status)

    for response in responses:
      if response.windows.drive_letter in self.state.drive_letters:
        self.SendReply(response)

  @flow.StateHandler()
  def ProcessVolumes(self, responses):
    if not responses.success:
      self.Log("Error running StatFS: %s", responses.status)

    for response in responses:
      self.SendReply(response)


text:
import {filePathToPseudoNamespace, filePathFromProtoWithoutExtension, getPathToRoot} from "../util";
import {ExportMap} from "../ExportMap";
import {Printer} from "../Printer";
import {FileDescriptorProto} from "google-protobuf/google/protobuf/descriptor_pb";
import {WellKnownTypesMap} from "../WellKnown";
import {printMessage} from "./message";
import {printEnum} from "./enum";
import {printExtension} from "./extensions";

export function printFileDescriptorTSD(fileDescriptor: FileDescriptorProto, exportMap: ExportMap) {
  const fileName = fileDescriptor.getName();
  const packageName = fileDescriptor.getPackage();

  const printer = new Printer(0);

  printer.printLn(`// package: ${packageName}`);
  printer.printLn(`// file: ${fileDescriptor.getName()}`);

  const upToRoot = getPathToRoot(fileName);

  printer.printEmptyLn();
  printer.printLn(`import * as jspb from "google-protobuf";`);

  fileDescriptor.getDependencyList().forEach((dependency: string) => {
    const pseudoNamespace = filePathToPseudoNamespace(dependency);
    if (dependency in WellKnownTypesMap) {
      printer.printLn(`import * as ${pseudoNamespace} from "${WellKnownTypesMap[dependency]}";`);
    } else {
      const filePath = filePathFromProtoWithoutExtension(dependency);
      printer.printLn(`import * as ${pseudoNamespace} from "${upToRoot}${filePath}";`);
    }
  });

  fileDescriptor.getMessageTypeList().forEach(enumType => {
    printer.print(printMessage(fileName, exportMap, enumType, 0, fileDescriptor));
  });

  fileDescriptor.getExtensionList().forEach(extension => {
    printer.print(printExtension(fileName, exportMap, extension, 0));
  });

  fileDescriptor.getEnumTypeList().forEach(enumType => {
    printer.print(printEnum(enumType, 0));
  });

  printer.printEmptyLn();

  return printer.getOutput();
}


text:
from django.test import TestCase
from django.db import IntegrityError
from .models import Product

class ProductTest(TestCase):

    def setUp(self):

        self.product = Product.objects.create(
            sku=001,
            price=2997,
            quantity=1,
            shipping=599,
            name='Test Product',
            description='Its a test product.',
            ships_to='USA',
            image='http://sunny95.com/wp-content/blogs.dir/16/files/2013/05/rick-astley.jpg',
            video_link='https://www.youtube.com/watch?v=dQw4w9WgXcQ'
            )

    def create_product_same_sku_as_other_returns_false(self):
        try:
            self.product_copy = Product.objects.create(
            sku=001,
            price=2997,
            quantity=1,
            shipping=599,
            name='Test Product',
            description='Its a test product.',
            ships_to='USA'
            )
        except IntegrityError:
            return False

    def test_product_sku_uniqueness(self):
        self.assertFalse(self.create_product_same_sku_as_other_returns_false())

    def test_try_to_ship_two_products_with_only_one_available(self):
        self.assertNotEqual(self.product.quantity, 2)

    def test_try_to_ship_product_to_other_country(self):
        self.assertNotEqual(self.product.ships_to, 'UK')

    def test_image_is_not_served_from_static(self):
        self.assertFalse(self.product.serve_image_from_static)

    def tearDown(self):
        pass


text:
from copy import copy

_not_in_db = object()

def _get_cache_key(proxy, name, kwargs):
    return type(proxy), proxy.module, name, frozenset(kwargs.items())

def _preload_settings(cls, proxy, cache, **kwargs):
    settings = cls.get_all(proxy.module, **kwargs)
    for name, value in settings.items():
        cache_key = _get_cache_key(proxy, name, kwargs)
        cache[cache_key] = value
    # cache missing entries as not in db
    for name in proxy.defaults.keys() - settings.keys():
        cache_key = _get_cache_key(proxy, name, kwargs)
        cache[cache_key] = _not_in_db
    return settings

def get_all_settings(cls, acl_cls, proxy, no_defaults, **kwargs):
    """Helper function for SettingsProxy.get_all."""
    if no_defaults:
        rv = cls.get_all(proxy.module, **kwargs)
        if acl_cls and proxy.acl_names:
            rv.update(acl_cls.get_all_acls(proxy.module, **kwargs))
        return {k: proxy._convert_to_python(k, v) for k, v in rv.items()}
    settings = dict(proxy.defaults)
    if acl_cls and proxy.acl_names:
        settings.update({name: set() for name in proxy.acl_names})
    settings.update({k: proxy._convert_to_python(k, v)
                     for k, v in cls.get_all(proxy.module, **kwargs).items()
                     if not proxy.strict or k in proxy.defaults})
    if acl_cls and proxy.acl_names:
        settings.update(acl_cls.get_all_acls(proxy.module, **kwargs))
    return settings

def get_setting(cls, proxy, name, default, cache, **kwargs):
    """Helper function for SettingsProxy.get."""
    from indico.core.settings import SettingsProxyBase

    cache_key = _get_cache_key(proxy, name, kwargs)
    try:
        value = cache[cache_key]
        if value is not _not_in_db:
            return proxy._convert_to_python(name, value)
    except KeyError:
        setting = _preload_settings(cls, proxy, cache, **kwargs).get(name, _not_in_db)
        cache[cache_key] = setting
        if setting is not _not_in_db:
            return proxy._convert_to_python(name, setting)
    # value is not_in_db, so use the default
    # we always copy the proxy's default in case it's something mutable
    return copy(proxy.defaults.get(name)) if default is SettingsProxyBase.default_sentinel else default

def get_setting_acl(cls, proxy, name, cache, **kwargs):
    """Helper function for ACLProxy.get."""
    cache_key = _get_cache_key(proxy, name, kwargs)
    try:
        return cache[cache_key]
    except KeyError:
        cache[cache_key] = acl = cls.get_acl(proxy.module, name, **kwargs)
        return acl


text:
/*	$NetBSD: check-tool.c,v 1.6 2014/03/01 03:24:32 christos Exp $	*/

/*
 * Copyright (C) 2004-2014  Internet Systems Consortium, Inc. ("ISC")
 * Copyright (C) 2000-2002  Internet Software Consortium.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

/* Id: check-tool.c,v 1.44 2011/12/22 07:32:39 each Exp  */

/*! \file */

#include <config.h>

#include <stdio.h>

#ifdef _WIN32
#include <Winsock2.h>
#endif

#include "check-tool.h"
#include <isc/buffer.h>
#include <isc/log.h>
#include <isc/mem.h>
#include <isc/netdb.h>
#include <isc/net.h>
#include <isc/region.h>
#include <isc/stdio.h>
#include <isc/string.h>
#include <isc/symtab.h>
#include <isc/types.h>
#include <isc/util.h>

#include <dns/db.h>
#include <dns/dbiterator.h>
#include <dns/fixedname.h>
#include <dns/log.h>
#include <dns/name.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
#include <dns/rdatatype.h>
#include <dns/result.h>
#include <dns/types.h>
#include <dns/zone.h>

#include <isccfg/log.h>

#ifndef CHECK_SIBLING
#define CHECK_SIBLING 1
#endif

#ifndef CHECK_LOCAL
#define CHECK_LOCAL 1
#endif

#ifdef HAVE_ADDRINFO
#ifdef HAVE_GETADDRINFO
#ifdef HAVE_GAISTRERROR
#define USE_GETADDRINFO
#endif
#endif
#endif

#define CHECK(r) \
	do { \
		result = (r); \
		if (result != ISC_R_SUCCESS) \
			goto cleanup; \
	} while (/*CONSTCOND*/0)

#define ERR_IS_CNAME 1
#define ERR_NO_ADDRESSES 2
#define ERR_LOOKUP_FAILURE 3
#define ERR_EXTRA_A 4
#define ERR_EXTRA_AAAA 5
#define ERR_MISSING_GLUE 5
#define ERR_IS_MXCNAME 6
#define ERR_IS_SRVCNAME 7

static const char *dbtype[] = { "rbt" };

int debug = 0;
const char *journal = NULL;
isc_boolean_t nomerge = ISC_TRUE;
#if CHECK_LOCAL
isc_boolean_t docheckmx = ISC_TRUE;
isc_boolean_t dochecksrv = ISC_TRUE;
isc_boolean_t docheckns = ISC_TRUE;
#else
isc_boolean_t docheckmx = ISC_FALSE;
isc_boolean_t dochecksrv = ISC_FALSE;
isc_boolean_t docheckns = ISC_FALSE;
#endif
unsigned int zone_options = DNS_ZONEOPT_CHECKNS |
			    DNS_ZONEOPT_CHECKMX |
			    DNS_ZONEOPT_MANYERRORS |
			    DNS_ZONEOPT_CHECKNAMES |
			    DNS_ZONEOPT_CHECKINTEGRITY |
#if CHECK_SIBLING
			    DNS_ZONEOPT_CHECKSIBLING |
#endif
			    DNS_ZONEOPT_CHECKWILDCARD |
			    DNS_ZONEOPT_WARNMXCNAME |
			    DNS_ZONEOPT_WARNSRVCNAME;
unsigned int zone_options2 = 0;

/*
 * This needs to match the list in bin/named/log.c.
 */
static isc_logcategory_t categories[] = {
	{ "",		     0 },
	{ "client",	     0 },
	{ "network",	     0 },
	{ "update",	     0 },
	{ "queries",	     0 },
	{ "unmatched", 	     0 },
	{ "update-security", 0 },
	{ "query-errors",    0 },
	{ NULL,		     0 }
};

static isc_symtab_t *symtab = NULL;
static isc_mem_t *sym_mctx;

static void
freekey(char *key, unsigned int type, isc_symvalue_t value, void *userarg) {
	UNUSED(type);
	UNUSED(value);
	isc_mem_free(userarg, key);
}

static void
add(char *key, int value) {
	isc_result_t result;
	isc_symvalue_t symvalue;

	if (sym_mctx == NULL) {
		result = isc_mem_create(0, 0, &sym_mctx);
		if (result != ISC_R_SUCCESS)
			return;
	}

	if (symtab == NULL) {
		result = isc_symtab_create(sym_mctx, 100, freekey, sym_mctx,
					   ISC_FALSE, &symtab);
		if (result != ISC_R_SUCCESS)
			return;
	}

	key = isc_mem_strdup(sym_mctx, key);
	if (key == NULL)
		return;

	symvalue.as_pointer = NULL;
	result = isc_symtab_define(symtab, key, value, symvalue,
				   isc_symexists_reject);
	if (result != ISC_R_SUCCESS)
		isc_mem_free(sym_mctx, key);
}

static isc_boolean_t
logged(char *key, int value) {
	isc_result_t result;

	if (symtab == NULL)
		return (ISC_FALSE);

	result = isc_symtab_lookup(symtab, key, value, NULL);
	if (result == ISC_R_SUCCESS)
		return (ISC_TRUE);
	return (ISC_FALSE);
}

static isc_boolean_t
checkns(dns_zone_t *zone, dns_name_t *name, dns_name_t *owner,
	dns_rdataset_t *a, dns_rdataset_t *aaaa)
{
#ifdef USE_GETADDRINFO
	dns_rdataset_t *rdataset;
	dns_rdata_t rdata = DNS_RDATA_INIT;
	struct addrinfo hints, *ai, *cur;
	char namebuf[DNS_NAME_FORMATSIZE + 1];
	char ownerbuf[DNS_NAME_FORMATSIZE];
	char addrbuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:123.123.123.123")];
	isc_boolean_t answer = ISC_TRUE;
	isc_boolean_t match;
	const char *type;
	void *ptr = NULL;
	int result;

	REQUIRE(a == NULL || !dns_rdataset_isassociated(a) ||
		a->type == dns_rdatatype_a);
	REQUIRE(aaaa == NULL || !dns_rdataset_isassociated(aaaa) ||
		aaaa->type == dns_rdatatype_aaaa);

	if (a == NULL || aaaa == NULL)
		return (answer);

	memset(&hints, 0, sizeof(hints));
	hints.ai_flags = AI_CANONNAME;
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	dns_name_format(name, namebuf, sizeof(namebuf) - 1);
	/*
	 * Turn off search.
	 */
	if (dns_name_countlabels(name) > 1U)
		strcat(namebuf, ".");
	dns_name_format(owner, ownerbuf, sizeof(ownerbuf));

	result = getaddrinfo(namebuf, NULL, &hints, &ai);
	dns_name_format(name, namebuf, sizeof(namebuf) - 1);
	switch (result) {
	case 0:
		/*
		 * Work around broken getaddrinfo() implementations that
		 * fail to set ai_canonname on first entry.
		 */
		cur = ai;
		while (cur != NULL && cur->ai_canonname == NULL &&
		       cur->ai_next != NULL)
			cur = cur->ai_next;
		if (cur != NULL && cur->ai_canonname != NULL &&
		    strcasecmp(cur->ai_canonname, namebuf) != 0 &&
		    !logged(namebuf, ERR_IS_CNAME)) {
			dns_zone_log(zone, ISC_LOG_ERROR,
				     "%s/NS '%s' (out of zone) "
				     "is a CNAME '%s' (illegal)",
				     ownerbuf, namebuf,
				     cur->ai_canonname);
			/* XXX950 make fatal for 9.5.0 */
			/* answer = ISC_FALSE; */
			add(namebuf, ERR_IS_CNAME);
		}
		break;
	case EAI_NONAME:
#if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
	case EAI_NODATA:
#endif
		if (!logged(namebuf, ERR_NO_ADDRESSES)) {
			dns_zone_log(zone, ISC_LOG_ERROR,
				     "%s/NS '%s' (out of zone) "
				     "has no addresses records (A or AAAA)",
				     ownerbuf, namebuf);
			add(namebuf, ERR_NO_ADDRESSES);
		}
		/* XXX950 make fatal for 9.5.0 */
		return (ISC_TRUE);

	default:
		if (!logged(namebuf, ERR_LOOKUP_FAILURE)) {
			dns_zone_log(zone, ISC_LOG_WARNING,
				     "getaddrinfo(%s) failed: %s",
				     namebuf, gai_strerror(result));
			add(namebuf, ERR_LOOKUP_FAILURE);
		}
		return (ISC_TRUE);
	}

	/*
	 * Check that all glue records really exist.
	 */
	if (!dns_rdataset_isassociated(a))
		goto checkaaaa;
	result = dns_rdataset_first(a);
	while (result == ISC_R_SUCCESS) {
		dns_rdataset_current(a, &rdata);
		match = ISC_FALSE;
		for (cur = ai; cur != NULL; cur = cur->ai_next) {
			if (cur->ai_family != AF_INET)
				continue;
			ptr = &((struct sockaddr_in *)(cur->ai_addr))->sin_addr;
			if (memcmp(ptr, rdata.data, rdata.length) == 0) {
				match = ISC_TRUE;
				break;
			}
		}
		if (!match && !logged(namebuf, ERR_EXTRA_A)) {
			dns_zone_log(zone, ISC_LOG_ERROR, "%s/NS '%s' "
				     "extra GLUE A record (%s)",
				     ownerbuf, namebuf,
				     inet_ntop(AF_INET, rdata.data,
					       addrbuf, sizeof(addrbuf)));
			add(namebuf, ERR_EXTRA_A);
			/* XXX950 make fatal for 9.5.0 */
			/* answer = ISC_FALSE; */
		}
		dns_rdata_reset(&rdata);
		result = dns_rdataset_next(a);
	}

 checkaaaa:
	if (!dns_rdataset_isassociated(aaaa))
		goto checkmissing;
	result = dns_rdataset_first(aaaa);
	while (result == ISC_R_SUCCESS) {
		dns_rdataset_current(aaaa, &rdata);
		match = ISC_FALSE;
		for (cur = ai; cur != NULL; cur = cur->ai_next) {
			if (cur->ai_family != AF_INET6)
				continue;
			ptr = &((struct sockaddr_in6 *)(cur->ai_addr))->sin6_addr;
			if (memcmp(ptr, rdata.data, rdata.length) == 0) {
				match = ISC_TRUE;
				break;
			}
		}
		if (!match && !logged(namebuf, ERR_EXTRA_AAAA)) {
			dns_zone_log(zone, ISC_LOG_ERROR, "%s/NS '%s' "
				     "extra GLUE AAAA record (%s)",
				     ownerbuf, namebuf,
				     inet_ntop(AF_INET6, rdata.data,
					       addrbuf, sizeof(addrbuf)));
			add(namebuf, ERR_EXTRA_AAAA);
			/* XXX950 make fatal for 9.5.0. */
			/* answer = ISC_FALSE; */
		}
		dns_rdata_reset(&rdata);
		result = dns_rdataset_next(aaaa);
	}

 checkmissing:
	/*
	 * Check that all addresses appear in the glue.
	 */
	if (!logged(namebuf, ERR_MISSING_GLUE)) {
		isc_boolean_t missing_glue = ISC_FALSE;
		for (cur = ai; cur != NULL; cur = cur->ai_next) {
			switch (cur->ai_family) {
			case AF_INET:
				rdataset = a;
				ptr = &((struct sockaddr_in *)(cur->ai_addr))->sin_addr;
				type = "A";
				break;
			case AF_INET6:
				rdataset = aaaa;
				ptr = &((struct sockaddr_in6 *)(cur->ai_addr))->sin6_addr;
				type = "AAAA";
				break;
			default:
				 continue;
			}
			match = ISC_FALSE;
			if (dns_rdataset_isassociated(rdataset))
				result = dns_rdataset_first(rdataset);
			else
				result = ISC_R_FAILURE;
			while (result == ISC_R_SUCCESS && !match) {
				dns_rdataset_current(rdataset, &rdata);
				if (memcmp(ptr, rdata.data, rdata.length) == 0)
					match = ISC_TRUE;
				dns_rdata_reset(&rdata);
				result = dns_rdataset_next(rdataset);
			}
			if (!match) {
				dns_zone_log(zone, ISC_LOG_ERROR, "%s/NS '%s' "
					     "missing GLUE %s record (%s)",
					     ownerbuf, namebuf, type,
					     inet_ntop(cur->ai_family, ptr,
						       addrbuf, sizeof(addrbuf)));
				/* XXX950 make fatal for 9.5.0. */
				/* answer = ISC_FALSE; */
				missing_glue = ISC_TRUE;
			}
		}
		if (missing_glue)
			add(namebuf, ERR_MISSING_GLUE);
	}
	freeaddrinfo(ai);
	return (answer);
#else
	return (ISC_TRUE);
#endif
}

static isc_boolean_t
checkmx(dns_zone_t *zone, dns_name_t *name, dns_name_t *owner) {
#ifdef USE_GETADDRINFO
	struct addrinfo hints, *ai, *cur;
	char namebuf[DNS_NAME_FORMATSIZE + 1];
	char ownerbuf[DNS_NAME_FORMATSIZE];
	int result;
	int level = ISC_LOG_ERROR;
	isc_boolean_t answer = ISC_TRUE;

	memset(&hints, 0, sizeof(hints));
	hints.ai_flags = AI_CANONNAME;
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	dns_name_format(name, namebuf, sizeof(namebuf) - 1);
	/*
	 * Turn off search.
	 */
	if (dns_name_countlabels(name) > 1U)
		strcat(namebuf, ".");
	dns_name_format(owner, ownerbuf, sizeof(ownerbuf));

	result = getaddrinfo(namebuf, NULL, &hints, &ai);
	dns_name_format(name, namebuf, sizeof(namebuf) - 1);
	switch (result) {
	case 0:
		/*
		 * Work around broken getaddrinfo() implementations that
		 * fail to set ai_canonname on first entry.
		 */
		cur = ai;
		while (cur != NULL && cur->ai_canonname == NULL &&
		       cur->ai_next != NULL)
			cur = cur->ai_next;
		if (cur != NULL && cur->ai_canonname != NULL &&
		    strcasecmp(cur->ai_canonname, namebuf) != 0) {
			if ((zone_options & DNS_ZONEOPT_WARNMXCNAME) != 0)
				level = ISC_LOG_WARNING;
			if ((zone_options & DNS_ZONEOPT_IGNOREMXCNAME) == 0) {
				if (!logged(namebuf, ERR_IS_MXCNAME)) {
					dns_zone_log(zone, level,
						     "%s/MX '%s' (out of zone)"
						     " is a CNAME '%s' "
						     "(illegal)",
						     ownerbuf, namebuf,
						     cur->ai_canonname);
					add(namebuf, ERR_IS_MXCNAME);
				}
				if (level == ISC_LOG_ERROR)
					answer = ISC_FALSE;
			}
		}
		freeaddrinfo(ai);
		return (answer);

	case EAI_NONAME:
#if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
	case EAI_NODATA:
#endif
		if (!logged(namebuf, ERR_NO_ADDRESSES)) {
			dns_zone_log(zone, ISC_LOG_ERROR,
				     "%s/MX '%s' (out of zone) "
				     "has no addresses records (A or AAAA)",
				     ownerbuf, namebuf);
			add(namebuf, ERR_NO_ADDRESSES);
		}
		/* XXX950 make fatal for 9.5.0. */
		return (ISC_TRUE);

	default:
		if (!logged(namebuf, ERR_LOOKUP_FAILURE)) {
			dns_zone_log(zone, ISC_LOG_WARNING,
			     "getaddrinfo(%s) failed: %s",
			     namebuf, gai_strerror(result));
			add(namebuf, ERR_LOOKUP_FAILURE);
		}
		return (ISC_TRUE);
	}
#else
	return (ISC_TRUE);
#endif
}

static isc_boolean_t
checksrv(dns_zone_t *zone, dns_name_t *name, dns_name_t *owner) {
#ifdef USE_GETADDRINFO
	struct addrinfo hints, *ai, *cur;
	char namebuf[DNS_NAME_FORMATSIZE + 1];
	char ownerbuf[DNS_NAME_FORMATSIZE];
	int result;
	int level = ISC_LOG_ERROR;
	isc_boolean_t answer = ISC_TRUE;

	memset(&hints, 0, sizeof(hints));
	hints.ai_flags = AI_CANONNAME;
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	dns_name_format(name, namebuf, sizeof(namebuf) - 1);
	/*
	 * Turn off search.
	 */
	if (dns_name_countlabels(name) > 1U)
		strcat(namebuf, ".");
	dns_name_format(owner, ownerbuf, sizeof(ownerbuf));

	result = getaddrinfo(namebuf, NULL, &hints, &ai);
	dns_name_format(name, namebuf, sizeof(namebuf) - 1);
	switch (result) {
	case 0:
		/*
		 * Work around broken getaddrinfo() implementations that
		 * fail to set ai_canonname on first entry.
		 */
		cur = ai;
		while (cur != NULL && cur->ai_canonname == NULL &&
		       cur->ai_next != NULL)
			cur = cur->ai_next;
		if (cur != NULL && cur->ai_canonname != NULL &&
		    strcasecmp(cur->ai_canonname, namebuf) != 0) {
			if ((zone_options & DNS_ZONEOPT_WARNSRVCNAME) != 0)
				level = ISC_LOG_WARNING;
			if ((zone_options & DNS_ZONEOPT_IGNORESRVCNAME) == 0) {
				if (!logged(namebuf, ERR_IS_SRVCNAME)) {
					dns_zone_log(zone, level, "%s/SRV '%s'"
						     " (out of zone) is a "
						     "CNAME '%s' (illegal)",
						     ownerbuf, namebuf,
						     cur->ai_canonname);
					add(namebuf, ERR_IS_SRVCNAME);
				}
				if (level == ISC_LOG_ERROR)
					answer = ISC_FALSE;
			}
		}
		freeaddrinfo(ai);
		return (answer);

	case EAI_NONAME:
#if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
	case EAI_NODATA:
#endif
		if (!logged(namebuf, ERR_NO_ADDRESSES)) {
			dns_zone_log(zone, ISC_LOG_ERROR,
				     "%s/SRV '%s' (out of zone) "
				     "has no addresses records (A or AAAA)",
				     ownerbuf, namebuf);
			add(namebuf, ERR_NO_ADDRESSES);
		}
		/* XXX950 make fatal for 9.5.0. */
		return (ISC_TRUE);

	default:
		if (!logged(namebuf, ERR_LOOKUP_FAILURE)) {
			dns_zone_log(zone, ISC_LOG_WARNING,
				     "getaddrinfo(%s) failed: %s",
				     namebuf, gai_strerror(result));
			add(namebuf, ERR_LOOKUP_FAILURE);
		}
		return (ISC_TRUE);
	}
#else
	return (ISC_TRUE);
#endif
}

isc_result_t
setup_logging(isc_mem_t *mctx, FILE *errout, isc_log_t **logp) {
	isc_logdestination_t destination;
	isc_logconfig_t *logconfig = NULL;
	isc_log_t *log = NULL;

	RUNTIME_CHECK(isc_log_create(mctx, &log, &logconfig) == ISC_R_SUCCESS);
	isc_log_registercategories(log, categories);
	isc_log_setcontext(log);
	dns_log_init(log);
	dns_log_setcontext(log);
	cfg_log_init(log);

	destination.file.stream = errout;
	destination.file.name = NULL;
	destination.file.versions = ISC_LOG_ROLLNEVER;
	destination.file.maximum_size = 0;
	RUNTIME_CHECK(isc_log_createchannel(logconfig, "stderr",
				       ISC_LOG_TOFILEDESC,
				       ISC_LOG_DYNAMIC,
				       &destination, 0) == ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_log_usechannel(logconfig, "stderr",
					 NULL, NULL) == ISC_R_SUCCESS);

	*logp = log;
	return (ISC_R_SUCCESS);
}

/*% scan the zone for oversize TTLs */
static isc_result_t
check_ttls(dns_zone_t *zone, dns_ttl_t maxttl) {
	isc_result_t result;
	dns_db_t *db = NULL;
	dns_dbversion_t *version = NULL;
	dns_dbnode_t *node = NULL;
	dns_dbiterator_t *dbiter = NULL;
	dns_rdatasetiter_t *rdsiter = NULL;
	dns_rdataset_t rdataset;
	dns_fixedname_t fname;
	dns_name_t *name;
	dns_fixedname_init(&fname);
	name = dns_fixedname_name(&fname);
	dns_rdataset_init(&rdataset);

	CHECK(dns_zone_getdb(zone, &db));
	INSIST(db != NULL);

	CHECK(dns_db_newversion(db, &version));
	CHECK(dns_db_createiterator(db, 0, &dbiter));

	for (result = dns_dbiterator_first(dbiter);
	     result == ISC_R_SUCCESS;
	     result = dns_dbiterator_next(dbiter)) {
		result = dns_dbiterator_current(dbiter, &node, name);
		if (result == DNS_R_NEWORIGIN)
			result = ISC_R_SUCCESS;
		CHECK(result);

		CHECK(dns_db_allrdatasets(db, node, version, 0, &rdsiter));
		for (result = dns_rdatasetiter_first(rdsiter);
		     result == ISC_R_SUCCESS;
		     result = dns_rdatasetiter_next(rdsiter)) {
			dns_rdatasetiter_current(rdsiter, &rdataset);
			if (rdataset.ttl > maxttl) {
				char nbuf[DNS_NAME_FORMATSIZE];
				char tbuf[255];
				isc_buffer_t b;
				isc_region_t r;

				dns_name_format(name, nbuf, sizeof(nbuf));
				isc_buffer_init(&b, tbuf, sizeof(tbuf) - 1);
				CHECK(dns_rdatatype_totext(rdataset.type, &b));
				isc_buffer_usedregion(&b, &r);
				r.base[r.length] = 0;

				dns_zone_log(zone, ISC_LOG_ERROR,
					     "%s/%s TTL %d exceeds "
					     "maximum TTL %d",
					     nbuf, tbuf, rdataset.ttl, maxttl);
				dns_rdataset_disassociate(&rdataset);
				CHECK(ISC_R_RANGE);
			}
			dns_rdataset_disassociate(&rdataset);
		}
		if (result == ISC_R_NOMORE)
			result = ISC_R_SUCCESS;
		CHECK(result);

		dns_rdatasetiter_destroy(&rdsiter);
		dns_db_detachnode(db, &node);
	}

	if (result == ISC_R_NOMORE)
		result = ISC_R_SUCCESS;

 cleanup:
	if (node != NULL)
		dns_db_detachnode(db, &node);
	if (rdsiter != NULL)
		dns_rdatasetiter_destroy(&rdsiter);
	if (dbiter != NULL)
		dns_dbiterator_destroy(&dbiter);
	if (version != NULL)
		dns_db_closeversion(db, &version, ISC_FALSE);
	if (db != NULL)
		dns_db_detach(&db);

	return (result);
}

/*% load the zone */
isc_result_t
load_zone(isc_mem_t *mctx, const char *zonename, const char *filename,
	  dns_masterformat_t fileformat, const char *classname,
	  dns_ttl_t maxttl, dns_zone_t **zonep)
{
	isc_result_t result;
	dns_rdataclass_t rdclass;
	isc_textregion_t region;
	isc_buffer_t buffer;
	dns_fixedname_t fixorigin;
	dns_name_t *origin;
	dns_zone_t *zone = NULL;

	REQUIRE(zonep == NULL || *zonep == NULL);

	if (debug)
		fprintf(stderr, "loading \"%s\" from \"%s\" class \"%s\"\n",
			zonename, filename, classname);

	CHECK(dns_zone_create(&zone, mctx));

	dns_zone_settype(zone, dns_zone_master);

	isc_buffer_constinit(&buffer, zonename, strlen(zonename));
	isc_buffer_add(&buffer, strlen(zonename));
	dns_fixedname_init(&fixorigin);
	origin = dns_fixedname_name(&fixorigin);
	CHECK(dns_name_fromtext(origin, &buffer, dns_rootname, 0, NULL));
	CHECK(dns_zone_setorigin(zone, origin));
	CHECK(dns_zone_setdbtype(zone, 1, (const char * const *) dbtype));
	CHECK(dns_zone_setfile2(zone, filename, fileformat));
	if (journal != NULL)
		CHECK(dns_zone_setjournal(zone, journal));

	DE_CONST(classname, region.base);
	region.length = strlen(classname);
	CHECK(dns_rdataclass_fromtext(&rdclass, &region));

	dns_zone_setclass(zone, rdclass);
	dns_zone_setoption(zone, zone_options, ISC_TRUE);
	dns_zone_setoption2(zone, zone_options2, ISC_TRUE);
	dns_zone_setoption(zone, DNS_ZONEOPT_NOMERGE, nomerge);

	dns_zone_setmaxttl(zone, maxttl);

	if (docheckmx)
		dns_zone_setcheckmx(zone, checkmx);
	if (docheckns)
		dns_zone_setcheckns(zone, checkns);
	if (dochecksrv)
		dns_zone_setchecksrv(zone, checksrv);

	CHECK(dns_zone_load(zone));

	/*
	 * When loading map files we can't catch oversize TTLs during
	 * load, so we check for them here.
	 */
	if (fileformat == dns_masterformat_map && maxttl != 0) {
		CHECK(check_ttls(zone, maxttl));
	}

	if (zonep != NULL) {
		*zonep = zone;
		zone = NULL;
	}

 cleanup:
	if (zone != NULL)
		dns_zone_detach(&zone);
	return (result);
}

/*% dump the zone */
isc_result_t
dump_zone(const char *zonename, dns_zone_t *zone, const char *filename,
	  dns_masterformat_t fileformat, const dns_master_style_t *style,
	  const isc_uint32_t rawversion)
{
	isc_result_t result;
	FILE *output = stdout;
	const char *flags;

	flags = (fileformat == dns_masterformat_text) ? "w+" : "wb+";

	if (debug) {
		if (filename != NULL && strcmp(filename, "-") != 0)
			fprintf(stderr, "dumping \"%s\" to \"%s\"\n",
				zonename, filename);
		else
			fprintf(stderr, "dumping \"%s\"\n", zonename);
	}

	if (filename != NULL && strcmp(filename, "-") != 0) {
		result = isc_stdio_open(filename, flags, &output);

		if (result != ISC_R_SUCCESS) {
			fprintf(stderr, "could not open output "
				"file \"%s\" for writing\n", filename);
			return (ISC_R_FAILURE);
		}
	}

	result = dns_zone_dumptostream3(zone, output, fileformat, style,
					rawversion);
	if (output != stdout)
		(void)isc_stdio_close(output);

	return (result);
}

#ifdef _WIN32
void
InitSockets(void) {
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;

	wVersionRequested = MAKEWORD(2, 0);

	err = WSAStartup( wVersionRequested, &wsaData );
	if (err != 0) {
		fprintf(stderr, "WSAStartup() failed: %d\n", err);
		exit(1);
	}
}

void
DestroySockets(void) {
	WSACleanup();
}
#endif


text:
package org.springframework.security.config.ldap;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.test.SpringTestContext;
import org.springframework.security.config.test.SpringTestContextExtension;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.test.web.servlet.MockMvc;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;

@ExtendWith(SpringTestContextExtension.class)
public class EmbeddedLdapServerContextSourceFactoryBeanITests {

	public final SpringTestContext spring = new SpringTestContext(this);

	@Autowired
	private MockMvc mockMvc;

	@Test
	public void contextSourceFactoryBeanWhenEmbeddedServerThenAuthenticates() throws Exception {
		this.spring.register(FromEmbeddedLdapServerConfig.class).autowire();

		this.mockMvc.perform(formLogin().user("bob").password("bobspassword"))
				.andExpect(authenticated().withUsername("bob"));
	}

	@Test
	public void contextSourceFactoryBeanWhenPortZeroThenAuthenticates() throws Exception {
		this.spring.register(PortZeroConfig.class).autowire();

		this.mockMvc.perform(formLogin().user("bob").password("bobspassword"))
				.andExpect(authenticated().withUsername("bob"));
	}

	@Test
	public void contextSourceFactoryBeanWhenCustomLdifAndRootThenAuthenticates() throws Exception {
		this.spring.register(CustomLdifAndRootConfig.class).autowire();

		this.mockMvc.perform(formLogin().user("pg").password("password")).andExpect(authenticated().withUsername("pg"));
	}

	@Test
	public void contextSourceFactoryBeanWhenCustomManagerDnThenAuthenticates() throws Exception {
		this.spring.register(CustomManagerDnConfig.class).autowire();

		this.mockMvc.perform(formLogin().user("bob").password("bobspassword"))
				.andExpect(authenticated().withUsername("bob"));
	}

	@Test
	public void contextSourceFactoryBeanWhenManagerDnAndNoPasswordThenException() {
		assertThatExceptionOfType(UnsatisfiedDependencyException.class)
				.isThrownBy(() -> this.spring.register(CustomManagerDnNoPasswordConfig.class).autowire())
				.withRootCauseInstanceOf(IllegalStateException.class)
				.withMessageContaining("managerPassword is required if managerDn is supplied");
	}

	@EnableWebSecurity
	static class FromEmbeddedLdapServerConfig {

		@Bean
		EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
			return EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer();
		}

		@Bean
		AuthenticationManager authenticationManager(LdapContextSource contextSource) {
			LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
			factory.setUserDnPatterns("uid={0},ou=people");
			return factory.createAuthenticationManager();
		}

	}

	@EnableWebSecurity
	static class PortZeroConfig {

		@Bean
		EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
			EmbeddedLdapServerContextSourceFactoryBean factoryBean = EmbeddedLdapServerContextSourceFactoryBean
					.fromEmbeddedLdapServer();
			factoryBean.setPort(0);
			return factoryBean;
		}

		@Bean
		AuthenticationManager authenticationManager(LdapContextSource contextSource) {
			LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
			factory.setUserDnPatterns("uid={0},ou=people");
			return factory.createAuthenticationManager();
		}

	}

	@EnableWebSecurity
	static class CustomLdifAndRootConfig {

		@Bean
		EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
			EmbeddedLdapServerContextSourceFactoryBean factoryBean = EmbeddedLdapServerContextSourceFactoryBean
					.fromEmbeddedLdapServer();
			factoryBean.setLdif("classpath*:test-server2.xldif");
			factoryBean.setRoot("dc=monkeymachine,dc=co,dc=uk");
			return factoryBean;
		}

		@Bean
		AuthenticationManager authenticationManager(LdapContextSource contextSource) {
			LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
			factory.setUserDnPatterns("uid={0},ou=gorillas");
			return factory.createAuthenticationManager();
		}

	}

	@EnableWebSecurity
	static class CustomManagerDnConfig {

		@Bean
		EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
			EmbeddedLdapServerContextSourceFactoryBean factoryBean = EmbeddedLdapServerContextSourceFactoryBean
					.fromEmbeddedLdapServer();
			factoryBean.setManagerDn("uid=admin,ou=system");
			factoryBean.setManagerPassword("secret");
			return factoryBean;
		}

		@Bean
		AuthenticationManager authenticationManager(LdapContextSource contextSource) {
			LdapPasswordComparisonAuthenticationManagerFactory factory = new LdapPasswordComparisonAuthenticationManagerFactory(
					contextSource, NoOpPasswordEncoder.getInstance());
			factory.setUserDnPatterns("uid={0},ou=people");
			return factory.createAuthenticationManager();
		}

	}

	@EnableWebSecurity
	static class CustomManagerDnNoPasswordConfig {

		@Bean
		EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
			EmbeddedLdapServerContextSourceFactoryBean factoryBean = EmbeddedLdapServerContextSourceFactoryBean
					.fromEmbeddedLdapServer();
			factoryBean.setManagerDn("uid=admin,ou=system");
			return factoryBean;
		}

		@Bean
		AuthenticationManager authenticationManager(LdapContextSource contextSource) {
			LdapPasswordComparisonAuthenticationManagerFactory factory = new LdapPasswordComparisonAuthenticationManagerFactory(
					contextSource, NoOpPasswordEncoder.getInstance());
			factory.setUserDnPatterns("uid={0},ou=people");
			return factory.createAuthenticationManager();
		}

	}

}