Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / test / caprotocolmessagetest.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <stdio.h>
22
23 #include "gtest/gtest.h"
24
25 #include "caprotocolmessage.h"
26
27 namespace {
28
29 static const char COAP_HEADER[] = "coap://[::]/";
30
31 class CoAPOptionCase
32 {
33 public:
34     unsigned short key;
35     unsigned int length;
36     std::string dataStr; // data could be binary... for testing we'll use str
37 };
38
39
40
41 /**
42  * Helper to validate the state of CoAP URI parsing.
43  *
44  * @param cases array of expected parse results.
45  * @param numCases number of expected parse results.
46  * @param optlist parsed option list to verify.
47  */
48 void verifyParsedOptions(CoAPOptionCase const *cases,
49                          size_t numCases,
50                          coap_list_t *optlist)
51 {
52     size_t index = 0;
53     for (coap_list_t *opt = optlist; opt; opt = opt->next)
54     {
55         coap_option *option = (coap_option *) opt->data;
56         EXPECT_TRUE(option != NULL);
57         EXPECT_LT(index, numCases);
58         if (option && (index < numCases))
59         {
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());
65
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);
70         }
71
72         index++;
73     }
74     // Ensure we saw the proper number of parts:
75     EXPECT_EQ(numCases, index);
76 }
77
78 } // namespace
79
80 TEST(CAProtocolMessage, CAParseURIBase)
81 {
82     char sampleURI[] = "coap://[::]/oc/core?rt=core.sensor&if=core.mi.ll";
83
84     CoAPOptionCase cases[] = {
85         {COAP_OPTION_URI_PATH, 2, "oc"},
86         {COAP_OPTION_URI_PATH, 4, "core"},
87         {COAP_OPTION_URI_QUERY, 14, "rt=core.sensor"},
88         {COAP_OPTION_URI_QUERY, 13, "if=core.mi.ll"},
89     };
90     size_t numCases = sizeof(cases) / sizeof(cases[0]);
91
92
93     coap_list_t *optlist = NULL;
94     CAParseURI(sampleURI, &optlist);
95
96
97     verifyParsedOptions(cases, numCases, optlist);
98 }
99
100 // Try for multiple URI path components that still total less than 128
101 TEST(CAProtocolMessage, CAParseURIManyPath)
102 {
103     char sampleURI[] = "coap://[::]"
104         "/medium/a/b/c/d/e/f/g/h/i/j/"
105         "?rt=core.sensor&if=core.mi.ll";
106
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"},
121     };
122     size_t numCases = sizeof(cases) / sizeof(cases[0]);
123
124
125     coap_list_t *optlist = NULL;
126     CAParseURI(sampleURI, &optlist);
127
128
129     verifyParsedOptions(cases, numCases, optlist);
130 }
131
132 // Try for multiple URI parameters that still total less than 128
133 TEST(CAProtocolMessage, CAParseURIManyParams)
134 {
135     char sampleURI[] = "coap://[::]/oc/core/"
136         "?rt=core.sensor&a=0&b=1&c=2&d=3&e=4&f=5&g=6&h=7&i=8&j=9";
137
138     CoAPOptionCase cases[] = {
139         {COAP_OPTION_URI_PATH, 2, "oc"},
140         {COAP_OPTION_URI_PATH, 4, "core"},
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"},
152     };
153     size_t numCases = sizeof(cases) / sizeof(cases[0]);
154
155
156     coap_list_t *optlist = NULL;
157     CAParseURI(sampleURI, &optlist);
158
159
160     verifyParsedOptions(cases, numCases, optlist);
161 }
162
163 // Test that an initial long path component won't hide latter ones.
164 TEST(CAProtocolMessage, CAParseURILongPath)
165 {
166     char sampleURI[] = "coap://[::]/oc"
167         "123456789012345678901234567890123456789012345678901234567890"
168         "12345678901234567890123456789012345678901234567890"
169         "/core?rt=core.sensor&if=core.mi.ll";
170
171     CoAPOptionCase cases[] = {
172         {COAP_OPTION_URI_PATH, 112, "oc"
173          "123456789012345678901234567890123456789012345678901234567890"
174          "12345678901234567890123456789012345678901234567890"},
175         {COAP_OPTION_URI_PATH, 4, "core"},
176         {COAP_OPTION_URI_QUERY, 14, "rt=core.sensor"},
177         {COAP_OPTION_URI_QUERY, 13, "if=core.mi.ll"},
178     };
179     size_t numCases = sizeof(cases) / sizeof(cases[0]);
180
181
182     coap_list_t *optlist = NULL;
183     CAParseURI(sampleURI, &optlist);
184
185
186     verifyParsedOptions(cases, numCases, optlist);
187 }