tagfile_test.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <config.h>
  2. #include <apt-pkg/fileutl.h>
  3. #include <apt-pkg/tagfile.h>
  4. #include <string>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include "assert.h"
  9. char *tempfile = NULL;
  10. int tempfile_fd = -1;
  11. static void remove_tmpfile(void)
  12. {
  13. if (tempfile_fd > 0)
  14. close(tempfile_fd);
  15. if (tempfile != NULL) {
  16. unlink(tempfile);
  17. free(tempfile);
  18. }
  19. }
  20. int main()
  21. {
  22. FileFd fd;
  23. const char contents[] = "FieldA-12345678: the value of the field";
  24. atexit(remove_tmpfile);
  25. tempfile = strdup("apt-test.XXXXXXXX");
  26. tempfile_fd = mkstemp(tempfile);
  27. /* (Re-)Open (as FileFd), write and seek to start of the temp file */
  28. equals(fd.OpenDescriptor(tempfile_fd, FileFd::ReadWrite), true);
  29. equals(fd.Write(contents, strlen(contents)), true);
  30. equals(fd.Seek(0), true);
  31. pkgTagFile tfile(&fd);
  32. pkgTagSection section;
  33. equals(tfile.Step(section), true);
  34. /* It has one field */
  35. equals(section.Count(), 1);
  36. /* ... and it is called FieldA-12345678 */
  37. equals(section.Exists("FieldA-12345678"), true);
  38. /* its value is correct */
  39. equals(section.FindS("FieldA-12345678"), std::string("the value of the field"));
  40. /* A non-existent field has an empty string as value */
  41. equals(section.FindS("FieldB-12345678"), std::string());
  42. /* ... and Exists does not lie about missing fields... */
  43. equalsNot(section.Exists("FieldB-12345678"), true);
  44. /* There is only one section in this tag file */
  45. equals(tfile.Step(section), false);
  46. /* clean up handled by atexit handler, so just return here */
  47. return 0;
  48. }