1 #include "clar_libgit2.h"
2 #include "git2/odb_backend.h"
3 #include "hash.h"
4 #include "odb.h"
5 
6 #define LARGEFILE_SIZE 5368709122
7 
8 static git_repository *repo;
9 static git_odb *odb;
10 
test_odb_largefiles__initialize(void)11 void test_odb_largefiles__initialize(void)
12 {
13 	repo = cl_git_sandbox_init("testrepo.git");
14 	cl_git_pass(git_repository_odb(&odb, repo));
15 }
16 
test_odb_largefiles__cleanup(void)17 void test_odb_largefiles__cleanup(void)
18 {
19 	git_odb_free(odb);
20 	cl_git_sandbox_cleanup();
21 }
22 
writefile(git_oid * oid)23 static void writefile(git_oid *oid)
24 {
25 	static git_odb_stream *stream;
26 	git_buf buf = GIT_BUF_INIT;
27 	size_t i;
28 
29 	for (i = 0; i < 3041; i++)
30 		cl_git_pass(git_buf_puts(&buf, "Hello, world.\n"));
31 
32 	cl_git_pass(git_odb_open_wstream(&stream, odb, LARGEFILE_SIZE, GIT_OBJECT_BLOB));
33 	for (i = 0; i < 126103; i++)
34 		cl_git_pass(git_odb_stream_write(stream, buf.ptr, buf.size));
35 
36 	cl_git_pass(git_odb_stream_finalize_write(oid, stream));
37 
38 	git_odb_stream_free(stream);
39 	git_buf_dispose(&buf);
40 }
41 
test_odb_largefiles__write_from_memory(void)42 void test_odb_largefiles__write_from_memory(void)
43 {
44 	git_oid expected, oid;
45 	git_buf buf = GIT_BUF_INIT;
46 	size_t i;
47 
48 #ifndef GIT_ARCH_64
49 	cl_skip();
50 #endif
51 
52 	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
53 		!cl_is_env_set("GITTEST_INVASIVE_MEMORY") ||
54 		!cl_is_env_set("GITTEST_SLOW"))
55 		cl_skip();
56 
57 	for (i = 0; i < (3041*126103); i++)
58 		cl_git_pass(git_buf_puts(&buf, "Hello, world.\n"));
59 
60 	git_oid_fromstr(&expected, "3fb56989cca483b21ba7cb0a6edb229d10e1c26c");
61 	cl_git_pass(git_odb_write(&oid, odb, buf.ptr, buf.size, GIT_OBJECT_BLOB));
62 
63 	cl_assert_equal_oid(&expected, &oid);
64 }
65 
test_odb_largefiles__streamwrite(void)66 void test_odb_largefiles__streamwrite(void)
67 {
68 	git_oid expected, oid;
69 
70 #ifndef GIT_ARCH_64
71 	cl_skip();
72 #endif
73 
74 	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
75 		!cl_is_env_set("GITTEST_SLOW"))
76 		cl_skip();
77 
78 	git_oid_fromstr(&expected, "3fb56989cca483b21ba7cb0a6edb229d10e1c26c");
79 	writefile(&oid);
80 
81 	cl_assert_equal_oid(&expected, &oid);
82 }
83 
test_odb_largefiles__streamread(void)84 void test_odb_largefiles__streamread(void)
85 {
86 	git_oid oid, read_oid;
87 	git_odb_stream *stream;
88 	char buf[10240];
89 	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 #ifndef GIT_ARCH_64
96 	cl_skip();
97 #endif
98 
99 	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
100 		!cl_is_env_set("GITTEST_SLOW"))
101 		cl_skip();
102 
103 	writefile(&oid);
104 
105 	cl_git_pass(git_odb_open_rstream(&stream, &len, &type, odb, &oid));
106 
107 	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 	cl_git_pass(git_hash_update(&hash, hdr, hdr_len));
114 
115 	while ((ret = git_odb_stream_read(stream, buf, 10240)) > 0) {
116 		cl_git_pass(git_hash_update(&hash, buf, ret));
117 		total += ret;
118 	}
119 
120 	cl_assert_equal_sz(LARGEFILE_SIZE, total);
121 
122 	git_hash_final(&read_oid, &hash);
123 
124 	cl_assert_equal_oid(&oid, &read_oid);
125 
126 	git_hash_ctx_cleanup(&hash);
127 	git_odb_stream_free(stream);
128 }
129 
test_odb_largefiles__read_into_memory(void)130 void test_odb_largefiles__read_into_memory(void)
131 {
132 	git_oid oid;
133 	git_odb_object *obj;
134 
135 #ifndef GIT_ARCH_64
136 	cl_skip();
137 #endif
138 
139 	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
140 		!cl_is_env_set("GITTEST_INVASIVE_MEMORY") ||
141 		!cl_is_env_set("GITTEST_SLOW"))
142 		cl_skip();
143 
144 	writefile(&oid);
145 	cl_git_pass(git_odb_read(&obj, odb, &oid));
146 
147 	git_odb_object_free(obj);
148 }
149 
test_odb_largefiles__read_into_memory_rejected_on_32bit(void)150 void test_odb_largefiles__read_into_memory_rejected_on_32bit(void)
151 {
152 	git_oid oid;
153 	git_odb_object *obj = NULL;
154 
155 #ifdef GIT_ARCH_64
156 	cl_skip();
157 #endif
158 
159 	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
160 		!cl_is_env_set("GITTEST_INVASIVE_MEMORY") ||
161 		!cl_is_env_set("GITTEST_SLOW"))
162 		cl_skip();
163 
164 	writefile(&oid);
165 	cl_git_fail(git_odb_read(&obj, odb, &oid));
166 
167 	git_odb_object_free(obj);
168 }
169 
test_odb_largefiles__read_header(void)170 void test_odb_largefiles__read_header(void)
171 {
172 	git_oid oid;
173 	size_t len;
174 	git_object_t type;
175 
176 #ifndef GIT_ARCH_64
177 	cl_skip();
178 #endif
179 
180 	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
181 		!cl_is_env_set("GITTEST_SLOW"))
182 		cl_skip();
183 
184 	writefile(&oid);
185 	cl_git_pass(git_odb_read_header(&len, &type, odb, &oid));
186 
187 	cl_assert_equal_sz(LARGEFILE_SIZE, len);
188 	cl_assert_equal_i(GIT_OBJECT_BLOB, type);
189 }
190