| 1 |
/* |
| 2 |
* Copyright © 2018 Simon Désaulniers |
| 3 |
* Author: Simon Désaulniers <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of dpaste. |
| 6 |
* |
| 7 |
* dpaste is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* dpaste is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with dpaste. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <algorithm> |
| 22 |
|
| 23 |
#include <catch.hpp> |
| 24 |
|
| 25 |
#include "tests.h" |
| 26 |
#include "bin.h" |
| 27 |
|
| 28 |
namespace dpaste { |
| 29 |
namespace tests { |
| 30 |
|
| 31 |
class PirateBinTester { |
| 32 |
#define YARRRRR |
| 33 |
public: |
| 34 |
static const constexpr unsigned int LOCATION_CODE_LEN = 8; |
| 35 |
static const constexpr char* DPASTE_URI_PREFIX = "dpaste:"; |
| 36 |
|
| 37 |
PirateBinTester () {} |
| 38 |
virtual ~PirateBinTester () {} |
| 39 |
|
| 40 |
std::string code_from_dpaste_uri(const std::string& uri) const { |
| 41 |
return Bin::code_from_dpaste_uri(uri); |
| 42 |
} |
| 43 |
|
| 44 |
std::vector<uint8_t> data_from_stream(std::stringstream&& input_stream) const { |
| 45 |
return Bin::data_from_stream(std::forward<std::stringstream>(input_stream)); |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
TEST_CASE("Bin get/paste on DHT", "[Bin][get][paste]") { |
| 50 |
using pbt = PirateBinTester; |
| 51 |
std::vector<uint8_t> data = {0, 1, 2, 3, 4}; |
| 52 |
Bin bin {}; |
| 53 |
crypto::Cipher::init(); |
| 54 |
SECTION ( "pasting data {0,1,2,3,4}" ) { |
| 55 |
auto code = bin.paste(std::vector<uint8_t> {data}, {}); |
| 56 |
REQUIRE ( code.size() == pbt::LOCATION_CODE_LEN+sizeof(pbt::DPASTE_URI_PREFIX)-1 ); |
| 57 |
|
| 58 |
SECTION ( "getting pasted blob back from the DHT" ) { |
| 59 |
auto rd = bin.get(std::move(code)).second; |
| 60 |
std::vector<uint8_t> rdv {rd.begin(), rd.end()}; |
| 61 |
REQUIRE ( data == rdv ); |
| 62 |
} |
| 63 |
} |
| 64 |
SECTION ( "pasting AES encrypted {0,1,2,3,4}" ) { |
| 65 |
auto p = std::make_unique<dpaste::crypto::Parameters>(); |
| 66 |
p->emplace<crypto::AESParameters>(); |
| 67 |
auto code = bin.paste(std::vector<uint8_t> {data}, std::move(p)); |
| 68 |
REQUIRE ( code.size() == 2*pbt::LOCATION_CODE_LEN+sizeof(pbt::DPASTE_URI_PREFIX)-1 ); |
| 69 |
|
| 70 |
SECTION ( "getting pasted AES encrypted blob back from the DHT" ) { |
| 71 |
auto rd = bin.get(std::move(code)).second; |
| 72 |
std::vector<uint8_t> rdv {rd.begin(), rd.end()}; |
| 73 |
REQUIRE ( data == rdv ); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
TEST_CASE("Bin parsing of uri code ([dpaste:]XXXXXXXX)", "[Bin][code_from_dpaste_uri]") { |
| 79 |
PirateBinTester pt; |
| 80 |
const std::string PIN = random_pin(); |
| 81 |
std::string pin_upper {PIN.begin(), PIN.end()}; |
| 82 |
std::transform(pin_upper.begin(), pin_upper.end(), pin_upper.begin(), ::toupper); |
| 83 |
|
| 84 |
SECTION ( "good pins" ) { |
| 85 |
auto gc1 = "dpaste:"+PIN; |
| 86 |
auto gc2 = PIN; |
| 87 |
REQUIRE ( pt.code_from_dpaste_uri(gc1) == PIN ); |
| 88 |
REQUIRE ( pt.code_from_dpaste_uri(gc2) == PIN ); |
| 89 |
|
| 90 |
SECTION ( "good pin (upper case)" ) { |
| 91 |
auto gc3 = "dpaste:"+pin_upper; |
| 92 |
auto gc4 = pin_upper; |
| 93 |
|
| 94 |
REQUIRE ( pt.code_from_dpaste_uri(gc3) == pin_upper ); |
| 95 |
REQUIRE ( pt.code_from_dpaste_uri(gc4) == pin_upper ); |
| 96 |
} |
| 97 |
} |
| 98 |
SECTION ( "bad pins" ) { |
| 99 |
std::string bc1 = "DPASTE:"+PIN; |
| 100 |
std::string bc2 = "DPaste:"+PIN; |
| 101 |
REQUIRE ( pt.code_from_dpaste_uri(bc1) != PIN ); |
| 102 |
REQUIRE ( pt.code_from_dpaste_uri(bc2) != PIN ); |
| 103 |
|
| 104 |
SECTION ( "bad pins (upper case)" ) { |
| 105 |
auto bc3 = "DPASTE:"+pin_upper; |
| 106 |
auto bc4 = "DPaste:"+pin_upper; |
| 107 |
|
| 108 |
REQUIRE ( pt.code_from_dpaste_uri(bc3) != pin_upper ); |
| 109 |
REQUIRE ( pt.code_from_dpaste_uri(bc4) != pin_upper ); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
TEST_CASE("Bin conversion of stringstream to vector", "[Bin][data_from_stream]") { |
| 115 |
PirateBinTester pt; |
| 116 |
|
| 117 |
const std::string DATA = "SOME DATA"; |
| 118 |
std::stringstream ss(DATA); |
| 119 |
std::vector<uint8_t> d {DATA.begin(), DATA.end()}; |
| 120 |
REQUIRE ( pt.data_from_stream(std::move(ss)) == d ); |
| 121 |
} |
| 122 |
|
| 123 |
} /* tests */ |
| 124 |
} /* dpaste */ |
| 125 |
|
| 126 |
/* vim: set ts=4 sw=4 tw=120 et :*/ |
| 127 |
|