Add test case for tokenEndECKeyValue function in SignatureReader.cpp
[platform/core/security/cert-svc.git] / tests / vcore / test-certificate.cpp
1 /*
2  * Copyright (c) 2016 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         result = cert.getCommonName(Certificate::FIELD_SUBJECT);
37         RUNNER_ASSERT_MSG(!result.empty(), "No common name");
38         RUNNER_ASSERT_MSG(!result.compare("www.verisign.com"), "CommonName mismatch");
39         result = cert.getCommonName(Certificate::FIELD_ISSUER);
40         RUNNER_ASSERT_MSG(!result.empty(), "No common name");
41         RUNNER_ASSERT_MSG(!result.compare("VeriSign Class 3 Extended Validation SSL SGC CA"),
42                                           "CommonName mismatch");
43         result = cert.getCountryName();
44         RUNNER_ASSERT_MSG(!result.empty(), "No country");
45         RUNNER_ASSERT_MSG(!result.compare("US"), "Country mismatch");
46 }
47
48 /*
49  * test: Certificate::getFingerprint
50  * description: Certificate should parse data passed to object constructor.
51  * expected: Function fingerprint should return valid fingerprint.
52  */
53 RUNNER_TEST(T0032_Certificate)
54 {
55         Certificate cert(TestData::certVerisign, Certificate::FORM_BASE64);
56         Certificate::Fingerprint fin =
57                 cert.getFingerprint(Certificate::FINGERPRINT_SHA1);
58         unsigned char buff[20] = {
59                 0xb9, 0x72, 0x1e, 0xd5, 0x49,
60                 0xed, 0xbf, 0x31, 0x84, 0xd8,
61                 0x27, 0x0c, 0xfe, 0x03, 0x11,
62                 0x19, 0xdf, 0xc2, 0x2b, 0x0a
63         };
64         RUNNER_ASSERT_MSG(fin.size() == 20, "Wrong size of fingerprint");
65
66         for (size_t i = 0; i < 20; ++i) {
67                 RUNNER_ASSERT_MSG(fin[i] == buff[i], "Fingerprint mismatch");
68         }
69 }
70
71 /*
72  * test: Certificate::getAlternativeNameDNS
73  * description: Certificate should parse data passed to object constructor.
74  * expected: Function getAlternativeNameDNS should return list of
75  * alternativeNames hardcoded in certificate.
76  */
77 RUNNER_TEST(T0033_Certificate)
78 {
79         Certificate cert(TestData::certVerisign, Certificate::FORM_BASE64);
80         Certificate::AltNameSet nameSet = cert.getAlternativeNameDNS();
81         RUNNER_ASSERT(nameSet.size() == 8);
82         std::string str("verisign.com");
83         RUNNER_ASSERT(nameSet.find(str) != nameSet.end());
84         str = std::string("fake.com");
85         RUNNER_ASSERT(nameSet.find(str) == nameSet.end());
86 }
87
88 /*
89  * test: Certificate::isCA
90  * description: Certificate should parse data passed to object constructor.
91  * expected: 1st and 2nd certificate should be identified as CA.
92  */
93 RUNNER_TEST(T0034_Certificate_isCA)
94 {
95         Certificate cert1(TestData::googleCA, Certificate::FORM_BASE64);
96         RUNNER_ASSERT(cert1.isCA() > 0);
97         Certificate cert2(TestData::google2nd, Certificate::FORM_BASE64);
98         RUNNER_ASSERT(cert2.isCA() > 0);
99         Certificate cert3(TestData::google3rd, Certificate::FORM_BASE64);
100         RUNNER_ASSERT(cert3.isCA() == 0);
101 }
102
103 /*
104  * test: Certificate::getAlternativeNameURI
105  * description: Certificate should parse data passed to object constructor.
106  * expected: Function getAlternativeNameURI should return list of
107  * alternativeNames hardcoded in certificate.
108  */
109 RUNNER_TEST(T0035_Certificate_AltNameURI)
110 {
111         Certificate cert(TestData::certGenUriIncluded, Certificate::FORM_BASE64);
112         Certificate::AltNameSet nameSet = cert.getAlternativeNameURI();
113         RUNNER_ASSERT(nameSet.size() == 8);
114         std::string str("URN:tizen:deviceid=2.0#R1q+lPsGCM7PAEPvzUcc38xPCLw=");
115         RUNNER_ASSERT(nameSet.find(str) != nameSet.end());
116         str = std::string("fakeURI");
117         RUNNER_ASSERT(nameSet.find(str) == nameSet.end());
118 }