cleanup headers and especially #includes everywhere
[ntk/apt.git] / test / libapt / tagfile_test.cc
CommitLineData
453b82a3
DK
1#include <config.h>
2
c8b860fb
MV
3#include <apt-pkg/fileutl.h>
4#include <apt-pkg/tagfile.h>
5
453b82a3 6#include <string>
c8b860fb
MV
7#include <stdlib.h>
8#include <string.h>
0c98ee5a 9#include <unistd.h>
c8b860fb 10
453b82a3
DK
11#include "assert.h"
12
c8b860fb
MV
13char *tempfile = NULL;
14int tempfile_fd = -1;
15
c3ccac92 16static void remove_tmpfile(void)
c8b860fb
MV
17{
18 if (tempfile_fd > 0)
19 close(tempfile_fd);
20 if (tempfile != NULL) {
21 unlink(tempfile);
22 free(tempfile);
23 }
24}
25
65512241 26int main()
c8b860fb
MV
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}