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 SuccessOrQuit(networkName.Set(MeshCoP::NameData(kEmptyName, 0)));
76 CompareNetworkName(networkName, kEmptyName);
77
78 SuccessOrQuit(networkName.Set(MeshCoP::NameData(kLongName, sizeof(kLongName))));
79 CompareNetworkName(networkName, kLongName);
80
81 VerifyOrQuit(networkName.Set(MeshCoP::NameData(kLongName, sizeof(kLongName) - 1)) == kErrorAlready,
82 "failed to detect duplicate");
83
84 SuccessOrQuit(networkName.Set(MeshCoP::NameData(kName1, sizeof(kName1))));
85
86 VerifyOrQuit(networkName.Set(MeshCoP::NameData(kTooLongName, sizeof(kTooLongName))) == kErrorInvalidArgs,
87 "accepted an invalid (too long) name");
88
89 CompareNetworkName(networkName, kName1);
90
91 memset(buffer, 'a', sizeof(buffer));
92 len = networkName.GetAsData().CopyTo(buffer, 1);
93 VerifyOrQuit(len == 1, "NameData::CopyTo() failed");
94 VerifyOrQuit(buffer[0] == kName1[0], "NameData::CopyTo() failed");
95 VerifyOrQuit(buffer[1] == 'a', "NameData::CopyTo() failed");
96
97 memset(buffer, 'a', sizeof(buffer));
98 len = networkName.GetAsData().CopyTo(buffer, sizeof(kName1) - 1);
99 VerifyOrQuit(len == sizeof(kName1) - 1, "NameData::CopyTo() failed");
100 VerifyOrQuit(memcmp(buffer, kName1, sizeof(kName1) - 1) == 0, "NameData::CopyTo() failed");
101 VerifyOrQuit(buffer[sizeof(kName1)] == 'a', "NameData::CopyTo() failed");
102
103 memset(buffer, 'a', sizeof(buffer));
104 len = networkName.GetAsData().CopyTo(buffer, sizeof(buffer));
105 VerifyOrQuit(len == sizeof(kName1) - 1, "NameData::CopyTo() failed");
106 VerifyOrQuit(memcmp(buffer, kName1, sizeof(kName1) - 1) == 0, "NameData::CopyTo() failed");
107 VerifyOrQuit(buffer[sizeof(kName1)] == 0, "NameData::CopyTo() failed");
108
109 SuccessOrQuit(networkName2.Set(MeshCoP::NameData(kName1, sizeof(kName1))));
110 VerifyOrQuit(networkName == networkName2);
111
112 SuccessOrQuit(networkName2.Set(kName2));
113 VerifyOrQuit(networkName != networkName2);
114 }
115
116 } // namespace ot
117
main(void)118 int main(void)
119 {
120 ot::TestNetworkName();
121 printf("All tests passed\n");
122 return 0;
123 }
124