1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF.  The full HDF copyright notice, including       *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /* $Id$ */
15 
16 /*
17    * Hopen
18    ** Create a file.
19    ** Open an existing file.
20    *** Normally.
21    *** Read-only file with DFACC_WRITE.
22    ** Open non-existent file.
23    *** With DFACC_READ.
24    *** With DFACC_WRITE.
25    ** Create an illegal file.
26    ** Open the same file twice.
27    *** First with DFACC_WRITE then with DFACC_READ.
28    *** First with DFACC_WRITE then with DFACC_WRITE.
29    *** First with DFACC_READ and then with DFACC_WRITE.
30    *** First with DFACC_<any> and then with DFACC_CREATE.
31    ** Open more files than there is slots.
32 
33    * Hclose
34    ** Close a proper file.
35    ** Close with an illegal file id.
36    *** Random file id.
37    *** Correct tag but bad slot.
38 
39    * Hstartread
40    ** Normal.
41    ** With illegal file id.
42    ** With illegal tag/ref.
43    ** With wildcard.
44    ** Open more access elements than there is space.
45 
46  */
47 
48 #include "tproto.h"
49 #define TESTFILE_NAME "t.hdf"
50 #define BUF_SIZE        4096
51 
52 static uint8  outbuf[BUF_SIZE],  inbuf[BUF_SIZE];
53 
54 void
test_hfile(void)55 test_hfile(void)
56 {
57     int32       fid, fid1;
58     int32       aid1, aid2;
59     int32       fileid, length, offset, posn;
60     uint16      tag, ref;
61     int16       acc_mode, special;
62     int32       ret;
63     int         i;
64     intn        errors = 0;
65     intn        ret_bool;
66 
67     for (i = 0; i < BUF_SIZE; i++)
68         outbuf[i] = (char) (i % 256);
69 
70     MESSAGE(5, printf("Creating a file %s\n", TESTFILE_NAME);
71         );
72     fid = Hopen(TESTFILE_NAME, DFACC_CREATE, 0);
73     CHECK_VOID(fid, FAIL, "Hopen");
74 
75     ret_bool = (intn) Hishdf(TESTFILE_NAME);
76     CHECK_VOID(ret_bool, FALSE, "Hishdf");
77 
78     ret = (int32)Hnewref(fid);
79     CHECK_VOID(ret, FAIL, "Hnewref");
80 
81     MESSAGE(5, printf("Reading / Writing to file\n");
82         );
83     ret = Hputelement(fid, (uint16) 100, 1,
84                   (const uint8 *) "testing 100 1", (int32)HDstrlen("testing 100 1") + 1);
85     CHECK_VOID(ret, FAIL, "Hputelement");
86 
87     ret = Hputelement(fid, (uint16) 100, (uint16) 4, outbuf, 2000);
88     CHECK_VOID(ret, FAIL, "Hputelement");
89 
90     ret = (int32)Hnewref(fid);
91     CHECK_VOID(ret, FAIL, "Hnewref");
92 
93     ret = Hputelement(fid, (uint16) 103, (uint16) 2,
94                   (const uint8 *) "element 103 2", (int32)HDstrlen("element 103 2") + 1);
95     CHECK_VOID(ret, FAIL, "Hputlement");
96 
97     ret = Hgetelement(fid, (uint16) 100, (uint16) 4, inbuf);
98     if (ret != 2000)
99       {
100           fprintf(stderr, "Line %d: Hgetelement returned wrong count: %d\n", (int)__LINE__, (int) ret);
101           errors++;
102       }
103 
104     for (i = 0; i < ret; i++)
105       {
106           if (inbuf[i] != outbuf[i])
107               printf("Wrong data at %d, out %d in %d\n", i, outbuf[i], inbuf[i]);
108           inbuf[i] = '\0';
109       }
110 
111     ret = Hputelement(fid, 102, 2, outbuf, BUF_SIZE);
112     CHECK_VOID(ret, FAIL, "Hputlement");
113 
114     ret = Hclose(fid);
115     CHECK_VOID(ret, FAIL, "Hclose");
116 
117     MESSAGE(5, printf("Closing and re-opening file %s\n", TESTFILE_NAME);
118         );
119     fid = Hopen(TESTFILE_NAME, DFACC_RDWR, 0);
120     CHECK_VOID(fid, FAIL, "Hopen");
121 
122     ret = (int32)Hnewref(fid);
123     CHECK_VOID(ret, FAIL, "Hnewref");
124 
125     aid1 = Hstartread(fid, 100, 1);
126     CHECK_VOID(aid1, FAIL, "Hstartread");
127 
128     ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn,
129                    &acc_mode, &special);
130     CHECK_VOID(ret, FAIL, "Hinquire");
131 
132     MESSAGE(5, printf("Verifying data\n\n");
133         );
134     ret = Hread(aid1, length, inbuf);
135     if (ret != 14)
136       {
137           fprintf(stderr, "ERROR: Hread returned the wrong length: %d\n", (int) ret);
138           errors++;
139       }
140 
141     if (HDstrcmp((const char *) inbuf, (const char *) "testing 100 1"))
142       {
143           fprintf(stderr, "ERROR: Hread returned the wrong data\n");
144           fprintf(stderr, "\t       Is: %s\n", (char *) inbuf);
145           fprintf(stderr, "\tShould be: testing 100 1\n");
146           errors++;
147       }
148 
149     ret = (int32)Hnewref(fid);
150     CHECK_VOID(ret, FAIL, "Hnewref");
151 
152     MESSAGE(5, printf("Testing a number of searching schemes\n");
153         );
154     ret = Hnextread(aid1, 100, DFREF_WILDCARD, DF_CURRENT);
155     CHECK_VOID(ret, FAIL, "Hnextread");
156 
157     ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn,
158                    &acc_mode, &special);
159     CHECK_VOID(ret, FAIL, "Hinquire");
160 
161     ret = Hnextread(aid1, 100, DFREF_WILDCARD, DF_CURRENT);
162     if (ret != FAIL)
163       {
164           fprintf(stderr, "ERROR: Found a non-existant element at line %d\n",
165                   __LINE__);
166           errors++;
167       }
168 
169     ret = Hnextread(aid1, DFTAG_WILDCARD, DFREF_WILDCARD, DF_START);
170     CHECK_VOID(ret, FAIL, "Hnextread");
171 
172     ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn,
173                    &acc_mode, &special);
174     CHECK_VOID(ret, FAIL, "Hinquire");
175 
176     ret = Hnextread(aid1, DFTAG_WILDCARD, 3, DF_CURRENT);
177     if (ret != FAIL)
178       {
179           fprintf(stderr, "ERROR: Found a non-existant element at line %d\n",
180                   __LINE__);
181           errors++;
182       }
183 
184     ret = Hnextread(aid1, DFTAG_WILDCARD, 2, DF_CURRENT);
185     CHECK_VOID(ret, FAIL, "Hnextread");
186 
187     ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn,
188                    &acc_mode, &special);
189     CHECK_VOID(ret, FAIL, "Hinquire");
190 
191     aid2 = Hstartwrite(fid, 100, 1, 4);
192     if (aid2 == FAIL)
193       {
194           fprintf(stderr, "ERROR: was not allowed to startwrite on existing object\n");
195           errors++;
196       }
197 
198     ret = Hwrite(aid1, 4, "ABCD");
199     if (ret != FAIL)
200       {
201           fprintf(stderr, "ERROR: was allowed to write to read access object\n");
202           errors++;
203       }
204 
205     ret = Hendaccess(aid1);
206     CHECK_VOID(ret, FAIL, "Hendaccess");
207 
208     ret = Hendaccess(aid2);
209     CHECK_VOID(ret, FAIL, "Hendaccess");
210 
211     MESSAGE(5, printf("Attempting to gain multiple access to file (is allowed)\n");
212         );
213     fid1 = Hopen(TESTFILE_NAME, DFACC_READ, 0);
214     if (fid1 == FAIL)
215       {
216           fprintf(stderr, "ERROR: Failed to have two concurrent access to file\n");
217           errors++;
218       }
219 
220     ret = (int32)Hnewref(fid1);
221     CHECK_VOID(ret, FAIL, "Hnewref");
222 
223     ret = Hclose(fid);
224     CHECK_VOID(ret, FAIL, "Hclose");
225 
226     ret = Hclose(fid1);
227     CHECK_VOID(ret, FAIL, "Hclose");
228 
229     ret_bool = (intn) Hishdf(TESTFILE_NAME);
230     CHECK_VOID(ret_bool, FALSE, "Hishdf");
231 
232     ret_bool = (intn) Hishdf(__FILE__);
233     CHECK_VOID(ret_bool, TRUE, "Hishdf");
234 
235     ret_bool = (intn) Hishdf("qqqqqqqq.qqq");   /* I sure hope it isn't there */
236     CHECK_VOID(ret, TRUE, "Hishdf");
237 
238 }
239