name_unittest.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #include <dns/exceptions.h>
  16. #include <dns/buffer.h>
  17. #include <dns/name.h>
  18. #include <gtest/gtest.h>
  19. namespace {
  20. using isc::dns::Name;
  21. using isc::dns::DNSLabelTooLong;
  22. // The fixture for testing class Name.
  23. class NameTest : public ::testing::Test {
  24. protected:
  25. NameTest()
  26. {
  27. example_name =
  28. new isc::dns::Name("www.example.com");
  29. //01234567890123456 => length should be 17.
  30. }
  31. virtual ~NameTest()
  32. {
  33. delete example_name;
  34. }
  35. isc::dns::Name *example_name;
  36. };
  37. TEST_F(NameTest, getLength)
  38. {
  39. EXPECT_EQ(17, static_cast<int>(example_name->getLength()));
  40. }
  41. TEST_F(NameTest, toText)
  42. {
  43. EXPECT_EQ(std::string("www.example.com"), example_name->toText(true));
  44. EXPECT_EQ(std::string("www.example.com."), example_name->toText(false));
  45. }
  46. TEST_F(NameTest, invalid_label)
  47. {
  48. EXPECT_THROW(Name invalidlabel("012345678901234567890123456789"
  49. "0123456789012345678901234567890123456789"),
  50. DNSLabelTooLong);
  51. }
  52. TEST_F(NameTest, equals)
  53. {
  54. EXPECT_EQ(true, example_name->equals(Name("WWW.EXAMPLE.COM")));
  55. EXPECT_EQ(true, example_name->equals(Name("www.example.com.")));
  56. }
  57. TEST_F(NameTest, operator_equals)
  58. {
  59. EXPECT_EQ(true, *example_name == Name("WWW.EXAMPLE.COM"));
  60. EXPECT_EQ(true, *example_name == Name("www.example.com."));
  61. }
  62. TEST_F(NameTest, toFromWire)
  63. {
  64. isc::SingleBuffer buffer;
  65. isc::dns::NameCompressor compressor;
  66. isc::dns::NameDecompressor decompressor;
  67. example_name->toWire(buffer, compressor);
  68. buffer.setCurrent(0);
  69. EXPECT_EQ(std::string("www.example.com."),
  70. Name(buffer, decompressor).toText(false));
  71. }
  72. }