1 /*===========================================================================
2 *
3 *                            PUBLIC DOMAIN NOTICE
4 *               National Center for Biotechnology Information
5 *
6 *  This software/database is a "United States Government Work" under the
7 *  terms of the United States Copyright Act.  It was written as part of
8 *  the author's official duties as a United States Government employee and
9 *  thus cannot be copyrighted.  This software/database is freely available
10 *  to the public for use. The National Library of Medicine and the U.S.
11 *  Government have not placed any restriction on its use or reproduction.
12 *
13 *  Although all reasonable efforts have been taken to ensure the accuracy
14 *  and reliability of the software and data, the NLM and the U.S.
15 *  Government do not and cannot warrant the performance or results that
16 *  may be obtained by using this software or data. The NLM and the U.S.
17 *  Government disclaim all warranties, express or implied, including
18 *  warranties of performance, merchantability or fitness for any particular
19 *  purpose.
20 *
21 *  Please cite the author in any work or product based on this material.
22 *
23 * ===========================================================================
24 *
25 */
26 
27 /**
28 * Unit tests for Kfs interface
29 */
30 
31 #include <cstring>
32 
33 #include <ktst/unit_test.hpp>
34 #include <kfs/mmap.h>
35 #include <kfs/directory.h>
36 #include <kfs/impl.h>
37 #include <kfs/tar.h>
38 
39 #include <kfs/ffext.h>
40 #include <kfs/ffmagic.h>
41 
42 #define class clss
43 #include <kfs/fileformat.h>
44 #undef class
45 
46 #include "../../libs/kfs/toc-priv.h"
47 
48 using namespace std;
49 
50 TEST_SUITE(KfsTestSuite);
51 
TEST_CASE(KMMapMakeRead_and_KMMapRelease)52 TEST_CASE(KMMapMakeRead_and_KMMapRelease)
53 {   // create a temporary file, open it with KMMapMakeRead, close KMMap, try to delete
54 
55     KDirectory *wd;
56     REQUIRE_RC(KDirectoryNativeDir ( & wd ));
57 
58     const char* fileName="test.file";
59 
60     {   // create temp file, close
61         KFile* file;
62         const char* contents="contents";
63         REQUIRE_RC(KDirectoryCreateFile(wd, &file, true, 0664, kcmInit, fileName));
64         size_t num_writ=0;
65         REQUIRE_RC(KFileWrite(file, 0, contents, strlen(contents), &num_writ));
66         REQUIRE_RC(KFileRelease(file));
67     }
68 
69     {   // open, memory-map, close
70         const KFile* file;
71         REQUIRE_RC(KDirectoryOpenFileRead(wd, &file, fileName));
72         const KMMap * mm;
73         REQUIRE_RC(KMMapMakeRead(&mm, file));
74         REQUIRE_RC(KMMapRelease(mm));
75 
76         REQUIRE_RC(KFileRelease(file));
77     }
78 
79     // now, remove the file
80     // on Windows: used to return ACCESS_DENIED, not removed file
81     // (cause: no call to UnmapViewOfFile in libs\kfs\win\KMapUnmap)
82     REQUIRE_RC(KDirectoryRemove(wd, false, fileName));
83 
84     REQUIRE_RC(KDirectoryRelease ( wd ));
85 }
86 
87 #ifdef HAVE_KFF
88 
TEST_CASE(ExtFileFormat)89 TEST_CASE(ExtFileFormat)
90 {
91     struct KFileFormat* pft;
92     const char format[] = {
93         "ext1\tTestFormat1\n"
94         "ext2\tTestFormat2\n"
95     };
96     const char typeAndClass[] = {
97         "TestFormat1\tTestClass1\n"
98         "TestFormat2\tTestClass2\n"
99     };
100     REQUIRE_RC(KExtFileFormatMake(&pft, format, sizeof(format) - 1, typeAndClass, sizeof(typeAndClass) - 1));
101 
102     KFileFormatType type;
103     KFileFormatClass clss;
104     char descr[1024];
105     size_t length;
106     REQUIRE_RC(KFileFormatGetTypePath(pft,
107                                       NULL, // ignored
108                                       "qq.ext2",
109                                       &type,
110                                       &clss,
111                                       descr,
112                                       sizeof(descr),
113                                       &length));
114     REQUIRE_EQ(type, 2);
115     REQUIRE_EQ(clss, 2);
116     REQUIRE_EQ(string(descr, length), string("TestFormat2"));
117 
118     REQUIRE_RC(KFileFormatGetClassDescr(pft, clss, descr, sizeof (descr)));
119     REQUIRE_EQ(string(descr), string("TestClass2"));
120     REQUIRE_EQ(length, string("TestClass2").length()+1);
121     REQUIRE_RC(KFileFormatRelease(pft));
122 }
123 
TEST_CASE(MagicFileFormat)124 TEST_CASE(MagicFileFormat)
125 {
126     struct KFileFormat* pft;
127     const char magic[] =
128     {
129         "Generic Format for Sequence Data (SRF)\tSequenceReadFormat\n"
130         "GNU tar archive\tTapeArchive\n"
131     };
132     const char typeAndClass[] = {
133         "SequenceReadFormat\tRead\n"
134         "TapeArchive\tArchive\n"
135     };
136     REQUIRE_RC(KMagicFileFormatMake (&pft, "/usr/share/misc/magic", magic, sizeof(magic) - 1, typeAndClass, sizeof(typeAndClass) - 1));
137     REQUIRE_RC(KFileFormatRelease(pft));
138 }
139 
140 #endif
141 
TEST_CASE(Tar_Parse)142 TEST_CASE(Tar_Parse)
143 {
144     KDirectory *dir;
145     REQUIRE_RC(KDirectoryNativeDir(&dir));
146 
147     const KDirectory *tarDir;
148     REQUIRE_RC(KDirectoryOpenTarArchiveRead(dir, &tarDir, false, "test.tar"));
149 
150     struct KNamelist *list;
151     REQUIRE_RC(KDirectoryList(tarDir, &list, NULL, NULL, NULL));
152 
153     uint32_t count;
154     REQUIRE_RC(KNamelistCount(list, &count));
155     REQUIRE_EQ(count, (uint32_t)2);
156 
157     const char* name;
158     REQUIRE_RC(KNamelistGet(list, 0, &name));
159     REQUIRE_EQ(string(name), string("Makefile"));
160     REQUIRE_RC(KNamelistGet(list, 1, &name));
161     REQUIRE_EQ(string(name), string("kfstest.cpp"));
162 
163     REQUIRE_RC(KNamelistRelease(list));
164     REQUIRE_RC(KDirectoryRelease(tarDir));
165     REQUIRE_RC(KDirectoryRelease(dir));
166 }
167 
168 //////////////////////////////////////////// Main
169 extern "C"
170 {
171 
172 #include <kapp/args.h>
173 #include <kfg/config.h>
174 
KAppVersion(void)175 ver_t CC KAppVersion ( void )
176 {
177     return 0x1000000;
178 }
UsageSummary(const char * progname)179 rc_t CC UsageSummary (const char * progname)
180 {
181     return 0;
182 }
183 
Usage(const Args * args)184 rc_t CC Usage ( const Args * args )
185 {
186     return 0;
187 }
188 
189 const char UsageDefaultName[] = "test-kfs";
190 
KMain(int argc,char * argv[])191 rc_t CC KMain ( int argc, char *argv [] )
192 {
193     KConfigDisableUserSettings();
194     rc_t rc=KfsTestSuite(argc, argv);
195     return rc;
196 }
197 
198 }
199