Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1221620 - Tests for DER integer decoding, r=mt
--HG-- extra : amend_source : 7fd04fb7cb4ee88d2461bce6fc9781516966990f extra : histedit_source : fe06387817129f67fee05518688804ce7b2cf293
- Loading branch information
1 parent
147d93c
commit e65dee6
Showing
8 changed files
with
290 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#! 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). # | ||
####################################################################### | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* -*- 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 "nss.h" | ||
#include "pk11pub.h" | ||
#include "secutil.h" | ||
#include <memory> | ||
#include <climits> | ||
#include <netinet/in.h> | ||
|
||
#include "gtest/gtest.h" | ||
#include "scoped_ptrs.h" | ||
|
||
namespace nss_test { | ||
|
||
class DERIntegerDecodingTest : public ::testing::Test { | ||
public: | ||
void TestGetInteger(long number, unsigned char *der_number, unsigned int len) | ||
{ | ||
SECItem input = {siBuffer, der_number, len}; | ||
EXPECT_EQ(number, DER_GetInteger(&input)); | ||
} | ||
|
||
void GetDerLongMax(unsigned char *der_number, unsigned int len) | ||
{ | ||
der_number[0] = 0x7F; | ||
for (unsigned int i = 1; i < len; ++i) { | ||
der_number[i] = 0xFF; | ||
} | ||
} | ||
|
||
void GetDerLongMin(unsigned char *der_number, unsigned int len) | ||
{ | ||
der_number[0] = 0x80; | ||
for (unsigned int i = 1; i < len; ++i) { | ||
der_number[i] = 0x00; | ||
} | ||
} | ||
}; | ||
|
||
TEST_F(DERIntegerDecodingTest, DecodeLongMinus126) { | ||
unsigned char der[] = {0x82}; | ||
TestGetInteger(-126, der, sizeof(der)); | ||
} | ||
|
||
TEST_F(DERIntegerDecodingTest, DecodeLong130) { | ||
unsigned char der[] = {0x00, 0x82}; | ||
TestGetInteger(130, der, sizeof(der)); | ||
} | ||
|
||
TEST_F(DERIntegerDecodingTest, DecodeLong0) { | ||
unsigned char der[] = {0x00}; | ||
TestGetInteger(0, der, sizeof(der)); | ||
} | ||
|
||
TEST_F(DERIntegerDecodingTest, DecodeLong1) { | ||
unsigned char der[] = {0x01}; | ||
TestGetInteger(1, der, sizeof(der)); | ||
} | ||
|
||
TEST_F(DERIntegerDecodingTest, DecodeLongMinus1) { | ||
unsigned char der[] = {0xFF}; | ||
TestGetInteger(-1, der, sizeof(der)); | ||
} | ||
|
||
TEST_F(DERIntegerDecodingTest, DecodeLongMax) { | ||
unsigned char der[sizeof(long)]; | ||
GetDerLongMax(der, sizeof(long)); | ||
TestGetInteger(LONG_MAX, der, sizeof(der)); | ||
} | ||
|
||
TEST_F(DERIntegerDecodingTest, DecodeLongMin) { | ||
unsigned char der[sizeof(long)]; | ||
GetDerLongMin(der, sizeof(long)); | ||
TestGetInteger(LONG_MIN, der, sizeof(der)); | ||
} | ||
|
||
TEST_F(DERIntegerDecodingTest, DecodeLongMaxMinus1) { | ||
unsigned char der[sizeof(long)]; | ||
GetDerLongMax(der, sizeof(long)); | ||
der[sizeof(long)-1] = 0xFE; | ||
TestGetInteger(LONG_MAX-1, der, sizeof(der)); | ||
} | ||
|
||
TEST_F(DERIntegerDecodingTest, DecodeLongMinPlus1) { | ||
unsigned char der[sizeof(long)]; | ||
GetDerLongMin(der, sizeof(long)); | ||
der[sizeof(long)-1] = 0x01; | ||
TestGetInteger(LONG_MIN+1, der, sizeof(der)); | ||
} | ||
|
||
TEST_F(DERIntegerDecodingTest, DecodeLongMinMinus1) { | ||
unsigned char der[sizeof(long)+1]; | ||
GetDerLongMax(der, sizeof(long)+1); | ||
der[0] = 0xFF; | ||
der[1] = 0x7F; | ||
TestGetInteger(LONG_MIN, der, sizeof(der)); | ||
EXPECT_EQ(SEC_ERROR_BAD_DER, PORT_GetError()); | ||
} | ||
|
||
TEST_F(DERIntegerDecodingTest, DecodeLongMaxPlus1) { | ||
unsigned char der[sizeof(long)+1]; | ||
GetDerLongMin(der, sizeof(long)+1); | ||
der[0] = 0x00; | ||
der[1] = 0x80; | ||
TestGetInteger(LONG_MAX, der, sizeof(der)); | ||
EXPECT_EQ(SEC_ERROR_BAD_DER, PORT_GetError()); | ||
} | ||
|
||
} // namespace nss_test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "nspr.h" | ||
#include "nss.h" | ||
#include "ssl.h" | ||
|
||
#include <cstdlib> | ||
|
||
#define GTEST_HAS_RTTI 0 | ||
#include "gtest/gtest.h" | ||
|
||
int main(int argc, char **argv) { | ||
// Start the tests | ||
::testing::InitGoogleTest(&argc, argv); | ||
|
||
NSS_NoDB_Init(nullptr); | ||
NSS_SetDomesticPolicy(); | ||
int rv = RUN_ALL_TESTS(); | ||
|
||
NSS_Shutdown(); | ||
|
||
return rv; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# | ||
# 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 = \ | ||
der_getint_unittest.cc \ | ||
der_gtest.cc \ | ||
$(NULL) | ||
|
||
INCLUDES += -I$(CORE_DEPTH)/external_tests/google_test/gtest/include \ | ||
-I$(CORE_DEPTH)/external_tests/common | ||
|
||
REQUIRES = nspr nss libdbm gtest | ||
|
||
PROGRAM = der_gtest | ||
EXTRA_LIBS = $(DIST)/lib/$(LIB_PREFIX)gtest.$(LIB_SUFFIX) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ DEPTH = .. | |
|
||
DIRS = \ | ||
google_test \ | ||
der_gtest \ | ||
pk11_gtest \ | ||
ssl_gtest \ | ||
$(NULL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#! /bin/bash | ||
# | ||
# 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/. | ||
|
||
######################################################################## | ||
# | ||
# tests/der_gtests/der_gtests.sh | ||
# | ||
# Script to drive the DER unit gtests | ||
# | ||
# needs to work on all Unix and Windows platforms | ||
# | ||
# special strings | ||
# --------------- | ||
# FIXME ... known problems, search for this string | ||
# NOTE .... unexpected behavior | ||
# | ||
######################################################################## | ||
|
||
############################## der_gtest_init ########################## | ||
# local shell function to initialize this script | ||
######################################################################## | ||
der_gtest_init() | ||
{ | ||
SCRIPTNAME=der_gtest.sh # sourced - $0 would point to all.sh | ||
|
||
if [ -z "${CLEANUP}" ] ; then # if nobody else is responsible for | ||
CLEANUP="${SCRIPTNAME}" # cleaning this script will do it | ||
fi | ||
if [ -z "${INIT_SOURCED}" -o "${INIT_SOURCED}" != "TRUE" ]; then | ||
cd ../common | ||
. ./init.sh | ||
fi | ||
|
||
SCRIPTNAME=der_gtest.sh | ||
html_head DER Gtests | ||
|
||
if [ ! -d "${DERGTESTDIR}" ]; then | ||
mkdir -p "${DERGTESTDIR}" | ||
fi | ||
|
||
cd "${DERGTESTDIR}" | ||
} | ||
|
||
########################## der_gtest_start ######################### | ||
# Local function to actually start the test | ||
#################################################################### | ||
der_gtest_start() | ||
{ | ||
if [ ! -f ${BINDIR}/der_gtest ]; then | ||
html_unknown "Skipping der_gtest (not built)" | ||
return | ||
fi | ||
|
||
# Temporarily disable asserts for PKCS#11 slot leakage (Bug 1168425) | ||
unset NSS_STRICT_SHUTDOWN | ||
DERGTESTREPORT="${DERGTESTDIR}/report.xml" | ||
${BINDIR}/der_gtest -d "${DERGTESTDIR}" --gtest_output=xml:"${DERGTESTREPORT}" | ||
html_msg $? 0 "der_gtest run successfully" | ||
sed -f ${COMMON}/parsegtestreport.sed "${DERGTESTREPORT}" | \ | ||
while read result name; do | ||
if [ "$result" = "notrun" ]; then | ||
echo "$name" SKIPPED | ||
elif [ "$result" = "run" ]; then | ||
html_passed "$name" > /dev/null | ||
else | ||
html_failed "$name" | ||
fi | ||
done | ||
} | ||
|
||
der_gtest_cleanup() | ||
{ | ||
cd ${QADIR} | ||
. common/cleanup.sh | ||
} | ||
|
||
################## main ################################################# | ||
cd "$(dirname "$0")" | ||
der_gtest_init | ||
der_gtest_start | ||
der_gtest_cleanup |