2014-12-18 18:50:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
2015-03-03 00:48:01 +00:00
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
2014-12-18 18:50:47 +00:00
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
2014-09-30 20:17:54 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-10-07 18:20:47 +00:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
2014-09-30 20:17:54 +00:00
|
|
|
#include <boost/thread.hpp>
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2014-12-03 23:14:02 +00:00
|
|
|
#include <osquery/events.h>
|
|
|
|
#include <osquery/filesystem.h>
|
|
|
|
#include <osquery/tables.h>
|
|
|
|
|
2014-09-30 20:17:54 +00:00
|
|
|
#include "osquery/events/darwin/fsevents.h"
|
|
|
|
|
|
|
|
namespace osquery {
|
|
|
|
|
|
|
|
const std::string kRealTestPath = "/private/tmp/osquery-fsevents-trigger";
|
|
|
|
int kMaxEventLatency = 3000;
|
|
|
|
|
|
|
|
class FSEventsTests : public testing::Test {
|
|
|
|
protected:
|
2015-03-03 00:48:01 +00:00
|
|
|
void TearDown() { boost::filesystem::remove_all(kRealTestPath); }
|
2014-09-30 20:17:54 +00:00
|
|
|
|
|
|
|
void StartEventLoop() {
|
2014-10-03 15:14:36 +00:00
|
|
|
event_pub_ = std::make_shared<FSEventsEventPublisher>();
|
|
|
|
EventFactory::registerEventPublisher(event_pub_);
|
2014-09-30 20:17:54 +00:00
|
|
|
FILE* fd = fopen(kRealTestPath.c_str(), "w");
|
|
|
|
fclose(fd);
|
|
|
|
|
2015-01-30 18:44:25 +00:00
|
|
|
temp_thread_ = boost::thread(EventFactory::run, "fsevents");
|
2014-09-30 20:17:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-01 08:35:44 +00:00
|
|
|
void EndEventLoop() {
|
|
|
|
while (!event_pub_->hasStarted()) {
|
|
|
|
::usleep(20);
|
|
|
|
}
|
|
|
|
EventFactory::end();
|
|
|
|
}
|
|
|
|
|
2014-09-30 20:17:54 +00:00
|
|
|
void WaitForStream(int max) {
|
|
|
|
int delay = 0;
|
|
|
|
while (delay < max * 1000) {
|
2014-10-03 15:14:36 +00:00
|
|
|
if (event_pub_->isStreamRunning()) {
|
2014-09-30 20:17:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
::usleep(50);
|
|
|
|
delay += 50;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WaitForEvents(int max, int num_events = 0) {
|
|
|
|
int delay = 0;
|
|
|
|
while (delay <= max * 1000) {
|
2014-10-03 15:14:36 +00:00
|
|
|
if (num_events > 0 && event_pub_->numEvents() >= num_events) {
|
2014-09-30 20:17:54 +00:00
|
|
|
return true;
|
2014-10-03 15:14:36 +00:00
|
|
|
} else if (num_events == 0 && event_pub_->numEvents() > 0) {
|
2014-09-30 20:17:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
delay += 50;
|
|
|
|
::usleep(50);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateEvents(int num = 1) {
|
|
|
|
WaitForStream(kMaxEventLatency);
|
|
|
|
for (int i = 0; i < num; ++i) {
|
|
|
|
FILE* fd = fopen(kRealTestPath.c_str(), "w");
|
|
|
|
fputs("fsevents", fd);
|
|
|
|
fclose(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-03 15:14:36 +00:00
|
|
|
std::shared_ptr<FSEventsEventPublisher> event_pub_;
|
2014-09-30 20:17:54 +00:00
|
|
|
boost::thread temp_thread_;
|
|
|
|
};
|
|
|
|
|
2014-10-03 15:14:36 +00:00
|
|
|
TEST_F(FSEventsTests, test_register_event_pub) {
|
2015-01-30 18:44:25 +00:00
|
|
|
auto pub = std::make_shared<FSEventsEventPublisher>();
|
|
|
|
auto status = EventFactory::registerEventPublisher(pub);
|
2014-09-30 20:17:54 +00:00
|
|
|
EXPECT_TRUE(status.ok());
|
|
|
|
|
|
|
|
// Make sure only one event type exists
|
2014-10-03 15:14:36 +00:00
|
|
|
EXPECT_EQ(EventFactory::numEventPublishers(), 1);
|
2015-02-01 08:35:44 +00:00
|
|
|
status = EventFactory::deregisterEventPublisher("fsevents");
|
|
|
|
EXPECT_TRUE(status.ok());
|
2014-09-30 20:17:54 +00:00
|
|
|
}
|
|
|
|
|
2014-10-03 15:26:41 +00:00
|
|
|
TEST_F(FSEventsTests, test_fsevents_add_subscription_missing_path) {
|
2015-01-30 18:44:25 +00:00
|
|
|
auto pub = std::make_shared<FSEventsEventPublisher>();
|
|
|
|
EventFactory::registerEventPublisher(pub);
|
2014-09-30 20:17:54 +00:00
|
|
|
|
2014-10-03 15:26:41 +00:00
|
|
|
// This subscription path is fake, and will succeed!
|
|
|
|
auto mc = std::make_shared<FSEventsSubscriptionContext>();
|
2014-09-30 20:17:54 +00:00
|
|
|
mc->path = "/this/path/is/fake";
|
|
|
|
|
2014-10-03 15:26:41 +00:00
|
|
|
auto subscription = Subscription::create(mc);
|
2015-01-30 18:44:25 +00:00
|
|
|
auto status = EventFactory::addSubscription("fsevents", subscription);
|
2014-09-30 20:17:54 +00:00
|
|
|
EXPECT_TRUE(status.ok());
|
2015-02-01 08:35:44 +00:00
|
|
|
EventFactory::deregisterEventPublisher("fsevents");
|
2014-09-30 20:17:54 +00:00
|
|
|
}
|
|
|
|
|
2014-10-03 15:26:41 +00:00
|
|
|
TEST_F(FSEventsTests, test_fsevents_add_subscription_success) {
|
2014-10-03 15:14:36 +00:00
|
|
|
auto event_pub = std::make_shared<FSEventsEventPublisher>();
|
|
|
|
EventFactory::registerEventPublisher(event_pub);
|
2014-09-30 20:17:54 +00:00
|
|
|
|
2014-10-03 15:26:41 +00:00
|
|
|
// This subscription path *should* be real.
|
|
|
|
auto mc = std::make_shared<FSEventsSubscriptionContext>();
|
2014-09-30 20:17:54 +00:00
|
|
|
mc->path = "/";
|
|
|
|
|
2014-10-03 15:26:41 +00:00
|
|
|
auto subscription = Subscription::create(mc);
|
2015-01-30 18:44:25 +00:00
|
|
|
auto status = EventFactory::addSubscription("fsevents", subscription);
|
2014-09-30 20:17:54 +00:00
|
|
|
EXPECT_TRUE(status.ok());
|
|
|
|
|
|
|
|
// Make sure configure was called.
|
2014-10-03 15:26:41 +00:00
|
|
|
size_t num_paths = event_pub->numSubscriptionedPaths();
|
2014-09-30 20:17:54 +00:00
|
|
|
EXPECT_EQ(num_paths, 1);
|
|
|
|
|
2014-10-03 15:26:41 +00:00
|
|
|
// A duplicate subscription will work.
|
|
|
|
auto mc_dup = std::make_shared<FSEventsSubscriptionContext>();
|
2014-09-30 20:17:54 +00:00
|
|
|
mc_dup->path = "/";
|
2014-10-03 15:26:41 +00:00
|
|
|
auto subscription_dup = Subscription::create(mc_dup);
|
2015-01-30 18:44:25 +00:00
|
|
|
status = EventFactory::addSubscription("fsevents", subscription_dup);
|
2014-09-30 20:17:54 +00:00
|
|
|
EXPECT_TRUE(status.ok());
|
|
|
|
|
|
|
|
// But the paths with be deduped when the event type reconfigures.
|
2014-10-03 15:26:41 +00:00
|
|
|
num_paths = event_pub->numSubscriptionedPaths();
|
2014-09-30 20:17:54 +00:00
|
|
|
EXPECT_EQ(num_paths, 1);
|
2015-02-01 08:35:44 +00:00
|
|
|
EventFactory::deregisterEventPublisher("fsevents");
|
2014-09-30 20:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(FSEventsTests, test_fsevents_run) {
|
|
|
|
// Assume event type is registered.
|
2014-10-03 15:14:36 +00:00
|
|
|
event_pub_ = std::make_shared<FSEventsEventPublisher>();
|
|
|
|
EventFactory::registerEventPublisher(event_pub_);
|
2014-09-30 20:17:54 +00:00
|
|
|
|
2014-10-03 15:26:41 +00:00
|
|
|
// Create a subscriptioning context
|
|
|
|
auto mc = std::make_shared<FSEventsSubscriptionContext>();
|
2014-09-30 20:17:54 +00:00
|
|
|
mc->path = kRealTestPath;
|
2015-01-30 18:44:25 +00:00
|
|
|
EventFactory::addSubscription("fsevents", Subscription::create(mc));
|
2014-09-30 20:17:54 +00:00
|
|
|
|
|
|
|
// Create an event loop thread (similar to main)
|
2015-01-30 18:44:25 +00:00
|
|
|
boost::thread temp_thread(EventFactory::run, "fsevents");
|
2014-10-03 15:14:36 +00:00
|
|
|
EXPECT_TRUE(event_pub_->numEvents() == 0);
|
2014-09-30 20:17:54 +00:00
|
|
|
|
|
|
|
// Cause an fsevents event(s) by writing to the watched path.
|
|
|
|
CreateEvents();
|
|
|
|
|
|
|
|
// Wait for the thread's run loop to select.
|
|
|
|
WaitForEvents(kMaxEventLatency);
|
|
|
|
|
2014-10-03 15:14:36 +00:00
|
|
|
EXPECT_TRUE(event_pub_->numEvents() > 0);
|
2014-09-30 20:17:54 +00:00
|
|
|
EventFactory::end();
|
|
|
|
}
|
|
|
|
|
2014-12-15 06:17:38 +00:00
|
|
|
class TestFSEventsEventSubscriber
|
|
|
|
: public EventSubscriber<FSEventsEventPublisher> {
|
|
|
|
DECLARE_SUBSCRIBER("TestFSEventsEventSubscriber");
|
2014-09-30 20:17:54 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
void init() { callback_count_ = 0; }
|
2015-02-18 02:04:15 +00:00
|
|
|
Status SimpleCallback(const FSEventsEventContextRef& ec,
|
|
|
|
const void* user_data) {
|
2014-09-30 20:17:54 +00:00
|
|
|
callback_count_ += 1;
|
|
|
|
return Status(0, "OK");
|
|
|
|
}
|
|
|
|
|
2014-12-15 06:17:38 +00:00
|
|
|
SCRef GetSubscription(uint32_t mask = 0) {
|
|
|
|
auto sc = createSubscriptionContext();
|
|
|
|
sc->path = kRealTestPath;
|
|
|
|
sc->mask = mask;
|
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
2015-02-18 02:04:15 +00:00
|
|
|
Status Callback(const FSEventsEventContextRef& ec, const void* user_data) {
|
2014-12-15 18:17:56 +00:00
|
|
|
// The following comments are an example Callback routine.
|
|
|
|
// Row r;
|
|
|
|
// r["action"] = ec->action;
|
|
|
|
// r["path"] = ec->path;
|
2014-09-30 20:17:54 +00:00
|
|
|
|
|
|
|
// Normally would call Add here.
|
|
|
|
actions_.push_back(ec->action);
|
|
|
|
callback_count_ += 1;
|
|
|
|
return Status(0, "OK");
|
|
|
|
}
|
|
|
|
|
2014-12-15 06:17:38 +00:00
|
|
|
void WaitForEvents(int max) {
|
2014-09-30 20:17:54 +00:00
|
|
|
int delay = 0;
|
|
|
|
while (delay < max * 1000) {
|
2014-12-15 06:17:38 +00:00
|
|
|
if (callback_count_ > 0) {
|
2014-09-30 20:17:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
::usleep(50);
|
|
|
|
delay += 50;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
int callback_count_;
|
|
|
|
std::vector<std::string> actions_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(FSEventsTests, test_fsevents_fire_event) {
|
|
|
|
// Assume event type is registered.
|
|
|
|
StartEventLoop();
|
2014-12-15 06:17:38 +00:00
|
|
|
|
|
|
|
// Simulate registering an event subscriber.
|
|
|
|
auto sub = std::make_shared<TestFSEventsEventSubscriber>();
|
|
|
|
sub->init();
|
2014-09-30 20:17:54 +00:00
|
|
|
|
2014-10-03 15:26:41 +00:00
|
|
|
// Create a subscriptioning context, note the added Event to the symbol
|
2014-12-15 06:17:38 +00:00
|
|
|
auto sc = sub->GetSubscription(0);
|
2015-02-18 02:04:15 +00:00
|
|
|
sub->subscribe(&TestFSEventsEventSubscriber::SimpleCallback, sc, nullptr);
|
2014-09-30 20:17:54 +00:00
|
|
|
CreateEvents();
|
|
|
|
|
|
|
|
// This time wait for the callback.
|
2014-12-15 06:17:38 +00:00
|
|
|
sub->WaitForEvents(kMaxEventLatency);
|
2014-09-30 20:17:54 +00:00
|
|
|
|
2014-10-03 15:26:41 +00:00
|
|
|
// Make sure our expected event fired (aka subscription callback was called).
|
2014-12-15 06:17:38 +00:00
|
|
|
EXPECT_TRUE(sub->callback_count_ > 0);
|
2014-09-30 20:17:54 +00:00
|
|
|
EndEventLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(FSEventsTests, test_fsevents_event_action) {
|
|
|
|
// Assume event type is registered.
|
|
|
|
StartEventLoop();
|
|
|
|
|
2014-12-15 06:17:38 +00:00
|
|
|
// Simulate registering an event subscriber.
|
|
|
|
auto sub = std::make_shared<TestFSEventsEventSubscriber>();
|
|
|
|
sub->init();
|
|
|
|
|
|
|
|
auto sc = sub->GetSubscription(0);
|
2015-02-18 02:04:15 +00:00
|
|
|
sub->subscribe(&TestFSEventsEventSubscriber::Callback, sc, nullptr);
|
2014-09-30 20:17:54 +00:00
|
|
|
CreateEvents();
|
2014-12-15 06:17:38 +00:00
|
|
|
sub->WaitForEvents(kMaxEventLatency);
|
2014-09-30 20:17:54 +00:00
|
|
|
|
|
|
|
// Make sure the fsevents action was expected.
|
2014-12-15 06:17:38 +00:00
|
|
|
EXPECT_TRUE(sub->actions_.size() > 0);
|
|
|
|
if (sub->actions_.size() > 1) {
|
|
|
|
EXPECT_EQ(sub->actions_[0], "UPDATED");
|
2014-09-30 20:17:54 +00:00
|
|
|
}
|
|
|
|
EndEventLoop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|