summaryrefslogtreecommitdiff
path: root/tests/test-helper.cpp
blob: 144299aa773ca9b22deca4a3d96089afae746ea7 (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
/*
 *  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 "key_handler.h"
#include "crypto_service.h"

#include "test-common.h"

namespace Wae {
namespace Test {

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

	std::vector<unsigned char> dek(32, 0);

	BOOST_REQUIRE(_get_random(dek.size(), dek.data()) == WAE_ERROR_NONE);

	remove_app_dek(pkg_id, app_type);

	int tmp = _add_dek_to_key_manager(pkg_id, app_type, dek.data(), dek.size());
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
			"Failed to _add_dek_to_key_manager. ec: " << tmp);

	unsigned char *_stored_dek = nullptr;
	size_t _stored_dek_len = 0;
	tmp = get_app_dek(pkg_id, app_type, &_stored_dek, &_stored_dek_len);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
			"Failed to get_app_dek. ec: " << tmp);

	auto stored_dek = Wae::Test::bytearr_to_vec(_stored_dek, _stored_dek_len);
	free(_stored_dek);

	BOOST_REQUIRE_MESSAGE(stored_dek == dek, "stored dek and dek isn't matched!");

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

	_stored_dek = nullptr;
	tmp = get_app_dek(pkg_id, app_type, &_stored_dek, &_stored_dek_len);
	if (_stored_dek)
		free(_stored_dek);

	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NO_KEY,
			"dek removed but it's remained still. ec: " << tmp);
}

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

	remove_app_dek(pkg_id, app_type);

	unsigned char *_dek = nullptr;
	size_t _dek_len = 0;

	int tmp = create_app_dek(pkg_id, app_type, &_dek, &_dek_len);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
			"Failed to create_app_dek. ec: " << tmp);

	auto dek = Wae::Test::bytearr_to_vec(_dek, _dek_len);
	free(_dek);

	unsigned char *_stored_dek = nullptr;
	size_t _stored_dek_len = 0;
	tmp = get_app_dek(pkg_id, app_type, &_stored_dek, &_stored_dek_len);
	BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to get_app_dek. ec: " << tmp);
	auto stored_dek = bytearr_to_vec(_stored_dek, _stored_dek_len);
	free(_stored_dek);

	BOOST_REQUIRE_MESSAGE(stored_dek == dek,
		"stored dek and dek isn't matched! "
		"stored_dek(" << Wae::Test::bytes_to_hex(stored_dek) << ") "
		"dek(" << Wae::Test::bytes_to_hex(dek) << ")");

	remove_app_dek(pkg_id, app_type);
}

void encrypt_decrypt_web_app(wae_app_type_e app_type)
{
	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
	wae_remove_app_dek(pkg_id, app_type);

	if (app_type == WAE_PRELOADED_APP)
		_clear_app_deks_loaded();

	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 = wae_encrypt_web_application(pkg_id, app_type, 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
	tmp = wae_encrypt_web_application(pkg_id, app_type, 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);

	_remove_app_dek_from_cache(pkg_id);

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

	unsigned char *_decrypted = nullptr;
	size_t _dec_len = 0;
	tmp = wae_decrypt_web_application(pkg_id, app_type, 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(" << Wae::Test::bytes_to_hex(plaintext) << ") "
		"decrypted(" << Wae::Test::bytes_to_hex(decrypted) << ")");

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

} // namespace Test
} // namespace Wae