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
 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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 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
// Copyright (C) 2018-2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <testutils/gtest_utils.h>
#include <yang/adaptor_host.h>

using namespace std;
using namespace isc;
using namespace isc::data;
using namespace isc::yang;

namespace {

// Verifies that quoteIdentifier does not touch an identifier which
// has a type different from flex-d.
TEST(AdaptorHostTest, notFlexId) {
    string config = "{\n"
        " \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        " \"ip-address\": \"192.0.2.201\"\n"
        "}";
    ElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    ConstElementPtr copied = copy(json);
    EXPECT_NO_THROW_LOG(AdaptorHost::quoteIdentifier(json));
    EXPECT_TRUE(copied->equals(*json));
}

// Verifies that quoteIdentifier does not touch a flex-id identifier
// without quotes.
TEST(AdaptorHostTest, noQuote) {<--- syntax error
    string config = "{\n"
        " \"flex-id\": \"s0mEVaLue\",\n"
        " \"ip-address\": \"192.0.2.206\"\n"
        "}";
    ElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    ConstElementPtr copied = copy(json);
    EXPECT_NO_THROW_LOG(AdaptorHost::quoteIdentifier(json));
    EXPECT_TRUE(copied->equals(*json));
}

// Verifies that quoteIdentifier removes quotes from a flex-id identifier.
TEST(AdaptorHostTest, quotes) {
    string config = "{\n"
        " \"flex-id\": \"'somevalue'\",\n"
        " \"ip-addresses\": \"2001:db8:1:cafe::2\"\n"
        "}";
    ElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    ConstElementPtr copied = copy(json);
    EXPECT_NO_THROW_LOG(AdaptorHost::quoteIdentifier(json));
    EXPECT_FALSE(copied->equals(*json));
    ConstElementPtr id = json->get("flex-id");
    ASSERT_TRUE(id);
    ASSERT_EQ(Element::string, id->getType());
    EXPECT_EQ("73:6f:6d:65:76:61:6c:75:65", id->stringValue());
}

// Verifies that quoteIdentifier removes quotes from a flex-id identifier
// but does not interpret a quote in the middle.
TEST(AdaptorHostTest, extraQuote) {
    string config = "{\n"
        " \"flex-id\": \"'some'value'\",\n"
        " \"ip-addresses\": \"2001:db8:1:cafe::2\"\n"
        "}";
    ElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    ConstElementPtr copied = copy(json);
    EXPECT_NO_THROW_LOG(AdaptorHost::quoteIdentifier(json));
    EXPECT_FALSE(copied->equals(*json));
    ConstElementPtr id = json->get("flex-id");
    ASSERT_TRUE(id);
    ASSERT_EQ(Element::string, id->getType());
    EXPECT_EQ("73:6f:6d:65:27:76:61:6c:75:65", id->stringValue());
}

// Verifies that quoteIdentifier works on not standard characters too.
TEST(AdaptorHostTest, notStandard) {
    string config = "{\n"
        " \"flex-id\": \"'some\\\"value'\",\n"
        " \"ip-addresses\": \"2001:db8:1:cafe::2\"\n"
        "}";
    ElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    ConstElementPtr copied = copy(json);
    EXPECT_NO_THROW_LOG(AdaptorHost::quoteIdentifier(json));
    EXPECT_FALSE(copied->equals(*json));
    ConstElementPtr id = json->get("flex-id");
    ASSERT_TRUE(id);
    ASSERT_EQ(Element::string, id->getType());
    EXPECT_EQ("73:6f:6d:65:22:76:61:6c:75:65", id->stringValue());
}

// Verifies that quoteIdentifier works on not standard characters too
// even without quotes.
TEST(AdaptorHostTest, notQuoted) {
    string config = "{\n"
        " \"flex-id\": \"some\\\"value\",\n"
        " \"ip-addresses\": \"2001:db8:1:cafe::2\"\n"
        "}";
    ElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    ConstElementPtr copied = copy(json);
    EXPECT_NO_THROW_LOG(AdaptorHost::quoteIdentifier(json));
    EXPECT_FALSE(copied->equals(*json));
    ConstElementPtr id = json->get("flex-id");
    ASSERT_TRUE(id);
    ASSERT_EQ(Element::string, id->getType());
    EXPECT_EQ("73:6f:6d:65:22:76:61:6c:75:65", id->stringValue());
}

}  // namespace