2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "filesystem/Directory.h"
22 #include "filesystem/File.h"
23 #include "utils/URIUtils.h"
25 #include "settings/Settings.h"
26 #include "test/TestUtils.h"
30 #include "gtest/gtest.h"
32 class TestZipFile : public testing::Test
37 /* Add default settings for locale.
38 * Settings here are taken from CGUISettings::Initialize()
41 CSettingsCategory *loc = CSettings::Get().AddCategory(7, "locale", 14090);
42 CSettings::Get().AddString(loc, "locale.language",248,"english",
44 CSettings::Get().AddString(loc, "locale.country", 20026, "USA",
46 CSettings::Get().AddString(loc, "locale.charset", 14091, "DEFAULT",
47 SPIN_CONTROL_TEXT); // charset is set by the
54 CSettings::Get().Unload();
58 TEST_F(TestZipFile, Read)
62 memset(&buf, 0, sizeof(buf));
63 CStdString reffile, strzippath, strpathinzip;
64 CFileItemList itemlist;
66 reffile = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.zip");
67 URIUtils::CreateArchivePath(strzippath, "zip", reffile, "");
68 ASSERT_TRUE(XFILE::CDirectory::GetDirectory(strzippath, itemlist, "",
69 XFILE::DIR_FLAG_NO_FILE_DIRS));
70 strpathinzip = itemlist[0]->GetPath();
71 ASSERT_TRUE(file.Open(strpathinzip));
72 EXPECT_EQ(0, file.GetPosition());
73 EXPECT_EQ(1616, file.GetLength());
74 EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
76 EXPECT_EQ(20, file.GetPosition());
77 EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1));
78 EXPECT_TRUE(file.ReadString(buf, sizeof(buf)));
79 EXPECT_EQ(39, file.GetPosition());
80 EXPECT_STREQ("an award-winning fr", buf);
81 EXPECT_EQ(100, file.Seek(100));
82 EXPECT_EQ(100, file.GetPosition());
83 EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
85 EXPECT_EQ(120, file.GetPosition());
86 EXPECT_TRUE(!memcmp("ent hub for digital ", buf, sizeof(buf) - 1));
87 EXPECT_EQ(220, file.Seek(100, SEEK_CUR));
88 EXPECT_EQ(220, file.GetPosition());
89 EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
91 EXPECT_EQ(240, file.GetPosition());
92 EXPECT_TRUE(!memcmp("rs, XBMC is a non-pr", buf, sizeof(buf) - 1));
93 EXPECT_EQ(1596, file.Seek(-(int64_t)sizeof(buf), SEEK_END));
94 EXPECT_EQ(1596, file.GetPosition());
95 EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
97 EXPECT_EQ(1616, file.GetPosition());
98 EXPECT_TRUE(!memcmp("multimedia jukebox.\n", buf, sizeof(buf) - 1));
99 EXPECT_EQ(-1, file.Seek(100, SEEK_CUR));
100 EXPECT_EQ(1616, file.GetPosition());
101 EXPECT_EQ(0, file.Seek(0, SEEK_SET));
102 EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
104 EXPECT_EQ(20, file.GetPosition());
105 EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1));
106 EXPECT_EQ(0, file.Seek(0, SEEK_SET));
107 EXPECT_EQ(-1, file.Seek(-100, SEEK_SET));
111 TEST_F(TestZipFile, Exists)
113 CStdString reffile, strzippath, strpathinzip;
114 CFileItemList itemlist;
116 reffile = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.zip");
117 URIUtils::CreateArchivePath(strzippath, "zip", reffile, "");
118 ASSERT_TRUE(XFILE::CDirectory::GetDirectory(strzippath, itemlist, "",
119 XFILE::DIR_FLAG_NO_FILE_DIRS));
120 strpathinzip = itemlist[0]->GetPath();
122 EXPECT_TRUE(XFILE::CFile::Exists(strpathinzip));
125 TEST_F(TestZipFile, Stat)
127 struct __stat64 buffer;
128 CStdString reffile, strzippath, strpathinzip;
129 CFileItemList itemlist;
131 reffile = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.zip");
132 URIUtils::CreateArchivePath(strzippath, "zip", reffile, "");
133 ASSERT_TRUE(XFILE::CDirectory::GetDirectory(strzippath, itemlist, "",
134 XFILE::DIR_FLAG_NO_FILE_DIRS));
135 strpathinzip = itemlist[0]->GetPath();
137 EXPECT_EQ(0, XFILE::CFile::Stat(strpathinzip, &buffer));
138 EXPECT_TRUE(buffer.st_mode | _S_IFREG);
141 /* Test case to test for graceful handling of corrupted input.
142 * NOTE: The test case is considered a "success" as long as the corrupted
143 * file was successfully generated and the test case runs without a segfault.
145 TEST_F(TestZipFile, CorruptedFile)
149 memset(&buf, 0, sizeof(buf));
150 CStdString reffilepath, strzippath, strpathinzip, str;
151 CFileItemList itemlist;
152 unsigned int size, i;
155 reffilepath = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.zip");
156 ASSERT_TRUE((file = XBMC_CREATECORRUPTEDFILE(reffilepath, ".zip")) != NULL);
157 std::cout << "Reference file generated at '" << XBMC_TEMPFILEPATH(file) << "'" << std::endl;
159 URIUtils::CreateArchivePath(strzippath, "zip", XBMC_TEMPFILEPATH(file), "");
160 if (!XFILE::CDirectory::GetDirectory(strzippath, itemlist, "",
161 XFILE::DIR_FLAG_NO_FILE_DIRS))
163 XBMC_DELETETEMPFILE(file);
167 if (itemlist.IsEmpty())
169 XBMC_DELETETEMPFILE(file);
173 strpathinzip = itemlist[0]->GetPath();
175 if (!file->Open(strpathinzip))
177 XBMC_DELETETEMPFILE(file);
181 std::cout << "file->GetLength(): " <<
182 testing::PrintToString(file->GetLength()) << std::endl;
183 std::cout << "file->Seek(file->GetLength() / 2, SEEK_CUR) return value: " <<
184 testing::PrintToString(file->Seek(file->GetLength() / 2, SEEK_CUR)) << std::endl;
185 std::cout << "file->Seek(0, SEEK_END) return value: " <<
186 testing::PrintToString(file->Seek(0, SEEK_END)) << std::endl;
187 std::cout << "file->Seek(0, SEEK_SET) return value: " <<
188 testing::PrintToString(file->Seek(0, SEEK_SET)) << std::endl;
189 std::cout << "File contents:" << std::endl;
190 while ((size = file->Read(buf, sizeof(buf))) > 0)
192 str.Format(" %08X", count);
193 std::cout << str << " ";
195 for (i = 0; i < size; i++)
197 str.Format("%02X ", buf[i]);
200 while (i++ < sizeof(buf))
203 for (i = 0; i < size; i++)
205 if (buf[i] >= ' ' && buf[i] <= '~')
210 std::cout << "]" << std::endl;
213 XBMC_DELETETEMPFILE(file);