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