20#include <winpr/stream.h>
21#include <freerdp/channels/scard.h>
22#include <freerdp/utils/smartcard_operations.h>
23#include <freerdp/utils/smartcard_pack.h>
27static wStream* build_locate_cards_by_atr_a(UINT32 cbAtr)
33 Stream_Write_UINT32(s, 0);
34 Stream_Write_UINT32(s, 0);
35 Stream_Write_UINT32(s, 1);
36 Stream_Write_UINT32(s, 0x00020000);
37 Stream_Write_UINT32(s, 0);
38 Stream_Write_UINT32(s, 0);
39 Stream_Write_UINT32(s, 1);
40 Stream_Write_UINT32(s, cbAtr);
45 if (!Stream_SetPosition(s, 0))
53static BOOL run_case(UINT32 cbAtr, BOOL expectAccept)
55 wStream* s = build_locate_cards_by_atr_a(cbAtr);
60 op.ioControlCode = SCARD_IOCTL_LOCATECARDSBYATRA;
61 const LONG status = smartcard_unpack_locate_cards_by_atr_a_call(s, &op.call.locateCardsByATRA);
64 const BOOL accepted = status == SCARD_S_SUCCESS;
65 if (accepted != expectAccept)
67 printf(
"cbAtr=%" PRIu32
": expected %s, unpack returned 0x%08" PRIX32
"\n", cbAtr,
68 expectAccept ?
"accept" :
"reject", (UINT32)status);
69 smartcard_operation_free(&op, FALSE);
73 smartcard_operation_free(&op, FALSE);
79static wStream* build_set_attrib(UINT32 cbAttr, UINT32 cbAttrLen)
81 const UINT32 pad = (4 - (cbAttr % 4)) % 4;
82 wStream* s = Stream_New(
nullptr, 28 + 4 + cbAttr + pad + 12);
86 Stream_Write_UINT32(s, 0);
87 Stream_Write_UINT32(s, 0);
88 Stream_Write_UINT32(s, 8);
89 Stream_Write_UINT32(s, 0x00020000);
90 Stream_Write_UINT32(s, 0);
91 Stream_Write_UINT32(s, cbAttrLen);
92 Stream_Write_UINT32(s, 0x00020004);
93 Stream_Write_UINT32(s, 8);
94 Stream_Write_UINT64(s, 0x123456789abcdef);
95 Stream_Write_UINT32(s, cbAttr);
96 Stream_Zero(s, cbAttr);
100 Stream_SealLength(s);
101 if (!Stream_SetPosition(s, 0))
103 Stream_Free(s, TRUE);
111static BOOL run_set_attrib_case(UINT32 cbAttr, UINT32 cbAttrLen)
113 wStream* s = build_set_attrib(cbAttr, cbAttrLen);
118 const LONG status = smartcard_unpack_set_attrib_call(s, &call);
119 Stream_Free(s, TRUE);
122 if (status != SCARD_S_SUCCESS)
124 printf(
"set_attrib cbAttr=%" PRIu32
": unpack returned 0x%08" PRIX32
"\n", cbAttr,
128 else if (call.cbAttrLen > cbAttr)
130 printf(
"set_attrib cbAttr=%" PRIu32
": cbAttrLen=%" PRIu32
" exceeds payload\n", cbAttr,
139int TestSmartcardPack(
int argc,
char* argv[])
145 if (!run_case(0, TRUE))
147 if (!run_case(36, TRUE))
151 if (!run_case(37, FALSE))
153 if (!run_case(0xFFFFFFFF, FALSE))
157 if (!run_set_attrib_case(1, 0xFFFFFFFF))
159 if (!run_set_attrib_case(5, 0xFFFFFFFF))
162 if (!run_set_attrib_case(4, 0xFFFFFFFF))
164 if (!run_set_attrib_case(8, 2))