1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cstdio>
17 #include <fcntl.h>
18 #include <unistd.h>
19
20 #include <gtest/gtest.h>
21
22 #include "securec.h"
23
24 #include "app_rsa.h"
25 #include "app_sha256.h"
26 #include "hota_updater.h"
27 #include "hota_verify.h"
28
29 using namespace std;
30 using namespace testing::ext;
31
32 static const int READ_BUF_LEN = 256;
33 static char g_readBuf[READ_BUF_LEN];
34 static const int SIGN_RSA2048_LEN = 256;
35 static const int SIGN_RSA3072_LEN = 384;
36
37 static const int OTA_PKG_SIZE = 16 * 58 + 11;
38 static const char *PASS_THROUGH_PATH = "/sdcard/update/ota_pkg_pass_through.bin";
39 static const int INFO_COMP_DATA_LEN = 267;
40 static const int WRONG_PKG_TYPE = 128;
41
42 /* this pkg is packed by same pubkey */
43 static char g_otaPkg[OTA_PKG_SIZE] = {
44 0x11, 0x00, 0x88, 0x00, 0x0b, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x68, 0x69, 0x73, 0x69,
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x70, 0x65, 0x6e,
49 0x48, 0x61, 0x72, 0x6d, 0x6f, 0x6e, 0x79, 0x20, 0x39, 0x39, 0x2e, 0x39, 0x39, 0x2e, 0x39, 0x39,
50 0x2e, 0x39, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x00,
53 0x32, 0x30, 0x32, 0x31, 0x2e, 0x30, 0x37, 0x2e, 0x30, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x31, 0x30, 0x3a, 0x33, 0x36, 0x3a, 0x33, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x05, 0x00, 0x47, 0x00, 0x6f, 0x74, 0x61, 0x5f, 0x74, 0x61, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x05, 0x00, 0x00, 0x31, 0x2e, 0x30, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3, 0x22, 0xf9, 0x3f, 0x69,
58 0x5d, 0x8f, 0xc6, 0x30, 0xa7, 0xb1, 0x62, 0xf1, 0x41, 0xd2, 0xf4, 0xef, 0x2a, 0x3a, 0xc8, 0xc3,
59 0x50, 0x99, 0x56, 0xe8, 0x57, 0x7a, 0xcc, 0x48, 0xa5, 0xac, 0x69, 0x75, 0x70, 0x64, 0x61, 0x74,
60 0x65, 0x2f, 0x69, 0x6e, 0x66, 0x6f, 0x2e, 0x62, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0xce, 0x42, 0x75, 0xef,
77 0x2e, 0xcf, 0x9c, 0xc7, 0xee, 0x90, 0xe9, 0x57, 0xb3, 0x63, 0x99, 0xd6, 0x32, 0xa4, 0x03, 0x54,
78 0xe4, 0x38, 0xe4, 0xf2, 0x41, 0x28, 0x3d, 0x40, 0xa5, 0x83, 0xb8, 0xbd, 0xaf, 0x7b, 0xa9, 0xdd,
79 0x7f, 0xa9, 0xb3, 0x2e, 0x78, 0x66, 0x85, 0xe6, 0x9d, 0xda, 0xb2, 0x1d, 0x1b, 0xaa, 0xd4, 0xa6,
80 0x07, 0xb8, 0xb4, 0xe3, 0xff, 0x36, 0x3a, 0x02, 0x80, 0xc6, 0xae, 0x56, 0x22, 0x40, 0x24, 0xf1,
81 0x22, 0x7a, 0x04, 0xf8, 0x2a, 0x3a, 0x88, 0xe6, 0x7e, 0xde, 0x46, 0x8e, 0x5e, 0x14, 0x9c, 0xce,
82 0xa6, 0x92, 0x64, 0x65, 0x70, 0x9e, 0x2f, 0x70, 0x39, 0x46, 0xa8, 0x1e, 0x85, 0x55, 0x5a, 0xea,
83 0x81, 0xc1, 0x22, 0x5d, 0xe7, 0xda, 0xbf, 0x28, 0x1e, 0x78, 0xa4, 0x8d, 0x66, 0xa3, 0x12, 0x9a,
84 0xb8, 0x0b, 0xb9, 0x4e, 0xfc, 0xa4, 0x66, 0x28, 0xf3, 0x3a, 0xd9, 0x72, 0x94, 0x8f, 0x04, 0x9a,
85 0x58, 0xf6, 0xcb, 0xf4, 0xee, 0x55, 0xf0, 0x39, 0x98, 0x8e, 0x08, 0x91, 0x82, 0x9c, 0x63, 0xed,
86 0x4c, 0xb2, 0xa1, 0xb2, 0xd2, 0x3f, 0xc7, 0x68, 0xcf, 0x3c, 0x75, 0x63, 0xb3, 0x9b, 0x9e, 0x99,
87 0x71, 0xcc, 0x47, 0x5a, 0x2c, 0xb8, 0xe4, 0x77, 0x17, 0x10, 0x66, 0x1e, 0x48, 0x17, 0xc5, 0x24,
88 0x09, 0x1a, 0x8f, 0xbd, 0x77, 0x2f, 0x4a, 0xba, 0x7a, 0x4e, 0x1c, 0x68, 0xb6, 0xdc, 0x76, 0x68,
89 0x73, 0x38, 0xfe, 0x8e, 0xf8, 0xfe, 0x67, 0xe7, 0x3d, 0xe4, 0x29, 0x9a, 0x91, 0xc3, 0x3a, 0x73,
90 0x35, 0x5c, 0x2f, 0xb9, 0x3d, 0x5e, 0x03, 0x44, 0x95, 0x3f, 0x7d, 0x3e, 0x9f, 0x29, 0x85, 0xc6,
91 0x8c, 0x4a, 0x25, 0x5d, 0xc6, 0xc4, 0xb7, 0x08, 0xdb, 0x87, 0x22, 0x61, 0x30, 0x2d, 0xba, 0xe5,
92 0xc1, 0x2f, 0x62, 0xc3, 0x7c, 0x70, 0xab, 0x41, 0xc9, 0x93, 0xee, 0x6e, 0xcc, 0x74, 0x85, 0x37,
93 0x61, 0xdc, 0x36, 0x98, 0x0e, 0xad, 0x67, 0x89, 0xdc, 0x94, 0xcb, 0xd1, 0xab, 0xc1, 0x71, 0x1c,
94 0x76, 0x18, 0x20, 0x73, 0xe8, 0x61, 0x55, 0xcb, 0x6a, 0xae, 0x2c, 0x2d, 0x10, 0x0c, 0x40, 0xf0,
95 0x17, 0xd8, 0xd1, 0x8d, 0x2e, 0x9b, 0xef, 0xca, 0x97, 0xc7, 0x21, 0x7f, 0x58, 0x5a, 0xdc, 0x6c,
96 0x1d, 0x9b, 0x57, 0x6a, 0x48, 0xd2, 0xc0, 0x6a, 0xa8, 0x4d, 0x95, 0x5b, 0x2e, 0x11, 0x6d, 0x7a,
97 0x30, 0x96, 0xce, 0x7e, 0x33, 0x55, 0x4f, 0x8e, 0x01, 0x67, 0xb6, 0x8f, 0x1c, 0x44, 0x46, 0x26,
98 0xa6, 0xab, 0x57, 0x6f, 0xe6, 0x22, 0x2e, 0xe0, 0xac, 0x3d, 0x78, 0x5c, 0x6a, 0xf2, 0xe2, 0xc9,
99 0xc6, 0x89, 0xcb, 0x0d, 0xc0, 0x93, 0x5c, 0xc1, 0x5b, 0x1e, 0x75, 0x64, 0xcf, 0xbd, 0x35, 0x26,
100 0x90, 0x98, 0xd5, 0x61, 0x47, 0xef, 0x18, 0x10, 0x4f, 0x33, 0x75, 0x70, 0x61, 0x63, 0x6b, 0x61,
101 0x67, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x3a, 0x6f, 0x74, 0x61, 0x41, 0x31, 0x53, 0x32, 0x44,
102 0x33, 0x46, 0x34, 0x47, 0x35, 0x48, 0x36, 0x4a, 0x37, 0x4b, 0x38
103 };
104
105 /* this pkg is packed by different pubkey */
106 static char g_otaPkgWrongKey[OTA_PKG_SIZE] = {
107 0x11, 0x00, 0x88, 0x00, 0x0b, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x68, 0x69, 0x73, 0x69,
108 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
110 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x70, 0x65, 0x6e,
112 0x48, 0x61, 0x72, 0x6d, 0x6f, 0x6e, 0x79, 0x20, 0x39, 0x39, 0x2e, 0x39, 0x39, 0x2e, 0x39, 0x39,
113 0x2e, 0x39, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x00,
116 0x32, 0x30, 0x32, 0x31, 0x2e, 0x30, 0x37, 0x2e, 0x30, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
117 0x31, 0x30, 0x3a, 0x33, 0x36, 0x3a, 0x33, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118 0x05, 0x00, 0x47, 0x00, 0x6f, 0x74, 0x61, 0x5f, 0x74, 0x61, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00,
119 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x05, 0x00, 0x00, 0x31, 0x2e, 0x30, 0x00, 0x00, 0x00, 0x00,
120 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3, 0x22, 0xf9, 0x3f, 0x69,
121 0x5d, 0x8f, 0xc6, 0x30, 0xa7, 0xb1, 0x62, 0xf1, 0x41, 0xd2, 0xf4, 0xef, 0x2a, 0x3a, 0xc8, 0xc3,
122 0x50, 0x99, 0x56, 0xe8, 0x57, 0x7a, 0xcc, 0x48, 0xa5, 0xac, 0x69, 0x75, 0x70, 0x64, 0x61, 0x74,
123 0x65, 0x2f, 0x69, 0x6e, 0x66, 0x6f, 0x2e, 0x62, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
133 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
137 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
138 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
139 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0xce, 0x42, 0x75, 0xef,
140 0x2e, 0xcf, 0x9c, 0xc7, 0xee, 0x90, 0xe9, 0x57, 0xb3, 0x63, 0x99, 0xd6, 0x32, 0xa4, 0x03, 0x54,
141 0xe4, 0x38, 0xe4, 0xf2, 0x41, 0x28, 0x3d, 0x40, 0xa5, 0x83, 0xb8, 0xbd, 0xaf, 0x7b, 0xa9, 0xdd,
142 0x7f, 0xa9, 0xb3, 0x2e, 0x78, 0x66, 0x85, 0xe6, 0x9d, 0xda, 0xb2, 0x1d, 0x1b, 0xaa, 0xd4, 0xa6,
143 0x07, 0xb8, 0xb4, 0xe3, 0xff, 0x36, 0x3a, 0x02, 0x80, 0xc6, 0xae, 0x56, 0x22, 0x40, 0x24, 0xf1,
144 0x22, 0x7a, 0x04, 0xf8, 0x2a, 0x3a, 0x88, 0xe6, 0x7e, 0xde, 0x46, 0x8e, 0x5e, 0x14, 0x9c, 0xce,
145 0xa6, 0x92, 0x64, 0x65, 0x70, 0x9e, 0x2f, 0x70, 0x39, 0x46, 0xa8, 0x1e, 0x85, 0x55, 0x5a, 0xea,
146 0x81, 0xc1, 0x22, 0x5d, 0xe7, 0xda, 0xbf, 0x28, 0x1e, 0x78, 0xa4, 0x8d, 0x66, 0xa3, 0x12, 0x9b,
147 0xb8, 0x0b, 0xb9, 0x4e, 0xfc, 0xa4, 0x66, 0x28, 0xf3, 0x3a, 0xd9, 0x72, 0x94, 0x8f, 0x04, 0x9b,
148 0x58, 0xf6, 0xcb, 0xf4, 0xee, 0x55, 0xf0, 0x39, 0x98, 0x8e, 0x08, 0x91, 0x82, 0x9c, 0x63, 0xee,
149 0x4c, 0xb2, 0xa1, 0xb2, 0xd2, 0x3f, 0xc7, 0x68, 0xcf, 0x3c, 0x75, 0x63, 0xb3, 0x9b, 0x9e, 0x9a,
150 0x71, 0xcc, 0x47, 0x5a, 0x2c, 0xb8, 0xe4, 0x77, 0x17, 0x10, 0x66, 0x1e, 0x48, 0x17, 0xc5, 0x25,
151 0x09, 0x1a, 0x8f, 0xbd, 0x77, 0x2f, 0x4a, 0xba, 0x7a, 0x4e, 0x1c, 0x68, 0xb6, 0xdc, 0x76, 0x69,
152 0x73, 0x38, 0xfe, 0x8e, 0xf8, 0xfe, 0x67, 0xe7, 0x3d, 0xe4, 0x29, 0x9a, 0x91, 0xc3, 0x3a, 0x74,
153 0x35, 0x5c, 0x2f, 0xb9, 0x3d, 0x5e, 0x03, 0x44, 0x95, 0x3f, 0x7d, 0x3e, 0x9f, 0x29, 0x85, 0xc7,
154 0x8c, 0x4a, 0x25, 0x5d, 0xc6, 0xc4, 0xb7, 0x08, 0xdb, 0x87, 0x22, 0x61, 0x30, 0x2d, 0xba, 0xe6,
155 0xc1, 0x2f, 0x62, 0xc3, 0x7c, 0x70, 0xab, 0x41, 0xc9, 0x93, 0xee, 0x6e, 0xcc, 0x74, 0x85, 0x38,
156 0x61, 0xdc, 0x36, 0x98, 0x0e, 0xad, 0x67, 0x89, 0xdc, 0x94, 0xcb, 0xd1, 0xab, 0xc1, 0x71, 0x1d,
157 0x76, 0x18, 0x20, 0x73, 0xe8, 0x61, 0x55, 0xcb, 0x6a, 0xae, 0x2c, 0x2d, 0x10, 0x0c, 0x40, 0xf1,
158 0x17, 0xd8, 0xd1, 0x8d, 0x2e, 0x9b, 0xef, 0xca, 0x97, 0xc7, 0x21, 0x7f, 0x58, 0x5a, 0xdc, 0x6d,
159 0x1d, 0x9b, 0x57, 0x6a, 0x48, 0xd2, 0xc0, 0x6a, 0xa8, 0x4d, 0x95, 0x5b, 0x2e, 0x11, 0x6d, 0x7b,
160 0x30, 0x96, 0xce, 0x7e, 0x33, 0x55, 0x4f, 0x8e, 0x01, 0x67, 0xb6, 0x8f, 0x1c, 0x44, 0x46, 0x27,
161 0xa6, 0xab, 0x57, 0x6f, 0xe6, 0x22, 0x2e, 0xe0, 0xac, 0x3d, 0x78, 0x5c, 0x6a, 0xf2, 0xe2, 0xca,
162 0xc6, 0x89, 0xcb, 0x0d, 0xc0, 0x93, 0x5c, 0xc1, 0x5b, 0x1e, 0x75, 0x64, 0xcf, 0xbd, 0x35, 0x26,
163 0x90, 0x98, 0xd5, 0x61, 0x47, 0xef, 0x18, 0x10, 0x4f, 0x33, 0x75, 0x70, 0x61, 0x63, 0x6b, 0x61,
164 0x67, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x3a, 0x6f, 0x74, 0x61, 0x41, 0x31, 0x53, 0x32, 0x44,
165 0x33, 0x46, 0x34, 0x47, 0x35, 0x48, 0x36, 0x4a, 0x37, 0x4b, 0x38
166 };
167
168
HotaGetOtaPkg(bool isValid)169 static const char *HotaGetOtaPkg(bool isValid)
170 {
171 return isValid ? g_otaPkg : g_otaPkgWrongKey;
172 }
173
HotaInterfaceTest()174 static int HotaInterfaceTest()
175 {
176 (void)HotaSetPackageType(USE_DEFAULT_PKG);
177 (void)HotaGetUpdateIndex(NULL);
178 (void)HotaInit(NULL, NULL);
179 (void)HotaWrite(NULL, 0, 0);
180 (void)HotaRead(0, 0, NULL);
181 (void)HotaCancel();
182 (void)HotaRebootAndCleanUserData();
183 (void)HotaRebootAndCleanCache();
184 return 0;
185 }
186
HotaWriteTest(const char * otaPkg,int len,int pkgOffset)187 static int HotaWriteTest(const char *otaPkg, int len, int pkgOffset)
188 {
189 int leftLen = len;
190 int offset = pkgOffset;
191 while (leftLen > 0) {
192 int tmpLen = ((leftLen >= READ_BUF_LEN) ? READ_BUF_LEN : leftLen);
193 (void)memset_s(g_readBuf, READ_BUF_LEN, 0, READ_BUF_LEN);
194 if (memcpy_s(g_readBuf, READ_BUF_LEN, otaPkg + offset, tmpLen) != EOK) {
195 printf("memcpy fail!\r\n");
196 (void)HotaCancel();
197 return -1;
198 }
199 if (HotaWrite(reinterpret_cast<unsigned char *>(g_readBuf), offset, tmpLen) != 0) {
200 printf("ota write fail!\r\n");
201 (void)HotaCancel();
202 return -1;
203 }
204 offset += READ_BUF_LEN;
205 leftLen -= tmpLen;
206 }
207 return 0;
208 }
209
210 /* isValid: true: use valid pkg, false: use invalid pkg */
HotaByDefaultPkg(bool isValid)211 static int HotaByDefaultPkg(bool isValid)
212 {
213 if (HotaInit(NULL, NULL) < 0) {
214 printf("ota update init fail!\r\n");
215 return -1;
216 }
217 HotaSetPackageType(USE_DEFAULT_PKG);
218 int offset = 0;
219 int pkgLen = OTA_PKG_SIZE;
220 int leftLen = pkgLen;
221 const char *otaPkg = HotaGetOtaPkg(isValid);
222 if (HotaWriteTest(otaPkg, leftLen, offset) == -1) {
223 return -1;
224 }
225 return 0;
226 }
227
HotaByDefaultPkgWithWrongHash()228 static int HotaByDefaultPkgWithWrongHash()
229 {
230 g_otaPkg[INFO_COMP_DATA_LEN - 1] += 1;
231 int ret = HotaByDefaultPkg(true);
232 g_otaPkg[INFO_COMP_DATA_LEN - 1] -= 1;
233 return ret;
234 }
235
HotaByDefaultPkgWithWrongKey()236 static int HotaByDefaultPkgWithWrongKey()
237 {
238 return HotaByDefaultPkg(false);
239 }
240
HotaReadTest(const char * otaPkg,int len,int pkgOffset)241 static int HotaReadTest(const char *otaPkg, int len, int pkgOffset)
242 {
243 int leftLen = len;
244 int offset = pkgOffset;
245 while (leftLen > 0) {
246 int tmpLen = ((leftLen >= READ_BUF_LEN) ? READ_BUF_LEN : leftLen);
247 (void)memset_s(g_readBuf, READ_BUF_LEN, 0, READ_BUF_LEN);
248 if (HotaRead(offset, READ_BUF_LEN, reinterpret_cast<unsigned char *>(g_readBuf)) != 0) {
249 printf("ota write fail!\r\n");
250 (void)HotaCancel();
251 return -1;
252 }
253 if (memcmp(g_readBuf, otaPkg + offset, tmpLen) != 0) {
254 printf("file content cmp fail!\r\n");
255 (void)HotaCancel();
256 return -1;
257 }
258 offset += READ_BUF_LEN;
259 leftLen -= tmpLen;
260 }
261
262 return 0;
263 }
264
HotaByNotDefaultPkg()265 static int HotaByNotDefaultPkg()
266 {
267 if (HotaInit(NULL, NULL) < 0) {
268 printf("ota update init fail!\r\n");
269 return -1;
270 }
271 HotaSetPackageType(NOT_USE_DEFAULT_PKG);
272 int offset = 0;
273 int pkgLen = OTA_PKG_SIZE;
274 int leftLen = pkgLen;
275 const char *otaPkg = HotaGetOtaPkg(true);
276 if (HotaWriteTest(otaPkg, leftLen, offset) == -1) {
277 return -1;
278 }
279
280 int fd = open(PASS_THROUGH_PATH, O_RDWR, S_IRUSR | S_IWUSR);
281 if (fd < 0) {
282 printf("file open failed, fd = %d\r\n", fd);
283 (void)HotaCancel();
284 return -1;
285 }
286 int newLen = lseek(fd, 0, SEEK_END);
287 if (pkgLen != newLen) {
288 printf("pkg len wrong! old = %d, new = %d\r\n", pkgLen, newLen);
289 (void)HotaCancel();
290 close(fd);
291 return -1;
292 }
293 close(fd);
294 leftLen = pkgLen;
295 offset = 0;
296 if (HotaReadTest(otaPkg, leftLen, offset) == -1) {
297 return -1;
298 }
299 return 0;
300 }
301
HotaCalcImageHash(uint8 * dataAddr,uint32 dataLen,uint8 * hash,uint32 hashLen)302 static int HotaCalcImageHash(uint8 *dataAddr, uint32 dataLen, uint8 *hash, uint32 hashLen)
303 {
304 AppSha256Context sha256;
305 uint32 count;
306
307 if ((dataAddr == nullptr) || (hash == nullptr) || (dataLen == 0) || (hashLen < HASH_LENGTH)) {
308 return -1;
309 }
310
311 uint8 *dataBuff = dataAddr;
312 if (memset_s(hash, hashLen, 0, hashLen) != EOK) {
313 return -1;
314 }
315 AppSha256Init(&sha256);
316
317 while (dataLen > 0) {
318 count = (dataLen > BUFFR_LENGTH) ? BUFFR_LENGTH : dataLen;
319 AppSha256Update(&sha256, dataBuff, count);
320 dataLen -= count;
321 dataBuff += count;
322 }
323
324 AppSha256Finish(&sha256, hash);
325 return 0;
326 }
327
328 class OtaTest : public testing::Test {
329 public:
330 static void SetUpTestCase(void);
331 static void TearDownTestCase(void);
332 void SetUp();
333 void TearDown();
334 };
335
SetUpTestCase(void)336 void OtaTest::SetUpTestCase(void)
337 {
338 }
339
TearDownTestCase(void)340 void OtaTest::TearDownTestCase(void)
341 {
342 }
343
SetUp(void)344 void OtaTest::SetUp(void)
345 {
346 }
347
TearDown(void)348 void OtaTest::TearDown(void)
349 {
350 (void)HotaCancel();
351 }
352
353
354 HWTEST_F(OtaTest, interface_call_001, TestSize.Level4)
355 {
356 EXPECT_EQ(0, HotaInterfaceTest());
357 }
358
359
360 HWTEST_F(OtaTest, default_pkg_001, TestSize.Level0)
361 {
362 EXPECT_EQ(0, HotaByDefaultPkg(true));
363 }
364
365
366 HWTEST_F(OtaTest, not_default_pkg_001, TestSize.Level2)
367 {
368 EXPECT_EQ(0, HotaByNotDefaultPkg());
369 }
370
371
372 HWTEST_F(OtaTest, default_pkg_wrong_hash_001, TestSize.Level3)
373 {
374 EXPECT_EQ(-1, HotaByDefaultPkgWithWrongHash());
375 }
376
377
378 HWTEST_F(OtaTest, default_pkg_wrong_key_001, TestSize.Level3)
379 {
380 EXPECT_EQ(-1, HotaByDefaultPkgWithWrongKey());
381 }
382
383
384 HWTEST_F(OtaTest, set_wrong_type_001, TestSize.Level3)
385 {
386 EXPECT_EQ(-1, HotaSetPackageType(WRONG_PKG_TYPE));
387 }
388
389
390 HWTEST_F(OtaTest, hota_sign_verify_001, TestSize.Level3)
391 {
392 EXPECT_EQ(0, HotaSignVerify(reinterpret_cast<uint8 *>(g_otaPkg), INFO_COMP_DATA_LEN,
393 reinterpret_cast<uint8 *>(g_otaPkg + INFO_COMP_DATA_LEN + SIGN_RSA2048_LEN), SIGN_RSA3072_LEN));
394 }
395
396
397 HWTEST_F(OtaTest, hota_get_hash_001, TestSize.Level3)
398 {
399 unsigned char hashOut[HASH_LENGTH] = {0};
400 EXPECT_EQ(0, HotaGetHash(hashOut, HASH_LENGTH));
401 }
402
403
404 HWTEST_F(OtaTest, hota_get_key_001, TestSize.Level3)
405 {
406 uint32 length = 0;
407 uint8 *keyBuf = HotaGetPubKey(&length);
408 EXPECT_NE(0, length);
409 EXPECT_NE(true, keyBuf == nullptr);
410 }
411
412
413 HWTEST_F(OtaTest, hota_decode_key_001, TestSize.Level3)
414 {
415 AppRsaContext rsa;
416 uint32 length = 0;
417
418 AppRsaInit(&rsa);
419 uint8 *keyBuf = HotaGetPubKey(&length);
420 EXPECT_NE(0, length);
421 EXPECT_NE(true, keyBuf == nullptr);
422
423 int ret = AppRsaDecodePublicKey(&rsa, keyBuf, length);
424 AppRsaFree(&rsa);
425 EXPECT_EQ(0, ret);
426 }
427
428
429 HWTEST_F(OtaTest, hota_verify_data_001, TestSize.Level3)
430 {
431 uint32 length = 0;
432 AppRsaContext rsa;
433
434 AppRsaInit(&rsa);
435 uint8 *keyBuf = HotaGetPubKey(&length);
436 EXPECT_NE(true, keyBuf == nullptr);
437 EXPECT_NE(0, length);
438
439 int ret = AppRsaDecodePublicKey(&rsa, keyBuf, length);
440 EXPECT_EQ(0, ret);
441
442 uint8 imageHashLocal[HASH_LENGTH] = {0};
443 ret = HotaCalcImageHash(reinterpret_cast<uint8 *>(g_otaPkg), INFO_COMP_DATA_LEN, imageHashLocal, HASH_LENGTH);
444 EXPECT_EQ(0, ret);
445
446 ret = AppVerifyData(&rsa, imageHashLocal, HASH_LENGTH,
447 reinterpret_cast<uint8 *>(g_otaPkg + INFO_COMP_DATA_LEN + SIGN_RSA2048_LEN), SIGN_RSA3072_LEN);
448 AppRsaFree(&rsa);
449 EXPECT_EQ(0, ret);
450 }
451