20c5c7c13e6422a0bb08b59eedb3441d14fcd244
[vuplus_xbmc] / xbmc / filesystem / test / TestZipFile.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #include "filesystem/Directory.h"
22 #include "filesystem/File.h"
23 #include "utils/URIUtils.h"
24 #include "FileItem.h"
25 #include "settings/Settings.h"
26 #include "test/TestUtils.h"
27
28 #include <errno.h>
29
30 #include "gtest/gtest.h"
31
32 class TestZipFile : public testing::Test
33 {
34 protected:
35   TestZipFile()
36   {
37     /* Add default settings for locale.
38      * Settings here are taken from CGUISettings::Initialize()
39      */
40     /* TODO
41     CSettingsCategory *loc = CSettings::Get().AddCategory(7, "locale", 14090);
42     CSettings::Get().AddString(loc, "locale.language",248,"english",
43                             SPIN_CONTROL_TEXT);
44     CSettings::Get().AddString(loc, "locale.country", 20026, "USA",
45                             SPIN_CONTROL_TEXT);
46     CSettings::Get().AddString(loc, "locale.charset", 14091, "DEFAULT",
47                             SPIN_CONTROL_TEXT); // charset is set by the
48                                                 // language file
49     */
50   }
51
52   ~TestZipFile()
53   {
54     CSettings::Get().Unload();
55   }
56 };
57
58 TEST_F(TestZipFile, Read)
59 {
60   XFILE::CFile file;
61   char buf[20];
62   memset(&buf, 0, sizeof(buf));
63   CStdString reffile, strzippath, strpathinzip;
64   CFileItemList itemlist;
65
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)));
75   file.Flush();
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)));
84   file.Flush();
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)));
90   file.Flush();
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)));
96   file.Flush();
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)));
103   file.Flush();
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));
108   file.Close();
109 }
110
111 TEST_F(TestZipFile, Exists)
112 {
113   CStdString reffile, strzippath, strpathinzip;
114   CFileItemList itemlist;
115
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();
121
122   EXPECT_TRUE(XFILE::CFile::Exists(strpathinzip));
123 }
124
125 TEST_F(TestZipFile, Stat)
126 {
127   struct __stat64 buffer;
128   CStdString reffile, strzippath, strpathinzip;
129   CFileItemList itemlist;
130
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();
136
137   EXPECT_EQ(0, XFILE::CFile::Stat(strpathinzip, &buffer));
138   EXPECT_TRUE(buffer.st_mode | _S_IFREG);
139 }
140
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.
144  */
145 TEST_F(TestZipFile, CorruptedFile)
146 {
147   XFILE::CFile *file;
148   char buf[16];
149   memset(&buf, 0, sizeof(buf));
150   CStdString reffilepath, strzippath, strpathinzip, str;
151   CFileItemList itemlist;
152   unsigned int size, i;
153   int64_t count = 0;
154
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;
158
159   URIUtils::CreateArchivePath(strzippath, "zip", XBMC_TEMPFILEPATH(file), "");
160   if (!XFILE::CDirectory::GetDirectory(strzippath, itemlist, "",
161                                        XFILE::DIR_FLAG_NO_FILE_DIRS))
162   {
163     XBMC_DELETETEMPFILE(file);
164     SUCCEED();
165     return;
166   }
167   if (itemlist.IsEmpty())
168   {
169     XBMC_DELETETEMPFILE(file);
170     SUCCEED();
171     return;
172   }
173   strpathinzip = itemlist[0]->GetPath();
174
175   if (!file->Open(strpathinzip))
176   {
177     XBMC_DELETETEMPFILE(file);
178     SUCCEED();
179     return;
180   }
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)
191   {
192     str.Format("  %08X", count);
193     std::cout << str << "  ";
194     count += size;
195     for (i = 0; i < size; i++)
196     {
197       str.Format("%02X ", buf[i]);
198       std::cout << str;
199     }
200     while (i++ < sizeof(buf))
201       std::cout << "   ";
202     std::cout << " [";
203     for (i = 0; i < size; i++)
204     {
205       if (buf[i] >= ' ' && buf[i] <= '~')
206         std::cout << buf[i];
207       else
208         std::cout << ".";
209     }
210     std::cout << "]" << std::endl;
211   }
212   file->Close();
213   XBMC_DELETETEMPFILE(file);
214 }