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
27
28
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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Cocoa/Cocoa.h>
#include "base/file_util.h"
#include "base/logging.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/memory/scoped_ptr.h"
#include "base/shared_memory.h"
#include "content/common/mac/font_descriptor.h"
#include "content/common/mac/font_loader.h"
#include "content/common/sandbox_mac_unittest_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
using sandboxtest::MacSandboxTest;
using sandbox::Sandbox;
class FontLoadingTestCase : public sandboxtest::MacSandboxTestCase {
public:
FontLoadingTestCase() : font_data_length_(-1) {}
virtual bool BeforeSandboxInit();
virtual bool SandboxedTest();
private:
scoped_ptr<base::SharedMemory> font_shmem_;
size_t font_data_length_;
};
REGISTER_SANDBOX_TEST_CASE(FontLoadingTestCase);
// Load raw font data into shared memory object.
bool FontLoadingTestCase::BeforeSandboxInit() {
std::string font_data;
if (!file_util::ReadFileToString(FilePath(test_data_.c_str()), &font_data)) {
LOG(ERROR) << "Failed to read font data from file (" << test_data_ << ")";
return false;
}
font_data_length_ = font_data.length();
if (font_data_length_ <= 0) {
LOG(ERROR) << "No font data: " << font_data_length_;
return false;
}
font_shmem_.reset(new base::SharedMemory);
if (!font_shmem_.get()) {
LOG(ERROR) << "Failed to create shared memory object.";
return false;
}
if (!font_shmem_->CreateAndMapAnonymous(font_data_length_)) {
LOG(ERROR) << "SharedMemory::Create failed";
return false;
}
memcpy(font_shmem_->memory(), font_data.c_str(), font_data_length_);
if (!font_shmem_->Unmap()) {
LOG(ERROR) << "SharedMemory::Unmap failed";
return false;
}
return true;
}
bool FontLoadingTestCase::SandboxedTest() {
base::SharedMemoryHandle shmem_handle;
if (!font_shmem_->ShareToProcess(NULL, &shmem_handle)) {
LOG(ERROR) << "SharedMemory::ShareToProcess failed";
return false;
}
CGFontRef cg_font_ref;
if (!FontLoader::CGFontRefFromBuffer(shmem_handle, font_data_length_,
&cg_font_ref)) {
LOG(ERROR) << "Call to CreateCGFontFromBuffer() failed";
return false;
}
if (!cg_font_ref) {
LOG(ERROR) << "Got NULL CGFontRef";
return false;
}
base::mac::ScopedCFTypeRef<CGFontRef> cgfont(cg_font_ref);
CTFontRef ct_font_ref =
CTFontCreateWithGraphicsFont(cgfont.get(), 16.0, NULL, NULL);
base::mac::ScopedCFTypeRef<CTFontRef> ctfont(ct_font_ref);
if (!ct_font_ref) {
LOG(ERROR) << "CTFontCreateWithGraphicsFont() failed";
return false;
}
// Do something with the font to make sure it's loaded.
CGFloat cap_height = CTFontGetCapHeight(ct_font_ref);
if (cap_height <= 0.0) {
LOG(ERROR) << "Got bad value for CTFontGetCapHeight " << cap_height;
return false;
}
return true;
}
TEST_F(MacSandboxTest, FontLoadingTest) {
FilePath temp_file_path;
FILE* temp_file = file_util::CreateAndOpenTemporaryFile(&temp_file_path);
ASSERT_TRUE(temp_file);
file_util::ScopedFILE temp_file_closer(temp_file);
NSFont* srcFont = [NSFont fontWithName:@"Geeza Pro" size:16.0];
FontDescriptor descriptor(srcFont);
FontLoader::Result result;
FontLoader::LoadFont(descriptor, &result);
EXPECT_GT(result.font_data_size, 0U);
EXPECT_GT(result.font_id, 0U);
file_util::WriteFileDescriptor(fileno(temp_file),
static_cast<const char *>(result.font_data.memory()),
result.font_data_size);
ASSERT_TRUE(RunTestInSandbox(content::SANDBOX_TYPE_RENDERER,
"FontLoadingTestCase", temp_file_path.value().c_str()));
temp_file_closer.reset();
ASSERT_TRUE(file_util::Delete(temp_file_path, false));
}
} // namespace
|