blob: de4a4cacb813f341ba1042b09c4b16afa57f1f9e [file] [log] [blame]
// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "shill/dhcp/dhcp_provider.h"
#include <base/files/file_util.h>
#include <base/files/scoped_temp_dir.h>
#include <base/strings/stringprintf.h>
#include "shill/dhcp/dhcp_config.h"
#include "shill/mock_control.h"
#include "shill/mock_event_dispatcher.h"
#include "shill/mock_glib.h"
using base::FilePath;
using base::ScopedTempDir;
using testing::_;
using testing::SaveArg;
using testing::StrictMock;
using testing::Test;
namespace shill {
namespace {
const char kDeviceName[] = "testdevicename";
const char kHostName[] = "testhostname";
const char kStorageIdentifier[] = "teststorageidentifier";
const bool kArpGateway = false;
} // namespace
class DHCPProviderTest : public Test {
public:
DHCPProviderTest() : provider_(DHCPProvider::GetInstance()) {
provider_->glib_ = &glib_;
provider_->control_interface_ = &control_;
provider_->dispatcher_ = &dispatcher_;
}
void SetUp() {
// DHCPProvider is a singleton, there is no guarentee that it is
// not setup/used elsewhere, so reset its state before running our
// tests.
provider_->configs_.clear();
provider_->recently_unbound_pids_.clear();
}
protected:
void RetireUnboundPID(int pid) { provider_->RetireUnboundPID(pid); }
MockControl control_;
MockGLib glib_;
DHCPProvider* provider_;
StrictMock<MockEventDispatcher> dispatcher_;
};
TEST_F(DHCPProviderTest, CreateIPv4Config) {
DHCPConfigRefPtr config = provider_->CreateIPv4Config(kDeviceName,
kHostName,
kStorageIdentifier,
kArpGateway);
EXPECT_TRUE(config.get());
EXPECT_EQ(&glib_, config->glib_);
EXPECT_EQ(kDeviceName, config->device_name());
EXPECT_TRUE(provider_->configs_.empty());
}
TEST_F(DHCPProviderTest, DestroyLease) {
ScopedTempDir temp_dir;
FilePath lease_file;
EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
provider_->root_ = temp_dir.path();
lease_file = provider_->root_.Append(base::StringPrintf(
DHCPProvider::kDHCPCDPathFormatLease,
kDeviceName));
EXPECT_TRUE(base::CreateDirectory(lease_file.DirName()));
EXPECT_EQ(0, base::WriteFile(lease_file, "", 0));
EXPECT_TRUE(base::PathExists(lease_file));
provider_->DestroyLease(kDeviceName);
EXPECT_FALSE(base::PathExists(lease_file));
}
TEST_F(DHCPProviderTest, BindAndUnbind) {
int kPid = 999;
EXPECT_EQ(nullptr, provider_->GetConfig(kPid));
EXPECT_FALSE(provider_->IsRecentlyUnbound(kPid));
DHCPConfigRefPtr config = provider_->CreateIPv4Config(kDeviceName,
kHostName,
kStorageIdentifier,
kArpGateway);
provider_->BindPID(kPid, config);
EXPECT_NE(nullptr, provider_->GetConfig(kPid));
EXPECT_FALSE(provider_->IsRecentlyUnbound(kPid));
base::Closure task;
EXPECT_CALL(dispatcher_, PostDelayedTask(_, _)); // TODO(pstew): crbug/502320
provider_->UnbindPID(kPid);
EXPECT_EQ(nullptr, provider_->GetConfig(kPid));
EXPECT_TRUE(provider_->IsRecentlyUnbound(kPid));
RetireUnboundPID(kPid); // Execute as if the PostDelayedTask() timer expired.
EXPECT_EQ(nullptr, provider_->GetConfig(kPid));
EXPECT_FALSE(provider_->IsRecentlyUnbound(kPid));
}
} // namespace shill