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