1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 Dreamhost
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.
15 #include "common/armor.h"
16 #include "common/config.h"
17 #include "include/buffer.h"
18 #include "include/encoding.h"
20 #include "gtest/gtest.h"
22 TEST(RoundTrip, SimpleRoundTrip) {
23 static const int OUT_LEN = 4096;
24 const char * const original = "abracadabra";
25 const char * const correctly_encoded = "YWJyYWNhZGFicmE=";
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);
32 memset(out2, 0, sizeof(out2));
33 ceph_unarmor(out2, out2 + OUT_LEN, out, out + alen);
34 ASSERT_STREQ(original, out2);
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;
46 memset(in, 0, sizeof(in));
47 for (int j = 0; j < in_len; ++j) {
48 in[j] = rand_r(&seed) % 0xff;
51 memset(out, 0, sizeof(out));
52 int alen = ceph_armor(out, out + OUT_MAX, in, in + in_len);
56 memset(decoded, 0, sizeof(decoded));
57 int blen = ceph_unarmor(decoded, decoded + IN_MAX, out, out + alen);
60 ASSERT_EQ(memcmp(in, decoded, in_len), 0);
64 TEST(EdgeCase, EndsInNewline) {
65 static const int OUT_MAX = 4096;
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);
76 TEST(FuzzEncoding, BadDecode1) {
77 static const int OUT_LEN = 4096;
78 const char * const bad_encoded = "FAKEBASE64 foo";
80 memset(out, 0, sizeof(out));
81 int alen = ceph_unarmor(out, out + OUT_LEN, bad_encoded, bad_encoded + strlen(bad_encoded));
85 TEST(FuzzEncoding, BadDecode2) {
86 string str("FAKEBASE64 foo");
94 cl.hexdump(std::cerr);
96 catch (const buffer::error &err) {
99 ASSERT_EQ(failed, true);