Merge branch 'ms/doc-push-fix' into maint-2.42
[alt-git.git] / reftable / refname_test.c
blob8645cd93bbd8f9b51e05eb9143ed99217bfa1035
1 /*
2 Copyright 2020 Google LLC
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://developers.google.com/open-source/licenses/bsd
7 */
9 #include "basics.h"
10 #include "block.h"
11 #include "blocksource.h"
12 #include "constants.h"
13 #include "reader.h"
14 #include "record.h"
15 #include "refname.h"
16 #include "reftable-error.h"
17 #include "reftable-writer.h"
18 #include "system.h"
20 #include "test_framework.h"
21 #include "reftable-tests.h"
23 struct testcase {
24 char *add;
25 char *del;
26 int error_code;
29 static void test_conflict(void)
31 struct reftable_write_options opts = { 0 };
32 struct strbuf buf = STRBUF_INIT;
33 struct reftable_writer *w =
34 reftable_new_writer(&strbuf_add_void, &buf, &opts);
35 struct reftable_ref_record rec = {
36 .refname = "a/b",
37 .value_type = REFTABLE_REF_SYMREF,
38 .value.symref = "destination", /* make sure it's not a symref.
40 .update_index = 1,
42 int err;
43 int i;
44 struct reftable_block_source source = { NULL };
45 struct reftable_reader *rd = NULL;
46 struct reftable_table tab = { NULL };
47 struct testcase cases[] = {
48 { "a/b/c", NULL, REFTABLE_NAME_CONFLICT },
49 { "b", NULL, 0 },
50 { "a", NULL, REFTABLE_NAME_CONFLICT },
51 { "a", "a/b", 0 },
53 { "p/", NULL, REFTABLE_REFNAME_ERROR },
54 { "p//q", NULL, REFTABLE_REFNAME_ERROR },
55 { "p/./q", NULL, REFTABLE_REFNAME_ERROR },
56 { "p/../q", NULL, REFTABLE_REFNAME_ERROR },
58 { "a/b/c", "a/b", 0 },
59 { NULL, "a//b", 0 },
61 reftable_writer_set_limits(w, 1, 1);
63 err = reftable_writer_add_ref(w, &rec);
64 EXPECT_ERR(err);
66 err = reftable_writer_close(w);
67 EXPECT_ERR(err);
68 reftable_writer_free(w);
70 block_source_from_strbuf(&source, &buf);
71 err = reftable_new_reader(&rd, &source, "filename");
72 EXPECT_ERR(err);
74 reftable_table_from_reader(&tab, rd);
76 for (i = 0; i < ARRAY_SIZE(cases); i++) {
77 struct modification mod = {
78 .tab = tab,
81 if (cases[i].add) {
82 mod.add = &cases[i].add;
83 mod.add_len = 1;
85 if (cases[i].del) {
86 mod.del = &cases[i].del;
87 mod.del_len = 1;
90 err = modification_validate(&mod);
91 EXPECT(err == cases[i].error_code);
94 reftable_reader_free(rd);
95 strbuf_release(&buf);
98 int refname_test_main(int argc, const char *argv[])
100 RUN_TEST(test_conflict);
101 return 0;