File: gfalt_test_double_cred.cpp

package info (click to toggle)
gfal2 2.23.2-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,452 kB
  • sloc: ansic: 15,858; cpp: 15,198; python: 257; sh: 228; makefile: 120
file content (170 lines) | stat: -rw-r--r-- 5,475 bytes parent folder | download | duplicates (4)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (c) CERN 2017
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include <gfal_api.h>
#include <exceptions/gerror_to_cpp.h>
#include <common/gfal_lib_test.h>
#include <common/gfal_gtest_asserts.h>


class CopyDoubleCredTest: public testing::Test {
public:
    static const char *source_root, *destination_root;
    static const char *source_proxy, *destination_proxy;

    gfal2_context_t handle;
    gfalt_params_t params;
    gfal2_cred_t *source_cred, *dest_cred;

    char source[2048];
    char destination[2048];

    CopyDoubleCredTest() {
        source_cred = gfal2_cred_new(GFAL_CRED_X509_CERT, source_proxy);
        dest_cred = gfal2_cred_new(GFAL_CRED_X509_CERT, destination_proxy);
    }

    virtual ~CopyDoubleCredTest() {
        gfal2_cred_free(source_cred);
        gfal2_cred_free(dest_cred);
    }

    void SetUp() {
        unsetenv("X509_USER_PROXY");
        unsetenv("X509_USER_CERT");
        unsetenv("X509_USER_KEY");

        int ret;
        GError *error = NULL;

        handle = gfal2_context_new(&error);
        Gfal::gerror_to_cpp(&error);
        params = gfalt_params_handle_new(&error);
        Gfal::gerror_to_cpp(&error);

        ret = gfal2_cred_clean(handle, &error);
        ASSERT_PRED_FORMAT2(AssertGfalSuccess, ret, error);

        // Clear automatic setup
        gfal2_remove_opt(handle, "X509", "CERT", NULL);
        gfal2_remove_opt(handle, "X509", "KEY", NULL);

        // Set configured values
        ret = gfal2_cred_set(handle, source_root, source_cred, &error);
        ASSERT_PRED_FORMAT2(AssertGfalSuccess, ret, error);
        ret = gfal2_cred_set(handle, destination_root, dest_cred, &error);
        ASSERT_PRED_FORMAT2(AssertGfalSuccess, ret, error);

        {
            setenv("X509_USER_PROXY", source_proxy, 1);
            generate_random_uri(source_root, "copyfile_doublecred_source", source, 2048);
            ret = generate_file_if_not_exists(handle, source, "file:///etc/hosts", &error);
            ASSERT_PRED_FORMAT2(AssertGfalSuccess, ret, error);
            unsetenv("X509_USER_PROXY");
        }

        generate_random_uri(destination_root, "copyfile_doublecred_dest", destination, 2048);

        // Just make sure the environment is well prepared
        ASSERT_EQ((char*)NULL, getenv("X509_USER_CERT"));
        ASSERT_EQ((char*)NULL, getenv("X509_USER_KEY"));
        ASSERT_EQ((char*)NULL, getenv("X509_USER_PROXY"));

        ASSERT_EQ((gchar*)NULL, gfal2_get_opt_string_with_default(handle, "X509", "CERT", NULL));
        ASSERT_EQ((gchar*)NULL, gfal2_get_opt_string_with_default(handle, "X509", "KEY", NULL));
    }

    void TearDown() {
        GError *error = NULL;
        gfalt_params_handle_delete(params, NULL);
        {
            setenv("X509_USER_PROXY", source_proxy, 1);
            gfal2_unlink(handle, source, &error);
            g_clear_error(&error);
            unsetenv("X509_USER_PROXY");
        }
        {
            setenv("X509_USER_PROXY", destination_proxy, 1);
            gfal2_unlink(handle, destination, &error);
            g_clear_error(&error);
            unsetenv("X509_USER_PROXY");
        }
        gfal2_context_free(handle);
    }
};


const char *CopyDoubleCredTest::source_root = NULL;
const char *CopyDoubleCredTest::destination_root = NULL;
const char *CopyDoubleCredTest::source_proxy = NULL;
const char *CopyDoubleCredTest::destination_proxy = NULL;


TEST_F(CopyDoubleCredTest, Stat)
{
    GError *error = NULL;
    int ret;

    ret = gfal2_cred_set(handle, source_root, source_cred, &error);
    ASSERT_PRED_FORMAT2(AssertGfalSuccess, ret, error);

    struct stat buf;
    ret = gfal2_stat(handle, source, &buf, &error);
    ASSERT_PRED_FORMAT2(AssertGfalSuccess, ret, error);

    ASSERT_TRUE(S_ISREG(buf.st_mode));
}


TEST_F(CopyDoubleCredTest, Simple)
{
    GError *error = NULL;
    int ret;

    ret = gfal2_cred_set(handle, source_root, source_cred, &error);
    ASSERT_PRED_FORMAT2(AssertGfalSuccess, ret, error);
    ret = gfal2_cred_set(handle, destination_root, dest_cred, &error);
    ASSERT_PRED_FORMAT2(AssertGfalSuccess, ret, error);

    ret = gfalt_copy_file(handle, params, source, destination, &error);
    ASSERT_PRED_FORMAT2(AssertGfalSuccess, ret, error);
}


int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);

    if (argc < 3 || (argc > 3 && argc != 5)) {
        printf("Usage: %s source_root destination [proxy-for-source_root proxy-for-destination]\n", argv[0]);
        return 1;
    }

    CopyDoubleCredTest::source_root = argv[1];
    CopyDoubleCredTest::destination_root = argv[2];

    if (argc > 3) {
        CopyDoubleCredTest::source_proxy = argv[3];
        CopyDoubleCredTest::destination_proxy = argv[4];
    }
    else {
        CopyDoubleCredTest::source_proxy = getenv("X509_USER_PROXY");
        CopyDoubleCredTest::destination_proxy =  getenv("X509_USER_PROXY");
    }

    return RUN_ALL_TESTS();
}