sensord: check validation of sensor URI to create a sensor provider 32/127232/4
authorkibak.yoon <kibak.yoon@samsung.com>
Wed, 26 Apr 2017 13:01:39 +0000 (22:01 +0900)
committerKibak Yoon <kibak.yoon@samsung.com>
Thu, 27 Apr 2017 03:42:53 +0000 (03:42 +0000)
- URI format : http://<vendor>/sensor/<sensor_type>/<sensor_name>
- Apps are not allowed to create a sensor of the type in sensor_type_e.

- Allowed:
  - "http://example.org/sensor/mysensor_type/mysensor"
  - "http://developer.samsung.com/sensor/mysensor_type/mysensor"
- Disallowed:
  - "http://tizen.org/sensor/accelerometer/mysensor"      (predefined type)
  - "http://tizen.org/mysensor/accelerometer/mysensor"    (syntax error)
  - "http:/tizen.org/accelerometer/mysensor"              (syntax error)
  - "http:/example.org/sensor/mysensor/mysensor/mysensor" (syntax error)

- test command
  $ sensorctl test auto provider_uri

Change-Id: I0ce36a9c2a5a7953b975917962d50cf39c863bb3
Signed-off-by: kibak.yoon <kibak.yoon@samsung.com>
src/client/sensor_internal.cpp
src/sensorctl/testcase/unit_client.cpp
src/sensorctl/testcase/unit_provider.cpp [new file with mode: 0644]
src/shared/sensor_types_private.h [new file with mode: 0644]

index 31af7d6..dcd497a 100644 (file)
@@ -20,6 +20,7 @@
 #include <sensor_internal.h>
 #include <sensor_internal_deprecated.h>
 #include <sensor_types.h>
+#include <sensor_types_private.h>
 #include <sensor_utils.h>
 
 #include <channel_handler.h>
@@ -28,6 +29,7 @@
 #include <sensor_provider.h>
 #include <sensor_log.h>
 #include <unordered_map>
+#include <regex>
 
 #define CONVERT_OPTION_PAUSE_POLICY(option) ((option) ^ 0b11)
 
@@ -592,6 +594,14 @@ API int sensord_create_provider(const char *uri, sensord_provider_h *provider)
 {
        retvm_if(!provider, -EINVAL, "Invalid paramter");
 
+       std::string str_uri(uri);
+       retvm_if(str_uri.find(PREDEFINED_TYPE_URI) != std::string::npos,
+                       -EINVAL, "Invalid URI format[%s]", uri);
+
+       static std::regex uri_regex(SENSOR_URI_REGEX, std::regex::optimize);
+       retvm_if(!std::regex_match(uri, uri_regex),
+                       -EINVAL, "Invalid URI format[%s]", uri);
+
        sensor_provider *p;
 
        p = new(std::nothrow) sensor_provider(uri);
index 661e4a0..28900bb 100644 (file)
@@ -174,7 +174,7 @@ static gboolean publish(gpointer gdata)
 /* TODO: change it from manual test to auto-test */
 TESTCASE(sensor_api_mysensor_provider, provider_p_1)
 {
-       const char *uri = "http://example.org/mysensor/mysensor";
+       const char *uri = "http://example.org/sensor/mysensor/mysensor";
        const char *name = "mysensor";
        const char *vendor = "tizen";
 
@@ -220,7 +220,7 @@ TESTCASE(sensor_api_mysensor_provider, provider_p_1)
 
 TESTCASE(sensor_api_mysensor_listener, listener_p_1)
 {
-       const char *uri = "http://example.org/mysensor/mysensor";
+       const char *uri = "http://example.org/sensor/mysensor/mysensor";
        int err;
        bool ret;
        int handle;
@@ -259,7 +259,7 @@ TESTCASE(sensor_api_mysensor_listener, listener_p_1)
 
 static void add_mysensor(void)
 {
-       const char *uri = "http://example.org/mysensor/mysensor";
+       const char *uri = "http://example.org/sensor/mysensor/mysensor";
        const char *name = "mysensor";
        const char *vendor = "tizen";
 
diff --git a/src/sensorctl/testcase/unit_provider.cpp b/src/sensorctl/testcase/unit_provider.cpp
new file mode 100644 (file)
index 0000000..1914652
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * sensorctl
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <unistd.h>
+#include <string.h>
+#include <sensor_internal.h>
+
+#include "log.h"
+#include "mainloop.h"
+#include "test_bench.h"
+
+TESTCASE(sensor_api_provider_uri, provider_check_uri)
+{
+       int err;
+       sensord_provider_h provider;
+
+       const char *uri_p1 = "http://example.org/sensor/mysensor_type/mysensor";
+       const char *uri_p2 = "http://developer.samsung.com/sensor/mysensor_type/mysensor";
+       const char *uri_n1 = "http://tizen.org/sensor/accelerometer/mysensor";
+       const char *uri_n2 = "http://tizen.org/mysensor/accelerometer/mysensor";
+       const char *uri_n3 = "http:/example.org/sensor/mysensor_type/mysensor";
+       const char *uri_n5 = "http://example.org/sensor/mysensor_type";
+       const char *uri_n4 = "http://example.org/sensor/mysensor_type/mysensor/mysensor";
+
+       err = sensord_create_provider(uri_p1, &provider);
+       EXPECT_EQ(err, 0);
+       err = sensord_create_provider(uri_p2, &provider);
+       EXPECT_EQ(err, 0);
+       err = sensord_create_provider(uri_n1, &provider);
+       EXPECT_EQ(err, -EINVAL);
+       err = sensord_create_provider(uri_n2, &provider);
+       EXPECT_EQ(err, -EINVAL);
+       err = sensord_create_provider(uri_n3, &provider);
+       EXPECT_EQ(err, -EINVAL);
+       err = sensord_create_provider(uri_n4, &provider);
+       EXPECT_EQ(err, -EINVAL);
+       err = sensord_create_provider(uri_n5, &provider);
+       EXPECT_EQ(err, -EINVAL);
+
+       return true;
+}
diff --git a/src/shared/sensor_types_private.h b/src/shared/sensor_types_private.h
new file mode 100644 (file)
index 0000000..17fcd02
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * sensord
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef __SENSOR_TYPES_PRIVATE__
+#define __SENSOR_TYPES_PRIVATE__
+
+#define URI_REGEX(CATEGORY) R"~(^http:\/\/[\w-]+(\.[\w-]+)*\/)~" CATEGORY R"~(\/[\w-]+(\.[\w-]+)*(\/[\w-]+(\.[\w-]+)*)$)~"
+#define SENSOR_URI_REGEX URI_REGEX("sensor")
+
+#define PREDEFINED_TYPE_URI "http://tizen.org/sensor/"
+
+#endif /* __SENSOR_TYPES_PRIVATE__ */