ReplayProtector_test.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/ReplayProtector.h"
  16. #include "crypto/random/Random.h"
  17. #include "memory/MallocAllocator.h"
  18. #include "util/Assert.h"
  19. #include <stdint.h>
  20. #include <stddef.h>
  21. #define CYCLES 1
  22. static void testDuplicates(struct Random* rand)
  23. {
  24. uint16_t randomShorts[8192];
  25. uint16_t out[8192];
  26. struct ReplayProtector rp = {.bitfield = 0};
  27. Random_bytes(rand, (uint8_t*)randomShorts, sizeof(randomShorts));
  28. uint32_t outIdx = 0;
  29. for (uint32_t i = 0; i < 1024; i++) {
  30. if (ReplayProtector_checkNonce((randomShorts[i] % (i + 20)), &rp)) {
  31. out[outIdx] = (randomShorts[i] % (i + 20));
  32. outIdx++;
  33. }
  34. }
  35. for (uint32_t i = 0; i < outIdx; i++) {
  36. for (uint32_t j = i + 1; j < outIdx; j++) {
  37. Assert_true(out[i] != out[j]);
  38. }
  39. }
  40. }
  41. int main()
  42. {
  43. struct Allocator* alloc = MallocAllocator_new(4096);
  44. struct Random* rand = Random_new(alloc, NULL, NULL);
  45. for (int i = 0; i < CYCLES; i++) {
  46. testDuplicates(rand);
  47. }
  48. return 0;
  49. }