1 /*
2  * RFC 7519 Json Web Tokens
3  *
4  * Copyright (C) 2018, Linaro, Ltd
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may
8  * 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, WITHOUT
15  * 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 <zephyr/types.h>
21 #include <stdbool.h>
22 #include <zephyr/ztest.h>
23 #include <zephyr/data/json.h>
24 #include <zephyr/data/jwt.h>
25 
26 #include <mbedtls/pk.h>
27 #include <mbedtls/rsa.h>
28 #include <mbedtls/sha256.h>
29 
30 extern unsigned char jwt_test_private_der[];
31 extern unsigned int jwt_test_private_der_len;
32 
ZTEST(jwt_tests,test_jwt)33 ZTEST(jwt_tests, test_jwt)
34 {
35 	/*
36 	 * TODO: This length should be computable, based on the length
37 	 * of the audience string.
38 	 */
39 	char buf[460];
40 	struct jwt_builder build;
41 	int res;
42 
43 	res = jwt_init_builder(&build, buf, sizeof(buf));
44 
45 	zassert_equal(res, 0, "Setting up jwt");
46 
47 	res = jwt_add_payload(&build, 1530312026, 1530308426,
48 			      "iot-work-199419");
49 	zassert_equal(res, 0, "Adding payload");
50 
51 	res = jwt_sign(&build, jwt_test_private_der, jwt_test_private_der_len);
52 	zassert_equal(res, 0, "Signing payload");
53 
54 	zassert_equal(build.overflowed, false, "Not overflow");
55 
56 	printk("JWT:\n%s\n", buf);
57 	printk("len: %zd\n", jwt_payload_len(&build));
58 }
59 
60 ZTEST_SUITE(jwt_tests, NULL, NULL, NULL, NULL, NULL);
61