GCC Code Coverage Report


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