| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <optional> |
| 16 | #include <string> |
| 17 | #include <string_view> |
| 18 |
| 19 | #include <openssl/pki/certificate.h> |
| 20 | #include <gmock/gmock.h> |
| 21 |
| 22 | #include "string_util.h" |
| 23 | #include "test_helpers.h" |
| 24 |
| 25 | TEST(CertificateTest, FromPEM) { |
| 26 | std::string diagnostic; |
| 27 | std::unique_ptr<bssl::Certificate> cert( |
| 28 | bssl::Certificate::FromPEM("nonsense", &diagnostic)); |
| 29 | EXPECT_FALSE(cert); |
| 30 |
| 31 | cert = bssl::Certificate::FromPEM(bssl::ReadTestFileToString( |
| 32 | "testdata/verify_unittest/self-issued.pem"), &diagnostic); |
| 33 | EXPECT_TRUE(cert); |
| 34 | } |
| 35 |
| 36 | TEST(CertificateTest, IsSelfIssued) { |
| 37 | std::string diagnostic; |
| 38 | const std::string leaf = |
| 39 | bssl::ReadTestFileToString("testdata/verify_unittest/google-leaf.der"); |
| 40 | std::unique_ptr<bssl::Certificate> leaf_cert( |
| 41 | bssl::Certificate::FromDER(bssl::StringAsBytes(leaf), &diagnostic)); |
| 42 | EXPECT_TRUE(leaf_cert); |
| 43 | EXPECT_FALSE(leaf_cert->IsSelfIssued()); |
| 44 |
| 45 | const std::string self_issued = |
| 46 | bssl::ReadTestFileToString("testdata/verify_unittest/self-issued.pem"); |
| 47 | std::unique_ptr<bssl::Certificate> self_issued_cert( |
| 48 | bssl::Certificate::FromPEM(self_issued, &diagnostic)); |
| 49 | EXPECT_TRUE(self_issued_cert); |
| 50 | EXPECT_TRUE(self_issued_cert->IsSelfIssued()); |
| 51 | } |
| 52 |
| 53 | TEST(CertificateTest, Validity) { |
| 54 | std::string diagnostic; |
| 55 | const std::string leaf = |
| 56 | bssl::ReadTestFileToString("testdata/verify_unittest/google-leaf.der"); |
| 57 | std::unique_ptr<bssl::Certificate> cert( |
| 58 | bssl::Certificate::FromDER(bssl::StringAsBytes(leaf), &diagnostic)); |
| 59 | EXPECT_TRUE(cert); |
| 60 |
| 61 | bssl::Certificate::Validity validity = cert->GetValidity(); |
| 62 | EXPECT_EQ(validity.not_before, 1498644466); |
| 63 | EXPECT_EQ(validity.not_after, 1505899620); |
| 64 | } |
| 65 |
| 66 | TEST(CertificateTest, SerialNumber) { |
| 67 | std::string diagnostic; |
| 68 | const std::string leaf = |
| 69 | bssl::ReadTestFileToString("testdata/verify_unittest/google-leaf.der"); |
| 70 | std::unique_ptr<bssl::Certificate> cert( |
| 71 | bssl::Certificate::FromDER(bssl::StringAsBytes(leaf), &diagnostic)); |
| 72 | EXPECT_TRUE(cert); |
| 73 |
| 74 | EXPECT_EQ(bssl::string_util::HexEncode(cert->GetSerialNumber()), |
| 75 | "0118F044A8F31892"); |
| 76 | } |
| 77 |