Fix & Refactor internal unit tests
[platform/core/security/cert-svc.git] / tests / vcore / test-certificate.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include <string>
18 #include <dpl/test/test_runner.h>
19 #include <vcore/Certificate.h>
20
21 #include "test-common.h"
22
23 using namespace ValidationCore;
24
25 RUNNER_TEST_GROUP_INIT(T0030_Certificate)
26
27 /*
28  * test: class Certificate
29  * description: Certificate should parse data passed to object constructor.
30  * expected: Getters should be able to return certificate information.
31  */
32 RUNNER_TEST(T0031_Certificate)
33 {
34     Certificate cert(TestData::certVerisign, Certificate::FORM_BASE64);
35     std::string result;
36
37     result = cert.getCommonName(Certificate::FIELD_SUBJECT);
38     RUNNER_ASSERT_MSG(!result.empty(), "No common name");
39     RUNNER_ASSERT_MSG(!result.compare("www.verisign.com"), "CommonName mismatch");
40
41     result = cert.getCommonName(Certificate::FIELD_ISSUER);
42     RUNNER_ASSERT_MSG(!result.empty(), "No common name");
43     RUNNER_ASSERT_MSG(!result.compare("VeriSign Class 3 Extended Validation SSL SGC CA"),
44             "CommonName mismatch");
45
46     result = cert.getCountryName();
47     RUNNER_ASSERT_MSG(!result.empty(), "No country");
48     RUNNER_ASSERT_MSG(!result.compare("US"), "Country mismatch");
49 }
50
51 /*
52  * test: Certificate::getFingerprint
53  * description: Certificate should parse data passed to object constructor.
54  * expected: Function fingerprint should return valid fingerprint.
55  */
56 RUNNER_TEST(T0032_Certificate)
57 {
58     Certificate cert(TestData::certVerisign, Certificate::FORM_BASE64);
59
60     Certificate::Fingerprint fin =
61         cert.getFingerprint(Certificate::FINGERPRINT_SHA1);
62
63     unsigned char buff[20] = {
64         0xb9, 0x72, 0x1e, 0xd5, 0x49,
65         0xed, 0xbf, 0x31, 0x84, 0xd8,
66         0x27, 0x0c, 0xfe, 0x03, 0x11,
67         0x19, 0xdf, 0xc2, 0x2b, 0x0a};
68     RUNNER_ASSERT_MSG(fin.size() == 20, "Wrong size of fingerprint");
69
70     for (size_t i = 0; i<20; ++i) {
71         RUNNER_ASSERT_MSG(fin[i] == buff[i], "Fingerprint mismatch");
72     }
73 }
74
75 /*
76  * test: Certificate::getAlternativeNameDNS
77  * description: Certificate should parse data passed to object constructor.
78  * expected: Function getAlternativeNameDNS should return list of
79  * alternativeNames hardcoded in certificate.
80  */
81 RUNNER_TEST(T0033_Certificate)
82 {
83     Certificate cert(TestData::certVerisign, Certificate::FORM_BASE64);
84
85     Certificate::AltNameSet nameSet = cert.getAlternativeNameDNS();
86
87     RUNNER_ASSERT(nameSet.size() == 8);
88
89     std::string str("verisign.com");
90     RUNNER_ASSERT(nameSet.find(str) != nameSet.end());
91
92     str = std::string("fake.com");
93     RUNNER_ASSERT(nameSet.find(str) == nameSet.end());
94
95 }
96
97 /*
98  * test: Certificate::isCA
99  * description: Certificate should parse data passed to object constructor.
100  * expected: 1st and 2nd certificate should be identified as CA.
101  */
102 RUNNER_TEST(T0034_Certificate_isCA)
103 {
104     Certificate cert1(TestData::googleCA, Certificate::FORM_BASE64);
105     RUNNER_ASSERT(cert1.isCA() > 0);
106
107     Certificate cert2(TestData::google2nd, Certificate::FORM_BASE64);
108     RUNNER_ASSERT(cert2.isCA() > 0);
109
110     Certificate cert3(TestData::google3rd, Certificate::FORM_BASE64);
111     RUNNER_ASSERT(cert3.isCA() == 0);
112 }