largefiles.c 3.91 KB
Newer Older
1 2
#include "clar_libgit2.h"
#include "git2/odb_backend.h"
3 4 5 6
#include "hash.h"
#include "odb.h"

#define LARGEFILE_SIZE 5368709122
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

static git_repository *repo;
static git_odb *odb;

void test_odb_largefiles__initialize(void)
{
	repo = cl_git_sandbox_init("testrepo.git");
	cl_git_pass(git_repository_odb(&odb, repo));
}

void test_odb_largefiles__cleanup(void)
{
	git_odb_free(odb);
	cl_git_sandbox_cleanup();
}

static void writefile(git_oid *oid)
{
	static git_odb_stream *stream;
	git_buf buf = GIT_BUF_INIT;
	size_t i;

	for (i = 0; i < 3041; i++)
		cl_git_pass(git_buf_puts(&buf, "Hello, world.\n"));

32
	cl_git_pass(git_odb_open_wstream(&stream, odb, LARGEFILE_SIZE, GIT_OBJECT_BLOB));
33 34 35 36 37 38
	for (i = 0; i < 126103; i++)
		cl_git_pass(git_odb_stream_write(stream, buf.ptr, buf.size));

	cl_git_pass(git_odb_stream_finalize_write(oid, stream));

	git_odb_stream_free(stream);
39
	git_buf_dispose(&buf);
40 41 42 43 44 45 46 47 48 49 50 51
}

void test_odb_largefiles__write_from_memory(void)
{
	git_oid expected, oid;
	git_buf buf = GIT_BUF_INIT;
	size_t i;

#ifndef GIT_ARCH_64
	cl_skip();
#endif

52 53 54
	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
		!cl_is_env_set("GITTEST_INVASIVE_MEMORY") ||
		!cl_is_env_set("GITTEST_SLOW"))
55 56 57 58 59 60
		cl_skip();

	for (i = 0; i < (3041*126103); i++)
		cl_git_pass(git_buf_puts(&buf, "Hello, world.\n"));

	git_oid_fromstr(&expected, "3fb56989cca483b21ba7cb0a6edb229d10e1c26c");
61
	cl_git_pass(git_odb_write(&oid, odb, buf.ptr, buf.size, GIT_OBJECT_BLOB));
62 63 64 65 66 67 68 69

	cl_assert_equal_oid(&expected, &oid);
}

void test_odb_largefiles__streamwrite(void)
{
	git_oid expected, oid;

70 71 72 73
#ifndef GIT_ARCH_64
	cl_skip();
#endif

74 75
	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
		!cl_is_env_set("GITTEST_SLOW"))
76 77 78 79 80 81 82 83
		cl_skip();

	git_oid_fromstr(&expected, "3fb56989cca483b21ba7cb0a6edb229d10e1c26c");
	writefile(&oid);

	cl_assert_equal_oid(&expected, &oid);
}

84 85 86 87 88 89
void test_odb_largefiles__streamread(void)
{
	git_oid oid, read_oid;
	git_odb_stream *stream;
	char buf[10240];
	char hdr[64];
90
	size_t len, hdr_len, total = 0;
91
	git_hash_ctx hash;
92
	git_object_t type;
93
	int ret;
94

95 96 97 98
#ifndef GIT_ARCH_64
	cl_skip();
#endif

99 100 101 102 103 104 105 106 107
	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
		!cl_is_env_set("GITTEST_SLOW"))
		cl_skip();

	writefile(&oid);

	cl_git_pass(git_odb_open_rstream(&stream, &len, &type, odb, &oid));

	cl_assert_equal_sz(LARGEFILE_SIZE, len);
108
	cl_assert_equal_i(GIT_OBJECT_BLOB, type);
109 110

	cl_git_pass(git_hash_ctx_init(&hash));
111
	cl_git_pass(git_odb__format_object_header(&hdr_len, hdr, sizeof(hdr), len, type));
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

	cl_git_pass(git_hash_update(&hash, hdr, hdr_len));

	while ((ret = git_odb_stream_read(stream, buf, 10240)) > 0) {
		cl_git_pass(git_hash_update(&hash, buf, ret));
		total += ret;
	}

	cl_assert_equal_sz(LARGEFILE_SIZE, total);

	git_hash_final(&read_oid, &hash);

	cl_assert_equal_oid(&oid, &read_oid);

	git_hash_ctx_cleanup(&hash);
	git_odb_stream_free(stream);
}

130 131 132 133 134 135 136 137 138
void test_odb_largefiles__read_into_memory(void)
{
	git_oid oid;
	git_odb_object *obj;

#ifndef GIT_ARCH_64
	cl_skip();
#endif

139 140 141
	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
		!cl_is_env_set("GITTEST_INVASIVE_MEMORY") ||
		!cl_is_env_set("GITTEST_SLOW"))
142 143 144 145 146 147 148
		cl_skip();

	writefile(&oid);
	cl_git_pass(git_odb_read(&obj, odb, &oid));

	git_odb_object_free(obj);
}
149 150 151 152 153 154 155 156 157 158

void test_odb_largefiles__read_into_memory_rejected_on_32bit(void)
{
	git_oid oid;
	git_odb_object *obj = NULL;

#ifdef GIT_ARCH_64
	cl_skip();
#endif

159 160 161
	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
		!cl_is_env_set("GITTEST_INVASIVE_MEMORY") ||
		!cl_is_env_set("GITTEST_SLOW"))
162 163 164 165 166 167 168
		cl_skip();

	writefile(&oid);
	cl_git_fail(git_odb_read(&obj, odb, &oid));

	git_odb_object_free(obj);
}
169 170 171 172 173

void test_odb_largefiles__read_header(void)
{
	git_oid oid;
	size_t len;
174
	git_object_t type;
175

176 177 178 179 180 181
#ifndef GIT_ARCH_64
	cl_skip();
#endif

	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
		!cl_is_env_set("GITTEST_SLOW"))
182 183 184 185 186 187
		cl_skip();

	writefile(&oid);
	cl_git_pass(git_odb_read_header(&len, &type, odb, &oid));

	cl_assert_equal_sz(LARGEFILE_SIZE, len);
188
	cl_assert_equal_i(GIT_OBJECT_BLOB, type);
189
}