Integrated the test from the "Services Kit" GSoC project. The urlRequest tests

don't currently compile since not all of the Services Kit classes are included
in the build ATM.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39163 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus 2010-10-27 14:16:51 +00:00
parent eb3504793b
commit 766bc131b4
12 changed files with 672 additions and 2 deletions

View File

@ -26,12 +26,15 @@ SimpleTest NetAddressTest : NetAddressTest.cpp
SimpleTest NetEndpointTest : NetEndpointTest.cpp SimpleTest NetEndpointTest : NetEndpointTest.cpp
: $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) be $(TARGET_LIBSUPC++) ; : $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) be $(TARGET_LIBSUPC++) ;
SubInclude HAIKU_TOP src tests kits net cookie ;
SubInclude HAIKU_TOP src tests kits net DialUpPreflet ; SubInclude HAIKU_TOP src tests kits net DialUpPreflet ;
SubInclude HAIKU_TOP src tests kits net icmp ;
SubInclude HAIKU_TOP src tests kits net ipv6 ;
SubInclude HAIKU_TOP src tests kits net multicast ; SubInclude HAIKU_TOP src tests kits net multicast ;
SubInclude HAIKU_TOP src tests kits net netperf ; SubInclude HAIKU_TOP src tests kits net netperf ;
SubInclude HAIKU_TOP src tests kits net preflet ; SubInclude HAIKU_TOP src tests kits net preflet ;
SubInclude HAIKU_TOP src tests kits net sock ; SubInclude HAIKU_TOP src tests kits net sock ;
SubInclude HAIKU_TOP src tests kits net tcp_shell ; SubInclude HAIKU_TOP src tests kits net tcp_shell ;
SubInclude HAIKU_TOP src tests kits net tcptester ; SubInclude HAIKU_TOP src tests kits net tcptester ;
SubInclude HAIKU_TOP src tests kits net icmp ; SubInclude HAIKU_TOP src tests kits net url ;
SubInclude HAIKU_TOP src tests kits net ipv6 ; SubInclude HAIKU_TOP src tests kits net urlRequest ;

View File

@ -0,0 +1,6 @@
SubDir HAIKU_TOP src tests kits net cookie ;
Application cookie_test
: cookie_test.cpp
: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)
;

View File

@ -0,0 +1,173 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <NetworkKit.h>
#include <SupportKit.h>
#ifdef ASSERT
#undef ASSERT
#endif
#define REPORT(i, assert, line) cout << "ASSERT() failed at line " << line \
<< ": " << #assert << " (test #" << i << ")" << endl
#define ASSERT(index, assertion) { if (!(assertion)) { REPORT(index, assertion, __LINE__ ); } }
using std::cout;
using std::endl;
typedef struct
{
const char* cookieString;
struct
{
const char* name;
const char* value;
const char* domain;
const char* path;
bool secure;
bool discard;
bool session;
int32 maxAge;
} expected;
} ExplodeTest;
const ExplodeTest kTestExplode[] =
// Cookie string
// Name Value Domain Path Secure Discard Session maxAge
// -------- --------- --------- --------- ------ ------- -------- -------
{
{ "name=value",
{ "name", "value", "", "", false, false, true, 0 } },
{ "name=value;secure=true",
{ "name", "value", "", "", true, false, true, 0 } },
{ "name=value;secure=false;maxage=5",
{ "name", "value", "", "", false, false, false, 5 } },
{ "name=value;discard=true",
{ "name", "value", "", "", false, true, true, 0 } },
};
void explodeImplodeTest()
{
uint8 testIndex;
BNetworkCookie cookie;
for (testIndex = 0; testIndex < (sizeof(kTestExplode) / sizeof(ExplodeTest)); testIndex++)
{
cookie.ParseCookieString(kTestExplode[testIndex].cookieString);
ASSERT(testIndex, BString(kTestExplode[testIndex].expected.name) == BString(cookie.Name()));
ASSERT(testIndex, BString(kTestExplode[testIndex].expected.value) == BString(cookie.Value()));
ASSERT(testIndex, BString(kTestExplode[testIndex].expected.domain) == BString(cookie.Domain()));
ASSERT(testIndex, BString(kTestExplode[testIndex].expected.path) == BString(cookie.Path()));
ASSERT(testIndex, kTestExplode[testIndex].expected.secure == cookie.Secure());
ASSERT(testIndex, kTestExplode[testIndex].expected.discard == cookie.Discard());
ASSERT(testIndex, kTestExplode[testIndex].expected.session == cookie.IsSessionCookie());
if (!cookie.IsSessionCookie())
ASSERT(testIndex, kTestExplode[testIndex].expected.maxAge == cookie.MaxAge());
}
}
void stressTest(int32 domainNumber, int32 totalCookies, char** flat, ssize_t* size)
{
char **domains = new char*[domainNumber];
cout << "Creating random domains" << endl;
srand(time(NULL));
for (int32 i = 0; i < domainNumber; i++) {
int16 charNum = (rand() % 16) + 1;
domains[i] = new char[charNum + 5];
// Random domain
for (int32 c = 0; c < charNum; c++)
domains[i][c] = (rand() % 26) + 'a';
domains[i][charNum] = '.';
// Random tld
for (int32 c = 0; c < 3; c++)
domains[i][charNum+1+c] = (rand() % 26) + 'a';
domains[i][charNum+4] = 0;
}
BNetworkCookieJar j;
BStopWatch* watch = new BStopWatch("Cookie insertion");
for (int32 i = 0; i < totalCookies; i++) {
BNetworkCookie c;
int16 domain = (rand() % domainNumber);
BString name("Foo");
name << i;
c.SetName(name);
c.SetValue("Bar");
c.SetDomain(domains[domain]);
c.SetPath("/");
j.AddCookie(c);
}
delete watch;
BNetworkCookie* c;
int16 domain = (rand() % domainNumber);
BString host("http://");
host << domains[domain] << "/";
watch = new BStopWatch("Cookie filtering");
BUrl url(host);
int32 count = 0;
for (BNetworkCookieJar::UrlIterator it(j.GetUrlIterator(url)); (c = it.Next()); ) {
//for (BNetworkCookieJar::Iterator it(j.GetIterator()); c = it.Next(); ) {
count++;
}
delete watch;
cout << "Count for " << host << ": " << count << endl;
cout << "Flat view of cookie jar is " << j.FlattenedSize() << " bytes large." << endl;
*flat = new char[j.FlattenedSize()];
*size = j.FlattenedSize();
if (j.Flatten(*flat, j.FlattenedSize()) == B_OK)
cout << "Flatten() success!" << endl;
else
cout << "Flatten() error!" << endl;
delete[] domains;
}
int
main(int, char**)
{
cout << "Running explodeImplodeTest:" << endl;
explodeImplodeTest();
cout << endl << endl;
cout << "Running stressTest:" << endl;
char* flatJar;
ssize_t size;
stressTest(10000, 40000, &flatJar, &size);
BNetworkCookieJar j;
j.Unflatten(B_ANY_TYPE, flatJar, size);
int32 count = 0;
BNetworkCookie* c;
for (BNetworkCookieJar::Iterator it(j.GetIterator()); (c = it.Next()); )
count++;
cout << "Count : " << count << endl;
delete[] flatJar;
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,6 @@
SubDir HAIKU_TOP src tests kits net url ;
Application url_test
: url_test.cpp
: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)
;

View File

@ -0,0 +1,116 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <NetworkKit.h>
#ifdef ASSERT
#undef ASSERT
#endif
#define REPORT(assert, line) cout << "ASSERT() failed at line " << line << ": " << #assert << endl
#define ASSERT(assertion) { if (!(assertion)) { REPORT(assertion, __LINE__ ); } }
using namespace std;
typedef struct
{
const char* url;
struct
{
const char* protocol;
const char* userName;
const char* password;
const char* host;
int16 port;
const char* path;
const char* request;
const char* fragment;
} expected;
} ExplodeTest;
const char* kTestLength[] =
{
"http://user:pass@www.foo.com:80/path?query#fragment",
"http://user:pass@www.foo.com:80/path?query#",
"http://user:pass@www.foo.com:80/path?query",
"http://user:pass@www.foo.com:80/path?",
"http://user:pass@www.foo.com:80/path",
"http://user:pass@www.foo.com:80/",
"http://user:pass@www.foo.com",
"http://user:pass@",
"http://www.foo.com",
"http://",
"http:"
};
const ExplodeTest kTestExplode[] =
// Url
// Protocol User Password Hostname Port Path Request Fragment
// -------- --------- --------- --------- ---- ---------- ---------- ------------
{
{ "http://user:pass@host:80/path?query#fragment",
{ "http", "user", "pass", "host", 80, "/path", "query", "fragment" } },
{ "http://www.host.tld/path?query#fragment",
{ "http", "", "", "www.host.tld",0, "/path", "query", "fragment" } }
};
// Test if rebuild url are the same length of parsed one
void lengthTest()
{
int8 testIndex;
BUrl testUrl;
for (testIndex = 0; kTestLength[testIndex] != NULL; testIndex++)
{
testUrl.SetUrlString(kTestLength[testIndex]);
cout << "`" << kTestLength[testIndex] << "' vs `" << testUrl.UrlString() << "'" << endl;
ASSERT(strlen(kTestLength[testIndex]) == strlen(testUrl.UrlString()));
}
}
void explodeImplodeTest()
{
uint8 testIndex;
BUrl testUrl;
for (testIndex = 0; testIndex < (sizeof(kTestExplode) / sizeof(ExplodeTest)); testIndex++)
{
testUrl.SetUrlString(kTestExplode[testIndex].url);
ASSERT(BString(kTestExplode[testIndex].url) == BString(testUrl.UrlString()));
ASSERT(BString(kTestExplode[testIndex].expected.protocol) == BString(testUrl.Protocol()));
ASSERT(BString(kTestExplode[testIndex].expected.userName) == BString(testUrl.UserName()));
ASSERT(BString(kTestExplode[testIndex].expected.password) == BString(testUrl.Password()));
ASSERT(BString(kTestExplode[testIndex].expected.host) == BString(testUrl.Host()));
ASSERT(kTestExplode[testIndex].expected.port == testUrl.Port());
ASSERT(BString(kTestExplode[testIndex].expected.path) == BString(testUrl.Path()));
ASSERT(BString(kTestExplode[testIndex].expected.request) == BString(testUrl.Request()));
ASSERT(BString(kTestExplode[testIndex].expected.fragment) == BString(testUrl.Fragment()));
}
}
using std::cout;
using std::endl;
int
main(int, char**)
{
cout << "Running lengthTest:" << endl;
lengthTest();
cout << endl;
cout << "Running explodeImplodeTest:" << endl;
explodeImplodeTest();
cout << endl;
BUrl test = "lol";
cout << "Path: " << test.HasPath() << " -> " << test.Path() << endl;
cout << "Rebuild: " << test.UrlString() << endl;
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,26 @@
SubDir HAIKU_TOP src tests kits net urlRequest ;
Application urlRequest_test
: urlRequest_test.cpp
: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)
;
Application urlAuth_test
: urlAuth_test.cpp
: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)
;
Application urlUpload_test
: urlUpload_test.cpp
: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)
;
Application urlAuthBasic_test
: urlAuthBasic_test.cpp
: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)
;
Application urlProtocolListener_test
: urlProtocolListener_test.cpp
: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)
;

View File

@ -0,0 +1,3 @@
Hello, world !
this is a file ...

View File

@ -0,0 +1,44 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <KernelKit.h>
#include <NetworkKit.h>
using std::cout;
using std::endl;
int
main(int, char**)
{
BUrl authTest("http://192.168.1.11/~chris/auth_basic/");
BUrlRequest t(authTest);
BUrlContext c;
t.SetContext(&c);
t.SetProtocolOption(B_HTTPOPT_AUTHUSERNAME, (void*)"haiku");
t.SetProtocolOption(B_HTTPOPT_AUTHPASSWORD, (void*)"haiku");
if (t.Perform() != B_OK) {
cout << "Error while performing request!" << endl;
return EXIT_FAILURE;
}
// Do nothing while the request is not finished
while (t.IsRunning()) {
cout << std::flush;
snooze(10000);
}
// Print the result
cout << "Request result : " << t.Result().StatusCode() << " (" << t.Result().StatusText() << ")" << endl;
//cout << " * " << c.GetCookieJar().CountCookies() << " cookies in context after request" << endl;
cout << " * " << t.Result().Headers().CountHeaders() << " headers received" << endl;
cout << " * " << t.Result().RawData().Position() << " bytes of raw data:" << endl;
cout << t.Result() << endl;
cout << "End of request" << endl << endl;
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,50 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <KernelKit.h>
#include <NetworkKit.h>
#ifdef ASSERT
#undef ASSERT
#endif
#define REPORT(i, assert, line) cout << "ASSERT() failed at line " << line \
<< ": " << #assert << " (test #" << i << ")" << endl
#define ASSERT(index, assertion) { if (!(assertion)) { REPORT(index, assertion, __LINE__ ); } }
using std::cout;
using std::endl;
int
main(int, char**)
{
BUrl authTest("http://192.168.1.11/~chris/auth_digest/");
BUrlRequest t(authTest);
BUrlContext c;
t.SetContext(&c);
t.SetProtocolOption(B_HTTPOPT_AUTHUSERNAME, (void*)"haiku");
t.SetProtocolOption(B_HTTPOPT_AUTHPASSWORD, (void*)"haiku");
if (t.Perform() != B_OK) {
cout << "Error while performing request!" << endl;
return EXIT_FAILURE;
}
// Do nothing while the request is not finished
while (t.IsRunning()) {
cout << std::flush;
snooze(10000);
}
// Print the result
cout << "Request result : " << t.Result().StatusCode() << " (" << t.Result().StatusText() << ")" << endl;
//cout << " * " << c.GetCookieJar().CountCookies() << " cookies in context after request" << endl;
cout << " * " << t.Result().Headers().CountHeaders() << " headers received" << endl;
cout << " * " << t.Result().RawData().Position() << " bytes of raw data:" << endl;
cout << t.Result() << endl;
cout << "End of request" << endl << endl;
}

View File

@ -0,0 +1,136 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <AppKit.h>
#include <Debug.h>
#include <KernelKit.h>
#include <NetworkKit.h>
class TestSyncListener : public BUrlProtocolListener {
public:
void ConnectionOpened(BUrlProtocol* caller) {
printf("Thread<#%5d> ", find_thread(NULL));
printf("TestSyncListener::ConnectionOpened(%p)\n", caller);
}
};
class TestAsyncListener : public BUrlProtocolAsynchronousListener {
public:
TestAsyncListener(bool transparent)
:
BUrlProtocolAsynchronousListener(transparent)
{ }
void ConnectionOpened(BUrlProtocol* caller) {
printf("Thread<#%5d> ", find_thread(NULL));
printf("TestAsyncListener::ConnectionOpened(%p)\n", caller);
}
};
class Test {
// Synchronous listener
TestSyncListener s;
// Asynchronous listener with dispatcher not embedded
TestAsyncListener a;
BUrlProtocolDispatchingListener a_sync;
// Asynchronous listener with embedded dispatcher
TestAsyncListener a_transparent;
public:
Test()
:
a(false),
a_sync(&a),
a_transparent(true)
{ }
void testListener(BUrlProtocolListener* listener)
{
listener->ConnectionOpened((BUrlProtocol*)0xdeadbeef);
}
void DoTest() {
// Tests
printf("Launching test from thread #%5ld\n", find_thread(NULL));
testListener(&s);
testListener(&a_sync);
testListener(a_transparent.SynchronousListener());
}
};
class TestThread {
Test t;
thread_id fThreadId;
public:
thread_id Run() {
fThreadId = spawn_thread(&TestThread::_ThreadEntry, "TestThread",
B_NORMAL_PRIORITY, this);
if (fThreadId < B_OK)
return fThreadId;
status_t launchErr = resume_thread(fThreadId);
if (launchErr < B_OK)
return launchErr;
return fThreadId;
}
static status_t _ThreadEntry(void* ptr) {
TestThread* parent = (TestThread*)ptr;
parent->t.DoTest();
return B_OK;
}
};
class TestApp : public BApplication {
Test t;
TestThread t2;
public:
TestApp()
:
BApplication("application/x-vnd.urlprotocollistener-test")
{ }
void ReadyToRun() {
t2.Run();
t.DoTest();
SetPulseRate(1000000);
}
void Pulse() {
static int count = 0;
count++;
if (count == 1) {
Quit();
}
}
};
int
main(int, char**)
{
new TestApp();
// Let the async calls be handled
be_app->Run();
delete be_app;
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,56 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <KernelKit.h>
#include <NetworkKit.h>
using std::cout;
using std::endl;
int
main(int, char**)
{
BUrl yahoo("http://www.yahoo.fr");
BUrlContext c;
BUrlRequest t(yahoo);
t.SetContext(&c);
if (!t.InitCheck()) {
cout << "URL request failed to initialize" << endl;
return EXIT_FAILURE;
}
if (t.Perform() != B_OK) {
cout << "Error while performing request!" << endl;
return EXIT_FAILURE;
}
// Do nothing while the request is not finished
while (t.IsRunning()) {
cout << std::flush;
snooze(10000);
}
// Print the result
cout << "Request result : " << t.Result().StatusCode() << " (" << t.Result().StatusText() << ")" << endl;
//cout << " * " << c.GetCookieJar().CountCookies() << " cookies in context after request" << endl;
cout << " * " << t.Result().Headers().CountHeaders() << " headers received" << endl;
cout << " * " << t.Result().RawData().Position() << " bytes of raw data:" << endl;
cout << t.Result() << endl;
cout << "End of request" << endl << endl;
// Flat view of the cookie jar :
cout << "cookie.txt :" << endl;
ssize_t flatSize = c.GetCookieJar().FlattenedSize();
char *flatCookieJar = new char[flatSize];
c.GetCookieJar().Flatten(flatCookieJar, flatSize);
cout << flatCookieJar << endl << endl;
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,51 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <KernelKit.h>
#include <NetworkKit.h>
using std::cout;
using std::endl;
int
main(int, char**)
{
BUrl google("http://192.168.1.11/~chris/post.php");
BUrlSynchronousRequest t(google);
BHttpForm f;
f.AddString("hello", "world");
if (f.AddFile("_uploadfile", BPath("libservices.so")) != B_OK) {
cout << "File upload failed" << endl;
return EXIT_FAILURE;
}
// Print form
for (BHttpForm::Iterator it(f.GetIterator()); BHttpFormData* element = it.Next(); ) {
cout << element->Name() << ": " << element->Type() << endl;
}
if (!t.InitCheck()) {
cout << "URL request failed to initialize" << endl;
return EXIT_FAILURE;
}
// Inject form in request
bool discard = true;
t.SetProtocolOption(B_HTTPOPT_DISCARD_DATA, &discard);
t.SetProtocolOption(B_HTTPOPT_POSTFIELDS, &f);
if (t.Perform() != B_OK) {
cout << "Error while performing request!" << endl;
return EXIT_FAILURE;
}
t.WaitUntilCompletion();
cout << "Request finished!" << endl;
cout << t.Result() << endl;
return EXIT_SUCCESS;
}