-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathclient_sign_url_integration_test.cc
173 lines (141 loc) · 5.58 KB
/
client_sign_url_integration_test.cc
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
171
172
173
// Copyright 2019 Google LLC
//
// 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 "google/cloud/storage/client.h"
#include "google/cloud/storage/internal/nljson.h"
#include "google/cloud/storage/list_objects_reader.h"
#include "google/cloud/storage/testing/storage_integration_test.h"
#include "google/cloud/testing_util/assert_ok.h"
#include "google/cloud/testing_util/init_google_mock.h"
#include <gmock/gmock.h>
#include <fstream>
namespace nl = google::cloud::storage::internal::nl;
namespace google {
namespace cloud {
namespace storage {
inline namespace STORAGE_CLIENT_NS {
namespace {
using ::testing::HasSubstr;
// Initialized in main() below.
char const* account_file_name_;
char const* data_file_name_;
class ObjectIntegrationTest
: public google::cloud::storage::testing::StorageIntegrationTest {};
TEST_F(ObjectIntegrationTest, V4SignGet) {
// This test uses a disabled key to create a V4 Signed URL for a GET
// operation. The bucket name was generated at random too.
// This is a dummy service account JSON file that is inactive. It's fine for
// it to be public.
std::string account_file = account_file_name_;
std::string data_file = data_file_name_;
auto creds =
oauth2::CreateServiceAccountCredentialsFromJsonFilePath(account_file);
ASSERT_STATUS_OK(creds);
Client client(*creds);
std::ifstream ifstr(data_file);
if (!ifstr.is_open()) {
// If the file does not exist, or
// if we were unable to open it for some other reason.
std::cout << "Cannot open credentials file " + data_file + '\n';
return;
}
nl::json json_array;
json_array = nl::json::parse(ifstr);
for (auto const& j_obj : json_array) {
if (j_obj["method"] == "GET" && j_obj["description"] == "Simple GET") {
std::string const method_name = j_obj["method"]; // GET
std::string const bucket_name = j_obj["bucket"];
std::string const object_name = j_obj["object"];
std::string const date = j_obj["timestamp"];
int validInt = j_obj["expiration"];
auto const valid_for = std::chrono::seconds(validInt);
auto actual = client.CreateV4SignedUrl(
method_name, bucket_name, object_name,
SignedUrlTimestamp(internal::ParseRfc3339(date)),
SignedUrlDuration(valid_for),
AddExtensionHeader("host", "storage.googleapis.com"));
ASSERT_STATUS_OK(actual);
EXPECT_THAT(*actual, HasSubstr(bucket_name));
EXPECT_THAT(*actual, HasSubstr(object_name));
std::string const expected = j_obj["expectedUrl"];
EXPECT_EQ(expected, *actual);
}
}
}
TEST_F(ObjectIntegrationTest, V4SignPost) {
// This test uses a disabled key to create a V4 Signed URL for a GET
// operation. The bucket name was generated at random too.
// This is a dummy service account JSON file that is inactive. It's fine for
// it to be public.
std::string account_file = account_file_name_;
std::string data_file = data_file_name_;
auto creds =
oauth2::CreateServiceAccountCredentialsFromJsonFilePath(account_file);
ASSERT_STATUS_OK(creds);
Client client(*creds);
std::ifstream ifstr(data_file);
if (!ifstr.is_open()) {
// If the file does not exist, or
// if we were unable to open it for some other reason.
std::cout << "Cannot open credentials file " + data_file + '\n';
return;
}
nl::json json_array;
json_array = nl::json::parse(ifstr);
for (auto const& j_obj : json_array) {
if (j_obj["method"] == "POST") {
std::string const method_name = j_obj["method"]; // POST
std::string const bucket_name = j_obj["bucket"];
std::string const object_name = j_obj["object"];
std::string const date = j_obj["timestamp"];
std::string key_name;
std::string header_name;
for (auto& x : j_obj["headers"].items()) {
key_name = x.key();
header_name = x.value()[0];
}
int validInt = j_obj["expiration"];
auto const valid_for = std::chrono::seconds(validInt);
auto actual = client.CreateV4SignedUrl(
method_name, bucket_name, object_name,
SignedUrlTimestamp(internal::ParseRfc3339(date)),
SignedUrlDuration(valid_for),
AddExtensionHeader("host", "storage.googleapis.com"),
AddExtensionHeader(key_name, header_name));
ASSERT_STATUS_OK(actual);
EXPECT_THAT(*actual, HasSubstr(bucket_name));
EXPECT_THAT(*actual, HasSubstr(object_name));
std::string const expected = j_obj["expectedUrl"];
EXPECT_EQ(expected, *actual);
}
}
}
} // namespace
} // namespace STORAGE_CLIENT_NS
} // namespace storage
} // namespace cloud
} // namespace google
int main(int argc, char* argv[]) {
google::cloud::testing_util::InitGoogleMock(argc, argv);
// Make sure the arguments are valid.
if (argc != 3) {
std::string const cmd = argv[0];
auto last_slash = std::string(argv[0]).find_last_of('/');
std::cerr << "Usage: " << cmd.substr(last_slash + 1)
<< " <key-file-name> <all-file-name>\n";
return 1;
}
google::cloud::storage::account_file_name_ = argv[1];
google::cloud::storage::data_file_name_ = argv[2];
return RUN_ALL_TESTS();
}