maskit commented on code in PR #13072:
URL: https://github.com/apache/trafficserver/pull/13072#discussion_r3054649075


##########
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, "
+          "when we create a JA4 fingerprint, "
+          "then the first character thereof should be 't'.")
+  {
+    datasource.set_protocol(ja4::Datasource::Protocol::TLS);
+
+    CHECK("t" == call_JA4(datasource).substr(0, 1));
+  }
+
+  SECTION("Given the protocol is QUIC, "
+          "when we create a JA4 fingerprint, "
+          "then the first character thereof should be 'q'.")
+  {
+    datasource.set_protocol(ja4::Datasource::Protocol::QUIC);
+    CHECK(call_JA4(datasource).starts_with('q'));
+  }
+
+  SECTION("Given the protocol is DTLS, "
+          "when we create a JA4 fingerprint, "
+          "then the first character thereof should be 'd'.")
+  {
+    datasource.set_protocol(ja4::Datasource::Protocol::DTLS);
+    CHECK(call_JA4(datasource).starts_with('d'));
+  }
+
+  SECTION("Given the TLS version is unknown, "
+          "when we create a JA4 fingerprint, "
+          "then indices [1,2] thereof should contain \"00\".")
+  {
+    datasource.set_version(0x123);
+    CHECK("00" == call_JA4(datasource).substr(1, 2));
+    datasource.set_version(0x234);
+    CHECK("00" == call_JA4(datasource).substr(1, 2));

Review Comment:
   Not a problem. Unrelated to the test.



-- 
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]

Reply via email to