Copilot commented on code in PR #13072: URL: https://github.com/apache/trafficserver/pull/13072#discussion_r3054673904
########## plugins/experimental/jax_fingerprint/ja4/test.cc: ########## @@ -0,0 +1,531 @@ +/** @file + * + Unit tests for JA4 fingerprint calculation. + + @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 "ja4.h" +#include "datasource.h" + +#include <catch2/catch_test_macros.hpp> +#include <openssl/sha.h> + +#include <algorithm> +#include <cctype> +#include <cstring> +#include <string> +#include <string_view> +#include <unordered_map> Review Comment: This test uses `std::vector` (`_ciphers` / `_extensions`) but the file doesn’t include `<vector>`, which can break compilation depending on transitive includes. Add the missing standard header to keep the test self-contained. ```suggestion #include <unordered_map> #include <vector> ``` ########## plugins/experimental/jax_fingerprint/ja4/test.cc: ########## @@ -0,0 +1,531 @@ +/** @file + * + Unit tests for JA4 fingerprint calculation. + + @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 "ja4.h" +#include "datasource.h" + +#include <catch2/catch_test_macros.hpp> +#include <openssl/sha.h> + +#include <algorithm> +#include <cctype> +#include <cstring> +#include <string> +#include <string_view> +#include <unordered_map> + +namespace +{ + +class MockDatasource : public ja4::Datasource +{ +public: + std::string_view + get_first_alpn() override + { + return this->_first_alpn; + } + + void + get_cipher_suites_hash(unsigned char out[32]) override + { + if (this->_ciphers.empty()) { + memset(out, 0, 32); + return; + } + auto sorted = this->_ciphers; + std::sort(sorted.begin(), sorted.end()); + SHA256_CTX ctx; + SHA256_Init(&ctx); + for (size_t i = 0; i < sorted.size(); ++i) { + char buf[5]; + char *p = buf; + if (i != 0) { + *p = ','; + p += 1; + } + uint16_t c = sorted[i]; + uint8_t h1 = (c & 0xF000) >> 12; + uint8_t l1 = (c & 0x0F00) >> 8; + uint8_t h2 = (c & 0x00F0) >> 4; + uint8_t l2 = c & 0x000F; + p[0] = h1 <= 9 ? ('0' + h1) : ('a' + h1 - 10); + p[1] = l1 <= 9 ? ('0' + l1) : ('a' + l1 - 10); + p[2] = h2 <= 9 ? ('0' + h2) : ('a' + h2 - 10); + p[3] = l2 <= 9 ? ('0' + l2) : ('a' + l2 - 10); + p += 4; + SHA256_Update(&ctx, buf, p - buf); + } + SHA256_Final(out, &ctx); + } + + void + get_extension_hash(unsigned char out[32]) override + { + if (this->_extensions.empty()) { + memset(out, 0, 32); + return; + } + auto sorted = this->_extensions; + std::sort(sorted.begin(), sorted.end()); + SHA256_CTX ctx; + SHA256_Init(&ctx); + for (size_t i = 0; i < sorted.size(); ++i) { + char buf[5]; + char *p = buf; + if (i != 0) { + *p = ','; + p += 1; + } + uint16_t e = sorted[i]; + uint8_t h1 = (e & 0xF000) >> 12; + uint8_t l1 = (e & 0x0F00) >> 8; + uint8_t h2 = (e & 0x00F0) >> 4; + uint8_t l2 = e & 0x000F; + p[0] = h1 <= 9 ? ('0' + h1) : ('a' + h1 - 10); + p[1] = l1 <= 9 ? ('0' + l1) : ('a' + l1 - 10); + p[2] = h2 <= 9 ? ('0' + h2) : ('a' + h2 - 10); + p[3] = l2 <= 9 ? ('0' + l2) : ('a' + l2 - 10); + p += 4; + SHA256_Update(&ctx, buf, p - buf); + } + SHA256_Final(out, &ctx); + } + + void + set_protocol(ja4::Datasource::Protocol protocol) + { + this->_protocol = protocol; + } + void + set_version(int version) + { + this->_version = version; + } + void + set_first_alpn(std::string first_alpn) + { + this->_first_alpn = first_alpn; + } + void + add_cipher(std::uint16_t cipher) + { + if (_is_GREASE(cipher)) { + return; + } + + ++this->_n_ciphers; + this->_ciphers.push_back(cipher); + } + + void + add_extension(uint16_t extension) + { + if (EXT_SNI == extension) { + this->_SNI_type = SNI::to_domain; + this->_has_SNI = true; + return; + } + if (EXT_ALPN == extension) { + this->_has_ALPN = true; + return; + } + if (_is_GREASE(extension)) { + return; + } + + ++this->_n_extensions; + this->_extensions.push_back(extension); + } + +private: + std::string _first_alpn; + + std::vector<std::uint16_t> _ciphers; + std::vector<std::uint16_t> _extensions; + SNI _SNI_type{SNI::to_IP}; +}; + +std::string_view +SHA256_12(std::string_view in) +{ + uint8_t hash[32]; + SHA256(reinterpret_cast<const uint8_t *>(in.data()), in.size(), hash); + + static char out[12]; + for (int i = 0; i < 6; ++i) { + uint8_t h = hash[i] >> 4; + uint8_t l = hash[i] & 0x0F; + out[i * 2] = h <= 9 ? '0' + h : 'a' + h - 10; + out[i * 2 + 1] = l <= 9 ? '0' + l : 'a' + l - 10; + } + return {out, sizeof(out)}; +} + +} // namespace + +static std::string call_JA4(ja4::Datasource &datasource); + +TEST_CASE("JA4") +{ + MockDatasource datasource{}; + + SECTION("Given the protocol is TCP, " Review Comment: This test section says the protocol is "TCP", but the enum/value being set is `ja4::Datasource::Protocol::TLS` (and JA4’s protocol codes are TLS/QUIC/DTLS). Consider updating the description string to "TLS" (or "TLS over TCP") to match the actual behavior being tested. ```suggestion SECTION("Given the protocol is TLS, " ``` -- 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]
