Skip to content

Commit

Permalink
To pass string_util_unittest, we need the ICU data file. To get the I…
Browse files Browse the repository at this point in the history
…CU data file, we need some of PathService, which brings along with it a bunch of other needed functions. So here are a bunch of stubs, along with another file's worth of passing tests. (Based on a patch from Dean.)

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@921 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
evanm@google.com committed Aug 15, 2008
1 parent 1b3deed commit 4c0040c
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 20 deletions.
11 changes: 8 additions & 3 deletions base/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ if env['PLATFORM'] == 'win32':
# cross-platform live below.
input_files = [
'at_exit.cc',
'base_paths.cc',
'base_switches.cc',
'bzip2_error_handler.cc',
'command_line.cc',
Expand Down Expand Up @@ -93,7 +94,6 @@ if env['PLATFORM'] == 'win32':
# TODO: move all these files to either the cross-platform block above or
# a platform-specific block below.
input_files.extend([
'base_paths.cc',
'clipboard_util.cc',
'debug_on_start.cc',
'event_recorder.cc',
Expand Down Expand Up @@ -160,6 +160,7 @@ if env['PLATFORM'] == 'darwin':
if env['PLATFORM'] == 'posix':
input_files.extend([
'atomicops_internals_x86_gcc.cc',
'base_paths_linux.cc',
'file_util_linux.cc',
'sys_string_conversions_linux.cc',
])
Expand Down Expand Up @@ -246,6 +247,7 @@ test_files = [
'string_escape_unittest.cc',
'string_piece_unittest.cc',
'string_tokenizer_unittest.cc',
'string_util_unittest.cc',
'time_unittest.cc',
'values_unittest.cc',
'waitable_event_unittest.cc',
Expand Down Expand Up @@ -274,7 +276,6 @@ if env['PLATFORM'] == 'win32':
'shared_event_unittest.cc',
'shared_memory_unittest.cc',
'stats_table_unittest.cc',
'string_util_unittest.cc',
'thread_local_storage_unittest.cc',
'thread_unittest.cc',
'timer_unittest.cc',
Expand Down Expand Up @@ -310,7 +311,11 @@ SConscript(sconscript_dirs, exports=['env'])


# Setup alias for all base related targets.
env.Alias('base', ['.', installed_base_unittests, '../icudt38.dll'])
if env['PLATFORM'] == 'win32':
icudata = '../icudt38.dll'
else:
icudata = '../icudt38l.dat'
env.Alias('base', ['.', installed_base_unittests, icudata])


# These aren't ported to other platforms yet.
Expand Down
2 changes: 2 additions & 0 deletions base/base_paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include "base/base_paths_win.h"
#elif defined(OS_MACOSX)
#include "base/base_paths_mac.h"
#elif defined(OS_LINUX)
#include "base/base_paths_linux.h"
#endif

namespace base {
Expand Down
57 changes: 57 additions & 0 deletions base/base_paths_linux.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "base/base_paths_linux.h"

#include <unistd.h>

#include "base/logging.h"
#include "base/sys_string_conversions.h"

namespace base {

bool PathProviderLinux(int key, std::wstring* result) {
switch (key) {
case base::FILE_EXE:
case base::FILE_MODULE: { // TODO(evanm): is this correct?
char bin_dir[PATH_MAX + 1];
int bin_dir_size = readlink("/proc/self/exe", bin_dir, PATH_MAX);
if (bin_dir_size < 0 || bin_dir_size > PATH_MAX) {
NOTREACHED() << "Unable to resolve /proc/self/exe.";
return false;
}
bin_dir[bin_dir_size] = 0;
*result = base::SysNativeMBToWide(bin_dir);
return true;
}
}
return false;
}

} // namespace base
52 changes: 52 additions & 0 deletions base/base_paths_linux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef BASE_BASE_PATHS_LINUX_H_
#define BASE_BASE_PATHS_LINUX_H_

// This file declares Linux-specific path keys for the base module.
// These can be used with the PathService to access various special
// directories and files.

namespace base {

enum {
PATH_LINUX_START = 200,

FILE_EXE, // Path and filename of the current executable.
FILE_MODULE, // Path and filename of the module containing the code for the
// PathService (which could differ from FILE_EXE if the
// PathService were compiled into a shared object, for example).

PATH_LINUX_END
};

} // namespace base

#endif // BASE_BASE_PATHS_LINUX_H_
1 change: 0 additions & 1 deletion base/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

namespace file_util {

const wchar_t kPathSeparator = L'\\';
const wchar_t kExtensionSeparator = L'.';

bool EndsWithSeparator(std::wstring* path) {
Expand Down
30 changes: 29 additions & 1 deletion base/file_util_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@
#include <string>

#include "base/logging.h"
#include "base/notimplemented.h"
#include "base/string_util.h"

namespace file_util {

const wchar_t kPathSeparator = L'/';

bool AbsolutePath(std::wstring* path) {
NOTIMPLEMENTED();
return false;
}

bool GetTempDir(std::wstring* path) {
const char* tmp = getenv("TMPDIR");
Expand All @@ -47,7 +55,27 @@ bool GetTempDir(std::wstring* path) {

bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) {
// TODO(erikkay): implement
DCHECK(false);
NOTIMPLEMENTED();
return false;
}

bool PathExists(const std::wstring& path) {
NOTIMPLEMENTED();
return false;
}

bool GetCurrentDirectory(std::wstring* path) {
NOTIMPLEMENTED();
return false;
}

bool CreateDirectory(const std::wstring& full_path) {
NOTIMPLEMENTED();
return false;
}

bool SetCurrentDirectory(const std::wstring& current_directory) {
NOTIMPLEMENTED();
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions base/file_util_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

namespace file_util {

const wchar_t kPathSeparator = L'/';

bool GetTempDir(std::wstring* path) {
NSString* tmp = NSTemporaryDirectory();
if (tmp == nil)
Expand Down
2 changes: 2 additions & 0 deletions base/file_util_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

namespace file_util {

const wchar_t kPathSeparator = L'\\';

std::wstring GetDirectoryFromPath(const std::wstring& path) {
wchar_t path_buffer[MAX_PATH];
wchar_t* file_ptr = NULL;
Expand Down
22 changes: 16 additions & 6 deletions base/icu_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#include "build/build_config.h"

#ifdef OS_WIN
#if defined(OS_WIN)
#include <windows.h>
#endif

Expand All @@ -40,12 +40,14 @@
#include "base/logging.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/sys_string_conversions.h"
#include "unicode/putil.h"
#include "unicode/udata.h"

namespace icu_util {

bool Initialize() {
#ifdef OS_WIN
#if defined(OS_WIN)
// Assert that we are not called more than once. Even though calling this
// function isn't harmful (ICU can handle it), being called twice probably
// indicates a programming error.
Expand All @@ -71,11 +73,19 @@ bool Initialize() {
UErrorCode err = U_ZERO_ERROR;
udata_setCommonData(reinterpret_cast<void*>(addr), &err);
return err == U_ZERO_ERROR;
#else
// Windows ships ICU's data separate, so it needs to link the code to data
// here. Other platforms don't need this.
#elif defined(OS_MACOSX)
// Mac bundles the ICU data in.
return true;
#endif // OS_WIN
#elif defined(OS_LINUX)
// For now, expect the data file to be alongside the executable.
// This is sufficient while we work on unit tests, but will eventually
// likely live in a data directory.
std::wstring data_path;
bool path_ok = PathService::Get(base::DIR_EXE, &data_path);
DCHECK(path_ok);
u_setDataDirectory(base::SysWideToNativeMB(data_path).c_str());
return true;
#endif
}

} // namespace icu_util
4 changes: 4 additions & 0 deletions base/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ const char* const log_severity_names[LOG_NUM_SEVERITIES] = {

int min_log_level = 0;
LogLockingState lock_log_file = LOCK_LOG_FILE;
#if defined(OS_WIN)
LoggingDestination logging_destination = LOG_ONLY_TO_FILE;
#elif defined(OS_POSIX)
LoggingDestination logging_destination = LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG;
#endif

const int kMaxFilteredLogLevel = LOG_WARNING;
std::string* log_filter_prefix;
Expand Down
21 changes: 17 additions & 4 deletions base/path_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ namespace base {
bool PathProvider(int key, std::wstring* result);
#if defined(OS_WIN)
bool PathProviderWin(int key, std::wstring* result);
#elif defined (OS_MACOSX)
#elif defined(OS_MACOSX)
bool PathProviderMac(int key, std::wstring* result);
#elif defined(OS_LINUX)
bool PathProviderLinux(int key, std::wstring* result);
#endif
}

Expand Down Expand Up @@ -97,8 +99,19 @@ static Provider base_provider_win = {
#endif
};
#endif



#if defined(OS_LINUX)
static Provider base_provider_linux = {
base::PathProviderLinux,
&base_provider,
#ifndef NDEBUG
base::PATH_LINUX_START,
base::PATH_LINUX_END
#endif
};
#endif


struct PathData {
Lock lock;
PathMap cache; // Track mappings from path key to path value.
Expand All @@ -111,7 +124,7 @@ struct PathData {
#elif defined(OS_MACOSX)
providers = &base_provider_mac;
#elif defined(OS_LINUX)
providers = &base_provider;
providers = &base_provider_linux;
#endif
}
};
Expand Down
13 changes: 8 additions & 5 deletions base/string_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,11 @@ TEST(StringUtilTest, FormatBytes) {

TEST(StringUtilTest, ReplaceSubstringsAfterOffset) {
static const struct {
wchar_t* str;
const wchar_t* str;
std::wstring::size_type start_offset;
wchar_t* find_this;
wchar_t* replace_with;
wchar_t* expected;
const wchar_t* find_this;
const wchar_t* replace_with;
const wchar_t* expected;
} cases[] = {
{L"aaa", 0, L"a", L"b", L"bbb"},
{L"abb", 0, L"ab", L"a", L"ab"},
Expand Down Expand Up @@ -1053,7 +1053,7 @@ TEST(StringUtilTest, Grow) {
src[i] = 'A';
src[1025] = 0;

char* fmt = "%sB%sB%sB%sB%sB%sB%s";
const char* fmt = "%sB%sB%sB%sB%sB%sB%s";

std::string out;
SStringPrintf(&out, fmt, src, src, src, src, src, src, src);
Expand Down Expand Up @@ -1087,6 +1087,8 @@ TEST(StringUtilTest, GrowBoundary) {
EXPECT_STREQ(src, out.c_str());
}

// TODO(evanm): what's the proper cross-platform test here?
#if defined(OS_WIN)
// sprintf in Visual Studio fails when given U+FFFF. This tests that the
// failure case is gracefuly handled.
TEST(StringUtilTest, Invalid) {
Expand All @@ -1098,6 +1100,7 @@ TEST(StringUtilTest, Invalid) {
SStringPrintf(&out, L"%ls", invalid);
EXPECT_STREQ(L"", out.c_str());
}
#endif

// Test for SplitString
TEST(StringUtilTest, SplitString) {
Expand Down

0 comments on commit 4c0040c

Please sign in to comment.