Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / ck_manager / unittest / ckm_info_test.cpp
1 /******************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      LICENSE-2.0" target="_blank">http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19
20  ******************************************************************/
21
22 #include <gtest/gtest.h>
23 #include "ckm_info.h"
24
25 #define ISSUER_NAME_SIZE (16)
26 #define CERT_LEN 469
27
28 class CKMInfoTest : public ::testing::Test
29 {
30 public:
31     static void SetUpTestCase()
32     {
33     }
34
35     static void TearDownTestCase()
36     {
37     }
38
39     static const ByteArray publicKey;
40     static const ByteArray privateKey;
41     static const ByteArray derCode ;
42     static const long nextSN;
43     static const long crlSerialNum;
44     static const ByteArray caName;
45 };
46
47 const ByteArray CKMInfoTest::publicKey = {(uint8_t[])
48 {
49     0x8c, 0xc8, 0x92, 0x1d, 0xaa, 0x7f, 0xf0, 0xe4, 0xb2, 0x75, 0xd6, 0x4a, 0xf1, 0xd5, 0x14, 0x3f,
50     0x1a, 0x09, 0xc5, 0x3e, 0x52, 0xd6, 0xda, 0xa0, 0xbf, 0x90, 0x43, 0xd1, 0x6b, 0xfe, 0xd1, 0xb3,
51     0x75, 0x5c, 0xdd, 0x69, 0xac, 0x42, 0xa1, 0xcb, 0x03, 0x16, 0xee, 0xa4, 0x30, 0xa5, 0x8d, 0x36,
52     0x8f, 0xc5, 0x7b, 0xb4, 0xb5, 0x6a, 0x7d, 0x9b, 0x16, 0x04, 0x46, 0xab, 0xae, 0xbb, 0x56, 0xa1
53 }, PUBLIC_KEY_SIZE };
54
55 const ByteArray CKMInfoTest::privateKey = {(uint8_t[])
56 {
57     0xd6, 0xc8, 0x92, 0x16, 0x36, 0x7f, 0xf0, 0xe4, 0xb2, 0x75, 0xd6, 0x4a, 0xf1, 0x3f, 0x14, 0x30,
58     0x1a, 0x69, 0xc5, 0x3e, 0x52, 0xd6, 0xda, 0xa0, 0xbf, 0xae, 0x43, 0xd1, 0x6b, 0xfe, 0xd1, 0x36
59 }, PRIVATE_KEY_SIZE };
60
61 const ByteArray CKMInfoTest::derCode = {(uint8_t[])
62     {
63         0x30, 0x82, 0x01, 0xd1, 0x30, 0x82, 0x01, 0x77, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
64         0xd7, 0x56, 0x8c, 0xfc, 0x53, 0x18, 0xb0, 0xab, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce,
65         0x3d, 0x04, 0x03, 0x02, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
66         0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f,
67         0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04,
68         0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67,
69         0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x1e, 0x17, 0x0d, 0x31,
70         0x35, 0x30, 0x33, 0x31, 0x32, 0x31, 0x32, 0x32, 0x35, 0x31, 0x31, 0x5a, 0x17, 0x0d, 0x31, 0x37,
71         0x30, 0x33, 0x31, 0x31, 0x31, 0x32, 0x32, 0x35, 0x31, 0x31, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30,
72         0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
73         0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31,
74         0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e,
75         0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c,
76         0x74, 0x64, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06,
77         0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x8c, 0xc8, 0x92,
78         0x1d, 0xaa, 0x7f, 0xf0, 0xe4, 0xb2, 0x75, 0xd6, 0x4a, 0xf1, 0xd5, 0x14, 0x3f, 0x1a, 0x09, 0xc5,
79         0x3e, 0x52, 0xd6, 0xda, 0xa0, 0xbf, 0x90, 0x43, 0xd1, 0x6b, 0xfe, 0xd1, 0xb3, 0x75, 0x5c, 0xdd,
80         0x69, 0xac, 0x42, 0xa1, 0xcb, 0x03, 0x16, 0xee, 0xa4, 0x30, 0xa5, 0x8d, 0x36, 0x8f, 0xc5, 0x7b,
81         0xb4, 0xb5, 0x6a, 0x7d, 0x9b, 0x16, 0x04, 0x46, 0xab, 0xae, 0xbb, 0x56, 0xa1, 0xa3, 0x50, 0x30,
82         0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x5c, 0x0e, 0x30, 0xa8,
83         0x8e, 0x7f, 0xc9, 0x02, 0xcd, 0xa8, 0xed, 0x0d, 0x1a, 0x1b, 0xd9, 0x7d, 0xe6, 0xce, 0x2a, 0x59,
84         0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x5c, 0x0e, 0x30,
85         0xa8, 0x8e, 0x7f, 0xc9, 0x02, 0xcd, 0xa8, 0xed, 0x0d, 0x1a, 0x1b, 0xd9, 0x7d, 0xe6, 0xce, 0x2a,
86         0x59, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30,
87         0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x48, 0x00, 0x30, 0x45,
88         0x02, 0x21, 0x00, 0xf6, 0x79, 0xed, 0x69, 0xd5, 0xe5, 0xba, 0x42, 0x14, 0xfc, 0xce, 0x47, 0xf1,
89         0x61, 0x1c, 0x51, 0x11, 0x2b, 0xba, 0x04, 0x70, 0x56, 0x78, 0xaf, 0xa9, 0xa6, 0x98, 0x8f, 0x4b,
90         0xa8, 0x11, 0x67, 0x02, 0x20, 0x3a, 0xdf, 0xf1, 0x74, 0xc9, 0x2f, 0xfb, 0x84, 0x46, 0xde, 0xbc,
91         0x2d, 0xda, 0xe3, 0x05, 0xb4, 0x81, 0x31, 0x45, 0xf7, 0x3d, 0x71, 0x46, 0x07, 0xa7, 0xd8, 0xcb,
92         0xae, 0x1e, 0x1b, 0x1c, 0x5a
93     }, CERT_LEN };
94
95 const long CKMInfoTest::nextSN = 0x111111;
96
97 const long CKMInfoTest::crlSerialNum = 0x22222;
98
99 const ByteArray CKMInfoTest::caName = {(uint8_t[])
100 {
101     0x1a, 0x1a, 0x1a, 0x1d, 0xaa, 0x7f, 0xf0, 0xe4, 0xb2, 0x75, 0xd6, 0x4a, 0xf1, 0xd5, 0x14, 0x3f
102 }, ISSUER_NAME_SIZE };
103
104 //InitCRT test
105 TEST(CKMInfoTest, InitCRT)
106 {
107     ASSERT_EQ(PKI_SUCCESS, InitCRT());
108 }
109
110 //SaveCRT test
111 TEST(CKMInfoTest, SaveCRT)
112 {
113     ASSERT_EQ(PKI_SUCCESS, SaveCRT());
114 }
115
116 //InitCRL test
117 TEST(CKMInfoTest, InitCRL)
118 {
119     ASSERT_EQ(PKI_SUCCESS, InitCRL());
120 }
121
122 //SaveCRL test
123 TEST(CKMInfoTest, SaveCRL)
124 {
125     ASSERT_EQ(PKI_SUCCESS, SaveCRL());
126 }
127
128 //InitCKMInfo test
129 TEST(CKMInfoTest, InitCKMInfo)
130 {
131     ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
132 }
133
134 //SaveCKMInfo test
135 TEST(CKMInfoTest, SaveCKMInfo)
136 {
137     ASSERT_EQ(PKI_SUCCESS, SaveCKMInfo());
138 }
139
140 //SetGetNextSerialNumber test
141 TEST(CKMInfoTest, SetGetNextSerialNumber)
142 {
143     ASSERT_EQ(PKI_SUCCESS, SetNextSerialNumber(CKMInfoTest::nextSN));
144     long nSn = 0;
145     ASSERT_EQ(PKI_SUCCESS, GetNextSerialNumber(&nSn));
146     EXPECT_TRUE(nSn == CKMInfoTest::nextSN);
147 }
148
149 //SetGetCAPrivateKey test
150 TEST(CKMInfoTest, SetGetCAPrivateKey)
151 {
152     ASSERT_EQ(PKI_SUCCESS, SetCAPrivateKey(&CKMInfoTest::privateKey));
153
154     ByteArray privateKey = {0,0};
155     uint8_t caPrivKey[PRIVATE_KEY_SIZE] = {0};
156
157     privateKey.data = caPrivKey;
158     privateKey.len = PRIVATE_KEY_SIZE;
159
160     EXPECT_EQ(PKI_SUCCESS, GetCAPrivateKey(&privateKey));
161     EXPECT_TRUE(0 == memcmp(CKMInfoTest::privateKey.data, privateKey.data, PRIVATE_KEY_SIZE));
162 }
163
164 //SetGetCAPublicKey test
165 TEST(CKMInfoTest, SetGetCAPublicKey)
166 {
167     ASSERT_EQ(PKI_SUCCESS, SetCAPublicKey(&CKMInfoTest::publicKey));
168
169     ByteArray publicKey = {0,0};
170     uint8_t caPubKey[PUBLIC_KEY_SIZE] = {0};
171
172     publicKey.data = caPubKey;
173     publicKey.len = PUBLIC_KEY_SIZE;
174
175     EXPECT_EQ(PKI_SUCCESS, GetCAPublicKey(&publicKey));
176     EXPECT_TRUE(0 == memcmp(CKMInfoTest::publicKey.data, publicKey.data, PUBLIC_KEY_SIZE));
177 }
178
179 //SetGetCAName test
180 TEST(CKMInfoTest, SetGetCAName)
181 {
182     ASSERT_EQ(PKI_SUCCESS, SetCAName(&CKMInfoTest::caName));
183
184     ByteArray caName = {0,0};
185     uint8_t caIssName[PUBLIC_KEY_SIZE] = {0};
186
187     caName.data = caIssName;
188     caName.len = ISSUER_NAME_SIZE;
189
190     EXPECT_EQ(PKI_SUCCESS, GetCAName(&caName));
191     EXPECT_TRUE(0 == memcmp(CKMInfoTest::caName.data, caName.data, ISSUER_NAME_SIZE));
192 }
193
194 //SetGetCKMInfo test
195 TEST(CKMInfoTest, SetGetCKMInfo)
196 {
197     ASSERT_EQ(PKI_SUCCESS, SetCKMInfo(CKMInfoTest::nextSN, CKMInfoTest::crlSerialNum,
198             &CKMInfoTest::privateKey, &CKMInfoTest::publicKey, &CKMInfoTest::caName));
199
200     long nSn = 0;
201     long serialNum = 0;
202
203     ByteArray publicKey = {0,0};
204     uint8_t caIssName[ISSUER_MAX_CERT_SIZE] = {0};
205     uint8_t caPubKey[PUBLIC_KEY_SIZE] = {0};
206     uint8_t caPrivKey[PRIVATE_KEY_SIZE] = {0};
207
208     publicKey.data = caPubKey;
209     publicKey.len = PUBLIC_KEY_SIZE;
210
211     ByteArray privateKey = {0,0};
212     privateKey.data = caPrivKey;
213     privateKey.len = PRIVATE_KEY_SIZE;
214
215     ByteArray caName = {0,0};
216     caName.data = caIssName;
217     caName.len = ISSUER_NAME_SIZE;
218
219     EXPECT_EQ(PKI_SUCCESS, GetCKMInfo(&nSn, &serialNum,
220             &privateKey, &publicKey, &caName));
221
222     EXPECT_TRUE(nSn == CKMInfoTest::nextSN);
223     EXPECT_TRUE(serialNum == CKMInfoTest::crlSerialNum);
224     EXPECT_TRUE(0 == memcmp(CKMInfoTest::privateKey.data, privateKey.data, PRIVATE_KEY_SIZE));
225     EXPECT_TRUE(0 == memcmp(CKMInfoTest::publicKey.data, publicKey.data, PUBLIC_KEY_SIZE));
226     EXPECT_TRUE(0 == memcmp(CKMInfoTest::caName.data, caName.data, ISSUER_NAME_SIZE));
227 }
228
229 //SetGetCACertificate test
230 TEST(CKMInfoTest, SetGetCACertificate)
231 {
232     ASSERT_EQ(PKI_SUCCESS, SetCACertificate(&CKMInfoTest::derCode));
233
234     ByteArray der = {0,0};
235     uint8_t caCert[CERT_LEN] = {0};
236     der.data = caCert;
237     der.len = CERT_LEN;
238     EXPECT_TRUE(NULL != der.data);
239     EXPECT_EQ(PKI_SUCCESS, GetCACertificate(&der));
240     EXPECT_TRUE(0 == memcmp(CKMInfoTest::derCode.data, der.data, CERT_LEN));
241 }
242
243 //SetGetCRLSerialNumber test
244 TEST(CKMInfoTest, SetGetCRLSerialNumber)
245 {
246     ASSERT_EQ(PKI_SUCCESS, SetCRLSerialNumber(CKMInfoTest::crlSerialNum));
247     long serialNum = 0;
248     ASSERT_EQ(PKI_SUCCESS, GetCRLSerialNumber(&serialNum));
249     EXPECT_TRUE(serialNum == CKMInfoTest::crlSerialNum);
250 }
251
252 //SetGetNumberOfRevoked test
253 TEST(CKMInfoTest, SetGetNumberOfRevoked)
254 {
255     const long NUM_OF_REVOKED = 5;
256     ASSERT_EQ(PKI_SUCCESS, SetNumberOfRevoked(NUM_OF_REVOKED));
257     long numRev = 0;
258     ASSERT_EQ(PKI_SUCCESS, GetNumberOfRevoked(&numRev));
259     EXPECT_TRUE(numRev == NUM_OF_REVOKED);
260 }
261
262 //SetCertificateRevocationList test
263 TEST(CKMInfoTest, SetCertificateRevocationList)
264 {
265     ASSERT_EQ(PKI_SUCCESS, SetCertificateRevocationList(&CKMInfoTest::derCode));
266 }
267
268 //CloseCKMInfo test
269 TEST(CKMInfoTest, CloseCKMInfo)
270 {
271     ASSERT_EQ(PKI_SUCCESS, CloseCKMInfo());
272 }