forked from yarrick/scsniff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_test.c
169 lines (159 loc) · 4.89 KB
/
data_test.c
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
#include "data.h"
#include <check.h>
#define T0_BYTE(data,byte,exp) ck_assert_uint_eq( \
data_t0_analyze((data),(byte)), (exp))
START_TEST(t0_empty_ack)
{
struct data data;
data_init(&data);
// Get data, 4 bytes
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0xCA, CONTINUE);
T0_BYTE(&data, 0x01, CONTINUE);
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0x04, PACKET_TO_CARD);
// ACK
T0_BYTE(&data, 0xCA, PACKET_FROM_CARD);
// Remaining bytes
T0_BYTE(&data, 0x01, CONTINUE);
T0_BYTE(&data, 0x02, CONTINUE);
T0_BYTE(&data, 0x03, CONTINUE);
T0_BYTE(&data, 0x04, PACKET_FROM_CARD);
// Extra ACKs, no data remaining
T0_BYTE(&data, 0xCA, PACKET_FROM_CARD);
T0_BYTE(&data, 0xCA, PACKET_FROM_CARD);
T0_BYTE(&data, 0xCA, PACKET_FROM_CARD);
// Status bytes
T0_BYTE(&data, 0x90, CONTINUE);
T0_BYTE(&data, 0x00, PACKET_FROM_CARD);
}
END_TEST
START_TEST(t0_null_procedure_byte)
{
struct data data;
data_init(&data);
// Get data, 4 bytes
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0xCA, CONTINUE);
T0_BYTE(&data, 0x01, CONTINUE);
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0x04, PACKET_TO_CARD);
// NULL procedure byte
T0_BYTE(&data, 0x60, PACKET_FROM_CARD);
T0_BYTE(&data, 0x60, PACKET_FROM_CARD);
T0_BYTE(&data, 0x60, PACKET_FROM_CARD);
T0_BYTE(&data, 0x60, PACKET_FROM_CARD);
// ACK
T0_BYTE(&data, 0xCA, PACKET_FROM_CARD);
// Remaining bytes
T0_BYTE(&data, 0x01, CONTINUE);
T0_BYTE(&data, 0x02, CONTINUE);
T0_BYTE(&data, 0x03, CONTINUE);
T0_BYTE(&data, 0x04, PACKET_FROM_CARD);
// NULL procedure byte
T0_BYTE(&data, 0x60, PACKET_FROM_CARD);
T0_BYTE(&data, 0x60, PACKET_FROM_CARD);
// Status bytes
T0_BYTE(&data, 0x90, CONTINUE);
T0_BYTE(&data, 0x00, PACKET_FROM_CARD);
}
END_TEST
START_TEST(t0_single_byte_transfer)
{
struct data data;
data_init(&data);
// Get data, 4 bytes
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0xCA, CONTINUE);
T0_BYTE(&data, 0x01, CONTINUE);
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0x04, PACKET_TO_CARD);
// Single-byte ACK
T0_BYTE(&data, 0xFF ^ 0xCA, PACKET_FROM_CARD);
// One byte
T0_BYTE(&data, 0x01, PACKET_FROM_CARD);
// Repeat
T0_BYTE(&data, 0xFF ^ 0xCA, PACKET_FROM_CARD);
T0_BYTE(&data, 0x02, PACKET_FROM_CARD);
T0_BYTE(&data, 0xFF ^ 0xCA, PACKET_FROM_CARD);
T0_BYTE(&data, 0x03, PACKET_FROM_CARD);
T0_BYTE(&data, 0xFF ^ 0xCA, PACKET_FROM_CARD);
T0_BYTE(&data, 0x04, PACKET_FROM_CARD);
// Extra single-byte acks with no data
T0_BYTE(&data, 0xFF ^ 0xCA, PACKET_FROM_CARD);
T0_BYTE(&data, 0xFF ^ 0xCA, PACKET_FROM_CARD);
// Status bytes
T0_BYTE(&data, 0x90, CONTINUE);
T0_BYTE(&data, 0x00, PACKET_FROM_CARD);
}
END_TEST
START_TEST(t0_get_response)
{
struct data data;
data_init(&data);
// Select DF, 4 bytes
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0xA4, CONTINUE);
T0_BYTE(&data, 0x04, CONTINUE);
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0x04, PACKET_TO_CARD);
// ACK
T0_BYTE(&data, 0xA4, PACKET_FROM_CARD);
// DF name
T0_BYTE(&data, 'T', CONTINUE);
T0_BYTE(&data, 'e', CONTINUE);
T0_BYTE(&data, 's', CONTINUE);
T0_BYTE(&data, 't', PACKET_TO_CARD);
// Status bytes
T0_BYTE(&data, 0x61, CONTINUE);
T0_BYTE(&data, 0x03, PACKET_FROM_CARD);
// Get response
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0xC0, CONTINUE);
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0x03, PACKET_TO_CARD);
// ACK
T0_BYTE(&data, 0xC0, PACKET_FROM_CARD);
// Response bytes
T0_BYTE(&data, 'Y', CONTINUE);
T0_BYTE(&data, 'a', CONTINUE);
T0_BYTE(&data, 'y', PACKET_FROM_CARD);
// Status bytes
T0_BYTE(&data, 0x90, CONTINUE);
T0_BYTE(&data, 0x00, PACKET_FROM_CARD);
}
END_TEST
START_TEST(t0_unknown_direction)
{
struct data data;
data_init(&data);
// Unknown command with 4 bytes payload
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0x23, CONTINUE);
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0x00, CONTINUE);
T0_BYTE(&data, 0x04, PACKET_TO_CARD);
// ACK
T0_BYTE(&data, 0x23, PACKET_FROM_CARD);
// Payload bytes
T0_BYTE(&data, 0x01, CONTINUE);
T0_BYTE(&data, 0x02, CONTINUE);
T0_BYTE(&data, 0x03, CONTINUE);
T0_BYTE(&data, 0x04, PACKET_UNKNOWN);
// Status bytes
T0_BYTE(&data, 0x90, CONTINUE);
T0_BYTE(&data, 0x00, PACKET_FROM_CARD);
}
END_TEST
Suite* data_tests() {
Suite* suite = suite_create("data");
TCase* test = tcase_create("data");
tcase_add_test(test, t0_empty_ack);
tcase_add_test(test, t0_null_procedure_byte);
tcase_add_test(test, t0_single_byte_transfer);
tcase_add_test(test, t0_get_response);
tcase_add_test(test, t0_unknown_direction);
suite_add_tcase(suite, test);
return suite;
}