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 <ktst/unit_test.hpp> // TEST_SUITE
26 
27 #include <kapp/args.h> // KAppVersion
28 
29 #include <klib/debug.h>
30 
31 #include <vdb/vdb-priv.h> // VDBManagerMakeReadWithVFSManager
32 #include <vdb/manager.h> // VDBManager
33 #include <vdb/database.h> /* VDatabase */
34 #include <vdb/dependencies.h> /* VDBDependencies */
35 #include <vdb/schema.h> /* VSchema */
36 
37 #include <kfg/kfg-priv.h> /* KConfigMakeLocal */
38 
39 #include <vfs/manager.h> // VFSManager
40 #include <vfs/manager-priv.h> // VFSManagerGetConfig
41 #include <vfs/resolver.h> // VResolverRelease
42 #include <vfs/path.h> // VPathMake
43 
44 #include <kfs/file.h> // KFileRelease
45 
46 #include <sra/sraschema.h>
47 
48 #include <sysalloc.h>
49 
50 #include <stdlib.h> // free
51 
52 #include <stdio.h> // puts
53 
54 #define RELEASE(type, obj) do { rc_t rc2 = type##Release(obj); \
55     if (rc2 != 0 && rc == 0) { rc = rc2; } obj = NULL; } while (false)
56 
57 class DepFixture {
StringRelease(String * self)58     rc_t StringRelease(String *self) {
59         free(self);
60         return 0;
61     }
62 protected:
DepFixture(const char * path)63     DepFixture(const char *path)
64         : mgr(NULL)
65         , vmgr(NULL)
66         , resolver(NULL)
67         , siteless(false)
68     {
69         rc_t rc = 0;
70 
71         KDirectory *wd = NULL;
72         if (KDirectoryNativeDir(&wd)) {
73             FAIL("failed to KDirectoryNativeDir");
74         }
75 
76         const KDirectory *dir = NULL;
77         KConfig *cfg = NULL;
78 
79         if (KDirectoryOpenDirRead(wd, &dir, false, path)) {
80             FAIL("failed to KDirectoryOpenDirRead()");
81         }
82         if (KConfigMakeLocal(&cfg, dir)) {
83             FAIL("failed to KConfigMake()");
84         }
85         RELEASE(KDirectory, dir);
86         if (VFSManagerMakeFromKfg(&vmgr, cfg)) {
87             FAIL("failed to VFSManagerMakeFromKfg()");
88         }
89         if (VFSManagerGetResolver(vmgr, &resolver)) {
90             FAIL("failed to VFSManagerGetResolver");
91         }
92 
93         String *result = NULL;
94         rc = KConfigReadString(cfg, "repository/site/main/tracearc/root",
95             &result);
96         if (rc != 0) {
97             if (rc == SILENT_RC(rcKFG, rcNode, rcOpening, rcPath, rcNotFound)) {
98                 rc = 0;
99                 siteless = true;
100             }
101             else {
102                 FAIL(
103               "failed to KConfigReadString(repository/site/main/tracearc/root)");
104             }
105         }
106         else {
107             assert(result);
108             KPathType t = KDirectoryPathType(wd, result->addr);
109             if (t != kptDir) {
110                 siteless = true;
111             }
112         }
113         RELEASE(String, result);
114 
115         RELEASE(KConfig, cfg);
116         if (VDBManagerMakeReadWithVFSManager(&mgr, NULL, vmgr)) {
117             FAIL("failed to VDBManagerMakeReadWithVFSManager()");
118         }
119 
120         RELEASE(KDirectory, wd);
121     }
122 
~DepFixture(void)123     ~DepFixture(void) {
124         rc_t rc = 0;
125         RELEASE(VResolver, resolver);
126         RELEASE(VDBManager, mgr);
127         RELEASE(VFSManager, vmgr);
128     }
129 public:
130     const VDBManager *mgr;
131     VFSManager *vmgr;
132     VResolver *resolver;
133     bool siteless;
134 };
135 class EmptyFixture : public DepFixture {
136 public:
EmptyFixture()137     EmptyFixture() : DepFixture("kfg/empty") {}
138 };
139 
140 class RefseqFixture : public DepFixture {
141 
142   #if LINUX || BSD
143     #define PATH "kfg/linux"
144   #elif MAC
145     #define PATH "kfg/mac"
146   #elif WINDOWS
147     #define PATH "kfg/win"
148   #endif
149 
150 public:
RefseqFixture()151     RefseqFixture() : DepFixture(PATH) {}
152 };
153 
154 static rc_t argsHandler(int argc, char* argv[]);
155 TEST_SUITE_WITH_ARGS_HANDLER(TestDependenciesSuite, argsHandler);
156 
FIXTURE_TEST_CASE(TestNoDeps,RefseqFixture)157 FIXTURE_TEST_CASE(TestNoDeps, RefseqFixture) {
158     if (siteless) {
159         TEST_MESSAGE("Test skipped because site repository does not exist");
160         return;
161     }
162 
163     rc_t rc = 0;
164 
165     const VDatabase *db = NULL;
166     const char SRR600096[] = "SRR600096";
167     REQUIRE_RC(VDBManagerOpenDBRead(mgr, &db, NULL, SRR600096));
168 //  RELEASE(VDatabase, db);
169 
170     VPath* acc = NULL;
171     REQUIRE_RC(VFSManagerMakePath(vmgr, &acc, SRR600096));
172     const VPath *local = NULL;
173 /*  REQUIRE_RC(VResolverLocal(resolver, acc, &local));
174     RELEASE(VPath, acc); noved to cldn */
175     const String *s = NULL;
176 /*  REQUIRE_RC(VPathMakeString(local, &s));
177     REQUIRE(s && s->addr);
178 
179     REQUIRE_RC(VDBManagerOpenDBRead(mgr, &db, NULL, s->addr)); */
180 
181     const VDBDependencies *dep = NULL;
182 
183     REQUIRE_RC(VDatabaseListDependencies(db, &dep, true));
184     uint32_t count = 1;
185     REQUIRE_RC(VDBDependenciesCount(dep, &count));
186     CHECK_EQUAL(count, (uint32_t)0);
187     RELEASE(VDBDependencies, dep);
188 
189     REQUIRE_RC(VDatabaseListDependencies(db, &dep, false));
190     REQUIRE_RC(VDBDependenciesCount(dep, &count));
191     CHECK_EQUAL(count, (uint32_t)0);
192     RELEASE(VDBDependencies, dep);
193 
194     RELEASE(VDatabase, db);
195     free(const_cast<String*>(s));
196     RELEASE(VPath, local);
197 }
198 
FIXTURE_TEST_CASE(Test1NoDep,EmptyFixture)199 FIXTURE_TEST_CASE(Test1NoDep, EmptyFixture) {
200     rc_t rc = 0;
201 
202     const char SRR619505[] = "SRR619505";
203 
204     VPath *acc = NULL;
205     REQUIRE_RC(VFSManagerMakePath(vmgr, &acc, SRR619505));
206     const VPath *local = NULL;
207     REQUIRE_RC_FAIL(VResolverLocal(resolver, acc, &local));
208     RELEASE(VPath, acc);
209 
210     const VDatabase *db = NULL;
211     REQUIRE_RC(VDBManagerOpenDBRead(mgr, &db, NULL, SRR619505));
212 
213     const VDBDependencies *dep = NULL;
214 
215     //                                             missing
216     REQUIRE_RC(VDatabaseListDependencies(db, &dep, true));
217     uint32_t count = 1;
218     REQUIRE_RC(VDBDependenciesCount(dep, &count));
219     CHECK_EQUAL(count, (uint32_t)1);
220     RELEASE(VDBDependencies, dep);
221 
222     REQUIRE_RC(VDatabaseListDependencies(db, &dep, false));
223     REQUIRE_RC(VDBDependenciesCount(dep, &count));
224     CHECK_EQUAL(count, (uint32_t)1);
225     RELEASE(VDBDependencies, dep);
226 
227     RELEASE(VDatabase, db);
228     RELEASE(VPath, local);
229 }
230 
FIXTURE_TEST_CASE(TestManyNoDep,EmptyFixture)231 FIXTURE_TEST_CASE(TestManyNoDep, EmptyFixture) {
232     rc_t rc = 0;
233 
234     const char SRR543323[] = "SRR543323";
235 
236     VPath* acc = NULL;
237     REQUIRE_RC(VFSManagerMakePath(vmgr, &acc, SRR543323));
238     const VPath *local = NULL;
239     REQUIRE_RC_FAIL(VResolverLocal(resolver, acc, &local));
240     RELEASE(VPath, acc);
241 
242 	struct VSchema *schema;
243 	REQUIRE_RC(VDBManagerMakeSRASchema(mgr, &schema));
244 
245     const VDatabase *db = NULL;
246     REQUIRE_RC(VDBManagerOpenDBRead(mgr, &db, schema, SRR543323));
247     REQUIRE_RC(VSchemaRelease(schema));
248 
249     const VDBDependencies *dep = NULL;
250 
251     //                                             missing
252     REQUIRE_RC(VDatabaseListDependencies(db, &dep, true));
253     uint32_t count = 1;
254     REQUIRE_RC(VDBDependenciesCount(dep, &count));
255     CHECK_EQUAL(count, (uint32_t)21);
256     RELEASE(VDBDependencies, dep);
257 
258     REQUIRE_RC(VDatabaseListDependencies(db, &dep, false));
259     REQUIRE_RC(VDBDependenciesCount(dep, &count));
260     CHECK_EQUAL(count, (uint32_t)21);
261     RELEASE(VDBDependencies, dep);
262 
263     RELEASE(VDatabase, db);
264     RELEASE(VPath, local);
265 }
266 
FIXTURE_TEST_CASE(TestDoubleNoDep,EmptyFixture)267 FIXTURE_TEST_CASE(TestDoubleNoDep, EmptyFixture) {
268     rc_t rc = 0;
269 
270     const char SRR353827[] = "SRR353827";
271 
272     VPath* acc = NULL;
273     REQUIRE_RC(VFSManagerMakePath(vmgr, &acc, SRR353827));
274     const VPath *local = NULL;
275     REQUIRE_RC_FAIL(VResolverLocal(resolver, acc, &local));
276     RELEASE(VPath, acc);
277 
278     const VDatabase *db = NULL;
279     REQUIRE_RC(VDBManagerOpenDBRead(mgr, &db, NULL, SRR353827));
280 
281     const VDBDependencies *dep = NULL;
282 
283     //                                             missing
284     REQUIRE_RC(VDatabaseListDependencies(db, &dep, true));
285     uint32_t count = 1;
286     REQUIRE_RC(VDBDependenciesCount(dep, &count));
287     CHECK_EQUAL(count, (uint32_t)1);
288     RELEASE(VDBDependencies, dep);
289 
290     REQUIRE_RC(VDatabaseListDependencies(db, &dep, false));
291     REQUIRE_RC(VDBDependenciesCount(dep, &count));
292     CHECK_EQUAL(count, (uint32_t)2);
293     RELEASE(VDBDependencies, dep);
294 
295     RELEASE(VDatabase, db);
296     RELEASE(VPath, local);
297 }
298 
FIXTURE_TEST_CASE(TestDoubleYesDep,RefseqFixture)299 FIXTURE_TEST_CASE(TestDoubleYesDep, RefseqFixture) {
300     if (siteless) {
301         TEST_MESSAGE("Test skipped because site repository does not exist");
302         return;
303     }
304 
305     rc_t rc = 0;
306 
307     const VDatabase *db = NULL;
308     const char ACC[] = "SRR618409";
309     REQUIRE_RC(VDBManagerOpenDBRead(mgr, &db, NULL, ACC));
310     RELEASE(VDatabase, db);
311 
312     VPath* acc = NULL;
313     REQUIRE_RC(VFSManagerMakePath(vmgr, &acc, ACC));
314     const VPath *local = NULL;
315     REQUIRE_RC(VResolverRemote(resolver, eProtocolHttps, acc, &local));
316     RELEASE(VPath, acc);
317     const String *s = NULL;
318     REQUIRE_RC(VPathMakeString(local, &s));
319     REQUIRE(s && s->addr);
320 
321     REQUIRE_RC(VDBManagerOpenDBReadVPath(mgr, &db, NULL, local));
322 
323     const VDBDependencies *dep = NULL;
324 
325     //                                             missing
326     REQUIRE_RC(VDatabaseListDependencies(db, &dep, true));
327     uint32_t count = 1;
328     REQUIRE_RC(VDBDependenciesCount(dep, &count));
329     CHECK_EQUAL(count, (uint32_t)0);
330     RELEASE(VDBDependencies, dep);
331 
332     REQUIRE_RC(VDatabaseListDependencies(db, &dep, false));
333     REQUIRE_RC(VDBDependenciesCount(dep, &count));
334     CHECK_EQUAL(count, (uint32_t)1);
335     RELEASE(VDBDependencies, dep);
336 
337     RELEASE(VDatabase, db);
338     free(const_cast<String*>(s));
339     RELEASE(VPath, local);
340 }
341 
FIXTURE_TEST_CASE(TestManyYesDep,RefseqFixture)342 FIXTURE_TEST_CASE(TestManyYesDep, RefseqFixture) {
343     if (siteless) {
344         TEST_MESSAGE("Test skipped because site repository does not exist");
345         return;
346     }
347 
348     rc_t rc = 0;
349 
350     const VDatabase *db = NULL;
351     const char SRR543323[] = "SRR543323";
352     REQUIRE_RC(VDBManagerOpenDBRead(mgr, &db, NULL, SRR543323));
353 //  RELEASE(VDatabase, db);
354 
355     VPath* acc = NULL;
356     REQUIRE_RC(VFSManagerMakePath(vmgr, &acc, SRR543323));
357     const VPath *local = NULL;
358 //  REQUIRE_RC(VResolverLocal(resolver, acc, &local)); noved to cldn
359     RELEASE(VPath, acc);
360     const String *s = NULL;
361 /*  REQUIRE_RC(VPathMakeString(local, &s));
362     REQUIRE(s && s->addr);
363 
364     REQUIRE_RC(VDBManagerOpenDBRead(mgr, &db, NULL, s->addr)); */
365 
366     const VDBDependencies *dep = NULL;
367 
368     //                                             missing
369     REQUIRE_RC(VDatabaseListDependencies(db, &dep, true));
370     uint32_t count = 1;
371     REQUIRE_RC(VDBDependenciesCount(dep, &count));
372     CHECK_EQUAL(count, (uint32_t)0);
373     RELEASE(VDBDependencies, dep);
374 
375     REQUIRE_RC(VDatabaseListDependencies(db, &dep, false));
376     REQUIRE_RC(VDBDependenciesCount(dep, &count));
377     CHECK_EQUAL(count, (uint32_t)21);
378     RELEASE(VDBDependencies, dep);
379 
380     RELEASE(VDatabase, db);
381     free(const_cast<String*>(s));
382     RELEASE(VPath, local);
383 }
384 
FIXTURE_TEST_CASE(Test1YesDep,RefseqFixture)385 FIXTURE_TEST_CASE(Test1YesDep, RefseqFixture) {
386     if (siteless) {
387         TEST_MESSAGE("Test skipped because site repository does not exist");
388         return;
389     }
390 
391     rc_t rc = 0;
392 
393     const VDatabase *db = NULL;
394     const char SRR619505[] = "SRR619505";
395     REQUIRE_RC(VDBManagerOpenDBRead(mgr, &db, NULL, SRR619505));
396     RELEASE(VDatabase, db);
397 
398     VPath* acc = NULL;
399     REQUIRE_RC(VFSManagerMakePath(vmgr, &acc, SRR619505));
400     const VPath *local = NULL;
401 
402 //  REQUIRE_RC(VResolverLocal(resolver, acc, &local));
403     REQUIRE_RC(VResolverRemote(resolver, eProtocolHttps, acc, &local));
404 
405 
406     RELEASE(VPath, acc);
407     const String *s = NULL;
408     REQUIRE_RC(VPathMakeString(local, &s));
409     REQUIRE(s && s->addr);
410 
411     REQUIRE_RC(VDBManagerOpenDBReadVPath(mgr, &db, NULL, local));
412 
413     const VDBDependencies *dep = NULL;
414 
415     //                                             missing
416     REQUIRE_RC(VDatabaseListDependencies(db, &dep, true));
417     uint32_t count = 1;
418     REQUIRE_RC(VDBDependenciesCount(dep, &count));
419     CHECK_EQUAL(count, (uint32_t)0);
420     RELEASE(VDBDependencies, dep);
421 
422     REQUIRE_RC(VDatabaseListDependencies(db, &dep, false));
423     REQUIRE_RC(VDBDependenciesCount(dep, &count));
424     CHECK_EQUAL(count, (uint32_t)1);
425     RELEASE(VDBDependencies, dep);
426 
427     RELEASE(VDatabase, db);
428     free(const_cast<String*>(s));
429     RELEASE(VPath, local);
430 }
431 
FIXTURE_TEST_CASE(Test1LocalDep,EmptyFixture)432 FIXTURE_TEST_CASE(Test1LocalDep, EmptyFixture) {
433     rc_t rc = 0;
434 
435     const char SRR413283[] = "SRR413283";
436 
437     VPath* acc = NULL;
438     REQUIRE_RC(VFSManagerMakePath(vmgr, &acc, SRR413283));
439     const VPath *local = NULL;
440     REQUIRE_RC_FAIL(VResolverLocal(resolver, acc, &local));
441     RELEASE(VPath, acc);
442 
443     const VDatabase *db = NULL;
444     REQUIRE_RC(VDBManagerOpenDBRead(mgr, &db, NULL, SRR413283));
445 
446     const VDBDependencies *dep = NULL;
447 
448     //                                             missing
449     REQUIRE_RC(VDatabaseListDependencies(db, &dep, true));
450     uint32_t count = 1;
451     REQUIRE_RC(VDBDependenciesCount(dep, &count));
452     CHECK_EQUAL(count, (uint32_t)0);
453     RELEASE(VDBDependencies, dep);
454 
455     REQUIRE_RC(VDatabaseListDependencies(db, &dep, false));
456     REQUIRE_RC(VDBDependenciesCount(dep, &count));
457     CHECK_EQUAL(count, (uint32_t)1);
458     RELEASE(VDBDependencies, dep);
459 
460     RELEASE(VDatabase, db);
461     RELEASE(VPath, local);
462 }
463 
argsHandler(int argc,char * argv[])464 static rc_t argsHandler(int argc, char* argv[]) {
465     Args* args = NULL;
466     rc_t rc = ArgsMakeAndHandle(&args, argc, argv, 0, NULL, 0);
467     ArgsWhack(args);
468     return rc;
469 }
470 
471 extern "C" {
KAppVersion(void)472     ver_t CC KAppVersion(void) { return 0x1000000; }
473     const char UsageDefaultName[] = "test-dependencies";
UsageSummary(const char * progname)474     rc_t CC UsageSummary(const char *progname) { return 0; }
Usage(const Args * args)475     rc_t CC Usage(const Args *args) { return 0; }
KMain(int argc,char * argv[])476     rc_t CC KMain(int argc, char *argv[]) {
477         KConfigDisableUserSettings();
478 if(
479 0)      assert(!KDbgSetString("VFS"));
480         return TestDependenciesSuite(argc, argv);
481     }
482 }
483