Adds UriParser::Parse and unit tests
authorVince Harron <vharron@google.com>
Tue, 6 Jan 2015 23:38:24 +0000 (23:38 +0000)
committerVince Harron <vharron@google.com>
Tue, 6 Jan 2015 23:38:24 +0000 (23:38 +0000)
This can be used to parse URIs passed to 'platform connect'

Differential Revision: http://reviews.llvm.org/D6858

llvm-svn: 225317

lldb/gtest/gtest.xcodeproj/project.pbxproj
lldb/gtest/unittest/Utility/Makefile
lldb/gtest/unittest/Utility/UriParserTest.cpp [new file with mode: 0644]
lldb/lldb.xcodeproj/project.pbxproj
lldb/source/Utility/CMakeLists.txt
lldb/source/Utility/UriParser.cpp [new file with mode: 0644]
lldb/source/Utility/UriParser.h [new file with mode: 0644]

index 07cccde..12d7a70 100644 (file)
@@ -10,6 +10,8 @@
                236ED33319D49076008CA7D7 /* ThreadStateCoordinatorTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ThreadStateCoordinatorTest.cpp; sourceTree = "<group>"; };
                236ED33419D49081008CA7D7 /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = "<group>"; };
                236ED33619D490B0008CA7D7 /* Makefile.rules */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Makefile.rules; sourceTree = "<group>"; };
+               33064C981A5C7A1A0033D415 /* UriParserTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = UriParserTest.cpp; path = Utility/UriParserTest.cpp; sourceTree = "<group>"; };
+               33064C9D1A5C7AC90033D415 /* do-gtest.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = "do-gtest.py"; sourceTree = "<group>"; };
                338C47F41A1E67B900B46077 /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; name = Makefile; path = Utility/Makefile; sourceTree = "<group>"; };
                338C47F51A1E67B900B46077 /* StringExtractorTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = StringExtractorTest.cpp; path = Utility/StringExtractorTest.cpp; sourceTree = "<group>"; };
 /* End PBXFileReference section */
@@ -60,6 +62,7 @@
                23CDD8EE19D4790700461DDC = {
                        isa = PBXGroup;
                        children = (
+                               33064C9D1A5C7AC90033D415 /* do-gtest.py */,
                                236ED33519D49098008CA7D7 /* make */,
                                236ED32F19D4901D008CA7D7 /* unittest */,
                        );
@@ -68,6 +71,7 @@
                338C47F31A1E677900B46077 /* Utility */ = {
                        isa = PBXGroup;
                        children = (
+                               33064C981A5C7A1A0033D415 /* UriParserTest.cpp */,
                                338C47F41A1E67B900B46077 /* Makefile */,
                                338C47F51A1E67B900B46077 /* StringExtractorTest.cpp */,
                        );
index cd006b3..c35aa6c 100644 (file)
@@ -5,7 +5,8 @@ LEVEL := $(realpath $(THIS_FILE_DIR)../../make)
 CFLAGS_EXTRAS := -D__STDC_LIMIT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_CONSTANT_MACROS
 ENABLE_THREADS := YES
 CXX_SOURCES := $(wildcard *.cpp) \
-       $(realpath $(LEVEL)/../../source/Utility/StringExtractor.cpp)
+       $(realpath $(LEVEL)/../../source/Utility/StringExtractor.cpp) \
+       $(realpath $(LEVEL)/../../source/Utility/UriParser.cpp)
 MAKE_DSYM := NO
 
 OS := $(shell uname -s)
diff --git a/lldb/gtest/unittest/Utility/UriParserTest.cpp b/lldb/gtest/unittest/Utility/UriParserTest.cpp
new file mode 100644 (file)
index 0000000..b9db7ac
--- /dev/null
@@ -0,0 +1,129 @@
+#include "gtest/gtest.h"
+#include "Utility/UriParser.h"
+
+namespace
+{
+    class UriParserTest: public ::testing::Test
+    {
+    };
+}
+
+// result strings (scheme/hostname/port/path) passed into UriParser::Parse
+// are initialized to kAsdf so we can verify that they are unmodified if the 
+// URI is invalid
+static const char* kAsdf = "asdf";
+
+class UriTestCase
+{
+public:
+    UriTestCase(const char* uri, const char* scheme, const char* hostname, int port, const char* path) :
+      m_uri(uri),
+      m_result(true),
+      m_scheme(scheme),
+      m_hostname(hostname),
+      m_port(port),
+      m_path(path)
+    {
+    }
+
+    UriTestCase(const char* uri) :
+      m_uri(uri),
+      m_result(false),
+      m_scheme(kAsdf),
+      m_hostname(kAsdf),
+      m_port(1138),
+      m_path(kAsdf)
+    {
+    }
+
+    const char* m_uri;
+    bool        m_result;
+    const char* m_scheme;
+    const char* m_hostname;
+    int         m_port;
+    const char* m_path;
+};
+
+#define VALIDATE \
+    std::string scheme(kAsdf); \
+    std::string hostname(kAsdf); \
+    int port(1138); \
+    std::string path(kAsdf); \
+    EXPECT_EQ (testCase.m_result, UriParser::Parse(testCase.m_uri, scheme, hostname, port, path)); \
+    EXPECT_STREQ (testCase.m_scheme, scheme.c_str()); \
+    EXPECT_STREQ (testCase.m_hostname, hostname.c_str()); \
+    EXPECT_EQ (testCase.m_port, port); \
+    EXPECT_STREQ (testCase.m_path, path.c_str());
+
+TEST_F (UriParserTest, Minimal)
+{
+    const UriTestCase testCase("x://y", "x", "y", 0, "/");
+    VALIDATE
+}
+
+TEST_F (UriParserTest, MinimalPort)
+{
+    const UriTestCase testCase("x://y:1", "x", "y", 1, "/");
+    VALIDATE
+}
+
+TEST_F (UriParserTest, MinimalPath)
+{
+    const UriTestCase testCase("x://y/", "x", "y", 0, "/");
+    VALIDATE
+}
+
+TEST_F (UriParserTest, MinimalPortPath)
+{
+    const UriTestCase testCase("x://y:1/", "x", "y", 1, "/");
+    VALIDATE
+}
+
+TEST_F (UriParserTest, TypicalPortPath)
+{
+    const UriTestCase testCase("connect://192.168.100.132:5432/", "connect", "192.168.100.132", 5432, "/");
+    VALIDATE
+}
+
+TEST_F (UriParserTest, SchemeHostSeparator)
+{
+    const UriTestCase testCase("x:/y");
+    VALIDATE
+}
+
+TEST_F (UriParserTest, SchemeHostSeparator2)
+{
+    const UriTestCase testCase("x:y");
+    VALIDATE
+}
+
+TEST_F (UriParserTest, SchemeHostSeparator3)
+{
+    const UriTestCase testCase("x//y");
+    VALIDATE
+}
+
+TEST_F (UriParserTest, SchemeHostSeparator4)
+{
+    const UriTestCase testCase("x/y");
+    VALIDATE
+}
+
+TEST_F (UriParserTest, BadPort)
+{
+    const UriTestCase testCase("x://y:a/");
+    VALIDATE
+}
+
+TEST_F (UriParserTest, BadPort2)
+{
+    const UriTestCase testCase("x://y:5432a/");
+    VALIDATE
+}
+
+TEST_F (UriParserTest, Empty)
+{
+    const UriTestCase testCase("");
+    VALIDATE
+}
+
index aa81d2c..3f851ba 100644 (file)
                26FFC19C14FC072100087D58 /* DYLDRendezvous.h in Headers */ = {isa = PBXBuildFile; fileRef = 26FFC19614FC072100087D58 /* DYLDRendezvous.h */; };
                26FFC19D14FC072100087D58 /* DynamicLoaderPOSIXDYLD.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26FFC19714FC072100087D58 /* DynamicLoaderPOSIXDYLD.cpp */; };
                26FFC19E14FC072100087D58 /* DynamicLoaderPOSIXDYLD.h in Headers */ = {isa = PBXBuildFile; fileRef = 26FFC19814FC072100087D58 /* DynamicLoaderPOSIXDYLD.h */; };
+               33064C9A1A5C7A330033D415 /* UriParser.cpp in CopyFiles */ = {isa = PBXBuildFile; fileRef = 33064C991A5C7A330033D415 /* UriParser.cpp */; };
+               33064C9C1A5C7A490033D415 /* UriParser.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 33064C9B1A5C7A490033D415 /* UriParser.h */; };
                3FDFDDBD199C3A06009756A7 /* FileAction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFDDBC199C3A06009756A7 /* FileAction.cpp */; };
                3FDFDDBF199D345E009756A7 /* FileCache.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFDDBE199D345E009756A7 /* FileCache.cpp */; };
                3FDFDDC6199D37ED009756A7 /* FileSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFDDC5199D37ED009756A7 /* FileSystem.cpp */; };
                        dstSubfolderSpec = 0;
                        files = (
                                AF90106515AB7D3600FF120D /* lldb.1 in CopyFiles */,
+                               33064C9C1A5C7A490033D415 /* UriParser.h in CopyFiles */,
+                               33064C9A1A5C7A330033D415 /* UriParser.cpp in CopyFiles */,
                        );
                        runOnlyForDeploymentPostprocessing = 1;
                };
                26FFC19614FC072100087D58 /* DYLDRendezvous.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DYLDRendezvous.h; sourceTree = "<group>"; };
                26FFC19714FC072100087D58 /* DynamicLoaderPOSIXDYLD.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DynamicLoaderPOSIXDYLD.cpp; sourceTree = "<group>"; };
                26FFC19814FC072100087D58 /* DynamicLoaderPOSIXDYLD.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DynamicLoaderPOSIXDYLD.h; sourceTree = "<group>"; };
+               33064C991A5C7A330033D415 /* UriParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = UriParser.cpp; path = source/Utility/UriParser.cpp; sourceTree = "<group>"; };
+               33064C9B1A5C7A490033D415 /* UriParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UriParser.h; path = source/Utility/UriParser.h; sourceTree = "<group>"; };
                3F5E8AF31A40D4A500A73232 /* PipeBase.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = PipeBase.h; path = include/lldb/Host/PipeBase.h; sourceTree = "<group>"; };
                3FDFD6C3199C396E009756A7 /* FileAction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FileAction.h; path = include/lldb/Target/FileAction.h; sourceTree = "<group>"; };
                3FDFDDBC199C3A06009756A7 /* FileAction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = FileAction.cpp; path = source/Target/FileAction.cpp; sourceTree = "<group>"; };
                2682F168115ED9C800CCFF99 /* Utility */ = {
                        isa = PBXGroup;
                        children = (
+                               33064C9B1A5C7A490033D415 /* UriParser.h */,
+                               33064C991A5C7A330033D415 /* UriParser.cpp */,
                                AF9107E91685709A00DBCD3C /* ARM64_GCC_Registers.h */,
                                26CF992414428766001E4138 /* AnsiTerminal.h */,
                                26F996A7119B79C300412154 /* ARM_DWARF_Registers.h */,
index 0c5158c..ad89050 100644 (file)
@@ -11,4 +11,5 @@ add_lldb_library(lldbUtility
   StringExtractorGDBRemote.cpp
   StringLexer.cpp
   TimeSpecTimeout.cpp
+  UriParser.cpp
   )
diff --git a/lldb/source/Utility/UriParser.cpp b/lldb/source/Utility/UriParser.cpp
new file mode 100644 (file)
index 0000000..bf1e601
--- /dev/null
@@ -0,0 +1,58 @@
+//===-- UriParser.cpp -------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Utility/UriParser.h"
+
+// C Includes
+#include <stdlib.h>
+
+// C++ Includes
+// Other libraries and framework includes
+// Project includes
+
+//----------------------------------------------------------------------
+// UriParser::Parse
+//----------------------------------------------------------------------
+bool
+UriParser::Parse(const char* uri,
+    std::string& scheme,
+    std::string& hostname,
+    int& port,
+    std::string& path
+    )
+{
+    char scheme_buf[100] = {0};
+    char hostname_buf[256] = {0};
+    char port_buf[11] = {0}; // 10==strlen(2^32)
+    char path_buf[2049] = {'/', 0};
+  
+    bool ok = false;
+         if (4==sscanf(uri, "%99[^:/]://%255[^/:]:%[^/]/%2047s", scheme_buf, hostname_buf, port_buf, path_buf+1)) { ok = true; }
+    else if (3==sscanf(uri, "%99[^:/]://%255[^/:]:%[^/]", scheme_buf, hostname_buf, port_buf)) { ok = true; }
+    else if (3==sscanf(uri, "%99[^:/]://%255[^/]/%2047s", scheme_buf, hostname_buf, path_buf+1)) { ok = true; }
+    else if (2==sscanf(uri, "%99[^:/]://%255[^/]", scheme_buf, hostname_buf)) { ok = true; }
+
+    char* end = port_buf;
+    int port_tmp = strtoul(port_buf, &end, 10);
+    if (*end != 0)
+    {
+        // there are invalid characters in port_buf
+        return false;
+    }
+
+    if (ok)
+    {
+        scheme.assign(scheme_buf);
+        hostname.assign(hostname_buf);
+        port = port_tmp;
+        path.assign(path_buf);
+    }
+    return ok;
+}
+
diff --git a/lldb/source/Utility/UriParser.h b/lldb/source/Utility/UriParser.h
new file mode 100644 (file)
index 0000000..c46628d
--- /dev/null
@@ -0,0 +1,31 @@
+//===-- UriParser.h ---------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef utility_UriParser_h_
+#define utility_UriParser_h_
+
+// C Includes
+// C++ Includes
+#include <string>
+
+// Other libraries and framework includes
+// Project includes
+
+class UriParser
+{
+public:
+    static bool Parse(const char* uri,
+        std::string& scheme,
+        std::string& hostname,
+        int& port,
+        std::string& path
+        );
+};
+
+#endif  // utility_UriParser_h_