tagfile_test.cc 1.5 KB

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