[Lldb-commits] [lldb] r225317 - Adds UriParser::Parse and unit tests

Vince Harron vharron at google.com
Tue Jan 6 15:38:25 PST 2015


Author: vharron
Date: Tue Jan  6 17:38:24 2015
New Revision: 225317

URL: http://llvm.org/viewvc/llvm-project?rev=225317&view=rev
Log:
Adds UriParser::Parse and unit tests

This can be used to parse URIs passed to 'platform connect'

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


Added:
    lldb/trunk/gtest/unittest/Utility/UriParserTest.cpp
    lldb/trunk/source/Utility/UriParser.cpp
    lldb/trunk/source/Utility/UriParser.h
Modified:
    lldb/trunk/gtest/gtest.xcodeproj/project.pbxproj
    lldb/trunk/gtest/unittest/Utility/Makefile
    lldb/trunk/lldb.xcodeproj/project.pbxproj
    lldb/trunk/source/Utility/CMakeLists.txt

Modified: lldb/trunk/gtest/gtest.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/gtest/gtest.xcodeproj/project.pbxproj?rev=225317&r1=225316&r2=225317&view=diff
==============================================================================
--- lldb/trunk/gtest/gtest.xcodeproj/project.pbxproj (original)
+++ lldb/trunk/gtest/gtest.xcodeproj/project.pbxproj Tue Jan  6 17:38:24 2015
@@ -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 */,
 			);

Modified: lldb/trunk/gtest/unittest/Utility/Makefile
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/gtest/unittest/Utility/Makefile?rev=225317&r1=225316&r2=225317&view=diff
==============================================================================
--- lldb/trunk/gtest/unittest/Utility/Makefile (original)
+++ lldb/trunk/gtest/unittest/Utility/Makefile Tue Jan  6 17:38:24 2015
@@ -5,7 +5,8 @@ LEVEL := $(realpath $(THIS_FILE_DIR)../.
 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)

Added: lldb/trunk/gtest/unittest/Utility/UriParserTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/gtest/unittest/Utility/UriParserTest.cpp?rev=225317&view=auto
==============================================================================
--- lldb/trunk/gtest/unittest/Utility/UriParserTest.cpp (added)
+++ lldb/trunk/gtest/unittest/Utility/UriParserTest.cpp Tue Jan  6 17:38:24 2015
@@ -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
+}
+

Modified: lldb/trunk/lldb.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lldb.xcodeproj/project.pbxproj?rev=225317&r1=225316&r2=225317&view=diff
==============================================================================
--- lldb/trunk/lldb.xcodeproj/project.pbxproj (original)
+++ lldb/trunk/lldb.xcodeproj/project.pbxproj Tue Jan  6 17:38:24 2015
@@ -581,6 +581,8 @@
 		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 */; };
@@ -932,6 +934,8 @@
 			dstSubfolderSpec = 0;
 			files = (
 				AF90106515AB7D3600FF120D /* lldb.1 in CopyFiles */,
+				33064C9C1A5C7A490033D415 /* UriParser.h in CopyFiles */,
+				33064C9A1A5C7A330033D415 /* UriParser.cpp in CopyFiles */,
 			);
 			runOnlyForDeploymentPostprocessing = 1;
 		};
@@ -1794,6 +1798,8 @@
 		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>"; };
@@ -3031,6 +3037,8 @@
 		2682F168115ED9C800CCFF99 /* Utility */ = {
 			isa = PBXGroup;
 			children = (
+				33064C9B1A5C7A490033D415 /* UriParser.h */,
+				33064C991A5C7A330033D415 /* UriParser.cpp */,
 				AF9107E91685709A00DBCD3C /* ARM64_GCC_Registers.h */,
 				26CF992414428766001E4138 /* AnsiTerminal.h */,
 				26F996A7119B79C300412154 /* ARM_DWARF_Registers.h */,

Modified: lldb/trunk/source/Utility/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/CMakeLists.txt?rev=225317&r1=225316&r2=225317&view=diff
==============================================================================
--- lldb/trunk/source/Utility/CMakeLists.txt (original)
+++ lldb/trunk/source/Utility/CMakeLists.txt Tue Jan  6 17:38:24 2015
@@ -11,4 +11,5 @@ add_lldb_library(lldbUtility
   StringExtractorGDBRemote.cpp
   StringLexer.cpp
   TimeSpecTimeout.cpp
+  UriParser.cpp
   )

Added: lldb/trunk/source/Utility/UriParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/UriParser.cpp?rev=225317&view=auto
==============================================================================
--- lldb/trunk/source/Utility/UriParser.cpp (added)
+++ lldb/trunk/source/Utility/UriParser.cpp Tue Jan  6 17:38:24 2015
@@ -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;
+}
+

Added: lldb/trunk/source/Utility/UriParser.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/UriParser.h?rev=225317&view=auto
==============================================================================
--- lldb/trunk/source/Utility/UriParser.h (added)
+++ lldb/trunk/source/Utility/UriParser.h Tue Jan  6 17:38:24 2015
@@ -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_





More information about the lldb-commits mailing list