tagfile_test.cc 1.5 KB

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