test-helpers.c++ 4.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "test.h"
#ifndef _WIN32
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
27 28
#else
#include <process.h>
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
#endif

namespace kj {
namespace _ {  // private

bool hasSubstring(StringPtr haystack, StringPtr needle) {
  // TODO(perf): This is not the best algorithm for substring matching.
  if (needle.size() <= haystack.size()) {
    for (size_t i = 0; i <= haystack.size() - needle.size(); i++) {
      if (haystack.slice(i).startsWith(needle)) {
        return true;
      }
    }
  }
  return false;
}

LogExpectation::LogExpectation(LogSeverity severity, StringPtr substring)
    : severity(severity), substring(substring), seen(false) {}
LogExpectation::~LogExpectation() {
  if (!unwindDetector.isUnwinding()) {
    KJ_ASSERT(seen, "expected log message not seen", severity, substring);
  }
}

void LogExpectation::logMessage(
    LogSeverity severity, const char* file, int line, int contextDepth,
    String&& text) {
  if (!seen && severity == this->severity) {
    if (hasSubstring(text, substring)) {
      // Match. Ignore it.
      seen = true;
      return;
    }
  }

  // Pass up the chain.
  ExceptionCallback::logMessage(severity, file, line, contextDepth, kj::mv(text));
}

// =======================================================================================

namespace {

class FatalThrowExpectation: public ExceptionCallback {
public:
  FatalThrowExpectation(Maybe<Exception::Type> type,
                        Maybe<StringPtr> message)
      : type(type), message(message) {}

  virtual void onFatalException(Exception&& exception) {
    KJ_IF_MAYBE(expectedType, type) {
      if (exception.getType() != *expectedType) {
        KJ_LOG(ERROR, "threw exception of wrong type", exception, *expectedType);
        _exit(1);
      }
    }
    KJ_IF_MAYBE(expectedSubstring, message) {
      if (!hasSubstring(exception.getDescription(), *expectedSubstring)) {
        KJ_LOG(ERROR, "threw exception with wrong message", exception, *expectedSubstring);
        _exit(1);
      }
    }
    _exit(0);
  }

private:
  Maybe<Exception::Type> type;
  Maybe<StringPtr> message;
};

}  // namespace

bool expectFatalThrow(kj::Maybe<Exception::Type> type, kj::Maybe<StringPtr> message,
                      Function<void()> code) {
#if _WIN32
  // We don't support death tests on Windows due to lack of efficient fork.
  return true;
#else
  pid_t child;
  KJ_SYSCALL(child = fork());
  if (child == 0) {
    KJ_DEFER(_exit(1));
    FatalThrowExpectation expectation(type, message);
    KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() {
      code();
    })) {
      KJ_LOG(ERROR, "a non-fatal exception was thrown, but we expected fatal", *e);
    } else {
      KJ_LOG(ERROR, "no fatal exception was thrown");
    }
  }

  int status;
  KJ_SYSCALL(waitpid(child, &status, 0));

  if (WIFEXITED(status)) {
    return WEXITSTATUS(status) == 0;
  } else if (WIFSIGNALED(status)) {
    KJ_FAIL_EXPECT("subprocess crashed without throwing exception", WTERMSIG(status));
    return false;
  } else {
    KJ_FAIL_EXPECT("subprocess neiter excited nor crashed?", status);
    return false;
  }
#endif
}

}  // namespace _ (private)
}  // namespace kj