Simon Hunt
Committed by Gerrit Code Review

EE unit test and h-bird.

Change-Id: I3d775cb90a64b142ccbce5665457bda8e8f28e4b
...@@ -34,7 +34,9 @@ ...@@ -34,7 +34,9 @@
34 'xcjvte', 34 'xcjvte',
35 'bgvest', 35 'bgvest',
36 'sevlr', 36 'sevlr',
37 - 'ias' 37 + 'ias',
38 + 'jweeidkpizn'
39 + // Add more beans...
38 ]; 40 ];
39 41
40 function pickBean() { 42 function pickBean() {
......
...@@ -55,7 +55,9 @@ ...@@ -55,7 +55,9 @@
55 '7:667186698384', 55 '7:667186698384',
56 '1:857780888778876787', 56 '1:857780888778876787',
57 '20:70717066', 57 '20:70717066',
58 - '24:886774868469' 58 + '24:886774868469',
59 + '17:7487696973687580739078'
60 + // Add more sauce...
59 ]; 61 ];
60 62
61 var defaultView = 'topo', 63 var defaultView = 'topo',
......
No preview for this file type
1 +/*
2 + * Copyright 2016-present Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.ui.impl.birds;
18 +
19 +import com.google.common.io.Files;
20 +import org.junit.Ignore;
21 +import org.junit.Test;
22 +
23 +import java.io.File;
24 +import java.io.FileOutputStream;
25 +import java.io.FilenameFilter;
26 +import java.io.IOException;
27 +import java.nio.ByteBuffer;
28 +import java.nio.channels.FileChannel;
29 +import java.util.ArrayList;
30 +import java.util.Arrays;
31 +import java.util.Collections;
32 +import java.util.Comparator;
33 +import java.util.Date;
34 +import java.util.List;
35 +import java.util.Random;
36 +
37 +/**
38 + * Encrypt png images to .foo format.
39 + */
40 +public class BirdEncoderTest {
41 +
42 + private static final String ORIG = " original> ";
43 + private static final String DATA = " data> ";
44 + private static final String BEAN = " bean> ";
45 + private static final String SAUCE = " sauce> ";
46 + private static final String NONE = "(none)";
47 +
48 + private static final String PNG = ".png";
49 + private static final String FOO = ".foo";
50 +
51 + private static class Encoding {
52 + private final String name;
53 + private String bean = NONE;
54 + private String sauce = NONE;
55 +
56 + Encoding(String name) {
57 + this.name = name;
58 + }
59 +
60 + @Override
61 + public String toString() {
62 + return ORIG + name + PNG +
63 + DATA + bean + FOO +
64 + BEAN + bean +
65 + SAUCE + sauce;
66 + }
67 +
68 + private Encoding encode() {
69 + String u = name.toUpperCase();
70 + int x = u.codePointAt(1) - 64;
71 + int r = (x * 7) % 26;
72 +
73 + String uc = doTransform(r, u);
74 + bean = uc.toLowerCase();
75 +
76 + StringBuilder sauceSb = new StringBuilder();
77 + sauceSb.append(r).append(":");
78 + for (String q : uc.split("")) {
79 + sauceSb.append(q.codePointAt(0));
80 + }
81 + sauce = sauceSb.toString();
82 + return this;
83 + }
84 +
85 + private String doTransform(int r, String u) {
86 + final int m = 65;
87 + final int x = 90;
88 +
89 + int d = x - m + 1;
90 + int s = x + m;
91 + String[] letters = u.split("");
92 + StringBuilder sb = new StringBuilder();
93 +
94 + for (String j : letters) {
95 + int k = j.codePointAt(0);
96 + int e = s - k + r;
97 + e = e > x ? e - d : e;
98 + sb.append(Character.toChars(e));
99 + }
100 +
101 + return sb.toString();
102 + }
103 +
104 + String dataName() {
105 + return bean + FOO;
106 + }
107 +
108 + String pngName() {
109 + return name + PNG;
110 + }
111 + }
112 +
113 +
114 + private static final FilenameFilter FOO_FILTER =
115 + (dir, name) -> name.endsWith(FOO);
116 + private static final FilenameFilter PNG_FILTER =
117 + (dir, name) -> name.endsWith(PNG);
118 +
119 + private static final Comparator<File> FILE_COMPARATOR =
120 + (o1, o2) -> o1.getName().compareTo(o2.getName());
121 +
122 + private static final File DIR =
123 + new File("src/test/java/org/onosproject/ui/impl/birds");
124 +
125 + private static final File FOO_DIR = new File(DIR, "foo");
126 + private static final File ORIG_DIR = new File(DIR, "orig");
127 +
128 +
129 + private static final byte UPPER = (byte) 0xf0;
130 + private static final byte LOWER = (byte) 0x0f;
131 +
132 +
133 + private static void print(String fmt, Object... params) {
134 + System.out.println(String.format(fmt, params));
135 + }
136 +
137 + private List<File> filesIn(File dir, FilenameFilter filter) {
138 + File[] files = dir.listFiles(filter);
139 + if (files == null) {
140 + return Collections.emptyList();
141 + }
142 + Arrays.sort(files, FILE_COMPARATOR);
143 + return Arrays.asList(files);
144 + }
145 +
146 + private String basename(File f) {
147 + String name = f.getName();
148 + int i = name.indexOf(PNG);
149 + return name.substring(0, i);
150 + }
151 +
152 + private boolean dataRequired(Encoding encoding, List<String> dataFiles) {
153 + return !dataFiles.contains(encoding.dataName());
154 + }
155 +
156 + private List<String> makeFileNames(List<File> dataFiles) {
157 + List<String> names = new ArrayList<>(dataFiles.size());
158 + for (File f : dataFiles) {
159 + names.add(f.getName());
160 + }
161 + return names;
162 + }
163 +
164 + private final Random random = new Random(new Date().getTime());
165 + private final byte[] rbytes = new byte[2];
166 +
167 + private int r1;
168 + private int r2;
169 +
170 + private void randomBytes() {
171 + random.nextBytes(rbytes);
172 + r1 = rbytes[0];
173 + r2 = rbytes[1];
174 + }
175 +
176 + private void addNoise(ByteBuffer bb, byte b) {
177 + randomBytes();
178 +
179 + int upper = b & UPPER;
180 + int lower = b & LOWER;
181 +
182 + int uNoise = r1 & LOWER;
183 + int lNoise = r2 & UPPER;
184 +
185 + int uEnc = upper | uNoise;
186 + int lEnc = lower | lNoise;
187 +
188 + bb.put((byte) uEnc);
189 + bb.put((byte) lEnc);
190 + }
191 +
192 + private ByteBuffer encodePng(File file) throws IOException {
193 + byte[] bytes = Files.toByteArray(file);
194 + int size = bytes.length;
195 + ByteBuffer bb = ByteBuffer.allocate(size * 2);
196 + for (int i = 0; i < size; i++) {
197 + addNoise(bb, bytes[i]);
198 + }
199 + return bb;
200 + }
201 +
202 + private void generateDataFile(Encoding encoding) {
203 + File png = new File(ORIG_DIR, encoding.pngName());
204 + File foo = new File(FOO_DIR, encoding.dataName());
205 + ByteBuffer bb;
206 + try {
207 + bb = encodePng(png);
208 + writeBufferToFile(bb, foo);
209 + } catch (IOException e) {
210 + e.printStackTrace();
211 + }
212 + }
213 +
214 + private void writeBufferToFile(ByteBuffer bb, File file)
215 + throws IOException {
216 + bb.flip();
217 + FileChannel chan = new FileOutputStream(file, false).getChannel();
218 + chan.write(bb);
219 + chan.close();
220 + print(" Wrote file: %s (%d bytes)", file, bb.capacity());
221 + }
222 +
223 + @Test @Ignore
224 + public void encodeThings() {
225 + print("Encoding things...");
226 + List<File> originals = filesIn(ORIG_DIR, PNG_FILTER);
227 + List<File> dataFiles = filesIn(FOO_DIR, FOO_FILTER);
228 + List<String> fileNames = makeFileNames(dataFiles);
229 +
230 + int count = 0;
231 + for (File f : originals) {
232 + Encoding encoding = new Encoding(basename(f)).encode();
233 + print("%n%s", encoding);
234 + if (dataRequired(encoding, fileNames)) {
235 + generateDataFile(encoding);
236 + count++;
237 + }
238 + }
239 +
240 + print("%nFoo files generated: %d", count);
241 + }
242 +
243 +}
1 +Foo files will be generated in this directory.