summaryrefslogtreecommitdiff
path: root/indra/llplugin
diff options
context:
space:
mode:
Diffstat (limited to 'indra/llplugin')
-rw-r--r--indra/llplugin/CMakeLists.txt17
-rw-r--r--indra/llplugin/llplugincookiestore.cpp75
-rw-r--r--indra/llplugin/llplugincookiestore.h9
-rw-r--r--indra/llplugin/tests/llplugincookiestore_test.cpp211
4 files changed, 301 insertions, 11 deletions
diff --git a/indra/llplugin/CMakeLists.txt b/indra/llplugin/CMakeLists.txt
index def9fcbeae..441becbae0 100644
--- a/indra/llplugin/CMakeLists.txt
+++ b/indra/llplugin/CMakeLists.txt
@@ -3,6 +3,7 @@
project(llplugin)
include(00-Common)
+include(CURL)
include(LLCommon)
include(LLImage)
include(LLMath)
@@ -55,3 +56,19 @@ list(APPEND llplugin_SOURCE_FILES ${llplugin_HEADER_FILES})
add_library (llplugin ${llplugin_SOURCE_FILES})
add_subdirectory(slplugin)
+
+# Add tests
+include(LLAddBuildTest)
+# UNIT TESTS
+SET(llplugin_TEST_SOURCE_FILES
+ llplugincookiestore.cpp
+ )
+
+# llplugincookiestore has a dependency on curl, so we need to link the curl library into the test.
+set_source_files_properties(
+ llplugincookiestore.cpp
+ PROPERTIES
+ LL_TEST_ADDITIONAL_LIBRARIES "${CURL_LIBRARIES}"
+ )
+
+LL_ADD_PROJECT_UNIT_TESTS(llplugin "${llplugin_TEST_SOURCE_FILES}")
diff --git a/indra/llplugin/llplugincookiestore.cpp b/indra/llplugin/llplugincookiestore.cpp
index 1964b8d789..85b1e70d78 100644
--- a/indra/llplugin/llplugincookiestore.cpp
+++ b/indra/llplugin/llplugincookiestore.cpp
@@ -53,7 +53,7 @@ LLPluginCookieStore::~LLPluginCookieStore()
LLPluginCookieStore::Cookie::Cookie(const std::string &s, std::string::size_type cookie_start, std::string::size_type cookie_end):
- mCookie(s, cookie_start, cookie_end),
+ mCookie(s, cookie_start, cookie_end - cookie_start),
mNameStart(0), mNameEnd(0),
mValueStart(0), mValueEnd(0),
mDomainStart(0), mDomainEnd(0),
@@ -62,11 +62,11 @@ LLPluginCookieStore::Cookie::Cookie(const std::string &s, std::string::size_type
{
}
-LLPluginCookieStore::Cookie *LLPluginCookieStore::Cookie::createFromString(const std::string &s, std::string::size_type cookie_start, std::string::size_type cookie_end)
+LLPluginCookieStore::Cookie *LLPluginCookieStore::Cookie::createFromString(const std::string &s, std::string::size_type cookie_start, std::string::size_type cookie_end, const std::string &host)
{
Cookie *result = new Cookie(s, cookie_start, cookie_end);
- if(!result->parse())
+ if(!result->parse(host))
{
delete result;
result = NULL;
@@ -92,7 +92,7 @@ std::string LLPluginCookieStore::Cookie::getKey() const
return result;
}
-bool LLPluginCookieStore::Cookie::parse()
+bool LLPluginCookieStore::Cookie::parse(const std::string &host)
{
bool first_field = true;
@@ -248,7 +248,50 @@ bool LLPluginCookieStore::Cookie::parse()
// The cookie MUST have a name
if(mNameEnd <= mNameStart)
return false;
+
+ // If the cookie doesn't have a domain, add the current host as the domain.
+ if(mDomainEnd <= mDomainStart)
+ {
+ if(host.empty())
+ {
+ // no domain and no current host -- this is a parse failure.
+ return false;
+ }
+
+ // Figure out whether this cookie ended with a ";" or not...
+ std::string::size_type last_char = mCookie.find_last_not_of(" ");
+ if((last_char != std::string::npos) && (mCookie[last_char] != ';'))
+ {
+ mCookie += ";";
+ }
+
+ mCookie += " domain=";
+ mDomainStart = mCookie.size();
+ mCookie += host;
+ mDomainEnd = mCookie.size();
+
+ lldebugs << "added domain (" << mDomainStart << " to " << mDomainEnd << "), new cookie is: " << mCookie << llendl;
+ }
+
+ // If the cookie doesn't have a path, add "/".
+ if(mPathEnd <= mPathStart)
+ {
+ // Figure out whether this cookie ended with a ";" or not...
+ std::string::size_type last_char = mCookie.find_last_not_of(" ");
+ if((last_char != std::string::npos) && (mCookie[last_char] != ';'))
+ {
+ mCookie += ";";
+ }
+
+ mCookie += " path=";
+ mPathStart = mCookie.size();
+ mCookie += "/";
+ mPathEnd = mCookie.size();
+ lldebugs << "added path (" << mPathStart << " to " << mPathEnd << "), new cookie is: " << mCookie << llendl;
+ }
+
+
return true;
}
@@ -409,13 +452,29 @@ void LLPluginCookieStore::setCookies(const std::string &cookies, bool mark_chang
while(start != std::string::npos)
{
- std::string::size_type end = cookies.find('\n', start);
+ std::string::size_type end = cookies.find_first_of("\r\n", start);
if(end > start)
{
// The line is non-empty. Try to create a cookie from it.
setOneCookie(cookies, start, end, mark_changed);
}
- start = cookies.find_first_not_of("\n ", end);
+ start = cookies.find_first_not_of("\r\n ", end);
+ }
+}
+
+void LLPluginCookieStore::setCookiesFromHost(const std::string &cookies, const std::string &host, bool mark_changed)
+{
+ std::string::size_type start = 0;
+
+ while(start != std::string::npos)
+ {
+ std::string::size_type end = cookies.find_first_of("\r\n", start);
+ if(end > start)
+ {
+ // The line is non-empty. Try to create a cookie from it.
+ setOneCookie(cookies, start, end, mark_changed, host);
+ }
+ start = cookies.find_first_not_of("\r\n ", end);
}
}
@@ -502,9 +561,9 @@ std::string LLPluginCookieStore::unquoteString(const std::string &s)
// When deleting with mark_changed set to true, this replaces the existing cookie in the list with an entry that's marked both dead and changed.
// Some time later when writeChangedCookies() is called with clear_changed set to true, the dead cookie is deleted from the list after being returned, so that the
// delete operation (in the form of the expired cookie) is passed along.
-void LLPluginCookieStore::setOneCookie(const std::string &s, std::string::size_type cookie_start, std::string::size_type cookie_end, bool mark_changed)
+void LLPluginCookieStore::setOneCookie(const std::string &s, std::string::size_type cookie_start, std::string::size_type cookie_end, bool mark_changed, const std::string &host)
{
- Cookie *cookie = Cookie::createFromString(s, cookie_start, cookie_end);
+ Cookie *cookie = Cookie::createFromString(s, cookie_start, cookie_end, host);
if(cookie)
{
lldebugs << "setting cookie: " << cookie->getCookie() << llendl;
diff --git a/indra/llplugin/llplugincookiestore.h b/indra/llplugin/llplugincookiestore.h
index 5250f008b6..a93f0c14f0 100644
--- a/indra/llplugin/llplugincookiestore.h
+++ b/indra/llplugin/llplugincookiestore.h
@@ -66,18 +66,21 @@ public:
void setCookies(const std::string &cookies, bool mark_changed = true);
void readCookies(std::istream& s, bool mark_changed = true);
+ // sets one or more cookies (without reinitializing anything), supplying a hostname the cookies came from -- use when setting a cookie manually
+ void setCookiesFromHost(const std::string &cookies, const std::string &host, bool mark_changed = true);
+
// quote or unquote a string as per the definition of 'quoted-string' in rfc2616
static std::string quoteString(const std::string &s);
static std::string unquoteString(const std::string &s);
private:
- void setOneCookie(const std::string &s, std::string::size_type cookie_start, std::string::size_type cookie_end, bool mark_changed);
+ void setOneCookie(const std::string &s, std::string::size_type cookie_start, std::string::size_type cookie_end, bool mark_changed, const std::string &host = LLStringUtil::null);
class Cookie
{
public:
- static Cookie *createFromString(const std::string &s, std::string::size_type cookie_start = 0, std::string::size_type cookie_end = std::string::npos);
+ static Cookie *createFromString(const std::string &s, std::string::size_type cookie_start = 0, std::string::size_type cookie_end = std::string::npos, const std::string &host = LLStringUtil::null);
// Construct a string from the cookie that uniquely represents it, to be used as a key in a std::map.
std::string getKey() const;
@@ -95,7 +98,7 @@ private:
private:
Cookie(const std::string &s, std::string::size_type cookie_start = 0, std::string::size_type cookie_end = std::string::npos);
- bool parse();
+ bool parse(const std::string &host);
std::string::size_type findFieldEnd(std::string::size_type start = 0, std::string::size_type end = std::string::npos);
bool matchName(std::string::size_type start, std::string::size_type end, const char *name);
diff --git a/indra/llplugin/tests/llplugincookiestore_test.cpp b/indra/llplugin/tests/llplugincookiestore_test.cpp
new file mode 100644
index 0000000000..020d9c1977
--- /dev/null
+++ b/indra/llplugin/tests/llplugincookiestore_test.cpp
@@ -0,0 +1,211 @@
+/**
+ * @file llplugincookiestore_test.cpp
+ * @brief Unit tests for LLPluginCookieStore.
+ *
+ * @cond
+ * $LicenseInfo:firstyear=2010&license=viewergpl$
+ *
+ * Copyright (c) 2010, Linden Research, Inc.
+ *
+ * Second Life Viewer Source Code
+ * The source code in this file ("Source Code") is provided by Linden Lab
+ * to you under the terms of the GNU General Public License, version 2.0
+ * ("GPL"), unless you have obtained a separate licensing agreement
+ * ("Other License"), formally executed by you and Linden Lab. Terms of
+ * the GPL can be found in doc/GPL-license.txt in this distribution, or
+ * online at http://secondlife.com/developers/opensource/gplv2
+ *
+ * There are special exceptions to the terms and conditions of the GPL as
+ * it is applied to this Source Code. View the full text of the exception
+ * in the file doc/FLOSS-exception.txt in this software distribution, or
+ * online at http://secondlife.com/developers/opensource/flossexception
+ *
+ * By copying, modifying or distributing this software, you acknowledge
+ * that you have read and understood your obligations described above,
+ * and agree to abide by those obligations.
+ *
+ * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
+ * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
+ * COMPLETENESS OR PERFORMANCE.
+ * $/LicenseInfo$
+ * @endcond
+ */
+
+#include "linden_common.h"
+#include "../test/lltut.h"
+
+#include "../llplugincookiestore.h"
+
+
+namespace tut
+{
+ // Main Setup
+ struct LLPluginCookieStoreFixture
+ {
+ LLPluginCookieStoreFixture()
+ {
+ // We need dates definitively in the past and the future to properly test cookie expiration.
+ LLDate now = LLDate::now();
+ LLDate past(now.secondsSinceEpoch() - (60.0 * 60.0 * 24.0)); // 1 day in the past
+ LLDate future(now.secondsSinceEpoch() + (60.0 * 60.0 * 24.0)); // 1 day in the future
+
+ mPastString = past.asRFC1123();
+ mFutureString = future.asRFC1123();
+ }
+
+ std::string mPastString;
+ std::string mFutureString;
+ LLPluginCookieStore mCookieStore;
+
+ // List of cookies used for validation
+ std::list<std::string> mCookies;
+
+ // This sets up mCookies from a string returned by one of the functions in LLPluginCookieStore
+ void setCookies(const std::string &cookies)
+ {
+ mCookies.clear();
+ std::string::size_type start = 0;
+
+ while(start != std::string::npos)
+ {
+ std::string::size_type end = cookies.find_first_of("\r\n", start);
+ if(end > start)
+ {
+ std::string line(cookies, start, end - start);
+ if(line.find_first_not_of("\r\n\t ") != std::string::npos)
+ {
+ // The line has some non-whitespace characters. Save it to the list.
+ mCookies.push_back(std::string(cookies, start, end - start));
+ }
+ }
+ start = cookies.find_first_not_of("\r\n ", end);
+ }
+ }
+
+ // This ensures that a cookie matching the one passed is in the list.
+ void ensureCookie(const std::string &cookie)
+ {
+ std::list<std::string>::iterator iter;
+ for(iter = mCookies.begin(); iter != mCookies.end(); iter++)
+ {
+ if(*iter == cookie)
+ {
+ // Found the cookie
+ // TODO: this should do a smarter equality comparison on the two cookies, instead of just a string compare.
+ return;
+ }
+ }
+
+ // Didn't find this cookie
+ std::string message = "cookie not found: ";
+ message += cookie;
+ ensure(message, false);
+ }
+
+ // This ensures that the number of cookies in the list matches what's expected.
+ void ensureSize(const std::string &message, size_t size)
+ {
+ if(mCookies.size() != size)
+ {
+ std::stringstream full_message;
+
+ full_message << message << " (expected " << size << ", actual " << mCookies.size() << ")";
+ ensure(full_message.str(), false);
+ }
+ }
+ };
+
+ typedef test_group<LLPluginCookieStoreFixture> factory;
+ typedef factory::object object;
+ factory tf("LLPluginCookieStore test");
+
+ // Tests
+ template<> template<>
+ void object::test<1>()
+ {
+ // Test 1: cookie uniqueness and update lists.
+ // Valid, distinct cookies:
+
+ std::string cookie01 = "cookieA=value; domain=example.com; path=/";
+ std::string cookie02 = "cookieB=value; domain=example.com; path=/"; // different name
+ std::string cookie03 = "cookieA=value; domain=foo.example.com; path=/"; // different domain
+ std::string cookie04 = "cookieA=value; domain=example.com; path=/bar/"; // different path
+ std::string cookie05 = "cookieC; domain=example.com; path=/"; // empty value
+ std::string cookie06 = "cookieD=value; domain=example.com; path=/; expires="; // different name, persistent cookie
+ cookie06 += mFutureString;
+
+ mCookieStore.setCookies(cookie01);
+ mCookieStore.setCookies(cookie02);
+ mCookieStore.setCookies(cookie03);
+ mCookieStore.setCookies(cookie04);
+ mCookieStore.setCookies(cookie05);
+ mCookieStore.setCookies(cookie06);
+
+ // Invalid cookies (these will get parse errors and not be added to the store)
+
+ std::string badcookie01 = "cookieD=value; domain=example.com; path=/; foo=bar"; // invalid field name
+ std::string badcookie02 = "cookieE=value; path=/"; // no domain
+
+ mCookieStore.setCookies(badcookie01);
+ mCookieStore.setCookies(badcookie02);
+
+ // All cookies added so far should have been marked as "changed"
+ setCookies(mCookieStore.getChangedCookies());
+ ensureSize("count of changed cookies", 6);
+ ensureCookie(cookie01);
+ ensureCookie(cookie02);
+ ensureCookie(cookie03);
+ ensureCookie(cookie04);
+ ensureCookie(cookie05);
+ ensureCookie(cookie06);
+
+ // Save off the current state of the cookie store (we'll restore it later)
+ std::string savedCookies = mCookieStore.getAllCookies();
+
+ // Test replacing cookies
+ std::string cookie01a = "cookieA=newvalue; domain=example.com; path=/"; // updated value
+ std::string cookie02a = "cookieB=newvalue; domain=example.com; path=/; expires="; // remove cookie (by setting an expire date in the past)
+ cookie02a += mPastString;
+
+ mCookieStore.setCookies(cookie01a);
+ mCookieStore.setCookies(cookie02a);
+
+ // test for getting changed cookies
+ setCookies(mCookieStore.getChangedCookies());
+ ensureSize("count of updated cookies", 2);
+ ensureCookie(cookie01a);
+ ensureCookie(cookie02a);
+
+ // and for the state of the store after getting changed cookies
+ setCookies(mCookieStore.getAllCookies());
+ ensureSize("count of valid cookies", 5);
+ ensureCookie(cookie01a);
+ ensureCookie(cookie03);
+ ensureCookie(cookie04);
+ ensureCookie(cookie05);
+ ensureCookie(cookie06);
+
+ // Check that only the persistent cookie is returned here
+ setCookies(mCookieStore.getPersistentCookies());
+ ensureSize("count of persistent cookies", 1);
+ ensureCookie(cookie06);
+
+ // Restore the cookie store to a previous state and verify
+ mCookieStore.setAllCookies(savedCookies);
+
+ // Since setAllCookies defaults to not marking cookies as changed, this list should be empty.
+ setCookies(mCookieStore.getChangedCookies());
+ ensureSize("count of changed cookies after restore", 0);
+
+ // Verify that the restore worked as it should have.
+ setCookies(mCookieStore.getAllCookies());
+ ensureSize("count of restored cookies", 6);
+ ensureCookie(cookie01);
+ ensureCookie(cookie02);
+ ensureCookie(cookie03);
+ ensureCookie(cookie04);
+ ensureCookie(cookie05);
+ ensureCookie(cookie06);
+ }
+
+}