#include "hns-test.h"
#include "dns-proto.h"

#include <sstream>
#include <vector>

namespace hns {
namespace test {

#define fingerprint_hex \
  "0c72ac70b745ac19998811b131d662c9ac69dbdbe7cb23e5b514b56664c5d3d6"

TEST_F(LibraryTest, ParseTlsaReplyOK) {
  std::string fp_hex(fingerprint_hex);

  DNSPacket pkt;

  pkt.set_qid(0x1234)
    .set_response()
    .set_rd()
    .set_ra()
    .set_ad()
    .add_question(new DNSQuestion("_443._tcp.www.ietf.org", ns_t_tlsa))
    .add_answer(new DNSTlsaRR("_443._tcp.www.ietf.org", 1500, 3, 1, 1, fp_hex));

  std::vector<byte> data = pkt.data();

  struct hns_tlsa_reply *tlsa = nullptr;
  EXPECT_EQ(HNS_SUCCESS,
    hns_parse_tlsa_reply(data.data(), data.size(), &tlsa));
  ASSERT_NE(nullptr, tlsa);

  ASSERT_EQ(tlsa->usage, 3);
  ASSERT_EQ(tlsa->selector, 1);
  ASSERT_EQ(tlsa->matching_type, 1);
  ASSERT_NE(tlsa->certificate, nullptr);

  ASSERT_EQ(
    hns_tlsa_name_size("www.ietf.org", "tcp", 443),
    strlen("_443._tcp.www.ietf.org"));

  char enc_name[256];

  ASSERT_NE(hns_tlsa_encode_name(
    "www.ietf.org.", "tcp", 443, enc_name, 255), 0);
  ASSERT_EQ(strcmp(enc_name, "_443._tcp.www.ietf.org."), 0);

  std::string cert_hex(
    "3082055130820439a003020102020821302c85a10fd0eb300d06092a"
    "864886f70d01010b05003081c6310b30090603550406130255533110"
    "300e060355040813074172697a6f6e61311330110603550407130a53"
    "636f74747364616c6531253023060355040a131c537461726669656c"
    "6420546563686e6f6c6f676965732c20496e632e3133303106035504"
    "0b132a687474703a2f2f63657274732e737461726669656c64746563"
    "682e636f6d2f7265706f7369746f72792f313430320603550403132b"
    "537461726669656c6420536563757265204365727469666963617465"
    "20417574686f72697479202d204732301e170d313730363132313031"
    "3230305a170d3138303831313233313235305a30383121301f060355"
    "040b1318446f6d61696e20436f6e74726f6c2056616c696461746564"
    "3113301106035504030c0a2a2e696574662e6f726730820122300d06"
    "092a864886f70d01010105000382010f003082010a0282010100b678"
    "cb9b5b6cd610b87cd36ddd49d00fdcbbddb0d7822a13c9661223771a"
    "fca35fb93c9d8c54c46009aa9ee1ab04e51f81b987dcf450d009a9aa"
    "58e05936f77772bc1f8505bc35904cf492db520c8e6f28a9dd7d9c4e"
    "46c568ab1aed280d1ce158b1ded5167b66ba2108f83fd4e72ccb8ed0"
    "66e2b695a141d2b126ad158198461b0e5dffc317f95dfa88aa93b29c"
    "9074dad857ad309c4cced1cf516a085b7f655641e61298d92f824dd1"
    "3d38854a96be161cc7eebd461e3a3d3a1a4c0b1491cdbf6b54fada2f"
    "23799847152e63852ea644b7302f8eb5ecbb5ca9b53fe20d8d65a87a"
    "09ba88202ddd5baca55cfeea9536c609779eb0153101c1e1f45e40f1"
    "f4a50203010001a38201ce308201ca300c0603551d130101ff040230"
    "00301d0603551d250416301406082b0601050507030106082b060105"
    "05070302300e0603551d0f0101ff0404030205a0303c0603551d1f04"
    "3530333031a02fa02d862b687474703a2f2f63726c2e737461726669"
    "656c64746563682e636f6d2f736669673273312d35362e63726c3063"
    "0603551d20045c305a304e060b6086480186fd6e01071701303f303d"
    "06082b060105050702011631687474703a2f2f636572746966696361"
    "7465732e737461726669656c64746563682e636f6d2f7265706f7369"
    "746f72792f3008060667810c01020130818606082b06010505070101"
    "047a3078302a06082b06010505073001861e687474703a2f2f6f6373"
    "702e737461726669656c64746563682e636f6d2f304a06082b060105"
    "05073002863e687474703a2f2f6365727469666963617465732e7374"
    "61726669656c64746563682e636f6d2f7265706f7369746f72792f73"
    "666967322e6372742e646572301f0603551d23041830168014254581"
    "685026383d3b2d2cbecd6ad9b63db36663301f0603551d1104183016"
    "820a2a2e696574662e6f72678208696574662e6f7267301d0603551d"
    "0e0416041406fe0babd8e6746efcc4730285f7a9487ed1344f300d06"
    "092a864886f70d01010b05000382010100e48dd77b333d85f376df04"
    "d8c04b4b8dd261889f667aedd7163fe925ce9ec9cc66fea9d6cf66c2"
    "346872995048743e5ef585c9fc2e585897e9fb189506856ec268e56a"
    "8731e4c5fc9f225b3a049fa92fa988c796def31a92574e1bcf23c165"
    "c3e7b85762fdd5c082313d8921477cd1fc82d8625e0ce08883c88d0a"
    "76c6e2437901d64f9650e6053117ca8f79c3b92fa23033364546e13a"
    "498cc5ca7b0e30e1a3b0f6099182e6dc963052e943cfb3ad44007037"
    "9c1e81ce6d4e0ab9f476853942cb09d31a90b21198c0b43f3488fa6d"
    "fe718a19eae7bff2af66cbb1e07a47153184399512a772bb3718cc68"
    "58dc983a96c6a817e8a00f27c30136d1fa5a717da2"
  );

  size_t len = cert_hex.length();
  std::vector<uint8_t> cert;

  for (size_t i = 0; i < len; i += 2) {
    std::string byte = cert_hex.substr(i, 2);
    unsigned char chr = (unsigned char)strtol(byte.c_str(), nullptr, 16);
    cert.push_back(chr);
  }

  ASSERT_EQ(hns_tlsa_verify(tlsa, cert.data(), cert.size()), 1);

  hns_free_data(tlsa);
}

}  
}
#include "hns-test.h"
#include "dns-proto.h"

#include <sstream>
#include <vector>

namespace hns {
namespace test {

#define fingerprint_hex \
  "0c72ac70b745ac19998811b131d662c9ac69dbdbe7cb23e5b514b56664c5d3d6"

TEST_F(LibraryTest, ParseTlsaReplyOK) {
  std::string fp_hex(fingerprint_hex);

  DNSPacket pkt;

  pkt.set_qid(0x1234)
    .set_response()
    .set_rd()
    .set_ra()
    .set_ad()
    .add_question(new DNSQuestion("_443._tcp.www.ietf.org", ns_t_tlsa))
    .add_answer(new DNSTlsaRR("_443._tcp.www.ietf.org", 1500, 3, 1, 1, fp_hex));

  std::vector<byte> data = pkt.data();

  struct hns_tlsa_reply *tlsa = nullptr;
  EXPECT_EQ(HNS_SUCCESS,
    hns_parse_tlsa_reply(data.data(), data.size(), &tlsa));
  ASSERT_NE(nullptr, tlsa);

  ASSERT_EQ(tlsa->usage, 3);
  ASSERT_EQ(tlsa->selector, 1);
  ASSERT_EQ(tlsa->matching_type, 1);
  ASSERT_NE(tlsa->certificate, nullptr);

  ASSERT_EQ(
    hns_tlsa_name_size("www.ietf.org", "tcp", 443),
    strlen("_443._tcp.www.ietf.org"));

  char enc_name[256];

  ASSERT_NE(hns_tlsa_encode_name(
    "www.ietf.org.", "tcp", 443, enc_name, 255), 0);
  ASSERT_EQ(strcmp(enc_name, "_443._tcp.www.ietf.org."), 0);

  std::string cert_hex(
    "3082055130820439a003020102020821302c85a10fd0eb300d06092a"
    "864886f70d01010b05003081c6310b30090603550406130255533110"
    "300e060355040813074172697a6f6e61311330110603550407130a53"
    "636f74747364616c6531253023060355040a131c537461726669656c"
    "6420546563686e6f6c6f676965732c20496e632e3133303106035504"
    "0b132a687474703a2f2f63657274732e737461726669656c64746563"
    "682e636f6d2f7265706f7369746f72792f313430320603550403132b"
    "537461726669656c6420536563757265204365727469666963617465"
    "20417574686f72697479202d204732301e170d313730363132313031"
    "3230305a170d3138303831313233313235305a30383121301f060355"
    "040b1318446f6d61696e20436f6e74726f6c2056616c696461746564"
    "3113301106035504030c0a2a2e696574662e6f726730820122300d06"
    "092a864886f70d01010105000382010f003082010a0282010100b678"
    "cb9b5b6cd610b87cd36ddd49d00fdcbbddb0d7822a13c9661223771a"
    "fca35fb93c9d8c54c46009aa9ee1ab04e51f81b987dcf450d009a9aa"
    "58e05936f77772bc1f8505bc35904cf492db520c8e6f28a9dd7d9c4e"
    "46c568ab1aed280d1ce158b1ded5167b66ba2108f83fd4e72ccb8ed0"
    "66e2b695a141d2b126ad158198461b0e5dffc317f95dfa88aa93b29c"
    "9074dad857ad309c4cced1cf516a085b7f655641e61298d92f824dd1"
    "3d38854a96be161cc7eebd461e3a3d3a1a4c0b1491cdbf6b54fada2f"
    "23799847152e63852ea644b7302f8eb5ecbb5ca9b53fe20d8d65a87a"
    "09ba88202ddd5baca55cfeea9536c609779eb0153101c1e1f45e40f1"
    "f4a50203010001a38201ce308201ca300c0603551d130101ff040230"
    "00301d0603551d250416301406082b0601050507030106082b060105"
    "05070302300e0603551d0f0101ff0404030205a0303c0603551d1f04"
    "3530333031a02fa02d862b687474703a2f2f63726c2e737461726669"
    "656c64746563682e636f6d2f736669673273312d35362e63726c3063"
    "0603551d20045c305a304e060b6086480186fd6e01071701303f303d"
    "06082b060105050702011631687474703a2f2f636572746966696361"
    "7465732e737461726669656c64746563682e636f6d2f7265706f7369"
    "746f72792f3008060667810c01020130818606082b06010505070101"
    "047a3078302a06082b06010505073001861e687474703a2f2f6f6373"
    "702e737461726669656c64746563682e636f6d2f304a06082b060105"
    "05073002863e687474703a2f2f6365727469666963617465732e7374"
    "61726669656c64746563682e636f6d2f7265706f7369746f72792f73"
    "666967322e6372742e646572301f0603551d23041830168014254581"
    "685026383d3b2d2cbecd6ad9b63db36663301f0603551d1104183016"
    "820a2a2e696574662e6f72678208696574662e6f7267301d0603551d"
    "0e0416041406fe0babd8e6746efcc4730285f7a9487ed1344f300d06"
    "092a864886f70d01010b05000382010100e48dd77b333d85f376df04"
    "d8c04b4b8dd261889f667aedd7163fe925ce9ec9cc66fea9d6cf66c2"
    "346872995048743e5ef585c9fc2e585897e9fb189506856ec268e56a"
    "8731e4c5fc9f225b3a049fa92fa988c796def31a92574e1bcf23c165"
    "c3e7b85762fdd5c082313d8921477cd1fc82d8625e0ce08883c88d0a"
    "76c6e2437901d64f9650e6053117ca8f79c3b92fa23033364546e13a"
    "498cc5ca7b0e30e1a3b0f6099182e6dc963052e943cfb3ad44007037"
    "9c1e81ce6d4e0ab9f476853942cb09d31a90b21198c0b43f3488fa6d"
    "fe718a19eae7bff2af66cbb1e07a47153184399512a772bb3718cc68"
    "58dc983a96c6a817e8a00f27c30136d1fa5a717da2"
  );

  size_t len = cert_hex.length();
  std::vector<uint8_t> cert;

  for (size_t i = 0; i < len; i += 2) {
    std::string byte = cert_hex.substr(i, 2);
    unsigned char chr = (unsigned char)strtol(byte.c_str(), nullptr, 16);
    cert.push_back(chr);
  }

  ASSERT_EQ(hns_tlsa_verify(tlsa, cert.data(), cert.size()), 1);

  hns_free_data(tlsa);
}

}  // namespace test
}  // namespace hns
