Skip to content

Commit 6b8a589

Browse files
authored
test: Add STInteger and STParsedJSON tests (#5726)
This change is to improve code coverage (and to simplify #5720 and #5725); there is otherwise no change in functionality. The change adds basic tests for `STInteger` and `STParsedJSON`, so it becomes easier to test smaller changes to the types, as well as removes `STParsedJSONArray`, since it is not used anywhere (including in Clio).
1 parent ffeabc9 commit 6b8a589

File tree

7 files changed

+2157
-126
lines changed

7 files changed

+2157
-126
lines changed

include/xrpl/protocol/Permissions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class Permission
7474
Permission&
7575
operator=(Permission const&) = delete;
7676

77+
std::optional<std::string>
78+
getPermissionName(std::uint32_t const value) const;
79+
7780
std::optional<std::uint32_t>
7881
getGranularValue(std::string const& name) const;
7982

include/xrpl/protocol/STParsedJSON.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,34 +54,6 @@ class STParsedJSONObject
5454
Json::Value error;
5555
};
5656

57-
/** Holds the serialized result of parsing an input JSON array.
58-
This does validation and checking on the provided JSON.
59-
*/
60-
class STParsedJSONArray
61-
{
62-
public:
63-
/** Parses and creates an STParsedJSON array.
64-
The result of the parsing is stored in array and error.
65-
Exceptions:
66-
Does not throw.
67-
@param name The name of the JSON field, used in diagnostics.
68-
@param json The JSON-RPC to parse.
69-
*/
70-
STParsedJSONArray(std::string const& name, Json::Value const& json);
71-
72-
STParsedJSONArray() = delete;
73-
STParsedJSONArray(STParsedJSONArray const&) = delete;
74-
STParsedJSONArray&
75-
operator=(STParsedJSONArray const&) = delete;
76-
~STParsedJSONArray() = default;
77-
78-
/** The STArray if the parse was successful. */
79-
std::optional<STArray> array;
80-
81-
/** On failure, an appropriate set of error values. */
82-
Json::Value error;
83-
};
84-
8557
} // namespace ripple
8658

8759
#endif

src/libxrpl/protocol/Permissions.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ Permission::getInstance()
101101
return instance;
102102
}
103103

104+
std::optional<std::string>
105+
Permission::getPermissionName(std::uint32_t const value) const
106+
{
107+
auto const permissionValue = static_cast<GranularPermissionType>(value);
108+
if (auto const granular = getGranularName(permissionValue))
109+
return *granular;
110+
111+
// not a granular permission, check if it maps to a transaction type
112+
auto const txType = permissionToTxType(value);
113+
if (auto const* item = TxFormats::getInstance().findByType(txType);
114+
item != nullptr)
115+
return item->getName();
116+
117+
return std::nullopt;
118+
}
119+
104120
std::optional<std::uint32_t>
105121
Permission::getGranularValue(std::string const& name) const
106122
{

src/libxrpl/protocol/STInteger.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ STUInt8::getText() const
6262
if (transResultInfo(TER::fromInt(value_), token, human))
6363
return human;
6464

65+
// LCOV_EXCL_START
6566
JLOG(debugLog().error())
6667
<< "Unknown result code in metadata: " << value_;
68+
// LCOV_EXCL_STOP
6769
}
6870

6971
return std::to_string(value_);
@@ -80,8 +82,10 @@ STUInt8::getJson(JsonOptions) const
8082
if (transResultInfo(TER::fromInt(value_), token, human))
8183
return token;
8284

85+
// LCOV_EXCL_START
8386
JLOG(debugLog().error())
8487
<< "Unknown result code in metadata: " << value_;
88+
// LCOV_EXCL_STOP
8589
}
8690

8791
return value_;
@@ -171,6 +175,13 @@ template <>
171175
std::string
172176
STUInt32::getText() const
173177
{
178+
if (getFName() == sfPermissionValue)
179+
{
180+
auto const permissionName =
181+
Permission::getInstance().getPermissionName(value_);
182+
if (permissionName)
183+
return *permissionName;
184+
}
174185
return std::to_string(value_);
175186
}
176187

@@ -180,23 +191,10 @@ STUInt32::getJson(JsonOptions) const
180191
{
181192
if (getFName() == sfPermissionValue)
182193
{
183-
auto const permissionValue =
184-
static_cast<GranularPermissionType>(value_);
185-
auto const granular =
186-
Permission::getInstance().getGranularName(permissionValue);
187-
188-
if (granular)
189-
{
190-
return *granular;
191-
}
192-
else
193-
{
194-
auto const txType =
195-
Permission::getInstance().permissionToTxType(value_);
196-
auto item = TxFormats::getInstance().findByType(txType);
197-
if (item != nullptr)
198-
return item->getName();
199-
}
194+
auto const permissionName =
195+
Permission::getInstance().getPermissionName(value_);
196+
if (permissionName)
197+
return *permissionName;
200198
}
201199

202200
return value_;

src/libxrpl/protocol/STParsedJSON.cpp

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ constexpr std::
8383
return static_cast<U1>(value);
8484
}
8585

86-
static std::string
86+
// LCOV_EXCL_START
87+
static inline std::string
8788
make_name(std::string const& object, std::string const& field)
8889
{
8990
if (field.empty())
@@ -92,90 +93,90 @@ make_name(std::string const& object, std::string const& field)
9293
return object + "." + field;
9394
}
9495

95-
static Json::Value
96+
static inline Json::Value
9697
not_an_object(std::string const& object, std::string const& field)
9798
{
9899
return RPC::make_error(
99100
rpcINVALID_PARAMS,
100101
"Field '" + make_name(object, field) + "' is not a JSON object.");
101102
}
102103

103-
static Json::Value
104+
static inline Json::Value
104105
not_an_object(std::string const& object)
105106
{
106107
return not_an_object(object, "");
107108
}
108109

109-
static Json::Value
110+
static inline Json::Value
110111
not_an_array(std::string const& object)
111112
{
112113
return RPC::make_error(
113114
rpcINVALID_PARAMS, "Field '" + object + "' is not a JSON array.");
114115
}
115116

116-
static Json::Value
117+
static inline Json::Value
117118
unknown_field(std::string const& object, std::string const& field)
118119
{
119120
return RPC::make_error(
120121
rpcINVALID_PARAMS,
121122
"Field '" + make_name(object, field) + "' is unknown.");
122123
}
123124

124-
static Json::Value
125+
static inline Json::Value
125126
out_of_range(std::string const& object, std::string const& field)
126127
{
127128
return RPC::make_error(
128129
rpcINVALID_PARAMS,
129130
"Field '" + make_name(object, field) + "' is out of range.");
130131
}
131132

132-
static Json::Value
133+
static inline Json::Value
133134
bad_type(std::string const& object, std::string const& field)
134135
{
135136
return RPC::make_error(
136137
rpcINVALID_PARAMS,
137138
"Field '" + make_name(object, field) + "' has bad type.");
138139
}
139140

140-
static Json::Value
141+
static inline Json::Value
141142
invalid_data(std::string const& object, std::string const& field)
142143
{
143144
return RPC::make_error(
144145
rpcINVALID_PARAMS,
145146
"Field '" + make_name(object, field) + "' has invalid data.");
146147
}
147148

148-
static Json::Value
149+
static inline Json::Value
149150
invalid_data(std::string const& object)
150151
{
151152
return invalid_data(object, "");
152153
}
153154

154-
static Json::Value
155+
static inline Json::Value
155156
array_expected(std::string const& object, std::string const& field)
156157
{
157158
return RPC::make_error(
158159
rpcINVALID_PARAMS,
159160
"Field '" + make_name(object, field) + "' must be a JSON array.");
160161
}
161162

162-
static Json::Value
163+
static inline Json::Value
163164
string_expected(std::string const& object, std::string const& field)
164165
{
165166
return RPC::make_error(
166167
rpcINVALID_PARAMS,
167168
"Field '" + make_name(object, field) + "' must be a string.");
168169
}
169170

170-
static Json::Value
171+
static inline Json::Value
171172
too_deep(std::string const& object)
172173
{
173174
return RPC::make_error(
174175
rpcINVALID_PARAMS,
175176
"Field '" + object + "' exceeds nesting depth limit.");
176177
}
177178

178-
static Json::Value
179+
static inline Json::Value
179180
singleton_expected(std::string const& object, unsigned int index)
180181
{
181182
return RPC::make_error(
@@ -184,7 +185,7 @@ singleton_expected(std::string const& object, unsigned int index)
184185
"]' must be an object with a single key/object value.");
185186
}
186187

187-
static Json::Value
188+
static inline Json::Value
188189
template_mismatch(SField const& sField)
189190
{
190191
return RPC::make_error(
@@ -193,14 +194,15 @@ template_mismatch(SField const& sField)
193194
"' contents did not meet requirements for that type.");
194195
}
195196

196-
static Json::Value
197+
static inline Json::Value
197198
non_object_in_array(std::string const& item, Json::UInt index)
198199
{
199200
return RPC::make_error(
200201
rpcINVALID_PARAMS,
201202
"Item '" + item + "' at index " + std::to_string(index) +
202203
" is not an object. Arrays may only contain objects.");
203204
}
205+
// LCOV_EXCL_STOP
204206

205207
template <class STResult, class Integer>
206208
static std::optional<detail::STVar>
@@ -385,10 +387,13 @@ parseLeaf(
385387

386388
auto const& field = SField::getField(fieldName);
387389

390+
// checked in parseObject
388391
if (field == sfInvalid)
389392
{
393+
// LCOV_EXCL_START
390394
error = unknown_field(json_name, fieldName);
391395
return ret;
396+
// LCOV_EXCL_STOP
392397
}
393398

394399
switch (field.fieldType)
@@ -760,6 +765,12 @@ parseLeaf(
760765
AccountID uAccount, uIssuer;
761766
Currency uCurrency;
762767

768+
if (!account && !currency && !issuer)
769+
{
770+
error = invalid_data(element_name);
771+
return ret;
772+
}
773+
763774
if (account)
764775
{
765776
// human account id
@@ -1153,24 +1164,4 @@ STParsedJSONObject::STParsedJSONObject(
11531164
object = parseObject(name, json, sfGeneric, 0, error);
11541165
}
11551166

1156-
//------------------------------------------------------------------------------
1157-
1158-
STParsedJSONArray::STParsedJSONArray(
1159-
std::string const& name,
1160-
Json::Value const& json)
1161-
{
1162-
using namespace STParsedJSONDetail;
1163-
auto arr = parseArray(name, json, sfGeneric, 0, error);
1164-
if (!arr)
1165-
array.reset();
1166-
else
1167-
{
1168-
auto p = dynamic_cast<STArray*>(&arr->get());
1169-
if (p == nullptr)
1170-
array.reset();
1171-
else
1172-
array = std::move(*p);
1173-
}
1174-
}
1175-
11761167
} // namespace ripple

0 commit comments

Comments
 (0)