<commit_before>#include <stdio.h>
#include <locale.h>
#include <glib.h>

#include <context.h>
#include <utils.h>
#include "signature.h"
#include "common.h"

static void signature_sign(void)
{
	GBytes *content = read_file("test/openssl-ca/manifest", NULL);
	GBytes *sig = NULL;
	GError *error = NULL;
	g_assert_nonnull(content);

	// Test valid signing
	sig = cms_sign(content,
		       r_context()->certpath,
		       r_context()->keypath,
		       &error);
	g_assert_nonnull(sig);
	g_assert_null(error);

	g_bytes_unref(sig);

	// Test signing fail with invalid key
	sig = cms_sign(content,
		       r_context()->certpath,
		       "test/random.dat",
		       &error);
	g_assert_null(sig);
	g_assert_error(error, R_SIGNATURE_ERROR, R_SIGNATURE_ERROR_PARSE_ERROR);

	g_clear_error(&error);

	// Test signing fail with invalid cert
	sig = cms_sign(content,
		       "test/random.dat",
		       r_context()->keypath,
		       &error);
	g_assert_null(sig);
	g_assert_error(error, R_SIGNATURE_ERROR, R_SIGNATURE_ERROR_PARSE_ERROR);

	g_clear_error(&error);

	g_bytes_unref(content);
}

static void signature_sign_file(void)
{
	GBytes *sig = NULL;
	GError *error = NULL;

	// Test valid file
	sig = cms_sign_file("test/openssl-ca/manifest",
			    r_context()->certpath,
			    r_context()->keypath,
			    &error);
	g_assert_nonnull(sig);
	g_assert_null(error);

	g_bytes_unref(sig);
	g_clear_error(&error);


	// Test non-existing file
	sig = cms_sign_file("path/to/nonexisting/file",
			    r_context()->certpath,
			    r_context()->keypath,
			    &error);
	g_assert_null(sig);
	g_assert_error(error, G_FILE_ERROR, G_FILE_ERROR_NOENT);

	g_bytes_unref(sig);
	g_clear_error(&error);

	// Test invalid certificate
	sig = cms_sign_file("test/openssl-ca/manifest",
			    NULL,
			    r_context()->keypath,
			    &error);
	g_assert_null(sig);
	g_assert_error(error, R_SIGNATURE_ERROR, R_SIGNATURE_ERROR_LOAD_FAILED);

	g_bytes_unref(sig);
	g_clear_error(&error);
}

static void signature_verify(void)
{
	GBytes *content = read_file("test/openssl-ca/manifest", NULL);
	GBytes *sig = read_file("test/openssl-ca/manifest-r1.sig", NULL);
	g_assert_nonnull(content);
	g_assert_nonnull(sig);
	g_assert_true(cms_verify(content, sig, NULL, NULL, NULL));
	g_bytes_unref(content);
	g_bytes_unref(sig);
}

static void signature_verify_file(void)
{
	GError *error = NULL;
	GBytes *sig = read_file("test/openssl-ca/manifest-r1.sig", NULL);
	GBytes *isig = read_file("test/random.dat", NULL);

	g_assert_nonnull(sig);
	g_assert_nonnull(isig);

	// Test valid manifest
	g_assert_true(cms_verify_file("test/openssl-ca/manifest", sig, 0, NULL, NULL, &error));
	g_assert_null(error);

	// Test non-existing file
	g_assert_false(cms_verify_file("path/to/nonexisting/file", sig, 0, NULL, NULL, &error));
	g_assert_error(error, G_FILE_ERROR, G_FILE_ERROR_NOENT);

	g_clear_error(&error);

	// Test valid manifest against invalid signature
	g_assert_false(cms_verify_file("test/openssl-ca/manifest", isig, 0, NULL, NULL, &error));
	g_assert_error(error, R_SIGNATURE_ERROR, R_SIGNATURE_ERROR_PARSE);

	g_clear_error(&error);

	g_bytes_unref(sig);
	g_bytes_unref(isig);
}

static void signature_loopback(void)
{
	GBytes *content = read_file("test/openssl-ca/manifest", NULL);
	GBytes *sig = NULL;
	g_assert_nonnull(content);
	sig = cms_sign(content,
		       r_context()->certpath,
		       r_context()->keypath,
		       NULL);
	g_assert_nonnull(sig);
	g_assert_true(cms_verify(content, sig, NULL, NULL, NULL));
	((char *)g_bytes_get_data(content, NULL))[0] = 0x00;
	g_assert_false(cms_verify(content, sig, NULL, NULL, NULL));
	g_bytes_unref(content);
	g_bytes_unref(sig);
}

int main(int argc, char *argv[])
{<commit_msg>test/signature: more targeted signature tests<commit_after>#include <stdio.h>
#include <locale.h>
#include <glib.h>

#include <context.h>
#include <utils.h>
#include "signature.h"
#include "common.h"

static void signature_sign(void)
{
	GBytes *content = read_file("test/openssl-ca/manifest", NULL);
	GBytes *sig = NULL;
	GError *error = NULL;
	g_assert_nonnull(content);

	// Test valid signing
	sig = cms_sign(content,
		       r_context()->certpath,
		       r_context()->keypath,
		       &error);
	g_assert_nonnull(sig);
	g_assert_null(error);

	g_bytes_unref(sig);

	// Test signing fail with invalid key
	sig = cms_sign(content,
		       r_context()->certpath,
		       "test/random.dat",
		       &error);
	g_assert_null(sig);
	g_assert_error(error, R_SIGNATURE_ERROR, R_SIGNATURE_ERROR_PARSE_ERROR);

	g_clear_error(&error);

	// Test signing fail with invalid cert
	sig = cms_sign(content,
		       "test/random.dat",
		       r_context()->keypath,
		       &error);
	g_assert_null(sig);
	g_assert_error(error, R_SIGNATURE_ERROR, R_SIGNATURE_ERROR_PARSE_ERROR);

	g_clear_error(&error);

	g_bytes_unref(content);
}

static void signature_sign_file(void)
{
	GBytes *sig = NULL;
	GError *error = NULL;

	// Test valid file
	sig = cms_sign_file("test/openssl-ca/manifest",
			    r_context()->certpath,
			    r_context()->keypath,
			    &error);
	g_assert_nonnull(sig);
	g_assert_null(error);

	g_bytes_unref(sig);
	g_clear_error(&error);


	// Test non-existing file
	sig = cms_sign_file("path/to/nonexisting/file",
			    r_context()->certpath,
			    r_context()->keypath,
			    &error);
	g_assert_null(sig);
	g_assert_error(error, G_FILE_ERROR, G_FILE_ERROR_NOENT);

	g_bytes_unref(sig);
	g_clear_error(&error);

	// Test invalid certificate
	sig = cms_sign_file("test/openssl-ca/manifest",
			    NULL,
			    r_context()->keypath,
			    &error);
	g_assert_null(sig);
	g_assert_error(error, R_SIGNATURE_ERROR, R_SIGNATURE_ERROR_LOAD_FAILED);

	g_bytes_unref(sig);
	g_clear_error(&error);
}

static void signature_verify(void)
{
	GError *error = NULL;
	CMS_ContentInfo *cms = NULL;
	X509_STORE *store = NULL;

	GBytes *content = read_file("test/openssl-ca/manifest", NULL);
	GBytes *sig = read_file("test/openssl-ca/manifest-r1.sig", NULL);
	GBytes *isig = read_file("test/random.dat", NULL);
	g_assert_nonnull(content);
	g_assert_nonnull(sig);
	g_assert_nonnull(isig);

	g_assert_true(cms_verify(content, sig, &cms, &store, &error));
	g_assert_no_error(error);
	g_assert_nonnull(cms);
	g_assert_nonnull(store);

	g_clear_pointer(&store, X509_STORE_free);
	g_clear_pointer(&cms, CMS_ContentInfo_free);

	// Test against invalid signature
	g_assert_false(cms_verify(content, isig, &cms, &store, &error));
	g_assert_error(error, R_SIGNATURE_ERROR, R_SIGNATURE_ERROR_PARSE);
	g_assert_null(cms);
	g_assert_null(store);

	g_bytes_unref(content);
	g_bytes_unref(sig);
	g_bytes_unref(isig);
}

static void signature_verify_file(void)
{
	CMS_ContentInfo *cms = NULL;
	X509_STORE *store = NULL;
	GError *error = NULL;

	GBytes *sig = read_file("test/openssl-ca/manifest-r1.sig", NULL);

	g_assert_nonnull(sig);

	// Test valid manifest
	g_assert_true(cms_verify_file("test/openssl-ca/manifest", sig, 0, &cms, &store, &error));
	g_assert_null(error);
	g_assert_nonnull(cms);
	g_assert_nonnull(store);

	g_clear_pointer(&store, X509_STORE_free);
	g_clear_pointer(&cms, CMS_ContentInfo_free);

	// Test valid manifest with invalid size limit
	g_assert_false(cms_verify_file("test/openssl-ca/manifest", sig, 42, &cms, &store, &error));
	g_assert_error(error, R_SIGNATURE_ERROR, R_SIGNATURE_ERROR_INVALID);
	g_assert_null(cms);
	g_assert_null(store);

	g_clear_error(&error);

	// Test non-existing file
	g_assert_false(cms_verify_file("path/to/nonexisting/file", sig, 0, &cms, &store, &error));
	g_assert_error(error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
	g_assert_null(cms);
	g_assert_null(store);

	g_clear_error(&error);

	g_bytes_unref(sig);
}

static void signature_loopback(void)
{
	GBytes *content = read_file("test/openssl-ca/manifest", NULL);
	GBytes *sig = NULL;
	g_assert_nonnull(content);
	sig = cms_sign(content,
		       r_context()->certpath,
		       r_context()->keypath,
		       NULL);
	g_assert_nonnull(sig);
	g_assert_true(cms_verify(content, sig, NULL, NULL, NULL));
	((char *)g_bytes_get_data(content, NULL))[0] = 0x00;
	g_assert_false(cms_verify(content, sig, NULL, NULL, NULL));
	g_bytes_unref(content);
	g_bytes_unref(sig);
}

int main(int argc, char *argv[])
{
{'fraction_non_alphanumeric': 0.13734115347018572, 'fraction_numerical': 0.003787878787878788, 'mean_word_length': 5.0584752035529235, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2080858', 'n_tokens_mistral': 3326, 'n_tokens_neox': 3108, 'n_words': 523}