mirror of
https://github.com/clearlinux/cve-check-tool.git
synced 2026-09-06 21:51:33 +00:00
This resolves issue #6 - but would require further testing Signed-off-by: Ikey Doherty <michael.i.doherty@intel.com>
260 lines
8.3 KiB
C
260 lines
8.3 KiB
C
/*
|
|
* This file is part of cve-check-tool
|
|
* Copyright (C) 2015 Intel Corporation
|
|
*
|
|
* cve-check-tool is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*/
|
|
|
|
#define _GNU_SOURCE
|
|
#include <check.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#include "cve-string.c"
|
|
#include "util.h"
|
|
#include "util.c"
|
|
#include "rpm.c"
|
|
#include "eopkg.c"
|
|
#include "pkgbuild.c"
|
|
|
|
#include "config.h"
|
|
|
|
static int add_count = 0;
|
|
|
|
/**
|
|
* Kept here as a no-op for now (linking)
|
|
*/
|
|
void cve_add_package(__attribute__((unused)) const char *path)
|
|
{
|
|
add_count++;
|
|
}
|
|
|
|
/**
|
|
* RPM .spec check
|
|
*/
|
|
START_TEST(cve_rpm_test)
|
|
{
|
|
struct source_package_t *pkg = NULL;
|
|
|
|
pkg = rpm_inspect_spec(TOP_DIR "/tests/dummy_data/rpm/package_does_not_exist.spec");
|
|
fail_if(pkg, "Incorrectly succeeded on non-existing spec!");
|
|
|
|
pkg = rpm_inspect_spec(TOP_DIR "/tests/dummy_data/rpm/package2.spec");
|
|
fail_if(!pkg, "Failed to inspect RPM spec!");
|
|
package_free(pkg);
|
|
pkg = NULL;
|
|
|
|
if (!g_find_program_in_path("rpm")) {
|
|
fprintf(stderr, "Unable to perform SRPM tests!");
|
|
goto rpm;
|
|
}
|
|
|
|
pkg = rpm_inspect_srpm(TOP_DIR "/tests/dummy_data/rpm/", "package", "1.", "5" );
|
|
fail_if(!pkg, "Failed to inspect source RPM!");
|
|
fail_if(!srpm_is_patched(pkg, "CVE-2014-5461"), "SRPM patch test failed");
|
|
fail_if(!g_str_equal(pkg->name, "package"),
|
|
"Invalid SRPM package name");
|
|
fail_if(!g_str_equal(pkg->version, "1."),
|
|
"Invalid SRPM package version");
|
|
|
|
fail_if(srpm_is_ignored(pkg, "CVE-2013-0012"),
|
|
"Incorrectly detected non-ignored CVE");
|
|
|
|
package_free(pkg);
|
|
pkg = NULL;
|
|
|
|
pkg = rpm_inspect_srpm(TOP_DIR "/tests/dummy_data/rpm/", "invalid_package", "1.", "5" );
|
|
fail_if(pkg, "Incorrectly succeeded at missing source RPM!");
|
|
|
|
rpm:
|
|
pkg = rpm_inspect_spec(TOP_DIR "/tests/dummy_data/rpm/package.spec");
|
|
fail_if(!pkg, "Failed to inspect RPM spec!");
|
|
|
|
fail_if(!g_str_equal(pkg->name, "test-package"),
|
|
"Invalid RPM package name");
|
|
fail_if(!g_str_equal(pkg->version, "1.1.0"),
|
|
"Invalid RPM package version");
|
|
fail_if(pkg->release != 5, "Invalid RPM package release");
|
|
|
|
fail_if(!rpm_is_patched(pkg, "CVE-2014-5461"),
|
|
"Failed to detect RPM CVE patch");
|
|
|
|
fail_if(!rpm_is_ignored(pkg, "CVE-2013-4459"),
|
|
"Failed to detect ignored CVE");
|
|
fail_if(rpm_is_ignored(pkg, "CVE-2013-0012"),
|
|
"Incorrectly detected non-ignored CVE");
|
|
|
|
package_free(pkg);
|
|
}
|
|
END_TEST
|
|
|
|
/**
|
|
* Solus Project pspec.xml check
|
|
*/
|
|
START_TEST(cve_eopkg_test)
|
|
{
|
|
struct source_package_t *pkg = NULL;
|
|
|
|
pkg = pkgbuild_inspect_spec(TOP_DIR "/tests/dummy_data/eopkg/not-exist");
|
|
fail_if(pkg, "Non-existent eopkg should be NULL");
|
|
|
|
pkg = eopkg_inspect_pspec(TOP_DIR "/tests/dummy_data/eopkg/pspec.xml");
|
|
fail_if(!pkg, "Failed to inspect eopkg spec!");
|
|
|
|
fail_if(!g_str_equal(pkg->name, "budgie-desktop"),
|
|
"Invalid eopkg package name");
|
|
fail_if(!g_str_equal(pkg->version, "8.1"),
|
|
"Invalid eopkg package version");
|
|
fail_if(pkg->release != 41, "Invalid eopkg package release");
|
|
|
|
fail_if(!eopkg_is_patched(pkg, "CVE-2014-5461"),
|
|
"Failed to detect eopkg CVE patch");
|
|
|
|
fail_if(!eopkg_is_ignored(pkg, "CVE-2013-4459"),
|
|
"Failed to detect ignored CVE");
|
|
fail_if(eopkg_is_ignored(pkg, "CVE-2013-0012"),
|
|
"Incorrectly detected non-ignored CVE");
|
|
|
|
package_free(pkg);
|
|
}
|
|
END_TEST
|
|
|
|
/**
|
|
* PKGBUILD (Arch Linux) check
|
|
*/
|
|
START_TEST(cve_pkgbuild_test)
|
|
{
|
|
struct source_package_t *pkg = NULL;
|
|
|
|
pkg = pkgbuild_inspect_spec(TOP_DIR "/tests/dummy_data/pkgbuild/not-exist");
|
|
fail_if(pkg, "Non-existent PKGBUILD should be NULL");
|
|
|
|
pkg = pkgbuild_inspect_spec(TOP_DIR "/tests/dummy_data/pkgbuild/PKGBUILD");
|
|
fail_if(!pkg, "Failed to inspect PKGBUILD spec!");
|
|
|
|
fail_if(!g_str_equal(pkg->name, "my-test-package"),
|
|
"Invalid PKGBUILD package name");
|
|
fail_if(!g_str_equal(pkg->version, "1.0.3"),
|
|
"Invalid PKGBUILD package version");
|
|
fail_if(pkg->release != 10, "Invalid PKGBUILD package release");
|
|
|
|
fail_if(!pkgbuild_is_patched(pkg, "CVE-2014-5461"),
|
|
"Failed to detect eopkg CVE patch");
|
|
|
|
package_free(pkg);
|
|
}
|
|
END_TEST
|
|
|
|
/**
|
|
* General packaging tests
|
|
*/
|
|
START_TEST(cve_packaging_test)
|
|
{
|
|
PackageType t = PACKAGE_TYPE_UNKNOWN;
|
|
|
|
fail_if(!is_package_list(TOP_DIR "/tests/dummy_data/rpm/packages"),
|
|
"Failed to detect packages list");
|
|
fail_if(is_package_list(TOP_DIR "/tests/dummy_data/rpm/package.spec"),
|
|
"Incorrectly detecting non-package list as package list");
|
|
|
|
t = guess_package_type(TOP_DIR "/tests/dummy_data/eopkg/pspec.xml", false);
|
|
fail_if(t != PACKAGE_TYPE_EOPKG, "Incorrect detection of eopkg pspec");
|
|
t = PACKAGE_TYPE_UNKNOWN;
|
|
|
|
t = guess_package_type(TOP_DIR "/tests/dummy_data/eopkg", true);
|
|
fail_if(t != PACKAGE_TYPE_EOPKG, "Incorrect recursive detection of eopkg format");
|
|
t = PACKAGE_TYPE_UNKNOWN;
|
|
|
|
t = guess_package_type(TOP_DIR "/tests/dummy_data/rpm/package.spec", false);
|
|
fail_if(t != PACKAGE_TYPE_RPM, "Incorrect detection of RPM spec");
|
|
t = PACKAGE_TYPE_UNKNOWN;
|
|
|
|
t = guess_package_type(TOP_DIR "/tests/dummy_data/pkgbuild/PKGBUILD", false);
|
|
fail_if(t != PACKAGE_TYPE_PKGBUILD, "Incorrect detection of PKGBUILD spec");
|
|
t = PACKAGE_TYPE_UNKNOWN;
|
|
|
|
t = guess_package_type(TOP_DIR "/tests/dummy_data/rpm", true);
|
|
fail_if(t != PACKAGE_TYPE_RPM, "Incorrect recursive detection of RPM format");
|
|
t = PACKAGE_TYPE_UNKNOWN;
|
|
|
|
t = guess_package_type(TOP_DIR "/tests/dummy_data/package.does-not-exist", false);
|
|
fail_if(t != PACKAGE_TYPE_UNKNOWN, "Incorrect unknown package type detection");
|
|
|
|
add_count = 0;
|
|
eopkg_locate_sources(TOP_DIR "/tests/dummy_data/eopkg");
|
|
fail_if(add_count != 1, "Failed to locate eopkg sources");
|
|
|
|
add_count = 0;
|
|
rpm_locate_sources(TOP_DIR "/tests/dummy_data/rpm");
|
|
fail_if(add_count != 2, "Failed to locate RPM sources");
|
|
|
|
add_count = 0;
|
|
pkgbuild_locate_sources(TOP_DIR "/tests/dummy_data/pkgbuild");
|
|
fail_if(add_count != 1, "Failed to locate PKGBUILD sources");
|
|
}
|
|
END_TEST
|
|
|
|
|
|
/**
|
|
* CVE Mapping test
|
|
*/
|
|
START_TEST(cve_mapping_test)
|
|
{
|
|
char *vendor = NULL, *product = NULL;
|
|
|
|
fail_if(!load_cve_mapping(TOP_DIR "/tests/dummy_data/cve.ini", &product, &vendor),
|
|
"Failed to load CVE mapping file");
|
|
fail_if(!vendor, "Vendor not set");
|
|
fail_if(!product, "Product not set");
|
|
|
|
fail_if(!g_str_equal(vendor, "dummy-vendor"), "Incorrect mapping vendor");
|
|
fail_if(!g_str_equal(product, "dummy-product"), "Incorrect mapping product");
|
|
free(vendor);
|
|
free(product);
|
|
|
|
fail_if(load_cve_mapping(TOP_DIR "/tests/dummy_data/cve.ini", NULL, NULL),
|
|
"Null-check CVE mapping load failed");
|
|
fail_if(load_cve_mapping(TOP_DIR "/tests/dummy_data/cve.no-exist.ini", NULL, NULL),
|
|
"Loaded non existent mapping file");
|
|
}
|
|
END_TEST
|
|
|
|
static Suite *core_suite(void)
|
|
{
|
|
Suite *s = NULL;
|
|
TCase *tc = NULL;
|
|
|
|
s = suite_create("cve_packaging");
|
|
tc = tcase_create("cve_packaging_functions");
|
|
tcase_add_test(tc, cve_packaging_test);
|
|
tcase_add_test(tc, cve_rpm_test);
|
|
tcase_add_test(tc, cve_eopkg_test);
|
|
tcase_add_test(tc, cve_pkgbuild_test);
|
|
tcase_add_test(tc, cve_mapping_test);
|
|
suite_add_tcase(s, tc);
|
|
|
|
return s;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
Suite *s;
|
|
SRunner *sr;
|
|
int fail;
|
|
|
|
s = core_suite();
|
|
sr = srunner_create(s);
|
|
srunner_run_all(sr, CK_VERBOSE);
|
|
fail = srunner_ntests_failed(sr);
|
|
srunner_free(sr);
|
|
|
|
if (fail > 0) {
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|