Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
snapshot_utils_test.cc
Go to the documentation of this file.
1// Copyright (c) 2022, 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
6#include "bin/dartutils.h"
7#include "bin/file.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
15#if defined(DART_TARGET_OS_MACOS)
16
17static const unsigned char kMachO32BitLittleEndianHeader[] = {
18 0xce, 0xfa, 0xed, 0xfe, 0x07, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00,
19 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21};
22
23static const unsigned char kMachO32BitBigEndianHeader[] = {
24 0xfe, 0xed, 0xfa, 0xce, 0x01, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
25 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27};
28
29static const unsigned char kMachO64BitLittleEndianHeader[] = {
30 0xcf, 0xfa, 0xed, 0xfe, 0x07, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00,
31 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33};
34
35static const unsigned char kMachO64BitBigEndianHeader[] = {
36 0xfe, 0xed, 0xfa, 0xcf, 0x01, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
37 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39};
40
41static const struct {
42 const char* filename;
43 const unsigned char* contents;
44 size_t contents_size;
45} kTestcases[] = {
46 {"macho_32bit_little_endian", kMachO32BitLittleEndianHeader,
47 ARRAY_SIZE(kMachO32BitLittleEndianHeader)},
48 {"macho_32bit_big_endian", kMachO32BitBigEndianHeader,
49 ARRAY_SIZE(kMachO32BitBigEndianHeader)},
50 {"macho_64bit_little_endian", kMachO64BitLittleEndianHeader,
51 ARRAY_SIZE(kMachO64BitLittleEndianHeader)},
52 {"macho_64bit_big_endian", kMachO64BitBigEndianHeader,
53 ARRAY_SIZE(kMachO64BitBigEndianHeader)},
54};
55
56TEST_CASE(CanDetectMachOFiles) {
57 for (uintptr_t i = 0; i < ARRAY_SIZE(kTestcases); i++) {
58 const auto& testcase = kTestcases[i];
59 auto* const file =
60 bin::DartUtils::OpenFile(testcase.filename, /*write=*/true);
61 bin::DartUtils::WriteFile(testcase.contents, testcase.contents_size, file);
63
64 EXPECT(bin::Snapshot::IsMachOFormattedBinary(testcase.filename));
65
66 EXPECT(bin::File::Delete(nullptr, testcase.filename));
67 }
68
69 const char* kFilename =
70 bin::test::GetFileName("runtime/bin/snapshot_utils_test.cc");
71 EXPECT(!bin::Snapshot::IsMachOFormattedBinary(kFilename));
72}
73#endif
74
75} // namespace dart
#define EXPECT(type, expectedAlignment, expectedSize)
static void CloseFile(void *stream)
Definition dartutils.cc:307
static void * OpenFile(const char *name, bool write)
Definition dartutils.cc:265
static void WriteFile(const void *buffer, intptr_t num_bytes, void *stream)
Definition dartutils.cc:298
static bool Delete(Namespace *namespc, const char *path)
const char * GetFileName(const char *name)
Definition test_utils.cc:12
#define TEST_CASE(name)
Definition unit_test.h:85
#define ARRAY_SIZE(array)
Definition globals.h:72