1 : /*
2 : * Copyright (C) 2007 Enrico Zini <enrico@enricozini.org>
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Lesser General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2.1 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Lesser General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Lesser General Public
15 : * License along with this library; if not, write to the Free Software
16 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 : */
18 :
19 : #include <ept/test.h>
20 : #include <ept/apt/recordparser.h>
21 :
22 : //#include <iostream>
23 :
24 : using namespace std;
25 : using namespace ept;
26 : using namespace ept::apt;
27 :
28 12 : struct TestAptRecordparser {
29 : std::string record;
30 12 : TestAptRecordparser()
31 12 : {
32 : record =
33 : "A:\n"
34 : "D: da de di do du\n"
35 : "B: b\n"
36 : "C: c \n"
37 : "Desc: this is the beginning\n"
38 : " this is the continuation\n"
39 12 : " this is the end\n";
40 12 : }
41 :
42 : // Check that the fields are identified and broken up correctly
43 1 : Test parsing()
44 : {
45 1 : RecordParser p(record);
46 :
47 1 : assert_eq(p.record(), record);
48 1 : assert_eq(p.size(), 5u);
49 1 : }
50 :
51 1 : Test fieldTuples()
52 : {
53 1 : RecordParser p(record);
54 1 : assert_eq(p.field(0), "A:\n");
55 1 : assert_eq(p.field(1), "D: da de di do du\n");
56 1 : assert_eq(p.field(2), "B: b\n");
57 1 : assert_eq(p.field(3), "C: c \n");
58 1 : assert_eq(p.field(4), "Desc: this is the beginning\n this is the continuation\n this is the end\n");
59 1 : }
60 :
61 1 : Test fieldKeys()
62 : {
63 1 : RecordParser p(record);
64 1 : assert_eq(p.name(0), "A");
65 1 : assert_eq(p.name(1), "D");
66 1 : assert_eq(p.name(2), "B");
67 1 : assert_eq(p.name(3), "C");
68 1 : assert_eq(p.name(4), "Desc");
69 1 : }
70 :
71 1 : Test fieldValues()
72 : {
73 1 : RecordParser p(record);
74 1 : assert_eq(p[0], "");
75 1 : assert_eq(p[1], "da de di do du");
76 1 : assert_eq(p[2], "b");
77 1 : assert_eq(p[3], "c");
78 1 : assert_eq(p[4], "this is the beginning\n this is the continuation\n this is the end");
79 1 : }
80 :
81 : // Check that the field search by name finds all the fields
82 1 : Test findByName()
83 : {
84 1 : RecordParser p(record);
85 :
86 1 : assert_eq(p.index("A"), 0u);
87 2 : assert_eq(p.index("D"), 1u);
88 2 : assert_eq(p.index("B"), 2u);
89 2 : assert_eq(p.index("C"), 3u);
90 2 : assert_eq(p.index("Desc"), 4u);
91 :
92 2 : assert_eq(p.name(p.index("A")), "A");
93 2 : assert_eq(p.name(p.index("B")), "B");
94 2 : assert_eq(p.name(p.index("C")), "C");
95 2 : assert_eq(p.name(p.index("D")), "D");
96 2 : assert_eq(p.name(p.index("Desc")), "Desc");
97 1 : }
98 :
99 1 : Test indexing()
100 : {
101 1 : RecordParser p(record);
102 1 : assert_eq(p["A"], "");
103 2 : assert_eq(p["B"], "b");
104 2 : assert_eq(p["C"], "c");
105 2 : assert_eq(p["D"], "da de di do du");
106 2 : assert_eq(p["Desc"], "this is the beginning\n this is the continuation\n this is the end");
107 1 : }
108 :
109 1 : Test missingBehaviour()
110 : {
111 1 : RecordParser p(record);
112 : // Missing fields give empty strings
113 1 : assert_eq(p.field(100), "");
114 1 : assert_eq(p.name(100), "");
115 1 : assert_eq(p[100], "");
116 1 : assert_eq(p["Missing"], "");
117 1 : }
118 :
119 : // Check that scanning twice replaces the old fields
120 1 : Test rescan()
121 : {
122 : std::string record =
123 : "A: a\n"
124 : "B: b\n"
125 1 : "C: c\n";
126 :
127 1 : RecordParser p(record);
128 1 : assert_eq(p.size(), 3u);
129 2 : assert_eq(p["A"], "a");
130 2 : assert_eq(p["B"], "b");
131 2 : assert_eq(p["C"], "c");
132 :
133 : std::string record1 =
134 : "Foo: bar\n"
135 2 : "A: different\n";
136 :
137 1 : p.scan(record1);
138 :
139 : //for (size_t i = 0; i < p.size(); ++i)
140 : // cerr << ">> " << i << "==" << p.index(p.name(i)) << " " << p.name(i) << " " << p[i] << endl;
141 :
142 1 : assert_eq(p.size(), 2u);
143 2 : assert_eq(p["A"], "different");
144 2 : assert_eq(p["B"], "");
145 2 : assert_eq(p["C"], "");
146 2 : assert_eq(p["Foo"], "bar");
147 1 : }
148 :
149 : // Real-life example
150 1 : Test realLife()
151 : {
152 : string record =
153 : "Package: apt\n"
154 : "Priority: important\n"
155 : "Section: admin\n"
156 : "Installed-Size: 4368\n"
157 : "Maintainer: APT Development Team <deity@lists.debian.org>\n"
158 : "Architecture: amd64\n"
159 : "Version: 0.6.46.4-0.1\n"
160 : "Replaces: libapt-pkg-doc (<< 0.3.7), libapt-pkg-dev (<< 0.3.7)\n"
161 : "Provides: libapt-pkg-libc6.3-6-3.11\n"
162 : "Depends: libc6 (>= 2.3.5-1), libgcc1 (>= 1:4.1.1-12), libstdc++6 (>= 4.1.1-12), debian-archive-keyring\n"
163 : "Suggests: aptitude | synaptic | gnome-apt | wajig, dpkg-dev, apt-doc, bzip2\n"
164 : "Filename: pool/main/a/apt/apt_0.6.46.4-0.1_amd64.deb\n"
165 : "Size: 1436478\n"
166 : "MD5sum: 1776421f80d6300c77a608e77a9f4a15\n"
167 : "SHA1: 1bd7337d2df56d267632cf72ac930c0a4895898f\n"
168 : "SHA256: b92442ab60046b4d0728245f39cc932f26e17db9f7933a5ec9aaa63172f51fda\n"
169 : "Description: Advanced front-end for dpkg\n"
170 : " This is Debian's next generation front-end for the dpkg package manager.\n"
171 : " It provides the apt-get utility and APT dselect method that provides a\n"
172 : " simpler, safer way to install and upgrade packages.\n"
173 : " .\n"
174 : " APT features complete installation ordering, multiple source capability\n"
175 : " and several other unique features, see the Users Guide in apt-doc.\n"
176 : "Build-Essential: yes\n"
177 1 : "Tag: admin::package-management, filetransfer::ftp, filetransfer::http, hardware::storage:cd, interface::commandline, network::client, protocol::{ftp,http,ipv6}, role::program, suite::debian, use::downloading, use::searching, works-with::software:package\n";
178 1 : RecordParser p(record);
179 :
180 1 : assert_eq(p.size(), 19u);
181 :
182 1 : string rec1;
183 20 : for (size_t i = 0; i < p.size(); ++i)
184 19 : rec1 += p.field(i);
185 1 : assert_eq(record, rec1);
186 1 : }
187 :
188 : // Various buffer termination patterns
189 1 : Test bufferTermination()
190 : {
191 : std::string record =
192 : "A: a\n"
193 1 : "B: b";
194 :
195 1 : RecordParser p(record);
196 1 : assert_eq(p.size(), 2u);
197 2 : assert_eq(p["A"], "a");
198 2 : assert_eq(p["B"], "b");
199 1 : }
200 :
201 1 : Test bufferTermination2()
202 : {
203 : std::string record =
204 : "A: a\n"
205 1 : "B: b\n\n";
206 :
207 1 : RecordParser p(record);
208 1 : assert_eq(p.size(), 2u);
209 2 : assert_eq(p["A"], "a");
210 2 : assert_eq(p["B"], "b");
211 1 : }
212 :
213 1 : Test bufferTermination3()
214 : {
215 : std::string record =
216 : "A: a\n"
217 : "B: b\n\n"
218 1 : "C: c\n";
219 :
220 1 : RecordParser p(record);
221 1 : assert_eq(p.size(), 2u);
222 2 : assert_eq(p["A"], "a");
223 2 : assert_eq(p["B"], "b");
224 1 : }
225 :
226 : };
227 :
228 : // vim:set ts=4 sw=4:
|