File: predicate_rw_test.c

package info (click to toggle)
liblicense 0.8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 6,428 kB
  • ctags: 868
  • sloc: sh: 9,739; ansic: 8,315; makefile: 859; xml: 178; cpp: 111; ruby: 41; python: 39
file content (148 lines) | stat: -rw-r--r-- 4,302 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * 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, Scott Shawcroft.
 * Copyright (C) 2007 Peter Miller
 */

#include "liblicense.h"

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

#define COPY_FILE_BUFSIZE 4096

static void copy_file(const char * infile, const char * outfile) {
	FILE * in_fd = fopen(infile, "r");
	assert(in_fd != NULL);
	FILE * out_fd = fopen(outfile, "w");
	assert(out_fd != NULL);
	
	int num_read_bytes = 0;
	int num_write_bytes = 0;

	char buf[COPY_FILE_BUFSIZE];
	
	while (! feof(in_fd)) {
		num_read_bytes = fread(buf, 1, COPY_FILE_BUFSIZE, in_fd);
		num_write_bytes = fwrite(buf, 1, num_read_bytes, out_fd);
		assert(num_read_bytes == num_write_bytes);
	}

	fclose(in_fd);
	fclose(out_fd);
}

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);

	/* Add the slash... */
	strncat(buffer, "/", remaining);
	remaining -= strlen("/");
	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";
	const char * png = "../tests/data_empty.png";
	
	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");
	copy_file(mp3, tempfile);

	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 */
	copy_file(pdf, tempfile);

	/* This PDF *only* work because the PDF has an XMP fragment
	 * already in it that can store the changes we ask for. 
	 * Sad but true - see http://code.creativecommons.org/issues/issue99
	 */
	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 PNG */
	path_join(tempfile, _LL_PATH_MAX, tempdir, "sample.png");
	copy_file(png, tempfile);
	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,
		     "http://example.com/give-us-all-all-your-money/");
	unlink(tempfile);

	return 0;
}