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