• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2022, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "common/code_utils.hpp"
30 #include "common/debug.hpp"
31 #include "meshcop/network_name.hpp"
32 
33 #include "test_util.h"
34 
35 namespace ot {
36 
CompareNetworkName(const MeshCoP::NetworkName & aNetworkName,const char * aNameString)37 void CompareNetworkName(const MeshCoP::NetworkName &aNetworkName, const char *aNameString)
38 {
39     uint8_t len = static_cast<uint8_t>(strlen(aNameString));
40 
41     VerifyOrQuit(strcmp(aNetworkName.GetAsCString(), aNameString) == 0);
42 
43     VerifyOrQuit(aNetworkName.GetAsData().GetLength() == len);
44     VerifyOrQuit(memcmp(aNetworkName.GetAsData().GetBuffer(), aNameString, len) == 0);
45 }
46 
TestNetworkName(void)47 void TestNetworkName(void)
48 {
49     const char kEmptyName[]   = "";
50     const char kName1[]       = "network";
51     const char kName2[]       = "network-name";
52     const char kLongName[]    = "0123456789abcdef";
53     const char kTooLongName[] = "0123456789abcdef0";
54 
55     char                 buffer[sizeof(kTooLongName) + 2];
56     uint8_t              len;
57     MeshCoP::NetworkName networkName;
58     MeshCoP::NetworkName networkName2;
59 
60     CompareNetworkName(networkName, kEmptyName);
61 
62     SuccessOrQuit(networkName.Set(MeshCoP::NameData(kName1, sizeof(kName1))));
63     CompareNetworkName(networkName, kName1);
64 
65     VerifyOrQuit(networkName.Set(MeshCoP::NameData(kName1, sizeof(kName1))) == kErrorAlready,
66                  "failed to detect duplicate");
67     CompareNetworkName(networkName, kName1);
68 
69     VerifyOrQuit(networkName.Set(MeshCoP::NameData(kName1, sizeof(kName1) - 1)) == kErrorAlready,
70                  "failed to detect duplicate");
71 
72     SuccessOrQuit(networkName.Set(MeshCoP::NameData(kName2, sizeof(kName2))));
73     CompareNetworkName(networkName, kName2);
74 
75     VerifyOrQuit(networkName.Set(MeshCoP::NameData(kEmptyName, 0)) == kErrorInvalidArgs);
76 
77     SuccessOrQuit(networkName.Set(MeshCoP::NameData(kLongName, sizeof(kLongName))));
78     CompareNetworkName(networkName, kLongName);
79 
80     VerifyOrQuit(networkName.Set(MeshCoP::NameData(kLongName, sizeof(kLongName) - 1)) == kErrorAlready,
81                  "failed to detect duplicate");
82 
83     VerifyOrQuit(networkName.Set(kEmptyName) == kErrorInvalidArgs);
84 
85     SuccessOrQuit(networkName.Set(MeshCoP::NameData(kName1, sizeof(kName1))));
86 
87     VerifyOrQuit(networkName.Set(MeshCoP::NameData(kTooLongName, sizeof(kTooLongName))) == kErrorInvalidArgs,
88                  "accepted an invalid (too long) name");
89 
90     CompareNetworkName(networkName, kName1);
91 
92     memset(buffer, 'a', sizeof(buffer));
93     len = networkName.GetAsData().CopyTo(buffer, 1);
94     VerifyOrQuit(len == 1, "NameData::CopyTo() failed");
95     VerifyOrQuit(buffer[0] == kName1[0], "NameData::CopyTo() failed");
96     VerifyOrQuit(buffer[1] == 'a', "NameData::CopyTo() failed");
97 
98     memset(buffer, 'a', sizeof(buffer));
99     len = networkName.GetAsData().CopyTo(buffer, sizeof(kName1) - 1);
100     VerifyOrQuit(len == sizeof(kName1) - 1, "NameData::CopyTo() failed");
101     VerifyOrQuit(memcmp(buffer, kName1, sizeof(kName1) - 1) == 0, "NameData::CopyTo() failed");
102     VerifyOrQuit(buffer[sizeof(kName1)] == 'a', "NameData::CopyTo() failed");
103 
104     memset(buffer, 'a', sizeof(buffer));
105     len = networkName.GetAsData().CopyTo(buffer, sizeof(buffer));
106     VerifyOrQuit(len == sizeof(kName1) - 1, "NameData::CopyTo() failed");
107     VerifyOrQuit(memcmp(buffer, kName1, sizeof(kName1) - 1) == 0, "NameData::CopyTo() failed");
108     VerifyOrQuit(buffer[sizeof(kName1)] == 0, "NameData::CopyTo() failed");
109 
110     SuccessOrQuit(networkName2.Set(MeshCoP::NameData(kName1, sizeof(kName1))));
111     VerifyOrQuit(networkName == networkName2);
112 
113     SuccessOrQuit(networkName2.Set(kName2));
114     VerifyOrQuit(networkName != networkName2);
115 }
116 
117 } // namespace ot
118 
main(void)119 int main(void)
120 {
121     ot::TestNetworkName();
122     printf("All tests passed\n");
123     return 0;
124 }
125