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 #include "../libs/vfs/SraDesc.c"
26 
27 #include <kfg/config.h> /* KConfigDisableUserSettings */
28 
29 #include <kfs/directory.h> /* KDirectoryRelease */
30 #include <kfs/file.h> /* KFileRelease */
31 
32 #include <klib/printf.h> /* string_printf */
33 
34 #include <ktst/unit_test.hpp> /* KMain */
35 
36 TEST_SUITE(SraDescTestSuite);
37 
38 #define ALL
39 #define DIR "tmp"
40 
41 struct SraDescTestFixture {
42     KDirectory * dir = NULL;
43     KFile * f = NULL;
44 
SraDescTestFixtureSraDescTestFixture45     SraDescTestFixture() : dir(0), f(0) {
46         rc_t rc = KDirectoryNativeDir(&dir); if (rc != 0) throw rc;
47         rc = KDirectoryRemove(dir, true, "%s", DIR); if (rc != 0) throw rc;
48     }
49 
~SraDescTestFixtureSraDescTestFixture50     ~SraDescTestFixture() { Fini(); }
51 
FiniSraDescTestFixture52     rc_t Fini() {
53         rc_t rc = KFileRelease(f); f = 0;
54 
55         rc_t r2 = KDirectoryRemove(dir, true, "%s", DIR);
56         if (r2 != 0 && rc == 0)
57             rc = r2;
58 
59         r2 = KDirectoryRelease(dir); dir = 0;
60         if (r2 != 0 && rc == 0)
61             rc = r2;
62 
63         return rc;
64     }
65 };
66 
67 #ifdef ALL
FIXTURE_TEST_CASE(BinFileTest,SraDescTestFixture)68 FIXTURE_TEST_CASE(BinFileTest, SraDescTestFixture) { // load from binary file
69     REQUIRE_RC(KDirectoryCreateFile(dir, &f, false,
70         0664, kcmInit | kcmParents, "%s/sdb", DIR));
71 
72     char b[512] = "";
73     size_t num_writ = 0;
74     REQUIRE_RC(string_printf(b, sizeof b, &num_writ, "NCBIRrDs"));
75     b[num_writ++] = 255;
76 
77     union {
78         uint64_t u;
79         uint8_t b[8];
80     } n;
81     n.u = 1234567890;
82 
83     for (int i = 0; i < 8; ++i)
84         b[num_writ++] = n.b[i];
85     for (int i = 0; i < 8; ++i)
86         b[num_writ++] = n.b[i];
87 
88     REQUIRE_RC(KFileWrite(f, 0, b, num_writ, NULL));
89 
90     SraDesc sd;
91     REQUIRE(memset(&sd, 0, sizeof sd));
92     sd._sdType = eBin;
93 
94     REQUIRE_RC(SraDescLoadPath(&sd, dir, "tmp/sdb"));
95 
96     SraDesc e;
97     SraDescSet(&e, eQualDefault, n.u, eBin);
98 
99     REQUIRE_RC(SraDescCmp(&sd, &e));
100 
101     REQUIRE_RC(Fini());
102 }
103 #endif
104 
105 #ifdef ALL
FIXTURE_TEST_CASE(TextFileTest,SraDescTestFixture)106 FIXTURE_TEST_CASE(TextFileTest, SraDescTestFixture) { // load from textual file
107     REQUIRE_RC(KDirectoryCreateFile(dir, &f, false,
108         0664, kcmInit | kcmParents, "%s/sdt", DIR));
109 
110     char b[512] = "";
111     size_t num_writ = 0;
112     REQUIRE_RC(string_printf(b, sizeof b, &num_writ,
113         "NCBIRrDs\n"
114         "255\n"
115         "1234567890\n"
116         "0123456789abcdef0123456789abcdef\n"));
117     REQUIRE_RC(KFileWrite(f, 0, b, num_writ, NULL));
118 
119     SraDesc sd;
120     REQUIRE(memset(&sd, 0, sizeof sd));
121     sd._sdType = eTextual;
122     REQUIRE_RC(SraDescLoadPath(&sd, dir, "tmp/sdt"));
123 
124     SraDesc e;
125     SraDescSet(&e, eQualDefault, 1234567890, eTextual);
126 
127     REQUIRE_RC(SraDescCmp(&sd, &e));
128 
129     REQUIRE_RC(Fini());
130 }
131 #endif
132 
133 #ifdef ALL
134 // save textual; convert to bin; convert to text
FIXTURE_TEST_CASE(ConvertTest,SraDescTestFixture)135 FIXTURE_TEST_CASE(ConvertTest, SraDescTestFixture) {
136     REQUIRE_RC(KDirectoryCreateFile(dir, &f, false,
137         0664, kcmInit | kcmParents, "%s/sd.dst", DIR));
138 
139     char b[512] = "";
140     size_t num_writ = 0;
141     REQUIRE_RC(string_printf(b, sizeof b, &num_writ,
142         "NCBIRrDs\n"
143         "255\n"
144         "1234567890\n"
145         "0123456789abcdef0123456789abcdef\n"));
146     REQUIRE_RC(KFileWrite(f, 0, b, num_writ, NULL));
147 
148     SraDesc sd;
149     REQUIRE(memset(&sd, 0, sizeof sd));
150     sd._sdType = eTextual;
151     REQUIRE_RC(SraDescLoadPath(&sd, dir, "tmp/sd.dst"));
152 
153     SraDesc e;
154     SraDescSet(&e, eQualDefault, 1234567890, eTextual);
155 
156     REQUIRE_RC(SraDescCmp(&sd, &e));
157 
158     bool recognized = false;
159     REQUIRE_RC(SraDescConvert(dir, "tmp/sd.dst", &recognized));
160     REQUIRE(recognized);
161 
162     sd._sdType = e._sdType = eBin;
163     REQUIRE_RC(SraDescLoadPath(&sd, dir, "tmp/sd.dsc"));
164 
165     REQUIRE_RC(SraDescCmp(&sd, &e));
166 
167     REQUIRE_RC(Fini());
168 }
169 #endif
170 
171 #ifdef ALL
FIXTURE_TEST_CASE(LoalFullQualityTest,SraDescTestFixture)172 FIXTURE_TEST_CASE(LoalFullQualityTest, SraDescTestFixture) {
173     const char sra[] = "0.sra";
174     char path[PATH_MAX] = "";
175     REQUIRE_RC(KDirectoryResolvePath(dir, true, path, sizeof path,
176         "%s/%s", DIR, sra));
177     REQUIRE_RC(KDirectoryCreateFile(dir, &f, false,
178         0664, kcmInit | kcmParents, "%s", path));
179 
180     char b[512] = "";
181     REQUIRE_RC(KFileWrite(f, 0, b, sizeof b, NULL));
182     REQUIRE_RC(KFileRelease(f)); f = 0;
183 
184     SraDesc sd;
185     VQuality q(eQualFull);
186     SraDescSet(&sd, q, sizeof b, eBin);
187 
188     REQUIRE_RC(KDirectoryCreateFile(dir, &f, false,
189         0664, kcmInit | kcmParents, "%s.dsc", path));
190 
191     REQUIRE_RC(SraDescSave(&sd, f));
192 
193     VPath * p = NULL;
194     REQUIRE_RC(VPathMake(&p, path));
195     REQUIRE_NOT_NULL(p);
196     REQUIRE_EQ(p->quality, (uint32_t)eQualLast);
197 
198     REQUIRE_RC(VPathLoadQuality(p));
199     REQUIRE_EQ(p->quality, q);
200 
201     REQUIRE_RC(VPathRelease(p));
202 
203     REQUIRE_RC(Fini());
204 }
205 #endif
206 
207 #ifdef ALL
FIXTURE_TEST_CASE(LoalObsoleteQualityTest,SraDescTestFixture)208 FIXTURE_TEST_CASE(LoalObsoleteQualityTest, SraDescTestFixture) {
209     const char sra[] = "0.sra";
210     char path[PATH_MAX] = "";
211     REQUIRE_RC(KDirectoryResolvePath(dir, true, path, sizeof path,
212         "%s/%s", DIR, sra));
213     REQUIRE_RC(KDirectoryCreateFile(dir, &f, false,
214         0664, kcmInit | kcmParents, "%s", path));
215 
216     char b[512] = "";
217     REQUIRE_RC(KFileWrite(f, 0, b, sizeof b, NULL));
218     REQUIRE_RC(KFileRelease(f)); f = 0;
219 
220     SraDesc sd;
221     VQuality q(eQualFull);
222     SraDescSet(&sd, q, sizeof b - 1, eBin);
223 
224     REQUIRE_RC(KDirectoryCreateFile(dir, &f, false,
225         0664, kcmInit | kcmParents, "%s.dsc", path));
226     REQUIRE_RC(SraDescSave(&sd, f));
227 
228     VPath * p = NULL;
229     REQUIRE_RC(VPathMake(&p, path));
230     REQUIRE_NOT_NULL(p);
231     REQUIRE_EQ(p->quality, (uint32_t)eQualLast);
232 
233     REQUIRE_RC(VPathLoadQuality(p));
234     REQUIRE_EQ(p->quality, (uint32_t)eQualLast);
235 
236     REQUIRE_RC(VPathRelease(p));
237 
238     REQUIRE_RC(Fini());
239 }
240 #endif
241 
242 extern "C" {
KAppVersion(void)243     ver_t CC KAppVersion(void) { return 0; }
KMain(int argc,char * argv[])244     rc_t CC KMain(int argc, char * argv[]) {
245         KConfigDisableUserSettings();
246         return SraDescTestSuite(argc, argv);
247     }
248 }
249 
250