// Copyright 2025 The BoringSSL Authors
//
// Licensed 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
//
//     https://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 <benchmark/benchmark.h>

#include <openssl/base.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/x509.h>

#include "./internal.h"

namespace {
const uint8_t kCert[] = {
    0x30, 0x82, 0x01, 0x3b, 0x30, 0x81, 0xe2, 0xa0, 0x03, 0x02, 0x01, 0x02,
    0x02, 0x01, 0x01, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
    0x04, 0x03, 0x02, 0x30, 0x1c, 0x31, 0x1a, 0x30, 0x18, 0x06, 0x03, 0x55,
    0x04, 0x03, 0x13, 0x11, 0x42, 0x61, 0x73, 0x69, 0x63, 0x20, 0x43, 0x6f,
    0x6e, 0x73, 0x74, 0x72, 0x61, 0x69, 0x6e, 0x74, 0x73, 0x30, 0x20, 0x17,
    0x0d, 0x30, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30,
    0x30, 0x5a, 0x18, 0x0f, 0x32, 0x31, 0x30, 0x30, 0x30, 0x31, 0x30, 0x31,
    0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x1c, 0x31, 0x1a, 0x30,
    0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x11, 0x42, 0x61, 0x73, 0x69,
    0x63, 0x20, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x72, 0x61, 0x69, 0x6e, 0x74,
    0x73, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d,
    0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
    0x03, 0x42, 0x00, 0x04, 0x91, 0x2a, 0xd8, 0xbc, 0x55, 0x6d, 0x91, 0x92,
    0x66, 0xbe, 0x2a, 0xdf, 0x63, 0x42, 0x43, 0x0d, 0x8a, 0xd9, 0x42, 0xb3,
    0x49, 0x79, 0xc3, 0xcb, 0xb7, 0x49, 0x2c, 0x26, 0xec, 0x76, 0x51, 0x12,
    0xf9, 0x9c, 0x04, 0x22, 0x41, 0x8d, 0x52, 0x11, 0x11, 0xa7, 0x11, 0xa8,
    0x8d, 0x2e, 0x9a, 0x1c, 0xd1, 0xd9, 0x5e, 0x4d, 0x2f, 0x30, 0x41, 0x3f,
    0xdc, 0x8f, 0x0f, 0xf0, 0x3e, 0x0a, 0x1e, 0x57, 0xa3, 0x13, 0x30, 0x11,
    0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05,
    0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48,
    0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x48, 0x00, 0x30, 0x45, 0x02, 0x20,
    0x4c, 0xdb, 0x36, 0x69, 0x03, 0xc3, 0x66, 0xcf, 0x8f, 0x6a, 0x5e, 0x4b,
    0x03, 0x57, 0xc0, 0x2b, 0x20, 0xb8, 0x00, 0xa4, 0xcd, 0x37, 0xe2, 0x44,
    0xfe, 0xf1, 0x18, 0x9d, 0xd2, 0xa1, 0x17, 0x16, 0x02, 0x21, 0x00, 0xdf,
    0x6f, 0xb2, 0x23, 0x0e, 0x85, 0xf1, 0xff, 0x71, 0x81, 0x9e, 0xca, 0xe1,
    0xb7, 0x5e, 0x0d, 0x52, 0x6c, 0xd4, 0x05, 0xda, 0xa4, 0x41, 0xa7, 0xc9,
    0xbc, 0x2f, 0x9e, 0x1c, 0x87, 0x6d, 0x40};

void BM_SpeedX509(benchmark::State &state) {
  for (auto _ : state) {
    const uint8_t *inp = kCert;
    bssl::UniquePtr<X509> x509(d2i_X509(nullptr, &inp, sizeof(kCert)));
    if (!x509) {
      state.SkipWithError("d2i_X509 failed.");
      return;
    }
  }
}
BSSL_BENCH_LAZY_REGISTER() {
  BENCHMARK(BM_SpeedX509)->Apply(bssl::bench::SetThreads);
}

}  // namespace
