]> git.saurik.com Git - apt.git/blob - test/libapt/tagfile_test.cc
2e2144f995f3090d0009de0db13958722e5fbe5c
[apt.git] / test / libapt / tagfile_test.cc
1 #include <apt-pkg/fileutl.h>
2 #include <apt-pkg/tagfile.h>
3
4 #include "assert.h"
5 #include <stdlib.h>
6 #include <string.h>
7
8 char *tempfile = NULL;
9 int tempfile_fd = -1;
10
11 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
21 int main(int argc, char *argv[])
22 {
23 FileFd fd;
24 const char contents[] = "FieldA-12345678: the value of the field";
25 atexit(remove_tmpfile);
26 tempfile = strdup("apt-test.XXXXXXXX");
27 tempfile_fd = mkstemp(tempfile);
28
29 /* (Re-)Open (as FileFd), write and seek to start of the temp file */
30 equals(fd.OpenDescriptor(tempfile_fd, FileFd::ReadWrite), true);
31 equals(fd.Write(contents, strlen(contents)), true);
32 equals(fd.Seek(0), true);
33
34 pkgTagFile tfile(&fd);
35 pkgTagSection section;
36 equals(tfile.Step(section), true);
37
38 /* It has one field */
39 equals(section.Count(), 1);
40
41 /* ... and it is called FieldA-12345678 */
42 equals(section.Exists("FieldA-12345678"), true);
43
44 /* its value is correct */
45 equals(section.FindS("FieldA-12345678"), std::string("the value of the field"));
46 /* A non-existent field has an empty string as value */
47 equals(section.FindS("FieldB-12345678"), std::string());
48
49 /* ... and Exists does not lie about missing fields... */
50 equalsNot(section.Exists("FieldB-12345678"), true);
51
52 /* There is only one section in this tag file */
53 equals(tfile.Step(section), false);
54
55 /* clean up handled by atexit handler, so just return here */
56 return 0;
57 }