name_unittest.cc 22.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Copyright (C) 2009  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 <vector>
#include <string>
#include <sstream>
#include <iomanip>
19
#include <limits>
20
21
#include <stdexcept>

22
#include <dns/buffer.h>
23
#include <dns/exceptions.h>
24
25
#include <dns/name.h>
#include <dns/messagerenderer.h>
26

27
#include <dns/tests/unittest_util.h>
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

#include <gtest/gtest.h>

using namespace std;
using namespace isc;
using namespace isc::dns;

//
// XXX: these are defined as class static constants, but some compilers
// seemingly cannot find the symbols when used in the EXPECT_xxx macros.
//
const size_t Name::MAX_WIRE;
const size_t Name::MAX_LABELS;

namespace {
class NameTest : public ::testing::Test {
protected:
    NameTest() : example_name("www.example.com"),
                 example_name_upper("WWW.EXAMPLE.COM"),
                 small_name("aaa.example.com"),
                 large_name("zzz.example.com"),
                 buffer_actual(0), buffer_expected(0)
    {}

    const Name example_name;
    Name example_name_upper;    // this will be modified and cannot be const
    const Name small_name;
    const Name large_name;
    OutputBuffer buffer_actual, buffer_expected;

    //
    // helper methods
    //
    static Name nameFactoryFromWire(const char* datafile, size_t position,
                                    bool downcase = false);
    // construct a name including all non-upper-case-alphabet characters.
    static Name nameFactoryLowerCase();
    void compareInWireFormat(const Name& name_actual,
                             const Name& name_expected);
};

69
70
const Name downcased_global("\\255.EXAMPLE.COM", true);

71
72
73
74
75
76
77
78
79
80
81
82
83
84
Name
NameTest::nameFactoryFromWire(const char* datafile, size_t position,
                              bool downcase)
{
    vector<unsigned char> data;
    UnitTestUtil::readWireData(datafile, data);

    InputBuffer buffer(&data[0], data.size());
    buffer.setPosition(position);

    return (Name(buffer, downcase));
}

Name
85
NameTest::nameFactoryLowerCase() {
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
    string lowercase_namestr;
    lowercase_namestr.reserve(Name::MAX_WIRE);

    unsigned int ch = 0;
    unsigned int labelcount = 0;
    do {
        if (ch < 'A' || ch > 'Z') {
            ostringstream ss;
            ss.setf(ios_base::right, ios_base::adjustfield);
            ss.width(3);
            ss << setfill('0') << ch;
            lowercase_namestr += '\\' + ss.str();

            if (++labelcount == Name::MAX_LABELLEN) {
                lowercase_namestr.push_back('.');
                labelcount = 0;
            }
        }
    } while (++ch <= Name::MAX_WIRE);

    return (Name(lowercase_namestr));
}

void
NameTest::compareInWireFormat(const Name& name_actual,
                              const Name& name_expected)
{
    buffer_actual.clear();
    buffer_expected.clear();

    name_actual.toWire(buffer_actual);
    name_expected.toWire(buffer_expected);

    EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
                        buffer_actual.getData(), buffer_actual.getLength(),
                        buffer_expected.getData(), buffer_expected.getLength());
}

124
TEST_F(NameTest, nonlocalObject) {
125
126
127
128
129
130
131
    // A previous version of code relied on a non local static object for
    // name construction, so a non local static Name object defined outside
    // the name module might not be initialized correctly.  This test detects
    // that kind of bug.
    EXPECT_EQ("\\255.example.com.", downcased_global.toText());
}

132
TEST_F(NameTest, fromText) {
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
    vector<string> strnames;
    strnames.push_back("www.example.com");
    strnames.push_back("www.example.com."); // with a trailing dot
    strnames.push_back("wWw.exAmpLe.com");  // mixed cases
    strnames.push_back("\\wWw.exAmpLe.com"); // escape with a backslash
    // decimal representation for "WWW"
    strnames.push_back("\\087\\087\\087.example.com");

    vector<string>::const_iterator it;
    for (it = strnames.begin(); it != strnames.end(); ++it) {
        EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name, Name(*it));
    }

    // root names
    EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, Name("@"), Name("."));

    // downcase
    EXPECT_EQ(Name("Www.eXample.coM", true).toText(), example_name.toText());

    //
    // Tests for bogus names.  These should trigger an exception.
    //
    // empty label cannot be followed by another label
    EXPECT_THROW(Name(".a"), EmptyLabel);
    // duplicate period
    EXPECT_THROW(Name("a.."), EmptyLabel);
    // label length must be < 64
    EXPECT_THROW(Name("012345678901234567890123456789"
                      "012345678901234567890123456789"
                      "0123"), TooLongLabel);
    // now-unsupported bitstring labels
    EXPECT_THROW(Name("\\[b11010000011101]"), BadLabelType);
    // label length must be < 64
    EXPECT_THROW(Name("012345678901234567890123456789"
                      "012345678901234567890123456789"
                      "012\\x"), TooLongLabel);
    // but okay as long as resulting len < 64 even if the original string is
    // "too long"
    EXPECT_NO_THROW(Name("012345678901234567890123456789"
                         "012345678901234567890123456789"
                         "01\\x"));
    // incomplete \DDD pattern (exactly 3 D's must appear)
    EXPECT_THROW(Name("\\12abc"), BadEscape);
    // \DDD must not exceed 255
    EXPECT_THROW(Name("\\256"), BadEscape);
    // Same tests for \111 as for \\x above
    EXPECT_THROW(Name("012345678901234567890123456789"
                      "012345678901234567890123456789"
                      "012\\111"), TooLongLabel);
    EXPECT_NO_THROW(Name("012345678901234567890123456789"
                         "012345678901234567890123456789"
                         "01\\111"));
    // A domain name must be 255 octets or less
    EXPECT_THROW(Name("123456789.123456789.123456789.123456789.123456789."
                      "123456789.123456789.123456789.123456789.123456789."
                      "123456789.123456789.123456789.123456789.123456789."
                      "123456789.123456789.123456789.123456789.123456789."
                      "123456789.123456789.123456789.123456789.123456789."
                      "1234"), TooLongName);
    // This is a possible longest name and should be accepted
    EXPECT_NO_THROW(Name("123456789.123456789.123456789.123456789.123456789."
                         "123456789.123456789.123456789.123456789.123456789."
                         "123456789.123456789.123456789.123456789.123456789."
                         "123456789.123456789.123456789.123456789.123456789."
                         "123456789.123456789.123456789.123456789.123456789."
                         "123"));
    // \DDD must consist of 3 digits.
    EXPECT_THROW(Name("\\12"), IncompleteName);

    // a name with the max number of labels.  should be constructed without
    // an error, and its length should be the max value.
    Name maxlabels = Name("0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9." // 40
                          "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9." // 80
                          "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9." // 120
                          "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9." // 160
                          "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9." // 200
                          "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9." // 240
                          "0.1.2.3.4.5.6.");
    EXPECT_EQ(Name::MAX_LABELS, maxlabels.getLabelCount());
}

214
TEST_F(NameTest, fromWire) {
215
216
217
218
219
    //
    // test cases derived from BIND9 tests.
    //
    // normal case with a compression pointer
    EXPECT_PRED_FORMAT2(UnitTestUtil::matchName,
220
                        nameFactoryFromWire("name_fromWire1", 25),
221
222
                        Name("vix.com"));
    // bogus label character (looks like a local compression pointer)
223
    EXPECT_THROW(nameFactoryFromWire("name_fromWire2", 25), DNSMessageFORMERR);
224
    // a bad compression pointer (too big)
225
    EXPECT_THROW(nameFactoryFromWire("name_fromWire3_1", 25),
226
                 DNSMessageFORMERR);
227
    // forward reference
228
    EXPECT_THROW(nameFactoryFromWire("name_fromWire3_2", 25),
229
                 DNSMessageFORMERR);
230
    // invalid name length
231
    EXPECT_THROW(nameFactoryFromWire("name_fromWire4", 550), DNSMessageFORMERR);
232
233
234
235
236

    // skip test for from Wire5.  It's for disabling decompression, but our
    // implementation always allows it.

    // bad pointer (too big)
237
    EXPECT_THROW(nameFactoryFromWire("name_fromWire6", 25), DNSMessageFORMERR);
238
    // input ends unexpectedly
239
    EXPECT_THROW(nameFactoryFromWire("name_fromWire7", 25), DNSMessageFORMERR);
240
241
    // many hops of compression but valid.  should succeed.
    EXPECT_PRED_FORMAT2(UnitTestUtil::matchName,
242
                        nameFactoryFromWire("name_fromWire8", 383),
243
244
245
246
247
248
249
250
                        Name("vix.com"));

    //
    // Additional test cases
    //

    // large names, a long but valid one, and invalid (too long) one.
    EXPECT_EQ(Name::MAX_WIRE,
251
252
              nameFactoryFromWire("name_fromWire9", 0).getLength());
    EXPECT_THROW(nameFactoryFromWire("name_fromWire10", 0).getLength(),
253
                 DNSMessageFORMERR);
254
255

    // A name with possible maximum number of labels; awkward but valid
256
    EXPECT_EQ(nameFactoryFromWire("name_fromWire11", 0).getLabelCount(),
257
258
259
              Name::MAX_LABELS);

    // Wire format including an invalid label length
260
    EXPECT_THROW(nameFactoryFromWire("name_fromWire12", 0), DNSMessageFORMERR);
261
262

    // converting upper-case letters to down-case
263
264
265
    EXPECT_EQ("vix.com.",
              nameFactoryFromWire("name_fromWire1", 25, true).toText());
    EXPECT_EQ(3, nameFactoryFromWire("name_fromWire1", 25).getLabelCount());
266
267
}

268
TEST_F(NameTest, copyConstruct) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
269
270
271
272
273
274
275
276
277
278
    Name copy(example_name);
    EXPECT_EQ(copy, example_name);

    // Check the copied data is valid even after the original is deleted
    Name* copy2 = new Name(example_name);
    Name copy3(*copy2);
    delete copy2;
    EXPECT_EQ(copy3, example_name);
}

279
TEST_F(NameTest, assignment) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
280
281
282
283
284
285
286
287
288
289
290
    Name copy(".");
    copy = example_name;
    EXPECT_EQ(copy, example_name);

    // Check if the copied data is valid even after the original is deleted
    Name* copy2 = new Name(example_name);
    Name copy3(".");
    copy3 = *copy2;
    delete copy2;
    EXPECT_EQ(copy3, example_name);

291
292
293
294
    // Self assignment (via a reference to silence cppcheck)
    Name& copyref(copy);
    copyref = copy;
    EXPECT_EQ(example_name, copyref);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
295
296
}

297
TEST_F(NameTest, toText) {
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
    // tests derived from BIND9
    EXPECT_EQ("a.b.c.d", Name("a.b.c.d").toText(true));
    EXPECT_EQ("a.\\\\[[.c.d", Name("a.\\\\[\\[.c.d").toText(true));
    EXPECT_EQ("a.b.C.d.", Name("a.b.C.d").toText(false));
    EXPECT_EQ("a.b.", Name("a.b.").toText(false));

    // test omit_final_dot.  It's false by default.
    EXPECT_EQ("a.b.c.d", Name("a.b.c.d.").toText(true));
    EXPECT_EQ(Name("a.b.").toText(false), Name("a.b.").toText());

    // the root name is a special case: omit_final_dot will be ignored.
    EXPECT_EQ(".", Name(".").toText(true));

    // test all printable characters to see whether special characters are
    // escaped while the others are intact.  note that the conversion is
    // implementation specific; for example, it's not invalid to escape a
    // "normal" character such as 'a' with regard to the standard.
    string all_printable("!\\\"#\\$%&'\\(\\)*+,-\\./0123456789:\\;<=>?\\@"
                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                              "[\\\\]^_.`abcdefghijklmnopqrstuvwxyz{|}~.");
    EXPECT_EQ(all_printable,
319
              nameFactoryFromWire("name_fromWire13", 0).toText());
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340

    string all_nonprintable(
        "\\000\\001\\002\\003\\004\\005\\006\\007\\008\\009"
        "\\010\\011\\012\\013\\014\\015\\016\\017\\018\\019"
        "\\020\\021\\022\\023\\024\\025\\026\\027\\028\\029"
        "\\030\\031\\032\\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.");
    EXPECT_EQ(all_nonprintable,
341
              nameFactoryFromWire("name_fromWire14", 0).toText());
342
343
}

344
TEST_F(NameTest, toWireBuffer) {
345
346
347
348
349
    vector<unsigned char> data;
    OutputBuffer buffer(0);

    UnitTestUtil::readWireData(string("01610376697803636f6d00"), data);
    Name("a.vix.com.").toWire(buffer);
350
351
    EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, &data[0], data.size(),
                        buffer.getData(), buffer.getLength());
352
353
354
355
356
357
}

//
// We test various corner cases in Renderer tests, but add this test case
// to fill the code coverage gap.
//
358
TEST_F(NameTest, toWireRenderer) {
359
360
361
362
363
364
    vector<unsigned char> data;
    OutputBuffer buffer(0);
    MessageRenderer renderer(buffer);

    UnitTestUtil::readWireData(string("01610376697803636f6d00"), data);
    Name("a.vix.com.").toWire(renderer);
365
366
    EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, &data[0], data.size(),
                        buffer.getData(), buffer.getLength());
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
}

//
// Helper class to hold comparison test parameters.
//
struct CompareParameters {
    CompareParameters(const Name& n1, const Name&  n2,
                      NameComparisonResult::NameRelation r, int o,
                      unsigned int l) :
        name1(n1), name2(n2), reln(r), order(o), labels(l) {}
    static int normalizeOrder(int o)
    {
        if (o > 0) {
            return (1);
        } else if (o < 0) {
            return (-1);
        }
        return (0);
    }
    Name name1;
    Name name2;
    NameComparisonResult::NameRelation reln;
    int order;
    unsigned int labels;
};

393
TEST_F(NameTest, compare) {
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
    vector<CompareParameters> params;
    params.push_back(CompareParameters(Name("c.d"), Name("a.b.c.d"),
                                       NameComparisonResult::SUPERDOMAIN,
                                       -1, 3));
    params.push_back(CompareParameters(Name("a.b.c.d"), Name("c.d"),
                                       NameComparisonResult::SUBDOMAIN, 1, 3));
    params.push_back(CompareParameters(Name("a.b.c.d"), Name("c.d.e.f"),
                                       NameComparisonResult::COMMONANCESTOR,
                                       -1, 1));
    params.push_back(CompareParameters(Name("a.b.c.d"), Name("f.g.c.d"),
                                       NameComparisonResult::COMMONANCESTOR,
                                       -1, 3));
    params.push_back(CompareParameters(Name("a.b.c.d"), Name("A.b.C.d."),
                                       NameComparisonResult::EQUAL,
                                       0, 5));

    vector<CompareParameters>::const_iterator it;
    for (it = params.begin(); it != params.end(); ++it) {
        NameComparisonResult result = (*it).name1.compare((*it).name2);
        EXPECT_EQ((*it).reln, result.getRelation());
        EXPECT_EQ((*it).order,
                  CompareParameters::normalizeOrder(result.getOrder()));
        EXPECT_EQ((*it).labels, result.getCommonLabels());
    }
}

420
TEST_F(NameTest, equal) {
421
422
423
424
425
426
427
428
429
430
431
432
433
434
    EXPECT_TRUE(example_name == Name("WWW.EXAMPLE.COM."));
    EXPECT_TRUE(example_name.equals(Name("WWW.EXAMPLE.COM.")));
    EXPECT_TRUE(example_name != Name("www.example.org."));
    EXPECT_TRUE(example_name.nequals(Name("www.example.org.")));
    // lengths don't match
    EXPECT_TRUE(example_name != Name("www2.example.com."));
    EXPECT_TRUE(example_name.nequals(Name("www2.example.com.")));
    // lengths are equal, but # of labels don't match (first test checks the
    // prerequisite).
    EXPECT_EQ(example_name.getLength(), Name("www\\.example.com.").getLength());
    EXPECT_TRUE(example_name != Name("www\\.example.com."));
    EXPECT_TRUE(example_name.nequals(Name("www\\.example.com.")));
}

435
TEST_F(NameTest, isWildcard) {
436
    EXPECT_FALSE(example_name.isWildcard());
437
    EXPECT_TRUE(Name("*.a.example.com").isWildcard());
438
    EXPECT_FALSE(Name("a.*.example.com").isWildcard());
439
440
}

441
TEST_F(NameTest, concatenate) {
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
    NameComparisonResult result =
        Name("aaa.www.example.com.").compare(Name("aaa").concatenate(example_name));
    EXPECT_EQ(NameComparisonResult::EQUAL, result.getRelation());

    result = example_name.compare(Name(".").concatenate(example_name));
    EXPECT_EQ(NameComparisonResult::EQUAL, result.getRelation());

    result = example_name.compare(example_name.concatenate(Name(".")));
    EXPECT_EQ(NameComparisonResult::EQUAL, result.getRelation());

    // concatenating two valid names would result in too long a name.
    Name n1("123456789.123456789.123456789.123456789.123456789."
            "123456789.123456789.123456789.123456789.123456789."
            "123456789.123456789.123456789.123456789.123456789.");
    Name n2("123456789.123456789.123456789.123456789.123456789."
            "123456789.123456789.123456789.123456789.123456789."
            "1234.");
    EXPECT_THROW(n1.concatenate(n2), TooLongName);
}

462
TEST_F(NameTest, reverse) {
463
464
    EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.reverse(),
                        Name("com.example.www."));
JINMEI Tatuya's avatar
JINMEI Tatuya committed
465
466
    EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, Name(".").reverse(),
                        Name("."));
467
468
469
    EXPECT_PRED_FORMAT2(UnitTestUtil::matchName,
                        Name("a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s").reverse(),
                        Name("s.r.q.p.o.n.m.l.k.j.i.h.g.f.e.d.c.b.a"));
470
471
}

472
TEST_F(NameTest, split) {
473
474
475
476
477
478
479
480
481
482
483
484
485
    // normal cases with or without explicitly specifying the trailing dot.
    EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(1, 2),
                        Name("example.com."));
    EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(1, 3),
                        Name("example.com."));
    // edge cases: only the first or last label.
    EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(0, 1),
                        Name("www."));
    EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(3, 1),
                        Name("."));
    // invalid range: an exception should be thrown.
    EXPECT_THROW(example_name.split(1, 0), OutOfRange);
    EXPECT_THROW(example_name.split(2, 3), OutOfRange);
486
487
488
489
490

    // invalid range: the following parameters would cause overflow,
    // bypassing naive validation.
    EXPECT_THROW(example_name.split(1, numeric_limits<unsigned int>::max()),
                 OutOfRange);
491
492
}

493
494
495
496
497
498
499
500
501
502
503
504
TEST_F(NameTest, split_for_suffix) {
    EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(1),
                        Name("example.com"));
    EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(0),
                        example_name);
    EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(3),
                        Name("."));

    // Invalid case: the level must be less than the original label count.
    EXPECT_THROW(example_name.split(4), OutOfRange);
}

505
TEST_F(NameTest, downcase) {
506
507
508
509
510
511
512
513
514
515
516
    // usual case: all-upper case name to all-lower case
    compareInWireFormat(example_name_upper.downcase(), example_name);
    // confirm that non upper-case characters are intact
    compareInWireFormat(nameFactoryLowerCase().downcase(),
                        nameFactoryLowerCase());
    // confirm the calling object is actually modified
    example_name_upper.downcase();
    compareInWireFormat(example_name_upper, example_name);
    
}

517
TEST_F(NameTest, at) {
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
    // Confirm at() produces the exact sequence of wire-format name data
    vector<uint8_t> data;

    for (size_t i = 0; i < example_name.getLength(); i++) {
        data.push_back(example_name.at(i));
    }

    example_name.toWire(buffer_expected);
    EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
                        &data[0], data.size(),
                        buffer_expected.getData(), buffer_expected.getLength());

    // Out-of-range access: should trigger an exception.
    EXPECT_THROW(example_name.at(example_name.getLength()), OutOfRange);
}

//
// The following set of tests confirm the result of <=, <, >=, >
// The test logic is simple, and all tests are just straightforward variations
// of the first one.
//
539
TEST_F(NameTest, leq) {
540
541
542
543
544
545
546
547
548
549
550
551
552
    // small <= large is true
    EXPECT_TRUE(small_name.leq(large_name));
    EXPECT_TRUE(small_name <= large_name);

    // small <= small is true
    EXPECT_TRUE(small_name.leq(small_name));
    EXPECT_TRUE(small_name <= small_name);

    // large <= small is false
    EXPECT_FALSE(large_name.leq(small_name));
    EXPECT_FALSE(large_name <= small_name);
}

553
TEST_F(NameTest, geq) {
554
555
556
557
558
559
560
561
562
563
    EXPECT_TRUE(large_name.geq(small_name));
    EXPECT_TRUE(large_name >= small_name);

    EXPECT_TRUE(large_name.geq(large_name));
    EXPECT_TRUE(large_name >= large_name);

    EXPECT_FALSE(small_name.geq(large_name));
    EXPECT_FALSE(small_name >= large_name);
}

564
TEST_F(NameTest, lthan) {
565
566
567
568
569
570
571
572
573
574
    EXPECT_TRUE(small_name.lthan(large_name));
    EXPECT_TRUE(small_name < large_name);

    EXPECT_FALSE(small_name.lthan(small_name));
    EXPECT_FALSE(small_name < small_name);

    EXPECT_FALSE(large_name.lthan(small_name));
    EXPECT_FALSE(large_name < small_name);
}

575
TEST_F(NameTest, gthan) {
576
577
578
579
580
581
582
583
584
585
    EXPECT_TRUE(large_name.gthan(small_name));
    EXPECT_TRUE(large_name > small_name);

    EXPECT_FALSE(large_name.gthan(large_name));
    EXPECT_FALSE(large_name > large_name);

    EXPECT_FALSE(small_name.gthan(large_name));
    EXPECT_FALSE(small_name > large_name);
}

586
TEST_F(NameTest, constants) {
587
588
589
    EXPECT_EQ(Name("."), Name::ROOT_NAME());
}

590
// test operator<<.  We simply confirm it appends the result of toText().
591
TEST_F(NameTest, LeftShiftOperator) {
592
593
594
595
596
    ostringstream oss;
    oss << example_name;
    EXPECT_EQ(example_name.toText(), oss.str());
}
}