Coverage Report

Created: 2026-06-10 06:57

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
292k
    : struct_pb_(struct_pb) {}
24
25
StructUtils::FindResult StructUtils::GetString(const std::string& name,
26
692k
                                               std::string* str_value) {
27
692k
  const ::google::protobuf::Value* found;
28
692k
  FindResult result = GetValue(name, found);
29
692k
  if (result != OK) {
30
327k
    return result;
31
327k
  }
32
365k
  if (found->kind_case() != google::protobuf::Value::kStringValue) {
33
2.72k
    return WRONG_TYPE;
34
2.72k
  }
35
363k
  *str_value = found->string_value();
36
363k
  return OK;
37
365k
}
38
39
StructUtils::FindResult StructUtils::GetDouble(const std::string& name,
40
8.25k
                                               double* double_value) {
41
8.25k
  const ::google::protobuf::Value* found;
42
8.25k
  FindResult result = GetValue(name, found);
43
8.25k
  if (result != OK) {
44
8.13k
    return result;
45
8.13k
  }
46
122
  if (found->kind_case() != google::protobuf::Value::kNumberValue) {
47
6
    return WRONG_TYPE;
48
6
  }
49
116
  *double_value = found->number_value();
50
116
  return OK;
51
122
}
52
53
StructUtils::FindResult StructUtils::GetUInt64(const std::string& name,
54
8.25k
                                               uint64_t* int_value) {
55
8.25k
  double double_value;
56
8.25k
  FindResult result = GetDouble(name, &double_value);
57
8.25k
  if (result != OK) {
58
8.13k
    return result;
59
8.13k
  }
60
116
  if (double_value < 0 ||
61
115
      double_value >=
62
115
          static_cast<double>(std::numeric_limits<uint64_t>::max())) {
63
3
    return OUT_OF_RANGE;
64
3
  }
65
113
  *int_value = static_cast<uint64_t>(double_value);
66
113
  return OK;
67
116
}
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
2.74k
    const std::string& name, std::vector<std::string>* list) {
85
2.74k
  const ::google::protobuf::Value* found;
86
2.74k
  FindResult result = GetValue(name, found);
87
2.74k
  if (result != OK) {
88
2.72k
    return result;
89
2.72k
  }
90
18
  if (found->kind_case() == google::protobuf::Value::kStringValue) {
91
16
    list->push_back(found->string_value());
92
16
    return OK;
93
16
  }
94
2
  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
2
  return WRONG_TYPE;
104
2
}
105
106
StructUtils::FindResult StructUtils::GetValue(
107
703k
    const std::string& nested_names, const google::protobuf::Value*& found) {
108
703k
  const std::vector<absl::string_view> name_vector =
109
703k
      absl::StrSplit(nested_names, '.');
110
111
703k
  const google::protobuf::Struct* current_struct = &struct_pb_;
112
703k
  for (int i = 0; i < name_vector.size(); ++i) {
113
703k
    const auto& fields = current_struct->fields();
114
703k
    const auto it = fields.find(std::string(name_vector[i]));
115
703k
    if (it == fields.end()) {
116
337k
      return MISSING;
117
337k
    }
118
365k
    if (i == name_vector.size() - 1) {
119
365k
      found = &it->second;
120
365k
      return OK;
121
365k
    }
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
703k
}
129
130
}  // namespace jwt_verify
131
}  // namespace google