~ubuntu-branches/ubuntu/wily/monodevelop/wily

« back to all changes in this revision

Viewing changes to external/mono-tools/gendarme/rules/Gendarme.Rules.BadPractice/Test/UseFileOpenOnlyWithFileAccessTest.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-05-27 18:08:20 UTC
  • mfrom: (19.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20120527180820-fydl21qnbnfr8w2t
Tags: 3.0.2+dfsg-3
* [fcecfe7] Fix monodevelop-core-addins.pc.in to point to actual 
  installed location of assemblies.
* [26e1a07] DebSrc 3.0 does not support Quilt's -p parameter, so 
  manually adjust the path in the patch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Test.Rules.BadPractice.UseFileOpenOnlyWithFileAccessTest
 
3
//
 
4
// Authors:
 
5
//      Yuri Stuken <stuken.yuri@gmail.com>
 
6
//
 
7
// Copyright (C) 2010 Yuri Stuken
 
8
//
 
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:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
 
 
28
using System;
 
29
using System.IO;
 
30
using System.IO.IsolatedStorage;
 
31
using System.Collections.Generic;
 
32
using System.Security.AccessControl;
 
33
 
 
34
using Gendarme.Rules.BadPractice;
 
35
 
 
36
using NUnit.Framework;
 
37
using Test.Rules.Fixtures;
 
38
using Test.Rules.Definitions;
 
39
 
 
40
 
 
41
namespace Test.Rules.BadPractice {
 
42
 
 
43
        [TestFixture]
 
44
        public class UseFileOpenOnlyWithFileAccessTest : MethodRuleTestFixture<UseFileOpenOnlyWithFileAccessRule> {
 
45
 
 
46
                [Test]
 
47
                public void DoesNotApply ()
 
48
                {
 
49
                        // no IL
 
50
                        AssertRuleDoesNotApply (SimpleMethods.ExternalMethod);
 
51
                        // no CALL[VIRT] instruction
 
52
                        AssertRuleDoesNotApply (SimpleMethods.EmptyMethod);
 
53
                }
 
54
 
 
55
                public void BadMethod1 ()
 
56
                {
 
57
                        var f = File.Open ("HelloWorld.cs", FileMode.Open);
 
58
                }
 
59
 
 
60
                public void BadMethod2 ()
 
61
                {
 
62
                        var f = File.Open ("HelloWorld.cs", FileMode.Append);
 
63
                        // unrelated call
 
64
                        string a = f.ToString ();
 
65
                        var g = new FileStream ("HelloWorld.cs", FileMode.Truncate);
 
66
                        // unrelated code
 
67
                        if (a.Length == 42)
 
68
                                return;
 
69
                        else
 
70
                                return;
 
71
 
 
72
                }
 
73
 
 
74
                public void BadMethod3()
 
75
                {
 
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);
 
79
                }
 
80
 
 
81
                public void GoodMethod ()
 
82
                {
 
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);
 
86
                        
 
87
                        // unrelated code
 
88
                        List<string> ls = new List<string> { "a", "b" };
 
89
                        ls.Clear ();
 
90
 
 
91
                        var i = new FileStream ("HelloWorld.cs", FileMode.Open, FileSystemRights.Read,
 
92
                                        FileShare.Read, 8, FileOptions.None);
 
93
                }
 
94
 
 
95
                [Test]
 
96
                public void Bad ()
 
97
                {
 
98
                        AssertRuleFailure<UseFileOpenOnlyWithFileAccessTest> ("BadMethod1", 1);
 
99
                        AssertRuleFailure<UseFileOpenOnlyWithFileAccessTest> ("BadMethod2", 2);
 
100
                        AssertRuleFailure<UseFileOpenOnlyWithFileAccessTest> ("BadMethod3", 3);
 
101
                }
 
102
 
 
103
                [Test]
 
104
                public void Good ()
 
105
                {
 
106
                        AssertRuleSuccess<UseFileOpenOnlyWithFileAccessTest> ("GoodMethod");
 
107
                }
 
108
        }
 
109
}