diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
commit | 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch) | |
tree | a31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /security/nss/gtests/base_gtest | |
parent | Initial commit. (diff) | |
download | firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip |
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'security/nss/gtests/base_gtest')
-rw-r--r-- | security/nss/gtests/base_gtest/Makefile | 43 | ||||
-rw-r--r-- | security/nss/gtests/base_gtest/base_gtest.gyp | 31 | ||||
-rw-r--r-- | security/nss/gtests/base_gtest/manifest.mn | 23 | ||||
-rw-r--r-- | security/nss/gtests/base_gtest/utf8_unittest.cc | 150 |
4 files changed, 247 insertions, 0 deletions
diff --git a/security/nss/gtests/base_gtest/Makefile b/security/nss/gtests/base_gtest/Makefile new file mode 100644 index 0000000000..0d547e0803 --- /dev/null +++ b/security/nss/gtests/base_gtest/Makefile @@ -0,0 +1,43 @@ +#! gmake +# +# 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/. + +####################################################################### +# (1) Include initial platform-independent assignments (MANDATORY). # +####################################################################### + +include manifest.mn + +####################################################################### +# (2) Include "global" configuration information. (OPTIONAL) # +####################################################################### + +include $(CORE_DEPTH)/coreconf/config.mk + +####################################################################### +# (3) Include "component" configuration information. (OPTIONAL) # +####################################################################### + + +####################################################################### +# (4) Include "local" platform-dependent assignments (OPTIONAL). # +####################################################################### + +include ../common/gtest.mk + +####################################################################### +# (5) Execute "global" rules. (OPTIONAL) # +####################################################################### + +include $(CORE_DEPTH)/coreconf/rules.mk + +####################################################################### +# (6) Execute "component" rules. (OPTIONAL) # +####################################################################### + + +####################################################################### +# (7) Execute "local" rules. (OPTIONAL). # +####################################################################### diff --git a/security/nss/gtests/base_gtest/base_gtest.gyp b/security/nss/gtests/base_gtest/base_gtest.gyp new file mode 100644 index 0000000000..408908f976 --- /dev/null +++ b/security/nss/gtests/base_gtest/base_gtest.gyp @@ -0,0 +1,31 @@ +# 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/. +{ + 'includes': [ + '../../coreconf/config.gypi', + '../common/gtest.gypi', + ], + 'targets': [ + { + 'target_name': 'base_gtest', + 'type': 'executable', + 'sources': [ + 'utf8_unittest.cc', + '<(DEPTH)/gtests/common/gtests.cc' + ], + 'dependencies': [ + '<(DEPTH)/exports.gyp:nss_exports', + '<(DEPTH)/gtests/google_test/google_test.gyp:gtest', + '<(DEPTH)/lib/util/util.gyp:nssutil3', + '<(DEPTH)/lib/ssl/ssl.gyp:ssl3', + '<(DEPTH)/lib/nss/nss.gyp:nss3', + '<(DEPTH)/lib/smime/smime.gyp:smime3', + '<(DEPTH)/lib/base/base.gyp:nssb', + ] + } + ], + 'variables': { + 'module': 'nss' + } +} diff --git a/security/nss/gtests/base_gtest/manifest.mn b/security/nss/gtests/base_gtest/manifest.mn new file mode 100644 index 0000000000..07506c96c0 --- /dev/null +++ b/security/nss/gtests/base_gtest/manifest.mn @@ -0,0 +1,23 @@ +# +# 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/. +CORE_DEPTH = ../.. +DEPTH = ../.. +MODULE = nss + +CPPSRCS = \ + utf8_unittest.cc \ + $(NULL) + +INCLUDES += -I$(CORE_DEPTH)/gtests/google_test/gtest/include \ + -I$(CORE_DEPTH)/gtests/common \ + -I$(CORE_DEPTH)/cpputil + +REQUIRES = nspr nss libdbm gtest + +PROGRAM = base_gtest + +EXTRA_LIBS = $(DIST)/lib/$(LIB_PREFIX)gtest.$(LIB_SUFFIX) $(EXTRA_OBJS) \ + $(DIST)/lib/$(LIB_PREFIX)nssb.$(LIB_SUFFIX) \ + $(DIST)/lib/$(LIB_PREFIX)gtestutil.$(LIB_SUFFIX) diff --git a/security/nss/gtests/base_gtest/utf8_unittest.cc b/security/nss/gtests/base_gtest/utf8_unittest.cc new file mode 100644 index 0000000000..a906529ea9 --- /dev/null +++ b/security/nss/gtests/base_gtest/utf8_unittest.cc @@ -0,0 +1,150 @@ +/* -*- 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 "gtest/gtest.h" + +#include "nss.h" +#include "base.h" +#include "secerr.h" + +namespace nss_test { + +class Utf8Test : public ::testing::Test {}; + +// Tests nssUTF8_Length rejects overlong forms, surrogates, etc. +TEST_F(Utf8Test, Utf8Length) { + PRStatus status; + + EXPECT_EQ(0u, nssUTF8_Length("", &status)); + EXPECT_EQ(PR_SUCCESS, status); + + // U+0000..U+007F + EXPECT_EQ(1u, nssUTF8_Length("\x01", &status)); + EXPECT_EQ(PR_SUCCESS, status); + EXPECT_EQ(1u, nssUTF8_Length("\x7F", &status)); + EXPECT_EQ(PR_SUCCESS, status); + + // lone trailing byte + EXPECT_EQ(0u, nssUTF8_Length("\x80", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + EXPECT_EQ(0u, nssUTF8_Length("\xBF", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + + // overlong U+0000..U+007F + EXPECT_EQ(0u, nssUTF8_Length("\xC0\x80", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + EXPECT_EQ(0u, nssUTF8_Length("\xC1\xBF", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + + // U+0080..U+07FF + EXPECT_EQ(2u, nssUTF8_Length("\xC2\x80", &status)); + EXPECT_EQ(PR_SUCCESS, status); + EXPECT_EQ(2u, nssUTF8_Length("\xDF\xBF", &status)); + EXPECT_EQ(PR_SUCCESS, status); + + // overlong U+0000..U+07FF + EXPECT_EQ(0u, nssUTF8_Length("\xE0\x80\x80", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + EXPECT_EQ(0u, nssUTF8_Length("\xE0\x9F\xBF", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + + // U+0800..U+D7FF + EXPECT_EQ(3u, nssUTF8_Length("\xE0\xA0\x80", &status)); + EXPECT_EQ(PR_SUCCESS, status); + EXPECT_EQ(3u, nssUTF8_Length("\xE0\xBF\xBF", &status)); + EXPECT_EQ(PR_SUCCESS, status); + EXPECT_EQ(3u, nssUTF8_Length("\xE1\x80\x80", &status)); + EXPECT_EQ(PR_SUCCESS, status); + EXPECT_EQ(3u, nssUTF8_Length("\xEC\xBF\xBF", &status)); + EXPECT_EQ(PR_SUCCESS, status); + EXPECT_EQ(3u, nssUTF8_Length("\xED\x80\x80", &status)); + EXPECT_EQ(PR_SUCCESS, status); + EXPECT_EQ(3u, nssUTF8_Length("\xED\x9F\xBF", &status)); + EXPECT_EQ(PR_SUCCESS, status); + + // lone surrogate + EXPECT_EQ(0u, nssUTF8_Length("\xED\xA0\x80", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + EXPECT_EQ(0u, nssUTF8_Length("\xED\xBF\xBF", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + + // U+E000..U+FFFF + EXPECT_EQ(3u, nssUTF8_Length("\xEE\x80\x80", &status)); + EXPECT_EQ(PR_SUCCESS, status); + EXPECT_EQ(3u, nssUTF8_Length("\xEF\xBF\xBF", &status)); + EXPECT_EQ(PR_SUCCESS, status); + + // overlong U+0000..U+FFFF + EXPECT_EQ(0u, nssUTF8_Length("\xF0\x80\x80\x80", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + EXPECT_EQ(0u, nssUTF8_Length("\xF0\x8F\xBF\xBF", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + + // U+10000..U+10FFFF + EXPECT_EQ(4u, nssUTF8_Length("\xF0\x90\x80\x80", &status)); + EXPECT_EQ(PR_SUCCESS, status); + EXPECT_EQ(4u, nssUTF8_Length("\xF0\xBF\xBF\xBF", &status)); + EXPECT_EQ(PR_SUCCESS, status); + EXPECT_EQ(4u, nssUTF8_Length("\xF1\x80\x80\x80", &status)); + EXPECT_EQ(PR_SUCCESS, status); + EXPECT_EQ(4u, nssUTF8_Length("\xF3\xBF\xBF\xBF", &status)); + EXPECT_EQ(PR_SUCCESS, status); + EXPECT_EQ(4u, nssUTF8_Length("\xF4\x80\x80\x80", &status)); + EXPECT_EQ(PR_SUCCESS, status); + EXPECT_EQ(4u, nssUTF8_Length("\xF4\x8F\xBF\xBF", &status)); + EXPECT_EQ(PR_SUCCESS, status); + + // out of Unicode range + EXPECT_EQ(0u, nssUTF8_Length("\xF4\x90\x80\x80", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + EXPECT_EQ(0u, nssUTF8_Length("\xF4\xBF\xBF\xBF", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + EXPECT_EQ(0u, nssUTF8_Length("\xF5\x80\x80\x80", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + EXPECT_EQ(0u, nssUTF8_Length("\xF7\xBF\xBF\xBF", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + + // former 5-byte sequence + EXPECT_EQ(0u, nssUTF8_Length("\xF8\x80\x80\x80\x80", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + EXPECT_EQ(0u, nssUTF8_Length("\xFB\xBF\xBF\xBF\xBF", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + + // former 6-byte sequence + EXPECT_EQ(0u, nssUTF8_Length("\xFC\x80\x80\x80\x80\x80", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + EXPECT_EQ(0u, nssUTF8_Length("\xFD\xBF\xBF\xBF\xBF\xBF", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + + // invalid lead byte + EXPECT_EQ(0u, nssUTF8_Length("\xFE", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + EXPECT_EQ(0u, nssUTF8_Length("\xFF", &status)); + EXPECT_EQ(PR_FAILURE, status); + EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError()); + + nss_DestroyErrorStack(); +} +} // namespace nss_test |