View Javadoc
1   /**
2    * Waffle (https://github.com/dblock/waffle)
3    *
4    * Copyright (c) 2010 - 2015 Application Security, Inc.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Eclipse Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/epl-v10.html
10   *
11   * Contributors:
12   *     Application Security, Inc.
13   */
14  package waffle.mock;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import waffle.windows.auth.IWindowsAccount;
20  import waffle.windows.auth.IWindowsIdentity;
21  import waffle.windows.auth.IWindowsImpersonationContext;
22  
23  /**
24   * A Mock windows identity.
25   * 
26   * @author dblock[at]dblock[dot]org
27   */
28  public class MockWindowsIdentity implements IWindowsIdentity {
29  
30      /** The fqn. */
31      private final String       fqn;
32      
33      /** The groups. */
34      private final List<String> groups;
35  
36      /**
37       * Instantiates a new mock windows identity.
38       *
39       * @param newFqn
40       *            the new fqn
41       * @param newGroups
42       *            the new groups
43       */
44      public MockWindowsIdentity(final String newFqn, final List<String> newGroups) {
45          this.fqn = newFqn;
46          this.groups = newGroups;
47      }
48  
49      /* (non-Javadoc)
50       * @see waffle.windows.auth.IWindowsIdentity#getFqn()
51       */
52      @Override
53      public String getFqn() {
54          return this.fqn;
55      }
56  
57      /* (non-Javadoc)
58       * @see waffle.windows.auth.IWindowsIdentity#getGroups()
59       */
60      @Override
61      public IWindowsAccount[] getGroups() {
62          final List<MockWindowsAccount> groupsList = new ArrayList<MockWindowsAccount>();
63          for (final String group : this.groups) {
64              groupsList.add(new MockWindowsAccount(group));
65          }
66          return groupsList.toArray(new IWindowsAccount[0]);
67      }
68  
69      /* (non-Javadoc)
70       * @see waffle.windows.auth.IWindowsIdentity#getSid()
71       */
72      @Override
73      public byte[] getSid() {
74          return new byte[0];
75      }
76  
77      /* (non-Javadoc)
78       * @see waffle.windows.auth.IWindowsIdentity#getSidString()
79       */
80      @Override
81      public String getSidString() {
82          return "S-" + this.fqn.hashCode();
83      }
84  
85      /* (non-Javadoc)
86       * @see waffle.windows.auth.IWindowsIdentity#dispose()
87       */
88      @Override
89      public void dispose() {
90          // Do Nothing
91      }
92  
93      /* (non-Javadoc)
94       * @see waffle.windows.auth.IWindowsIdentity#isGuest()
95       */
96      @Override
97      public boolean isGuest() {
98          return this.fqn.equals("Guest");
99      }
100 
101     /* (non-Javadoc)
102      * @see waffle.windows.auth.IWindowsIdentity#impersonate()
103      */
104     @Override
105     public IWindowsImpersonationContext impersonate() {
106         return new MockWindowsImpersonationContext();
107     }
108 }