apdu_test.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. *******************************************************************************
  3. \file apdu_test.c
  4. \brief Tests for APDU formats
  5. \project bee2/test
  6. \created 2022.10.31
  7. \version 2023.03.30
  8. \copyright The Bee2 authors
  9. \license Licensed under the Apache License, Version 2.0 (see LICENSE.txt).
  10. *******************************************************************************
  11. */
  12. #include <bee2/core/apdu.h>
  13. #include <bee2/core/hex.h>
  14. #include <bee2/core/mem.h>
  15. #include <bee2/core/str.h>
  16. #include <bee2/core/util.h>
  17. /*
  18. *******************************************************************************
  19. Тестирование
  20. - used https://habr.com/ru/post/439574/
  21. *******************************************************************************
  22. */
  23. bool_t apduTest()
  24. {
  25. octet stack[2048];
  26. apdu_cmd_t* cmd = (apdu_cmd_t*)stack;
  27. apdu_cmd_t* cmd1 = (apdu_cmd_t*)(stack + 1024);
  28. apdu_resp_t* resp = (apdu_resp_t*)stack;
  29. apdu_resp_t* resp1 = (apdu_resp_t*)(stack + 1024);
  30. octet apdu[1024];
  31. size_t count;
  32. size_t count1;
  33. // cmd: точечный тест
  34. memSetZero(cmd, sizeof(apdu_cmd_t));
  35. cmd->cla = 0x00, cmd->ins = 0xA4, cmd->p1 = 0x04, cmd->p2 = 0x04;
  36. cmd->cdf_len = 4, cmd->rdf_len = 256;
  37. hexTo(cmd->cdf, "54657374");
  38. count = apduCmdEnc(0, cmd);
  39. if (count > sizeof(apdu) ||
  40. count != 10 ||
  41. apduCmdEnc(apdu, cmd) != count ||
  42. !hexEq(apdu, "00A40404045465737400"))
  43. return FALSE;
  44. count1 = apduCmdDec(0, apdu, count);
  45. if (count1 > sizeof(stack) / 2 ||
  46. count1 != sizeof(apdu_cmd_t) + 4 ||
  47. apduCmdDec(cmd1, apdu, count) != count1 ||
  48. !memEq(cmd, cmd1, count1))
  49. return FALSE;
  50. // cmd: сочетания длин
  51. cmd->cla = 0x00, cmd->ins = 0xA4, cmd->p1 = 0x04, cmd->p2 = 0x04;
  52. memSet(cmd->cdf, 0x36, 257);
  53. for (cmd->cdf_len = 0; cmd->cdf_len <= 257; ++cmd->cdf_len)
  54. for (cmd->rdf_len = 0; cmd->rdf_len <= 257; ++cmd->rdf_len)
  55. {
  56. count = apduCmdEnc(0, cmd);
  57. if (count > sizeof(apdu) ||
  58. apduCmdEnc(apdu, cmd) != count)
  59. return FALSE;
  60. count1 = apduCmdDec(0, apdu, count);
  61. if (count1 > sizeof(stack) / 2 ||
  62. apduCmdDec(cmd1, apdu, count) != count1 ||
  63. !memEq(cmd, cmd1, count1))
  64. return FALSE;
  65. }
  66. // resp: точечный тест
  67. memSetZero(resp, sizeof(apdu_resp_t));
  68. resp->sw1 = 0x90, resp->sw2 = 0x00;
  69. cmd->rdf_len = 20;
  70. hexTo(resp->rdf, "E012C00401FF8010C00402FF8010C00403FF8010");
  71. count = apduRespEnc(0, resp);
  72. if (count > sizeof(apdu) ||
  73. count != 22 ||
  74. apduRespEnc(apdu, resp) != count ||
  75. !hexEq(apdu, "E012C00401FF8010C00402FF8010C00403FF80109000"))
  76. return FALSE;
  77. count1 = apduRespDec(0, apdu, count);
  78. if (count1 > sizeof(stack) / 2 ||
  79. count1 != sizeof(apdu_resp_t) + 20 ||
  80. apduRespDec(resp1, apdu, count) != count1 ||
  81. !memEq(resp, resp1, count1))
  82. return FALSE;
  83. // все нормально
  84. return TRUE;
  85. }