summaryrefslogtreecommitdiff
path: root/tests/test-helper.cpp
blob: d0ca263346ad5b45cbe40673b32c00fb8986c9a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 *
 *
 * @file        test-helper.cpp
 * @author      Kyungwook Tak (k.tak@samsung.com)
 * @version     1.0
 */
#include "test-helper.h"

#include <cstring>
#include <vector>

#include "web_app_enc.h"
#include "key_handler.h"
#include "crypto_service.h"
#include "types.h"
#include "key_manager.h"

#include "test-common.h"

namespace Wae {
namespace Test {
namespace {

const uid_t UID_OWNER = 5001;

} // namespace anonymous

void add_get_remove_ce(wae_app_type_e app_type)
{
	const char *pkg_id = "TEST_PKG_ID";

	const crypto_element_s *ce = nullptr;
	uid_t uid = app_type == WAE_DOWNLOADED_NORMAL_APP ? UID_OWNER : 0;

	int tmp = create_app_ce(uid, pkg_id, app_type, &ce);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to create_app_ce. ec: " << tmp);

	const crypto_element_s *stored_ce = nullptr;
	tmp = get_app_ce(uid, pkg_id, app_type, true, &stored_ce);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to get_app_ce. ec: " << tmp);

	BOOST_REQUIRE_MESSAGE(ce == stored_ce,
		"ce(" << ce << ") and cached ce(" << stored_ce << ") pointer addr is different!");

	tmp = remove_app_ce(uid, pkg_id, app_type);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to remove_app_ce. ec: " << tmp);

	if (app_type == WAE_DOWNLOADED_GLOBAL_APP) {
		tmp = get_app_ce(uid, pkg_id, app_type, true, &stored_ce);
		BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE && stored_ce->is_migrated_app,
				"when getting app ce which is there isn't, it should be migrated case! "
				"ret("<< tmp << ") and is_migrated_app(" << stored_ce->is_migrated_app << ")");
	} else {
		tmp = get_app_ce(uid, pkg_id, app_type, false, &stored_ce);
		BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NO_KEY,
				"removed app ce is still remaining. ret(" << tmp << ")");
	}
}

void create_app_ce(wae_app_type_e app_type)
{
	uid_t uid = app_type == WAE_DOWNLOADED_NORMAL_APP ? UID_OWNER : 0;
	const char *pkg_id = "TEST_PKG_ID";

	remove_app_ce(uid, pkg_id, app_type);

	const crypto_element_s *ce = nullptr;

	int tmp = create_app_ce(uid, pkg_id, app_type, &ce);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to create_app_ce. ec: " << tmp);

	const crypto_element_s *stored_ce = nullptr;
	tmp = get_app_ce(uid, pkg_id, app_type, false, &stored_ce);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to get_app_ce. ec: " << tmp);

	BOOST_REQUIRE_MESSAGE(ce == stored_ce,
		"ce(" << ce << ") and cached ce(" << stored_ce << ") pointer addr is different!");

	tmp = remove_app_ce(uid, pkg_id, app_type);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to remove_app_ce. ec: " << tmp);
}

void encrypt_decrypt_web_app(wae_app_type_e app_type)
{
	uid_t uid = app_type == WAE_DOWNLOADED_NORMAL_APP ? UID_OWNER : 0;
	const char *pkg_id1 = "testpkg_for_normal";
	const char *pkg_id2 = "testpkg_for_global";
	const char *pkg_id3 = "testpkg_for_preloaded";

	const char *pkg_id = nullptr;
	switch (app_type) {
	case WAE_DOWNLOADED_NORMAL_APP:
		pkg_id = pkg_id1;
		break;

	case WAE_DOWNLOADED_GLOBAL_APP:
		pkg_id = pkg_id2;
		break;

	case WAE_PRELOADED_APP:
	default:
		pkg_id = pkg_id3;
		break;
	}

	// remove old test data
	if (app_type == WAE_DOWNLOADED_NORMAL_APP)
		wae_remove_app_dek(uid, pkg_id);
	else
		wae_remove_global_app_dek(pkg_id, app_type == WAE_PRELOADED_APP);

	if (app_type == WAE_PRELOADED_APP)
		clear_app_deks_loaded_from_key_manager();

	std::vector<unsigned char> plaintext = {
		'a', 'b', 'c', 'a', 'b', 'c', 'x', 'y',
		'o', 'q', '2', 'e', 'v', '0', '1', 'x'
	};

	// test for downloaded web application
	unsigned char *_encrypted = nullptr;
	size_t _enc_len = 0;
	int tmp = 0;
	if (app_type == WAE_DOWNLOADED_NORMAL_APP)
		tmp = wae_encrypt_web_application(uid, pkg_id, plaintext.data(), plaintext.size(),
										  &_encrypted, &_enc_len);
	else
		tmp = wae_encrypt_global_web_application(pkg_id, app_type == WAE_PRELOADED_APP,
												 plaintext.data(), plaintext.size(),
												 &_encrypted, &_enc_len);

	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
			"Failed to wae_encrypt_web_application. ec: " << tmp);
	free(_encrypted);

	// encrypt test twice
	if (app_type == WAE_DOWNLOADED_NORMAL_APP)
		tmp = wae_encrypt_web_application(uid, pkg_id, plaintext.data(),
										  plaintext.size(), &_encrypted, &_enc_len);
	else
		tmp = wae_encrypt_global_web_application(pkg_id, app_type == WAE_PRELOADED_APP,
												 plaintext.data(), plaintext.size(),
												 &_encrypted, &_enc_len);

	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
			"Failed to wae_encrypt_web_application second time. ec: " << tmp);

	auto encrypted = bytearr_to_vec(_encrypted, _enc_len);
	free(_encrypted);

	if (app_type == WAE_DOWNLOADED_NORMAL_APP) {
		char *key_per_user = _create_map_key(uid, pkg_id);
		_remove_app_ce_from_cache(key_per_user);
		free(key_per_user);
	} else {
		_remove_app_ce_from_cache(pkg_id);
	}

	if (app_type == WAE_PRELOADED_APP)
		load_preloaded_app_deks(true);

	unsigned char *_decrypted = nullptr;
	size_t _dec_len = 0;
	if (app_type == WAE_DOWNLOADED_NORMAL_APP)
		tmp = wae_decrypt_web_application(uid, pkg_id, encrypted.data(), encrypted.size(),
										  &_decrypted, &_dec_len);
	else
		tmp = wae_decrypt_global_web_application(pkg_id, app_type == WAE_PRELOADED_APP,
										  		 encrypted.data(), encrypted.size(),
												 &_decrypted, &_dec_len);

	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
			"Failed to wae_decrypt_web_application. ec: " << tmp);

	auto decrypted = bytearr_to_vec(_decrypted, _dec_len);

	BOOST_REQUIRE_MESSAGE(plaintext == decrypted,
		"plaintext and decrypted isn't matched! "
		"plaintext(" << bytes_to_hex(plaintext) << ") "
		"decrypted(" << bytes_to_hex(decrypted) << ")");

	if (app_type == WAE_DOWNLOADED_NORMAL_APP)
		tmp = wae_remove_app_dek(uid, pkg_id);
	else
		tmp = wae_remove_global_app_dek(pkg_id, app_type == WAE_PRELOADED_APP);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
			"Failed to wae_remove_app_dek. ec: " << tmp);
}

} // namespace Test
} // namespace Wae