aboutsummaryrefslogtreecommitdiff
path: root/test/libelftc/tset/elftc_string_table/string_table.m4
blob: 0c6f730686db951adf00d3bcb6c625db0c336457 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
/*-
 * Copyright (c) 2013 Joseph Koshy
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: string_table.m4 2868 2013-01-06 13:28:47Z jkoshy $
 */

/*
 * include(`elfts.m4')
 */
#include <sys/types.h>

#include <errno.h>
#include <fcntl.h>
#include <libelftc.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "tet_api.h"

/*
 * A list of test strings.
 *
 * For the curious, these are titles of stories by Cordwainer Smith.
 */

static const char *test_strings[] = {
	"Mark Elf",
	"Scanners Live in Vain",
	"The Dead Lady of Clown Town",
	"The Lady Who Sailed the \"The Soul\"",
	"No, No, Not Rogov!",
	NULL
};

static const int nteststrings = sizeof(test_strings) / sizeof(test_strings[0]);

static char test_image[] = {
	0,
	'M', 'a', 'r', 'k', ' ', 'E', 'l', 'f', 0,
	'S', 'c', 'a', 'n', 'n', 'e', 'r', 's', ' ',
	'L', 'i', 'v', 'e', ' ', 'i', 'n', ' ', 'V', 'a', 'i', 'n', 0,
	'T', 'h', 'e', ' ', 'D', 'e', 'a', 'd', ' ',
	'L', 'a', 'd', 'y', ' ', 'o', 'f', ' ', 'C', 'l', 'o', 'w', 'n', ' ',
	'T', 'o', 'w', 'n', 0,
	'T', 'h', 'e', ' ', 'L', 'a', 'd', 'y', ' ', 'W', 'h', 'o', ' ',
	'S', 'a', 'i', 'l', 'e', 'd', ' ', 't', 'h', 'e', ' ',
	'"', 'T', 'h', 'e',  'S', 'o', 'u', 'l', '"', 0,
	'N', 'o', ',', ' ', 'N', 'o', ',', ' ', 'N', 'o', 't', ' ',
	'R', 'o', 'g', 'o', 'v', '!', 0
};

#define	UNKNOWN_STRING	"Don't Panic!"

/*
 * Verify that strings are inserted at the expected offsets, and that
 * the returned value is equivalent to the original string.
 */
void
tcInsertReturnValues(void)
{
	int	result;
	const char **s;
	unsigned int expectedindex, hashindex;
	Elftc_String_Table *table;

	result = TET_UNRESOLVED;

	TP_ANNOUNCE("Insertion returns the expected offsets.");

	if ((table = elftc_string_table_create(0)) == NULL) {
		TP_UNRESOLVED("elftc_string_table_create() failed: %s",
		    strerror(errno));
		goto done;
	}

	expectedindex = 1;
	/* Insert test strings. */
	for (s = test_strings; *s != NULL; s++) {
		hashindex = elftc_string_table_insert(table, *s);
		if (hashindex != expectedindex) {
			TP_FAIL("incorrect hash index: expected %d, actual %d",
				expectedindex, hashindex);
			goto done;
		}

		expectedindex += strlen(*s) + 1;
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);
	tet_result(result);
}

/*
 * Verify that multiple insertions of the same string yield the same
 * return values and offsets.
 */

void
tcInsertDuplicate(void)
{
	const char **s;
	int	n, result;
	Elftc_String_Table *table;
	unsigned int hindex, *hashrecord;

	result = TET_UNRESOLVED;

	TP_ANNOUNCE("Multiple insertions return the same offset value.");

	hashrecord = NULL;

	if ((table = elftc_string_table_create(0)) == NULL) {
		TP_UNRESOLVED("elftc_string_table_create() failed: %s",
		    strerror(errno));
		goto done;
	}

	if ((hashrecord = malloc(nteststrings*sizeof(*hashrecord))) == NULL) {
		TP_UNRESOLVED("memory allocation failed.");
		goto done;
	}

	/* Insert test strings. */
	for (n = 0, s = test_strings; *s != NULL; s++, n++)
		hashrecord[n] = elftc_string_table_insert(table, *s);

	/* Re-insert, and verify the returned pointers and offsets. */
	for (n = 0, s = test_strings; *s != NULL; s++, n++) {
		hindex = elftc_string_table_insert(table, *s);

		if (hindex != hashrecord[n]) {
			TP_FAIL("incorrect hash index: expected %d, actual %d",
				hashrecord[n], hindex);
			goto done;
		}
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);
	free(hashrecord);

	tet_result(result);
}

/*
 * Verify that the lookup() API returns the expected
 * values.
 */

void
tcLookupReturn(void)
{
	int	result;
	const char **s, *str;
	unsigned int expectedindex, hashindex;
	Elftc_String_Table *table;

	result = TET_UNRESOLVED;

	TP_ANNOUNCE("A lookup after an insertion returns the correct "
	    "string.");

	if ((table = elftc_string_table_create(0)) == NULL) {
		TP_UNRESOLVED("elftc_string_table_create() failed: %s",
		    strerror(errno));
		goto done;
	}

	/* Insert test strings. */
	for (s = test_strings; *s != NULL; s++)
		(void) elftc_string_table_insert(table, *s);

	expectedindex = 1;
	for (s = test_strings; *s != NULL; s++) {
		hashindex = elftc_string_table_lookup(table, *s);

		if (hashindex != expectedindex) {
			TP_FAIL("incorrect hash index: expected %d, actual %d",
				expectedindex, hashindex);
			goto done;
		}

		str = elftc_string_table_to_string(table, hashindex);

		if (str == NULL || strcmp(str, *s)) {
			TP_FAIL("Lookup of \"%s\" returned \"%s\".", *s,
			    str);
			goto done;
		}

		expectedindex += strlen(*s) + 1;
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);
	tet_result(result);
}

/*
 * Verify that multiple lookups return the same pointer
 * and string offset.
 */

void
tcLookupDuplicate(void)
{
	int	n, result;
	const char **s, *str1, *str2;
	unsigned int hindex1, hindex2, *hashrecord;
	Elftc_String_Table *table;

	result = TET_UNRESOLVED;

	TP_ANNOUNCE("Multiple invocations of lookup on a valid string "
	    "return the same value.");

	hashrecord = NULL;

	if ((table = elftc_string_table_create(0)) == NULL) {
		TP_UNRESOLVED("elftc_string_table_create() failed: %s",
		    strerror(errno));
		goto done;
	}

	if ((hashrecord = malloc(nteststrings*sizeof(*hashrecord))) == NULL) {
		TP_UNRESOLVED("memory allocation failed.");
		goto done;
	}

	/* Insert test strings. */
	for (n = 0, s = test_strings; *s != NULL; s++, n++)
		hashrecord[n] = elftc_string_table_insert(table, *s);

	for (n = 0, s = test_strings; *s != NULL; s++, n++) {
		hindex1 = elftc_string_table_lookup(table, *s);
		hindex2 = elftc_string_table_lookup(table, *s);

		if (hindex1 != hindex2 || hindex1 != hashrecord[n]) {
			TP_FAIL("incorrect hash index: expected %d, "
			    "actual %d & %d", hashrecord[n], hindex1,
			    hindex2);
			goto done;
		}

		str1 = elftc_string_table_to_string(table, hindex1);
		str2 = elftc_string_table_to_string(table, hindex2);
		if (str1 == NULL || str2 == NULL || str1 != str2 ||
		    strcmp(str1, *s)) {
			TP_FAIL("Lookup of \"%s\" returned \"%s\" & \"%s\".",
			    *s, str1, str2);
			goto done;
		}
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);
	free(hashrecord);

	tet_result(result);
}

/*
 * Verify that a deleted string cannot be subsequently looked up.
 */

void
tcDeletionCheck(void)
{
	const char **s;
	Elftc_String_Table *table;
	int hindex, n, result, status;

	result = TET_UNRESOLVED;

	TP_ANNOUNCE("Lookup after deletion should fail.");

	if ((table = elftc_string_table_create(0)) == NULL) {
		TP_UNRESOLVED("elftc_string_table_create() failed: %s",
		    strerror(errno));
		goto done;
	}

	/* Insert test strings. */
	for (n = 0, s = test_strings; *s != NULL; s++, n++)
		(void) elftc_string_table_insert(table, *s);

	/* Delete strings, and look them up. */
	for (n = 0, s = test_strings; *s != NULL; s++, n++) {
		status = elftc_string_table_remove(table, *s);
		if (status == 0) {
			TP_FAIL("Deletion of \"%s\" failed.", *s);
			goto done;
		}

		hindex = elftc_string_table_lookup(table, *s);
		if (hindex != 0) {
			TP_FAIL("Lookup of \"%s\" succeeded unexpectedly.");
			goto done;
		}
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);

	tet_result(result);

}

/*
 * Verify that a deleted string is re-inserted at the old index.
 */

void
tcDeletionInsertion(void)
{
	const char **s;
	unsigned int *hashrecord, hindex;
	Elftc_String_Table *table;
	int n, result, status;

	result = TET_UNRESOLVED;

	TP_ANNOUNCE("Re-insertion of a string after deletion should "
	    "return the prior offset.");

	hashrecord = NULL;

	if ((table = elftc_string_table_create(0)) == NULL) {
		TP_UNRESOLVED("elftc_string_table_create() failed: %s",
		    strerror(errno));
		goto done;
	}

	if ((hashrecord = malloc(nteststrings*sizeof(*hashrecord))) == NULL) {
		TP_UNRESOLVED("memory allocation failed.");
		goto done;
	}

	/* Insert test strings. */
	for (n = 0, s = test_strings; *s != NULL; s++, n++)
		hashrecord[n] = elftc_string_table_insert(table, *s);

	/* Delete strings ... */
	for (n = 0, s = test_strings; *s != NULL; s++, n++) {
		status = elftc_string_table_remove(table, *s);
		if (status == 0) {
			TP_UNRESOLVED("Deletion of \"%s\" failed.", *s);
			goto done;
		}
	}

	/* and re-insert them, and check. */
	for (n = 0, s = test_strings; *s != NULL; s++, n++) {
		hindex = elftc_string_table_insert(table, *s);

		if (hindex != hashrecord[n]) {
			TP_FAIL("Re-insertion at a different offset: "
			    "old %d, new %p", hashrecord[n], hindex);
			goto done;
		}
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);
	if (hashrecord)
		free(hashrecord);

	tet_result(result);

}

/*
 * Verify that the 2nd deletion of the string fails.
 */

void
tcDoubleDeletion(void)
{
	const char **s;
	int n, result, status;
	Elftc_String_Table *table;

	result = TET_UNRESOLVED;

	TP_ANNOUNCE("Double deletion of a string should fail.");

	if ((table = elftc_string_table_create(0)) == NULL) {
		TP_UNRESOLVED("elftc_string_table_create() failed: %s",
		    strerror(errno));
		goto done;
	}

	/* Insert test strings. */
	for (n = 0, s = test_strings; *s != NULL; s++, n++)
		(void) elftc_string_table_insert(table, *s);

	/* Delete strings twice. */
	for (n = 0, s = test_strings; *s != NULL; s++, n++) {
		status = elftc_string_table_remove(table, *s);
		if (status == 0) {
			TP_FAIL("First deletion of \"%s\" failed.", *s);
			goto done;
		}
		status = elftc_string_table_remove(table, *s);
		if (status != 0) {
			TP_FAIL("Second deletion of \"%s\" succeeded.", *s);
			goto done;
		}
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);

	tet_result(result);

}

/*
 * Verify that deletion of an unknown string fails.
 */

void
tcUnknownDeletion(void)
{
	const char **s;
	int n, result, status;
	Elftc_String_Table *table;

	result = TET_UNRESOLVED;

	TP_ANNOUNCE("Deletion of an unknown string should fail.");

	if ((table = elftc_string_table_create(0)) == NULL) {
		TP_UNRESOLVED("elftc_string_table_create() failed: %s",
		    strerror(errno));
		goto done;
	}

	/* Insert test strings. */
	for (n = 0, s = test_strings; *s != NULL; s++, n++)
		(void) elftc_string_table_insert(table, *s);

	status = elftc_string_table_remove(table, UNKNOWN_STRING);
	if (status != 0) {
		TP_FAIL("Deletion of an unknown string succeeded.");
		goto done;
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);

	tet_result(result);

}

/*
 * Ensure that string indices remain constant after the underlying
 * string pool is resized/moved.
 */

#define	TC_STRING_SIZE	64
#define	TC_INSERT_SIZE	20

void
tcIndicesAfterRebase(void)
{
	int j, n, result;
	const char *str1, *str2;
	char buf[TC_STRING_SIZE];
	Elftc_String_Table *table;
	unsigned int offset1, offset2, expectedoffset;

	n = 0;
	result = TET_UNRESOLVED;

	TP_ANNOUNCE("Indices are consistent after a pool resize.");

	if ((table = elftc_string_table_create(0)) == NULL) {
		TP_UNRESOLVED("elftc_string_table_create() failed: %s",
		    strerror(errno));
		goto done;
	}

	/* Insert test strings. */
#define	TC_BUILD_STRING(buf, n) do {				\
		(void) snprintf(buf, sizeof(buf), "%-*.*d",	\
		    TC_INSERT_SIZE - 1, TC_INSERT_SIZE - 1, n);	\
	} while (0)

	TC_BUILD_STRING(buf, n);

	offset1 = elftc_string_table_insert(table, buf);
	str1 = elftc_string_table_to_string(table, offset1);

	if (offset1 == 0 || str1 == NULL) {
		TP_UNRESOLVED("Initialization failed.");
		goto done;
	}

	n++;

	/*
	 * Insert unique strings till we detect a move of the initial
	 * string.
	 */
	do {

		TC_BUILD_STRING(buf, n);

		if ((offset2 = elftc_string_table_insert(table, buf)) == 0) {
			TP_UNRESOLVED("String insertion failed at %d", n);
			goto done;
		}

		if ((str2 = elftc_string_table_to_string(table, offset1)) ==
		    NULL) {
			TP_UNRESOLVED("String looked failed at %d", n);
			goto done;
		}

		n++;
	} while (str2 == str1);

	/*
	 * Verify the offset for each string inserted so far.
	 */
	expectedoffset = 1;
	for (j = 0; j < n; j++) {
		TC_BUILD_STRING(buf, j);
		offset1 = elftc_string_table_lookup(table, buf);
		if (offset1 != expectedoffset) {
			TP_FAIL("Offset mismatch: string #%d, "
			    "expected %d, actual %d", j, expectedoffset,
			    offset1);
			goto done;
		}
		expectedoffset += TC_INSERT_SIZE;
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);

	tet_result(result);
}

void
tcEmptyImage(void)
{
	int result;
	size_t tblsz;
	const char *image;
	Elftc_String_Table *table;

	TP_ANNOUNCE("Check the image for an empty table.");

	if ((table = elftc_string_table_create(0)) == NULL) {
		TP_UNRESOLVED("elftc_string_table_create() failed: %s",
		    strerror(errno));
		goto done;
	}

	tblsz = 0;
	image = elftc_string_table_image(table, &tblsz);
	if (image == NULL) {
		TP_FAIL("Null image returned.");
		goto done;
	}

	if (*image != 0 || tblsz != 1) {
		TP_FAIL("Incorrect image parameters: [0]=%d, sz=%d",
		    *image, tblsz);
		goto done;
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);

	tet_result(result);
}

void
tcImageDeleted(void)
{
	const char **s;
	int n, result;
	size_t tblsz;
	const char *image;
	Elftc_String_Table *table;

	TP_ANNOUNCE("Check the image for an empty table.");

	if ((table = elftc_string_table_create(0)) == NULL) {
		TP_UNRESOLVED("elftc_string_table_create() failed: %s",
		    strerror(errno));
		goto done;
	}

	/* Insert, then delete a set of strings. */
	for (n = 0, s = test_strings; *s != NULL; s++, n++)
		if (elftc_string_table_insert(table, *s) == 0) {
			TP_UNRESOLVED("String insertion of \"%s\" failed.",
			    *s);
			goto done;
		}

	for (n = 0, s = test_strings; *s != NULL; s++, n++)
		if (elftc_string_table_remove(table, *s) == 0) {
			TP_UNRESOLVED("String deletion of \"%s\" failed.",
			    *s);
			goto done;
		}

	/* Check for an empty table. */
	tblsz = 0;
	image = elftc_string_table_image(table, &tblsz);
	if (image == NULL) {
		TP_FAIL("Null image returned.");
		goto done;
	}

	if (*image != 0 || tblsz != 1) {
		TP_FAIL("Incorrect image parameters: [0]=%d, sz=%d",
		    *image, tblsz);
		goto done;
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);

	tet_result(result);
}

static int
validate_string_table(int nstrings, const char *image, size_t imagesz,
    const char **strings)
{
	int n, result, *seen;
	const char *s, *end;

	if (*image != '\0')
		return (0);

	if (nstrings == 0)
		return (1);

	if ((seen = calloc(nstrings, sizeof(int))) == NULL)
		return (0);

	/*
	 * Each string in the image should be in strings[],
	 * and vice-versa.
	 */
	s = image + 1;
	end = image + imagesz;
	while (s < end) {
		/* Look for this string in the strings[] array. */
		for (n = 0; n < nstrings; n++)
			if (strcmp(s, strings[n]) == 0) {
				seen[n] = 1;
				break;
			}
		if (n == nstrings) /* Not in the strings[] array. */
			goto fail;

		s += strlen(s) + 1;
	}

	/* Verify all strings in the array were seen. */
	for (n = 0; n < nstrings; n++)
		if (seen[n] == 0)
			goto fail;

	free(seen);

	return (1);

fail:
	free(seen);
	return (0);
}

void
tcImageInsertOnly(void)
{
	int result;
	const char **s;
	const char *image;
	Elftc_String_Table *table;
	size_t expectedsize, imagesz;

	TP_ANNOUNCE("Insertion returns the expected image.");

	if ((table = elftc_string_table_create(0)) == NULL) {
		TP_UNRESOLVED("elftc_string_table_create() failed: %s",
		    strerror(errno));
		goto done;
	}

	result = TET_PASS;

	expectedsize = 1;
	for (s = test_strings; *s != NULL; s++) {
		expectedsize += strlen(*s) + 1;
		if(elftc_string_table_insert(table, *s) == 0) {
			TP_UNRESOLVED("String insertion failed for \"%s\".",
			    *s);
			goto done;
		}
	}

	imagesz = 0;
	image = elftc_string_table_image(table, &imagesz);

	if (image == NULL || imagesz != expectedsize) {
		TP_FAIL("Incorrect image parameters: [0]=%d, sz=%d != %d",
		    *image, imagesz, expectedsize);
		goto done;
	}

	if (!validate_string_table(nteststrings - 1, image, imagesz,
		test_strings)) {
		TP_FAIL("Image mismatch.");
		goto done;
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);
	tet_result(result);
}

void
tcImagePartiallyDeleted(void)
{
	int n, nstr, result;
	Elftc_String_Table *table;
	size_t expectedsize, imagesz;
	const char *image, **s, **savedstr;

	TP_ANNOUNCE("Insertion+deletion returns the expected image.");

	savedstr = NULL;

	if ((table = elftc_string_table_create(0)) == NULL) {
		TP_UNRESOLVED("elftc_string_table_create() failed: %s",
		    strerror(errno));
		goto done;
	}

	expectedsize = 1;
	for (nstr = 0, s = test_strings; *s != NULL; s++, nstr++) {
		expectedsize += strlen(*s) + 1;
		if(elftc_string_table_insert(table, *s) == 0) {
			TP_UNRESOLVED("String insertion failed for \"%s\".",
			    *s);
			goto done;
		}
	}

	if ((savedstr = malloc(sizeof(*savedstr) * nstr)) == NULL) {
		TP_UNRESOLVED("Memory allocation failed.");
		goto done;
	}

	for (nstr = n = 0, s = test_strings; *s != NULL; s++, n++) {
		if ((n & 1) == 0) {
			savedstr[nstr++] = *s;
			continue;
		}

		expectedsize -= (strlen(*s) + 1);
		if (elftc_string_table_remove(table, *s) == 0) {
			TP_UNRESOLVED("String removal failed for \"%s\".",
			    *s);
			goto done;
		}
	}

	imagesz = 0;
	image = elftc_string_table_image(table, &imagesz);

	if (image == NULL || imagesz != expectedsize) {
		TP_FAIL("Incorrect image parameters: [0]=%d, sz=%d != %d",
		    *image, imagesz, expectedsize);
		goto done;
	}

	if (!validate_string_table(nstr, image, imagesz, savedstr)) {
		TP_FAIL("Image mismatch.");
		goto done;
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);
	free(savedstr);
	tet_result(result);
}

/*
 * Verify that initialization from a ELF string table works.
 */

void
tcFromSection(void)
{
	Elf *e;
	Elf_Data *d;
	Elf_Scn *scn;
	int fd, result;
	const char *image;
	Elf32_Ehdr *eh;
	Elf32_Shdr *shdr;
	Elftc_String_Table *table;
	size_t imagesz, scnindex;

	table = NULL;
	result = TET_UNRESOLVED;

	TP_ANNOUNCE("Loading a table form an ELF section works correctly.");

	fd = -1;
	e = NULL;
	scn = NULL;
	d = NULL;

	/*
	 * Create the ELF section.
	 */
	if ((fd = open("/dev/null", O_RDONLY)) < 0) {
		TP_UNRESOLVED("File open failed.");
		goto done;
	}

	if (elf_version(EV_CURRENT) == EV_NONE) {
		TP_UNRESOLVED("libelf initialization failed.");
		goto done;
	}

	if ((e = elf_begin(fd, ELF_C_WRITE, NULL)) == NULL) {
		TP_UNRESOLVED("Elf open failed: %s",
		    elf_errmsg(-1));
		goto done;
	}

	if ((eh = elf32_getehdr(e)) == NULL) {
		TP_UNRESOLVED("Elf open failed: %s",
		    elf_errmsg(-1));
		goto done;
	}

	eh->e_ident[EI_DATA] = ELFDATA2LSB;
	eh->e_machine = EM_386;
	eh->e_version = EV_CURRENT;

	if ((scn = elf_newscn(e)) == NULL) {
		TP_UNRESOLVED("Elf newscn failed: %s",
		    elf_errmsg(-1));
		goto done;
	}

	scnindex = elf_ndxscn(scn);

	if ((shdr = elf32_getshdr(scn)) == NULL) {
		TP_UNRESOLVED("Elf getshdr failed: %s",
		    elf_errmsg(-1));
		goto done;
	}

	shdr->sh_type = SHT_STRTAB;

	if ((d = elf_newdata(scn)) == NULL) {
		TP_UNRESOLVED("Elf newdata failed: %s",
		    elf_errmsg(-1));
		goto done;
	}

	d->d_buf = test_image;
	d->d_size = sizeof(test_image);

	if (elf_update(e, ELF_C_NULL) < 0) {
		TP_UNRESOLVED("elf_update failed: %s",
		    elf_errmsg(-1));
		goto done;
	}

	if ((scn = elf_getscn(e, scnindex)) == NULL) {
		TP_UNRESOLVED("Elf getscn failed: %s",
		    elf_errmsg(-1));
		goto done;
	}

	/* Create a string table from the contents. */
	if ((table = elftc_string_table_from_section(scn, 0)) == NULL) {
		TP_FAIL("from_section call failed.");
		goto done;
	}

	/* Retrieve the image. */
	if ((image = elftc_string_table_image(table, &imagesz)) == NULL) {
		TP_FAIL("from_section call failed.");
		goto done;
	}

	/* Check the retrieved image against the original. */
	if (imagesz != sizeof(test_image) ||
	    memcmp(image, test_image, imagesz) != 0) {
		TP_FAIL("image compare failed.");
		goto done;
	}

	result = TET_PASS;

done:
	if (table)
		(void) elftc_string_table_destroy(table);
	if (e)
		elf_end(e);
	if (fd != -1)
		(void) close(fd);
	tet_result(result);
}