1# To do: Should run all tests and return a useful exit status, not
2# punt on the first failure.
3
4set wd [pwd]
5set verbose 0
6
7proc test1 {} {
8    global wd verbose
9    set p [profile_init_path $wd/test2.ini]
10    set sect {{test section 1} child_section child}
11    set iter [profile_iterator_create $p $sect 0]
12    set done 0
13    if $verbose { puts "Iterating over {$sect} entries:" }
14    while {!$done} {
15	set pair [profile_iterator $iter]
16	if [string match $pair {{} {}}] {
17	    set done 1
18	} else {
19	    set val [lindex $pair 1]
20	    if $verbose { puts -nonewline "\t$val" }
21	}
22    }
23    if $verbose { puts "" }
24    profile_iterator_free $iter
25
26    set iter [profile_iterator_create $p $sect 0]
27    set done 0
28    if $verbose { puts "Iterating again, deleting:" }
29    while {!$done} {
30	set pair [profile_iterator $iter]
31	if [string match $pair {{} {}}] {
32	    set done 1
33	} else {
34	    set val [lindex $pair 1]
35	    if $verbose { puts -nonewline "\t$val" }
36	    profile_update_relation $p $sect $val
37	}
38    }
39    if $verbose { puts "" }
40    profile_iterator_free $iter
41    catch {file delete $wd/test3.ini}
42    profile_flush_to_file $p $wd/test3.ini
43    profile_abandon $p
44
45    if $verbose { puts "Reloading new profile" }
46    set p [profile_init_path $wd/test3.ini]
47    set iter [profile_iterator_create $p $sect 0]
48    set done 0
49    if $verbose { puts "Iterating again:" }
50    set found_some 0
51    while {!$done} {
52	set pair [profile_iterator $iter]
53	if [string match $pair {{} {}}] {
54	    set done 1
55	} else {
56	    set found_some 1
57	    set val [lindex $pair 1]
58	    if $verbose { puts -nonewline "\t$val" }
59	}
60    }
61    profile_iterator_free $iter
62    profile_abandon $p
63
64    if {$found_some} {
65	if $verbose { puts "" }
66	puts stderr "Error: Deleting in iterator didn't get them all."
67	exit 1
68    } else {
69	puts "OK: test1: Deleting in iteration got rid of all entries."
70    }
71}
72
73proc test2 {} {
74    global wd verbose
75
76    # lxs said: create A, read A, flush A, read A, create B, read B, crash
77    # (where "create" refers to the object, not the file)
78
79    if $verbose { puts "Running test2" }
80    set c [profile_init_path $wd/test2.ini]
81    # create A
82    set a [profile_init_path $wd/test2.ini]
83    if $verbose { puts "Opened profile $wd/test2.ini" }
84    # read A
85    set x [profile_get_values $a {{test section 1} foo}]
86    if $verbose { puts "Read $x from profile" }
87    if $verbose { puts "updating" }
88    exec sleep 2
89    profile_update_relation $a {{test section 1} foo} [lindex $x 0] [lindex $x 0]
90    set x [profile_get_values $a {{test section 1} foo}]
91    if $verbose { puts "Read $x from profile" }
92    # flush A
93    profile_flush $a
94    # read A again
95    set x [profile_get_values $a {{test section 1} foo}]
96    if $verbose { puts "Read $x from profile" }
97    profile_release $a
98    # create B
99    set b [profile_init_path $wd/test2.ini]
100    if $verbose { puts "Opened profile again" }
101    # read B
102    set x [profile_get_values $b {{test section 1} foo}]
103    if $verbose { puts "Read $x from profile" }
104    # read B
105    set x [profile_get_values $b {{test section 1} foo}]
106    if $verbose { puts "Read $x from profile" }
107    # If we got this far, now what?
108    profile_release $b
109    profile_release $c
110    puts "OK: test2: Modifications don't corrupt existing open handles"
111}
112
113proc test3 {} {
114    # lxs said: Start with a relation in the file.  Open, delete
115    # relation, add relation back, list relations.  In 1.4 release
116    # code, got two back.
117
118    global wd verbose
119
120    exec cp $wd/test2.ini $wd/test1c.ini
121    set p [profile_init_path $wd/test1c.ini]
122    set sect {{test section 1} quux}
123
124    set v [profile_get_values $p $sect]
125    set v1 [lindex $v 0]
126    if $verbose { puts "Old values: $v" }
127    profile_clear_relation $p $sect
128    if $verbose { puts "Cleared." }
129    # profile_get_values raises an exception if no data is there; so if
130    # it succeeds, the test fails.
131    catch {
132	set v [profile_get_values $p $sect]
133	if $verbose { puts "New values: $v" }
134	puts stderr "Error: test3: Clearing relation didn't get rid of all values."
135	exit 1
136    }
137    if $verbose { puts "Adding back $v1 ..." }
138    profile_add_relation $p $sect $v1
139    set v [profile_get_values $p $sect]
140    if $verbose { puts "New values: $v" }
141    if [llength $v]!=1 {
142	puts stderr "Error: test3: Adding one entry after clearing relation leaves [llength $v] entries."
143	exit 1
144    }
145    profile_abandon $p
146    file delete $wd/test1c.ini
147    puts "OK: test3: Clearing relation and adding one entry yields correct count."
148}
149
150# Exercise the include and includedir directives.
151proc test4 {} {
152    global wd verbose
153
154    # Test expected error message when including nonexistent file.
155    catch [file delete $wd/testinc.ini]
156    exec echo "include does-not-exist" >$wd/testinc.ini
157    catch { profile_init_path $wd/testinc.ini } err
158    if $verbose { puts "Got error message $err" }
159    if ![string equal $err "Included profile file could not be read"] {
160	puts stderr "Error: test4: Did not get expected error when including nonexistent file."
161	exit 1
162    }
163
164    # Test expected error message when including nonexistent directory.
165    catch [file delete $wd/testinc.ini]
166    exec echo "includedir does-not-exist" >$wd/testinc.ini
167    catch { profile_init_path $wd/testinc.ini } err
168    if $verbose { puts "Got error message $err" }
169    if ![string equal $err "Included profile directory could not be read"] {
170	puts stderr "Error: test4: Did not get expected error when including nonexistent directory."
171	exit 1
172    }
173
174    # Test including a file.
175    catch [file delete $wd/testinc.ini]
176    exec echo "include $wd/test2.ini" >$wd/testinc.ini
177    set p [profile_init_path $wd/testinc.ini]
178    set x [profile_get_values $p {{test section 1} bar}]
179    if $verbose { puts "Read $x from included profile" }
180    if ![string equal [lindex $x 0] "foo"] {
181	puts stderr "Error: test4: Did not get expected result from included profile."
182	exit 1
183    }
184    profile_release $p
185
186    # Test including a directory.  Put four copies of test2.ini inside
187    # the directory, two with invalid names.  Check that we get two
188    # values for one of the variables.
189    catch [file delete -force $wd/test_include_dir]
190    exec mkdir $wd/test_include_dir
191    exec cp $wd/test2.ini $wd/test_include_dir/a
192    exec cp $wd/test2.ini $wd/test_include_dir/a~
193    exec cp $wd/test2.ini $wd/test_include_dir/b.conf
194    exec cp $wd/test2.ini $wd/test_include_dir/b.conf.rpmsave
195    catch [file delete $wd/testinc.ini]
196    exec echo "includedir $wd/test_include_dir" >$wd/testinc.ini
197    set p [profile_init_path $wd/testinc.ini]
198    set x [profile_get_values $p {{test section 1} bar}]
199    if $verbose { puts "Read $x from included directory" }
200    if ![string equal $x "foo foo"] {
201	puts stderr, "Error: test4: Did not get expected result from included directory."
202	exit 1
203    }
204    profile_release $p
205
206    # Directly list the directory in the profile path and try again.
207    set p [profile_init_path $wd/test_include_dir]
208    set x [profile_get_values $p {{test section 1} bar}]
209    if $verbose { puts "Read $x from directory" }
210    if ![string equal $x "foo foo"] {
211	puts stderr, "Error: test4: Did not get expected result from directory."
212	exit 1
213    }
214    profile_release $p
215
216    puts "OK: test4: include and includedir directives"
217}
218
219proc test5 {} {
220    global wd verbose
221
222    # Test syntactic independence of included profile files.
223    catch [file delete $wd/testinc.ini]
224    set f [open "$wd/testinc.ini" w]
225    puts $f {[sec1]}
226    puts $f "var = {"
227    puts $f "a = 1"
228    puts $f "include testinc2.ini"
229    puts $f "c = 3"
230    puts $f "}"
231    close $f
232    catch [file delete $wd/testinc2.ini]
233    set f [open "$wd/testinc2.ini" w]
234    puts $f {[sec2]}
235    puts $f "b = 2"
236    close $f
237    set p [profile_init_path $wd/testinc.ini]
238    set a [profile_get_values $p {sec1 var a}]
239    set b [profile_get_values $p {sec2 b}]
240    set c [profile_get_values $p {sec1 var c}]
241    if $verbose { puts "Read values [concat $a $b $c] from profile" }
242    if { $a != 1 || $b != 2 || $c != 3 } {
243	puts stderr, "Error: test5: Wrong results from profile"
244	exit 1
245    }
246    profile_release $p
247
248    puts "OK: test5: syntax independence of included files"
249}
250
251proc test6 {} {
252    global wd verbose
253
254    # If a section is deleted and replaced, the new section should be
255    # used when retrieving values.
256    set p [profile_init_path $wd/test2.ini]
257    set sect {{test section 1}}
258    set newrel [concat $sect testkey]
259    set oldrel [concat $sect child]
260    if $verbose { puts "Removing and replacing {$sect}" }
261    profile_rename_section $p $sect
262    profile_add_relation $p $sect
263    if $verbose { puts "Adding {$newrel}" }
264    profile_add_relation $p $newrel 6
265    set x [profile_get_values $p $newrel]
266    if $verbose { puts "Read from new relation {$newrel}: $x" }
267    if { $x != 6 } {
268	puts stderr, "Error: test6: Could not get value from new section"
269	exit 1
270    }
271    if $verbose { puts "Reading old relation {$oldrel} which should be gone" }
272    catch {
273	profile_get_values $p $oldrel
274	puts stderr, "Error: test6: Got value from deleted section"
275	exit 1
276    }
277    profile_abandon $p
278
279    puts "OK: test6: section replacement"
280}
281
282proc test7 {} {
283    global wd verbose
284
285    # A deleted node at the end of a relation's value set should not cause
286    # profile_clear_relation to error, as long as some value is present.
287    set p [profile_init_path $wd/test2.ini]
288    set rel {{test section 1} testkey}
289    if $verbose { puts "Adding values 1 2 at {$rel}" }
290    profile_add_relation $p $rel 1
291    profile_add_relation $p $rel 2
292    if $verbose { puts "Removing value 2 at {$rel}" }
293    profile_update_relation $p $rel 2
294    if $verbose { puts "Clearing values at {$rel}" }
295    profile_clear_relation $p $rel
296    profile_abandon $p
297    puts "OK: test7: profile_clear_relation with deleted node at end"
298}
299
300proc test8 {} {
301    global wd verbose
302
303    # Order of relation operations should be reflected even if some of
304    # the relations were deleted.
305    set p [profile_init_path $wd/test2.ini]
306    set rel {{test section 1} testkey}
307    if $verbose { puts "Adding values 1 2 3 at {$rel}" }
308    profile_add_relation $p $rel 1
309    profile_add_relation $p $rel 2
310    profile_add_relation $p $rel 3
311    if $verbose { puts "Removing values 2 and adding 4 at {$rel}" }
312    profile_update_relation $p $rel 2
313    profile_add_relation $p $rel 4
314    set x [profile_get_values $p $rel]
315    if $verbose { puts "Read values from {$rel}: $x" }
316    if { $x != {1 3 4} } {
317	puts stderr, "Error: test8: Wrong order of values: $x"
318	exit 1
319    }
320    profile_abandon $p
321
322    puts "OK: test8: relation order in the presence of deletions"
323}
324
325proc test9 {} {
326    global wd verbose
327
328    # Regression test for #8431: profile_flush_to_file erroneously
329    # cleared the DIRTY and SHARED flags from the data object, which
330    # could lead to a dangling reference in g_shared_trees on release.
331    set p [profile_init_path $wd/test2.ini]
332    catch {file delete $wd/test3.ini}
333    profile_flush_to_file $p $wd/test3.ini
334    profile_release $p
335
336    # If a dangling reference was created in g_shared_trees, the next
337    # profile open will trigger an assertion failure.
338    set p [profile_init_path $wd/test2.ini]
339    profile_release $p
340
341    puts "OK: test9: profile_flush_to_file with no changes"
342}
343
344proc test10 {} {
345    global wd verbose
346
347    # Regression test for #7863: multiply-specified subsections should
348    # be merged.
349    set p [profile_init_path $wd/test2.ini]
350    set x [profile_get_values $p {{test section 2} child_section2 child}]
351    if $verbose { puts "Read $x from profile" }
352    if ![string equal $x "slick harry {john\tb } ron"] {
353	puts stderr "Error: test10: Did not get expected merged children."
354	exit 1
355    }
356
357    set x [profile_get_string $p {test section 2} child_section2 chores]
358    if $verbose { puts "Read $x from profile" }
359    if ![string equal $x "cleaning"] {
360	puts stderr "Error: test10: Did not find expected chores."
361	exit 1
362    }
363    profile_release $p
364}
365
366test1
367test2
368test3
369test4
370test5
371test6
372test7
373test8
374test9
375test10
376
377exit 0
378