Copilot commented on code in PR #13141: URL: https://github.com/apache/trafficserver/pull/13141#discussion_r3222512550
########## src/mgmt/rpc/handlers/common/unit_tests/test_record_yaml.cc: ########## @@ -0,0 +1,146 @@ +/** + @section license License + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#include <catch2/catch_test_macros.hpp> + +#include <cstddef> +#include <cstring> +#include <yaml-cpp/yaml.h> + +#include "../convert.h" + +namespace +{ +// +// Build minimal RecRecord fixtures aimed at the YAML encoder under test in +// place. RecRecord embeds a RecMutex (with an atomic) and is therefore not +// copyable, so the helpers operate on an output reference and only populate +// the fields the encoder actually reads. The lock and the unused half of the +// stat_meta / config_meta union stay zero-initialized. +// +void +fill_string_config(RecRecord &r, const char *name, const char *current, const char *def, RecAccessT access) +{ + r.rec_type = RECT_CONFIG; + r.data_type = RECD_STRING; + r.name = name; + r.data.rec_string = const_cast<char *>(current); + r.data_default.rec_string = const_cast<char *>(def); + r.config_meta.access_type = access; +} + +void +fill_int_config(RecRecord &r, const char *name, RecInt current, RecInt def, RecAccessT access) +{ + r.rec_type = RECT_CONFIG; + r.data_type = RECD_INT; + r.name = name; + r.data.rec_int = current; + r.data_default.rec_int = def; + r.config_meta.access_type = access; +} + +void +fill_int_stat(RecRecord &r, const char *name, RecInt current) +{ + r.rec_type = RECT_PROCESS; // STAT category + r.data_type = RECD_INT; + r.name = name; + r.data.rec_int = current; + r.data_default.rec_int = 0; + + // stat_meta is the active union member after value-initialization of + // RecRecord; explicitly re-establish that to keep the contract clear + // and to give the encoder a well-defined object to read. + r.stat_meta = RecStatMeta{}; + + // Plant a RECA_NO_ACCESS bit pattern at the storage location that the + // overlaid RecConfigMeta would expose as access_type, without making + // config_meta the active union member. A hypothetical encoder that + // reads record.config_meta.access_type without a REC_TYPE_IS_CONFIG + // guard would observe RECA_NO_ACCESS and incorrectly suppress the + // value fields below; the well-defined encoder path only reads + // stat_meta for STAT records. + static_assert(sizeof(RecStatMeta) >= offsetof(RecConfigMeta, access_type) + sizeof(RecAccessT), + "RecStatMeta must fully overlap RecConfigMeta::access_type"); + const RecAccessT bad_access = RECA_NO_ACCESS; + std::memcpy(reinterpret_cast<char *>(&r.stat_meta) + offsetof(RecConfigMeta, access_type), &bad_access, sizeof(bad_access)); +} + +YAML::Node +encode_record_node(const RecRecord &record) +{ + // The encoder wraps the actual record fields in a {"record": ...} envelope. + return YAML::convert<RecRecord>::encode(record)[constants_rec::REC]; +} +} // namespace + +TEST_CASE("Record YAML encoder exposes values for default-access config records", "[mgmt][rpc][record_yaml]") +{ + RecRecord rec{}; + fill_string_config(rec, "proxy.config.example.normal", "current", "default", RECA_NULL); + YAML::Node node = encode_record_node(rec); + + REQUIRE(node[constants_rec::DATA_TYPE].as<std::string>() == "STRING"); + REQUIRE(node[constants_rec::CURRENT_VALUE].as<std::string>() == "current"); + REQUIRE(node[constants_rec::DEFAULT_VALUE].as<std::string>() == "default"); +} + +TEST_CASE("Record YAML encoder withholds values for RECA_NO_ACCESS string records", "[mgmt][rpc][record_yaml][no_access]") +{ + RecRecord rec{}; + fill_string_config(rec, "proxy.config.example.secret", "supersecret", "secret-default", RECA_NO_ACCESS); + YAML::Node node = encode_record_node(rec); + + // Type label and metadata are still expected so callers can enumerate the + // record's existence and tier. + REQUIRE(node[constants_rec::DATA_TYPE].as<std::string>() == "STRING"); + REQUIRE(node[constants_rec::NAME].as<std::string>() == "proxy.config.example.secret"); + REQUIRE(node[constants_rec::CONFIG_META][constants_rec::ACCESS_TYPE].as<int>() == RECA_NO_ACCESS); + + // The protected value fields must not appear in the encoded output. + REQUIRE_FALSE(node[constants_rec::CURRENT_VALUE]); + REQUIRE_FALSE(node[constants_rec::DEFAULT_VALUE]); +} Review Comment: `REQUIRE_FALSE(node[...])` is using YAML::Node’s non-const `operator[]`, which inserts the missing key into the map. That makes the node defined and will cause this test to fail (or mutate the node under test). Make `node` const before indexing (so the const `operator[]` returns a Zombie node when missing), or explicitly check `!node[...].IsDefined()` via a const access path. ########## src/mgmt/rpc/handlers/common/unit_tests/test_record_yaml.cc: ########## @@ -0,0 +1,146 @@ +/** + @section license License + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#include <catch2/catch_test_macros.hpp> + +#include <cstddef> +#include <cstring> +#include <yaml-cpp/yaml.h> + +#include "../convert.h" + +namespace +{ +// +// Build minimal RecRecord fixtures aimed at the YAML encoder under test in +// place. RecRecord embeds a RecMutex (with an atomic) and is therefore not +// copyable, so the helpers operate on an output reference and only populate +// the fields the encoder actually reads. The lock and the unused half of the +// stat_meta / config_meta union stay zero-initialized. +// +void +fill_string_config(RecRecord &r, const char *name, const char *current, const char *def, RecAccessT access) +{ + r.rec_type = RECT_CONFIG; + r.data_type = RECD_STRING; + r.name = name; + r.data.rec_string = const_cast<char *>(current); + r.data_default.rec_string = const_cast<char *>(def); + r.config_meta.access_type = access; +} + +void +fill_int_config(RecRecord &r, const char *name, RecInt current, RecInt def, RecAccessT access) +{ + r.rec_type = RECT_CONFIG; + r.data_type = RECD_INT; + r.name = name; + r.data.rec_int = current; + r.data_default.rec_int = def; + r.config_meta.access_type = access; +} + +void +fill_int_stat(RecRecord &r, const char *name, RecInt current) +{ + r.rec_type = RECT_PROCESS; // STAT category + r.data_type = RECD_INT; + r.name = name; + r.data.rec_int = current; + r.data_default.rec_int = 0; + + // stat_meta is the active union member after value-initialization of + // RecRecord; explicitly re-establish that to keep the contract clear + // and to give the encoder a well-defined object to read. + r.stat_meta = RecStatMeta{}; + + // Plant a RECA_NO_ACCESS bit pattern at the storage location that the + // overlaid RecConfigMeta would expose as access_type, without making + // config_meta the active union member. A hypothetical encoder that + // reads record.config_meta.access_type without a REC_TYPE_IS_CONFIG + // guard would observe RECA_NO_ACCESS and incorrectly suppress the + // value fields below; the well-defined encoder path only reads + // stat_meta for STAT records. + static_assert(sizeof(RecStatMeta) >= offsetof(RecConfigMeta, access_type) + sizeof(RecAccessT), + "RecStatMeta must fully overlap RecConfigMeta::access_type"); + const RecAccessT bad_access = RECA_NO_ACCESS; + std::memcpy(reinterpret_cast<char *>(&r.stat_meta) + offsetof(RecConfigMeta, access_type), &bad_access, sizeof(bad_access)); +} + +YAML::Node +encode_record_node(const RecRecord &record) +{ + // The encoder wraps the actual record fields in a {"record": ...} envelope. + return YAML::convert<RecRecord>::encode(record)[constants_rec::REC]; +} +} // namespace + +TEST_CASE("Record YAML encoder exposes values for default-access config records", "[mgmt][rpc][record_yaml]") +{ + RecRecord rec{}; + fill_string_config(rec, "proxy.config.example.normal", "current", "default", RECA_NULL); + YAML::Node node = encode_record_node(rec); + + REQUIRE(node[constants_rec::DATA_TYPE].as<std::string>() == "STRING"); + REQUIRE(node[constants_rec::CURRENT_VALUE].as<std::string>() == "current"); + REQUIRE(node[constants_rec::DEFAULT_VALUE].as<std::string>() == "default"); +} + +TEST_CASE("Record YAML encoder withholds values for RECA_NO_ACCESS string records", "[mgmt][rpc][record_yaml][no_access]") +{ + RecRecord rec{}; + fill_string_config(rec, "proxy.config.example.secret", "supersecret", "secret-default", RECA_NO_ACCESS); + YAML::Node node = encode_record_node(rec); + + // Type label and metadata are still expected so callers can enumerate the + // record's existence and tier. + REQUIRE(node[constants_rec::DATA_TYPE].as<std::string>() == "STRING"); + REQUIRE(node[constants_rec::NAME].as<std::string>() == "proxy.config.example.secret"); + REQUIRE(node[constants_rec::CONFIG_META][constants_rec::ACCESS_TYPE].as<int>() == RECA_NO_ACCESS); + + // The protected value fields must not appear in the encoded output. + REQUIRE_FALSE(node[constants_rec::CURRENT_VALUE]); + REQUIRE_FALSE(node[constants_rec::DEFAULT_VALUE]); +} + +TEST_CASE("Record YAML encoder withholds values for RECA_NO_ACCESS int records", "[mgmt][rpc][record_yaml][no_access]") +{ + RecRecord rec{}; + fill_int_config(rec, "proxy.config.example.token", 42, 0, RECA_NO_ACCESS); + YAML::Node node = encode_record_node(rec); + + REQUIRE(node[constants_rec::DATA_TYPE].as<std::string>() == "INT"); + REQUIRE_FALSE(node[constants_rec::CURRENT_VALUE]); + REQUIRE_FALSE(node[constants_rec::DEFAULT_VALUE]); Review Comment: Same issue as above: indexing a non-const YAML::Node with `operator[]` will create `current_value` / `default_value` entries when they’re absent, so `REQUIRE_FALSE(node[...])` won’t correctly assert omission. Use a const node (or a const reference) when checking for missing keys, or check `IsDefined()` on a const access. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
