domaintree_unittest.cc 50.4 KB
Newer Older
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
// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <gtest/gtest.h>

#include <exceptions/exceptions.h>

#include <util/memory_segment_local.h>

#include <dns/name.h>
#include <dns/rrclass.h>
#include <dns/rrset.h>
#include <dns/rrtype.h>
#include <dns/rrttl.h>

#include <datasrc/memory/domaintree.h>

#include <dns/tests/unittest_util.h>

using namespace std;
using namespace isc;
using namespace isc::dns;
using isc::UnitTestUtil;
using namespace isc::datasrc::memory;

// XXX: some compilers cannot find class static constants used in
// EXPECT_xxx macros, for which we need an explicit empty definition.
const size_t Name::MAX_LABELS;

41
/* The initial structure of dtree
42
 *
43
 *             .
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 *             |
 *             b
 *           /   \
 *          a    d.e.f
 *              /  |   \
 *             c   |    g.h
 *                 |     |
 *                w.y    i
 *              /  |  \   \
 *             x   |   z   k
 *                 |   |
 *                 p   j
 *               /   \
 *              o     q
 */

namespace {
61

62
63
64
void deleteData(int* i) {
    delete i;
}
65

66
67
68
typedef DomainTree<int> TestDomainTree;
typedef DomainTreeNode<int> TestDomainTreeNode;
typedef DomainTreeNodeChain<int> TestDomainTreeNodeChain;
69

70
71
class TreeHolder {
public:
72
    TreeHolder(util::MemorySegment& mem_sgmt, TestDomainTree* tree) :
73
74
75
        mem_sgmt_(mem_sgmt), tree_(tree)
    {}
    ~TreeHolder() {
76
        TestDomainTree::destroy(mem_sgmt_, tree_, deleteData);
77
    }
78
    TestDomainTree* get() { return (tree_); }
79
80
private:
    util::MemorySegment& mem_sgmt_;
81
    TestDomainTree* tree_;
82
83
84
85
86
};

class DomainTreeTest : public::testing::Test {
protected:
    DomainTreeTest() :
87
88
        dtree_holder_(mem_sgmt_, TestDomainTree::create(mem_sgmt_)),
        dtree_expose_empty_node_holder_(mem_sgmt_,
89
                                         TestDomainTree::create(mem_sgmt_, true)),
90
91
92
        dtree(*dtree_holder_.get()),
        dtree_expose_empty_node(*dtree_expose_empty_node_holder_.get()),
        cdtnode(NULL)
93
94
95
96
97
98
    {
        const char* const domain_names[] = {
            "c", "b", "a", "x.d.e.f", "z.d.e.f", "g.h", "i.g.h", "o.w.y.d.e.f",
            "j.z.d.e.f", "p.w.y.d.e.f", "q.w.y.d.e.f", "k.g.h"};
        int name_count = sizeof(domain_names) / sizeof(domain_names[0]);
        for (int i = 0; i < name_count; ++i) {
99
            dtree.insert(mem_sgmt_, Name(domain_names[i]), &dtnode);
100
101
102
            // Check the node doesn't have any data initially.
            EXPECT_EQ(static_cast<int*>(NULL),
                      dtnode->setData(new int(i + 1)));
103

104
105
            dtree_expose_empty_node.insert(mem_sgmt_, Name(domain_names[i]),
                                            &dtnode);
106
            EXPECT_EQ(static_cast<int*>(NULL), dtnode->setData(new int(i + 1)));
107
108
109
110
        }
    }

    util::MemorySegmentLocal mem_sgmt_;
111
112
113
114
115
116
    TreeHolder dtree_holder_;
    TreeHolder dtree_expose_empty_node_holder_;
    TestDomainTree& dtree;
    TestDomainTree& dtree_expose_empty_node;
    TestDomainTreeNode* dtnode;
    const TestDomainTreeNode* cdtnode;
117
    uint8_t buf[LabelSequence::MAX_SERIALIZED_LENGTH];
118
119
120
};

TEST_F(DomainTreeTest, nodeCount) {
121
    EXPECT_EQ(15, dtree.getNodeCount());
122
123
124

    // Delete all nodes, then the count should be set to 0.  This also tests
    // the behavior of deleteAllNodes().
125
    dtree.deleteAllNodes(mem_sgmt_, deleteData);
126
    EXPECT_EQ(0, dtree.getNodeCount());
127
128
129
}

TEST_F(DomainTreeTest, setGetData) {
130
131
132
133
134
    // set new data to an existing node.  It should have some data.
    int* newdata = new int(11);
    int* olddata = dtnode->setData(newdata);
    EXPECT_NE(static_cast<int*>(NULL), olddata);
    deleteData(olddata);
135
    EXPECT_EQ(11, *(dtnode->getData()));
136
137
138
139
140

    // clear the node.  we should get the new data back we just passed.
    olddata = dtnode->setData(NULL);
    EXPECT_EQ(newdata, olddata);
    deleteData(olddata);
141
142
143
}

TEST_F(DomainTreeTest, insertNames) {
144
    EXPECT_EQ(TestDomainTree::ALREADYEXISTS, dtree.insert(mem_sgmt_,
145
                                                        Name("d.e.f"),
146
147
148
                                                        &dtnode));
    EXPECT_EQ(Name("d.e.f"), dtnode->getName());
    EXPECT_EQ(15, dtree.getNodeCount());
149
150

    // insert not exist node
151
152
153
154
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("0"),
                                                  &dtnode));
    EXPECT_EQ(Name("0"), dtnode->getName());
    EXPECT_EQ(16, dtree.getNodeCount());
155

156
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_,
157
                                                  Name("example.com"),
158
159
                                                  &dtnode));
    EXPECT_EQ(17, dtree.getNodeCount());
160
    // add data to it; also make sure it doesn't have data right now
161
162
    // (otherwise it would leak)
    EXPECT_EQ(static_cast<int*>(NULL), dtnode->setData(new int(12)));
163
164
165

    // return ALREADYEXISTS, since node "example.com" already has
    // been explicitly inserted
166
    EXPECT_EQ(TestDomainTree::ALREADYEXISTS, dtree.insert(mem_sgmt_,
167
                                                        Name("example.com"),
168
169
                                                        &dtnode));
    EXPECT_EQ(17, dtree.getNodeCount());
170
171

    // split the node "d.e.f"
172
173
174
175
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("k.e.f"),
                                                  &dtnode));
    EXPECT_EQ(Name("k"), dtnode->getName());
    EXPECT_EQ(19, dtree.getNodeCount());
176
177

    // split the node "g.h"
178
179
180
181
    EXPECT_EQ(TestDomainTree::ALREADYEXISTS, dtree.insert(mem_sgmt_, Name("h"),
                                                        &dtnode));
    EXPECT_EQ(Name("h"), dtnode->getName());
    EXPECT_EQ(20, dtree.getNodeCount());
182
183

    // add child domain
184
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_,
185
                                                  Name("m.p.w.y.d.e.f"),
186
187
188
189
                                                  &dtnode));
    EXPECT_EQ(Name("m"), dtnode->getName());
    EXPECT_EQ(21, dtree.getNodeCount());
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_,
190
                                                  Name("n.p.w.y.d.e.f"),
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
                                                  &dtnode));
    EXPECT_EQ(Name("n"), dtnode->getName());
    EXPECT_EQ(22, dtree.getNodeCount());

    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("l.a"),
                                                  &dtnode));
    EXPECT_EQ(Name("l"), dtnode->getName());
    EXPECT_EQ(23, dtree.getNodeCount());

    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("r.d.e.f"),
                                                  &dtnode));
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("s.d.e.f"),
                                                  &dtnode));
    EXPECT_EQ(25, dtree.getNodeCount());

    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_,
207
                                                  Name("h.w.y.d.e.f"),
208
                                                  &dtnode));
209
210

    // add more nodes one by one to cover leftRotate and rightRotate
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    EXPECT_EQ(TestDomainTree::ALREADYEXISTS, dtree.insert(mem_sgmt_, Name("f"),
                                                        &dtnode));
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("m"),
                                                  &dtnode));
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("nm"),
                                                  &dtnode));
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("om"),
                                                  &dtnode));
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("k"),
                                                  &dtnode));
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("l"),
                                                  &dtnode));
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("fe"),
                                                  &dtnode));
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("ge"),
                                                  &dtnode));
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("i"),
                                                  &dtnode));
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("ae"),
                                                  &dtnode));
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_, Name("n"),
                                                  &dtnode));
233
234
235
236
237
238
239
240
}

TEST_F(DomainTreeTest, subTreeRoot) {
    // This is a testcase for a particular issue that went unchecked in
    // #2089's implementation, but was fixed in #2092. The issue was
    // that when a node was fissioned, FLAG_SUBTREE_ROOT was not being
    // copied correctly.

241
    EXPECT_EQ(TestDomainTree::ALREADYEXISTS,
242
243
              dtree_expose_empty_node.insert(mem_sgmt_, Name("d.e.f"),
                                              &dtnode));
244
    EXPECT_EQ(TestDomainTree::SUCCESS,
245
246
              dtree_expose_empty_node.insert(mem_sgmt_, Name("0"),
                                              &dtnode));
247
    EXPECT_EQ(TestDomainTree::SUCCESS,
248
249
              dtree_expose_empty_node.insert(mem_sgmt_, Name("example.com"),
                                              &dtnode));
250
    EXPECT_EQ(TestDomainTree::ALREADYEXISTS,
251
252
              dtree_expose_empty_node.insert(mem_sgmt_, Name("example.com"),
                                              &dtnode));
253
    EXPECT_EQ(TestDomainTree::SUCCESS,
254
255
              dtree_expose_empty_node.insert(mem_sgmt_, Name("k.e.f"),
                                              &dtnode));
256
257

    // "g.h" is not a subtree root
258
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
259
260
              dtree_expose_empty_node.find(Name("g.h"), &cdtnode));
    EXPECT_FALSE(cdtnode->getFlag(TestDomainTreeNode::FLAG_SUBTREE_ROOT));
261
262

    // fission the node "g.h"
263
    EXPECT_EQ(TestDomainTree::ALREADYEXISTS,
264
265
              dtree_expose_empty_node.insert(mem_sgmt_, Name("h"),
                                              &dtnode));
266
267
268

    // the node "h" (h.down_ -> "g") should not be a subtree root. "g"
    // should be a subtree root.
269
    EXPECT_FALSE(dtnode->getFlag(TestDomainTreeNode::FLAG_SUBTREE_ROOT));
270
271

    // "g.h" should be a subtree root now.
272
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
273
274
              dtree_expose_empty_node.find(Name("g.h"), &cdtnode));
    EXPECT_TRUE(cdtnode->getFlag(TestDomainTreeNode::FLAG_SUBTREE_ROOT));
275
276
}

277
278
279
280
281
282
283
284
285
286
287
288
TEST_F(DomainTreeTest, additionalNodeFission) {
    // These are additional nodeFission tests added by #2054's rewrite
    // of DomainTree::nodeFission(). These test specific corner cases that
    // are not covered by other tests.

    // Insert "t.0" (which becomes the left child of its parent)
    EXPECT_EQ(TestDomainTree::SUCCESS,
              dtree_expose_empty_node.insert(mem_sgmt_, Name("t.0"),
                                             &dtnode));

    // "t.0" is not a subtree root
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
289
290
              dtree_expose_empty_node.find(Name("t.0"), &cdtnode));
    EXPECT_FALSE(cdtnode->getFlag(TestDomainTreeNode::FLAG_SUBTREE_ROOT));
291
292
293
294
295
296
297
298

    // fission the node "t.0"
    EXPECT_EQ(TestDomainTree::ALREADYEXISTS,
              dtree_expose_empty_node.insert(mem_sgmt_, Name("0"),
                                             &dtnode));

    // the node "0" ("0".down_ -> "t") should not be a subtree root. "t"
    // should be a subtree root.
299
    EXPECT_FALSE(dtnode->getFlag(TestDomainTreeNode::FLAG_SUBTREE_ROOT));
300
301
302

    // "t.0" should be a subtree root now.
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
303
304
              dtree_expose_empty_node.find(Name("t.0"), &cdtnode));
    EXPECT_TRUE(cdtnode->getFlag(TestDomainTreeNode::FLAG_SUBTREE_ROOT));
305
306
}

307
TEST_F(DomainTreeTest, findName) {
308
    // find const dtnode
309
    // exact match
310
311
    EXPECT_EQ(TestDomainTree::EXACTMATCH, dtree.find(Name("a"), &cdtnode));
    EXPECT_EQ(Name("a"), cdtnode->getName());
312
313

    // not found
314
315
316
317
    EXPECT_EQ(TestDomainTree::NOTFOUND, dtree.find(Name("d.e.f"), &cdtnode));
    EXPECT_EQ(TestDomainTree::NOTFOUND, dtree.find(Name("y.d.e.f"), &cdtnode));
    EXPECT_EQ(TestDomainTree::NOTFOUND, dtree.find(Name("x"), &cdtnode));
    EXPECT_EQ(TestDomainTree::NOTFOUND, dtree.find(Name("m.n"), &cdtnode));
318
319

    // if we expose empty node, we can get the empty node created during insert
320
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
321
              dtree_expose_empty_node.find(Name("d.e.f"), &cdtnode));
322
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
323
              dtree_expose_empty_node.find(Name("w.y.d.e.f"), &cdtnode));
324
325

    // partial match
326
327
    EXPECT_EQ(TestDomainTree::PARTIALMATCH, dtree.find(Name("m.b"), &cdtnode));
    EXPECT_EQ(Name("b"), cdtnode->getName());
328
    EXPECT_EQ(TestDomainTree::PARTIALMATCH,
329
              dtree_expose_empty_node.find(Name("m.d.e.f"), &cdtnode));
330

Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
331
    // find cdtnode
332
    EXPECT_EQ(TestDomainTree::EXACTMATCH, dtree.find(Name("q.w.y.d.e.f"),
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
333
334
                                                   &cdtnode));
    EXPECT_EQ(Name("q"), cdtnode->getName());
335
336
337
338
}

TEST_F(DomainTreeTest, findError) {
    // For the version that takes a node chain, the chain must be empty.
339
    TestDomainTreeNodeChain chain;
340
    EXPECT_EQ(TestDomainTree::EXACTMATCH, dtree.find(Name("a"), &cdtnode,
341
342
                                                   chain));
    // trying to reuse the same chain.  it should result in an exception.
343
    EXPECT_THROW(dtree.find(Name("a"), &cdtnode, chain),
344
345
346
347
                 BadValue);
}

TEST_F(DomainTreeTest, flags) {
348
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt_,
349
                                                  Name("flags.example"),
350
                                                  &dtnode));
351
352

    // by default, flags are all off
353
    EXPECT_FALSE(dtnode->getFlag(TestDomainTreeNode::FLAG_CALLBACK));
354
355

    // set operation, by default it enables the flag
356
357
    dtnode->setFlag(TestDomainTreeNode::FLAG_CALLBACK);
    EXPECT_TRUE(dtnode->getFlag(TestDomainTreeNode::FLAG_CALLBACK));
358
359

    // try disable the flag explicitly
360
361
    dtnode->setFlag(TestDomainTreeNode::FLAG_CALLBACK, false);
    EXPECT_FALSE(dtnode->getFlag(TestDomainTreeNode::FLAG_CALLBACK));
362
363

    // try enable the flag explicitly
364
365
    dtnode->setFlag(TestDomainTreeNode::FLAG_CALLBACK, true);
    EXPECT_TRUE(dtnode->getFlag(TestDomainTreeNode::FLAG_CALLBACK));
366
367

    // setting an unknown flag will trigger an exception
368
    EXPECT_THROW(dtnode->setFlag(static_cast<TestDomainTreeNode::Flags>(2), true),
369
370
371
372
                 isc::InvalidParameter);
}

bool
373
testCallback(const TestDomainTreeNode&, bool* callback_checker) {
374
375
376
377
378
379
    *callback_checker = true;
    return (false);
}

template <typename T>
void
380
performCallbackTest(TestDomainTree& dtree,
381
382
383
384
                    util::MemorySegmentLocal& mem_sgmt,
                    const T& name_called,
                    const T& name_not_called)
{
385
386
    TestDomainTreeNode* dtnode;
    const TestDomainTreeNode* cdtnode;
387
388

    // by default callback isn't enabled
389
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt,
390
                                                  Name("callback.example"),
391
                                                  &dtnode));
392
    EXPECT_EQ(static_cast<int*>(NULL), dtnode->setData(new int(1)));
393
    EXPECT_FALSE(dtnode->getFlag(TestDomainTreeNode::FLAG_CALLBACK));
394
395

    // enable/re-disable callback
396
397
398
399
    dtnode->setFlag(TestDomainTreeNode::FLAG_CALLBACK);
    EXPECT_TRUE(dtnode->getFlag(TestDomainTreeNode::FLAG_CALLBACK));
    dtnode->setFlag(TestDomainTreeNode::FLAG_CALLBACK, false);
    EXPECT_FALSE(dtnode->getFlag(TestDomainTreeNode::FLAG_CALLBACK));
400
401

    // enable again for subsequent tests
402
    dtnode->setFlag(TestDomainTreeNode::FLAG_CALLBACK);
403
    // add more levels below and above the callback node for partial match.
404
405
    TestDomainTreeNode* subdtnode;
    EXPECT_EQ(TestDomainTree::SUCCESS, dtree.insert(mem_sgmt,
406
                                                  Name("sub.callback.example"),
407
                                                  &subdtnode));
408
    EXPECT_EQ(static_cast<int*>(NULL), subdtnode->setData(new int(2)));
409
410
    TestDomainTreeNode* parentdtnode;
    EXPECT_EQ(TestDomainTree::ALREADYEXISTS, dtree.insert(mem_sgmt,
411
                                                        Name("example"),
412
                                                        &parentdtnode));
413
    // the child/parent nodes shouldn't "inherit" the callback flag.
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
414
415
    // "dtnode" should still validly point to "callback.example", but we
    // explicitly confirm it.
416
    EXPECT_EQ(TestDomainTree::EXACTMATCH, dtree.find(Name("callback.example"),
417
                                                     &cdtnode));
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
418
    ASSERT_EQ(dtnode, cdtnode);
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
419
    EXPECT_TRUE(cdtnode->getFlag(TestDomainTreeNode::FLAG_CALLBACK));
420
421
    EXPECT_FALSE(subdtnode->getFlag(TestDomainTreeNode::FLAG_CALLBACK));
    EXPECT_FALSE(parentdtnode->getFlag(TestDomainTreeNode::FLAG_CALLBACK));
422
423

    // check if the callback is called from find()
424
    TestDomainTreeNodeChain node_path1;
425
    bool callback_called = false;
426
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
427
              dtree.find(name_called, &cdtnode, node_path1,
428
429
430
431
432
                          testCallback, &callback_called));
    EXPECT_TRUE(callback_called);

    // enable callback at the parent node, but it doesn't have data so
    // the callback shouldn't be called.
433
    TestDomainTreeNodeChain node_path2;
434
    parentdtnode->setFlag(TestDomainTreeNode::FLAG_CALLBACK);
435
    callback_called = false;
436
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
437
              dtree.find(name_not_called, &cdtnode, node_path2,
438
439
440
441
442
443
444
445
                          testCallback, &callback_called));
    EXPECT_FALSE(callback_called);
}

TEST_F(DomainTreeTest, callbackName) {
    const Name n1("sub.callback.example");
    const Name n2("callback.example");

446
    performCallbackTest(dtree, mem_sgmt_, n1, n2);
447
448
449
450
451
452
453
454
}

TEST_F(DomainTreeTest, callbackLabelSequence) {
    const Name n1("sub.callback.example");
    const Name n2("callback.example");
    const LabelSequence ls1(n1);
    const LabelSequence ls2(n2);

455
    performCallbackTest(dtree, mem_sgmt_, ls1, ls2);
456
457
}

458
459
460
461
462
TEST_F(DomainTreeTest, findInSubTree) {
    // For the version that takes a node chain, the chain must be empty.
    DomainTreeNodeChain<int> chain;
    bool flag;

463
464
465
466
467
468
469
470
471
    // Searching for a non-absolute (right-stripped) label sequence when
    // chain is empty should throw.
    const Name n0("w.y.d.e.f");
    LabelSequence ls0(n0);
    ls0.stripRight(1);
    EXPECT_THROW(dtree_expose_empty_node.find(ls0, &cdtnode, chain,
                                              testCallback, &flag),
                 isc::BadValue);

472
    // First, find a sub-tree node
473
    chain.clear();
474
    const LabelSequence ls1(n0);
475
476
477
478
    DomainTree<int>::Result result =
        dtree_expose_empty_node.find(ls1, &cdtnode, chain,
                                     testCallback, &flag);
    EXPECT_EQ(DomainTree<int>::EXACTMATCH, result);
479
    EXPECT_EQ(n0, chain.getAbsoluteName());
480
481
482
483
484
485
486
487
488
489

    // Searching for an absolute label sequence when chain is already
    // populated should throw.
    const Name n2a("o");
    const LabelSequence ls2a(n2a);
    EXPECT_THROW(dtree_expose_empty_node.find(ls2a, &cdtnode, chain,
                                              testCallback, &flag),
                 isc::BadValue);

    // Now, find "o.w.y.d.e.f." by right-stripping the "w.y.d.e.f."
490
    // suffix to "o" (non-absolute).
491
492
493
    const Name n2("o.w.y.d.e.f");
    LabelSequence ls2(n2);
    ls2.stripRight(6);
494
    EXPECT_EQ("o", ls2.toText());
495
496
497
498
499

    result = dtree_expose_empty_node.find(ls2, &cdtnode, chain,
                                          testCallback, &flag);
    EXPECT_EQ(DomainTree<int>::EXACTMATCH, result);
    EXPECT_EQ(n2, chain.getAbsoluteName());
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521

    // Another test. Start with "d.e.f." node.
    chain.clear();
    const Name n3("d.e.f");
    const LabelSequence ls3(n3);
    result =
        dtree_expose_empty_node.find(ls3, &cdtnode, chain,
                                     testCallback, &flag);
    EXPECT_EQ(DomainTree<int>::EXACTMATCH, result);
    EXPECT_EQ(n3, chain.getAbsoluteName());

    // Now, find "o.w.y.d.e.f." by right-stripping the "w.y.d.e.f."
    // suffix to "o.w.y" (non-absolute).
    const Name n4("o.w.y.d.e.f");
    LabelSequence ls4(n2);
    ls4.stripRight(4);
    EXPECT_EQ("o.w.y", ls4.toText());

    result = dtree_expose_empty_node.find(ls4, &cdtnode, chain,
                                          testCallback, &flag);
    EXPECT_EQ(DomainTree<int>::EXACTMATCH, result);
    EXPECT_EQ(n4, chain.getAbsoluteName());
522
523
524
525
526
527
528
529
530
531
532
533
}

TEST_F(DomainTreeTest, findInSubTreeSameLabelSequence) {
    // For the version that takes a node chain, the chain must be empty.
    DomainTreeNodeChain<int> chain;
    bool flag;

    const Name n1("c.g.h");

    // First insert a "c.g.h." node.
    dtree_expose_empty_node.insert(mem_sgmt_, n1, &dtnode);

534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
    /* Now, the tree looks like:
     *
     *             .
     *             |
     *             b
     *           /   \
     *          a    d.e.f
     *              /  |  \____
     *             c   |       \
     *                 |        g.h
     *                 |         |
     *                w.y        i
     *              /  |  \     / \
     *             x   |   z   c   k
     *                 |   |
     *                 p   j
     *               /   \
     *              o     q
     */

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
    // Make a non-absolute label sequence. We will search for this same
    // sequence in two places in the tree.
    LabelSequence ls1(n1);
    ls1.stripRight(3);
    EXPECT_EQ("c", ls1.toText());

    // First, find "g.h."
    const Name n2("g.h");
    const LabelSequence ls2(n2);
    DomainTree<int>::Result result =
        dtree_expose_empty_node.find(ls2, &cdtnode, chain,
                                     testCallback, &flag);
    EXPECT_EQ(DomainTree<int>::EXACTMATCH, result);
    EXPECT_EQ(n2, chain.getAbsoluteName());

    // Now, find "c.g.h." by searching just the non-absolute ls1 label
    // sequence.
    result = dtree_expose_empty_node.find(ls1, &cdtnode, chain,
                                          testCallback, &flag);
    EXPECT_EQ(DomainTree<int>::EXACTMATCH, result);
    EXPECT_EQ(n1, chain.getAbsoluteName());

    // Now, find "." (the root node)
    chain.clear();
    const Name n3(".");
    const LabelSequence ls3(n3);
    result =
        dtree_expose_empty_node.find(ls3, &cdtnode, chain,
                                     testCallback, &flag);
    EXPECT_EQ(DomainTree<int>::EXACTMATCH, result);
    EXPECT_EQ(n3, chain.getAbsoluteName());

    // Now, find "c." by searching just the non-absolute ls1 label
    // sequence.
    result = dtree_expose_empty_node.find(ls1, &cdtnode, chain,
                                          testCallback, &flag);
    EXPECT_EQ(DomainTree<int>::EXACTMATCH, result);
    EXPECT_EQ(Name("c."), chain.getAbsoluteName());
592
593
}

594
TEST_F(DomainTreeTest, chainLevel) {
595
    TestDomainTreeNodeChain chain;
596
597
598
599

    // by default there should be no level in the chain.
    EXPECT_EQ(0, chain.getLevelCount());

600
601
602
603
    // Copy should be consistent
    TestDomainTreeNodeChain chain2(chain);
    EXPECT_EQ(chain.getLevelCount(), chain2.getLevelCount());

604
605
    // insert one node to the tree and find it.  there should be exactly
    // one level in the chain.
606
607
    TreeHolder tree_holder(mem_sgmt_, TestDomainTree::create(mem_sgmt_, true));
    TestDomainTree& tree(*tree_holder.get());
608
    Name node_name(Name::ROOT_NAME());
609
    EXPECT_EQ(TestDomainTree::SUCCESS, tree.insert(mem_sgmt_, node_name,
610
                                                &dtnode));
611
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
612
              tree.find(node_name, &cdtnode, chain));
613
614
    EXPECT_EQ(1, chain.getLevelCount());

615
616
617
618
619
    // Copy should be consistent
    TestDomainTreeNodeChain chain3(chain);
    EXPECT_EQ(chain.getLevelCount(), chain3.getLevelCount());
    EXPECT_EQ(chain.getAbsoluteName(), chain3.getAbsoluteName());

620
621
622
623
624
    // Check the name of the found node (should have '.' as both non-absolute
    // and absolute name
    EXPECT_EQ(".", cdtnode->getLabels().toText());
    EXPECT_EQ(".", cdtnode->getAbsoluteLabels(buf).toText());

625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
    /*
     * Now creating a possibly deepest tree with MAX_LABELS levels.
     * it should look like:
     *           (.)
     *            |
     *            a
     *            |
     *            a
     *            : (MAX_LABELS - 1) "a"'s
     *
     * then confirm that find() for the deepest name succeeds without any
     * disruption, and the resulting chain has the expected level.
     * Note that the root name (".") solely belongs to a single level,
     * so the levels begin with 2.
     */
    for (unsigned int i = 2; i <= Name::MAX_LABELS; ++i) {
        node_name = Name("a.").concatenate(node_name);
642
        EXPECT_EQ(TestDomainTree::SUCCESS, tree.insert(mem_sgmt_, node_name,
643
                                                    &dtnode));
644
645
        TestDomainTreeNodeChain found_chain;
        EXPECT_EQ(TestDomainTree::EXACTMATCH,
646
                  tree.find(node_name, &cdtnode, found_chain));
647
        EXPECT_EQ(i, found_chain.getLevelCount());
648
649
650
651
652
653

        // The non-absolute name should only have the first label
        EXPECT_EQ("a", cdtnode->getLabels().toText());
        // But the absolute name should have all labels
        EXPECT_EQ(node_name.toText(),
                  cdtnode->getAbsoluteLabels(buf).toText());
654
655
656
    }

    // Confirm the last inserted name has the possible maximum length with
657
    // maximum label count.  This confirms the dtree and chain level cannot
658
659
660
661
662
663
664
    // be larger.
    EXPECT_EQ(Name::MAX_LABELS, node_name.getLabelCount());
    EXPECT_THROW(node_name.concatenate(Name("a.")), TooLongName);
}

TEST_F(DomainTreeTest, getAbsoluteNameError) {
    // an empty chain isn't allowed.
665
    TestDomainTreeNodeChain chain;
666
667
668
669
    EXPECT_THROW(chain.getAbsoluteName(), BadValue);
}

/*
670
 * The domain order should be:
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
 * ., a, b, c, d.e.f, x.d.e.f, w.y.d.e.f, o.w.y.d.e.f, p.w.y.d.e.f,
 * q.w.y.d.e.f, z.d.e.f, j.z.d.e.f, g.h, i.g.h, k.g.h
 *             . (no data, can't be found)
 *             |
 *             b
 *           /   \
 *          a    d.e.f
 *              /  |   \
 *             c   |    g.h
 *                 |     |
 *                w.y    i
 *              /  |  \   \
 *             x   |   z   k
 *                 |   |
 *                 p   j
 *               /   \
 *              o     q
 */
const char* const names[] = {
    "a", "b", "c", "d.e.f", "x.d.e.f", "w.y.d.e.f", "o.w.y.d.e.f",
    "p.w.y.d.e.f", "q.w.y.d.e.f", "z.d.e.f", "j.z.d.e.f",
    "g.h", "i.g.h", "k.g.h"};
const size_t name_count(sizeof(names) / sizeof(*names));

const char* const upper_node_names[] = {
    ".", ".", ".", ".", "d.e.f", "d.e.f", "w.y.d.e.f",
    "w.y.d.e.f", "w.y.d.e.f", "d.e.f", "z.d.e.f",
    ".", "g.h", "g.h"};

TEST_F(DomainTreeTest, getUpperNode) {
701
702
703
    TestDomainTreeNodeChain node_path;
    const TestDomainTreeNode* node = NULL;
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
704
              dtree_expose_empty_node.find(Name(names[0]),
705
706
707
708
709
                                            &node,
                                            node_path));
    for (int i = 0; i < name_count; ++i) {
        EXPECT_NE(static_cast<void*>(NULL), node);

710
        const TestDomainTreeNode* upper_node = node->getUpperNode();
711
        if (upper_node_names[i] != NULL) {
712
713
            const TestDomainTreeNode* upper_node2 = NULL;
            EXPECT_EQ(TestDomainTree::EXACTMATCH,
714
                      dtree_expose_empty_node.find(Name(upper_node_names[i]),
715
716
717
718
719
720
721
                                                    &upper_node2));
            EXPECT_NE(static_cast<void*>(NULL), upper_node2);
            EXPECT_EQ(upper_node, upper_node2);
        } else {
            EXPECT_EQ(static_cast<void*>(NULL), upper_node);
        }

722
        node = dtree_expose_empty_node.nextNode(node_path);
723
724
725
726
727
728
    }

    // We should have reached the end of the tree.
    EXPECT_EQ(static_cast<void*>(NULL), node);
}

729
730

#if 0
731
732
// Disabled and kept still, for use in case we make getSubTreeRoot() a
// public function again.
733
734
735
736
737
738

const char* const subtree_root_node_names[] = {
    "b", "b", "b", "b", "w.y.d.e.f", "w.y.d.e.f", "p.w.y.d.e.f",
    "p.w.y.d.e.f", "p.w.y.d.e.f", "w.y.d.e.f", "j.z.d.e.f",
    "b", "i.g.h", "i.g.h"};

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
TEST_F(DomainTreeTest, getSubTreeRoot) {
    TestDomainTreeNodeChain node_path;
    const TestDomainTreeNode* node = NULL;
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
              dtree_expose_empty_node.find(Name(names[0]),
                                            &node,
                                            node_path));
    for (int i = 0; i < name_count; ++i) {
        EXPECT_NE(static_cast<void*>(NULL), node);

        const TestDomainTreeNode* sr_node = node->getSubTreeRoot();
        if (subtree_root_node_names[i] != NULL) {
            const TestDomainTreeNode* sr_node2 = NULL;
            EXPECT_EQ(TestDomainTree::EXACTMATCH,
                dtree_expose_empty_node.find(Name(subtree_root_node_names[i]),
                                             &sr_node2));
            EXPECT_NE(static_cast<void*>(NULL), sr_node2);
            EXPECT_EQ(sr_node, sr_node2);
        } else {
            EXPECT_EQ(static_cast<void*>(NULL), sr_node);
        }

        node = dtree_expose_empty_node.nextNode(node_path);
    }

    // We should have reached the end of the tree.
    EXPECT_EQ(static_cast<void*>(NULL), node);
}

768
#endif // disabled getSubTreeRoot()
769
770


771
TEST_F(DomainTreeTest, nextNode) {
772
773
774
    TestDomainTreeNodeChain node_path;
    const TestDomainTreeNode* node = NULL;
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
775
              dtree.find(Name(names[0]), &node, node_path));
776
777
778
    for (int i = 0; i < name_count; ++i) {
        EXPECT_NE(static_cast<void*>(NULL), node);
        EXPECT_EQ(Name(names[i]), node_path.getAbsoluteName());
779
        node = dtree.nextNode(node_path);
780
781
782
783
784
785
786
787
788
    }

    // We should have reached the end of the tree.
    EXPECT_EQ(static_cast<void*>(NULL), node);
}

// Just walk using previousNode until the beginning of the tree and check it is
// OK
//
789
// dtree - the tree to walk
790
791
792
793
794
795
796
797
798
799
// node - result of previous call to find(), starting position of the walk
// node_path - the path from the previous call to find(), will be modified
// chain_length - the number of names that should be in the chain to be walked
//   (0 means it should be empty, 3 means 'a', 'b' and 'c' should be there -
//   this is always from the beginning of the names[] list).
// skip_first - if this is false, the node should already contain the node with
//   the first name of the chain. If it is true, the node should be NULL
//   (true is for finds that return no match, false for the ones that return
//   match)
void
800
previousWalk(TestDomainTree& dtree, const TestDomainTreeNode* node,
801
             TestDomainTreeNodeChain& node_path, size_t chain_length,
802
803
804
805
806
807
             bool skip_first)
{
    if (skip_first) {
        // If the first is not found, this is supposed to be NULL and we skip
        // it in our checks.
        EXPECT_EQ(static_cast<void*>(NULL), node);
808
        node = dtree.previousNode(node_path);
809
810
811
812
813
814
815
816
817
    }
    for (size_t i(chain_length); i > 0; --i) {
        EXPECT_NE(static_cast<void*>(NULL), node);
        EXPECT_EQ(Name(names[i - 1]), node_path.getAbsoluteName());
        // Find the node at the path and check the value is the same
        // (that it really returns the correct corresponding node)
        //
        // The "empty" nodes can not be found
        if (node->getData()) {
818
819
820
            const TestDomainTreeNode* node2(NULL);
            TestDomainTreeNodeChain node_path2;
            EXPECT_EQ(TestDomainTree::EXACTMATCH,
821
                      dtree.find(Name(names[i - 1]), &node2, node_path2));
822
823
            EXPECT_EQ(node, node2);
        }
824
        node = dtree.previousNode(node_path);
825
826
827
828
829
830
831
    }

    // We should have reached the start of the tree.
    ASSERT_NE(static_cast<void*>(NULL), node);
    EXPECT_EQ(".", node->getLabels().toText());

    // With one more call it results in NULL
832
    node = dtree.previousNode(node_path);
833
834
835
836
    EXPECT_EQ(static_cast<void*>(NULL), node);

    // Calling previousNode() yet again should still return NULL without
    // fail.
837
    node = dtree.previousNode(node_path);
838
839
840
841
842
843
    EXPECT_EQ(static_cast<void*>(NULL), node);
}

// Check the previousNode
TEST_F(DomainTreeTest, previousNode) {
    // First, iterate the whole tree from the end to the beginning.
844
    TestDomainTreeNodeChain node_path;
845
    EXPECT_THROW(dtree.previousNode(node_path), isc::BadValue) <<
846
        "Throw before a search was done on the path";
847
    const TestDomainTreeNode* node(NULL);
848
849
    {
        SCOPED_TRACE("Iterate through");
850
        EXPECT_EQ(TestDomainTree::EXACTMATCH,
851
852
                  dtree.find(Name(names[name_count - 1]), &node, node_path));
        previousWalk(dtree, node, node_path, name_count, false);
853
854
855
856
857
858
859
        node = NULL;
        node_path.clear();
    }

    {
        SCOPED_TRACE("Iterate from the middle");
        // Now, start somewhere in the middle, but within the real node.
860
        EXPECT_EQ(TestDomainTree::EXACTMATCH,
861
862
                  dtree.find(Name(names[4]), &node, node_path));
        previousWalk(dtree, node, node_path, 5, false);
863
864
865
866
867
868
869
870
        node = NULL;
        node_path.clear();
    }

    {
        SCOPED_TRACE("Start at the first");
        // If we start at the lowest (which is "a"), we get to the beginning
        // right away.
871
        EXPECT_EQ(TestDomainTree::EXACTMATCH,
872
                  dtree.find(Name(names[0]), &node, node_path));
873
        EXPECT_NE(static_cast<void*>(NULL), node);
874
        node = dtree.previousNode(node_path);
875
876
877
878
879
880
881
882
883
884
        ASSERT_NE(static_cast<void*>(NULL), node);
        EXPECT_EQ(".", node->getLabels().toText());
        node = NULL;
        node_path.clear();
    }

    {
        SCOPED_TRACE("Start before the first");
        // If we start before the lowest (. < 0. < a.), we should not get a
        // node.  Its previous node should be the root.
885
        EXPECT_EQ(TestDomainTree::NOTFOUND,
886
                  dtree.find<void*>(Name("0"), &node, node_path, NULL, NULL));
887
        EXPECT_EQ(static_cast<void*>(NULL), node);
888
        node = dtree.previousNode(node_path);
889
890
891
892
893
894
895
896
        ASSERT_NE(static_cast<void*>(NULL), node);
        EXPECT_EQ(".", node->getLabels().toText());
        node = NULL;
        node_path.clear();
    }

    {
        SCOPED_TRACE("Start after the last");
897
        EXPECT_EQ(TestDomainTree::NOTFOUND,
898
899
                  dtree.find(Name("z"), &node, node_path));
        previousWalk(dtree, node, node_path, name_count, true);
900
901
902
903
904
905
906
907
908
        node = NULL;
        node_path.clear();
    }

    {
        SCOPED_TRACE("Start below a leaf");
        // We exit a leaf by going down. We should start by the one
        // we exited - 'c' (actually, we should get it by the find, as partial
        // match).
909
        EXPECT_EQ(TestDomainTree::PARTIALMATCH,
910
911
                  dtree.find(Name("b.c"), &node, node_path));
        previousWalk(dtree, node, node_path, 3, false);
912
913
914
915
916
917
918
919
920
921
922
        node = NULL;
        node_path.clear();
    }

    {
        SCOPED_TRACE("Start to the right of a leaf");
        // When searching for this, we exit the 'x' node to the right side,
        // so we should go x afterwards.

        // The d.e.f is empty node, so it is hidden by find. Therefore NOTFOUND
        // and not PARTIALMATCH.
923
        EXPECT_EQ(TestDomainTree::NOTFOUND,
924
925
                  dtree.find(Name("xy.d.e.f"), &node, node_path));
        previousWalk(dtree, node, node_path, 5, true);
926
927
928
929
930
931
932
933
934
935
936
        node = NULL;
        node_path.clear();
    }

    {
        SCOPED_TRACE("Start to the left of a leaf");
        // This is similar to the previous, but we exit the 'z' leaf to the
        // left side, so should not visit z at all then.

        // The d.e.f is empty node, so it is hidden by find. Therefore NOTFOUND
        // and not PARTIALMATCH.
937
        EXPECT_EQ(TestDomainTree::NOTFOUND,
938
939
                  dtree.find(Name("yz.d.e.f"), &node, node_path));
        previousWalk(dtree, node, node_path, 9, true);
940
941
942
943
944
945
946
947
948
949
950
        node = NULL;
        node_path.clear();
    }

    {
        SCOPED_TRACE("Start to the right of a parent");
        // When searching for this, we exit the 'g.h' node to the right
        // side, so we should go to g.h's children afterwards.

        // 'g.h' is an empty node, so we get a NOTFOUND and not
        // PARTIALMATCH.
951
        EXPECT_EQ(TestDomainTree::NOTFOUND,
952
                  dtree.find(Name("x.h"), &node, node_path));
953
954
955
956
957
958
959
960
        // 'g.h' is the COMMONANCESTOR.
        EXPECT_EQ(node_path.getLastComparedNode()->getName(), Name("g.h"));
        EXPECT_EQ(NameComparisonResult::COMMONANCESTOR,
                  node_path.getLastComparisonResult().getRelation());
        // find() exits to the right of 'g.h'
        EXPECT_GT(node_path.getLastComparisonResult().getOrder(), 0);
        // We then descend into 'i.g.h' and walk all the nodes in the
        // tree.
961
        previousWalk(dtree, node, node_path, name_count, true);
962
963
964
965
966
967
968
969
        node = NULL;
        node_path.clear();
    }

    {
        SCOPED_TRACE("Start inside a wrong node");
        // The d.e.f is a single node, but we want only part of it. We
        // should start iterating before it.
970
        EXPECT_EQ(TestDomainTree::NOTFOUND,
971
972
                  dtree.find(Name("e.f"), &node, node_path));
        previousWalk(dtree, node, node_path, 3, true);
973
974
975
976
977
978
979
        node = NULL;
        node_path.clear();
    }

    {
        SCOPED_TRACE("Lookup in empty tree");
        // Just check it doesn't crash, etc.
980
981
982
        TreeHolder tree_holder(mem_sgmt_, TestDomainTree::create(mem_sgmt_));
        TestDomainTree& empty_tree(*tree_holder.get());
        EXPECT_EQ(TestDomainTree::NOTFOUND,
983
984
985
986
987
988
989
990
                  empty_tree.find(Name("x"), &node, node_path));
        EXPECT_EQ(static_cast<void*>(NULL), node);
        EXPECT_EQ(static_cast<void*>(NULL),
                  empty_tree.previousNode(node_path));
        node = NULL;
        node_path.clear();
    }
}
991
992
993
994

TEST_F(DomainTreeTest, largestNode) {
    cdtnode = dtree.largestNode();
    EXPECT_EQ(Name("k"), cdtnode->getName());
995
996
997
998
999

    // Check for largest node in an empty tree.
    TreeHolder empty_tree_holder
        (mem_sgmt_, TestDomainTree::create(mem_sgmt_));
    TestDomainTree& empty_tree(*empty_tree_holder.get());
1000
    EXPECT_EQ(static_cast<void*>(NULL), empty_tree.largestNode());
1001
}
1002
1003
1004

TEST_F(DomainTreeTest, nextNodeError) {
    // Empty chain for nextNode() is invalid.
1005
    TestDomainTreeNodeChain chain;
1006
    EXPECT_THROW(dtree.nextNode(chain), BadValue);
1007
1008
1009
1010
}

// A helper function for getLastComparedNode() below.
void
1011
comparisonChecks(const TestDomainTreeNodeChain& chain,
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
                 int expected_order, int expected_common_labels,
                 NameComparisonResult::NameRelation expected_reln)
{
    if (expected_order > 0) {
        EXPECT_LT(0, chain.getLastComparisonResult().getOrder());
    } else if (expected_order < 0) {
        EXPECT_GT(0, chain.getLastComparisonResult().getOrder());
    } else {
        EXPECT_EQ(0, chain.getLastComparisonResult().getOrder());
    }
    EXPECT_EQ(expected_common_labels,
              chain.getLastComparisonResult().getCommonLabels());
    EXPECT_EQ(expected_reln,
              chain.getLastComparisonResult().getRelation());
}

TEST_F(DomainTreeTest, getLastComparedNode) {
1029
    TestDomainTree& tree = dtree_expose_empty_node; // use the "empty OK" mode
1030
    TestDomainTreeNodeChain chain;
1031
1032
1033
1034
1035

    // initially there should be no 'last compared'.
    EXPECT_EQ(static_cast<void*>(NULL), chain.getLastComparedNode());

    // A search for an empty tree should result in no 'last compared', too.
1036
1037
1038
    TreeHolder tree_holder(mem_sgmt_, TestDomainTree::create(mem_sgmt_));
    TestDomainTree& empty_tree(*tree_holder.get());
    EXPECT_EQ(TestDomainTree::NOTFOUND,
1039
              empty_tree.find(Name("a"), &cdtnode, chain));
1040
1041
1042
    EXPECT_EQ(static_cast<void*>(NULL), chain.getLastComparedNode());
    chain.clear();

1043
    const TestDomainTreeNode* expected_node = NULL;
1044
1045

    // Exact match case.  The returned node should be last compared.
1046
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
1047
1048
1049
1050
1051
1052
1053
1054
              tree.find(Name("x.d.e.f"), &expected_node, chain));
    EXPECT_EQ(expected_node, chain.getLastComparedNode());
    // 1 = # labels of "x" (note: excluding ".")
    comparisonChecks(chain, 0, 1, NameComparisonResult::EQUAL);
    chain.clear();

    // Partial match, search stopped at the matching node, which should be
    // the last compared node.
1055
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
1056
              tree.find(Name("k.g.h"), &expected_node));
1057
    EXPECT_EQ(TestDomainTree::PARTIALMATCH,
1058
              tree.find(Name("x.k.g.h"), &cdtnode, chain));
1059
1060
1061
1062
1063
1064
1065
    EXPECT_EQ(expected_node, chain.getLastComparedNode());
    // k.g.h < x.k.g.h, 1 = # labels of "k"
    comparisonChecks(chain, 1, 1, NameComparisonResult::SUBDOMAIN);
    chain.clear();

    // Partial match, search stopped in the subtree below the matching node
    // after following a left branch.
1066
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
1067
              tree.find(Name("x.d.e.f"), &expected_node));
1068
    EXPECT_EQ(TestDomainTree::PARTIALMATCH,
1069
              tree.find(Name("a.d.e.f"), &cdtnode, chain));
1070
1071
1072
1073
1074
1075
1076
    EXPECT_EQ(expected_node, chain.getLastComparedNode());
    // a < x, no common labels
    comparisonChecks(chain, -1, 0, NameComparisonResult::NONE);
    chain.clear();

    // Partial match, search stopped in the subtree below the matching node
    // after following a right branch.
1077
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
1078
              tree.find(Name("z.d.e.f"), &expected_node));
1079
    EXPECT_EQ(TestDomainTree::PARTIALMATCH,
1080
              tree.find(Name("zz.d.e.f"), &cdtnode, chain));
1081
1082
1083
1084
1085
1086
1087
    EXPECT_EQ(expected_node, chain.getLastComparedNode());
    // zz > z, no common label
    comparisonChecks(chain, 1, 0, NameComparisonResult::NONE);
    chain.clear();

    // Partial match, search stopped at a node for a super domain of the
    // search name in the subtree below the matching node.
1088
    EXPECT_EQ(TestDomainTree::EXACTMATCH,
1089
              tree.find(Name("w.y.d.e.f"), &expected_node));
1090
    EXPECT_EQ(TestDomainTree::PARTIALMATCH,
1091
              tree.find(Name("y.d.e.f"), &cdtnode, chain));
1092
1093
1094
1095
1096
1097
1098
1099
    EXPECT_EQ(expected_node, chain.getLastComparedNode());
    // y < w.y, 1 = # labels of "y"
    comparisonChecks(chain, -1, 1, NameComparisonResult::SUPERDOMAIN);
    chain.clear();

    // Partial match, search stopped at a node that share a common ancestor
    // with the search name in the subtree below the matching node.
    // (the expected node is the same as the previous case)
1100
    EXPECT_EQ(TestDomainTree::PARTIALMATCH,
1101
              tree.find(Name("z.y.d.e.f"), &cdtnode, chain));
1102
1103
1104
1105
1106
1107
1108
    EXPECT_EQ(expected_node, chain.getLastComparedNode());
    // z.y > w.y, 1 = # labels of "y"
    comparisonChecks(chain, 1, 1, NameComparisonResult::COMMONANCESTOR);
    chain.clear();

    // Search stops in the highest level (under ".") after following a left
    // branch. (find() still returns PARTIALMATCH due to the top level ".")
1109
1110
    EXPECT_EQ(TestDomainTree::EXACTMATCH, tree.find(Name("c"), &expected_node));
    EXPECT_EQ(TestDomainTree::PARTIALMATCH,
1111
              tree.find(Name("bb"), &cdtnode, chain));
1112
1113
1114
1115
1116
1117
1118
    EXPECT_EQ(expected_node, chain.getLastComparedNode());
    // bb < c, no common label
    comparisonChecks(chain, -1, 0, NameComparisonResult::NONE);
    chain.clear();

    // Search stops in the highest level (under ".") after following a right
    // branch. (the expected node is the same as the previous case)
1119
    EXPECT_EQ(TestDomainTree::PARTIALMATCH,
1120
              tree.find(Name("d"), &cdtnode, chain));
1121
1122
1123
1124
1125
1126
1127
1128
1129
    EXPECT_EQ(expected_node, chain.getLastComparedNode());
    // d > c, no common label
    comparisonChecks(chain, 1, 0, NameComparisonResult::NONE);
    chain.clear();
}

TEST_F(DomainTreeTest, dumpTree) {
    std::ostringstream str;
    std::ostringstream str2;
1130
    dtree.dumpTree(str);
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
    str2 << "tree has 15 node(s)\n"
            ". (black) [invisible] [subtreeroot]\n"
            "     begin down from .\n"
            "     b (black) [subtreeroot]\n"
            "          a (black)\n"
            "               NULL\n"
            "               NULL\n"
            "          d.e.f (black) [invisible]\n"
            "               begin down from d.e.f\n"
            "               w.y (black) [invisible] [subtreeroot]\n"
            "                    begin down from w.y\n"
            "                    p (black) [subtreeroot]\n"
            "                         o (red)\n"
            "                              NULL\n"
            "                              NULL\n"
            "                         q (red)\n"
            "                              NULL\n"
            "                              NULL\n"
            "                    end down from w.y\n"
            "                    x (red)\n"
            "                         NULL\n"
            "                         NULL\n"
            "                    z (red)\n"
            "                         begin down from z\n"
            "                         j (black) [subtreeroot]\n"
            "                              NULL\n"
            "                              NULL\n"
            "                         end down from z\n"
            "                         NULL\n"
            "                         NULL\n"
            "               end down from d.e.f\n"
            "               c (red)\n"
            "                    NULL\n"
            "                    NULL\n"
            "               g.h (red)\n"
            "                    begin down from g.h\n"
            "                    i (black) [subtreeroot]\n"
            "                         NULL\n"
            "                         k (red)\n"
            "                              NULL\n"
            "                              NULL\n"
            "                    end down from g.h\n"
            "                    NULL\n"
            "                    NULL\n"
            "     end down from .\n"
            "     NULL\n"
            "     NULL\n";
    EXPECT_EQ(str2.str(), str.str());
}

TEST_F(DomainTreeTest, swap) {
    // Store info about the first tree
    std::ostringstream str1;