<commit_before>/*
 * Creative Commons has made the contents of this file
 * available under a CC-GNU-LGPL license:
 *
 * http://creativecommons.org/licenses/LGPL/2.1/
 *
 * A copy of the full license can be found as part of this
 * distribution in the file COPYING.
 *
 * You may use the liblicense software in accordance with the
 * terms of that license. You agree that you are solely
 * responsible for your use of the liblicense software and you
 * represent and warrant to Creative Commons that your use
 * of the liblicense software will comply with the CC-GNU-LGPL.
 *
 * Copyright 2007-2008, Creative Commons, www.creativecommons.org.
 * Copyright 2007, <NAME>.
 * Copyright (C) 2007 <NAME>
 */

#include "liblicense.h"

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>

static void get(const char * filename,
		const char * predicate,
		const char * expected_result) {

	char * got_result = ll_read(filename, predicate);
	if (expected_result == NULL) { // expected empty
		assert(got_result == NULL);
		/* success */
	}
	else {
		assert (got_result != NULL);
		/* otherwise, check the strings */
		assert (strcmp(got_result, expected_result) == 0);
		/* success */
	}
}
	
static void set_then_get(const char * filename,
			 const char * predicate,
			 const char * expected_result) {
	int success = ll_write(filename, predicate, expected_result);
	assert(success);

	get(filename, predicate, expected_result);
}

int main() {
	const char * mp3 = "../tests/data_empty.mp3";
	const char * pdf = "../tests/data_empty.pdf";
	
	ll_init();

	char * tempfile = malloc(4096);
	strcpy(tempfile, "/tmp/liblicense_test.XXXXXX");
	assert(mkstemp(tempfile) > -1);

	/* First, copy in the MP3 to the temp file */
	FILE * mp3_fd = fopen(mp3, "r");
	FILE * tempfile_fd = fopen(tempfile, "w");

	int bufsize = 4096;
	char * buf = malloc(bufsize);
	int num_read_bytes = 0;
	int num_write_bytes = 0;

	while (! feof(mp3_fd)) {
		num_read_bytes = fread(buf, 1, bufsize, mp3_fd);
		num_write_bytes = fwrite(buf, 1, num_read_bytes, tempfile_fd);
		assert(num_read_bytes == num_write_bytes);
	}

	/* Then rename it so that it ends in .mp3 */
	fclose(tempfile_fd);
	rename(tempfile, "/tmp/wtf.mp3"); /* FIXME this sucks */
	strcpy(tempfile, "/tmp/wtf.mp3");


	set_then_get(tempfile, LL_LICENSE, "http://creativecommons.org/licenses/by/2.0/");
	set_then_get(tempfile, LL_WEBSTATEMENT, "http://example.com/statement/");
	set_then_get(tempfile, LL_MORE_PERMISSIONS, NULL);
	unlink(tempfile);

	/* Copy pasta for PDF :-( */

	tempfile = malloc(4096);
	strcpy(tempfile, "/tmp/liblicense_test.XXXXXX");
	assert(mkstemp(tempfile) > -1);

	/* First, copy in the PDF to the temp file */
	FILE * pdf_fd = fopen(pdf, "r");
	tempfile_fd = fopen(tempfile, "w");

	bufsize = 4096;
	buf = malloc(bufsize);
	num_read_bytes = 0;
	num_write_bytes = 0;

	while (! feof(pdf_fd)) {
		num_read_bytes = fread(buf, 1, bufsize, pdf_fd);
		num_write_bytes = fwrite(buf, 1, num_read_bytes, tempfile_fd);
		assert(num_read_bytes == num_write_bytes);
	}

	/* Then rename it so that it ends in .pdf */
	fclose(tempfile_fd);
	rename(tempfile, "/tmp/wtf.pdf"); /* FIXME this sucks */
	strcpy(tempfile, "/tmp/wtf.pdf");


	set_then_get("/tmp/omg.pdf", LL_LICENSE, "http://creativecommons.org/licenses/by/2.0/");
	set_then_get("/tmp/omg.pdf", LL_WEBSTATEMENT, "http://example.com/statement/");
	set_then_get("/tmp/omg.pdf", LL_MORE_PERMISSIONS, NULL);
	unlink(tempfile);

	return 0;
}<commit_msg>Use a safe mkdtemp() directory in predicate_rw_test<commit_after>/*
 * Creative Commons has made the contents of this file
 * available under a CC-GNU-LGPL license:
 *
 * http://creativecommons.org/licenses/LGPL/2.1/
 *
 * A copy of the full license can be found as part of this
 * distribution in the file COPYING.
 *
 * You may use the liblicense software in accordance with the
 * terms of that license. You agree that you are solely
 * responsible for your use of the liblicense software and you
 * represent and warrant to Creative Commons that your use
 * of the liblicense software will comply with the CC-GNU-LGPL.
 *
 * Copyright 2007-2008, Creative Commons, www.creativecommons.org.
 * Copyright 2007, <NAME>.
 * Copyright (C) 2007 <NAME>
 */

#include "liblicense.h"

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>

static const int _LL_PATH_MAX = 4096;

static void get(const char * filename,
		const char * predicate,
		const char * expected_result) {

	char * got_result = ll_read(filename, predicate);
	if (expected_result == NULL) { /* expected empty */
		assert(got_result == NULL);
		/* success */
	}
	else {
		assert (got_result != NULL);
		/* otherwise, check the strings */
		assert (strcmp(got_result, expected_result) == 0);
		/* success */
	}
}
	
static void set_then_get(const char * filename,
			 const char * predicate,
			 const char * expected_result) {
	int success = ll_write(filename, predicate, expected_result);
	assert(success);

	get(filename, predicate, expected_result);
}

void path_join(char * buffer, int buffer_size, char * dirname, char * basename) {
	/* Honestly, is this what C programmers do? */
	int remaining = buffer_size - 1; /* Zero padded */

	/* Zero that buffer */
	memset(buffer, 0, buffer_size);

	/* Jam the dirname in */
	strncat(buffer, dirname, remaining);
	remaining -= strlen(dirname);
	assert (remaining > 0);

	/* Slide the basename on */
	strncat(buffer, basename, remaining);
}

int main() {
	const char * mp3 = "../tests/data_empty.mp3";
	const char * pdf = "../tests/data_empty.pdf";
	
	ll_init();

	char * tempfile = malloc(_LL_PATH_MAX);
	char * tempdir = malloc(_LL_PATH_MAX);
	strcpy(tempdir, "/tmp/liblicense_test_dir.XXXXXX");
	assert(mkdtemp(tempdir) > -1);

	/* First, copy in the MP3 to the temp file */
	path_join(tempfile, _LL_PATH_MAX, tempdir, "data_empty.mp3");
	FILE * mp3_fd = fopen(mp3, "r");
	FILE * tempfile_fd = fopen(tempfile, "w");

	int bufsize = 4096;
	char * buf = malloc(bufsize);
	int num_read_bytes = 0;
	int num_write_bytes = 0;

	while (! feof(mp3_fd)) {
		num_read_bytes = fread(buf, 1, bufsize, mp3_fd);
		num_write_bytes = fwrite(buf, 1, num_read_bytes, tempfile_fd);
		assert(num_read_bytes == num_write_bytes);
	}

	set_then_get(tempfile, LL_LICENSE, "http://creativecommons.org/licenses/by/2.0/");
	set_then_get(tempfile, LL_WEBSTATEMENT, "http://example.com/statement/");
	set_then_get(tempfile, LL_MORE_PERMISSIONS, NULL);
	unlink(tempfile);

	/* Copy pasta for PDF :-( */

	path_join(tempfile, _LL_PATH_MAX, tempdir, "data_empty.pdf");
	/* First, copy in the PDF to the temp file */
	FILE * pdf_fd = fopen(pdf, "r");
	tempfile_fd = fopen(tempfile, "w");

	bufsize = 4096;
	buf = malloc(bufsize);
	num_read_bytes = 0;
	num_write_bytes = 0;

	while (! feof(pdf_fd)) {
		num_read_bytes = fread(buf, 1, bufsize, pdf_fd);
		num_write_bytes = fwrite(buf, 1, num_read_bytes, tempfile_fd);
		assert(num_read_bytes == num_write_bytes);
	}

	set_then_get(tempfile, LL_LICENSE, "http://creativecommons.org/licenses/by/2.0/");
	set_then_get(tempfile, LL_WEBSTATEMENT, "http://example.com/statement/");
	set_then_get(tempfile, LL_MORE_PERMISSIONS, NULL);
	unlink(tempfile);

	return 0;
}
{'fraction_non_alphanumeric': 0.13649564375605033, 'fraction_numerical': 0.016042041211450697, 'mean_word_length': 4.248185776487663, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4642305', 'n_tokens_mistral': 2736, 'n_tokens_neox': 2550, 'n_words': 683}