t03-objwrite.c 6.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2,
 * as published by the Free Software Foundation.
 *
 * In addition to the permissions in the GNU General Public License,
 * the authors give you unlimited permission to link the compiled
 * version of this file into combinations with other programs,
 * and to distribute those combinations without any restriction
 * coming from the use of this file.  (The General Public License
 * restrictions do apply in other respects; for example, they cover
 * modification of the file, and distribution when not linked into
 * a combined executable.)
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
Ramsay Jones committed
25 26
#include "test_lib.h"
#include "fileops.h"
Vicent Marti committed
27
#include "odb.h"
Ramsay Jones committed
28

29 30 31
static char *odb_dir = "test-objects";
#include "t03-data.h"

Ramsay Jones committed
32 33
static int make_odb_dir(void)
{
Vicent Marti committed
34
	if (p_mkdir(odb_dir, 0755) < 0) {
35 36 37 38 39 40 41 42
		int err = errno;
		fprintf(stderr, "can't make directory \"%s\"", odb_dir);
		if (err == EEXIST)
			fprintf(stderr, " (already exists)");
		fprintf(stderr, "\n");
		return -1;
	}
	return 0;
Ramsay Jones committed
43 44 45 46
}

static int check_object_files(object_data *d)
{
Vicent Marti committed
47
	if (git_futils_exists(d->dir) < 0)
48
		return -1;
Vicent Marti committed
49
	if (git_futils_exists(d->file) < 0)
50 51
		return -1;
	return 0;
Ramsay Jones committed
52 53
}

54
static int cmp_objects(git_rawobj *o1, git_rawobj *o2)
Ramsay Jones committed
55
{
56 57 58 59 60 61 62
	if (o1->type != o2->type)
		return -1;
	if (o1->len != o2->len)
		return -1;
	if ((o1->len > 0) && (memcmp(o1->data, o2->data, o1->len) != 0))
		return -1;
	return 0;
Ramsay Jones committed
63 64
}

65 66
static int remove_object_files(object_data *d)
{
Vicent Marti committed
67
	if (p_unlink(d->file) < 0) {
68 69 70
		fprintf(stderr, "can't delete object file \"%s\"\n", d->file);
		return -1;
	}
Vicent Marti committed
71
	if ((p_rmdir(d->dir) < 0) && (errno != ENOTEMPTY)) {
72 73 74 75
		fprintf(stderr, "can't remove directory \"%s\"\n", d->dir);
		return -1;
	}

Vicent Marti committed
76
	if (p_rmdir(odb_dir) < 0) {
77 78 79 80 81 82 83
		fprintf(stderr, "can't remove directory \"%s\"\n", odb_dir);
		return -1;
	}

	return 0;
}

Vicent Marti committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
static int streaming_write(git_oid *oid, git_odb *odb, git_rawobj *raw)
{
	git_odb_stream *stream;
	int error;

	if ((error = git_odb_open_wstream(&stream, odb, raw->len, raw->type)) < GIT_SUCCESS)
		return error;

	stream->write(stream, raw->data, raw->len);

	error = stream->finalize_write(oid, stream);
	stream->free(stream);

	return error;
}

100
BEGIN_TEST(write0, "write loose commit object")
Ramsay Jones committed
101 102
    git_odb *db;
    git_oid id1, id2;
Vicent Marti committed
103
    git_odb_object *obj;
Ramsay Jones committed
104 105 106

    must_pass(make_odb_dir());
    must_pass(git_odb_open(&db, odb_dir));
Vicent Marti committed
107
    must_pass(git_oid_fromstr(&id1, commit.id));
Ramsay Jones committed
108

Vicent Marti committed
109
    must_pass(streaming_write(&id2, db, &commit_obj));
Ramsay Jones committed
110 111 112
    must_be_true(git_oid_cmp(&id1, &id2) == 0);
    must_pass(check_object_files(&commit));

113
    must_pass(git_odb_read(&obj, db, &id1));
Vicent Marti committed
114
    must_pass(cmp_objects(&obj->raw, &commit_obj));
Ramsay Jones committed
115

Vicent Marti committed
116
    git_odb_object_close(obj);
Ramsay Jones committed
117 118 119 120
    git_odb_close(db);
    must_pass(remove_object_files(&commit));
END_TEST

121
BEGIN_TEST(write1, "write loose tree object")
Ramsay Jones committed
122 123
    git_odb *db;
    git_oid id1, id2;
Vicent Marti committed
124
    git_odb_object *obj;
Ramsay Jones committed
125 126 127

    must_pass(make_odb_dir());
    must_pass(git_odb_open(&db, odb_dir));
Vicent Marti committed
128
    must_pass(git_oid_fromstr(&id1, tree.id));
Ramsay Jones committed
129

Vicent Marti committed
130
    must_pass(streaming_write(&id2, db, &tree_obj));
Ramsay Jones committed
131 132 133
    must_be_true(git_oid_cmp(&id1, &id2) == 0);
    must_pass(check_object_files(&tree));

134
    must_pass(git_odb_read(&obj, db, &id1));
Vicent Marti committed
135
    must_pass(cmp_objects(&obj->raw, &tree_obj));
Ramsay Jones committed
136

Vicent Marti committed
137
    git_odb_object_close(obj);
Ramsay Jones committed
138 139 140 141
    git_odb_close(db);
    must_pass(remove_object_files(&tree));
END_TEST

142
BEGIN_TEST(write2, "write loose tag object")
Ramsay Jones committed
143 144
    git_odb *db;
    git_oid id1, id2;
Vicent Marti committed
145
    git_odb_object *obj;
Ramsay Jones committed
146 147 148

    must_pass(make_odb_dir());
    must_pass(git_odb_open(&db, odb_dir));
Vicent Marti committed
149
    must_pass(git_oid_fromstr(&id1, tag.id));
Ramsay Jones committed
150

Vicent Marti committed
151
    must_pass(streaming_write(&id2, db, &tag_obj));
Ramsay Jones committed
152 153 154
    must_be_true(git_oid_cmp(&id1, &id2) == 0);
    must_pass(check_object_files(&tag));

155
    must_pass(git_odb_read(&obj, db, &id1));
Vicent Marti committed
156
    must_pass(cmp_objects(&obj->raw, &tag_obj));
Ramsay Jones committed
157

Vicent Marti committed
158
    git_odb_object_close(obj);
Ramsay Jones committed
159 160 161 162
    git_odb_close(db);
    must_pass(remove_object_files(&tag));
END_TEST

163
BEGIN_TEST(write3, "write zero-length object")
Ramsay Jones committed
164 165
    git_odb *db;
    git_oid id1, id2;
Vicent Marti committed
166
    git_odb_object *obj;
Ramsay Jones committed
167 168 169

    must_pass(make_odb_dir());
    must_pass(git_odb_open(&db, odb_dir));
Vicent Marti committed
170
    must_pass(git_oid_fromstr(&id1, zero.id));
Ramsay Jones committed
171

Vicent Marti committed
172
    must_pass(streaming_write(&id2, db, &zero_obj));
Ramsay Jones committed
173 174 175
    must_be_true(git_oid_cmp(&id1, &id2) == 0);
    must_pass(check_object_files(&zero));

176
    must_pass(git_odb_read(&obj, db, &id1));
Vicent Marti committed
177
    must_pass(cmp_objects(&obj->raw, &zero_obj));
Ramsay Jones committed
178

Vicent Marti committed
179
    git_odb_object_close(obj);
Ramsay Jones committed
180 181 182 183
    git_odb_close(db);
    must_pass(remove_object_files(&zero));
END_TEST

184
BEGIN_TEST(write4, "write one-byte long object")
Ramsay Jones committed
185 186
    git_odb *db;
    git_oid id1, id2;
Vicent Marti committed
187
    git_odb_object *obj;
Ramsay Jones committed
188 189 190

    must_pass(make_odb_dir());
    must_pass(git_odb_open(&db, odb_dir));
Vicent Marti committed
191
    must_pass(git_oid_fromstr(&id1, one.id));
Ramsay Jones committed
192

Vicent Marti committed
193
    must_pass(streaming_write(&id2, db, &one_obj));
Ramsay Jones committed
194 195 196
    must_be_true(git_oid_cmp(&id1, &id2) == 0);
    must_pass(check_object_files(&one));

197
    must_pass(git_odb_read(&obj, db, &id1));
Vicent Marti committed
198
    must_pass(cmp_objects(&obj->raw, &one_obj));
Ramsay Jones committed
199

Vicent Marti committed
200
    git_odb_object_close(obj);
Ramsay Jones committed
201 202 203 204
    git_odb_close(db);
    must_pass(remove_object_files(&one));
END_TEST

205
BEGIN_TEST(write5, "write two-byte long object")
Ramsay Jones committed
206 207
    git_odb *db;
    git_oid id1, id2;
Vicent Marti committed
208
    git_odb_object *obj;
Ramsay Jones committed
209 210 211

    must_pass(make_odb_dir());
    must_pass(git_odb_open(&db, odb_dir));
Vicent Marti committed
212
    must_pass(git_oid_fromstr(&id1, two.id));
Ramsay Jones committed
213

Vicent Marti committed
214
    must_pass(streaming_write(&id2, db, &two_obj));
Ramsay Jones committed
215 216 217
    must_be_true(git_oid_cmp(&id1, &id2) == 0);
    must_pass(check_object_files(&two));

218
    must_pass(git_odb_read(&obj, db, &id1));
Vicent Marti committed
219
    must_pass(cmp_objects(&obj->raw, &two_obj));
Ramsay Jones committed
220

Vicent Marti committed
221
    git_odb_object_close(obj);
Ramsay Jones committed
222 223 224 225
    git_odb_close(db);
    must_pass(remove_object_files(&two));
END_TEST

226
BEGIN_TEST(write6, "write an object which is several bytes long")
Ramsay Jones committed
227 228
    git_odb *db;
    git_oid id1, id2;
Vicent Marti committed
229
    git_odb_object *obj;
Ramsay Jones committed
230 231 232

    must_pass(make_odb_dir());
    must_pass(git_odb_open(&db, odb_dir));
Vicent Marti committed
233
    must_pass(git_oid_fromstr(&id1, some.id));
Ramsay Jones committed
234

Vicent Marti committed
235
    must_pass(streaming_write(&id2, db, &some_obj));
Ramsay Jones committed
236 237 238
    must_be_true(git_oid_cmp(&id1, &id2) == 0);
    must_pass(check_object_files(&some));

239
    must_pass(git_odb_read(&obj, db, &id1));
Vicent Marti committed
240
    must_pass(cmp_objects(&obj->raw, &some_obj));
Ramsay Jones committed
241

Vicent Marti committed
242
    git_odb_object_close(obj);
Ramsay Jones committed
243 244 245 246
    git_odb_close(db);
    must_pass(remove_object_files(&some));
END_TEST

247 248 249 250 251 252 253 254 255
BEGIN_SUITE(objwrite)
	ADD_TEST(write0);
	ADD_TEST(write1);
	ADD_TEST(write2);
	ADD_TEST(write3);
	ADD_TEST(write4);
	ADD_TEST(write5);
	ADD_TEST(write6);
END_SUITE