Remove Tabs, per Jococo
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / config / test / JU_MapBathConverter.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  */
20
21 package org.onap.aaf.cadi.config.test;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.sql.Date;
26 import java.text.SimpleDateFormat;
27 import java.util.ArrayList;
28 import java.util.GregorianCalendar;
29 import java.util.Iterator;
30 import java.util.List;
31
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.aaf.cadi.Access;
36 import org.onap.aaf.cadi.CadiException;
37 import org.onap.aaf.cadi.PropAccess;
38 import org.onap.aaf.cadi.Symm;
39 import org.onap.aaf.cadi.filter.MapBathConverter;
40 import org.onap.aaf.cadi.util.CSV;
41 import org.onap.aaf.cadi.util.CSV.Visitor;
42 import org.onap.aaf.cadi.util.CSV.Writer;
43
44 import junit.framework.Assert;
45
46 /**
47  * Test a simple Migration conversion tool for CADI
48  * 
49  * @author Instrumental(Jonathan)
50  *
51  */
52 public class JU_MapBathConverter {
53     private static final String NEW_USER_SOMETHING_ORG = "NEW_USER@Something.org";
54     private static final String OLD_ID = "OLD_ID";
55     private static final String SHARED_PASS = "SHARED_PASS";
56     private static CSV csv;
57     private static ArrayList<String> expected;
58     private static final Access access = new PropAccess();
59     private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
60
61     @BeforeClass
62     public static void createFile() throws IOException {
63         // Note, you cate a "MapBathConverter" by access to a File.
64         // We will create that file now.  Local is fine.
65         csv = new CSV(access,"JU_MapBathConverter.csv"); 
66     }
67     
68     @BeforeClass
69     public static void beforeClass() {
70         expected = new ArrayList<>();
71     }
72     
73     @Before
74     public void before() {
75         expected.clear();
76     }
77     
78     @Test
79     public void test() throws IOException, CadiException {
80         CSV.Writer cw = csv.writer();
81         GregorianCalendar gc = new GregorianCalendar();
82         gc.add(GregorianCalendar.MONTH, 6);
83         try {
84             try {
85                 // CSV can simply be OLD ID and NEW,  no passwords
86                 cw.row(exp(OLD_ID), exp(NEW_USER_SOMETHING_ORG),sdf.format(gc.getTime()));
87
88                 // Style 1 - Incoming ID/pass, create new cred with NweID and same Pass
89                 cw.row(exp(bath(OLD_ID,SHARED_PASS)), exp(NEW_USER_SOMETHING_ORG),sdf.format(gc.getTime()));
90                 // the response should be Basic with NEW_ID and OLD_PASS
91                 
92                 // Style 2
93                 cw.row(exp(bath(OLD_ID,"OLD_PASS")), exp(bath(NEW_USER_SOMETHING_ORG,"NEW_PASS")),sdf.format(gc.getTime()));
94
95             } finally {
96                 cw.close();
97             }
98             
99             final Iterator<String> exp = expected.iterator();
100             csv.visit(new Visitor() {
101                 @Override
102                 public void visit(List<String> row) {
103                     int i=0;
104                     for(String s : row) {
105                         switch(i++) {
106                             case 0:
107                             case 1:
108                                 Assert.assertEquals(exp.next(), s);
109                                 break;
110                             case 2:
111                                 try {
112                                     Date.valueOf(s);
113                                 } catch (Exception e) {
114                                     Assert.assertTrue("Last entry should be a date",false);
115                                 }
116                                 break;
117                             default:
118                                 Assert.fail("There should only be 3 columns in this test case.");
119                         }
120                     }
121                 }
122             });
123             
124             MapBathConverter mbc = new MapBathConverter(access, csv);
125
126             // Check no lookup just returns the same
127             Assert.assertEquals("NonKey", "NonKey"); // if not in map, expect same value
128
129             Iterator<String> exp1 = expected.iterator();
130             // there's no passwords in CSV
131             String old = exp1.next(); 
132             String nw = exp1.next();
133             Assert.assertEquals(nw, mbc.convert(access,old));
134             
135             Assert.assertEquals(bath(NEW_USER_SOMETHING_ORG,SHARED_PASS), mbc.convert(access,bath(OLD_ID,SHARED_PASS)));
136             
137             // Style 1 (new cred, old password)
138             old = exp1.next();
139             nw = bath(exp1.next(),SHARED_PASS);
140             Assert.assertEquals(nw, mbc.convert(access,old));
141
142             // Style 2
143             old = exp1.next();
144             nw = exp1.next();
145             Assert.assertEquals(nw, mbc.convert(access,old));
146
147         } finally {
148             csv.delete();
149         }
150     }
151
152     @Test
153     public void testInsecureRole() throws IOException {
154         CSV.Writer cw = csv.writer();
155         GregorianCalendar gc = new GregorianCalendar();
156         gc.add(GregorianCalendar.MONTH, 6);
157         try {
158             try {
159                 // Invalid Scenario - Non Authenticated ID to authenticated User
160                 cw.row(exp(OLD_ID), exp(bath(NEW_USER_SOMETHING_ORG,"NEW_PASS")),sdf.format(gc.getTime()));
161
162             } finally {
163                 cw.close();
164             }
165             
166             try {
167                 new MapBathConverter(access, csv);
168                 Assert.fail("Invalid Data should throw Exception");
169             } catch (CadiException e) {
170                 Assert.assertTrue("Invalid Data should throw Exception",true);
171             }
172
173         } finally {
174             csv.delete();
175         }
176     }
177
178     @Test
179     public void testTooFewColumns() throws IOException, CadiException {
180         CSV.Writer cw = csv.writer();
181         try {
182             try {
183                 cw.row(exp(bath(OLD_ID,"OLD_PASS")), exp(bath(NEW_USER_SOMETHING_ORG,"NEW_PASS")));
184             } finally {
185                 cw.close();
186             }
187             
188             try {
189                 new MapBathConverter(access, csv);
190                 Assert.fail("file with too few rows should throw exception");
191             } catch(CadiException | IOException e) {
192                 Assert.assertTrue("Correctly thrown Exception",true);
193             }
194         } finally {
195             csv.delete();
196         }
197     }
198
199     @Test
200     public void testNoFile() {
201         try {
202             new MapBathConverter(access, new CSV(access,"Bogus"));
203             Assert.fail("Non Existent File should throw exception");
204         } catch(CadiException | IOException e) {
205             Assert.assertTrue("Correctly thrown Exception",true);
206         }
207     }
208     
209     @Test
210     public void testBadRows() throws IOException {
211         try {
212             Writer cw = csv.writer();
213             try {
214                 cw.row("Single Column");
215             } finally {
216                 cw.close();
217             }
218             
219             try {
220                 new MapBathConverter(access,csv);
221                 Assert.fail("Non Existent File should throw exception");
222             } catch(CadiException | IOException e) {
223                 Assert.assertTrue("Correctly thrown Exception",true);
224             }
225         } finally {
226             csv.delete();
227         }
228         
229         // Check for deletion 
230         Assert.assertFalse(csv.toString() + "should have been deleted",new File(csv.toString()).exists());
231     }
232     
233     private String bath(String user, String password) throws IOException {
234         StringBuilder sb = new StringBuilder(user);
235         sb.append(':');
236         sb.append(password);
237         byte[] encoded = Symm.base64noSplit.encode(sb.toString().getBytes());
238         sb.setLength(0);
239         sb.append("Basic ");
240         sb.append(new String(encoded));
241         return sb.toString();
242     }
243
244     private String exp(String s) {
245         expected.add(s);
246         return s;
247     }
248 }