xref: /llvm-project/llvm/unittests/Support/Base64Test.cpp (revision 1454c27b60447d969d0c1ecaf20b2186fe9d85ec)
1 //===- llvm/unittest/Support/Base64Test.cpp - Base64 tests
2 //--------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements unit tests for the Base64 functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/Base64.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "gtest/gtest.h"
17 
18 using namespace llvm;
19 
20 namespace {
21 /// Tests an arbitrary set of bytes passed as \p Input.
22 void TestBase64(StringRef Input, StringRef Final) {
23   auto Res = encodeBase64(Input);
24   EXPECT_EQ(Res, Final);
25 }
26 
27 } // namespace
28 
29 TEST(Base64Test, Base64) {
30   // from: https://tools.ietf.org/html/rfc4648#section-10
31   TestBase64("", "");
32   TestBase64("f", "Zg==");
33   TestBase64("fo", "Zm8=");
34   TestBase64("foo", "Zm9v");
35   TestBase64("foob", "Zm9vYg==");
36   TestBase64("fooba", "Zm9vYmE=");
37   TestBase64("foobar", "Zm9vYmFy");
38 
39   // With non-printable values.
40   char NonPrintableVector[] = {0x00, 0x00, 0x00,       0x46,
41                                0x00, 0x08, (char)0xff, (char)0xee};
42   TestBase64({NonPrintableVector, sizeof(NonPrintableVector)}, "AAAARgAI/+4=");
43 
44   // Large test case
45   char LargeVector[] = {0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b,
46                         0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f,
47                         0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x73, 0x20, 0x6f,
48                         0x76, 0x65, 0x72, 0x20, 0x31, 0x33, 0x20, 0x6c, 0x61,
49                         0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67, 0x73, 0x2e};
50   TestBase64({LargeVector, sizeof(LargeVector)},
51              "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIDEzIGxhenkgZG9ncy4=");
52 }
53