GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/gateway_util.cc
Date: 2024-04-28 02:33:07
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
9 #include <algorithm>
10 #include <vector>
11
12 #include "util/logging.h"
13 #include "util/posix.h"
14 #include "util/string.h"
15
16 namespace {
17
18 179 bool BothAreSpaces(const char& c1, const char& c2) {
19
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);
20 }
21
22 }
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 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 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 8 bool ParseKey(const std::string& body, std::string* key_id,
60 std::string* secret) {
61
1/2
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
8 std::string line = GetLineMem(body.data(), body.size());
62
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", " "));
63
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());
64
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 std::vector<std::string> tokens = SplitString(l, ' ');
65
66
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) {
67 1 return false;
68 }
69
70
2/2
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 2 times.
7 if (tokens[0] == "plain_text") {
71
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 *key_id = tokens[1];
72
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 *secret = tokens[2];
73 } else {
74 2 return false;
75 }
76
77 5 return true;
78 8 }
79
80 } // namespace gateway
81