summaryrefslogtreecommitdiff
path: root/tests/test-pull-c.c
blob: 037a17899b040ccf8be96b7ed8dafa960b7ba9fc (plain)
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
/*
 * Copyright (C) 2016 Red Hat, Inc.
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"
#include "libglnx.h"
#include <glib.h>
#include <stdlib.h>
#include <gio/gio.h>
#include <string.h>

#include "libostreetest.h"

typedef struct {
  OstreeRepo *repo;
} TestData;

static void
test_data_init (TestData *td)
{
  GError *local_error = NULL;
  GError **error = &local_error;
  g_autofree char *http_address = NULL;
  g_autofree char *repo_url = NULL;

  td->repo = ot_test_setup_repo (NULL, error);
  if (!td->repo)
    goto out;

  if (!ot_test_run_libtest ("setup_fake_remote_repo1 archive", error))
    goto out;

  if (!g_file_get_contents ("httpd-address", &http_address, NULL, error))
    goto out;

  g_strstrip (http_address);

  repo_url = g_strconcat (http_address, "/ostree/gnomerepo", NULL);

  { g_autoptr(GVariantBuilder) builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
    g_autoptr(GVariant) opts = NULL;

    g_variant_builder_add (builder, "{s@v}", "gpg-verify", g_variant_new_variant (g_variant_new_boolean (FALSE)));
    opts = g_variant_ref_sink (g_variant_builder_end (builder));

    if (!ostree_repo_remote_change (td->repo, NULL, OSTREE_REPO_REMOTE_CHANGE_ADD,
                                    "origin", repo_url, opts, NULL, error))
      goto out;
  }

 out:
  g_assert_no_error (local_error);
}

static void
test_pull_multi_nochange (gconstpointer data)
{
  GError *local_error = NULL;
  GError **error = &local_error;
  TestData *td = (void*)data;
  char *refs[] = { "main", NULL };

  if (!ostree_repo_pull (td->repo, "origin", (char**)&refs, 0, NULL, NULL, error))
    goto out;
  if (!ostree_repo_pull (td->repo, "origin", (char**)&refs, 0, NULL, NULL, error))
    goto out;
  if (!ostree_repo_pull (td->repo, "origin", (char**)&refs, 0, NULL, NULL, error))
    goto out;

 out:
  g_assert_no_error (local_error);
}

static void
test_pull_multi_error_then_ok (gconstpointer data)
{
  GError *local_error = NULL;
  GError **error = &local_error;

  TestData *td = (void*)data;
  char *ok_refs[] = { "main", NULL };
  char *bad_refs[] = { "nosuchbranch", NULL };

  for (guint i = 0; i < 3; i++)
    {
      g_autoptr(GError) tmp_error = NULL;
      if (!ostree_repo_pull (td->repo, "origin", (char**)&ok_refs, 0, NULL, NULL, error))
        goto out;
      if (ostree_repo_pull (td->repo, "origin", (char**)&bad_refs, 0, NULL, NULL, &tmp_error))
        g_assert_not_reached ();
      g_clear_error (&tmp_error);
      if (ostree_repo_pull (td->repo, "origin", (char**)&bad_refs, 0, NULL, NULL, &tmp_error))
        g_assert_not_reached ();
      g_clear_error (&tmp_error);
      if (!ostree_repo_pull (td->repo, "origin", (char**)&ok_refs, 0, NULL, NULL, error))
        goto out;
    }

 out:
  g_assert_no_error (local_error);
}

int main (int argc, char **argv)
{
  TestData td = {NULL,};
  int r;

  test_data_init (&td);

  g_test_init (&argc, &argv, NULL);

  g_test_add_data_func ("/test-pull-c/multi-nochange", &td, test_pull_multi_nochange);
  g_test_add_data_func ("/test-pull-c/multi-ok-error-repeat", &td, test_pull_multi_error_then_ok);

  r = g_test_run();
  g_clear_object (&td.repo);

  return r;
}