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 KMD5File
29 */
30 
31 #include <cstring>
32 #include <cstdio>
33 
34 #include <ktst/unit_test.hpp>
35 
36 #include <kfs/directory.h>
37 #include <kfs/impl.h>
38 #include <kfs/md5.h>
39 
40 
41 #define class clss
42 #include <kfs/fileformat.h>
43 #undef class
44 
45 using namespace std;
46 
47 TEST_SUITE(KMD5FileTestSuite);
48 
TEST_CASE(KMD5WriteFileTruncateAfterChange)49 TEST_CASE(KMD5WriteFileTruncateAfterChange)
50 {
51     KDirectory *wd;
52     REQUIRE_RC(KDirectoryNativeDir ( & wd ));
53 
54     const char* filename = "md5test_trunc.file";
55     const char* md5sums_filename = "md5test_sums_trunc.file";
56     const char* contents = "contents";
57 
58     {   // create temp file, close
59         KFile* file;
60         REQUIRE_RC( KDirectoryCreateFile(wd, &file, true, 0664, kcmInit, filename) );
61 
62         size_t num_writ;
63         REQUIRE_RC( KFileWrite(file, 0, contents, strlen(contents), &num_writ) );
64         assert(num_writ == strlen(contents));
65 
66         uint64_t file_size;
67         REQUIRE_RC( KFileSize(file, &file_size) );
68         REQUIRE( file_size == strlen(contents) );
69 
70         REQUIRE_RC( KFileRelease(file) );
71     }
72 
73     {
74         KFile * file, * md5sums_file;
75         KMD5File * md5_file_obj;
76         KMD5SumFmt * md5sums;
77 
78         REQUIRE_RC( KDirectoryCreateFile(wd, &md5sums_file, true, 0664, kcmInit, md5sums_filename) );
79         REQUIRE_RC( KMD5SumFmtMakeUpdate(&md5sums, md5sums_file) );
80 
81         REQUIRE_RC( KDirectoryOpenFileWrite(wd, &file, true, filename) );
82 
83         REQUIRE_RC( KMD5FileMakeWrite(&md5_file_obj, file, md5sums, filename) );
84         file = KMD5FileToKFile(md5_file_obj);
85 
86         size_t num_writ;
87         REQUIRE_RC( KFileWrite(file, 0, contents, strlen(contents) - 2, &num_writ) );
88         assert(num_writ == strlen(contents) - 2);
89 
90         REQUIRE_RC( KFileRelease(file) );
91         REQUIRE_RC( KMD5SumFmtRelease(md5sums) );
92     }
93 
94     {
95         const KFile * file;
96         REQUIRE_RC( KDirectoryOpenFileRead(wd, &file, filename) );
97 
98         uint64_t file_size;
99         REQUIRE_RC( KFileSize(file, &file_size) );
100         REQUIRE( file_size == strlen(contents) - 2 );
101 
102         REQUIRE_RC( KFileRelease(file) );
103     }
104 
105     REQUIRE_RC( KDirectoryRemove(wd, false, filename) );
106     REQUIRE_RC( KDirectoryRemove(wd, false, md5sums_filename) );
107 
108     REQUIRE_RC( KDirectoryRelease ( wd ) );
109 }
110 
TEST_CASE(KMD5WriteFileNotTruncateWithoutChange)111 TEST_CASE(KMD5WriteFileNotTruncateWithoutChange)
112 {
113     KDirectory *wd;
114     REQUIRE_RC(KDirectoryNativeDir ( & wd ));
115 
116     const char* filename = "md5test_notrunc.file";
117     const char* md5sums_filename = "md5test_sums_notrunc.file";
118     const char* contents = "contents";
119 
120     {   // create temp file, close
121         KFile* file;
122         REQUIRE_RC( KDirectoryCreateFile(wd, &file, true, 0664, kcmInit, filename) );
123 
124         size_t num_writ;
125         REQUIRE_RC( KFileWrite(file, 0, contents, strlen(contents), &num_writ) );
126         assert(num_writ == strlen(contents));
127 
128         uint64_t file_size;
129         REQUIRE_RC( KFileSize(file, &file_size) );
130         REQUIRE( file_size == strlen(contents) );
131 
132         REQUIRE_RC( KFileRelease(file) );
133     }
134 
135     {
136         KFile * file, * md5sums_file;
137         KMD5File * md5_file_obj;
138         KMD5SumFmt * md5sums;
139 
140         REQUIRE_RC( KDirectoryCreateFile(wd, &md5sums_file, true, 0664, kcmInit, md5sums_filename) );
141         REQUIRE_RC( KMD5SumFmtMakeUpdate(&md5sums, md5sums_file) );
142 
143         REQUIRE_RC( KDirectoryOpenFileWrite(wd, &file, true, filename) );
144 
145         REQUIRE_RC( KMD5FileMakeWrite(&md5_file_obj, file, md5sums, filename) );
146         file = KMD5FileToKFile(md5_file_obj);
147 
148         REQUIRE_RC( KFileRelease(file) );
149         REQUIRE_RC( KMD5SumFmtRelease(md5sums) );
150     }
151 
152     {
153         const KFile * file;
154         REQUIRE_RC( KDirectoryOpenFileRead(wd, &file, filename) );
155 
156         uint64_t file_size;
157         REQUIRE_RC( KFileSize(file, &file_size) );
158         REQUIRE( file_size == strlen(contents) );
159 
160         REQUIRE_RC( KFileRelease(file) );
161     }
162 
163     REQUIRE_RC( KDirectoryRemove(wd, false, filename) );
164     REQUIRE_RC( KDirectoryRemove(wd, false, md5sums_filename) );
165 
166     REQUIRE_RC( KDirectoryRelease ( wd ) );
167 }
168 
169 //////////////////////////////////////////// Main
170 extern "C"
171 {
172 
173 #include <kapp/args.h>
174 #include <kfg/config.h>
175 
KAppVersion(void)176 ver_t CC KAppVersion ( void )
177 {
178     return 0x1000000;
179 }
UsageSummary(const char * progname)180 rc_t CC UsageSummary (const char * progname)
181 {
182     return 0;
183 }
184 
Usage(const Args * args)185 rc_t CC Usage ( const Args * args )
186 {
187     return 0;
188 }
189 
190 const char UsageDefaultName[] = "test-kfs-md5";
191 
KMain(int argc,char * argv[])192 rc_t CC KMain ( int argc, char *argv [] )
193 {
194     KConfigDisableUserSettings();
195     rc_t rc=KMD5FileTestSuite(argc, argv);
196     return rc;
197 }
198 
199 }
200