Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. #include <iostream>
  2. #include <string.h>
  3. #include <gtest/gtest.h>
  4. #include <libnfc_cpptools/LibNfc.h>
  5. #include <libnfc_cpptools/StringUtils.h>
  6. #include <libnfc_cpptools/ArrayUtils.h>
  7. #include <libnfc_cpptools/FreeFareAccessBits.h>
  8. TEST(StringUtils, rawToHumanChar)
  9. {
  10. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int8_t)255), "ff");
  11. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int8_t)127), "7f");
  12. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int8_t)10), "0a");
  13. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int8_t)0), "00");
  14. }
  15. TEST(StringUtils, rawToHumanShort)
  16. {
  17. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int16_t)65535), "ffff");
  18. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int16_t)65534), "fffe");
  19. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int16_t)256), "0100");
  20. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int16_t)255), "00ff");
  21. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int16_t)127), "007f");
  22. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int16_t)10), "000a");
  23. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int16_t)0), "0000");
  24. }
  25. TEST(StringUtils, rawToHumanInt)
  26. {
  27. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int32_t)4294967295), "ffffffff");
  28. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int32_t)4294967294), "fffffffe");
  29. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int32_t)65536), "00010000");
  30. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int32_t)65535), "0000ffff");
  31. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int32_t)65534), "0000fffe");
  32. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int32_t)256), "00000100");
  33. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int32_t)255), "000000ff");
  34. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int32_t)127), "0000007f");
  35. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int32_t)10), "0000000a");
  36. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int32_t)0), "00000000");
  37. }
  38. TEST(StringUtils, rawToHumanLong)
  39. {
  40. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int64_t)std::numeric_limits<unsigned long>::max()), "ffffffffffffffff");
  41. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int64_t)std::numeric_limits<unsigned long>::max() - 1), "fffffffffffffffe");
  42. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int64_t)4294967296), "0000000100000000");
  43. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int64_t)4294967295), "00000000ffffffff");
  44. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int64_t)4294967294), "00000000fffffffe");
  45. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int64_t)65536), "0000000000010000");
  46. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int64_t)65535), "000000000000ffff");
  47. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int64_t)65534), "000000000000fffe");
  48. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int64_t)256), "0000000000000100");
  49. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int64_t)255), "00000000000000ff");
  50. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int64_t)127), "000000000000007f");
  51. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int64_t)10), "000000000000000a");
  52. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman((int64_t)0), "0000000000000000");
  53. }
  54. TEST(StringUtils, rawToHumanString)
  55. {
  56. ASSERT_EQ(LibNfc::Utils::StringUtils::rawToHuman(std::string({0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
  57. 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f})),
  58. "000102030405060708090a0b0c0d0e0f");
  59. }
  60. TEST(StringUtils, toLower)
  61. {
  62. ASSERT_EQ(LibNfc::Utils::StringUtils::toLower('a'), 'a');
  63. ASSERT_EQ(LibNfc::Utils::StringUtils::toLower('z'), 'z');
  64. ASSERT_EQ(LibNfc::Utils::StringUtils::toLower('A'), 'a');
  65. ASSERT_EQ(LibNfc::Utils::StringUtils::toLower('Z'), 'z');
  66. ASSERT_EQ(LibNfc::Utils::StringUtils::toLower('1'), '1');
  67. ASSERT_EQ(LibNfc::Utils::StringUtils::toLower('@'), '@');
  68. }
  69. TEST(StringUtils, toUpper)
  70. {
  71. ASSERT_EQ(LibNfc::Utils::StringUtils::toUpper('a'), 'A');
  72. ASSERT_EQ(LibNfc::Utils::StringUtils::toUpper('z'), 'Z');
  73. ASSERT_EQ(LibNfc::Utils::StringUtils::toUpper('A'), 'A');
  74. ASSERT_EQ(LibNfc::Utils::StringUtils::toUpper('Z'), 'Z');
  75. ASSERT_EQ(LibNfc::Utils::StringUtils::toUpper('1'), '1');
  76. ASSERT_EQ(LibNfc::Utils::StringUtils::toUpper('@'), '@');
  77. }
  78. TEST(StringUtils, humanToRaw)
  79. {
  80. ASSERT_EQ(LibNfc::Utils::StringUtils::humanToRaw("0f00").getData(), std::string({0x0f, 0x00}));
  81. ASSERT_EQ(LibNfc::Utils::StringUtils::humanToRaw("000f").getData(), std::string({0x00, 0x0f}));
  82. ASSERT_EQ(LibNfc::Utils::StringUtils::humanToRaw("0a0a").getData(), std::string({0x0a, 0x0a}));
  83. ASSERT_EQ(LibNfc::Utils::StringUtils::humanToRaw("1a0f").getData(), std::string({0x1a, 0x0f}));
  84. ASSERT_EQ(LibNfc::Utils::StringUtils::humanToRaw("1a00f").getError(), "Malformed hex data: invalid length");
  85. ASSERT_EQ(LibNfc::Utils::StringUtils::humanToRaw("1a\n00f").getError(), "Malformed hex data at char 2");
  86. }
  87. TEST(StringUtils, ensureSize)
  88. {
  89. ASSERT_EQ(LibNfc::Utils::StringUtils::ensureSize("test", 3), "tes");
  90. ASSERT_EQ(LibNfc::Utils::StringUtils::ensureSize("test", 4), "test");
  91. ASSERT_EQ(LibNfc::Utils::StringUtils::ensureSize("test", 5), std::string({'t', 'e', 's', 't', '\0'}));
  92. }
  93. TEST(StringUtils, ltrim)
  94. {
  95. ASSERT_EQ(LibNfc::Utils::StringUtils::ltrim(" \n 42"), "42");
  96. ASSERT_EQ(LibNfc::Utils::StringUtils::ltrim(" \n 42 \n"), "42 \n");
  97. ASSERT_EQ(LibNfc::Utils::StringUtils::ltrim("42 \n "), "42 \n ");
  98. ASSERT_EQ(LibNfc::Utils::StringUtils::ltrim(" 42 \n 42 \n "), "42 \n 42 \n ");
  99. }
  100. TEST(StringUtils, rtrim)
  101. {
  102. ASSERT_EQ(LibNfc::Utils::StringUtils::rtrim(" \n 42"), " \n 42");
  103. ASSERT_EQ(LibNfc::Utils::StringUtils::rtrim(" \n 42 \n"), " \n 42");
  104. ASSERT_EQ(LibNfc::Utils::StringUtils::rtrim("42 \n "), "42");
  105. ASSERT_EQ(LibNfc::Utils::StringUtils::rtrim(" 42 \n 42 \n "), " 42 \n 42");
  106. }
  107. TEST(StringUtils, trim)
  108. {
  109. ASSERT_EQ(LibNfc::Utils::StringUtils::trim(" \n 42"), "42");
  110. ASSERT_EQ(LibNfc::Utils::StringUtils::trim(" \n 42 \n"), "42");
  111. ASSERT_EQ(LibNfc::Utils::StringUtils::trim("42 \n "), "42");
  112. ASSERT_EQ(LibNfc::Utils::StringUtils::trim(" 42 \n 42 \n "), "42 \n 42");
  113. }
  114. TEST(StringUtils, removeSpaces)
  115. {
  116. ASSERT_EQ(LibNfc::Utils::StringUtils::removeSpaces(" \n 42"), "42");
  117. ASSERT_EQ(LibNfc::Utils::StringUtils::removeSpaces(" \n 42 \n"), "42");
  118. ASSERT_EQ(LibNfc::Utils::StringUtils::removeSpaces("42 \n "), "42");
  119. ASSERT_EQ(LibNfc::Utils::StringUtils::removeSpaces(" 42 \n 42 \n "), "4242");
  120. }
  121. TEST(ArrayUtils, getArrayBitimple)
  122. {
  123. const unsigned char buf[] = {0x04};
  124. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 0));
  125. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 1));
  126. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 2));
  127. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 3));
  128. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 4));
  129. ASSERT_TRUE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 5));
  130. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 6));
  131. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 7));
  132. }
  133. TEST(ArrayUtils, getArrayBitMultiple)
  134. {
  135. const unsigned char buf[] = {0x80, 0x14, 0x01};
  136. ASSERT_TRUE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 0));
  137. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 1));
  138. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 2));
  139. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 3));
  140. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 4));
  141. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 5));
  142. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 6));
  143. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 7));
  144. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 8));
  145. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 9));
  146. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 10));
  147. ASSERT_TRUE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 11));
  148. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 12));
  149. ASSERT_TRUE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 13));
  150. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 14));
  151. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 15));
  152. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 16));
  153. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 17));
  154. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 18));
  155. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 19));
  156. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 20));
  157. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 21));
  158. ASSERT_FALSE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 22));
  159. ASSERT_TRUE(LibNfc::Utils::ArrayUtils::getArrayBit(buf, 23));
  160. }
  161. TEST(AccessBitsDbo, getBit)
  162. {
  163. const unsigned char buf[4] = {0x05, 0xa3, 0xcf, 0x00};
  164. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  165. ASSERT_FALSE(dbo.getBit(1, 0));
  166. ASSERT_TRUE(dbo.getBit(2, 0));
  167. ASSERT_FALSE(dbo.getBit(3, 0));
  168. ASSERT_TRUE(dbo.getBit(1, 1));
  169. ASSERT_TRUE(dbo.getBit(2, 1));
  170. ASSERT_FALSE(dbo.getBit(3, 1));
  171. ASSERT_FALSE(dbo.getBit(1, 2));
  172. ASSERT_TRUE(dbo.getBit(2, 2));
  173. ASSERT_TRUE(dbo.getBit(3, 2));
  174. ASSERT_TRUE(dbo.getBit(1, 3));
  175. ASSERT_TRUE(dbo.getBit(2, 3));
  176. ASSERT_TRUE(dbo.getBit(3, 3));
  177. }
  178. TEST(AccessBitsDbo, canKeyDoThisOnThisBlock1)
  179. {
  180. const unsigned char buf[4] = {0xff, 0x0f, 0x00, 0x00};
  181. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  182. ASSERT_TRUE(dbo.canKeyAReadBlock(0));
  183. ASSERT_TRUE(dbo.canKeyBReadBlock(0));
  184. ASSERT_TRUE(dbo.canKeyAWriteBlock(0));
  185. ASSERT_TRUE(dbo.canKeyBWriteBlock(0));
  186. ASSERT_TRUE(dbo.canKeyAIncrementBlock(0));
  187. ASSERT_TRUE(dbo.canKeyBIncrementBlock(0));
  188. ASSERT_TRUE(dbo.canKeyADecrementBlock(0));
  189. ASSERT_TRUE(dbo.canKeyBDecrementBlock(0));
  190. }
  191. TEST(AccessBitsDbo, canKeyDoThisOnThisBlock2)
  192. {
  193. const unsigned char buf[4] = {0xef, 0x0f, 0x01, 0x00};
  194. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  195. ASSERT_TRUE(dbo.canKeyAReadBlock(0));
  196. ASSERT_TRUE(dbo.canKeyBReadBlock(0));
  197. ASSERT_FALSE(dbo.canKeyAWriteBlock(0));
  198. ASSERT_FALSE(dbo.canKeyBWriteBlock(0));
  199. ASSERT_FALSE(dbo.canKeyAIncrementBlock(0));
  200. ASSERT_FALSE(dbo.canKeyBIncrementBlock(0));
  201. ASSERT_FALSE(dbo.canKeyADecrementBlock(0));
  202. ASSERT_FALSE(dbo.canKeyBDecrementBlock(0));
  203. }
  204. TEST(AccessBitsDbo, canKeyDoThisOnThisBlock3)
  205. {
  206. const unsigned char buf[4] = {0xfe, 0x1f, 0x00, 0x00};
  207. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  208. ASSERT_TRUE(dbo.canKeyAReadBlock(0));
  209. ASSERT_TRUE(dbo.canKeyBReadBlock(0));
  210. ASSERT_FALSE(dbo.canKeyAWriteBlock(0));
  211. ASSERT_TRUE(dbo.canKeyBWriteBlock(0));
  212. ASSERT_FALSE(dbo.canKeyAIncrementBlock(0));
  213. ASSERT_FALSE(dbo.canKeyBIncrementBlock(0));
  214. ASSERT_FALSE(dbo.canKeyADecrementBlock(0));
  215. ASSERT_FALSE(dbo.canKeyBDecrementBlock(0));
  216. }
  217. TEST(AccessBitsDbo, canKeyDoThisOnThisBlock4)
  218. {
  219. const unsigned char buf[4] = {0xee, 0x1f, 0x01, 0x00};
  220. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  221. ASSERT_TRUE(dbo.canKeyAReadBlock(0));
  222. ASSERT_TRUE(dbo.canKeyBReadBlock(0));
  223. ASSERT_FALSE(dbo.canKeyAWriteBlock(0));
  224. ASSERT_TRUE(dbo.canKeyBWriteBlock(0));
  225. ASSERT_FALSE(dbo.canKeyAIncrementBlock(0));
  226. ASSERT_TRUE(dbo.canKeyBIncrementBlock(0));
  227. ASSERT_TRUE(dbo.canKeyADecrementBlock(0));
  228. ASSERT_TRUE(dbo.canKeyBDecrementBlock(0));
  229. }
  230. TEST(AccessBitsDbo, canKeyDoThisOnThisBlock5)
  231. {
  232. const unsigned char buf[4] = {0xff, 0x0e, 0x10, 0x00};
  233. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  234. ASSERT_TRUE(dbo.canKeyAReadBlock(0));
  235. ASSERT_TRUE(dbo.canKeyBReadBlock(0));
  236. ASSERT_FALSE(dbo.canKeyAWriteBlock(0));
  237. ASSERT_FALSE(dbo.canKeyBWriteBlock(0));
  238. ASSERT_FALSE(dbo.canKeyAIncrementBlock(0));
  239. ASSERT_FALSE(dbo.canKeyBIncrementBlock(0));
  240. ASSERT_TRUE(dbo.canKeyADecrementBlock(0));
  241. ASSERT_TRUE(dbo.canKeyBDecrementBlock(0));
  242. }
  243. TEST(AccessBitsDbo, canKeyDoThisOnThisBlock6)
  244. {
  245. const unsigned char buf[4] = {0xef, 0x0e, 0x11, 0x00};
  246. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  247. ASSERT_FALSE(dbo.canKeyAReadBlock(0));
  248. ASSERT_TRUE(dbo.canKeyBReadBlock(0));
  249. ASSERT_FALSE(dbo.canKeyAWriteBlock(0));
  250. ASSERT_TRUE(dbo.canKeyBWriteBlock(0));
  251. ASSERT_FALSE(dbo.canKeyAIncrementBlock(0));
  252. ASSERT_FALSE(dbo.canKeyBIncrementBlock(0));
  253. ASSERT_FALSE(dbo.canKeyADecrementBlock(0));
  254. ASSERT_FALSE(dbo.canKeyBDecrementBlock(0));
  255. }
  256. TEST(AccessBitsDbo, canKeyDoThisOnThisBlock7)
  257. {
  258. const unsigned char buf[4] = {0xfe, 0x1e, 0x10, 0x00};
  259. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  260. ASSERT_FALSE(dbo.canKeyAReadBlock(0));
  261. ASSERT_TRUE(dbo.canKeyBReadBlock(0));
  262. ASSERT_FALSE(dbo.canKeyAWriteBlock(0));
  263. ASSERT_FALSE(dbo.canKeyBWriteBlock(0));
  264. ASSERT_FALSE(dbo.canKeyAIncrementBlock(0));
  265. ASSERT_FALSE(dbo.canKeyBIncrementBlock(0));
  266. ASSERT_FALSE(dbo.canKeyADecrementBlock(0));
  267. ASSERT_FALSE(dbo.canKeyBDecrementBlock(0));
  268. }
  269. TEST(AccessBitsDbo, canKeyDoThisOnThisBlock8)
  270. {
  271. const unsigned char buf[4] = {0xee, 0x1e, 0x11, 0x00};
  272. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  273. ASSERT_FALSE(dbo.canKeyAReadBlock(0));
  274. ASSERT_FALSE(dbo.canKeyBReadBlock(0));
  275. ASSERT_FALSE(dbo.canKeyAWriteBlock(0));
  276. ASSERT_FALSE(dbo.canKeyBWriteBlock(0));
  277. ASSERT_FALSE(dbo.canKeyAIncrementBlock(0));
  278. ASSERT_FALSE(dbo.canKeyBIncrementBlock(0));
  279. ASSERT_FALSE(dbo.canKeyADecrementBlock(0));
  280. ASSERT_FALSE(dbo.canKeyBDecrementBlock(0));
  281. }
  282. TEST(AccessBitsDbo, canKeyDoThisOnThisTrailer1)
  283. {
  284. const unsigned char buf[4] = {0xff, 0x0f, 0x00, 0x00};
  285. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  286. ASSERT_FALSE(dbo.canKeyAReadKeyATrailer());
  287. ASSERT_FALSE(dbo.canKeyBReadKeyATrailer());
  288. ASSERT_TRUE(dbo.canKeyAWriteKeyATrailer());
  289. ASSERT_FALSE(dbo.canKeyBWriteKeyATrailer());
  290. ASSERT_TRUE(dbo.canKeyAReadAccessBitsTrailer());
  291. ASSERT_FALSE(dbo.canKeyBReadAccessBitsTrailer());
  292. ASSERT_FALSE(dbo.canKeyAWriteAccessBitsTrailer());
  293. ASSERT_FALSE(dbo.canKeyBWriteAccessBitsTrailer());
  294. ASSERT_TRUE(dbo.canKeyAReadKeyBTrailer());
  295. ASSERT_FALSE(dbo.canKeyBReadKeyBTrailer());
  296. ASSERT_TRUE(dbo.canKeyAWriteKeyBTrailer());
  297. ASSERT_FALSE(dbo.canKeyBWriteKeyBTrailer());
  298. }
  299. TEST(AccessBitsDbo, canKeyDoThisOnThisTrailer2)
  300. {
  301. const unsigned char buf[4] = {0x7f, 0x0f, 0x08, 0x00};
  302. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  303. ASSERT_FALSE(dbo.canKeyAReadKeyATrailer());
  304. ASSERT_FALSE(dbo.canKeyBReadKeyATrailer());
  305. ASSERT_FALSE(dbo.canKeyAWriteKeyATrailer());
  306. ASSERT_FALSE(dbo.canKeyBWriteKeyATrailer());
  307. ASSERT_TRUE(dbo.canKeyAReadAccessBitsTrailer());
  308. ASSERT_FALSE(dbo.canKeyBReadAccessBitsTrailer());
  309. ASSERT_FALSE(dbo.canKeyAWriteAccessBitsTrailer());
  310. ASSERT_FALSE(dbo.canKeyBWriteAccessBitsTrailer());
  311. ASSERT_TRUE(dbo.canKeyAReadKeyBTrailer());
  312. ASSERT_FALSE(dbo.canKeyBReadKeyBTrailer());
  313. ASSERT_FALSE(dbo.canKeyAWriteKeyBTrailer());
  314. ASSERT_FALSE(dbo.canKeyBWriteKeyBTrailer());
  315. }
  316. TEST(AccessBitsDbo, canKeyDoThisOnThisTrailer3)
  317. {
  318. const unsigned char buf[4] = {0xf7, 0x8f, 0x00, 0x00};
  319. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  320. ASSERT_FALSE(dbo.canKeyAReadKeyATrailer());
  321. ASSERT_FALSE(dbo.canKeyBReadKeyATrailer());
  322. ASSERT_FALSE(dbo.canKeyAWriteKeyATrailer());
  323. ASSERT_TRUE(dbo.canKeyBWriteKeyATrailer());
  324. ASSERT_TRUE(dbo.canKeyAReadAccessBitsTrailer());
  325. ASSERT_TRUE(dbo.canKeyBReadAccessBitsTrailer());
  326. ASSERT_FALSE(dbo.canKeyAWriteAccessBitsTrailer());
  327. ASSERT_FALSE(dbo.canKeyBWriteAccessBitsTrailer());
  328. ASSERT_FALSE(dbo.canKeyAReadKeyBTrailer());
  329. ASSERT_FALSE(dbo.canKeyBReadKeyBTrailer());
  330. ASSERT_FALSE(dbo.canKeyAWriteKeyBTrailer());
  331. ASSERT_TRUE(dbo.canKeyBWriteKeyBTrailer());
  332. }
  333. TEST(AccessBitsDbo, canKeyDoThisOnThisTrailer4)
  334. {
  335. const unsigned char buf[4] = {0x77, 0x8f, 0x08, 0x00};
  336. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  337. ASSERT_FALSE(dbo.canKeyAReadKeyATrailer());
  338. ASSERT_FALSE(dbo.canKeyBReadKeyATrailer());
  339. ASSERT_FALSE(dbo.canKeyAWriteKeyATrailer());
  340. ASSERT_FALSE(dbo.canKeyBWriteKeyATrailer());
  341. ASSERT_TRUE(dbo.canKeyAReadAccessBitsTrailer());
  342. ASSERT_TRUE(dbo.canKeyBReadAccessBitsTrailer());
  343. ASSERT_FALSE(dbo.canKeyAWriteAccessBitsTrailer());
  344. ASSERT_FALSE(dbo.canKeyBWriteAccessBitsTrailer());
  345. ASSERT_FALSE(dbo.canKeyAReadKeyBTrailer());
  346. ASSERT_FALSE(dbo.canKeyBReadKeyBTrailer());
  347. ASSERT_FALSE(dbo.canKeyAWriteKeyBTrailer());
  348. ASSERT_FALSE(dbo.canKeyBWriteKeyBTrailer());
  349. }
  350. TEST(AccessBitsDbo, canKeyDoThisOnThisTrailer5)
  351. {
  352. const unsigned char buf[4] = {0xff, 0x07, 0x80, 0x00};
  353. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  354. ASSERT_FALSE(dbo.canKeyAReadKeyATrailer());
  355. ASSERT_FALSE(dbo.canKeyBReadKeyATrailer());
  356. ASSERT_TRUE(dbo.canKeyAWriteKeyATrailer());
  357. ASSERT_FALSE(dbo.canKeyBWriteKeyATrailer());
  358. ASSERT_TRUE(dbo.canKeyAReadAccessBitsTrailer());
  359. ASSERT_FALSE(dbo.canKeyBReadAccessBitsTrailer());
  360. ASSERT_TRUE(dbo.canKeyAWriteAccessBitsTrailer());
  361. ASSERT_FALSE(dbo.canKeyBWriteAccessBitsTrailer());
  362. ASSERT_TRUE(dbo.canKeyAReadKeyBTrailer());
  363. ASSERT_FALSE(dbo.canKeyBReadKeyBTrailer());
  364. ASSERT_TRUE(dbo.canKeyAWriteKeyBTrailer());
  365. ASSERT_FALSE(dbo.canKeyBWriteKeyBTrailer());
  366. }
  367. TEST(AccessBitsDbo, canKeyDoThisOnThisTrailer6)
  368. {
  369. const unsigned char buf[4] = {0x7f, 0x07, 0x88, 0x00};
  370. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  371. ASSERT_FALSE(dbo.canKeyAReadKeyATrailer());
  372. ASSERT_FALSE(dbo.canKeyBReadKeyATrailer());
  373. ASSERT_FALSE(dbo.canKeyAWriteKeyATrailer());
  374. ASSERT_TRUE(dbo.canKeyBWriteKeyATrailer());
  375. ASSERT_TRUE(dbo.canKeyAReadAccessBitsTrailer());
  376. ASSERT_TRUE(dbo.canKeyBReadAccessBitsTrailer());
  377. ASSERT_FALSE(dbo.canKeyAWriteAccessBitsTrailer());
  378. ASSERT_TRUE(dbo.canKeyBWriteAccessBitsTrailer());
  379. ASSERT_FALSE(dbo.canKeyAReadKeyBTrailer());
  380. ASSERT_FALSE(dbo.canKeyBReadKeyBTrailer());
  381. ASSERT_FALSE(dbo.canKeyAWriteKeyBTrailer());
  382. ASSERT_TRUE(dbo.canKeyBWriteKeyBTrailer());
  383. }
  384. TEST(AccessBitsDbo, canKeyDoThisOnThisTrailer7)
  385. {
  386. const unsigned char buf[4] = {0xf7, 0x87, 0x80, 0x00};
  387. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  388. ASSERT_FALSE(dbo.canKeyAReadKeyATrailer());
  389. ASSERT_FALSE(dbo.canKeyBReadKeyATrailer());
  390. ASSERT_FALSE(dbo.canKeyAWriteKeyATrailer());
  391. ASSERT_FALSE(dbo.canKeyBWriteKeyATrailer());
  392. ASSERT_TRUE(dbo.canKeyAReadAccessBitsTrailer());
  393. ASSERT_TRUE(dbo.canKeyBReadAccessBitsTrailer());
  394. ASSERT_FALSE(dbo.canKeyAWriteAccessBitsTrailer());
  395. ASSERT_TRUE(dbo.canKeyBWriteAccessBitsTrailer());
  396. ASSERT_FALSE(dbo.canKeyAReadKeyBTrailer());
  397. ASSERT_FALSE(dbo.canKeyBReadKeyBTrailer());
  398. ASSERT_FALSE(dbo.canKeyAWriteKeyBTrailer());
  399. ASSERT_FALSE(dbo.canKeyBWriteKeyBTrailer());
  400. }
  401. TEST(AccessBitsDbo, canKeyDoThisOnThisTrailer8)
  402. {
  403. const unsigned char buf[4] = {0x77, 0x87, 0x88, 0x00};
  404. LibNfc::FreeFare::FreeFareAccessBits dbo((const char*)buf);
  405. ASSERT_FALSE(dbo.canKeyAReadKeyATrailer());
  406. ASSERT_FALSE(dbo.canKeyBReadKeyATrailer());
  407. ASSERT_FALSE(dbo.canKeyAWriteKeyATrailer());
  408. ASSERT_FALSE(dbo.canKeyBWriteKeyATrailer());
  409. ASSERT_TRUE(dbo.canKeyAReadAccessBitsTrailer());
  410. ASSERT_TRUE(dbo.canKeyBReadAccessBitsTrailer());
  411. ASSERT_FALSE(dbo.canKeyAWriteAccessBitsTrailer());
  412. ASSERT_FALSE(dbo.canKeyBWriteAccessBitsTrailer());
  413. ASSERT_FALSE(dbo.canKeyAReadKeyBTrailer());
  414. ASSERT_FALSE(dbo.canKeyBReadKeyBTrailer());
  415. ASSERT_FALSE(dbo.canKeyAWriteKeyBTrailer());
  416. ASSERT_FALSE(dbo.canKeyBWriteKeyBTrailer());
  417. }
  418. int main(int argc, char* argv[])
  419. {
  420. std::cout << "LibNfc version: " << LibNfc::Core::LibNfcContext::getLibNfcVersion() << std::endl;
  421. ::testing::InitGoogleTest(&argc, argv);
  422. return RUN_ALL_TESTS();
  423. }