replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / unittest / psinterface.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  *     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 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23
24 #include "gtest/gtest.h"
25
26 #define STR(...) STR_(__VA_ARGS__)
27 #define STR_(...) # __VA_ARGS__
28
29 #define DNEF "does_not_exist.dat"
30 #define DEFAULT_FILE "oic_svr_db_prov.dat"
31
32 extern "C" {
33 #include "../src/psinterface.c"
34
35 #include "ocstack.h"
36 #include "cainterface.h"
37 #include "secureresourcemanager.h"
38 }
39
40 static OCPersistentStorage _ps;
41
42 char localPath[PATH_MAX] = {0};
43
44 FILE *fopenCustom(const char *path, const char *mode)
45 {
46     (void)path;
47     FILE *fp = fopen(localPath, mode);
48     return fp;
49 }
50
51 TEST(init,db)
52 {
53     _ps.open = fopenCustom;
54     _ps.read = fread;
55     _ps.write = fwrite;
56     _ps.close = fclose;
57     _ps.unlink = unlink;
58
59     int len = strlen(STR(SECURITY_BUILD_UNITTEST_DIR)) + strlen(DEFAULT_FILE) + 1;
60     snprintf(localPath, len, "%s%s", STR(SECURITY_BUILD_UNITTEST_DIR), DEFAULT_FILE);
61
62     InitPersistentStorageInterface();
63
64     EXPECT_EQ(OC_STACK_OK, SRMRegisterPersistentStorageHandler(&_ps));
65 }
66
67 TEST(PersistentStorageInterfaceTest, Null)
68 {
69     EXPECT_NE(OC_STACK_OK,GetSecureVirtualDatabaseFromPS(OIC_JSON_CRED_NAME, NULL, NULL));
70 }
71
72 TEST(PersistentStorageInterfaceTest, NullPayload)
73 {
74
75     size_t payloadSize = 0;
76
77     EXPECT_NE(OC_STACK_OK,GetSecureVirtualDatabaseFromPS(OIC_JSON_CRED_NAME, NULL, &payloadSize));
78 }
79
80 TEST(PersistentStorageInterfaceTest, NullPayloadSize)
81 {
82
83     uint8_t* payload = NULL;
84
85     EXPECT_NE(OC_STACK_OK,GetSecureVirtualDatabaseFromPS(OIC_JSON_CRED_NAME, &payload, NULL));
86 }
87
88 TEST(PersistentStorageInterfaceTest, DoesNotExistsDbFile)
89 {
90     uint8_t* payload = NULL;
91     size_t payloadSize = 0;
92
93     int len = strlen(STR(SECURITY_BUILD_UNITTEST_DIR)) + strlen(DEFAULT_FILE) + 1;
94     snprintf(localPath, len, "%s%s", STR(SECURITY_BUILD_UNITTEST_DIR), DNEF);
95     unlink(localPath);
96
97     InitPersistentStorageInterface();
98
99     EXPECT_NE(OC_STACK_OK,GetSecureVirtualDatabaseFromPS(OIC_JSON_CRED_NAME, &payload, &payloadSize));
100 }
101
102 TEST(PersistentStorageInterfaceTest, CredRead)
103 {
104
105     uint8_t* payload = NULL;
106     size_t payloadSize = 0;
107
108     int len = strlen(STR(SECURITY_BUILD_UNITTEST_DIR)) + strlen(DEFAULT_FILE) + 1;
109     snprintf(localPath, len, "%s%s", STR(SECURITY_BUILD_UNITTEST_DIR), DEFAULT_FILE);
110     InitPersistentStorageInterface();
111
112     EXPECT_EQ(OC_STACK_OK,GetSecureVirtualDatabaseFromPS(OIC_JSON_CRED_NAME, &payload, &payloadSize));
113 }
114
115 TEST(PersistentStorageInterfaceTest, FileSizeCashe)
116 {
117
118     uint8_t* payload = NULL;
119     size_t payloadSize = 0;
120
121     InitPersistentStorageInterface();
122
123     EXPECT_EQ(OC_STACK_OK,GetSecureVirtualDatabaseFromPS(OIC_JSON_CRED_NAME, &payload, &payloadSize));
124 }