t-buffer.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * t-buffer.c - test buffer handling
  4. *
  5. * Copyright © 2009-2011 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This 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 <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <dpkg/test.h>
  23. #include <dpkg/buffer.h>
  24. #include <dpkg/dpkg.h>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. static const char str_empty[] = "";
  30. static const char ref_hash_empty[] = "d41d8cd98f00b204e9800998ecf8427e";
  31. static const char str_test[] = "this is a test string\n";
  32. static const char ref_hash_test[] = "475aae3b885d70a9130eec23ab33f2b9";
  33. static void
  34. test_buffer_hash(void)
  35. {
  36. char hash[MD5HASHLEN + 1];
  37. buffer_md5(str_empty, hash, strlen(str_empty));
  38. test_str(hash, ==, ref_hash_empty);
  39. buffer_md5(str_test, hash, strlen(str_test));
  40. test_str(hash, ==, ref_hash_test);
  41. }
  42. static void
  43. test_fdio_hash(void)
  44. {
  45. char hash[MD5HASHLEN + 1];
  46. char *test_file;
  47. int fd;
  48. test_file = test_alloc(strdup("test.XXXXXX"));
  49. fd = mkstemp(test_file);
  50. test_pass(fd >= 0);
  51. test_pass(fd_md5(fd, hash, -1, NULL) >= 0);
  52. test_str(hash, ==, ref_hash_empty);
  53. test_pass(write(fd, str_test, strlen(str_test)) == strlen(str_test));
  54. test_pass(lseek(fd, 0, SEEK_SET) == 0);
  55. test_pass(fd_md5(fd, hash, -1, NULL) >= 0);
  56. test_str(hash, ==, ref_hash_test);
  57. test_pass(unlink(test_file) == 0);
  58. }
  59. static void
  60. test(void)
  61. {
  62. test_plan(10);
  63. test_buffer_hash();
  64. test_fdio_hash();
  65. }