1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "gtest/gtest.h"
25 #include "caprotocolmessage.h"
34 std::string dataStr; // data could be binary... for testing we'll use str
40 * Helper to validate the state of CoAP URI parsing.
42 * @param cases array of expected parse results.
43 * @param numCases number of expected parse results.
44 * @param optlist parsed option list to verify.
46 void verifyParsedOptions(CoAPOptionCase const *cases,
51 for (coap_list_t *opt = optlist; opt; opt = opt->next)
53 coap_option *option = (coap_option *) opt->data;
54 EXPECT_TRUE(option != NULL);
55 EXPECT_LT(index, numCases);
56 if (option && (index < numCases))
58 unsigned short key = COAP_OPTION_KEY(*option);
59 unsigned int length = COAP_OPTION_LENGTH(*option);
60 std::string dataStr((const char*)COAP_OPTION_DATA(*option), length);
61 // First validate the test case:
62 EXPECT_EQ(cases[index].length, cases[index].dataStr.length());
64 // Ensure data matches expected parsing
65 EXPECT_EQ(cases[index].key, key);
66 EXPECT_EQ(cases[index].length, length);
67 EXPECT_EQ(cases[index].dataStr, dataStr);
72 // Ensure we saw the proper number of parts:
73 EXPECT_EQ(numCases, index);
78 TEST(CAProtocolMessage, CAParseURIBase)
80 char sampleURI[] = "coap://[::]/oic/res?rt=core.sensor&if=core.mi.ll";
81 CoAPOptionCase cases[] = {
82 {COAP_OPTION_URI_PATH, 3, "oic"},
83 {COAP_OPTION_URI_PATH, 3, "res"},
84 {COAP_OPTION_URI_QUERY, 14, "rt=core.sensor"},
85 {COAP_OPTION_URI_QUERY, 13, "if=core.mi.ll"},
87 size_t numCases = sizeof(cases) / sizeof(cases[0]);
90 coap_list_t *optlist = NULL;
91 CAParseURI(sampleURI, &optlist);
94 verifyParsedOptions(cases, numCases, optlist);
95 coap_delete_list(optlist);
98 // Try for multiple URI path components that still total less than 128
99 TEST(CAProtocolMessage, CAParseURIManyPath)
101 char sampleURI[] = "coap://[::]"
102 "/medium/a/b/c/d/e/f/g/h/i/j"
103 "?rt=core.sensor&if=core.mi.ll";
105 CoAPOptionCase cases[] = {
106 {COAP_OPTION_URI_PATH, 6, "medium"},
107 {COAP_OPTION_URI_PATH, 1, "a"},
108 {COAP_OPTION_URI_PATH, 1, "b"},
109 {COAP_OPTION_URI_PATH, 1, "c"},
110 {COAP_OPTION_URI_PATH, 1, "d"},
111 {COAP_OPTION_URI_PATH, 1, "e"},
112 {COAP_OPTION_URI_PATH, 1, "f"},
113 {COAP_OPTION_URI_PATH, 1, "g"},
114 {COAP_OPTION_URI_PATH, 1, "h"},
115 {COAP_OPTION_URI_PATH, 1, "i"},
116 {COAP_OPTION_URI_PATH, 1, "j"},
117 {COAP_OPTION_URI_QUERY, 14, "rt=core.sensor"},
118 {COAP_OPTION_URI_QUERY, 13, "if=core.mi.ll"},
120 size_t numCases = sizeof(cases) / sizeof(cases[0]);
123 coap_list_t *optlist = NULL;
124 CAParseURI(sampleURI, &optlist);
127 verifyParsedOptions(cases, numCases, optlist);
128 coap_delete_list(optlist);
131 // Try for multiple URI parameters that still total less than 128
132 TEST(CAProtocolMessage, CAParseURIManyParams)
134 char sampleURI[] = "coap://[::]/oic/res"
135 "?rt=core.sensor&a=0&b=1&c=2&d=3&e=4&f=5&g=6&h=7&i=8&j=9";
137 CoAPOptionCase cases[] = {
138 {COAP_OPTION_URI_PATH, 3, "oic"},
139 {COAP_OPTION_URI_PATH, 3, "res"},
140 {COAP_OPTION_URI_QUERY, 14, "rt=core.sensor"},
141 {COAP_OPTION_URI_QUERY, 3, "a=0"},
142 {COAP_OPTION_URI_QUERY, 3, "b=1"},
143 {COAP_OPTION_URI_QUERY, 3, "c=2"},
144 {COAP_OPTION_URI_QUERY, 3, "d=3"},
145 {COAP_OPTION_URI_QUERY, 3, "e=4"},
146 {COAP_OPTION_URI_QUERY, 3, "f=5"},
147 {COAP_OPTION_URI_QUERY, 3, "g=6"},
148 {COAP_OPTION_URI_QUERY, 3, "h=7"},
149 {COAP_OPTION_URI_QUERY, 3, "i=8"},
150 {COAP_OPTION_URI_QUERY, 3, "j=9"},
152 size_t numCases = sizeof(cases) / sizeof(cases[0]);
155 coap_list_t *optlist = NULL;
156 CAParseURI(sampleURI, &optlist);
159 verifyParsedOptions(cases, numCases, optlist);
160 coap_delete_list(optlist);
163 // Test that an initial long path component won't hide latter ones.
164 TEST(CAProtocolMessage, CAParseURILongPath)
166 char sampleURI[] = "coap://[::]/oic"
167 "123456789012345678901234567890123456789012345678901234567890"
168 "12345678901234567890123456789012345678901234567890"
169 "/res?rt=core.sensor&if=core.mi.ll";
171 CoAPOptionCase cases[] = {
172 {COAP_OPTION_URI_PATH, 113, "oic"
173 "123456789012345678901234567890123456789012345678901234567890"
174 "12345678901234567890123456789012345678901234567890"},
175 {COAP_OPTION_URI_PATH, 3, "res"},
176 {COAP_OPTION_URI_QUERY, 14, "rt=core.sensor"},
177 {COAP_OPTION_URI_QUERY, 13, "if=core.mi.ll"},
179 size_t numCases = sizeof(cases) / sizeof(cases[0]);
182 coap_list_t *optlist = NULL;
183 CAParseURI(sampleURI, &optlist);
186 verifyParsedOptions(cases, numCases, optlist);
187 coap_delete_list(optlist);
190 TEST(CAProtocolMessage, CAGetTokenFromPDU)
192 CAEndpoint_t tempRep;
193 memset(&tempRep, 0, sizeof(CAEndpoint_t));
194 tempRep.flags = CA_DEFAULT_FLAGS;
195 tempRep.adapter = CA_ADAPTER_IP;
198 coap_pdu_t *pdu = NULL;
199 coap_list_t *options = NULL;
200 coap_transport_t transport = COAP_UDP;
203 memset(&inData, 0, sizeof(CAInfo_t));
204 inData.token = (CAToken_t)"token";
205 inData.tokenLength = strlen(inData.token);
206 inData.type = CA_MSG_NONCONFIRM;
208 pdu = CAGeneratePDU(CA_GET, &inData, &tempRep, &options, &transport);
211 memset(&outData, 0, sizeof(CAInfo_t));
212 outData.type = CA_MSG_NONCONFIRM;
214 EXPECT_EQ(CA_STATUS_OK, CAGetTokenFromPDU(pdu->transport_hdr, &outData, &tempRep));