Browse code
Refactor and test base32 codec
Ed Langley authored on 07/06/2017 16:13:30
Showing 3 changed files
Showing 3 changed files
... | ... |
@@ -9,10 +9,9 @@ |
9 | 9 |
* at https://github.com/cjdev/dual-control. |
10 | 10 |
*/ |
11 | 11 |
|
12 |
-#include <iostream> |
|
13 |
-#include <iomanip> |
|
14 |
-#include <vector> |
|
12 |
+#include <bitset> |
|
15 | 13 |
#include <unordered_map> |
14 |
+#include <memory> |
|
16 | 15 |
|
17 | 16 |
#include "base32.h" |
18 | 17 |
|
... | ... |
@@ -201,6 +200,7 @@ public: |
201 | 200 |
|
202 | 201 |
template class std::vector<unsigned char>; |
203 | 202 |
|
204 |
-base32::base32 ( |
|
203 |
+base32::base32 (): |
|
205 | 204 |
delegate_ (std::make_shared<base32_impl> ()) |
206 | 205 |
{} |
206 |
+ |
... | ... |
@@ -20,7 +20,7 @@ class base32_ifc |
20 | 20 |
{ |
21 | 21 |
public: |
22 | 22 |
virtual std::string encode (std::vector<uint8_t> input) = 0; |
23 |
- virtual std::vector<uint8_t> decode (std::vector<uint8_t> input) = 0; |
|
23 |
+ virtual std::vector<uint8_t> decode (std::string input) = 0; |
|
24 | 24 |
}; |
25 | 25 |
|
26 | 26 |
class base32 |
... | ... |
@@ -32,13 +32,16 @@ private: |
32 | 32 |
public: |
33 | 33 |
base32 (); |
34 | 34 |
|
35 |
- std::string encode (std::vector<uint8_t> input) { |
|
36 |
- return delegate_->encode(input); |
|
35 |
+ std::string encode (std::vector<uint8_t> input) |
|
36 |
+ { |
|
37 |
+ return delegate_->encode (input); |
|
37 | 38 |
}; |
38 | 39 |
|
39 |
- std::vector<uint8_t> decode (std::vector<uint8_t> input) { |
|
40 |
- return delegate_->decode(input); |
|
40 |
+ std::vector<uint8_t> decode (std::string input) |
|
41 |
+ { |
|
42 |
+ return delegate_->decode (input); |
|
41 | 43 |
}; |
42 | 44 |
}; |
43 | 45 |
|
44 | 46 |
#endif |
47 |
+ |
... | ... |
@@ -20,42 +20,69 @@ |
20 | 20 |
#include "base32.h" |
21 | 21 |
#include "test_util.h" |
22 | 22 |
|
23 |
-// totp test |
|
24 |
-int int_precomputed() |
|
23 |
+int precomputed_values() |
|
25 | 24 |
{ |
26 | 25 |
// given |
27 | 26 |
std::map<std::string, std::string> precomputed = { |
28 |
- {"consimilate", "mnxw443jnvuwyylumu======"}, |
|
29 |
- {"defacing", "mrswmyldnfxgo==="}, |
|
30 |
- {"downcome", "mrxxo3tdn5wwk==="}, |
|
31 |
- {"filchery", "mzuwyy3imvzhs==="}, |
|
32 |
- {"Galatic", "i5qwyylunfrq===="}, |
|
33 |
- {"hearthrug", "nbswc4tunbzhkzy="}, |
|
34 |
- {"heterotypic", "nbsxizlsn52hs4djmm======"}, |
|
35 |
- {"kinase", "nnuw4yltmu======"}, |
|
36 |
- {"Lycopodiales", "jr4wg33qn5sgsylmmvzq===="}, |
|
37 |
- {"mosker", "nvxxg23foi======"}, |
|
38 |
- {"ornithosaurian", "n5zg42lunbxxgylvojuwc3q="}, |
|
39 |
- {"quilkin", "of2ws3dlnfxa===="}, |
|
40 |
- {"swartly", "on3wc4tunr4q===="}, |
|
41 |
- {"teleost", "orswyzlpon2a===="}, |
|
42 |
- {"thinglet", "orugs3thnrsxi==="}, |
|
43 |
- {"unpregnant", "ovxha4tfm5xgc3tu"}, |
|
44 |
- {"unreachably", "ovxhezlbmnugcytmpe======"}, |
|
45 |
- {"unusableness", "ovxhk43bmjwgk3tfonzq===="}, |
|
46 |
- {"wickawee", "o5uwg23bo5swk==="}, |
|
47 |
- {"yareta", "pfqxezlume======"}, |
|
27 |
+ {"consimilate", "MNXW443JNVUWYYLUMU======"}, |
|
28 |
+ {"defacing", "MRSWMYLDNFXGO==="}, |
|
29 |
+ {"downcome", "MRXXO3TDN5WWK==="}, |
|
30 |
+ {"filchery", "MZUWYY3IMVZHS==="}, |
|
31 |
+ {"Galatic", "I5QWYYLUNFRQ===="}, |
|
32 |
+ {"hearthrug", "NBSWC4TUNBZHKZY="}, |
|
33 |
+ {"heterotypic", "NBSXIZLSN52HS4DJMM======"}, |
|
34 |
+ {"kinase", "NNUW4YLTMU======"}, |
|
35 |
+ {"Lycopodiales", "JR4WG33QN5SGSYLMMVZQ===="}, |
|
36 |
+ {"mosker", "NVXXG23FOI======"}, |
|
37 |
+ {"ornithosaurian", "N5ZG42LUNBXXGYLVOJUWC3Q="}, |
|
38 |
+ {"quilkin", "OF2WS3DLNFXA===="}, |
|
39 |
+ {"swartly", "ON3WC4TUNR4Q===="}, |
|
40 |
+ {"teleost", "ORSWYZLPON2A===="}, |
|
41 |
+ {"thinglet", "ORUGS3THNRSXI==="}, |
|
42 |
+ {"unpregnant", "OVXHA4TFM5XGC3TU"}, |
|
43 |
+ {"unreachably", "OVXHEZLBMNUGCYTMPE======"}, |
|
44 |
+ {"unusableness", "OVXHK43BMJWGK3TFONZQ===="}, |
|
45 |
+ {"wickawee", "O5UWG23BO5SWK==="}, |
|
46 |
+ {"yareta", "PFQXEZLUME======"}, |
|
48 | 47 |
}; |
49 | 48 |
|
50 | 49 |
// The token for key 76I6WTYEUTNCJUREMGKVM45PMA and time '2017/01/01 00:00:00' is 258675 |
51 |
- base32 codec; |
|
50 |
+ base32 codec = base32(); |
|
52 | 51 |
|
52 |
+ for (const auto &entry: precomputed) { |
|
53 |
+ std::string encoded = codec.encode ( std::vector<uint8_t> |
|
54 |
+ (entry.first.begin(), entry.first.end()) ); |
|
55 |
+ check (encoded == entry.second, "precomputed value didn't match"); |
|
56 |
+ } |
|
53 | 57 |
|
58 |
+ succeed(); |
|
59 |
+} |
|
60 |
+ |
|
61 |
+int roundtrip() |
|
62 |
+{ |
|
63 |
+ // given |
|
64 |
+ std::vector<std::vector<uint8_t>> values = { |
|
65 |
+ { 0 }, { 1, 1 }, { 2, 2, 2 }, { 3, 3, 3, 3 }, { 4, 4, 4, 4, 4 }, |
|
66 |
+ { 5, 5, 5, 5, 5, 5, }, { 6, 6, 6, 6, 6, 6, 6, } |
|
67 |
+ }; |
|
68 |
+ |
|
69 |
+ // The token for key 76I6WTYEUTNCJUREMGKVM45PMA and time '2017/01/01 00:00:00' is 258675 |
|
70 |
+ base32 codec = base32(); |
|
71 |
+ |
|
72 |
+ for (auto expected: values) { |
|
73 |
+ std::string encoded = codec.encode (expected); |
|
74 |
+ std::vector<std::uint8_t> actual = codec.decode (encoded); |
|
75 |
+ |
|
76 |
+ check (expected == actual, "precomputed value didn't match"); |
|
77 |
+ } |
|
78 |
+ |
|
79 |
+ succeed(); |
|
54 | 80 |
} |
55 | 81 |
|
56 | 82 |
int run_tests() |
57 | 83 |
{ |
58 |
- test (int_precomputed); |
|
84 |
+ test (precomputed_values); |
|
85 |
+ test (roundtrip); |
|
59 | 86 |
succeed(); |
60 | 87 |
} |
61 | 88 |
|
... | ... |
@@ -63,3 +90,4 @@ int main (int argc, char *argv[]) |
63 | 90 |
{ |
64 | 91 |
return !run_tests(); |
65 | 92 |
} |
93 |
+ |