dart-sdk/runtime/bin/file_test.cc
Zachary Anderson d0295c873c [dart:io] Namespaces for file IO
Fuchsia requires the ability to sandbox Isolates w.r.t. file IO.
When a new Isolate starts, Fuchsia will pass the Isolate an object
called a namespace. We can translate the namespace object into a
file descriptor suitable for passing to the *at() family of
POSIX file system calls. The file system calls will then
have visibility only into the specified namespace.

We also plumb Namespaces through on all the other platforms as well to
make the change easier to test and so that in the future we can
implement e.g. per-isolate cwds.

This change adds a new internal class to dart:io called _Namespace,
which is implemented in a patch file. See:

sdk/lib/io/namespace_impl.dart
runtime/bin/namespace_patch.dart

The embedder can set up a non-default namespace by calling
_Namespace._setupNamespace during Isolate setup.

Instances of _Namespace have a native field that holds a pointer
to a native Namespace object. See:

runtime/bin/namespace.h

Calls from e.g. file_impl.dart are now also passed a
_Namespace object. The implementations in e.g. file.cc and
file_linux.cc then extract the namespace, and use it to compute a
file descriptor and path suitable for passing to e.g. openat().

related US-313

R=asiva@google.com, rmacnak@google.com

Review-Url: https://codereview.chromium.org/3007703002 .
2017-08-30 09:34:36 -07:00

61 lines
1.7 KiB
C++

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "bin/file.h"
#include "platform/assert.h"
#include "platform/globals.h"
#include "vm/unit_test.h"
namespace dart {
namespace bin {
// Helper method to be able to run the test from the runtime
// directory, or the top directory.
static const char* GetFileName(const char* name) {
if (File::Exists(NULL, name)) {
return name;
} else {
static const int kRuntimeLength = strlen("runtime/");
return name + kRuntimeLength;
}
}
TEST_CASE(Read) {
const char* kFilename = GetFileName("runtime/bin/file_test.cc");
File* file = File::Open(NULL, kFilename, File::kRead);
EXPECT(file != NULL);
char buffer[16];
buffer[0] = '\0';
EXPECT(file->ReadFully(buffer, 13)); // ReadFully returns true.
buffer[13] = '\0';
EXPECT_STREQ("// Copyright ", buffer);
EXPECT(!file->WriteByte(1)); // Cannot write to a read-only file.
file->Release();
}
TEST_CASE(FileLength) {
const char* kFilename =
GetFileName("runtime/tests/vm/data/fixed_length_file");
File* file = File::Open(NULL, kFilename, File::kRead);
EXPECT(file != NULL);
EXPECT_EQ(42, file->Length());
file->Release();
}
TEST_CASE(FilePosition) {
char buf[42];
const char* kFilename =
GetFileName("runtime/tests/vm/data/fixed_length_file");
File* file = File::Open(NULL, kFilename, File::kRead);
EXPECT(file != NULL);
EXPECT(file->ReadFully(buf, 12));
EXPECT_EQ(12, file->Position());
EXPECT(file->ReadFully(buf, 6));
EXPECT_EQ(18, file->Position());
file->Release();
}
} // namespace bin
} // namespace dart