test_util.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Name: test_util.c
  2. *
  3. * This file contains functions for manipulating tagged flows.
  4. *
  5. * Slitheen - a decoy routing system for censorship resistance
  6. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, version 3.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Additional permission under GNU GPL version 3 section 7
  21. *
  22. * If you modify this Program, or any covered work, by linking or combining
  23. * it with the OpenSSL library (or a modified version of that library),
  24. * containing parts covered by the terms of the OpenSSL Licence and the
  25. * SSLeay license, the licensors of this Program grant you additional
  26. * permission to convey the resulting work. Corresponding Source for a
  27. * non-source form of such a combination shall include the source code
  28. * for the parts of the OpenSSL library used as well as that of the covered
  29. * work.
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <stdint.h>
  34. #include <check.h>
  35. #include "../util.h"
  36. int32_t read_file(const char *path, uint8_t **target){
  37. FILE *fp;
  38. int32_t fsize;
  39. fp = fopen(path, "rb");
  40. if (fp == NULL) {
  41. perror("fopen");
  42. return 0;
  43. }
  44. fseek(fp, 0, SEEK_END);
  45. fsize = ftell(fp);
  46. fseek(fp, 0, SEEK_SET);
  47. *target = smalloc(fsize);
  48. int32_t result = fread(*target, fsize, 1, fp);
  49. fclose(fp);
  50. return result;
  51. }
  52. int32_t read_file_len(const char *path, uint8_t **target, int32_t *len){
  53. FILE *fp;
  54. int32_t fsize;
  55. fp = fopen(path, "rb");
  56. if (fp == NULL) {
  57. perror("fopen");
  58. return 0;
  59. }
  60. fseek(fp, 0, SEEK_END);
  61. fsize = ftell(fp);
  62. fseek(fp, 0, SEEK_SET);
  63. *target = smalloc(fsize);
  64. int32_t result = fread(*target, fsize, 1, fp);
  65. *len = fsize;
  66. fclose(fp);
  67. return result;
  68. }