root/trunk/0022_DigestEngine/DigestEngineTest.cpp

User picture

Author: Setsu

Revision: 348 («Previous)


File Size: 5.11 KB

(August 30, 2011 02:21 UTC) 9 months ago

Poco::MD2Engine removed from Poco-1.4.2.

 
Show/hide line numbers
//
//  DigestEngineTest.cpp
//
//  Created by Setsu on 5/29/2010.
//  Copyright 2010 RoundSquare Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// 
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//

//----------------------------------------
//	include
//----------------------------------------
#include <Poco/Logger.h>
#include <Poco/PatternFormatter.h>
#include <Poco/FormattingChannel.h>
#include <Poco/ConsoleChannel.h>
#include <Poco/URI.h>

#include <Poco/TypeList.h>
#include <Poco/DigestEngine.h>
#ifndef POCO_VERSION
#include <Poco/Version.h>
#endif
#if (POCO_VERSION < 0x01040200)
#include <Poco/MD2Engine.h>
#endif
#include <Poco/MD4Engine.h>
#include <Poco/MD5Engine.h>
#include <Poco/SHA1Engine.h>
#include <Poco/HMACEngine.h>
#include <Poco/DigestStream.h>
#include <Poco/Format.h>
#include <Poco/String.h>

#include <string>
#include <sstream>
#include <vector>
#include <utility>
#include <algorithm>

#include "ScopedLogMessage.h"
#include "HTTPGetter.h"

//----------------------------------------
//	const
//----------------------------------------
const char* kPassphrase = "Poco";

//----------------------------------------
//	TestDigestEngine
//----------------------------------------
class TestDigestEngine
{
public:
	TestDigestEngine(ScopedLogMessage& msg, HTTPGetter& getter, const Poco::URI& uri, const char* passphrase=kPassphrase) :
		m_msg(msg)
	,	m_getter(getter)
	,	m_uri(uri)
	,	m_passphrase(passphrase)
	{
	}
	void operator()(std::pair<Poco::DigestEngine*, std::string>& item)
	{
		Poco::DigestOutputStream dos(*item.first);
		m_getter.Get(m_uri.getPath(), dos);
		dos.close();
		std::string digestStr(Poco::DigestEngine::digestToHex(item.first->digest()));

		Poco::URI digestUri("http://poco.roundsquare.net/downloads/test."+item.second);
		HTTPGetter digestgetter(digestUri.getHost(), digestUri.getPort());
		std::stringstream ss;
		digestgetter.Get(digestUri.getPath(), ss);
		m_msg.Message(Poco::format("   %s: %s [%s]"
								,	Poco::toUpper((4 == item.second.length()) ? item.second:(" "+item.second))
								,	digestStr
								,	std::string((0 == ss.str().compare(digestStr)) ? "OK":"NG"))	);
		
		delete item.first;
		item.first = NULL;
	}

private:
	ScopedLogMessage&	m_msg;
	HTTPGetter&			m_getter;
	const Poco::URI&	m_uri;
	const char*			m_passphrase;
};

//----------------------------------------
//	PrepareConsoleLogger
//----------------------------------------
void PrepareConsoleLogger(const std::string& name, int level=Poco::Message::PRIO_INFORMATION)
{
	Poco::FormattingChannel* pFCConsole = new Poco::FormattingChannel(new Poco::PatternFormatter("%t"));
	pFCConsole->setChannel(new Poco::ConsoleChannel);
	pFCConsole->open();

	Poco::Logger::create(name, pFCConsole, level);
}

//----------------------------------------
//	main
//----------------------------------------
int main(int /*argc*/, char** /*argv*/)
{
	PrepareConsoleLogger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION);

	ScopedLogMessage msg("DigestEngineTest ", "start", "end");

	Poco::URI uri("http://poco.roundsquare.net/downloads/test.txt");
	HTTPGetter getter(uri.getHost(), uri.getPort());
	std::stringstream ss;
	getter.Get(uri.getPath(), ss);
	msg.Message("  source text:");
	std::cout << ss.str();

	std::vector< std::pair<Poco::DigestEngine*, std::string> > engineVec;
#if (POCO_VERSION < 0x01040200)
	engineVec.push_back(std::pair<Poco::DigestEngine*, std::string>(new Poco::MD2Engine, "md2"));
#endif
	engineVec.push_back(std::pair<Poco::DigestEngine*, std::string>(new Poco::MD4Engine, "md4"));
	engineVec.push_back(std::pair<Poco::DigestEngine*, std::string>(new Poco::MD5Engine, "md5"));
	engineVec.push_back(std::pair<Poco::DigestEngine*, std::string>(new Poco::SHA1Engine, "sha1"));
	engineVec.push_back(std::pair<Poco::DigestEngine*, std::string>(new Poco::HMACEngine<Poco::MD5Engine>(kPassphrase), "hmac"));
	std::for_each(engineVec.begin(), engineVec.end(), TestDigestEngine(msg, getter, uri));

	return 0;
}