summaryrefslogtreecommitdiff
path: root/indra/llplugin/tests/llplugincookiestore_test.cpp
blob: e3e8ac98046efb8769953f09fea7a5e3f59da6f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#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);
	}

}