aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/lib/filters/data_snk.cpp14
-rw-r--r--src/lib/filters/data_snk.h22
-rw-r--r--src/lib/utils/data_src.cpp14
-rw-r--r--src/lib/utils/data_src.h11
-rw-r--r--src/tests/test_filters.cpp57
5 files changed, 83 insertions, 35 deletions
diff --git a/src/lib/filters/data_snk.cpp b/src/lib/filters/data_snk.cpp
index e6dc0d671..7a213cfeb 100644
--- a/src/lib/filters/data_snk.cpp
+++ b/src/lib/filters/data_snk.cpp
@@ -15,7 +15,6 @@
namespace Botan {
-#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
/*
* Write to a stream
*/
@@ -33,35 +32,34 @@ void DataSink_Stream::write(const uint8_t out[], size_t length)
DataSink_Stream::DataSink_Stream(std::ostream& out,
const std::string& name) :
m_identifier(name),
- m_sink_p(nullptr),
m_sink(out)
{
}
+#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
+
/*
* DataSink_Stream Constructor
*/
DataSink_Stream::DataSink_Stream(const std::string& path,
bool use_binary) :
m_identifier(path),
- m_sink_p(new std::ofstream(path,
- use_binary ? std::ios::binary : std::ios::out)),
- m_sink(*m_sink_p)
+ m_sink_memory(new std::ofstream(path, use_binary ? std::ios::binary : std::ios::out)),
+ m_sink(*m_sink_memory)
{
if(!m_sink.good())
{
- delete m_sink_p;
throw Stream_IO_Error("DataSink_Stream: Failure opening " + path);
}
}
+#endif
/*
* DataSink_Stream Destructor
*/
DataSink_Stream::~DataSink_Stream()
{
- delete m_sink_p;
+ // for ~unique_ptr
}
-#endif
}
diff --git a/src/lib/filters/data_snk.h b/src/lib/filters/data_snk.h
index 46aef285c..4c70d7f19 100644
--- a/src/lib/filters/data_snk.h
+++ b/src/lib/filters/data_snk.h
@@ -9,6 +9,7 @@
#define BOTAN_DATA_SINK_H__
#include <botan/filter.h>
+#include <memory>
#include <iosfwd>
namespace Botan {
@@ -27,18 +28,12 @@ class BOTAN_DLL DataSink : public Filter
DataSink(const DataSink&) = delete;
};
-#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
-
/**
* This class represents a data sink which writes its output to a stream.
*/
class BOTAN_DLL DataSink_Stream : public DataSink
{
public:
- std::string name() const override { return m_identifier; }
-
- void write(const uint8_t[], size_t) override;
-
/**
* Construct a DataSink_Stream from a stream.
* @param stream the stream to write to
@@ -47,25 +42,32 @@ class BOTAN_DLL DataSink_Stream : public DataSink
DataSink_Stream(std::ostream& stream,
const std::string& name = "<std::ostream>");
+#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
+
/**
- * Construct a DataSink_Stream from a stream.
+ * Construct a DataSink_Stream from a filesystem path name.
* @param pathname the name of the file to open a stream to
* @param use_binary indicates whether to treat the file
* as a binary file or not
*/
DataSink_Stream(const std::string& pathname,
bool use_binary = false);
+#endif
+
+ std::string name() const override { return m_identifier; }
+
+ void write(const uint8_t[], size_t) override;
~DataSink_Stream();
+
private:
const std::string m_identifier;
- std::ostream* m_sink_p;
+ // May be null, if m_sink was an external reference
+ std::unique_ptr<std::ostream> m_sink_memory;
std::ostream& m_sink;
};
-#endif
-
}
#endif
diff --git a/src/lib/utils/data_src.cpp b/src/lib/utils/data_src.cpp
index 169f8e186..55202daac 100644
--- a/src/lib/utils/data_src.cpp
+++ b/src/lib/utils/data_src.cpp
@@ -101,8 +101,6 @@ DataSource_Memory::DataSource_Memory(const std::string& in) :
{
}
-#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
-
/*
* Read from a stream
*/
@@ -176,6 +174,8 @@ std::string DataSource_Stream::id() const
return m_identifier;
}
+#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
+
/*
* DataSource_Stream Constructor
*/
@@ -194,26 +194,22 @@ DataSource_Stream::DataSource_Stream(const std::string& path,
}
}
+#endif
+
/*
* DataSource_Stream Constructor
*/
DataSource_Stream::DataSource_Stream(std::istream& in,
const std::string& name) :
m_identifier(name),
- m_source_p(nullptr),
m_source(in),
m_total_read(0)
{
}
-/*
-* DataSource_Stream Destructor
-*/
DataSource_Stream::~DataSource_Stream()
{
- delete m_source_p;
+ // for ~unique_ptr
}
-#endif
-
}
diff --git a/src/lib/utils/data_src.h b/src/lib/utils/data_src.h
index ea28b7602..29f557606 100644
--- a/src/lib/utils/data_src.h
+++ b/src/lib/utils/data_src.h
@@ -137,8 +137,6 @@ class BOTAN_DLL DataSource_Memory : public DataSource
size_t m_offset;
};
-#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
-
/**
* This class represents a Stream-Based DataSource.
*/
@@ -154,12 +152,14 @@ class BOTAN_DLL DataSource_Stream : public DataSource
DataSource_Stream(std::istream&,
const std::string& id = "<std::istream>");
+#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
/**
- * Construct a Stream-Based DataSource from file
- * @param file the name of the file
+ * Construct a Stream-Based DataSource from filesystem path
+ * @param file the path to the file
* @param use_binary whether to treat the file as binary or not
*/
DataSource_Stream(const std::string& file, bool use_binary = false);
+#endif
DataSource_Stream(const DataSource_Stream&) = delete;
@@ -171,13 +171,12 @@ class BOTAN_DLL DataSource_Stream : public DataSource
private:
const std::string m_identifier;
+ std::unique_ptr<std::istream> m_source_memory;
std::istream* m_source_p;
std::istream& m_source;
size_t m_total_read;
};
-#endif
-
}
#endif
diff --git a/src/tests/test_filters.cpp b/src/tests/test_filters.cpp
index b3decb5e2..4b41863f5 100644
--- a/src/tests/test_filters.cpp
+++ b/src/tests/test_filters.cpp
@@ -11,6 +11,7 @@
#include <botan/secqueue.h>
#include <botan/pipe.h>
#include <botan/filters.h>
+ #include <botan/data_snk.h>
#include <botan/comp_filter.h>
#include <botan/cipher_filter.h>
#endif
@@ -27,6 +28,8 @@ class Filter_Tests : public Test
std::vector<Test::Result> results;
results.push_back(test_secqueue());
+ results.push_back(test_data_src_sink());
+ results.push_back(test_pipe_io());
results.push_back(test_pipe_hash());
results.push_back(test_pipe_mac());
results.push_back(test_pipe_stream());
@@ -36,7 +39,6 @@ class Filter_Tests : public Test
results.push_back(test_fork());
#if defined(BOTAN_TARGET_OS_HAS_THREADS)
- // Threaded_Fork is broken
results.push_back(test_threaded_fork());
#endif
@@ -79,6 +81,55 @@ class Filter_Tests : public Test
return result;
}
+ Test::Result test_data_src_sink()
+ {
+ Test::Result result("DataSink");
+ std::ostringstream oss;
+
+ Botan::Pipe pipe(new Botan::Hex_Decoder, new Botan::DataSink_Stream(oss));
+
+ Botan::DataSource_Memory input_mem("65666768");
+ pipe.process_msg(input_mem);
+
+ result.test_eq("output string", oss.str(), "efgh");
+
+ Botan::DataSource_Memory input_mem2("41414141");
+ pipe.process_msg(input_mem2);
+
+ result.test_eq("output string", oss.str(), "efghAAAA");
+
+ std::istringstream iss("4343");
+ Botan::DataSource_Stream input_strm(iss);
+ pipe.process_msg(input_strm);
+
+ result.test_eq("output string", oss.str(), "efghAAAACC");
+
+ return result;
+ }
+
+ Test::Result test_pipe_io()
+ {
+ Test::Result result("Pipe I/O operators");
+
+ Botan::Pipe pipe(new Botan::Hex_Encoder);
+
+ pipe.process_msg("ABCD");
+
+ std::ostringstream oss;
+ oss << pipe;
+ result.test_eq("output string", oss.str(), "41424344");
+
+ std::istringstream iss("AAAA");
+ pipe.start_msg();
+ iss >> pipe;
+ pipe.end_msg();
+
+ pipe.set_default_msg(1);
+ oss << pipe;
+ result.test_eq("output string2", oss.str(), "4142434441414141");
+ return result;
+ }
+
Test::Result test_pipe_mac()
{
Test::Result result("Pipe");
@@ -261,10 +312,12 @@ class Filter_Tests : public Test
Test::Result result("Pipe");
Botan::Keyed_Filter* aes = nullptr;
+ const Botan::SymmetricKey some_other_key("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE");
const Botan::SymmetricKey key("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
const Botan::InitializationVector iv("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
- Botan::Pipe pipe(aes = new Botan::StreamCipher_Filter("CTR-BE(AES-128)", key));
+ Botan::Pipe pipe(aes = new Botan::StreamCipher_Filter("CTR-BE(AES-128)", some_other_key));
+ aes->set_key(key);
aes->set_iv(iv);
pipe.process_msg("ABCDEF");