GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/gateway_util.cc
Date: 2025-06-29 02:35:41
Exec Total Coverage
Lines: 15 35 42.9%
Branches: 21 56 37.5%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "gateway_util.h"
6
7 #include <fcntl.h>
8 #include <unistd.h>
9
10 #include <algorithm>
11 #include <vector>
12
13 #include "util/logging.h"
14 #include "util/posix.h"
15 #include "util/string.h"
16
17 namespace {
18
19 179 bool BothAreSpaces(const char &c1, const char &c2) {
20
4/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 153 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 16 times.
179 return c1 == ' ' && (c1 == c2);
21 }
22
23 } // namespace
24
25 namespace gateway {
26
27 int APIVersion() { return 2; }
28
29 GatewayKey ReadGatewayKey(const std::string &key_file_name) {
30 std::string id;
31 std::string secret;
32 const bool retval = ReadKeys(key_file_name, &id, &secret);
33 if (!retval)
34 return GatewayKey();
35 return GatewayKey(id, secret);
36 }
37
38 bool ReadKeys(const std::string &key_file_name, std::string *key_id,
39 std::string *secret) {
40 if (!(key_id && secret)) {
41 return false;
42 }
43
44 const int key_file_fd = open(key_file_name.c_str(), O_RDONLY);
45 if (!key_file_fd) {
46 return false;
47 }
48
49 std::string body;
50 if (!SafeReadToString(key_file_fd, &body)) {
51 close(key_file_fd);
52 return false;
53 }
54
55 close(key_file_fd);
56
57 return ParseKey(body, key_id, secret);
58 }
59
60 8 bool ParseKey(const std::string &body, std::string *key_id,
61 std::string *secret) {
62
1/2
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
8 const std::string line = GetLineMem(body.data(), body.size());
63
4/8
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 8 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 8 times.
✗ Branch 13 not taken.
16 std::string l = Trim(ReplaceAll(line, "\t", " "));
64
2/4
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 8 times.
✗ Branch 10 not taken.
8 l.erase(std::unique(l.begin(), l.end(), BothAreSpaces), l.end());
65
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 std::vector<std::string> tokens = SplitString(l, ' ');
66
67
5/6
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 7 times.
8 if (tokens.size() < 2 || tokens.size() > 3) {
68 1 return false;
69 }
70
71
2/2
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 2 times.
7 if (tokens[0] == "plain_text") {
72
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 *key_id = tokens[1];
73
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 *secret = tokens[2];
74 } else {
75 2 return false;
76 }
77
78 5 return true;
79 8 }
80
81 } // namespace gateway
82