1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <algorithm>
18 #include <iterator>
19 #include <iostream>
20
21 #include <gtest/gtest.h>
22
23 #include <apdu/apdu.h>
24
25 using android::CommandApdu;
26 using android::ResponseApdu;
27
28 /* CommandApdu */
29
TEST(CommandApduTest,Case1)30 TEST(CommandApduTest, Case1) {
31 const CommandApdu apdu{1, 2, 3, 4};
32 const std::vector<uint8_t> expected{1, 2, 3, 4};
33 ASSERT_EQ(expected.size(), apdu.size());
34 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
35 }
36
TEST(CommandApduTest,Case2s)37 TEST(CommandApduTest, Case2s) {
38 const CommandApdu apdu{4, 3, 2, 1, 0, 3};
39 const std::vector<uint8_t> expected{4, 3, 2, 1, 3};
40 ASSERT_EQ(expected.size(), apdu.size());
41 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
42 }
43
TEST(CommandApduTest,Case2s_maxLe)44 TEST(CommandApduTest, Case2s_maxLe) {
45 const CommandApdu apdu{4, 3, 2, 1, 0, 256};
46 const std::vector<uint8_t> expected{4, 3, 2, 1, 0};
47 ASSERT_EQ(expected.size(), apdu.size());
48 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
49 }
50
TEST(CommandApduTest,Case2e)51 TEST(CommandApduTest, Case2e) {
52 const CommandApdu apdu{5, 6, 7, 8, 0, 258};
53 const std::vector<uint8_t> expected{5, 6, 7, 8, 0, 1, 2};
54 ASSERT_EQ(expected.size(), apdu.size());
55 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
56 }
57
TEST(CommandApduTest,Case2e_maxLe)58 TEST(CommandApduTest, Case2e_maxLe) {
59 const CommandApdu apdu{5, 6, 7, 8, 0, 65536};
60 const std::vector<uint8_t> expected{5, 6, 7, 8, 0, 0, 0};
61 ASSERT_EQ(expected.size(), apdu.size());
62 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
63 }
64
TEST(CommandApduTest,Case3s)65 TEST(CommandApduTest, Case3s) {
66 const CommandApdu apdu{8, 7, 6, 5, 5, 0};
67 const std::vector<uint8_t> expected{8, 7, 6, 5, 5, 0, 0, 0, 0, 0};
68 ASSERT_EQ(expected.size(), apdu.size());
69 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
70 }
71
TEST(CommandApduTest,Case3s_data)72 TEST(CommandApduTest, Case3s_data) {
73 CommandApdu apdu{8, 7, 6, 5, 3, 0};
74 auto it = apdu.dataBegin();
75 *it++ = 10;
76 *it++ = 11;
77 *it++ = 12;
78 ASSERT_EQ(apdu.dataEnd(), it);
79
80 const std::vector<uint8_t> expected{8, 7, 6, 5, 3, 10, 11, 12};
81 ASSERT_EQ(expected.size(), apdu.size());
82 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
83 }
84
TEST(CommandApduTest,Case3e)85 TEST(CommandApduTest, Case3e) {
86 const CommandApdu apdu{8, 7, 6, 5, 256, 0};
87 std::vector<uint8_t> expected{8, 7, 6, 5, 0, 1, 0};
88 expected.resize(expected.size() + 256, 0);
89 ASSERT_EQ(expected.size(), apdu.size());
90 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
91 }
92
TEST(CommandApduTest,Case3e_data)93 TEST(CommandApduTest, Case3e_data) {
94 CommandApdu apdu{8, 7, 6, 5, 65535, 0};
95 ASSERT_EQ(size_t{65535}, apdu.dataSize());
96 std::fill(apdu.dataBegin(), apdu.dataEnd(), 7);
97 std::vector<uint8_t> expected{8, 7, 6, 5, 0, 255, 255};
98 expected.resize(expected.size() + 65535, 7);
99 ASSERT_EQ(expected.size(), apdu.size());
100 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
101 }
102
TEST(CommandApduTest,Case4s)103 TEST(CommandApduTest, Case4s) {
104 const CommandApdu apdu{1, 3, 5, 7, 2, 3};
105 const std::vector<uint8_t> expected{1, 3, 5, 7, 2, 0, 0, 3};
106 ASSERT_EQ(expected.size(), apdu.size());
107 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
108 }
109
TEST(CommandApduTest,Case4s_data)110 TEST(CommandApduTest, Case4s_data) {
111 CommandApdu apdu{1, 3, 5, 7, 1, 90};
112 auto it = apdu.dataBegin();
113 *it++ = 8;
114 ASSERT_EQ(apdu.dataEnd(), it);
115
116 const std::vector<uint8_t> expected{1, 3, 5, 7, 1, 8, 90};
117 ASSERT_EQ(expected.size(), apdu.size());
118 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
119 }
120
TEST(CommandApduTest,Case4s_maxLe)121 TEST(CommandApduTest, Case4s_maxLe) {
122 const CommandApdu apdu{1, 3, 5, 7, 2, 256};
123 const std::vector<uint8_t> expected{1, 3, 5, 7, 2, 0, 0, 0};
124 ASSERT_EQ(expected.size(), apdu.size());
125 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
126 }
127
TEST(CommandApduTest,Case4e)128 TEST(CommandApduTest, Case4e) {
129 const CommandApdu apdu{1, 3, 5, 7, 527, 349};
130 std::vector<uint8_t> expected{1, 3, 5, 7, 0, 2, 15};
131 expected.resize(expected.size() + 527, 0);
132 expected.push_back(1);
133 expected.push_back(93);
134 ASSERT_EQ(expected.size(), apdu.size());
135 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
136 }
137
TEST(CommandApduTest,Case4e_maxLe)138 TEST(CommandApduTest, Case4e_maxLe) {
139 const CommandApdu apdu{1, 3, 5, 7, 20, 65536};
140 std::vector<uint8_t> expected{1, 3, 5, 7, 0, 0, 20};
141 expected.resize(expected.size() + 20, 0);
142 expected.push_back(0);
143 expected.push_back(0);
144 ASSERT_EQ(expected.size(), apdu.size());
145 ASSERT_TRUE(std::equal(apdu.begin(), apdu.end(), expected.begin(), expected.end()));
146 }
147
148 /* ResponseApdu */
149
TEST(ResponseApduTest,bad)150 TEST(ResponseApduTest, bad) {
151 const std::vector<uint8_t> empty{};
152 const ResponseApdu<std::vector<uint8_t>> apdu{empty};
153 ASSERT_FALSE(apdu.ok());
154 }
155
TEST(ResponseApduTest,statusOnly)156 TEST(ResponseApduTest, statusOnly) {
157 const std::vector<uint8_t> statusOnly{0x90, 0x37};
158 const ResponseApdu<std::vector<uint8_t>> apdu{statusOnly};
159 ASSERT_TRUE(apdu.ok());
160 ASSERT_EQ(0x90, apdu.sw1());
161 ASSERT_EQ(0x37, apdu.sw2());
162 ASSERT_EQ(0x9037, apdu.status());
163 ASSERT_EQ(size_t{0}, apdu.dataSize());
164 }
165
TEST(ResponseApduTest,data)166 TEST(ResponseApduTest, data) {
167 const std::vector<uint8_t> data{1, 2, 3, 9, 8, 7, 0x3a, 0xbc};
168 const ResponseApdu<std::vector<uint8_t>> apdu{data};
169 ASSERT_TRUE(apdu.ok());
170 ASSERT_EQ(0x3abc, apdu.status());
171 ASSERT_EQ(size_t{6}, apdu.dataSize());
172
173 const uint8_t expected[] = {1, 2, 3, 9, 8, 7};
174 ASSERT_TRUE(std::equal(apdu.dataBegin(), apdu.dataEnd(),
175 std::begin(expected), std::end(expected)));
176 }
177
TEST(ResponseApduTest,remainingBytes)178 TEST(ResponseApduTest, remainingBytes) {
179 const std::vector<uint8_t> remainingBytes{0x61, 23};
180 const ResponseApdu<std::vector<uint8_t>> apdu{remainingBytes};
181 ASSERT_EQ(23, apdu.remainingBytes());
182 ASSERT_FALSE(apdu.isWarning());
183 ASSERT_FALSE(apdu.isExecutionError());
184 ASSERT_FALSE(apdu.isCheckingError());
185 ASSERT_FALSE(apdu.isError());
186 }
187
TEST(ResponseApduTest,warning)188 TEST(ResponseApduTest, warning) {
189 const std::vector<uint8_t> warning{0x62, 0};
190 const ResponseApdu<std::vector<uint8_t>> apdu{warning};
191 ASSERT_TRUE(apdu.isWarning());
192 ASSERT_FALSE(apdu.isExecutionError());
193 ASSERT_FALSE(apdu.isCheckingError());
194 ASSERT_FALSE(apdu.isError());
195 }
196
TEST(ResponseApduTest,executionError)197 TEST(ResponseApduTest, executionError) {
198 const std::vector<uint8_t> executionError{0x66, 0};
199 const ResponseApdu<std::vector<uint8_t>> apdu{executionError};
200 ASSERT_FALSE(apdu.isWarning());
201 ASSERT_TRUE(apdu.isExecutionError());
202 ASSERT_FALSE(apdu.isCheckingError());
203 ASSERT_TRUE(apdu.isError());
204 }
205
TEST(ResponseApduTest,checkingError)206 TEST(ResponseApduTest, checkingError) {
207 const std::vector<uint8_t> checkingError{0x67, 0};
208 const ResponseApdu<std::vector<uint8_t>> apdu{checkingError};
209 ASSERT_FALSE(apdu.isWarning());
210 ASSERT_FALSE(apdu.isExecutionError());
211 ASSERT_TRUE(apdu.isCheckingError());
212 ASSERT_TRUE(apdu.isError());
213 }
214