coap 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,81 @@
1
+ module CoAP
2
+ class MySocket
3
+
4
+ attr_writer :socket_type, :ack_timeout
5
+
6
+ def initialize
7
+ @logger = CoAP.logger
8
+
9
+ end
10
+
11
+ def connect(hostname, port)
12
+ # hostname or ipv4/ipv6 address?
13
+ address = IPAddr.new(hostname)
14
+ return connect_socket(address, port)
15
+
16
+ rescue IPAddr::InvalidAddressError
17
+ # got a hostname, trying to resolv
18
+
19
+ addresses = IPv6FavorResolv.getaddresses(hostname)
20
+
21
+ raise Resolv::ResolvError if addresses.empty?
22
+
23
+ addresses.each do |address|
24
+
25
+ begin
26
+
27
+ # transform to IPAddr object
28
+ address = IPAddr.new(address.to_s)
29
+ return connect_socket(address, port)
30
+
31
+ rescue Errno::EHOSTUNREACH
32
+
33
+ @logger.fatal 'Address unreachable: ' + address.to_s if $DEBUG
34
+ # try next one if exists
35
+
36
+ rescue Errno::ENETUNREACH
37
+
38
+ @logger.fatal 'Net unreachable: ' + address.to_s if $DEBUG
39
+ # try next one if exists
40
+
41
+ end
42
+
43
+ end
44
+ end
45
+
46
+ def send(data, flags)
47
+ @socket.send(data, flags)
48
+ end
49
+
50
+ def receive(timeout = nil, retry_count = 0)
51
+ timeout = @ack_timeout**(retry_count + 1) if timeout.nil? # timeout doubles
52
+
53
+ @logger.debug @socket.peeraddr.inspect
54
+ @logger.debug @socket.addr.inspect
55
+ @logger.debug 'AAA Timeout: ' + timeout.to_s
56
+
57
+ recv_data = nil
58
+ status = Timeout.timeout(timeout) do
59
+ recv_data = @socket.recvfrom(1024)
60
+ end
61
+
62
+ # pp recv_data
63
+ recv_data
64
+ end
65
+
66
+ private
67
+
68
+ def connect_socket(address, port)
69
+ if address.ipv6?
70
+ @socket = @socket_type.new(Socket::AF_INET6)
71
+ else
72
+ @socket = @socket_type.new
73
+ end
74
+
75
+ # TODO: Error handling connection
76
+ @socket.connect(address.to_s, port)
77
+
78
+ @socket
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,41 @@
1
+ module CoAP
2
+ class Observer
3
+ MAX_OBSERVE_OPTION_VALUE = 8_388_608
4
+
5
+ def initialize
6
+ @logger = CoAP.logger
7
+
8
+ @retry_count = 0
9
+ end
10
+
11
+ def observe(recv_parsed, recv_data, observe_callback, mySocket)
12
+ observe_number = recv_parsed.options[:observe]
13
+ observe_callback.call(recv_parsed, recv_data)
14
+
15
+ loop do
16
+ begin # todo fix this
17
+ recv_data = mySocket.receive(60, @retry_count)
18
+ rescue Timeout::Error
19
+ @retry_count = 0
20
+ @logger.error 'Observe Timeout'
21
+ end
22
+
23
+ recv_parsed = CoAP.parse(recv_data[0].force_encoding('BINARY'))
24
+
25
+ if recv_parsed.tt == :con
26
+ message = Message.new(:ack, 0, recv_parsed.mid, nil, {})
27
+ mySocket.send message.to_wire, 0
28
+ end
29
+
30
+ if recv_parsed.options[:observe] &&
31
+ ((recv_parsed.options[:observe] > observe_number && recv_parsed.options[:observe] - observe_number < MAX_OBSERVE_OPTION_VALUE) ||
32
+ (recv_parsed.options[:observe] < observe_number && observe_number - recv_parsed.options[:observe] > MAX_OBSERVE_OPTION_VALUE))
33
+
34
+ observe_callback.call(recv_parsed, recv_data)
35
+
36
+ end
37
+
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module Coap
2
+ VERSION = "0.0.13"
3
+ end
@@ -0,0 +1,17 @@
1
+ # hexdump.rb
2
+ # Copyright (C) 2010..2013 Carsten Bormann <cabo@tzi.org>
3
+
4
+ class String
5
+ def hexdump(prefix = '', out = STDOUT)
6
+ i = 0
7
+ while i < length
8
+ slice = byteslice(i, 16)
9
+ out.puts '%s%-48s |%-16s|' %
10
+ [prefix,
11
+ slice.bytes.map { |b| '%02x' % b.ord }.join(' '),
12
+ slice.gsub(/[^ -~]/mn, '.')]
13
+ i += 16
14
+ end
15
+ out
16
+ end
17
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require 'coap.rb'
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require_relative 'coap_test_helper'
3
+
4
+ ECONOTAG_PAYLOAD = Random.rand(999).to_s + 'TESTLorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligul'
5
+ ECONOTAG_SHORT_PAYLOAD = Random.rand(999).to_s + 'TESTLorem ipsum'
6
+
7
+ class TestMessage < Test::Unit::TestCase
8
+ def observe_tester(data, socket)
9
+ $observe_count += 1
10
+ end
11
+
12
+ def test_econotag_pr_b1_b2
13
+
14
+ client = CoAP::Client.new
15
+ client.max_payload = 48
16
+ answer = client.post('aaaa::60b1:10', 5683, '/r', ECONOTAG_PAYLOAD)
17
+ assert_equal(2, answer.mcode[0])
18
+ assert_equal(1, answer.mcode[1])
19
+ assert_equal(ECONOTAG_PAYLOAD, answer.payload)
20
+
21
+ client = CoAP::Client.new
22
+ client.max_payload = 48
23
+ answer = client.post('aaaa::60b1:10', 5683, '/r', ECONOTAG_SHORT_PAYLOAD)
24
+ assert_equal(ECONOTAG_SHORT_PAYLOAD, answer.payload)
25
+
26
+ end
27
+
28
+ end
29
+
30
+ # post
31
+ # mit payload und block1 oder ohne
32
+ # seperate
33
+ # block2 antwort
@@ -0,0 +1,311 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require_relative 'coap_test_helper'
3
+
4
+ # BAD: tests rely on working connection to coap.me (!)
5
+ # TODO: rewrite tests with local coap server
6
+ # test
7
+ $observe_count = 0
8
+ PAYLOAD = Random.rand(999).to_s + 'TESTLorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nuncLorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nuncTEST'
9
+
10
+ PAYLOAD_SONDERZEICHEN = Random.rand(999).to_s + 'TESTLorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla 超大航母 consequat massa quis enim. Donec pede justoidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nuncTEST'
11
+
12
+
13
+ class TestMessage < Test::Unit::TestCase
14
+ def observe_tester(data, socket)
15
+ $observe_count += 1
16
+ end
17
+
18
+ def test_client_get_v4_v6_hostname
19
+ # client = CoAP::Client.new
20
+ # answer = client.get('2001:638:708:30da:219:d1ff:fea4:abc5', 5683, '/hello')
21
+ # assert_equal(2,answer.mcode[0])
22
+ # assert_equal(5,answer.mcode[1])
23
+ # assert_equal('world',answer.payload)
24
+
25
+ client = CoAP::Client.new
26
+ answer = client.get('coap.me', 5683, '/hello')
27
+ assert_equal(2, answer.mcode[0])
28
+ assert_equal(5, answer.mcode[1])
29
+ assert_equal('world', answer.payload)
30
+
31
+ client = CoAP::Client.new
32
+ answer = client.get('134.102.218.16', 5683, '/hello')
33
+ assert_equal(2, answer.mcode[0])
34
+ assert_equal(5, answer.mcode[1])
35
+ assert_equal('world', answer.payload)
36
+ end
37
+
38
+ def test_client_404
39
+ # 404
40
+ client = CoAP::Client.new
41
+ answer = client.get('coap.me', 5683, '/hello-this-does-not-exist')
42
+ assert_equal(4, answer.mcode[0])
43
+ assert_equal(4, answer.mcode[1])
44
+ assert_equal('Not found', answer.payload)
45
+ end
46
+
47
+ def test_multi_request
48
+
49
+ x = Time.now
50
+ client = CoAP::Client.new
51
+ assert_raise RuntimeError do
52
+ answer = client.get('192.0.2.1', 5683, '/hello')
53
+ end
54
+
55
+ x = Time.now
56
+ assert_raise RuntimeError do
57
+ answer = client.get('192.0.2.1', 5683, '/hello')
58
+ end
59
+ duration = Time.now - x
60
+ #assert_operator duration, :>, 0
61
+ #assert_operator duration, :<, 5
62
+
63
+ end
64
+
65
+ def test_client_connection_dns_problems
66
+
67
+ # invalid hostname
68
+ client = CoAP::Client.new
69
+ assert_raise Resolv::ResolvError do
70
+ client.get('coapunknown.mexyz', 5683, '/hello')
71
+ end
72
+
73
+ end
74
+
75
+ def test_max_retransmit_ack_timeout
76
+
77
+ client = CoAP::Client.new(256, 0, 30)
78
+ duration = 0
79
+ x = Time.now
80
+ assert_raise RuntimeError do
81
+ answer = client.get('bongo.xmorpheus.de', 1194, '/hello')
82
+ end
83
+ duration = Time.now - x
84
+ assert_operator duration, :>, 25
85
+ assert_operator duration, :<, 35
86
+
87
+ client = CoAP::Client.new(256, 1, 30)
88
+ duration = 0
89
+ x = Time.now
90
+ assert_raise RuntimeError do
91
+ answer = client.get('bongo.xmorpheus.de', 1194, '/hello')
92
+ end
93
+ duration = Time.now - x
94
+ assert_operator duration, :>, 55
95
+ assert_operator duration, :<, 110
96
+
97
+ end
98
+
99
+ def test_client_arguments
100
+ # wrong port
101
+ client = CoAP::Client.new
102
+ assert_raise Errno::ECONNREFUSED do
103
+ client.get('coap.me', 15_683, '/hello')
104
+ end
105
+
106
+ # empty uri
107
+ client = CoAP::Client.new
108
+ assert_raise ArgumentError do
109
+ answer = client.get('coap.me', 5683, '')
110
+ end
111
+
112
+ # empty hostname
113
+ client = CoAP::Client.new
114
+ assert_raise ArgumentError do
115
+ answer = client.get('', 15_683, '/hello')
116
+ end
117
+
118
+ # missing port
119
+ client = CoAP::Client.new
120
+ assert_raise ArgumentError do
121
+ answer = client.get('coap.me', nil, '/hello')
122
+ end
123
+
124
+ # string port
125
+ client = CoAP::Client.new
126
+ assert_raise ArgumentError do
127
+ answer = client.get('coap.me', 'i m a sring', '/hello')
128
+ end
129
+
130
+ # empty payload
131
+ client = CoAP::Client.new
132
+ @suchEmpty = ''
133
+ assert_raise ArgumentError do
134
+ answer = client.post('coap.me', 5683, '/large-create', @suchEmpty)
135
+ end
136
+
137
+ end
138
+
139
+ def test_client_by_url
140
+ client = CoAP::Client.new
141
+ answer = client.get_by_url('coap://coap.me:5683/hello')
142
+ assert_equal('world', answer.payload)
143
+
144
+ # broken url
145
+ client = CoAP::Client.new
146
+ assert_raise ArgumentError do
147
+ answer = client.get_by_url('coap:/#/coap.me:5683/hello')
148
+ end
149
+ end
150
+
151
+ # need to test payload
152
+
153
+ def test_client_post
154
+ client = CoAP::Client.new
155
+ answer = client.post('coap.me', 5683, '/test', 'TD_COAP_CORE_04')
156
+ assert_equal(2, answer.mcode[0])
157
+ assert_equal(1, answer.mcode[1])
158
+ assert_equal('POST OK', answer.payload)
159
+ end
160
+
161
+ # basic test put
162
+ def test_client_put
163
+ client = CoAP::Client.new
164
+ answer = client.put('coap.me', 5683, '/test', 'TD_COAP_CORE_03')
165
+ assert_equal(2, answer.mcode[0])
166
+ assert_equal(4, answer.mcode[1])
167
+ assert_equal('PUT OK', answer.payload)
168
+ end
169
+
170
+ def test_client_delete
171
+ client = CoAP::Client.new
172
+ answer = client.delete('coap.me', 5683, '/test')
173
+ assert_equal(2, answer.mcode[0])
174
+ assert_equal(2, answer.mcode[1])
175
+ assert_equal('DELETE OK', answer.payload)
176
+ end
177
+
178
+ def test_client_sönderzeichen
179
+ client = CoAP::Client.new
180
+ answer = client.get_by_url('coap://coap.me/bl%C3%A5b%C3%A6rsyltet%C3%B8y')
181
+ assert_equal(2, answer.mcode[0])
182
+ assert_equal(5, answer.mcode[1])
183
+ assert_equal('Übergrößenträger = 特大の人 = 超大航母', answer.payload.force_encoding('utf-8'))
184
+ end
185
+
186
+ def test_client_custom_call
187
+ client = CoAP::Client.new
188
+ answer = client.custom('coap://coap.me/bl%C3%A5b%C3%A6rsyltet%C3%B8y', 'get')
189
+ assert_equal(2, answer.mcode[0])
190
+ assert_equal(5, answer.mcode[1])
191
+ assert_equal('Übergrößenträger = 特大の人 = 超大航母', answer.payload.force_encoding('utf-8'))
192
+ end
193
+
194
+ def test_multi_request_without_hostname_port
195
+
196
+ client = CoAP::Client.new
197
+ client.hostname = 'coap.me'
198
+ client.port = 5683
199
+ answer = client.get(nil, nil, '/hello')
200
+ assert_equal(2, answer.mcode[0])
201
+ assert_equal(5, answer.mcode[1])
202
+ assert_equal('world', answer.payload)
203
+
204
+ answer = client.get(nil, nil, '/hello')
205
+ assert_equal(2, answer.mcode[0])
206
+ assert_equal(5, answer.mcode[1])
207
+ assert_equal('world', answer.payload)
208
+
209
+ answer = client.get(nil, nil, '/hello')
210
+ assert_equal(2, answer.mcode[0])
211
+ assert_equal(5, answer.mcode[1])
212
+ assert_equal('world', answer.payload)
213
+
214
+ client = CoAP::Client.new
215
+ answer = client.get('coap.me', 5683, '/hello')
216
+ assert_equal(2, answer.mcode[0])
217
+ assert_equal(5, answer.mcode[1])
218
+ assert_equal('world', answer.payload)
219
+
220
+ answer = client.get(nil, nil, '/hello')
221
+ assert_equal(2, answer.mcode[0])
222
+ assert_equal(5, answer.mcode[1])
223
+ assert_equal('world', answer.payload)
224
+
225
+ answer = client.get(nil, nil, '/hello')
226
+ assert_equal(2, answer.mcode[0])
227
+ assert_equal(5, answer.mcode[1])
228
+ assert_equal('world', answer.payload)
229
+
230
+ end
231
+
232
+ def test_initialize
233
+ client = CoAP::Client.new(16, 4, 2, 'coap.me', 5683)
234
+ answer = client.get(nil, nil, '/large')
235
+ assert_equal(2, answer.mcode[0])
236
+ assert_equal(5, answer.mcode[1])
237
+ assert_equal(%Q'\n 0 1 2 3\n 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1\n +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n |Ver| T | TKL | Code | Message ID |\n +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n | Token (if any, TKL bytes) ...\n +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n | Options (if any) ...\n +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n |1 1 1 1 1 1 1 1| Payload (if any) ...\n +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n\n[...]\n\n Token Length (TKL): 4-bit unsigned integer. Indicates the length of\n the variable-length Token field (0-8 bytes). Lengths 9-15 are\n reserved, MUST NOT be sent, and MUST be processed as a message\n format error.\n\n Code: 8-bit unsigned integer, split into a 3-bit class (most\n significant bits) and a 5-bit detail (least significant bits),\n documented as c.dd where c is a digit from 0 to 7 for the 3-bit\n subfield and dd are two digits from 00 to 31 for the 5-bit\n subfield. The class can indicate a request (0), a success\n response (2), a client error response (4), or a server error\n response (5). (All other class values are reserved.) As a\n special case, Code 0.00 indicates an Empty message. In case of a\n request, the Code field indicates the Request Method; in case of a\n response a Response Code. Possible values are maintained in the\n CoAP Code Registries (Section 12.1). The semantics of requests\n and responses are defined in Section 5.\n\n', answer.payload)
238
+ end
239
+
240
+ def test_client_separate
241
+ client = CoAP::Client.new
242
+ answer = client.get('coap.me', 5683, '/separate')
243
+ assert_equal(2, answer.mcode[0])
244
+ assert_equal(5, answer.mcode[1])
245
+ assert_equal('That took a long time', answer.payload)
246
+ end
247
+
248
+ def test_client_observe
249
+ # basic observe test
250
+ client = CoAP::Client.new
251
+
252
+ t1 = Thread.new do
253
+ answer = client.observe('vs0.inf.ethz.ch', 5683, '/obs', method(:observe_tester))
254
+ end
255
+ sleep(10)
256
+ t1.kill
257
+
258
+ assert_operator $observe_count, :>, 2
259
+ end
260
+
261
+ def test_client_block2
262
+ # blockwise transfer (block2)
263
+ client = CoAP::Client.new
264
+ client.max_payload = 512
265
+ answer = client.get('coap.me', 5683, '/large')
266
+ assert_equal(2, answer.mcode[0])
267
+ assert_equal(5, answer.mcode[1])
268
+ assert_equal(%Q'\n 0 1 2 3\n 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1\n +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n |Ver| T | TKL | Code | Message ID |\n +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n | Token (if any, TKL bytes) ...\n +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n | Options (if any) ...\n +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n |1 1 1 1 1 1 1 1| Payload (if any) ...\n +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n\n[...]\n\n Token Length (TKL): 4-bit unsigned integer. Indicates the length of\n the variable-length Token field (0-8 bytes). Lengths 9-15 are\n reserved, MUST NOT be sent, and MUST be processed as a message\n format error.\n\n Code: 8-bit unsigned integer, split into a 3-bit class (most\n significant bits) and a 5-bit detail (least significant bits),\n documented as c.dd where c is a digit from 0 to 7 for the 3-bit\n subfield and dd are two digits from 00 to 31 for the 5-bit\n subfield. The class can indicate a request (0), a success\n response (2), a client error response (4), or a server error\n response (5). (All other class values are reserved.) As a\n special case, Code 0.00 indicates an Empty message. In case of a\n request, the Code field indicates the Request Method; in case of a\n response a Response Code. Possible values are maintained in the\n CoAP Code Registries (Section 12.1). The semantics of requests\n and responses are defined in Section 5.\n\n', answer.payload)
269
+ end
270
+
271
+ def test_client_block1
272
+ client = CoAP::Client.new
273
+ client.max_payload = 512
274
+ answer = client.post('coap.me', 5683, '/large-create', PAYLOAD)
275
+ assert_equal(2, answer.mcode[0])
276
+ assert_equal(1, answer.mcode[1])
277
+
278
+ client = CoAP::Client.new
279
+ client.max_payload = 512
280
+ answer = client.get('coap.me', 5683, '/large-create')
281
+ assert_equal(2, answer.mcode[0])
282
+ assert_equal(5, answer.mcode[1])
283
+ assert_equal(PAYLOAD, answer.payload)
284
+
285
+ client = CoAP::Client.new
286
+ client.max_payload = 512
287
+ answer = client.post('coap.me', 5683, '/large-create', PAYLOAD_SONDERZEICHEN)
288
+ assert_equal(2, answer.mcode[0])
289
+ assert_equal(1, answer.mcode[1])
290
+
291
+ client = CoAP::Client.new
292
+ client.max_payload = 512
293
+ answer = client.get('coap.me', 5683, '/large-create')
294
+ assert_equal(2, answer.mcode[0])
295
+ assert_equal(5, answer.mcode[1])
296
+ assert_equal(PAYLOAD_SONDERZEICHEN, answer.payload.force_encoding('utf-8'))
297
+
298
+ client = CoAP::Client.new
299
+ client.max_payload = 512
300
+ answer = client.put('coap.me', 5683, '/large-update', PAYLOAD)
301
+ assert_equal(2, answer.mcode[0])
302
+ assert_equal(4, answer.mcode[1])
303
+
304
+ client = CoAP::Client.new
305
+ client.max_payload = 512
306
+ answer = client.get('coap.me', 5683, '/large-update')
307
+ assert_equal(2, answer.mcode[0])
308
+ assert_equal(5, answer.mcode[1])
309
+ assert_equal(PAYLOAD, answer.payload)
310
+ end
311
+ end