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/StringUtils.h"
24 #include "utils/URIUtils.h"
26 #include "settings/Settings.h"
27 #include "test/TestUtils.h"
31 #include "gtest/gtest.h"
33 class TestZipFile : public testing::Test
38 /* Add default settings for locale.
39 * Settings here are taken from CGUISettings::Initialize()
42 CSettingsCategory *loc = CSettings::Get().AddCategory(7, "locale", 14090);
43 CSettings::Get().AddString(loc, "locale.language",248,"english",
45 CSettings::Get().AddString(loc, "locale.country", 20026, "USA",
47 CSettings::Get().AddString(loc, "locale.charset", 14091, "DEFAULT",
48 SPIN_CONTROL_TEXT); // charset is set by the
55 CSettings::Get().Unload();
59 TEST_F(TestZipFile, Read)
63 memset(&buf, 0, sizeof(buf));
64 CStdString reffile, strzippath, strpathinzip;
65 CFileItemList itemlist;
67 reffile = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.zip");
68 URIUtils::CreateArchivePath(strzippath, "zip", reffile, "");
69 ASSERT_TRUE(XFILE::CDirectory::GetDirectory(strzippath, itemlist, "",
70 XFILE::DIR_FLAG_NO_FILE_DIRS));
71 strpathinzip = itemlist[0]->GetPath();
72 ASSERT_TRUE(file.Open(strpathinzip));
73 EXPECT_EQ(0, file.GetPosition());
74 EXPECT_EQ(1616, file.GetLength());
75 EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
77 EXPECT_EQ(20, file.GetPosition());
78 EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1));
79 EXPECT_TRUE(file.ReadString(buf, sizeof(buf)));
80 EXPECT_EQ(39, file.GetPosition());
81 EXPECT_STREQ("an award-winning fr", buf);
82 EXPECT_EQ(100, file.Seek(100));
83 EXPECT_EQ(100, file.GetPosition());
84 EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
86 EXPECT_EQ(120, file.GetPosition());
87 EXPECT_TRUE(!memcmp("ent hub for digital ", buf, sizeof(buf) - 1));
88 EXPECT_EQ(220, file.Seek(100, SEEK_CUR));
89 EXPECT_EQ(220, file.GetPosition());
90 EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
92 EXPECT_EQ(240, file.GetPosition());
93 EXPECT_TRUE(!memcmp("rs, XBMC is a non-pr", buf, sizeof(buf) - 1));
94 EXPECT_EQ(1596, file.Seek(-(int64_t)sizeof(buf), SEEK_END));
95 EXPECT_EQ(1596, file.GetPosition());
96 EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
98 EXPECT_EQ(1616, file.GetPosition());
99 EXPECT_TRUE(!memcmp("multimedia jukebox.\n", buf, sizeof(buf) - 1));
100 EXPECT_EQ(-1, file.Seek(100, SEEK_CUR));
101 EXPECT_EQ(1616, file.GetPosition());
102 EXPECT_EQ(0, file.Seek(0, SEEK_SET));
103 EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
105 EXPECT_EQ(20, file.GetPosition());
106 EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1));
107 EXPECT_EQ(0, file.Seek(0, SEEK_SET));
108 EXPECT_EQ(-1, file.Seek(-100, SEEK_SET));
112 TEST_F(TestZipFile, Exists)
114 CStdString reffile, strzippath, strpathinzip;
115 CFileItemList itemlist;
117 reffile = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.zip");
118 URIUtils::CreateArchivePath(strzippath, "zip", reffile, "");
119 ASSERT_TRUE(XFILE::CDirectory::GetDirectory(strzippath, itemlist, "",
120 XFILE::DIR_FLAG_NO_FILE_DIRS));
121 strpathinzip = itemlist[0]->GetPath();
123 EXPECT_TRUE(XFILE::CFile::Exists(strpathinzip));
126 TEST_F(TestZipFile, Stat)
128 struct __stat64 buffer;
129 CStdString reffile, strzippath, strpathinzip;
130 CFileItemList itemlist;
132 reffile = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.zip");
133 URIUtils::CreateArchivePath(strzippath, "zip", reffile, "");
134 ASSERT_TRUE(XFILE::CDirectory::GetDirectory(strzippath, itemlist, "",
135 XFILE::DIR_FLAG_NO_FILE_DIRS));
136 strpathinzip = itemlist[0]->GetPath();
138 EXPECT_EQ(0, XFILE::CFile::Stat(strpathinzip, &buffer));
139 EXPECT_TRUE(buffer.st_mode | _S_IFREG);
142 /* Test case to test for graceful handling of corrupted input.
143 * NOTE: The test case is considered a "success" as long as the corrupted
144 * file was successfully generated and the test case runs without a segfault.
146 TEST_F(TestZipFile, CorruptedFile)
150 memset(&buf, 0, sizeof(buf));
151 CStdString reffilepath, strzippath, strpathinzip, str;
152 CFileItemList itemlist;
153 unsigned int size, i;
156 reffilepath = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.zip");
157 ASSERT_TRUE((file = XBMC_CREATECORRUPTEDFILE(reffilepath, ".zip")) != NULL);
158 std::cout << "Reference file generated at '" << XBMC_TEMPFILEPATH(file) << "'" << std::endl;
160 URIUtils::CreateArchivePath(strzippath, "zip", XBMC_TEMPFILEPATH(file), "");
161 if (!XFILE::CDirectory::GetDirectory(strzippath, itemlist, "",
162 XFILE::DIR_FLAG_NO_FILE_DIRS))
164 XBMC_DELETETEMPFILE(file);
168 if (itemlist.IsEmpty())
170 XBMC_DELETETEMPFILE(file);
174 strpathinzip = itemlist[0]->GetPath();
176 if (!file->Open(strpathinzip))
178 XBMC_DELETETEMPFILE(file);
182 std::cout << "file->GetLength(): " <<
183 testing::PrintToString(file->GetLength()) << std::endl;
184 std::cout << "file->Seek(file->GetLength() / 2, SEEK_CUR) return value: " <<
185 testing::PrintToString(file->Seek(file->GetLength() / 2, SEEK_CUR)) << std::endl;
186 std::cout << "file->Seek(0, SEEK_END) return value: " <<
187 testing::PrintToString(file->Seek(0, SEEK_END)) << std::endl;
188 std::cout << "file->Seek(0, SEEK_SET) return value: " <<
189 testing::PrintToString(file->Seek(0, SEEK_SET)) << std::endl;
190 std::cout << "File contents:" << std::endl;
191 while ((size = file->Read(buf, sizeof(buf))) > 0)
193 str = StringUtils::Format(" %08X", count);
194 std::cout << str << " ";
196 for (i = 0; i < size; i++)
198 str = StringUtils::Format("%02X ", buf[i]);
201 while (i++ < sizeof(buf))
204 for (i = 0; i < size; i++)
206 if (buf[i] >= ' ' && buf[i] <= '~')
211 std::cout << "]" << std::endl;
214 XBMC_DELETETEMPFILE(file);