xref: /llvm-project/llvm/unittests/Support/Base64Test.cpp (revision 21e83244cf7ec5bc3eab81677c9cb15c4fb2d581)
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(StringRef(NonPrintableVector, sizeof(NonPrintableVector)),
43              "AAAARgAI/+4=");
44 
45   // Large test case
46   char LargeVector[] = {0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b,
47                         0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f,
48                         0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x73, 0x20, 0x6f,
49                         0x76, 0x65, 0x72, 0x20, 0x31, 0x33, 0x20, 0x6c, 0x61,
50                         0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67, 0x73, 0x2e};
51   TestBase64(LargeVector,
52              "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIDEzIGxhenkgZG9ncy4=");
53 }
54