~ubuntu-branches/debian/sid/subversion/sid

« back to all changes in this revision

Viewing changes to subversion/bindings/cxxhl/tests/test_aprwrap.cpp

  • Committer: Package Import Robot
  • Author(s): James McCoy
  • Date: 2015-08-07 21:32:47 UTC
  • mfrom: (0.2.15) (4.1.7 experimental)
  • Revision ID: package-import@ubuntu.com-20150807213247-ozyewtmgsr6tkewl
Tags: 1.9.0-1
* Upload to unstable
* New upstream release.
  + Security fixes
    - CVE-2015-3184: Mixed anonymous/authenticated path-based authz with
      httpd 2.4
    - CVE-2015-3187: svn_repos_trace_node_locations() reveals paths hidden
      by authz
* Add >= 2.7 requirement for python-all-dev Build-Depends, needed to run
  tests.
* Remove Build-Conflicts against ruby-test-unit.  (Closes: #791844)
* Remove patches/apache_module_dependency in favor of expressing the
  dependencies in authz_svn.load/dav_svn.load.
* Build-Depend on apache2-dev (>= 2.4.16) to ensure ap_some_authn_required()
  is available when building mod_authz_svn and Depend on apache2-bin (>=
  2.4.16) for runtime support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ====================================================================
 
3
 *    Licensed to the Apache Software Foundation (ASF) under one
 
4
 *    or more contributor license agreements.  See the NOTICE file
 
5
 *    distributed with this work for additional information
 
6
 *    regarding copyright ownership.  The ASF licenses this file
 
7
 *    to you under the Apache License, Version 2.0 (the
 
8
 *    "License"); you may not use this file except in compliance
 
9
 *    with the License.  You may obtain a copy of the License at
 
10
 *
 
11
 *      http://www.apache.org/licenses/LICENSE-2.0
 
12
 *
 
13
 *    Unless required by applicable law or agreed to in writing,
 
14
 *    software distributed under the License is distributed on an
 
15
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
16
 *    KIND, either express or implied.  See the License for the
 
17
 *    specific language governing permissions and limitations
 
18
 *    under the License.
 
19
 * ====================================================================
 
20
 */
 
21
 
 
22
#include <algorithm>
 
23
#include <stdexcept>
 
24
 
 
25
#include "../src/aprwrap.hpp"
 
26
 
 
27
#include <gmock/gmock.h>
 
28
 
 
29
//
 
30
// Pools
 
31
//
 
32
 
 
33
TEST(Pools, InitializeGlobalPool)
 
34
{
 
35
  APR::Pool pool;
 
36
  EXPECT_THAT(pool.get(), testing::NotNull());
 
37
  EXPECT_THAT(apr_pool_parent_get(pool.get()), testing::NotNull());
 
38
}
 
39
 
 
40
TEST(Pools, CreateSubpool)
 
41
{
 
42
  APR::Pool pool;
 
43
  APR::Pool subpool(&pool);
 
44
  EXPECT_EQ(pool.get(), apr_pool_parent_get(subpool.get()));
 
45
}
 
46
 
 
47
TEST(Pools, TypedAllocate)
 
48
{
 
49
  APR::Pool pool;
 
50
  const unsigned char* buffer = pool.alloc<unsigned char>(1);
 
51
  EXPECT_THAT(buffer, testing::NotNull());
 
52
}
 
53
 
 
54
// N.B.: This test may pass randomly even if zero-filled allocation
 
55
// does not work correctly, since we cannot make assumptions about the
 
56
// values of uninitialized memory.
 
57
TEST(Pools, TypedAllocateZerofill)
 
58
{
 
59
  APR::Pool pool;
 
60
  static const std::size_t size = 32757;
 
61
  const unsigned char* buffer = pool.allocz<unsigned char>(size);
 
62
  ASSERT_THAT(buffer, testing::NotNull());
 
63
  EXPECT_EQ(size, std::count(buffer, buffer + size, 0));
 
64
}
 
65
 
 
66
//
 
67
// Array helper functions
 
68
//
 
69
 
 
70
namespace {
 
71
// Create a randomly-ordered array of constant strings.
 
72
apr_array_header_t* fill_array(APR::Pool& pool)
 
73
{
 
74
  apr_array_header_t* a = apr_array_make(pool.get(), 0, sizeof(const char*));
 
75
  APR_ARRAY_PUSH(a, const char*) = "primus";
 
76
  APR_ARRAY_PUSH(a, const char*) = "secundus";
 
77
  APR_ARRAY_PUSH(a, const char*) = "tertius";
 
78
  APR_ARRAY_PUSH(a, const char*) = "quartus";
 
79
  APR_ARRAY_PUSH(a, const char*) = "quintus";
 
80
  APR_ARRAY_PUSH(a, const char*) = "sextus";
 
81
  APR_ARRAY_PUSH(a, const char*) = "septimus";
 
82
  std::random_shuffle(&APR_ARRAY_IDX(a, 0, const char*),
 
83
                      &APR_ARRAY_IDX(a, a->nelts, const char*));
 
84
  return a;
 
85
}
 
86
} // anonymous namespace
 
87
 
 
88
//
 
89
// Arrays
 
90
//
 
91
 
 
92
TEST(Arrays, CreateArray)
 
93
{
 
94
  typedef APR::Array<unsigned char> Array;
 
95
 
 
96
  APR::Pool pool;
 
97
  Array array(pool);
 
98
 
 
99
  EXPECT_THAT(array.array(), testing::NotNull());
 
100
  EXPECT_EQ(0, array.size());
 
101
  EXPECT_EQ(sizeof(unsigned char), sizeof(Array::value_type));
 
102
  EXPECT_EQ(sizeof(Array::value_type), array.array()->elt_size);
 
103
}
 
104
 
 
105
TEST(Arrays, WrapArray)
 
106
{
 
107
  typedef APR::Array<unsigned char> Array;
 
108
 
 
109
  APR::Pool pool;
 
110
  apr_array_header_t* apr_array =
 
111
    apr_array_make(pool.get(), 0, sizeof(Array::value_type));
 
112
  ASSERT_THAT(apr_array, testing::NotNull());
 
113
 
 
114
  Array array(apr_array);
 
115
  EXPECT_EQ(apr_array, array.array());
 
116
  EXPECT_EQ(0, array.size());
 
117
}
 
118
 
 
119
TEST(Arrays, RewrapTypeMismatch)
 
120
{
 
121
  typedef APR::Array<unsigned char> ByteArray;
 
122
  typedef APR::Array<int> IntArray;
 
123
 
 
124
  APR::Pool pool;
 
125
  EXPECT_THROW(ByteArray array(IntArray(pool).array()),
 
126
               std::invalid_argument);
 
127
}
 
128
 
 
129
TEST(Arrays, OutOfBounds)
 
130
{
 
131
  typedef APR::Array<unsigned char> Array;
 
132
 
 
133
  APR::Pool pool;
 
134
  Array array(pool);
 
135
 
 
136
  EXPECT_THROW(array.at(-1), std::out_of_range);
 
137
  EXPECT_THROW(array.at(array.size()), std::out_of_range);
 
138
}
 
139
 
 
140
TEST(Arrays, Indexing)
 
141
{
 
142
  typedef APR::Array<const char*> Array;
 
143
 
 
144
  APR::Pool pool;
 
145
  Array array(fill_array(pool));
 
146
 
 
147
  EXPECT_STREQ(array[0], APR_ARRAY_IDX(array.array(), 0, Array::value_type));
 
148
  EXPECT_STREQ(array[array.size() - 1], APR_ARRAY_IDX(array.array(),
 
149
                                                      array.array()->nelts - 1,
 
150
                                                      Array::value_type));
 
151
}
 
152
 
 
153
TEST(Arrays, CheckedIndexing)
 
154
{
 
155
  typedef APR::Array<const char*> Array;
 
156
 
 
157
  APR::Pool pool;
 
158
  Array array(fill_array(pool));
 
159
 
 
160
  EXPECT_STREQ(array.at(0), APR_ARRAY_IDX(array.array(), 0, Array::value_type));
 
161
  EXPECT_STREQ(array.at(array.size() - 1),
 
162
               APR_ARRAY_IDX(array.array(), array.array()->nelts - 1,
 
163
                             Array::value_type));
 
164
}
 
165
 
 
166
TEST(Arrays, Iteration)
 
167
{
 
168
  typedef APR::Array<const char*> Array;
 
169
 
 
170
  APR::Pool pool;
 
171
  Array array(fill_array(pool));
 
172
 
 
173
  struct Iteration : public Array::Iteration
 
174
  {
 
175
    Iteration(apr_array_header_t* raw_array)
 
176
      : m_index(0), m_raw_array(raw_array)
 
177
      {}
 
178
 
 
179
    bool operator()(Array::value_type& value)
 
180
      {
 
181
        EXPECT_STREQ(value, APR_ARRAY_IDX(m_raw_array, m_index,
 
182
                                          Array::value_type));
 
183
        ++m_index;
 
184
        return true;
 
185
      }
 
186
 
 
187
  private:
 
188
    Array::size_type m_index;
 
189
    apr_array_header_t* m_raw_array;
 
190
  } callback(array.array());
 
191
 
 
192
  array.iterate(callback);
 
193
}
 
194
 
 
195
TEST(Arrays, ConstIteration)
 
196
{
 
197
  typedef APR::Array<const char*> Array;
 
198
 
 
199
  APR::Pool pool;
 
200
  Array array(fill_array(pool));
 
201
 
 
202
  struct Iteration : public Array::ConstIteration
 
203
  {
 
204
    Iteration(const  apr_array_header_t* raw_array)
 
205
      : m_index(0), m_raw_array(raw_array)
 
206
      {}
 
207
 
 
208
    bool operator()(const Array::value_type& value)
 
209
      {
 
210
        EXPECT_STREQ(value, APR_ARRAY_IDX(m_raw_array, m_index,
 
211
                                          Array::value_type));
 
212
        ++m_index;
 
213
        return true;
 
214
      }
 
215
 
 
216
  private:
 
217
    Array::size_type m_index;
 
218
    const apr_array_header_t* m_raw_array;
 
219
  } callback(array.array());
 
220
 
 
221
  array.iterate(callback);
 
222
}
 
223
 
 
224
TEST(Arrays, Push)
 
225
{
 
226
  typedef APR::Array<const char*> Array;
 
227
 
 
228
  APR::Pool pool;
 
229
  Array array(fill_array(pool));
 
230
 
 
231
  const Array::size_type point = array.size();
 
232
  const Array::value_type first = array[0];
 
233
  const Array::value_type last = array[point - 1];
 
234
 
 
235
  array.push("octavius");
 
236
  array.push("nonus");
 
237
  array.push("decimus");
 
238
 
 
239
  EXPECT_EQ(point + 3, array.size());
 
240
  EXPECT_STREQ(first, array[0]);
 
241
  EXPECT_STREQ(last, array[point - 1]);
 
242
  EXPECT_STREQ("octavius", array[point]);
 
243
  EXPECT_STREQ("decimus", array[array.size() - 1]);
 
244
}
 
245
 
 
246
TEST(Arrays, Pop)
 
247
{
 
248
  typedef APR::Array<const char*> Array;
 
249
 
 
250
  APR::Pool pool;
 
251
  Array array(fill_array(pool));
 
252
 
 
253
  for (Array::size_type i = 0, z = array.size(); i <= z; ++i)
 
254
    {
 
255
      const char** last = (!array.array()->nelts ? NULL
 
256
                           : &APR_ARRAY_IDX(array.array(),
 
257
                                            array.array()->nelts - 1,
 
258
                                            const char*));
 
259
      EXPECT_EQ(last, array.pop());
 
260
    }
 
261
}
 
262
 
 
263
//
 
264
// ConstArrays
 
265
//
 
266
 
 
267
TEST(ConstArrays, WrapArray)
 
268
{
 
269
  typedef APR::ConstArray<unsigned char> Array;
 
270
 
 
271
  APR::Pool pool;
 
272
  const apr_array_header_t* apr_array =
 
273
    apr_array_make(pool.get(), 0, sizeof(Array::value_type));
 
274
  ASSERT_THAT(apr_array, testing::NotNull());
 
275
 
 
276
  Array array(apr_array);
 
277
  EXPECT_EQ(apr_array, array.array());
 
278
  EXPECT_EQ(0, array.size());
 
279
}
 
280
 
 
281
TEST(ConstArrays, RewrapTypeMismatch)
 
282
{
 
283
  typedef APR::ConstArray<unsigned char> ByteArray;
 
284
  typedef APR::Array<int> IntArray;
 
285
 
 
286
  APR::Pool pool;
 
287
  EXPECT_THROW(ByteArray array(IntArray(pool).array()),
 
288
               std::invalid_argument);
 
289
}
 
290
 
 
291
TEST(ConstArrays, OutOfBounds)
 
292
{
 
293
  typedef APR::ConstArray<unsigned char> Array;
 
294
 
 
295
  APR::Pool pool;
 
296
  Array array = Array(APR::Array<Array::value_type>(pool));
 
297
 
 
298
  EXPECT_THROW(array.at(-1), std::out_of_range);
 
299
  EXPECT_THROW(array.at(array.size()), std::out_of_range);
 
300
}
 
301
 
 
302
TEST(ConstArrays, Indexing)
 
303
{
 
304
  typedef APR::ConstArray<const char*> Array;
 
305
 
 
306
  APR::Pool pool;
 
307
  Array array(fill_array(pool));
 
308
 
 
309
  EXPECT_STREQ(array[0], APR_ARRAY_IDX(array.array(), 0, Array::value_type));
 
310
  EXPECT_STREQ(array[array.size() - 1], APR_ARRAY_IDX(array.array(),
 
311
                                                      array.array()->nelts - 1,
 
312
                                                      Array::value_type));
 
313
}
 
314
 
 
315
TEST(ConstArrays, CheckedIndexing)
 
316
{
 
317
  typedef APR::ConstArray<const char*> Array;
 
318
 
 
319
  APR::Pool pool;
 
320
  Array array(fill_array(pool));
 
321
 
 
322
  EXPECT_STREQ(array.at(0), APR_ARRAY_IDX(array.array(), 0, Array::value_type));
 
323
  EXPECT_STREQ(array.at(array.size() - 1),
 
324
               APR_ARRAY_IDX(array.array(), array.array()->nelts - 1,
 
325
                             Array::value_type));
 
326
}
 
327
 
 
328
TEST(ConstArrays, Iteration)
 
329
{
 
330
  typedef APR::ConstArray<const char*> Array;
 
331
 
 
332
  APR::Pool pool;
 
333
  Array array(fill_array(pool));
 
334
 
 
335
  struct Iteration : public Array::Iteration
 
336
  {
 
337
    Iteration(const  apr_array_header_t* raw_array)
 
338
      : m_index(0), m_raw_array(raw_array)
 
339
      {}
 
340
 
 
341
    bool operator()(const Array::value_type& value)
 
342
      {
 
343
        EXPECT_STREQ(value, APR_ARRAY_IDX(m_raw_array, m_index,
 
344
                                          Array::value_type));
 
345
        ++m_index;
 
346
        return true;
 
347
      }
 
348
 
 
349
  private:
 
350
    Array::size_type m_index;
 
351
    const apr_array_header_t* m_raw_array;
 
352
  } callback(array.array());
 
353
 
 
354
  array.iterate(callback);
 
355
}
 
356
 
 
357
//
 
358
// Hash tables
 
359
//
 
360
 
 
361
TEST(Hashes, StringHash)
 
362
{
 
363
  typedef APR::Hash<char, const char> H;
 
364
 
 
365
  APR::Pool pool;
 
366
  H hash(pool);
 
367
  hash.set("aa", "a");
 
368
  hash.set("bbb", "b");
 
369
  hash.set("cccc", "c");
 
370
 
 
371
  EXPECT_EQ(3, hash.size());
 
372
  EXPECT_STREQ("a", hash.get("aa"));
 
373
  EXPECT_STREQ("b", hash.get("bbb"));
 
374
  EXPECT_STREQ("c", hash.get("cccc"));
 
375
}
 
376
 
 
377
TEST(Hashes, FixedStringHash)
 
378
{
 
379
  // The point of this test is to verify that the key-length parameter
 
380
  // of the template actually limits the length of the keys.
 
381
  typedef APR::Hash<char, const char, 2> H;
 
382
 
 
383
  APR::Pool pool;
 
384
  H hash(pool);
 
385
  hash.set("aa&qux", "a");
 
386
  hash.set("bb#foo", "b");
 
387
  hash.set("cc@bar", "c");
 
388
 
 
389
  EXPECT_EQ(3, hash.size());
 
390
  EXPECT_STREQ("a", hash.get("aa%foo"));
 
391
  EXPECT_STREQ("b", hash.get("bb*bar"));
 
392
  EXPECT_STREQ("c", hash.get("cc$qux"));
 
393
}
 
394
 
 
395
TEST(Hashes, Delete)
 
396
{
 
397
  typedef APR::Hash<char, const char> H;
 
398
 
 
399
  APR::Pool pool;
 
400
  H hash(pool);
 
401
  hash.set("aa", "a");
 
402
  hash.set("bbb", "b");
 
403
  hash.set("cccc", "c");
 
404
 
 
405
  hash.del("bbb");
 
406
 
 
407
  EXPECT_EQ(2, hash.size());
 
408
  EXPECT_STREQ("a", hash.get("aa"));
 
409
  EXPECT_STREQ("c", hash.get("cccc"));
 
410
}
 
411
 
 
412
TEST(Hashes, Iterate)
 
413
{
 
414
  typedef APR::Hash<char, const char> H;
 
415
 
 
416
  APR::Pool pool;
 
417
  H hash(pool);
 
418
  hash.set("aa", "a");
 
419
  hash.set("bbb", "b");
 
420
  hash.set("cccc", "c");
 
421
 
 
422
  struct C : public H::Iteration
 
423
  {
 
424
    H& m_hash;
 
425
    explicit C(H& hashref) : m_hash(hashref) {}
 
426
 
 
427
    bool operator()(const H::Key& key, H::value_type value)
 
428
      {
 
429
        EXPECT_STREQ(value, m_hash.get(key));
 
430
        return true;
 
431
      }
 
432
  } callback(hash);
 
433
 
 
434
  hash.iterate(callback, pool);
 
435
}