2
// Test.Rules.BadPractice.UseFileOpenOnlyWithFileAccessTest
5
// Yuri Stuken <stuken.yuri@gmail.com>
7
// Copyright (C) 2010 Yuri Stuken
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
using System.IO.IsolatedStorage;
31
using System.Collections.Generic;
32
using System.Security.AccessControl;
34
using Gendarme.Rules.BadPractice;
36
using NUnit.Framework;
37
using Test.Rules.Fixtures;
38
using Test.Rules.Definitions;
41
namespace Test.Rules.BadPractice {
44
public class UseFileOpenOnlyWithFileAccessTest : MethodRuleTestFixture<UseFileOpenOnlyWithFileAccessRule> {
47
public void DoesNotApply ()
50
AssertRuleDoesNotApply (SimpleMethods.ExternalMethod);
51
// no CALL[VIRT] instruction
52
AssertRuleDoesNotApply (SimpleMethods.EmptyMethod);
55
public void BadMethod1 ()
57
var f = File.Open ("HelloWorld.cs", FileMode.Open);
60
public void BadMethod2 ()
62
var f = File.Open ("HelloWorld.cs", FileMode.Append);
64
string a = f.ToString ();
65
var g = new FileStream ("HelloWorld.cs", FileMode.Truncate);
74
public void BadMethod3()
76
var f = File.Open ("HelloWorld.cs", FileMode.CreateNew);
77
var g = new FileStream ("HelloWorld.cs", FileMode.OpenOrCreate);
78
var h = new IsolatedStorageFileStream ("HelloWorld.cs", FileMode.Create);
81
public void GoodMethod ()
83
var f = File.Open ("HelloWorld.cs", FileMode.Open, FileAccess.Read);
84
var g = new FileStream ("HelloWorld.cs", FileMode.OpenOrCreate, FileAccess.ReadWrite);
85
var h = new IsolatedStorageFileStream ("HelloWorld.cs", FileMode.Create, FileAccess.Write);
88
List<string> ls = new List<string> { "a", "b" };
91
var i = new FileStream ("HelloWorld.cs", FileMode.Open, FileSystemRights.Read,
92
FileShare.Read, 8, FileOptions.None);
98
AssertRuleFailure<UseFileOpenOnlyWithFileAccessTest> ("BadMethod1", 1);
99
AssertRuleFailure<UseFileOpenOnlyWithFileAccessTest> ("BadMethod2", 2);
100
AssertRuleFailure<UseFileOpenOnlyWithFileAccessTest> ("BadMethod3", 3);
106
AssertRuleSuccess<UseFileOpenOnlyWithFileAccessTest> ("GoodMethod");