2016-04-18 17:52:10 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2016-09-30 08:46:28 -05:00
|
|
|
#include <ChildSession.hpp>
|
2016-04-18 17:52:10 -05:00
|
|
|
#include <Common.hpp>
|
2016-11-24 08:56:06 -06:00
|
|
|
#include <Kit.hpp>
|
|
|
|
#include <Protocol.hpp>
|
2016-09-27 10:03:39 -05:00
|
|
|
#include <MessageQueue.hpp>
|
2016-04-18 17:52:10 -05:00
|
|
|
#include <Util.hpp>
|
|
|
|
|
|
|
|
/// WhiteBox unit-tests.
|
|
|
|
class WhiteBoxTests : public CPPUNIT_NS::TestFixture
|
|
|
|
{
|
|
|
|
CPPUNIT_TEST_SUITE(WhiteBoxTests);
|
|
|
|
|
2016-09-26 11:31:19 -05:00
|
|
|
CPPUNIT_TEST(testLOOLProtocolFunctions);
|
2016-04-18 17:52:10 -05:00
|
|
|
CPPUNIT_TEST(testRegexListMatcher);
|
2016-04-18 20:11:33 -05:00
|
|
|
CPPUNIT_TEST(testRegexListMatcher_Init);
|
2016-09-30 08:46:28 -05:00
|
|
|
CPPUNIT_TEST(testEmptyCellCursor);
|
2016-04-18 17:52:10 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2016-09-26 11:31:19 -05:00
|
|
|
void testLOOLProtocolFunctions();
|
2016-04-18 17:52:10 -05:00
|
|
|
void testRegexListMatcher();
|
2016-04-18 20:11:33 -05:00
|
|
|
void testRegexListMatcher_Init();
|
2016-09-30 08:46:28 -05:00
|
|
|
void testEmptyCellCursor();
|
2016-04-18 17:52:10 -05:00
|
|
|
};
|
|
|
|
|
2016-09-26 11:31:19 -05:00
|
|
|
void WhiteBoxTests::testLOOLProtocolFunctions()
|
|
|
|
{
|
|
|
|
int foo;
|
|
|
|
CPPUNIT_ASSERT(LOOLProtocol::getTokenInteger("foo=42", "foo", foo));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(42, foo);
|
|
|
|
|
|
|
|
std::string bar;
|
|
|
|
CPPUNIT_ASSERT(LOOLProtocol::getTokenString("bar=hello-sailor", "bar", bar));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("hello-sailor"), bar);
|
|
|
|
|
|
|
|
int mumble;
|
|
|
|
std::map<std::string, int> map { { "hello", 1 }, { "goodbye", 2 }, { "adieu", 3 } };
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(LOOLProtocol::getTokenKeyword("mumble=goodbye", "mumble", map, mumble));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(2, mumble);
|
|
|
|
|
|
|
|
std::string message("hello x=1 y=2 foo=42 bar=hello-sailor mumble=goodbye zip zap");
|
|
|
|
Poco::StringTokenizer tokens(message, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(LOOLProtocol::getTokenInteger(tokens, "foo", foo));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(42, foo);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(LOOLProtocol::getTokenString(tokens, "bar", bar));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("hello-sailor"), bar);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(LOOLProtocol::getTokenKeyword(tokens, "mumble", map, mumble));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(2, mumble);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(LOOLProtocol::getTokenIntegerFromMessage(message, "foo", foo));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(42, foo);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(LOOLProtocol::getTokenStringFromMessage(message, "bar", bar));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("hello-sailor"), bar);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(LOOLProtocol::getTokenKeywordFromMessage(message, "mumble", map, mumble));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(2, mumble);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-18 17:52:10 -05:00
|
|
|
void WhiteBoxTests::testRegexListMatcher()
|
|
|
|
{
|
|
|
|
Util::RegexListMatcher matcher;
|
|
|
|
|
|
|
|
matcher.allow("localhost");
|
|
|
|
CPPUNIT_ASSERT(matcher.match("localhost"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match(""));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("localhost2"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("xlocalhost"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("192.168.1.1"));
|
|
|
|
|
|
|
|
matcher.deny("localhost");
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("localhost"));
|
|
|
|
|
|
|
|
matcher.allow("www[0-9].*");
|
|
|
|
CPPUNIT_ASSERT(matcher.match("www1example"));
|
|
|
|
|
|
|
|
matcher.allow("192\\.168\\..*\\..*");
|
|
|
|
CPPUNIT_ASSERT(matcher.match("192.168.1.1"));
|
|
|
|
CPPUNIT_ASSERT(matcher.match("192.168.159.1"));
|
|
|
|
CPPUNIT_ASSERT(matcher.match("192.168.1.134"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("192.169.1.1"));
|
|
|
|
CPPUNIT_ASSERT(matcher.match("192.168.."));
|
|
|
|
|
|
|
|
matcher.deny("192\\.168\\.1\\..*");
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("192.168.1.1"));
|
|
|
|
|
|
|
|
matcher.allow("staging\\.collaboracloudsuite\\.com.*");
|
|
|
|
matcher.deny(".*collaboracloudsuite.*");
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("staging.collaboracloudsuite"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("web.collaboracloudsuite"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("staging.collaboracloudsuite.com"));
|
2016-04-18 20:11:33 -05:00
|
|
|
|
|
|
|
matcher.allow("10\\.10\\.[0-9]{1,3}\\.[0-9]{1,3}");
|
|
|
|
matcher.deny("10\\.10\\.10\\.10");
|
|
|
|
CPPUNIT_ASSERT(matcher.match("10.10.001.001"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("10.10.10.10"));
|
|
|
|
CPPUNIT_ASSERT(matcher.match("10.10.250.254"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void WhiteBoxTests::testRegexListMatcher_Init()
|
|
|
|
{
|
|
|
|
Util::RegexListMatcher matcher({"localhost", "192\\..*"}, {"192\\.168\\..*"});
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(matcher.match("localhost"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match(""));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("localhost2"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("xlocalhost"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("192.168.1.1"));
|
|
|
|
CPPUNIT_ASSERT(matcher.match("192.172.10.122"));
|
|
|
|
|
|
|
|
matcher.deny("localhost");
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("localhost"));
|
|
|
|
|
|
|
|
matcher.allow("www[0-9].*");
|
|
|
|
CPPUNIT_ASSERT(matcher.match("www1example"));
|
|
|
|
|
|
|
|
matcher.allow("192\\.168\\..*\\..*");
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("192.168.1.1"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("192.168.159.1"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("192.168.1.134"));
|
|
|
|
CPPUNIT_ASSERT(matcher.match("192.169.1.1"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("192.168.."));
|
|
|
|
|
|
|
|
matcher.clear();
|
|
|
|
|
|
|
|
matcher.allow("192\\.168\\..*\\..*");
|
|
|
|
CPPUNIT_ASSERT(matcher.match("192.168.1.1"));
|
|
|
|
CPPUNIT_ASSERT(matcher.match("192.168.159.1"));
|
|
|
|
CPPUNIT_ASSERT(matcher.match("192.168.1.134"));
|
|
|
|
CPPUNIT_ASSERT(!matcher.match("192.169.1.1"));
|
|
|
|
CPPUNIT_ASSERT(matcher.match("192.168.."));
|
2016-04-18 17:52:10 -05:00
|
|
|
}
|
|
|
|
|
2016-09-30 08:46:28 -05:00
|
|
|
/// A stub IDocumentManager implementation for unit test purposes.
|
|
|
|
class DummyDocument : public IDocumentManager
|
|
|
|
{
|
|
|
|
std::shared_ptr<TileQueue> _tileQueue;
|
|
|
|
std::mutex _mutex;
|
2016-11-15 04:37:47 -06:00
|
|
|
std::mutex _documentMutex;
|
2016-09-30 08:46:28 -05:00
|
|
|
public:
|
|
|
|
DummyDocument()
|
|
|
|
: _tileQueue(new TileQueue()),
|
2016-11-15 04:37:47 -06:00
|
|
|
_mutex(),
|
|
|
|
_documentMutex()
|
2016-09-30 08:46:28 -05:00
|
|
|
{
|
|
|
|
}
|
2016-11-15 04:37:47 -06:00
|
|
|
bool onLoad(const std::string& /*sessionId*/,
|
|
|
|
const std::string& /*jailedFilePath*/,
|
|
|
|
const std::string& /*userName*/,
|
|
|
|
const std::string& /*docPassword*/,
|
|
|
|
const std::string& /*renderOpts*/,
|
|
|
|
const bool /*haveDocPassword*/) override
|
2016-09-30 08:46:28 -05:00
|
|
|
{
|
2016-11-15 04:37:47 -06:00
|
|
|
return false;
|
2016-09-30 08:46:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void onUnload(const ChildSession& /*session*/) override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-15 04:37:47 -06:00
|
|
|
std::shared_ptr<lok::Document> getLOKitDocument() override
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-10-05 21:06:02 -05:00
|
|
|
void notifyViewInfo(const std::vector<int>& /*viewIds*/) override
|
2016-09-30 08:46:28 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-26 10:08:25 -05:00
|
|
|
std::map<int, UserInfo> getViewInfo() override
|
2016-09-30 08:46:28 -05:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::mutex& getMutex() override
|
|
|
|
{
|
|
|
|
return _mutex;
|
|
|
|
}
|
|
|
|
|
2016-11-15 04:37:47 -06:00
|
|
|
std::mutex& getDocumentMutex() override
|
|
|
|
{
|
|
|
|
return _mutex;
|
|
|
|
}
|
|
|
|
|
2016-09-30 08:46:28 -05:00
|
|
|
std::shared_ptr<TileQueue>& getTileQueue() override
|
|
|
|
{
|
|
|
|
return _tileQueue;
|
|
|
|
}
|
2016-10-09 10:28:22 -05:00
|
|
|
|
|
|
|
bool sendTextFrame(const std::string& /*message*/) override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-30 08:46:28 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
void WhiteBoxTests::testEmptyCellCursor()
|
|
|
|
{
|
|
|
|
DummyDocument document;
|
|
|
|
CallbackDescriptor callbackDescriptor{&document, 0};
|
|
|
|
// This failed as stoi raised an std::invalid_argument exception.
|
|
|
|
documentViewCallback(LOK_CALLBACK_CELL_CURSOR, "EMPTY", &callbackDescriptor);
|
|
|
|
}
|
|
|
|
|
2016-04-18 17:52:10 -05:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(WhiteBoxTests);
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|