Coverage Report

Created: 2026-07-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/src/struct_utils.cc
Line
Count
Source
1
// Copyright 2018 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//    https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include "jwt_verify_lib/struct_utils.h"
16
17
#include "absl/strings/str_split.h"
18
19
namespace google {
20
namespace jwt_verify {
21
22
StructUtils::StructUtils(const ::google::protobuf::Struct& struct_pb)
23
244k
    : struct_pb_(struct_pb) {}
24
25
StructUtils::FindResult StructUtils::GetString(const std::string& name,
26
578k
                                               std::string* str_value) {
27
578k
  const ::google::protobuf::Value* found;
28
578k
  FindResult result = GetValue(name, found);
29
578k
  if (result != OK) {
30
269k
    return result;
31
269k
  }
32
309k
  if (found->kind_case() != google::protobuf::Value::kStringValue) {
33
2.74k
    return WRONG_TYPE;
34
2.74k
  }
35
307k
  *str_value = found->string_value();
36
307k
  return OK;
37
309k
}
38
39
StructUtils::FindResult StructUtils::GetDouble(const std::string& name,
40
9.08k
                                               double* double_value) {
41
9.08k
  const ::google::protobuf::Value* found;
42
9.08k
  FindResult result = GetValue(name, found);
43
9.08k
  if (result != OK) {
44
8.99k
    return result;
45
8.99k
  }
46
95
  if (found->kind_case() != google::protobuf::Value::kNumberValue) {
47
2
    return WRONG_TYPE;
48
2
  }
49
93
  *double_value = found->number_value();
50
93
  return OK;
51
95
}
52
53
StructUtils::FindResult StructUtils::GetUInt64(const std::string& name,
54
9.08k
                                               uint64_t* int_value) {
55
9.08k
  double double_value;
56
9.08k
  FindResult result = GetDouble(name, &double_value);
57
9.08k
  if (result != OK) {
58
8.99k
    return result;
59
8.99k
  }
60
93
  if (double_value < 0 ||
61
91
      double_value >=
62
91
          static_cast<double>(std::numeric_limits<uint64_t>::max())) {
63
3
    return OUT_OF_RANGE;
64
3
  }
65
90
  *int_value = static_cast<uint64_t>(double_value);
66
90
  return OK;
67
93
}
68
69
StructUtils::FindResult StructUtils::GetBoolean(const std::string& name,
70
0
                                                bool* bool_value) {
71
0
  const ::google::protobuf::Value* found;
72
0
  FindResult result = GetValue(name, found);
73
0
  if (result != OK) {
74
0
    return result;
75
0
  }
76
0
  if (found->kind_case() != google::protobuf::Value::kBoolValue) {
77
0
    return WRONG_TYPE;
78
0
  }
79
0
  *bool_value = found->bool_value();
80
0
  return OK;
81
0
}
82
83
StructUtils::FindResult StructUtils::GetStringList(
84
3.02k
    const std::string& name, std::vector<std::string>* list) {
85
3.02k
  const ::google::protobuf::Value* found;
86
3.02k
  FindResult result = GetValue(name, found);
87
3.02k
  if (result != OK) {
88
3.01k
    return result;
89
3.01k
  }
90
15
  if (found->kind_case() == google::protobuf::Value::kStringValue) {
91
12
    list->push_back(found->string_value());
92
12
    return OK;
93
12
  }
94
3
  if (found->kind_case() == google::protobuf::Value::kListValue) {
95
0
    for (const auto& v : found->list_value().values()) {
96
0
      if (v.kind_case() != google::protobuf::Value::kStringValue) {
97
0
        return WRONG_TYPE;
98
0
      }
99
0
      list->push_back(v.string_value());
100
0
    }
101
0
    return OK;
102
0
  }
103
3
  return WRONG_TYPE;
104
3
}
105
106
StructUtils::FindResult StructUtils::GetValue(
107
591k
    const std::string& nested_names, const google::protobuf::Value*& found) {
108
591k
  const std::vector<absl::string_view> name_vector =
109
591k
      absl::StrSplit(nested_names, '.');
110
111
591k
  const google::protobuf::Struct* current_struct = &struct_pb_;
112
591k
  for (int i = 0; i < name_vector.size(); ++i) {
113
591k
    const auto& fields = current_struct->fields();
114
591k
    const auto it = fields.find(std::string(name_vector[i]));
115
591k
    if (it == fields.end()) {
116
281k
      return MISSING;
117
281k
    }
118
309k
    if (i == name_vector.size() - 1) {
119
309k
      found = &it->second;
120
309k
      return OK;
121
309k
    }
122
0
    if (it->second.kind_case() != google::protobuf::Value::kStructValue) {
123
0
      return WRONG_TYPE;
124
0
    }
125
0
    current_struct = &it->second.struct_value();
126
0
  }
127
0
  return MISSING;
128
591k
}
129
130
}  // namespace jwt_verify
131
}  // namespace google