Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 1057584 - Add gtest framework and initial tests optionally (3/4).…
… Testing Code. r=wtc
  • Loading branch information
Camilo Viecco committed Sep 8, 2014
1 parent a804709 commit bf49804
Show file tree
Hide file tree
Showing 9 changed files with 1,481 additions and 0 deletions.
31 changes: 31 additions & 0 deletions external_tests/README
@@ -0,0 +1,31 @@
GTest-based Unit Tests

This directory contains GTest-based unit tests for NSS.

Currently, these are only loopback-type tests of libsssl,
but could be expanded to other types of tests. To make these
work do:

- Set NSS_BUILD_GTESTS=1 before starting your build

- cd tests/

- Set NSS_TESTS=ssl_gtests and NSS_CYCLES=standard

- run ./all.sh

This will run the certutil tests (generating a test db) and
will finalize with a call to the ssl_gtest

You should be able to run the unit tests manually as:

ssl_gtest -d ${SSLGTESTDIR}

Where $SSLGTESTDIR the directory created by ./all.sh or a manually
created directory with a database containing a certificate called
server (with its private keys)


There is a very trivial set of tests that demonstrate some
of the features.

39 changes: 39 additions & 0 deletions external_tests/ssl_gtest/databuffer.h
@@ -0,0 +1,39 @@
/* -*- 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/. */

#ifndef databuffer_h__
#define databuffer_h__

class DataBuffer {
public:
DataBuffer() : data_(nullptr), len_(0) {}
DataBuffer(const uint8_t *data, size_t len) : data_(nullptr), len_(0) {
Assign(data, len);
}
~DataBuffer() { delete[] data_; }

void Assign(const uint8_t *data, size_t len) {
Allocate(len);
memcpy(static_cast<void *>(data_), static_cast<const void *>(data), len);
}

void Allocate(size_t len) {
delete[] data_;
data_ = new unsigned char[len ? len : 1]; // Don't depend on new [0].
len_ = len;
}

const uint8_t *data() const { return data_; }
uint8_t *data() { return data_; }
size_t len() const { return len_; }
const bool empty() const { return len_ != 0; }

private:
uint8_t *data_;
size_t len_;
};

#endif
51 changes: 51 additions & 0 deletions external_tests/ssl_gtest/gtest_utils.h
@@ -0,0 +1,51 @@
/* -*- 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/. */

#ifndef gtest_utils_h__
#define gtest_utils_h__

namespace nss_test {

// Gtest utilities
class Timeout : public PollTarget {
public:
Timeout(int32_t timer_ms) : handle_(nullptr), timed_out_(false) {
Poller::Instance()->SetTimer(timer_ms, this, &Timeout::ExpiredCallback,
&handle_);
}

static void ExpiredCallback(PollTarget* target, Event event) {
Timeout* timeout = static_cast<Timeout*>(target);
timeout->timed_out_ = true;
}

void Cancel() { handle_->Cancel(); }

bool timed_out() const { return timed_out_; }

private:
Poller::Timer* handle_;
bool timed_out_;
};

} // namespace nss_test

#define WAIT_(expression, timeout) \
do { \
Timeout tm(timeout); \
while (!(expression)) { \
Poller::Instance()->Poll(); \
if (tm.timed_out()) break; \
} \
} while (0)

#define ASSERT_TRUE_WAIT(expression, timeout) \
do { \
WAIT_(expression, timeout); \
ASSERT_TRUE(expression); \
} while (0)

#endif
32 changes: 32 additions & 0 deletions external_tests/ssl_gtest/ssl_gtest.cc
@@ -0,0 +1,32 @@
#include "nspr.h"
#include "nss.h"
#include "ssl.h"

#include "test_io.h"

#define GTEST_HAS_RTTI 0
#include "gtest/gtest.h"

int main(int argc, char **argv) {
// Start the tests
::testing::InitGoogleTest(&argc, argv);
std::string path = ".";

for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-d")) {
path = argv[i + 1];
++i;
}
}

NSS_Initialize(path.c_str(), "", "", SECMOD_DB, NSS_INIT_READONLY);
NSS_SetDomesticPolicy();

int rv = RUN_ALL_TESTS();

NSS_Shutdown();

nss_test::Poller::Shutdown();

return rv;
}

0 comments on commit bf49804

Please sign in to comment.