From 724eca71433e5ccba05cae08cd32842c3372430d Mon Sep 17 00:00:00 2001
From: Guilherme Arthur Geronimo <guilherme.geronimo@ufsc.br>
Date: Thu, 27 Jun 2024 18:10:15 -0300
Subject: [PATCH] Validating UNIT values on Series Creation. Fwqixed #28

---
 bin/smartdata/Backend.php  | 10 +++++-----
 bin/smartdata/SmartAPI.php | 20 +++++++-------------
 bin/smartdata/Unit.php     | 10 ++++++++++
 3 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/bin/smartdata/Backend.php b/bin/smartdata/Backend.php
index b69a98d..858d640 100644
--- a/bin/smartdata/Backend.php
+++ b/bin/smartdata/Backend.php
@@ -447,7 +447,6 @@ final class Credentials
 class Backend_V1_0 extends Backend_Common
 {
     // Create a new series if not exist
-    //public function create(Series $series) : bool {
     public function create($series) {
         $sucess = true;
         $parameters = array(':version'  => $series->version,
@@ -460,7 +459,8 @@ class Backend_V1_0 extends Backend_Common
                             ':t1'       => $series->t1,
                             ':domain'   => $this->_domain,
                             ':count'    => Config::config()::CASSANDRA_MAX_ROW_SIZE);
-        try {
+        try {    
+            Unit::validate( $series->unit );
             $conn = self::_mysqlConnect(Config::config()::MYSQL_SEVERNAME, Config::config()::MYSQL_PORT, Config::config()::MYSQL_DBNAME, Config::config()::MYSQL_USERNAME, Config::config()::MYSQL_PASSWORD);
             $table = Config::config()::MySQL_Table($series->version);
             $stmt_select = $conn->prepare("SELECT * FROM {$table} WHERE version = :version
@@ -1658,6 +1658,7 @@ class Backend_V1_1 extends Backend_Common
                             ':count'    => $series->count,
                             ':uncertainty' => $series->uncertainty);
         try {
+            Unit::validate( $series->unit );
             // As there can be OPEN series (with t0 = 0, waiting first event, or t1 = 99999999999999, wainting to be finished) NO new series should be created until they are closed.
             $conn = self::_mysqlConnect(Config::config()::MYSQL_SEVERNAME, Config::config()::MYSQL_PORT, Config::config()::MYSQL_DBNAME, Config::config()::MYSQL_USERNAME, Config::config()::MYSQL_PASSWORD);
             $table = Config::config()::MySQL_Table($series->version);
@@ -1776,9 +1777,8 @@ class Backend_V1_1 extends Backend_Common
                             ':type'      => $tracker->type ?? 'OLD');
 
         try {
-            if (!property_exists($tracker,"signature") || $tracker->signature === 0) {
-                throw new Exception\InternalException('Signature must be provided in Mobile SmartData.');
-            }
+            if (!property_exists($tracker,"signature") || $tracker->signature === 0) throw new Exception\InternalException('Signature must be provided in Mobile SmartData.');
+            Unit::validate( $tracker->unit );
             $conn = self::_mysqlConnect(Config::config()::MYSQL_SEVERNAME, Config::config()::MYSQL_PORT, Config::config()::MYSQL_DBNAME, Config::config()::MYSQL_USERNAME, Config::config()::MYSQL_PASSWORD);
             $table = Config::config()::MySQL_Table($tracker->version);
             $stmt_select = $conn->prepare("SELECT * FROM {$table} WHERE
diff --git a/bin/smartdata/SmartAPI.php b/bin/smartdata/SmartAPI.php
index 3864931..9ab795b 100644
--- a/bin/smartdata/SmartAPI.php
+++ b/bin/smartdata/SmartAPI.php
@@ -213,19 +213,13 @@ namespace SmartData\SmartAPI
     {
         $json = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);
         $last_error = json_last_error();
-        if ($last_error === JSON_ERROR_NONE) {
-            list($credentials,$series,$params) = JsonAPI::parse_create($json);
-        } else {
-            list($credentials,$series,$params) = BinaryAPI::parse_create($content);
-        }
-        if ($series instanceof Series) {
-            $backend = new Backend($credentials);
-            if(!$backend->create($series, ...$params)) {
-                throw new CreationFailedException("Creation Failed");
-            } 
-        } else {
-            throw new BadRequestException("Error parsing content request: invalid series");
-        }
+
+        list($credentials,$series,$params) = ($last_error === JSON_ERROR_NONE) ? JsonAPI::parse_create($json) : BinaryAPI::parse_create($content);
+    
+        if ( ! $series instanceof Series) throw new BadRequestException("Error parsing content request: invalid series");
+        
+        $backend = new Backend($credentials);
+        if( ! $backend->create($series, ...$params) ) throw new CreationFailedException("Creation Failed");
     }
 
     function attach($content)
diff --git a/bin/smartdata/Unit.php b/bin/smartdata/Unit.php
index 68f2dec..ef3238b 100644
--- a/bin/smartdata/Unit.php
+++ b/bin/smartdata/Unit.php
@@ -84,6 +84,16 @@ abstract class Unit
         return self::interpret(Unpack::uInt32($bin, true));
     }
 
+    public static function is_valid($u) : bool
+    {
+        return ( $u >= 0 || $u <= 4294967295 ) ;
+    }
+
+    public static function validate($u)
+    {
+        if ( ! self::is_valid($u) ) throw new Exception\InternalException("Unit value not allowed. Has to be [0~4294967295]");
+    }
+
     public static function is_digital_unit($u) : bool
     {
         return (($u & (1 << 31)) == self::DIGITAL);    
-- 
GitLab