TFCweb  1.0.4 $Rev: 483 $
TFC Primavera 2012: Nucli d'un servidor web
TestBase64.cc
Veure la documentació d'aquest fitxer.
1 
11 /*
12  * Copyright (c) 2012 Toni Corvera
13  *
14  * This file is part of TFCWeb.
15  *
16  * TFCWeb is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * TFCWeb is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with TFCWeb. If not, see <http://www.gnu.org/licenses/>.
28  */
29 
30 #include <fstream>
31 #include <string>
32 
33 #include <Base64.h>
34 #include <portabilitat.h>
35 
36 #include "comu.h"
37 
38 using namespace std;
39 using namespace tfc;
40 using namespace tfc::Base64;
41 
42 
43 #if 0
44 // Filtrat amb Boost.Iostreams. Aquest codi no és necessari però l'he escrit per
45 // aprofundir en l'ús de la biblioteca abans d'escriure filtres com Chunked (Transformacions.h)
46 
47 #include <cstdio> // EOF
48 #include <boost/iostreams/concepts.hpp>
49 #include <boost/iostreams/categories.hpp>
50 #include <boost/iostreams/operations.hpp>
51 #include <boost/iostreams/invert.hpp>
52 #include <boost/iostreams/device/file_descriptor.hpp>
53 #include <boost/iostreams/filtering_stream.hpp>
54 
55 namespace tfc {
56 
57 namespace Base64 {
58 
59 namespace io = boost::iostreams;
60 
61 class CodificadorBase64 : public io::multichar_output_filter {
62 public:
63  template<typename TDesti>
64  streamsize write(TDesti& dest, const char * buf, streamsize mida) {
65  const string codificat = codifica(string(buf, mida));
66  return dest.write(codificat.c_str(), codificat.length());
67  }
68 
69 };
70 
71 }} // ns
72 
73 #endif
74 
75 namespace tfc { namespace Fixtures {
76 
77 // generat amb bash:
78 // echo -n '{ '; for i in $(seq 1 40); do printf '0x%x, ' $(($RANDOM % 256)); done ; echo '0x00 }'
79 const unsigned char test_base64_cadena_binaria[] = { 0x2d, 0xe1, 0xb0, 0xa9, 0x28, 0x8c, 0x5a,
80  0x21, 0xd4, 0xb5, 0x84, 0x67, 0x7f, 0x96, 0x18, 0xa, 0x0, 0x15, 0x3f, 0xcd,
81  0x3f, 0x7f, 0x58, 0x27, 0x96, 0xd2, 0x8d, 0x63, 0xab, 0x17, 0x4, 0x25, 0xec,
82  0x9c, 0xeb, 0x5, 0x5e, 0xa3, 0xf6, 0xa8, 0x00 };
83 
84 struct FxtBase64 {
86  : text_llarg("Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
87  "In in urna ligula. Nunc condimentum pretium urna eu pharetra. "
88  "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
89  "Donec auctor felis sed ipsum tristique fermentum. "
90  "Vivamus id luctus justo. Phasellus enim neque, ultricies sed "
91  "varius at, vehicula aliquam elit. Sed molestie condimentum "
92  "erat, at commodo tortor consectetur eu. Donec lacinia lorem "
93  "vel arcu dictum commodo eget at libero. Cras rutrum quam "
94  "quis tortor tincidunt at convallis turpis euismod.")
95  {
96  const char * principi = reinterpret_cast<const char *>(test_base64_cadena_binaria);
97  const char * final = principi + 40;
98  cadena_binaria.assign(principi, final);
99  }
100  string text_llarg;
102 };
103 
104 } // ns Fixtures
105 } // ns tfc
106 
107 BOOST_FIXTURE_TEST_SUITE( Base64_TestSuite, Fixtures::FxtBase64 )
108 
109 BOOST_AUTO_TEST_CASE( sense_padding )
110 {
111  BOOST_CHECK_EQUAL( codifica("Sense pad"), "U2Vuc2UgcGFk" );
112  BOOST_CHECK_EQUAL( descodifica("U2Vuc2UgcGFk"), "Sense pad" );
113 }
114 
115 BOOST_AUTO_TEST_CASE( padding_1_caracter )
116 {
117  BOOST_CHECK_EQUAL( codifica("PAD=1"), "UEFEPTE=" );
118  BOOST_CHECK_EQUAL( descodifica("UEFEPTE="), "PAD=1" );
119 }
120 
121 BOOST_AUTO_TEST_CASE( padding_2_caracters )
122 {
123  BOOST_CHECK_EQUAL( codifica("padding: 2"), "cGFkZGluZzogMg==" );
124  BOOST_CHECK_EQUAL( descodifica("cGFkZGluZzogMg=="), "padding: 2" );
125  BOOST_CHECK_EQUAL( codifica("abcdefghijkml"), "YWJjZGVmZ2hpamttbA==" );
126 }
127 
129 {
130  BOOST_CHECK_EQUAL( text_llarg, descodifica(codifica(text_llarg)) );
131  BOOST_CHECK_EQUAL( cadena_binaria, descodifica(codifica(cadena_binaria)) );
132  /*
133  ifstream ifs("/dev/urandom");
134  char buf[1024];
135  ifs.read(buf, 1024);
136  ifs.close();
137  const string sx(buf, 1024);
138  BOOST_CHECK_EQUAL( sx, descodifica(codifica(sx)) );
139  */
140 }
141 
142 #if 0
143 BOOST_AUTO_TEST_CASE( boost_iostreams )
144 {
145  namespace io = boost::iostreams;
146  string nom;
147  std::shared_ptr<std::ofstream> tmp = portabilitat::fitxer_temporal("base64", nom);
148  cerr << "Fitxer temporal: " << nom << endl;
149  tmp->close();
150  unlink(nom.c_str());
151 
152 // io::basic_file_sink(nom.c_str());
153  io::filtering_ostream out;
154  out.push(CodificadorBase64());
155 }
156 #endif
157 
158 BOOST_AUTO_TEST_SUITE_END()
159 
160 // vim:set ts=4 et ai: //