]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2011 Dreamhost | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include "common/armor.h" | |
16 | #include "common/config.h" | |
17 | #include "include/buffer.h" | |
18 | #include "include/encoding.h" | |
19 | ||
20 | #include "gtest/gtest.h" | |
21 | ||
22 | TEST(RoundTrip, SimpleRoundTrip) { | |
23 | static const int OUT_LEN = 4096; | |
24 | const char * const original = "abracadabra"; | |
25 | const char * const correctly_encoded = "YWJyYWNhZGFicmE="; | |
26 | char out[OUT_LEN]; | |
27 | memset(out, 0, sizeof(out)); | |
28 | int alen = ceph_armor(out, out + OUT_LEN, original, original + strlen(original)); | |
29 | ASSERT_STREQ(correctly_encoded, out); | |
30 | ||
31 | char out2[OUT_LEN]; | |
32 | memset(out2, 0, sizeof(out2)); | |
33 | ceph_unarmor(out2, out2 + OUT_LEN, out, out + alen); | |
34 | ASSERT_STREQ(original, out2); | |
35 | } | |
36 | ||
37 | TEST(RoundTrip, RandomRoundTrips) { | |
38 | static const int IN_MAX = 1024; | |
39 | static const int OUT_MAX = 4096; | |
40 | static const int ITERS = 1000; | |
41 | for (int i = 0; i < ITERS; ++i) { | |
42 | unsigned int seed = i; | |
43 | int in_len = rand_r(&seed) % IN_MAX; | |
44 | ||
45 | char in[IN_MAX]; | |
46 | memset(in, 0, sizeof(in)); | |
47 | for (int j = 0; j < in_len; ++j) { | |
48 | in[j] = rand_r(&seed) % 0xff; | |
49 | } | |
50 | char out[OUT_MAX]; | |
51 | memset(out, 0, sizeof(out)); | |
52 | int alen = ceph_armor(out, out + OUT_MAX, in, in + in_len); | |
53 | ASSERT_GE(alen, 0); | |
54 | ||
55 | char decoded[IN_MAX]; | |
56 | memset(decoded, 0, sizeof(decoded)); | |
57 | int blen = ceph_unarmor(decoded, decoded + IN_MAX, out, out + alen); | |
58 | ASSERT_GE(blen, 0); | |
59 | ||
60 | ASSERT_EQ(memcmp(in, decoded, in_len), 0); | |
61 | } | |
62 | } | |
63 | ||
64 | TEST(EdgeCase, EndsInNewline) { | |
65 | static const int OUT_MAX = 4096; | |
66 | ||
67 | char b64[] = | |
68 | "aaaa\n"; | |
69 | ||
70 | char decoded[OUT_MAX]; | |
71 | memset(decoded, 0, sizeof(decoded)); | |
72 | int blen = ceph_unarmor(decoded, decoded + OUT_MAX, b64, b64 + sizeof(b64)-1); | |
73 | ASSERT_GE(blen, 0); | |
74 | } | |
75 | ||
76 | TEST(FuzzEncoding, BadDecode1) { | |
77 | static const int OUT_LEN = 4096; | |
78 | const char * const bad_encoded = "FAKEBASE64 foo"; | |
79 | char out[OUT_LEN]; | |
80 | memset(out, 0, sizeof(out)); | |
81 | int alen = ceph_unarmor(out, out + OUT_LEN, bad_encoded, bad_encoded + strlen(bad_encoded)); | |
82 | ASSERT_LT(alen, 0); | |
83 | } | |
84 | ||
85 | TEST(FuzzEncoding, BadDecode2) { | |
86 | string str("FAKEBASE64 foo"); | |
87 | bool failed = false; | |
88 | try { | |
89 | bufferlist bl; | |
90 | bl.append(str); | |
91 | ||
92 | bufferlist cl; | |
93 | cl.decode_base64(bl); | |
94 | cl.hexdump(std::cerr); | |
95 | } | |
96 | catch (const buffer::error &err) { | |
97 | failed = true; | |
98 | } | |
99 | ASSERT_EQ(failed, true); | |
100 | } |