divine 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/LICENSE.txt +2 -1
  2. data/Rakefile +49 -0
  3. data/lib/divine/code_generators/code_generator.rb +29 -6
  4. data/lib/divine/code_generators/java.rb +121 -14
  5. data/lib/divine/code_generators/javascript.rb +100 -11
  6. data/lib/divine/code_generators/ruby.rb +89 -4
  7. data/lib/divine/version.rb +1 -1
  8. data/test/basic_complex_test/basic_complex_test.rb +34 -0
  9. data/test/basic_complex_test/java_test/JavaTest.java +96 -0
  10. data/test/basic_complex_test/java_test/test_babel.java +368 -0
  11. data/test/basic_complex_test/js_test/js_testBasic.js +58 -0
  12. data/test/basic_complex_test/js_test/js_testComplex.js +68 -0
  13. data/test/basic_complex_test/js_test/test_babel.js +523 -0
  14. data/test/basic_complex_test/ruby_test/ruby_test.rb +68 -0
  15. data/test/basic_complex_test/ruby_test/test_babel.rb +368 -0
  16. data/test/binaryTree_test/binaryTree_test.rb +19 -0
  17. data/test/binaryTree_test/java_test/JavaTest.java +114 -0
  18. data/test/binaryTree_test/java_test/test_binaryTree.java +301 -0
  19. data/test/binaryTree_test/js_test/js_test.js +76 -0
  20. data/test/binaryTree_test/js_test/test_binaryTree.js +447 -0
  21. data/test/binaryTree_test/ruby_test/ruby_test.rb +68 -0
  22. data/test/binaryTree_test/ruby_test/test_binaryTree.rb +303 -0
  23. data/test/complex_test/complex_test.rb +23 -0
  24. data/test/complex_test/java_test/JavaTest.java +126 -0
  25. data/test/complex_test/java_test/test_complex.java +331 -0
  26. data/test/complex_test/js_test/js_test.js +69 -0
  27. data/test/complex_test/js_test/test_complex.js +478 -0
  28. data/test/complex_test/ruby_test/ruby_test.rb +55 -0
  29. data/test/complex_test/ruby_test/test_complex.rb +330 -0
  30. data/test/ipv6_test/ipv6_test.rb +14 -0
  31. data/test/ipv6_test/java_test/JavaTest.java +77 -0
  32. data/test/ipv6_test/java_test/junit.jar +0 -0
  33. data/test/ipv6_test/java_test/test_ipv6.java +270 -0
  34. data/test/ipv6_test/js_test/js_test.js +60 -0
  35. data/test/ipv6_test/js_test/test_ipv6.js +409 -0
  36. data/test/ipv6_test/ruby_test/ruby_test.rb +42 -0
  37. data/test/ipv6_test/ruby_test/test_ipv6.rb +267 -0
  38. data/test/java_lib/junit.jar +0 -0
  39. data/test/unify_test/java_test/test_unify.java +171 -0
  40. data/test/unify_test/js_test/js_test.js +56 -0
  41. data/test/unify_test/js_test/test_unify.js +326 -0
  42. data/test/unify_test/ruby_test/ruby_test.rb +35 -0
  43. data/test/unify_test/ruby_test/test_unify.rb +187 -0
  44. data/test/unify_test/unify_test.rb +17 -0
  45. metadata +77 -3
@@ -0,0 +1,42 @@
1
+ require_relative 'test_ipv6.rb'
2
+ require 'minitest/autorun'
3
+
4
+ class TestBabelTestBasic < MiniTest::Unit::TestCase
5
+
6
+ def test_ipv6
7
+ puts "Test IPv6"
8
+ obj_ser = BabelTest::IPV6.new
9
+ obj_ser.list1 = ["255.102.0.25","ff:fabf:faf:f15f:f1ff:f2f:1f:f2","2001:db8::ff00:1:8329","f::","::1",""]
10
+
11
+ # Should Fail or raise error
12
+ #obj_ser.ip = "2001:0db8:0000:0000:0000:ff00:0042:8329";
13
+ #obj_ser.ip = "2001:db8::fff00:42:8329";
14
+ #obj_ser.ip = "2001:db8:::ff00:42:8329";
15
+ #obj_ser.ip = "2001:db8:ff00:42:8329";
16
+ #obj_ser.ip = "::";
17
+
18
+ ser = obj_ser.serialize
19
+ serialize(ser)
20
+ res = deserialize()
21
+
22
+ obj_deser = BabelTest::IPV6.new
23
+ obj_deser.deserialize res
24
+ compare(obj_ser, obj_deser)
25
+
26
+ end
27
+
28
+ def compare(obj_ser, obj_deser)
29
+ assert_equal obj_ser.list1, obj_deser.list1
30
+ end
31
+
32
+ def serialize(data)
33
+ File.open("test/ipv6_test/ruby_test/bin.babel.rb", "w+b") do |f|
34
+ f.write(data)
35
+ end
36
+ end
37
+
38
+ def deserialize()
39
+ mem_buf = File.new('test/ipv6_test/ruby_test/bin.babel.rb').binmode
40
+ end
41
+
42
+ end
@@ -0,0 +1,267 @@
1
+ module BabelTest
2
+
3
+
4
+ class BabelBase < Object
5
+ public
6
+ def serialize
7
+ out = []
8
+ serialize_internal(out)
9
+ out.flatten.pack("C*")
10
+ end
11
+
12
+ protected
13
+ ### Read methods ###
14
+ def read_int8(data)
15
+ data.getbyte
16
+ end
17
+
18
+ def read_int16(data)
19
+ (data.getbyte << 8) | read_int8(data)
20
+ end
21
+
22
+ def read_int24(data)
23
+ (data.getbyte << 16) | read_int16(data)
24
+ end
25
+
26
+ def read_int32(data)
27
+ (data.getbyte << 24) | read_int24(data)
28
+ end
29
+
30
+ def read_bool(data)
31
+ read_int8(data) == 1
32
+ end
33
+
34
+ def read_string(data)
35
+ data.read(read_int16(data)).force_encoding('UTF-8')
36
+ end
37
+
38
+ def read_binary(data)
39
+ data.read(read_int32(data))
40
+ end
41
+
42
+ def read_short_binary(data)
43
+ data.read(read_int8(data))
44
+ end
45
+
46
+ def read_ip_number(data)
47
+ ips = read_short_binary(data)
48
+ if ips.size == 4
49
+ read_ipv4_number(ips)
50
+ else
51
+ read_ipv6_number(ips)
52
+ end
53
+ end
54
+
55
+ def read_ipv4_number(ips)
56
+ ips.bytes.to_a.join('.')
57
+ end
58
+
59
+ def read_ipv6_number(ips)
60
+ ipv6 = []
61
+ ips.bytes.each_slice(2) do |t|
62
+ fst = t[0]
63
+ lst = t[1]
64
+ tmp = ""
65
+ tmp = fst.to_s 16 if fst != 0
66
+ if fst != 0 and lst < 10
67
+ tmp << "0#{lst.to_s 16}"
68
+ elsif fst != 0 and lst > 10 or fst == 0 and lst > 0
69
+ tmp << lst.to_s(16)
70
+ end
71
+ ipv6.push(tmp)
72
+ end
73
+ ipv6.join(':').gsub(/:{2,}/, "::")
74
+ end
75
+
76
+ ### Write methods ###
77
+ def write_int8(v, out)
78
+ v = v.to_i
79
+ raise_error "Too large int8 number: #{v}" if v > 0xFF # Max 255
80
+ raise_error "a negative number passed to int8 number: #{v}" if v < 0
81
+ out << v
82
+ end
83
+
84
+ def write_int16(v, out)
85
+ v = v.to_i
86
+ raise_error "Too large int16 number: #{v}" if v > 0xFFFF # Max 65.535
87
+ raise_error "a negative number passed to int16 number: #{v}" if v < 0
88
+ write_int8( v >> 8 & 0xFF, out)
89
+ write_int8( v & 0xFF, out)
90
+ end
91
+
92
+ def write_int24(v, out)
93
+ v = v.to_i
94
+ raise_error "Too large int24 number: #{v}" if v > 0xFFFFFF # Max 16.777.215
95
+ raise_error "a negative number passed to int24 number: #{v}" if v < 0 # In Case added to ruby declaration
96
+ write_int8( v >> 16 & 0xFF, out)
97
+ write_int16( v & 0xFFFF, out)
98
+ end
99
+
100
+ def write_int32(v, out)
101
+ v = v.to_i
102
+ raise_error "Too large int32 number: #{v}" if v > 0xFFFFFFFF # Max 4.294.967.295
103
+ raise_error "a negative number passed to int32 number: #{v}" if v < 0
104
+ write_int8( v >> 24 & 0xFF, out)
105
+ write_int24( v & 0xFFFFFF, out)
106
+ end
107
+
108
+ def write_bool(v, out)
109
+ write_int8(v ? 1 : 0, out)
110
+ end
111
+
112
+ def write_string(v, out)
113
+ s = force_to_utf8_string(v)
114
+ raise_error "Too large string: #{s.bytesize} bytes" if s.bytesize > 0xFFFF
115
+ write_int16(s.bytesize, out)
116
+ out << s.bytes.to_a
117
+ end
118
+
119
+ def write_binary(v, out)
120
+ if v.is_a?(Array)
121
+ raise_error "Too large binary: #{v.size} (#{v.class.name})" unless v.size < 0xFFFFFFFF
122
+ write_int32(v.size, out)
123
+ v.each do |x|
124
+ write_int8(x, out)
125
+ end
126
+ elsif v.is_a?(String)
127
+ raise_error "Too large binary: #{v.size} (#{v.class.name})" unless v.size < 0xFFFFFFFF
128
+ write_int32(v.size, out)
129
+ out << v.bytes.to_a
130
+ else
131
+ raise_error "Unsupported binary 'nil'" if v == nil
132
+ raise_error "Unsupported binary of type '#{v.class.name}'"
133
+ end
134
+ end
135
+
136
+ def write_16_binary(v, out)
137
+ if v.is_a?(Array)
138
+ raise_error "Too large 16 binary: #{v.size} (#{v.class.name})" unless v.size*2 < 0xFF
139
+ write_int8(v.size * 2, out) # IPv6 consists of 8 parts each of them has zise of 2 bytes
140
+ v.each do |x|
141
+ write_int16(x, out)
142
+ end
143
+ else
144
+ raise_error "Unsupported binary 'nil'" if v == nil
145
+ raise_error "Unsupported binary of type '#{v.class.name}'"
146
+ end
147
+ end
148
+
149
+ def write_short_binary(v, out)
150
+ if v.is_a?(Array)
151
+ raise_error "Too large short_binary: #{v.size} (#{v.class.name})" unless v.size < 0xFF
152
+ write_int8(v.size, out)
153
+ v.each do |x|
154
+ write_int8(x, out)
155
+ end
156
+ elsif v.is_a?(String)
157
+ raise_error "To large short_binary: #{v.size} (#{v.class.name})" unless v.size < 0xFF
158
+ write_int8(v.size, out)
159
+ out << v.bytes.to_a
160
+ else
161
+ raise_error "Unsupported binary 'nil'" if v == nil
162
+ raise_error "Unsupported binary of type '#{v.class.name}'"
163
+ end
164
+ end
165
+
166
+ def write_ip_number(v, out)
167
+ if v.is_a?(Array)
168
+ if v.size == 4
169
+ write_ipv4_number(v, out);
170
+ else
171
+ write_ipv6_number(v, out);
172
+ end
173
+ elsif v.is_a?(String)
174
+ if v.include?":"
175
+ write_ipv6_number(v, out);
176
+ else
177
+ write_ipv4_number(v, out);
178
+ end
179
+ else
180
+ raise_error "Unknown IP number '#{v}'"
181
+ end
182
+ end
183
+
184
+ def write_ipv4_number(v,out)
185
+ if v.is_a?(Array)
186
+ raise_error "Unknown IP v4 number #{v}" unless v.size == 0 || v.size == 4 # Only IPv4 for now
187
+ write_short_binary(v, out)
188
+ elsif v.is_a?(String)
189
+ ss = v.split(/\./).map do |s|
190
+ s.to_i
191
+ end
192
+ write_ipv4_number(ss, out)
193
+ else
194
+ raise_error "Unknown IP number '#{v}'"
195
+ end
196
+ end
197
+
198
+ def write_ipv6_number(v, out)
199
+ if v.is_a?(Array)
200
+ write_16_binary(v, out)
201
+ elsif v.is_a?(String)
202
+ v = v.gsub(" ","") + " " # Temporary: To avoid the split problem when we have : at the end of "v"
203
+ raise_error "Unknown IPv6 number #{v}" unless v.strip.empty? ||
204
+ v.strip.match(/[^:0-9a-f]+/i) == nil && #Should not contains numbers or letters 0-9a-f
205
+ v.strip.match(/[0-9a-f]+/i) != nil && #Should contains numbers or letters 0-9a-f
206
+ v.match(":{3,}") == nil &&
207
+ v.split("::").size <= 2
208
+ ss = v.split(/:/).map do |s|
209
+ s = s.strip
210
+ raise_error "Unknown IPv6 Group #{s}" unless s.size <= 4
211
+ s.to_i 16
212
+ end
213
+ ss = [] if v.strip.empty?
214
+ raise_error "Unknown IPv6 number #{v}" unless (!v.include?("::") && ss.size == 0 || ss.size == 8) ||
215
+ (v.include?("::") && ss.size > 2 && ss.size < 8)
216
+ write_ipv6_number(ss, out)
217
+ else
218
+ raise_error "Unknown IPv6 number '#{v}'"
219
+ end
220
+ end
221
+
222
+ private
223
+ def raise_error(msg)
224
+ raise "[#{self.class.name}] #{msg}"
225
+ end
226
+
227
+ def force_to_utf8_string(string)
228
+ if string.encoding != Encoding::UTF_8
229
+ string = string.encode(Encoding::UTF_8)
230
+ end
231
+ return string
232
+ end
233
+ end
234
+
235
+
236
+
237
+ class IPV6 < BabelBase
238
+ attr_accessor :list1
239
+
240
+ def initialize()
241
+ super
242
+ @list1 ||= []
243
+ end
244
+
245
+ def serialize_internal(out)
246
+ print "+"
247
+ # Serialize list 'list1'
248
+ write_int32(list1.size, out)
249
+ list1.each do |var_100|
250
+ write_ip_number(var_100, out)
251
+ end
252
+ end
253
+
254
+ def deserialize(data)
255
+ print "-"
256
+ # Deserialize list 'list1'
257
+ @list1 = []
258
+ var_101 = read_int32(data)
259
+ (1..var_101).each do
260
+ var_102 = read_ip_number(data)
261
+ @list1 << var_102
262
+ end
263
+ end
264
+ end
265
+
266
+
267
+ end
Binary file
@@ -0,0 +1,171 @@
1
+ import java.io.ByteArrayInputStream;
2
+ import java.io.ByteArrayOutputStream;
3
+ import java.io.IOException;
4
+ import java.nio.charset.Charset;
5
+ import java.util.ArrayList;
6
+ import java.util.HashMap;
7
+
8
+ abstract class BabelBase {
9
+ private static final Charset UTF8 = Charset.forName("UTF-8");
10
+
11
+ public byte[] serialize() throws IOException {
12
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
13
+ serializeInternal(baos);
14
+ baos.close();
15
+ return baos.toByteArray();
16
+ }
17
+
18
+ abstract void serializeInternal(ByteArrayOutputStream baos) throws IOException;
19
+
20
+ abstract void deserialize(ByteArrayInputStream baos) throws IOException;
21
+
22
+ protected int readInt8(ByteArrayInputStream data) {
23
+ return data.read() & 0xff;
24
+ }
25
+
26
+ protected int readInt16(ByteArrayInputStream data) {
27
+ return (data.read() << 8) | readInt8(data);
28
+ }
29
+
30
+ protected int readInt24(ByteArrayInputStream data) {
31
+ return (data.read() << 16) | readInt16(data);
32
+ }
33
+
34
+ protected long readInt32(ByteArrayInputStream data) {
35
+ return (data.read() << 24) | readInt24(data);
36
+ }
37
+
38
+ protected boolean readBool(ByteArrayInputStream data) {
39
+ return readInt8(data) == 1;
40
+ }
41
+
42
+ protected String readString(ByteArrayInputStream data) throws IOException {
43
+ // Force utf8
44
+ return new String(readBytes(readInt16(data), data), UTF8);
45
+ }
46
+
47
+ private byte[] readBytes(int size, ByteArrayInputStream data) throws IOException {
48
+ byte[] bs = new byte[size];
49
+ data.read(bs);
50
+ return bs;
51
+ }
52
+
53
+ protected byte[] readBinary(ByteArrayInputStream data) throws IOException {
54
+ long c = readInt32(data);
55
+ if (c > Integer.MAX_VALUE) {
56
+ throw new IndexOutOfBoundsException("Binary data to big for java");
57
+ }
58
+ return readBytes((int) c, data);
59
+ }
60
+
61
+ protected byte[] readShortBinary(ByteArrayInputStream data) throws IOException {
62
+ return readBytes(readInt8(data), data);
63
+ }
64
+
65
+ protected String readIpNumber(ByteArrayInputStream data) throws IOException {
66
+ byte[] ips = readShortBinary(data);
67
+ String ip = "";
68
+ for (byte b : ips) {
69
+ if (ip.length() > 0) {
70
+ ip += ".";
71
+ }
72
+ ip += (b & 0xFF);
73
+ }
74
+ return ip;
75
+ }
76
+
77
+ protected void writeInt8(int v, ByteArrayOutputStream out) {
78
+ if (v > 0xFF) { // Max 255
79
+ raiseError("Too large int8 number: " + v);
80
+ }
81
+ out.write(v);
82
+ }
83
+
84
+ protected void writeInt16(int v, ByteArrayOutputStream out) {
85
+ if (v > 0xFFFF) { // Max 65.535
86
+ raiseError("Too large int16 number: " + v);
87
+ }
88
+ writeInt8(v >> 8 & 0xFF, out);
89
+ writeInt8(v & 0xFF, out);
90
+ }
91
+
92
+ protected void writeInt24(int v, ByteArrayOutputStream out) {
93
+ if (v > 0xFFFFFF) { // Max 16.777.215
94
+ raiseError("Too large int24 number: " + v);
95
+ }
96
+ writeInt8(v >> 16 & 0xFF, out);
97
+ writeInt16(v & 0xFFFF, out);
98
+ }
99
+
100
+ protected void writeInt32(long v, ByteArrayOutputStream out) {
101
+ if (v > 0xFFFFFFFFL) { // Max 4.294.967.295
102
+ raiseError("Too large int32 number: " + v);
103
+ }
104
+ writeInt8((int) ((v >> 24) & 0xFF), out);
105
+ writeInt24((int) (v & 0xFFFFFF), out);
106
+ }
107
+
108
+ protected void writeBool(boolean v, ByteArrayOutputStream out) {
109
+ writeInt8(v ? 1 : 0, out);
110
+ }
111
+
112
+ protected void writeString(String v, ByteArrayOutputStream out) throws IOException {
113
+ byte[] bs = v.getBytes(UTF8);
114
+ if (bs.length > 0xFFFF) {
115
+ raiseError("Too large string: " + bs.length + " bytes");
116
+ }
117
+ writeInt16(bs.length, out);
118
+ out.write(bs);
119
+ }
120
+
121
+ protected void writeBinary(byte[] v, ByteArrayOutputStream out) throws IOException {
122
+ if (v.length > 0xFFFFFFFFL) {
123
+ raiseError("Too large binary: " + v.length + " bytes");
124
+ }
125
+ writeInt32(v.length, out);
126
+ out.write(v);
127
+ }
128
+
129
+ protected void writeShortBinary(byte[] v, ByteArrayOutputStream out) throws IOException {
130
+ if (v.length > 0xFF) {
131
+ raiseError("Too large short_binary: " + v.length + " bytes");
132
+ }
133
+ writeInt8(v.length, out);
134
+ out.write(v);
135
+ }
136
+
137
+ protected void writeIpNumber(String v, ByteArrayOutputStream out) throws IOException {
138
+ byte[] ss = new byte[0];
139
+ if(!v.isEmpty()){
140
+ String[] bs = v.split("\\.");
141
+ ss = new byte[bs.length];
142
+ for (int i = 0; i < bs.length; i++) {
143
+ ss[i] = (byte) (Integer.parseInt(bs[i]) & 0xFF);
144
+ }
145
+ }
146
+ if (ss.length == 0 || ss.length == 4) {
147
+ writeShortBinary(ss, out);
148
+ } else {
149
+ raiseError("Unknown IP v4 number " + v); // Only IPv4 for now
150
+ }
151
+ }
152
+
153
+ protected void raiseError(String msg) {
154
+ throw new IllegalArgumentException("[" + this.getClass().getCanonicalName() + "] " + msg);
155
+ }
156
+ }
157
+
158
+
159
+ class UnifySer extends BabelBase {
160
+ public int i8 = 0;
161
+
162
+ @Override
163
+ void serializeInternal(ByteArrayOutputStream baos) throws IOException {
164
+ writeInt8(this.i8, baos);
165
+ }
166
+
167
+ @Override
168
+ void deserialize(ByteArrayInputStream bais) throws IOException {
169
+ this.i8 = readInt8(bais);
170
+ }
171
+ }