src/net/cert/internal/parse_certificate_unittest.cc
void RunTbsCertificateTestGivenVersion(const std::string& file_name,
                                       CertificateVersion expected_version) {
  std::string data;
  std::string expected_serial_number;
  std::string expected_signature_algorithm;
  std::string expected_issuer;
  std::string expected_validity_not_before;
  std::string expected_validity_not_after;
  std::string expected_subject;
  std::string expected_spki;
  std::string expected_issuer_unique_id;
  std::string expected_subject_unique_id;
  std::string expected_extensions;
  std::string expected_errors;

  // Read the certificate data and test expectations from a single PEM file.
  const PemBlockMapping mappings[] = {
      {"TBS CERTIFICATE", &data},
      {"SIGNATURE ALGORITHM", &expected_signature_algorithm, true},
      {"SERIAL NUMBER", &expected_serial_number, true},
      {"ISSUER", &expected_issuer, true},
      {"VALIDITY NOTBEFORE", &expected_validity_not_before, true},
      {"VALIDITY NOTAFTER", &expected_validity_not_after, true},
      {"SUBJECT", &expected_subject, true},
      {"SPKI", &expected_spki, true},
      {"ISSUER UNIQUE ID", &expected_issuer_unique_id, true},
      {"SUBJECT UNIQUE ID", &expected_subject_unique_id, true},
      {"EXTENSIONS", &expected_extensions, true},
      {"ERRORS", &expected_errors, true},
  };
  std::string test_file_path = GetFilePath(file_name);
  ASSERT_TRUE(ReadTestDataFromPemFile(test_file_path, mappings));

  bool expected_result = !expected_spki.empty();

  ParsedTbsCertificate parsed;
  CertErrors errors;
  bool actual_result =
      ParseTbsCertificate(der::Input(&data), {}, &parsed, &errors);

  EXPECT_EQ(expected_result, actual_result);
  VerifyCertErrors(expected_errors, errors, test_file_path);

  if (!expected_result || !actual_result)
    return;

  // Ensure that the ParsedTbsCertificate matches expectations.
  EXPECT_EQ(expected_version, parsed.version);

  EXPECT_EQ(der::Input(&expected_serial_number), parsed.serial_number);
  EXPECT_EQ(der::Input(&expected_signature_algorithm),
            parsed.signature_algorithm_tlv);

  EXPECT_EQ(der::Input(&expected_issuer), parsed.issuer_tlv);

  // In the test expectations PEM file, validity is described as a
  // textual string of the parsed value (rather than as DER).
  EXPECT_EQ(expected_validity_not_before, ToString(parsed.validity_not_before));
  EXPECT_EQ(expected_validity_not_after, ToString(parsed.validity_not_after));

  EXPECT_EQ(der::Input(&expected_subject), parsed.subject_tlv);
  EXPECT_EQ(der::Input(&expected_spki), parsed.spki_tlv);

  EXPECT_EQ(der::Input(&expected_issuer_unique_id),
            parsed.issuer_unique_id.bytes());
  EXPECT_EQ(!expected_issuer_unique_id.empty(), parsed.has_issuer_unique_id);
  EXPECT_EQ(der::Input(&expected_subject_unique_id),
            parsed.subject_unique_id.bytes());
  EXPECT_EQ(!expected_subject_unique_id.empty(), parsed.has_subject_unique_id);

  EXPECT_EQ(der::Input(&expected_extensions), parsed.extensions_tlv);
  EXPECT_EQ(!expected_extensions.empty(), parsed.has_extensions);
}
// Loads tbsCertificate data and expectations from the PEM file |file_name|.
// Verifies that parsing the TBSCertificate succeeds, and each parsed field
// matches the expectations.
//
// TODO(eroman): Get rid of the |expected_version| parameter -- this should be
// encoded in the test expectations file.
Loads tbsCertificate data and expectations from the PEM file |file_name|.
Verifies that parsing the TBSCertificate succeeds, and each parsed field
matches the expectations.

Get rid of the |expected_version| parameter -- this should be
encoded in the test expectations file.
Loads tbsCertificate data and expectations from the PEM file |file_name| . Verifies that parsing the TBSCertificate succeeds and each parsed field matches the expectations . Get rid of the |expected_version| parameter -- this should be encoded in the test expectations file .
void RunTbsCertificateTestGivenVersion(const std::string& file_name,
                                       CertificateVersion expected_version) {
  std::string data;
  std::string expected_serial_number;
  std::string expected_signature_algorithm;
  std::string expected_issuer;
  std::string expected_validity_not_before;
  std::string expected_validity_not_after;
  std::string expected_subject;
  std::string expected_spki;
  std::string expected_issuer_unique_id;
  std::string expected_subject_unique_id;
  std::string expected_extensions;
  std::string expected_errors;
  const PemBlockMapping mappings[] = {
      {"TBS CERTIFICATE", &data},
      {"SIGNATURE ALGORITHM", &expected_signature_algorithm, true},
      {"SERIAL NUMBER", &expected_serial_number, true},
      {"ISSUER", &expected_issuer, true},
      {"VALIDITY NOTBEFORE", &expected_validity_not_before, true},
      {"VALIDITY NOTAFTER", &expected_validity_not_after, true},
      {"SUBJECT", &expected_subject, true},
      {"SPKI", &expected_spki, true},
      {"ISSUER UNIQUE ID", &expected_issuer_unique_id, true},
      {"SUBJECT UNIQUE ID", &expected_subject_unique_id, true},
      {"EXTENSIONS", &expected_extensions, true},
      {"ERRORS", &expected_errors, true},
  };
  std::string test_file_path = GetFilePath(file_name);
  ASSERT_TRUE(ReadTestDataFromPemFile(test_file_path, mappings));
  bool expected_result = !expected_spki.empty();
  ParsedTbsCertificate parsed;
  CertErrors errors;
  bool actual_result =
      ParseTbsCertificate(der::Input(&data), {}, &parsed, &errors);
  EXPECT_EQ(expected_result, actual_result);
  VerifyCertErrors(expected_errors, errors, test_file_path);
  if (!expected_result || !actual_result)
    return;
  EXPECT_EQ(expected_version, parsed.version);
  EXPECT_EQ(der::Input(&expected_serial_number), parsed.serial_number);
  EXPECT_EQ(der::Input(&expected_signature_algorithm),
            parsed.signature_algorithm_tlv);
  EXPECT_EQ(der::Input(&expected_issuer), parsed.issuer_tlv);
  EXPECT_EQ(expected_validity_not_before, ToString(parsed.validity_not_before));
  EXPECT_EQ(expected_validity_not_after, ToString(parsed.validity_not_after));
  EXPECT_EQ(der::Input(&expected_subject), parsed.subject_tlv);
  EXPECT_EQ(der::Input(&expected_spki), parsed.spki_tlv);
  EXPECT_EQ(der::Input(&expected_issuer_unique_id),
            parsed.issuer_unique_id.bytes());
  EXPECT_EQ(!expected_issuer_unique_id.empty(), parsed.has_issuer_unique_id);
  EXPECT_EQ(der::Input(&expected_subject_unique_id),
            parsed.subject_unique_id.bytes());
  EXPECT_EQ(!expected_subject_unique_id.empty(), parsed.has_subject_unique_id);
  EXPECT_EQ(der::Input(&expected_extensions), parsed.extensions_tlv);
  EXPECT_EQ(!expected_extensions.empty(), parsed.has_extensions);
}
void RunTbsCertificateTestGivenVersion ( const std :: string & file_name , CertificateVersion expected_version ) { std :: string data ; std :: string expected_serial_number ; std :: string expected_signature_algorithm ; std :: string expected_issuer ; std :: string expected_validity_not_before ; std :: string expected_validity_not_after ; std :: string expected_subject ; std :: string expected_spki ; std :: string expected_issuer_unique_id ; std :: string expected_subject_unique_id ; std :: string expected_extensions ; std :: string expected_errors ; const PemBlockMapping mappings [ ] = { { " " , & data } , { " " , & expected_signature_algorithm , true } , { " " , & expected_serial_number , true } , { " " , & expected_issuer , true } , { " " , & expected_validity_not_before , true } , { " " , & expected_validity_not_after , true } , { " " , & expected_subject , true } , { " " , & expected_spki , true } , { " " , & expected_issuer_unique_id , true } , { " " , & expected_subject_unique_id , true } , { " " , & expected_extensions , true } , { " " , & expected_errors , true } , } ; std :: string test_file_path = GetFilePath ( file_name ) ; ASSERT_TRUE ( ReadTestDataFromPemFile ( test_file_path , mappings ) ) ; bool expected_result = ! expected_spki . empty ( ) ; ParsedTbsCertificate parsed ; CertErrors errors ; bool actual_result = ParseTbsCertificate ( der :: Input ( & data ) , { } , & parsed , & errors ) ; EXPECT_EQ ( expected_result , actual_result ) ; VerifyCertErrors ( expected_errors , errors , test_file_path ) ; if ( ! expected_result || ! actual_result ) return ; EXPECT_EQ ( expected_version , parsed . version ) ; EXPECT_EQ ( der :: Input ( & expected_serial_number ) , parsed . serial_number ) ; EXPECT_EQ ( der :: Input ( & expected_signature_algorithm ) , parsed . signature_algorithm_tlv ) ; EXPECT_EQ ( der :: Input ( & expected_issuer ) , parsed . issuer_tlv ) ; EXPECT_EQ ( expected_validity_not_before , ToString ( parsed . validity_not_before ) ) ; EXPECT_EQ ( expected_validity_not_after , ToString ( parsed . validity_not_after ) ) ; EXPECT_EQ ( der :: Input ( & expected_subject ) , parsed . subject_tlv ) ; EXPECT_EQ ( der :: Input ( & expected_spki ) , parsed . spki_tlv ) ; EXPECT_EQ ( der :: Input ( & expected_issuer_unique_id ) , parsed . issuer_unique_id . bytes ( ) ) ; EXPECT_EQ ( ! expected_issuer_unique_id . empty ( ) , parsed . has_issuer_unique_id ) ; EXPECT_EQ ( der :: Input ( & expected_subject_unique_id ) , parsed . subject_unique_id . bytes ( ) ) ; EXPECT_EQ ( ! expected_subject_unique_id . empty ( ) , parsed . has_subject_unique_id ) ; EXPECT_EQ ( der :: Input ( & expected_extensions ) , parsed . extensions_tlv ) ; EXPECT_EQ ( ! expected_extensions . empty ( ) , parsed . has_extensions ) ; }
Loads tbsCertificate data and expectations from the PEM file |file_name|.
Loads tbsCertificate data and expectations from the PEM file |file_name| .
// Read the certificate data and test expectations from a single PEM file. // Ensure that the ParsedTbsCertificate matches expectations. // In the test expectations PEM file, validity is described as a // textual string of the parsed value (rather than as DER).