Flutter Engine
The Flutter Engine
file_test.cc
Go to the documentation of this file.
1// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "bin/file.h"
6#include "bin/dartutils.h"
7#include "bin/directory.h"
8#include "bin/test_utils.h"
9#include "platform/assert.h"
10#include "platform/globals.h"
11#include "vm/unit_test.h"
12
13namespace dart {
14
16 const char* kFilename = bin::test::GetFileName("runtime/bin/file_test.cc");
17 bin::File* file = bin::File::Open(nullptr, kFilename, bin::File::kRead);
18 EXPECT(file != nullptr);
19 char buffer[16];
20 buffer[0] = '\0';
21 EXPECT(file->ReadFully(buffer, 13)); // ReadFully returns true.
22 buffer[13] = '\0';
23 EXPECT_STREQ("// Copyright ", buffer);
24 EXPECT(!file->WriteByte(1)); // Cannot write to a read-only file.
25 file->Release();
26}
27
28TEST_CASE(OpenUri_RelativeFilename) {
29 const char* kFilename = bin::test::GetFileName("runtime/bin/file_test.cc");
30 char* encoded = reinterpret_cast<char*>(
31 bin::DartUtils::ScopedCString(strlen(kFilename) * 3 + 1));
32 char* t = encoded;
33 // percent-encode all characters 'c'
34 for (const char* p = kFilename; *p != '\0'; p++) {
35 if (*p == 'c') {
36 *t++ = '%';
37 *t++ = '6';
38 *t++ = '3';
39 } else {
40 *t++ = *p;
41 }
42 }
43 *t = 0;
45 EXPECT(file != nullptr);
46 char buffer[16];
47 buffer[0] = '\0';
48 EXPECT(file->ReadFully(buffer, 13)); // ReadFully returns true.
49 buffer[13] = '\0';
50 EXPECT_STREQ("// Copyright ", buffer);
51 EXPECT(!file->WriteByte(1)); // Cannot write to a read-only file.
52 file->Release();
53}
54
55TEST_CASE(OpenUri_AbsoluteFilename) {
56 const char* kRelativeFilename =
57 bin::test::GetFileName("runtime/bin/file_test.cc");
58 const char* kFilename =
59 bin::File::GetCanonicalPath(nullptr, kRelativeFilename);
60 EXPECT_NOTNULL(kFilename);
61 char* encoded = reinterpret_cast<char*>(
62 bin::DartUtils::ScopedCString(strlen(kFilename) * 3 + 1));
63 char* t = encoded;
64 // percent-encode all characters 'c'
65 for (const char* p = kFilename; *p != '\0'; p++) {
66 if (*p == 'c') {
67 *t++ = '%';
68 *t++ = '6';
69 *t++ = '3';
70 } else {
71 *t++ = *p;
72 }
73 }
74 *t = 0;
76 EXPECT(file != nullptr);
77 char buffer[16];
78 buffer[0] = '\0';
79 EXPECT(file->ReadFully(buffer, 13)); // ReadFully returns true.
80 buffer[13] = '\0';
81 EXPECT_STREQ("// Copyright ", buffer);
82 EXPECT(!file->WriteByte(1)); // Cannot write to a read-only file.
83 file->Release();
84}
85
86static const char* Concat(const char* a, const char* b) {
87 const intptr_t len = strlen(a) + strlen(b);
89 EXPECT_NOTNULL(c);
90 snprintf(c, len + 1, "%s%s", a, b);
91 return c;
92}
93
94TEST_CASE(OpenUri_ValidUri) {
95 const char* kRelativeFilename =
96 bin::test::GetFileName("runtime/bin/file_test.cc");
97 const char* kAbsoluteFilename =
98 bin::File::GetCanonicalPath(nullptr, kRelativeFilename);
99 EXPECT_NOTNULL(kAbsoluteFilename);
100 const char* kFilename = Concat("file:///", kAbsoluteFilename);
101
102 char* encoded = reinterpret_cast<char*>(
103 bin::DartUtils::ScopedCString(strlen(kFilename) * 3 + 1));
104 char* t = encoded;
105 // percent-encode all characters 'c'
106 for (const char* p = kFilename; *p != '\0'; p++) {
107 if (*p == 'c') {
108 *t++ = '%';
109 *t++ = '6';
110 *t++ = '3';
111 } else {
112 *t++ = *p;
113 }
114 }
115 *t = 0;
117 EXPECT(file != nullptr);
118 char buffer[16];
119 buffer[0] = '\0';
120 EXPECT(file->ReadFully(buffer, 13)); // ReadFully returns true.
121 buffer[13] = '\0';
122 EXPECT_STREQ("// Copyright ", buffer);
123 EXPECT(!file->WriteByte(1)); // Cannot write to a read-only file.
124 file->Release();
125}
126
127TEST_CASE(OpenUri_UriWithSpaces) {
128 const char* kRelativeFilename =
129 bin::test::GetFileName("runtime/bin/file_test.cc");
130 const char* strSystemTemp = bin::Directory::SystemTemp(nullptr);
131 EXPECT_NOTNULL(strSystemTemp);
132 const char* kTempDir = Concat(strSystemTemp, "/foo bar");
133 const char* strTempDir = bin::Directory::CreateTemp(nullptr, kTempDir);
134 EXPECT_NOTNULL(strTempDir);
135 const char* kTargetFilename = Concat(strTempDir, "/file test.cc");
136 bool result = bin::File::Copy(nullptr, kRelativeFilename, kTargetFilename);
137 EXPECT(result);
138
139 const char* kAbsoluteFilename =
140 bin::File::GetCanonicalPath(nullptr, kTargetFilename);
141 EXPECT_NOTNULL(kAbsoluteFilename);
142 const char* kFilename = Concat("file:///", kAbsoluteFilename);
143
144 char* encoded = reinterpret_cast<char*>(
145 bin::DartUtils::ScopedCString(strlen(kFilename) * 3 + 1));
146 char* t = encoded;
147 // percent-encode all spaces
148 for (const char* p = kFilename; *p != '\0'; p++) {
149 if (*p == ' ') {
150 *t++ = '%';
151 *t++ = '2';
152 *t++ = '0';
153 } else {
154 *t++ = *p;
155 }
156 }
157 *t = 0;
158 printf("encoded: %s\n", encoded);
160 EXPECT(file != nullptr);
161 char buffer[16];
162 buffer[0] = '\0';
163 EXPECT(file->ReadFully(buffer, 13)); // ReadFully returns true.
164 buffer[13] = '\0';
165 EXPECT_STREQ("// Copyright ", buffer);
166 EXPECT(!file->WriteByte(1)); // Cannot write to a read-only file.
167 file->Release();
168 bin::Directory::Delete(nullptr, strTempDir, /* recursive= */ true);
169}
170
171TEST_CASE(OpenUri_InvalidUriPercentEncoding) {
172 const char* kFilename = bin::test::GetFileName("runtime/bin/file_test.cc");
173 char* encoded = reinterpret_cast<char*>(
174 bin::DartUtils::ScopedCString(strlen(kFilename) * 3 + 1));
175 char* t = encoded;
176 // percent-encode all characters 'c'
177 for (const char* p = kFilename; *p != '\0'; p++) {
178 if (*p == 'c') {
179 *t++ = '%';
180 *t++ = 'f';
181 *t++ = 'o';
182 } else {
183 *t++ = *p;
184 }
185 }
186 *t = 0;
188 EXPECT(file == nullptr);
189}
190
191TEST_CASE(OpenUri_TruncatedUriPercentEncoding) {
192 const char* kFilename = bin::test::GetFileName("runtime/bin/file_test.cc");
193 char* encoded = reinterpret_cast<char*>(
194 bin::DartUtils::ScopedCString(strlen(kFilename) * 3 + 1));
195 char* t = encoded;
196 // percent-encode all characters 'c'
197 for (const char* p = kFilename; *p != '\0'; p++) {
198 if (*p == 'c') {
199 *t++ = '%';
200 *t++ = 'f';
201 *t++ = 'o';
202 } else {
203 *t++ = *p;
204 }
205 }
206 *(t - 1) = 0; // truncate last uri encoding
208 EXPECT(file == nullptr);
209}
210
211TEST_CASE(FileLength) {
212 const char* kFilename =
213 bin::test::GetFileName("runtime/tests/vm/data/fixed_length_file");
214 bin::File* file = bin::File::Open(nullptr, kFilename, bin::File::kRead);
215 EXPECT(file != nullptr);
216 EXPECT_EQ(42, file->Length());
217 file->Release();
218}
219
220TEST_CASE(FilePosition) {
221 char buf[42];
222 const char* kFilename =
223 bin::test::GetFileName("runtime/tests/vm/data/fixed_length_file");
224 bin::File* file = bin::File::Open(nullptr, kFilename, bin::File::kRead);
225 EXPECT(file != nullptr);
226 EXPECT(file->ReadFully(buf, 12));
227 EXPECT_EQ(12, file->Position());
228 EXPECT(file->ReadFully(buf, 6));
229 EXPECT_EQ(18, file->Position());
230 file->Release();
231}
232
233} // namespace dart
#define EXPECT(type, expectedAlignment, expectedSize)
static char * ScopedCString(intptr_t length)
Definition: dartutils.h:224
static bool Delete(Namespace *namespc, const char *path, bool recursive)
static const char * CreateTemp(Namespace *namespc, const char *path)
static const char * SystemTemp(Namespace *namespc)
static const char * GetCanonicalPath(Namespace *namespc, const char *path, char *dest=nullptr, int dest_size=0)
static File * OpenUri(Namespace *namespc, const char *uri, FileOpenMode mode)
static bool Copy(Namespace *namespc, const char *old_path, const char *new_path)
static File * Open(Namespace *namespc, const char *path, FileOpenMode mode)
static bool b
struct MyStruct a[10]
GAsyncResult * result
SK_API bool Read(SkStreamSeekable *src, SkDocumentPage *dstArray, int dstArrayCount, const SkDeserialProcs *=nullptr)
std::string printf(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: SkSLString.cpp:83
const char * GetFileName(const char *name)
Definition: test_utils.cc:12
Definition: dart_vm.cc:33
static const char * Concat(const char *a, const char *b)
Definition: file_test.cc:86
TEST_CASE(DirectoryCurrent)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126