Files
sdk/runtime/bin/file_test.cc
Daco Harkes f98a2138b7 [vm] Run clang-format on code base
When uploading CLs, the presubmit checks verify that the lines in the
diff are formatted correctly according to `git cl format runtime`.

However, when `buildtools/<os>-<arch>/clang/bin/clang-format` is
updated, it does not force reformatting of files that would be
reformatted.

This leads to two issues:
* Inconsistent style within the code base and within a single file.
* Spurious reformatting in CLs when (1) clang-format is used on the
  whole file, or (2) the diff lines overlap.

`clang-format` doesn't change that frequently, so in general this is
not a large issue, but I've seen a bit too many "spurious formatting,
please revert" comments on CLs recently.

This CL formats the runtime to be in line with the current pinned
`clang-format`:

```
$ find runtime/ -iname *.h -o -iname *.cc | xargs buildtools/mac-arm64/clang/bin/clang-format -i
```

`git cl format` (which only formats changed lines, and does so with
`clang-format`) seems to not agree with itself, or clang-format, or
cpplint in a handful of places. This CL adds `// clang-format off`
for these. (See previous patchsets for the specific instances.)

TEST=A variety of bots including GCC, MacOS and Windows.

Change-Id: I470892e898971899fda14bb3b8f2c8efefd67686
Cq-Include-Trybots: luci.dart.try:vm-gcc-linux-try,vm-ffi-qemu-linux-release-riscv64-try,vm-ffi-qemu-linux-release-arm-try,vm-aot-win-debug-x64-try,vm-win-debug-x64c-try,vm-mac-debug-x64-try,vm-mac-debug-arm64-try,vm-aot-linux-debug-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/362780
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
2024-04-17 19:14:41 +00:00

234 lines
7.2 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 "bin/dartutils.h"
#include "bin/directory.h"
#include "bin/test_utils.h"
#include "platform/assert.h"
#include "platform/globals.h"
#include "vm/unit_test.h"
namespace dart {
TEST_CASE(Read) {
const char* kFilename = bin::test::GetFileName("runtime/bin/file_test.cc");
bin::File* file = bin::File::Open(nullptr, kFilename, bin::File::kRead);
EXPECT(file != nullptr);
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(OpenUri_RelativeFilename) {
const char* kFilename = bin::test::GetFileName("runtime/bin/file_test.cc");
char* encoded = reinterpret_cast<char*>(
bin::DartUtils::ScopedCString(strlen(kFilename) * 3 + 1));
char* t = encoded;
// percent-encode all characters 'c'
for (const char* p = kFilename; *p != '\0'; p++) {
if (*p == 'c') {
*t++ = '%';
*t++ = '6';
*t++ = '3';
} else {
*t++ = *p;
}
}
*t = 0;
bin::File* file = bin::File::OpenUri(nullptr, encoded, bin::File::kRead);
EXPECT(file != nullptr);
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(OpenUri_AbsoluteFilename) {
const char* kRelativeFilename =
bin::test::GetFileName("runtime/bin/file_test.cc");
const char* kFilename =
bin::File::GetCanonicalPath(nullptr, kRelativeFilename);
EXPECT_NOTNULL(kFilename);
char* encoded = reinterpret_cast<char*>(
bin::DartUtils::ScopedCString(strlen(kFilename) * 3 + 1));
char* t = encoded;
// percent-encode all characters 'c'
for (const char* p = kFilename; *p != '\0'; p++) {
if (*p == 'c') {
*t++ = '%';
*t++ = '6';
*t++ = '3';
} else {
*t++ = *p;
}
}
*t = 0;
bin::File* file = bin::File::OpenUri(nullptr, encoded, bin::File::kRead);
EXPECT(file != nullptr);
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();
}
static const char* Concat(const char* a, const char* b) {
const intptr_t len = strlen(a) + strlen(b);
char* c = bin::DartUtils::ScopedCString(len + 1);
EXPECT_NOTNULL(c);
snprintf(c, len + 1, "%s%s", a, b);
return c;
}
TEST_CASE(OpenUri_ValidUri) {
const char* kRelativeFilename =
bin::test::GetFileName("runtime/bin/file_test.cc");
const char* kAbsoluteFilename =
bin::File::GetCanonicalPath(nullptr, kRelativeFilename);
EXPECT_NOTNULL(kAbsoluteFilename);
const char* kFilename = Concat("file:///", kAbsoluteFilename);
char* encoded = reinterpret_cast<char*>(
bin::DartUtils::ScopedCString(strlen(kFilename) * 3 + 1));
char* t = encoded;
// percent-encode all characters 'c'
for (const char* p = kFilename; *p != '\0'; p++) {
if (*p == 'c') {
*t++ = '%';
*t++ = '6';
*t++ = '3';
} else {
*t++ = *p;
}
}
*t = 0;
bin::File* file = bin::File::OpenUri(nullptr, encoded, bin::File::kRead);
EXPECT(file != nullptr);
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(OpenUri_UriWithSpaces) {
const char* kRelativeFilename =
bin::test::GetFileName("runtime/bin/file_test.cc");
const char* strSystemTemp = bin::Directory::SystemTemp(nullptr);
EXPECT_NOTNULL(strSystemTemp);
const char* kTempDir = Concat(strSystemTemp, "/foo bar");
const char* strTempDir = bin::Directory::CreateTemp(nullptr, kTempDir);
EXPECT_NOTNULL(strTempDir);
const char* kTargetFilename = Concat(strTempDir, "/file test.cc");
bool result = bin::File::Copy(nullptr, kRelativeFilename, kTargetFilename);
EXPECT(result);
const char* kAbsoluteFilename =
bin::File::GetCanonicalPath(nullptr, kTargetFilename);
EXPECT_NOTNULL(kAbsoluteFilename);
const char* kFilename = Concat("file:///", kAbsoluteFilename);
char* encoded = reinterpret_cast<char*>(
bin::DartUtils::ScopedCString(strlen(kFilename) * 3 + 1));
char* t = encoded;
// percent-encode all spaces
for (const char* p = kFilename; *p != '\0'; p++) {
if (*p == ' ') {
*t++ = '%';
*t++ = '2';
*t++ = '0';
} else {
*t++ = *p;
}
}
*t = 0;
printf("encoded: %s\n", encoded);
bin::File* file = bin::File::OpenUri(nullptr, encoded, bin::File::kRead);
EXPECT(file != nullptr);
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();
bin::Directory::Delete(nullptr, strTempDir, /* recursive= */ true);
}
TEST_CASE(OpenUri_InvalidUriPercentEncoding) {
const char* kFilename = bin::test::GetFileName("runtime/bin/file_test.cc");
char* encoded = reinterpret_cast<char*>(
bin::DartUtils::ScopedCString(strlen(kFilename) * 3 + 1));
char* t = encoded;
// percent-encode all characters 'c'
for (const char* p = kFilename; *p != '\0'; p++) {
if (*p == 'c') {
*t++ = '%';
*t++ = 'f';
*t++ = 'o';
} else {
*t++ = *p;
}
}
*t = 0;
bin::File* file = bin::File::OpenUri(nullptr, encoded, bin::File::kRead);
EXPECT(file == nullptr);
}
TEST_CASE(OpenUri_TruncatedUriPercentEncoding) {
const char* kFilename = bin::test::GetFileName("runtime/bin/file_test.cc");
char* encoded = reinterpret_cast<char*>(
bin::DartUtils::ScopedCString(strlen(kFilename) * 3 + 1));
char* t = encoded;
// percent-encode all characters 'c'
for (const char* p = kFilename; *p != '\0'; p++) {
if (*p == 'c') {
*t++ = '%';
*t++ = 'f';
*t++ = 'o';
} else {
*t++ = *p;
}
}
*(t - 1) = 0; // truncate last uri encoding
bin::File* file = bin::File::OpenUri(nullptr, encoded, bin::File::kRead);
EXPECT(file == nullptr);
}
TEST_CASE(FileLength) {
const char* kFilename =
bin::test::GetFileName("runtime/tests/vm/data/fixed_length_file");
bin::File* file = bin::File::Open(nullptr, kFilename, bin::File::kRead);
EXPECT(file != nullptr);
EXPECT_EQ(42, file->Length());
file->Release();
}
TEST_CASE(FilePosition) {
char buf[42];
const char* kFilename =
bin::test::GetFileName("runtime/tests/vm/data/fixed_length_file");
bin::File* file = bin::File::Open(nullptr, kFilename, bin::File::kRead);
EXPECT(file != nullptr);
EXPECT(file->ReadFully(buf, 12));
EXPECT_EQ(12, file->Position());
EXPECT(file->ReadFully(buf, 6));
EXPECT_EQ(18, file->Position());
file->Release();
}
} // namespace dart