<commit_before>/*
 * Copyright (C) 2011-2013 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */


#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <selinux/selinux.h>
#include <selinux/context.h>

#include "internal.h"
#include "testutils.h"
#include "viralloc.h"
#include "virlog.h"
#include "virerror.h"
#include "virfile.h"
#include "security/security_manager.h"
#include "virstring.h"

#define VIR_FROM_THIS VIR_FROM_NONE

VIR_LOG_INIT("tests.securityselinuxtest");

struct testSELinuxGenLabelData {
    virSecurityManagerPtr mgr;

    const char *pidcon;

    bool dynamic;
    const char *label;
    const char *baselabel;

    const char *user;
    const char *role;
    const char *imagerole;
    const char *type;
    const char *imagetype;

    int sensMin;
    int sensMax;
    int catMin;
    int catMax;
};

static virDomainDefPtr
testBuildDomainDef(bool dynamic,
                   const char *label,
                   const char *baselabel)
{
    virDomainDefPtr def;
    virSecurityLabelDefPtr secdef;

    if (!(def = virDomainDefNew()))
        goto error;

    if (VIR_ALLOC_N(def->seclabels, 1) < 0)
        goto error;

    if (VIR_ALLOC(secdef) < 0)
        goto error;

    def->virtType = VIR_DOMAIN_VIRT_KVM;
    def->seclabels[0] = secdef;
    def->seclabels[0]->type = dynamic ? VIR_DOMAIN_SECLABEL_DYNAMIC : VIR_DOMAIN_SECLABEL_STATIC;

    if (label &&
        VIR_STRDUP(def->seclabels[0]->label, label) < 0)
        goto error;

    if (baselabel &&
        VIR_STRDUP(def->seclabels[0]->baselabel, baselabel) < 0)
        goto error;

    return def;

 error:
    virDomainDefFree(def);
    return NULL;
}


static bool
testSELinuxCheckCon(context_t con,
                    const char *user,
                    const char *role,
                    const char *type,
                    int sensMin,
                    int sensMax ATTRIBUTE_UNUSED,<commit_msg>Fix building domain def in securityselinuxtest<commit_after>/*
 * Copyright (C) 2011-2013 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */


#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <selinux/selinux.h>
#include <selinux/context.h>

#include "internal.h"
#include "testutils.h"
#include "viralloc.h"
#include "virlog.h"
#include "virerror.h"
#include "virfile.h"
#include "security/security_manager.h"
#include "virstring.h"

#define VIR_FROM_THIS VIR_FROM_NONE

VIR_LOG_INIT("tests.securityselinuxtest");

struct testSELinuxGenLabelData {
    virSecurityManagerPtr mgr;

    const char *pidcon;

    bool dynamic;
    const char *label;
    const char *baselabel;

    const char *user;
    const char *role;
    const char *imagerole;
    const char *type;
    const char *imagetype;

    int sensMin;
    int sensMax;
    int catMin;
    int catMax;
};

static virDomainDefPtr
testBuildDomainDef(bool dynamic,
                   const char *label,
                   const char *baselabel)
{
    virDomainDefPtr def;
    virSecurityLabelDefPtr secdef;

    if (!(def = virDomainDefNew()))
        goto error;

    def->virtType = VIR_DOMAIN_VIRT_KVM;
    if (VIR_ALLOC_N(def->seclabels, 1) < 0)
        goto error;

    if (VIR_ALLOC(secdef) < 0)
        goto error;

    if (VIR_STRDUP(secdef->model, "selinux") < 0)
        goto error;

    secdef->type = dynamic ? VIR_DOMAIN_SECLABEL_DYNAMIC : VIR_DOMAIN_SECLABEL_STATIC;
    if (label &&
        VIR_STRDUP(secdef->label, label) < 0)
        goto error;

    if (baselabel &&
        VIR_STRDUP(secdef->baselabel, baselabel) < 0)
        goto error;

    def->seclabels[0] = secdef;
    def->nseclabels++;
    return def;

 error:
    virDomainDefFree(def);
    return NULL;
}


static bool
testSELinuxCheckCon(context_t con,
                    const char *user,
                    const char *role,
                    const char *type,
                    int sensMin,
                    int sensMax ATTRIBUTE_UNUSED,
{'fraction_non_alphanumeric': 0.09408652937842434, 'fraction_numerical': 0.006801435858681277, 'mean_word_length': 2.816870944484499, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4199220', 'n_tokens_mistral': 1773, 'n_tokens_neox': 1682, 'n_words': 525}