GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/swissknife_lease_json.cc
Date: 2025-06-29 02:35:41
Exec Total Coverage
Lines: 51 88 58.0%
Branches: 63 184 34.2%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "swissknife_lease_json.h"
6
7 #include "json.h"
8 #include "json_document.h"
9 #include "util/logging.h"
10 #include "util/pointer.h"
11
12 // TODO(@vvolkl): refactor
13 LeaseReply ParseAcquireReplyWithRevision(const CurlBuffer &buffer,
14 std::string *session_token,
15 uint64_t *current_revision,
16 std::string &current_root_hash) {
17 if (buffer.data.size() == 0 || session_token == NULL) {
18 return kLeaseReplyFailure;
19 }
20
21 const UniquePtr<JsonDocument> reply(JsonDocument::Create(buffer.data));
22 if (!reply.IsValid() || !reply->IsValid()) {
23 return kLeaseReplyFailure;
24 }
25
26 const JSON *result = JsonDocument::SearchInObject(reply->root(), "status",
27 JSON_STRING);
28 if (result != NULL) {
29 const std::string status = result->string_value;
30 if (status == "ok") {
31 LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok");
32 const JSON *token = JsonDocument::SearchInObject(
33 reply->root(), "session_token", JSON_STRING);
34 if (token != NULL) {
35 const JSON *rev = JsonDocument::SearchInObject(
36 reply->root(), "revision",
37 JSON_INT); // TODO FIXME: make the json lib uint64 aware
38 if (rev != NULL) {
39 *current_revision = (uint64_t)rev->int_value;
40 }
41 const JSON *hash = JsonDocument::SearchInObject(
42 reply->root(), "root_hash", JSON_STRING);
43 if (hash != NULL) {
44 current_root_hash = hash->string_value;
45 }
46 LogCvmfs(kLogCvmfs, kLogDebug, "Session token: %s",
47 token->string_value);
48 *session_token = token->string_value;
49 return kLeaseReplySuccess;
50 }
51 } else if (status == "path_busy") {
52 const JSON *time_remaining = JsonDocument::SearchInObject(
53 reply->root(), "time_remaining", JSON_STRING);
54 if (time_remaining != NULL) {
55 LogCvmfs(kLogCvmfs, kLogStdout, "Path busy. Time remaining = %s",
56 time_remaining->string_value);
57 return kLeaseReplyBusy;
58 }
59 } else if (status == "error") {
60 const JSON *reason = JsonDocument::SearchInObject(reply->root(), "reason",
61 JSON_STRING);
62 if (reason != NULL) {
63 LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", reason->string_value);
64 }
65 } else {
66 LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s",
67 status.c_str());
68 }
69 }
70
71 return kLeaseReplyFailure;
72 }
73 234 LeaseReply ParseAcquireReply(const CurlBuffer &buffer,
74 std::string *session_token) {
75
5/6
✓ Branch 1 taken 156 times.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 156 times.
✓ Branch 5 taken 78 times.
✓ Branch 6 taken 156 times.
234 if (buffer.data.size() == 0 || session_token == NULL) {
76 78 return kLeaseReplyFailure;
77 }
78
79
2/4
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 156 times.
✗ Branch 5 not taken.
156 const UniquePtr<JsonDocument> reply(JsonDocument::Create(buffer.data));
80
5/6
✓ Branch 1 taken 117 times.
✓ Branch 2 taken 39 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 117 times.
✓ Branch 7 taken 39 times.
✓ Branch 8 taken 117 times.
156 if (!reply.IsValid() || !reply->IsValid()) {
81 39 return kLeaseReplyFailure;
82 }
83
84
2/4
✓ Branch 2 taken 117 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 117 times.
✗ Branch 8 not taken.
117 const JSON *result = JsonDocument::SearchInObject(reply->root(), "status",
85 JSON_STRING);
86
1/2
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
117 if (result != NULL) {
87
1/2
✓ Branch 2 taken 117 times.
✗ Branch 3 not taken.
117 const std::string status = result->string_value;
88
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 78 times.
117 if (status == "ok") {
89
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
39 LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok");
90
2/4
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 39 times.
✗ Branch 8 not taken.
39 const JSON *token = JsonDocument::SearchInObject(
91 reply->root(), "session_token", JSON_STRING);
92
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (token != NULL) {
93 39 LogCvmfs(kLogCvmfs, kLogDebug, "Session token: %s",
94
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
39 token->string_value);
95
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
39 *session_token = token->string_value;
96 39 return kLeaseReplySuccess;
97 }
98
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 39 times.
78 } else if (status == "path_busy") {
99
2/4
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 39 times.
✗ Branch 8 not taken.
39 const JSON *time_remaining = JsonDocument::SearchInObject(
100 reply->root(), "time_remaining", JSON_INT);
101
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (time_remaining != NULL) {
102
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
39 LogCvmfs(kLogCvmfs, kLogStdout, "Path busy. Time remaining = %d s",
103
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
39 time_remaining->int_value);
104 39 return kLeaseReplyBusy;
105 }
106
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
39 } else if (status == "error") {
107
2/4
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 39 times.
✗ Branch 8 not taken.
39 const JSON *reason = JsonDocument::SearchInObject(reply->root(), "reason",
108 JSON_STRING);
109
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (reason != NULL) {
110
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
39 LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", reason->string_value);
111 }
112 } else {
113 LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s",
114 status.c_str());
115 }
116
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 78 times.
117 }
117
118 39 return kLeaseReplyFailure;
119 156 }
120
121
122 195 LeaseReply ParseDropReply(const CurlBuffer &buffer) {
123
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 156 times.
195 if (buffer.data.size() == 0) {
124 39 return kLeaseReplyFailure;
125 }
126
127
2/4
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 156 times.
✗ Branch 5 not taken.
156 const UniquePtr<const JsonDocument> reply(JsonDocument::Create(buffer.data));
128
5/6
✓ Branch 1 taken 117 times.
✓ Branch 2 taken 39 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 117 times.
✓ Branch 7 taken 39 times.
✓ Branch 8 taken 117 times.
156 if (!reply.IsValid() || !reply->IsValid()) {
129 39 return kLeaseReplyFailure;
130 }
131
132
2/4
✓ Branch 2 taken 117 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 117 times.
✗ Branch 8 not taken.
117 const JSON *result = JsonDocument::SearchInObject(reply->root(), "status",
133 JSON_STRING);
134
1/2
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
117 if (result != NULL) {
135
1/2
✓ Branch 2 taken 117 times.
✗ Branch 3 not taken.
117 const std::string status = result->string_value;
136
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 78 times.
117 if (status == "ok") {
137
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
39 LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok");
138 39 return kLeaseReplySuccess;
139
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 39 times.
78 } else if (status == "invalid_token") {
140
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
39 LogCvmfs(kLogCvmfs, kLogStdout, "Error: invalid session token");
141
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
39 } else if (status == "error") {
142
2/4
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 39 times.
✗ Branch 8 not taken.
39 const JSON *reason = JsonDocument::SearchInObject(reply->root(), "reason",
143 JSON_STRING);
144
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (reason != NULL) {
145
1/2
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
39 LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", reason->string_value);
146 }
147 } else {
148 LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s",
149 status.c_str());
150 }
151
2/2
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 39 times.
117 }
152
153 78 return kLeaseReplyFailure;
154 156 }
155