diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /security/nss/gtests/util_gtest/util_b64_unittest.cc | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'security/nss/gtests/util_gtest/util_b64_unittest.cc')
-rw-r--r-- | security/nss/gtests/util_gtest/util_b64_unittest.cc | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/security/nss/gtests/util_gtest/util_b64_unittest.cc b/security/nss/gtests/util_gtest/util_b64_unittest.cc new file mode 100644 index 000000000..5a691fff7 --- /dev/null +++ b/security/nss/gtests/util_gtest/util_b64_unittest.cc @@ -0,0 +1,79 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include <climits> +#include <memory> +#include "nssb64.h" + +#include "gtest/gtest.h" +#include "scoped_ptrs.h" + +namespace nss_test { + +class B64EncodeDecodeTest : public ::testing::Test { + public: + void TestDecodeStr(const std::string &str) { + ScopedSECItem tmp( + NSSBase64_DecodeBuffer(nullptr, nullptr, str.c_str(), str.size())); + ASSERT_TRUE(tmp); + char *out = NSSBase64_EncodeItem(nullptr, nullptr, 0, tmp.get()); + ASSERT_TRUE(out); + ASSERT_EQ(std::string(out), str); + PORT_Free(out); + } + bool TestEncodeItem(SECItem *item) { + bool rv = true; + char *out = NSSBase64_EncodeItem(nullptr, nullptr, 0, item); + rv = !!out; + if (out) { + ScopedSECItem tmp( + NSSBase64_DecodeBuffer(nullptr, nullptr, out, strlen(out))); + EXPECT_TRUE(tmp); + EXPECT_EQ(SECEqual, SECITEM_CompareItem(item, tmp.get())); + PORT_Free(out); + } + return rv; + } + bool TestFakeDecode(size_t str_len) { + std::string str(str_len, 'A'); + ScopedSECItem tmp( + NSSBase64_DecodeBuffer(nullptr, nullptr, str.c_str(), str.size())); + return !!tmp; + } + bool TestFakeEncode(size_t len) { + std::vector<uint8_t> data(len, 0x30); + SECItem tmp = {siBuffer, data.data(), + static_cast<unsigned int>(data.size())}; + return TestEncodeItem(&tmp); + } + + protected: +}; + +TEST_F(B64EncodeDecodeTest, DecEncTest) { TestDecodeStr("VGhpcyBpcyBOU1Mh"); } + +TEST_F(B64EncodeDecodeTest, EncDecTest) { + uint8_t data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}; + SECItem tmp = {siBuffer, data, sizeof(data)}; + TestEncodeItem(&tmp); +} + +TEST_F(B64EncodeDecodeTest, FakeDecTest) { EXPECT_TRUE(TestFakeDecode(100)); } + +TEST_F(B64EncodeDecodeTest, FakeEncDecTest) { + EXPECT_TRUE(TestFakeEncode(100)); +} + +// These takes a while ... +TEST_F(B64EncodeDecodeTest, LongFakeDecTest1) { + EXPECT_TRUE(TestFakeDecode(0x66666666)); +} +TEST_F(B64EncodeDecodeTest, LongFakeEncDecTest1) { TestFakeEncode(0x3fffffff); } +TEST_F(B64EncodeDecodeTest, LongFakeEncDecTest2) { + EXPECT_FALSE(TestFakeEncode(0x40000000)); +} + +} // namespace nss_test |