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"
29 static const char COAP_HEADER[] = "coap://[::]/";
36 std::string dataStr; // data could be binary... for testing we'll use str
42 * Helper to validate the state of CoAP URI parsing.
44 * @param cases array of expected parse results.
45 * @param numCases number of expected parse results.
46 * @param optlist parsed option list to verify.
48 void verifyParsedOptions(CoAPOptionCase const *cases,
53 for (coap_list_t *opt = optlist; opt; opt = opt->next)
55 coap_option *option = (coap_option *) opt->data;
56 EXPECT_TRUE(option != NULL);
57 EXPECT_LT(index, numCases);
58 if (option && (index < numCases))
60 unsigned short key = COAP_OPTION_KEY(*option);
61 unsigned int length = COAP_OPTION_LENGTH(*option);
62 std::string dataStr((const char*)COAP_OPTION_DATA(*option), length);
63 // First validate the test case:
64 EXPECT_EQ(cases[index].length, cases[index].dataStr.length());
66 // Ensure data matches expected parsing
67 EXPECT_EQ(cases[index].key, key);
68 EXPECT_EQ(cases[index].length, length);
69 EXPECT_EQ(cases[index].dataStr, dataStr);
74 // Ensure we saw the proper number of parts:
75 EXPECT_EQ(numCases, index);
80 TEST(CAProtocolMessage, CAParseURIBase)
82 char sampleURI[] = "coap://[::]/oic/res?rt=core.sensor&if=core.mi.ll";
84 CoAPOptionCase cases[] = {
85 {COAP_OPTION_URI_PATH, 3, "oic"},
86 {COAP_OPTION_URI_PATH, 3, "res"},
87 {COAP_OPTION_URI_QUERY, 14, "rt=core.sensor"},
88 {COAP_OPTION_URI_QUERY, 13, "if=core.mi.ll"},
90 size_t numCases = sizeof(cases) / sizeof(cases[0]);
93 coap_list_t *optlist = NULL;
94 CAParseURI(sampleURI, &optlist);
97 verifyParsedOptions(cases, numCases, optlist);
100 // Try for multiple URI path components that still total less than 128
101 TEST(CAProtocolMessage, CAParseURIManyPath)
103 char sampleURI[] = "coap://[::]"
104 "/medium/a/b/c/d/e/f/g/h/i/j/"
105 "?rt=core.sensor&if=core.mi.ll";
107 CoAPOptionCase cases[] = {
108 {COAP_OPTION_URI_PATH, 6, "medium"},
109 {COAP_OPTION_URI_PATH, 1, "a"},
110 {COAP_OPTION_URI_PATH, 1, "b"},
111 {COAP_OPTION_URI_PATH, 1, "c"},
112 {COAP_OPTION_URI_PATH, 1, "d"},
113 {COAP_OPTION_URI_PATH, 1, "e"},
114 {COAP_OPTION_URI_PATH, 1, "f"},
115 {COAP_OPTION_URI_PATH, 1, "g"},
116 {COAP_OPTION_URI_PATH, 1, "h"},
117 {COAP_OPTION_URI_PATH, 1, "i"},
118 {COAP_OPTION_URI_PATH, 1, "j"},
119 {COAP_OPTION_URI_QUERY, 14, "rt=core.sensor"},
120 {COAP_OPTION_URI_QUERY, 13, "if=core.mi.ll"},
122 size_t numCases = sizeof(cases) / sizeof(cases[0]);
125 coap_list_t *optlist = NULL;
126 CAParseURI(sampleURI, &optlist);
129 verifyParsedOptions(cases, numCases, optlist);
132 // Try for multiple URI parameters that still total less than 128
133 TEST(CAProtocolMessage, CAParseURIManyParams)
135 char sampleURI[] = "coap://[::]/oic/res/"
136 "?rt=core.sensor&a=0&b=1&c=2&d=3&e=4&f=5&g=6&h=7&i=8&j=9";
138 CoAPOptionCase cases[] = {
139 {COAP_OPTION_URI_PATH, 3, "oic"},
140 {COAP_OPTION_URI_PATH, 3, "res"},
141 {COAP_OPTION_URI_QUERY, 14, "rt=core.sensor"},
142 {COAP_OPTION_URI_QUERY, 3, "a=0"},
143 {COAP_OPTION_URI_QUERY, 3, "b=1"},
144 {COAP_OPTION_URI_QUERY, 3, "c=2"},
145 {COAP_OPTION_URI_QUERY, 3, "d=3"},
146 {COAP_OPTION_URI_QUERY, 3, "e=4"},
147 {COAP_OPTION_URI_QUERY, 3, "f=5"},
148 {COAP_OPTION_URI_QUERY, 3, "g=6"},
149 {COAP_OPTION_URI_QUERY, 3, "h=7"},
150 {COAP_OPTION_URI_QUERY, 3, "i=8"},
151 {COAP_OPTION_URI_QUERY, 3, "j=9"},
153 size_t numCases = sizeof(cases) / sizeof(cases[0]);
156 coap_list_t *optlist = NULL;
157 CAParseURI(sampleURI, &optlist);
160 verifyParsedOptions(cases, numCases, 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);