/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"

#include "cert.h"
#include "certdb.h"
#include "nss.h"
#include "secitem.h"
#include "secerr.h"
#include "nss_scoped_ptrs.h"

namespace nss_test {

// Self-signed RSA certificate with CN=Test, emailAddress=test@example.com
// in the subject DN. Generated with:
//   openssl req -x509 -newkey rsa:2048 -nodes -days 365
//     -subj "/CN=Test/emailAddress=test@example.com"
static unsigned char kTestCertWithEmail[] = {
    0x30, 0x82, 0x03, 0x41, 0x30, 0x82, 0x02, 0x29, 0xa0, 0x03, 0x02, 0x01,
    0x02, 0x02, 0x14, 0x49, 0xe1, 0x23, 0x34, 0xe4, 0x4d, 0xda, 0x35, 0x34,
    0x18, 0xfc, 0x66, 0xa4, 0x3f, 0xa7, 0x1d, 0xed, 0x80, 0xd2, 0x71, 0x30,
    0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
    0x05, 0x00, 0x30, 0x30, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04,
    0x03, 0x0c, 0x04, 0x54, 0x65, 0x73, 0x74, 0x31, 0x1f, 0x30, 0x1d, 0x06,
    0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
    0x74, 0x65, 0x73, 0x74, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65,
    0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x36, 0x30, 0x33,
    0x32, 0x36, 0x32, 0x31, 0x32, 0x32, 0x30, 0x36, 0x5a, 0x17, 0x0d, 0x32,
    0x37, 0x30, 0x33, 0x32, 0x36, 0x32, 0x31, 0x32, 0x32, 0x30, 0x36, 0x5a,
    0x30, 0x30, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c,
    0x04, 0x54, 0x65, 0x73, 0x74, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
    0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x74, 0x65,
    0x73, 0x74, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63,
    0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
    0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
    0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc8,
    0x62, 0x36, 0x4d, 0x62, 0xaa, 0xe4, 0x28, 0x7d, 0x11, 0xf7, 0x2c, 0x3d,
    0x98, 0xb6, 0xac, 0xe3, 0xc1, 0x54, 0xca, 0xa3, 0x0f, 0x7c, 0x11, 0x3d,
    0x03, 0xd0, 0x93, 0x4b, 0x49, 0x75, 0x28, 0xae, 0xa5, 0xf5, 0xd1, 0x24,
    0x47, 0x3f, 0xaa, 0xb6, 0xae, 0xe3, 0x22, 0x78, 0xda, 0x9e, 0x5d, 0x27,
    0x55, 0x99, 0xa0, 0xbb, 0x2e, 0xd1, 0x99, 0x15, 0xf7, 0x9e, 0xa8, 0x20,
    0xb8, 0x22, 0x08, 0x41, 0xfd, 0xbc, 0x91, 0x83, 0x0a, 0xe9, 0x25, 0x8b,
    0x66, 0xd0, 0x55, 0x90, 0xbb, 0x0d, 0x59, 0x7d, 0x0f, 0x05, 0xf3, 0x0f,
    0x51, 0x8a, 0xd1, 0xf9, 0x49, 0xc8, 0xb2, 0x13, 0x4e, 0x7d, 0xa2, 0xb7,
    0x33, 0x89, 0x67, 0x6b, 0xd5, 0x2b, 0x58, 0xb4, 0x74, 0xe0, 0x0f, 0xfe,
    0x28, 0x0f, 0xce, 0xac, 0x28, 0xad, 0xf7, 0x7a, 0x5c, 0x73, 0x41, 0x9e,
    0xa0, 0xdc, 0x9c, 0x46, 0x1d, 0xd0, 0x94, 0xb4, 0xd8, 0x61, 0x6a, 0x3c,
    0xfc, 0x9f, 0xe2, 0x70, 0x52, 0xb3, 0x2f, 0xe5, 0xb8, 0x16, 0xff, 0xc8,
    0xa3, 0x58, 0xe8, 0x92, 0xdb, 0xed, 0x63, 0x44, 0x27, 0x0c, 0xb3, 0x84,
    0xb3, 0x45, 0x22, 0x26, 0x7c, 0x91, 0x62, 0xd4, 0xa4, 0xf5, 0xdb, 0xa0,
    0xb7, 0x82, 0xf7, 0xcb, 0x05, 0x19, 0x36, 0x46, 0x3d, 0xa3, 0x70, 0x87,
    0x5b, 0xf2, 0x9d, 0xb6, 0x21, 0x75, 0x0f, 0x18, 0x61, 0xfa, 0xc0, 0x79,
    0x62, 0xe8, 0xa2, 0xa7, 0xea, 0xe2, 0x19, 0x49, 0x10, 0x1b, 0x1e, 0x9f,
    0x86, 0x9f, 0x0e, 0xef, 0xfc, 0xc1, 0x80, 0xb7, 0xc9, 0x86, 0x1f, 0xf1,
    0x93, 0x4d, 0x3d, 0xd7, 0x2f, 0xf3, 0x76, 0x2a, 0x1b, 0xd7, 0xb8, 0xdd,
    0xe7, 0x34, 0x13, 0x0f, 0x4b, 0x04, 0x05, 0xa4, 0x34, 0x19, 0xda, 0xf5,
    0x84, 0x8b, 0x92, 0x3a, 0xb5, 0x7f, 0xef, 0xfa, 0x40, 0x11, 0x05, 0x04,
    0xee, 0xbe, 0xbb, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x53, 0x30, 0x51,
    0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x69,
    0x11, 0x73, 0x73, 0xf3, 0xb4, 0x40, 0xe7, 0x0a, 0xe7, 0xa6, 0xe5, 0x6f,
    0xb4, 0xa9, 0x1b, 0x1f, 0x62, 0xe4, 0x0d, 0x30, 0x1f, 0x06, 0x03, 0x55,
    0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x69, 0x11, 0x73, 0x73,
    0xf3, 0xb4, 0x40, 0xe7, 0x0a, 0xe7, 0xa6, 0xe5, 0x6f, 0xb4, 0xa9, 0x1b,
    0x1f, 0x62, 0xe4, 0x0d, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01,
    0x01, 0xff, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0d, 0x06,
    0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00,
    0x03, 0x82, 0x01, 0x01, 0x00, 0x1e, 0x77, 0xf0, 0x78, 0x0f, 0x0b, 0x6d,
    0x2a, 0xdf, 0xb3, 0x2b, 0xba, 0x21, 0x23, 0x63, 0x26, 0x47, 0xc2, 0xea,
    0x73, 0xdd, 0x25, 0x85, 0xc4, 0xd3, 0x65, 0x70, 0xca, 0x80, 0x6c, 0xaa,
    0xfb, 0x7d, 0x3c, 0xfe, 0x73, 0x82, 0xfd, 0xc5, 0x2b, 0x4f, 0xc1, 0x21,
    0xc2, 0x95, 0x5d, 0x18, 0xde, 0x07, 0x92, 0x0e, 0xfa, 0xcf, 0xf2, 0x9b,
    0x45, 0x48, 0x8f, 0xb4, 0x26, 0xd2, 0xcd, 0xef, 0x48, 0x52, 0x42, 0x97,
    0x9f, 0x45, 0x64, 0x39, 0x23, 0x30, 0x90, 0xd4, 0xaf, 0x8a, 0xc2, 0x2a,
    0x73, 0xc3, 0xb9, 0xc8, 0x50, 0xe2, 0x47, 0xcc, 0xa9, 0xd7, 0x84, 0x43,
    0x1b, 0xd1, 0xcc, 0xf6, 0xc4, 0x94, 0xa5, 0xfd, 0x8b, 0xee, 0x2e, 0x8c,
    0x8e, 0x95, 0xfd, 0xd9, 0x0b, 0xb4, 0x9a, 0xed, 0xaa, 0xf8, 0x13, 0xbc,
    0x2b, 0xb5, 0xee, 0xec, 0x42, 0xce, 0x4e, 0x3c, 0x99, 0x7a, 0x82, 0xf8,
    0x7d, 0x79, 0xec, 0xf5, 0xe5, 0xa9, 0xd1, 0x54, 0xd9, 0xb4, 0x7f, 0x67,
    0xe5, 0xb3, 0x5f, 0xf5, 0x45, 0x68, 0x3d, 0xb0, 0xd4, 0x5e, 0x0e, 0x0a,
    0xd3, 0xc4, 0xd2, 0x04, 0x2a, 0x0f, 0x40, 0x0a, 0xf4, 0x5c, 0xa2, 0x09,
    0x78, 0x76, 0xf4, 0x8b, 0x97, 0xa6, 0x3e, 0x72, 0xc6, 0x73, 0x3f, 0x2d,
    0xe6, 0x1a, 0xad, 0xd4, 0xf3, 0x7b, 0x32, 0x42, 0x2e, 0x93, 0xca, 0x1c,
    0xd5, 0xe2, 0x06, 0x7c, 0x96, 0xf6, 0x28, 0x41, 0x94, 0xc2, 0x1c, 0x68,
    0xa3, 0x07, 0xa2, 0x06, 0xde, 0x85, 0x0a, 0xf4, 0xb7, 0x31, 0x43, 0x40,
    0x94, 0xda, 0xf2, 0x9b, 0xd0, 0x0a, 0xa7, 0x01, 0xa1, 0x95, 0x77, 0x4c,
    0x64, 0xb1, 0x3b, 0xea, 0x12, 0xcf, 0x4a, 0xd0, 0x02, 0x3a, 0xcb, 0x24,
    0xb1, 0x0d, 0x6c, 0xfc, 0xde, 0xd9, 0x1a, 0xb0, 0xb0, 0x95, 0xe4, 0x39,
    0xab, 0x98, 0x23, 0x2e, 0x72, 0xa7, 0x52, 0x0b, 0x4c,
};

class SMimeProfileTest : public ::testing::Test {};

TEST_F(SMimeProfileTest, FindSMimeProfileTempCert) {
  SECItem certDER = {siBuffer, kTestCertWithEmail, sizeof(kTestCertWithEmail)};

  ScopedCERTCertificate cert(CERT_NewTempCertificate(
      CERT_GetDefaultCertDB(), &certDER, nullptr, PR_FALSE, PR_TRUE));
  ASSERT_NE(nullptr, cert.get());
  ASSERT_NE(nullptr, cert->emailAddr);
  ASSERT_NE('\0', cert->emailAddr[0]);

  unsigned char profileData[] = {0x30, 0x00};
  SECItem emailProfile = {siBuffer, profileData, sizeof(profileData)};

  unsigned char timeData[] = {'2', '6', '0', '1', '0', '1', '0',
                              '0', '0', '0', '0', '0', 'Z'};
  SECItem profileTime = {siBuffer, timeData, sizeof(timeData)};

  SECStatus rv = CERT_SaveSMimeProfile(cert.get(), &emailProfile, &profileTime);
  ASSERT_EQ(SECSuccess, rv);

  SECItem* found = CERT_FindSMimeProfile(cert.get());
  ASSERT_NE(nullptr, found);
  ASSERT_NE(nullptr, found->data);
  ASSERT_EQ(sizeof(profileData), found->len);
  EXPECT_EQ(0, memcmp(found->data, profileData, sizeof(profileData)));

  SECITEM_FreeItem(found, PR_TRUE);
}

}  // namespace nss_test
