1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=====================================================================
6**
7** Source: test1.c
8**
9** Purpose: Tests the PAL implementation of the fopen function.
10** This test simply attempts to open a number of files
11** with different modes. It checks to ensure a valid
12** file pointer is returned. It doesn't do any checking
13** to ensure the mode is really what it claims. And checks
14** for a NULL pointer when attempts to open a directory.
15**
16
17**
18**===================================================================*/
19
20#include <palsuite.h>
21
22struct testCase
23{
24 int CorrectResult;
25 char mode[20];
26};
27
28int __cdecl main(int argc, char **argv)
29{
30
31 FILE *fp;
32 char name[128];
33 int i;
34
35 struct testCase testCases[] =
36 {
37 {0, "r"}, {1, "w"}, {1, "a"},
38 {0, "r+"}, {1, "w+"}, {1, "a+"},
39 {1, "wt"}, {1, "wb"}, {1, "wS"},
40 {1, "w+c"}, {1, "w+n"}, {1, "wR"},
41 {1, "wT"}, {0, "tw"}
42 };
43
44 if (PAL_Initialize(argc, argv))
45 {
46 return FAIL;
47 }
48
49
50
51 for(i = 0; i < sizeof(testCases) / sizeof(struct testCase); i++)
52 {
53 strcpy(name,"testfiles");
54 strcat(name,testCases[i].mode);
55
56 fp = fopen(name,testCases[i].mode);
57
58 if ((fp == 0 && testCases[i].CorrectResult != 0) ||
59 (testCases[i].CorrectResult == 0 && fp != 0) )
60 {
61 Fail("ERROR: fopen returned incorrectly "
62 "opening a file in %s mode. Perhaps it opened a "
63 "read only file which didn't exist and returned a correct "
64 "pointer?",testCases[i].mode);
65 }
66
67 memset(name, '\0', 128);
68
69 }
70
71 /* When attempt to open a directory fopen should returned NULL */
72 if ( fopen(".", "r") != NULL)
73 {
74 Fail("ERROR: fopen returned non-NULL when trying to open a directory"
75 " the returned value was %d\n", fp);
76 }
77
78 PAL_Terminate();
79 return PASS;
80}
81
82
83