Imported Upstream version 0.9.2
[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 class CoAPOptionCase
30 {
31 public:
32     unsigned short key;
33     unsigned int length;
34     std::string dataStr; // data could be binary... for testing we'll use str
35 };
36
37
38
39 /**
40  * Helper to validate the state of CoAP URI parsing.
41  *
42  * @param cases array of expected parse results.
43  * @param numCases number of expected parse results.
44  * @param optlist parsed option list to verify.
45  */
46 void verifyParsedOptions(CoAPOptionCase const *cases,
47                          size_t numCases,
48                          coap_list_t *optlist)
49 {
50     size_t index = 0;
51     for (coap_list_t *opt = optlist; opt; opt = opt->next)
52     {
53         coap_option *option = (coap_option *) opt->data;
54         EXPECT_TRUE(option != NULL);
55         EXPECT_LT(index, numCases);
56         if (option && (index < numCases))
57         {
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());
63
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);
68         }
69
70         index++;
71     }
72     // Ensure we saw the proper number of parts:
73     EXPECT_EQ(numCases, index);
74 }
75
76 } // namespace
77
78 TEST(CAProtocolMessage, CAParseURIBase)
79 {
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"},
86     };
87     size_t numCases = sizeof(cases) / sizeof(cases[0]);
88
89
90     coap_list_t *optlist = NULL;
91     CAParseURI(sampleURI, &optlist);
92
93
94     verifyParsedOptions(cases, numCases, optlist);
95     coap_delete_list(optlist);
96 }
97
98 // Try for multiple URI path components that still total less than 128
99 TEST(CAProtocolMessage, CAParseURIManyPath)
100 {
101     char sampleURI[] = "coap://[::]"
102         "/medium/a/b/c/d/e/f/g/h/i/j/"
103         "?rt=core.sensor;if=core.mi.ll";
104
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"},
119     };
120     size_t numCases = sizeof(cases) / sizeof(cases[0]);
121
122
123     coap_list_t *optlist = NULL;
124     CAParseURI(sampleURI, &optlist);
125
126
127     verifyParsedOptions(cases, numCases, optlist);
128     coap_delete_list(optlist);
129 }
130
131 // Try for multiple URI parameters that still total less than 128
132 TEST(CAProtocolMessage, CAParseURIManyParams)
133 {
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";
136
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"},
151     };
152     size_t numCases = sizeof(cases) / sizeof(cases[0]);
153
154
155     coap_list_t *optlist = NULL;
156     CAParseURI(sampleURI, &optlist);
157
158
159     verifyParsedOptions(cases, numCases, optlist);
160     coap_delete_list(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://[::]/oic"
167         "123456789012345678901234567890123456789012345678901234567890"
168         "12345678901234567890123456789012345678901234567890"
169         "/res?rt=core.sensor;if=core.mi.ll";
170
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"},
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     coap_delete_list(optlist);
188 }