~ubuntu-branches/ubuntu/maverick/nss-pam-ldapd/maverick

« back to all changes in this revision

Viewing changes to tests/test_dict.c

  • Committer: Bazaar Package Importer
  • Author(s): Arthur de Jong
  • Date: 2009-09-01 17:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090901170000-54hqcpmbxk32rzer
Tags: 0.7.0
* rename software to nss-pam-ldapd to indicate that PAM module is now a
  standard part of the software
* split into the binary packages libnss-ldapd, libpam-ldapd and nslcd
  (libpam-ldapd packaging used a patch for libpam-ldap by Steve Langasek)
  (closes: #535505)
* the configuration file name has been changed to /etc/nslcd.conf (package
  upgrade should migrate the configuration)
* updated Galician debconf translation by Marce Villarino (closes: #537424)
* patch by Petter Reinholdtsen to fix init script to start before autofs
  (closes: #544093)
* the default values for bind_timelimit and reconnect_maxsleeptime were
  lowered from 30 to 10 seconds (closes: #532874)
* upgrade to standards-version 3.8.3 (no changes needed)
* password hashes are no longer returned to non-root users (based on a patch
  by Alexander V. Chernikov)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   test_dict.c - simple test for the dict module
 
3
   This file is part of the nss-pam-ldapd library.
 
4
 
 
5
   Copyright (C) 2007, 2008 Arthur de Jong
 
6
 
 
7
   This library is free software; you can redistribute it and/or
 
8
   modify it under the terms of the GNU Lesser General Public
 
9
   License as published by the Free Software Foundation; either
 
10
   version 2.1 of the License, or (at your option) any later version.
 
11
 
 
12
   This library is distributed in the hope that it will be useful,
 
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
   Lesser General Public License for more details.
 
16
 
 
17
   You should have received a copy of the GNU Lesser General Public
 
18
   License along with this library; if not, write to the Free Software
 
19
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
20
   02110-1301 USA
 
21
*/
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <stdio.h>
 
26
#include <string.h>
 
27
#include <assert.h>
 
28
#include <stdlib.h>
 
29
 
 
30
#include "common/dict.h"
 
31
#include "compat/attrs.h"
 
32
 
 
33
/* Simple test that adds a few key/value pairs to the dict and the does
 
34
   most operations. */
 
35
static void test_simple(void)
 
36
{
 
37
  DICT *dict;
 
38
  const char *key;
 
39
  void *val;
 
40
  static char *value1="value1";
 
41
  static char *value2="value2";
 
42
  static char *replace2="replace2";
 
43
  /* initialize */
 
44
  dict=dict_new();
 
45
  /* store some entries */
 
46
  dict_put(dict,"key1",value1);
 
47
  dict_put(dict,"key2",value2);
 
48
  dict_put(dict,"key3",dict);
 
49
  dict_put(dict,"KEY2",replace2);
 
50
  /* check dictionary contents */
 
51
  val=dict_get(dict,"KeY1");
 
52
  assert(val==value1);
 
53
  val=dict_get(dict,"kEy2");
 
54
  assert(val==replace2);
 
55
  val=dict_get(dict,"KeY3");
 
56
  assert(val==dict);
 
57
  val=dict_get(dict,"key4");
 
58
  assert(val==NULL);
 
59
  /* remove a key */
 
60
  dict_put(dict,"kEy3",NULL);
 
61
  val=dict_get(dict,"keY3");
 
62
  assert(val==NULL);
 
63
  /* loop over dictionary contents */
 
64
  dict_loop_first(dict);
 
65
  while (dict_loop_next(dict,&key,&val)!=NULL)
 
66
  {
 
67
    assert(((val==value1)||(val==replace2)));
 
68
  }
 
69
  /* free dictionary */
 
70
  dict_free(dict);
 
71
}
 
72
 
 
73
/* Test to insert a large number of elements in the dict. */
 
74
static void test_lotsofelements(void)
 
75
{
 
76
  DICT *dict;
 
77
  char buf[80];
 
78
  int i,r;
 
79
  const char *key;
 
80
  void *val;
 
81
  /* initialize */
 
82
  dict=dict_new();
 
83
  /* insert a number of entries */
 
84
  for (i=0;i<1024;i++)
 
85
  {
 
86
    r=1+(int)(10000.0*(rand()/(RAND_MAX+1.0)));
 
87
    sprintf(buf,"test%04d",r);
 
88
    dict_put(dict,buf,&buf);
 
89
  }
 
90
  /* remove a number of entries */
 
91
  for (i=0;i<100;i++)
 
92
  {
 
93
    r=1+(int)(10000.0*(rand()/(RAND_MAX+1.0)));
 
94
    sprintf(buf,"test%04d",r);
 
95
    dict_put(dict,buf,NULL);
 
96
  }
 
97
  /* add some more entries */
 
98
  for (i=0;i<1024;i++)
 
99
  {
 
100
    r=1+(int)(10000.0*(rand()/(RAND_MAX+1.0)));
 
101
    sprintf(buf,"test%04d",r);
 
102
    dict_put(dict,buf,&buf);
 
103
  }
 
104
  /* loop over dictionary contents */
 
105
  dict_loop_first(dict);
 
106
  while (dict_loop_next(dict,&key,&val)!=NULL)
 
107
  {
 
108
    assert(val==buf);
 
109
  }
 
110
  /* free dictionary */
 
111
  dict_free(dict);
 
112
}
 
113
 
 
114
/* Test to insert a large number of elements in the dict. */
 
115
static void test_readelements(const char *fname)
 
116
{
 
117
  DICT *dict;
 
118
  char buf[80];
 
119
  FILE *fp;
 
120
  const char *key;
 
121
  void *val;
 
122
  /* initialize */
 
123
  dict=dict_new();
 
124
  /* read file and insert all entries */
 
125
  fp=fopen(fname,"r");
 
126
  assert(fp!=NULL);
 
127
  while (fgets(buf,sizeof(buf),fp)!=NULL)
 
128
  {
 
129
    /* strip newline */
 
130
    buf[strlen(buf)-1]='\0';
 
131
    dict_put(dict,buf,&buf);
 
132
  }
 
133
  fclose(fp);
 
134
  /* loop over dictionary contents */
 
135
  dict_loop_first(dict);
 
136
  while (dict_loop_next(dict,&key,&val)!=NULL)
 
137
  {
 
138
    assert(val==buf);
 
139
  }
 
140
  /* free dictionary */
 
141
  dict_free(dict);
 
142
}
 
143
 
 
144
static void test_countelements(int num)
 
145
{
 
146
  DICT *dict;
 
147
  char buf[80];
 
148
  int i,j,r;
 
149
  const char *key;
 
150
  void *val;
 
151
  /* initialize */
 
152
  dict=dict_new();
 
153
  /* insert a number of entries */
 
154
  for (i=0;i<num;i++)
 
155
  {
 
156
    r=1+(int)(10000.0*(rand()/(RAND_MAX+1.0)));
 
157
    sprintf(buf,"%04dx%04d",i,r);
 
158
    dict_put(dict,buf,&buf);
 
159
  }
 
160
  /* loop over dictionary contents */
 
161
  dict_loop_first(dict);
 
162
  i=0;
 
163
  while (dict_loop_next(dict,&key,&val)!=NULL)
 
164
  {
 
165
    assert(val==buf);
 
166
    i++;
 
167
  }
 
168
  /* we should have num elements */
 
169
  assert(i==num);
 
170
  /* free dictionary */
 
171
  dict_free(dict);
 
172
}
 
173
 
 
174
/* the main program... */
 
175
int main(int UNUSED(argc),char UNUSED(*argv[]))
 
176
{
 
177
  char *srcdir;
 
178
  char fname[100];
 
179
  /* build the name of the file */
 
180
  srcdir=getenv("srcdir");
 
181
  if (srcdir==NULL)
 
182
    strcpy(fname,"usernames.txt");
 
183
  else
 
184
    snprintf(fname,sizeof(fname),"%s/usernames.txt",srcdir);
 
185
  fname[sizeof(fname)-1]='\0';
 
186
  /* run the tests */
 
187
  test_simple();
 
188
  test_lotsofelements();
 
189
  test_readelements(fname);
 
190
  test_countelements(0);
 
191
  test_countelements(1);
 
192
  test_countelements(2);
 
193
  test_countelements(3);
 
194
  test_countelements(4);
 
195
  test_countelements(10);
 
196
  test_countelements(20);
 
197
  return 0;
 
198
}