2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
19 #include <gtest/gtest.h>
21 #include "hphp/runtime/base/mem-file.h"
22 #include "hphp/runtime/base/resource-data.h"
23 #include "hphp/runtime/server/static-content-cache.h"
31 bool makeTempFile(const string
& contents
, string
* fn
) {
32 char fn_tmpl
[] = "/tmp/hhvm_unit_test.XXXXXX";
33 int fd
= mkstemp(fn_tmpl
);
39 ssize_t ret
= write(fd
, contents
.c_str(), contents
.length());
42 if (ret
!= contents
.length()) {
52 TEST(TestMemFile
, DataConstructor
) {
53 const char* test_data
= "some test data";
54 auto mf
= req::make
<MemFile
>(test_data
, strlen(test_data
));
56 ASSERT_EQ(mf
->getc(), 's');
57 ASSERT_EQ(mf
->getc(), 'o');
58 ASSERT_EQ(mf
->getc(), 'm');
59 ASSERT_EQ(mf
->getc(), 'e');
60 ASSERT_EQ(mf
->getc(), ' ');
61 ASSERT_EQ(mf
->getc(), 't');
62 ASSERT_EQ(mf
->getc(), 'e');
63 ASSERT_EQ(mf
->getc(), 's');
64 ASSERT_EQ(mf
->getc(), 't');
65 ASSERT_EQ(mf
->getc(), ' ');
66 ASSERT_EQ(mf
->getc(), 'd');
67 ASSERT_EQ(mf
->getc(), 'a');
68 ASSERT_EQ(mf
->getc(), 't');
69 ASSERT_EQ(mf
->getc(), 'a');
70 ASSERT_EQ(mf
->getc(), EOF
);
73 TEST(TestMemFile
, BadReadFromCache
) {
74 StaticContentCache::TheFileCache
= std::make_shared
<FileCache
>();
76 auto mf
= req::make
<MemFile
>();
77 ASSERT_FALSE(mf
->open("/some/random/file", "r"));
80 TEST(TestMemFile
, BadOpenModes
) {
81 auto mf
= req::make
<MemFile
>();
82 ASSERT_FALSE(mf
->open("/some/file1", "+"));
83 ASSERT_FALSE(mf
->open("/some/file2", "a"));
84 ASSERT_FALSE(mf
->open("/some/file3", "w"));
87 TEST(TestMemFile
, EmptyFileInCache
) {
88 StaticContentCache::TheFileCache
= std::make_shared
<FileCache
>();
89 StaticContentCache::TheFileCache
->write("/no/content/entry");
91 auto mf
= req::make
<MemFile
>();
94 ASSERT_FALSE(mf
->open("/no/content/entry", "r"));
96 // ... and one of the automatically-created "directory" entries.
97 ASSERT_FALSE(mf
->open("/no/content", "r"));
100 TEST(TestMemFile
, NotInCache
) {
101 StaticContentCache::TheFileCache
= std::make_shared
<FileCache
>();
103 auto mf
= req::make
<MemFile
>();
104 ASSERT_FALSE(mf
->open("/not/even/there", "r"));
107 TEST(TestMemFile
, RealFileInCache
) {
109 ASSERT_TRUE(makeTempFile("123abc", &temp_fn
));
111 StaticContentCache::TheFileCache
= std::make_shared
<FileCache
>();
112 StaticContentCache::TheFileCache
->write("/content", temp_fn
.c_str());
114 auto mf
= req::make
<MemFile
>();
115 ASSERT_TRUE(mf
->open("/content", "r"));
117 ASSERT_EQ(mf
->getc(), '1');
118 ASSERT_EQ(mf
->getc(), '2');
119 ASSERT_EQ(mf
->getc(), '3');
120 ASSERT_EQ(mf
->getc(), 'a');
121 ASSERT_EQ(mf
->getc(), 'b');
122 ASSERT_EQ(mf
->getc(), 'c');
124 ASSERT_EQ(mf
->getc(), EOF
);
126 ASSERT_TRUE(mf
->close());
128 ASSERT_EQ(unlink(temp_fn
.c_str()), 0);