diff --git a/.github/workflows/JuliaFormatter.yml b/.github/workflows/JuliaFormatter.yml new file mode 100644 index 0000000..916c15c --- /dev/null +++ b/.github/workflows/JuliaFormatter.yml @@ -0,0 +1,47 @@ +name: Code Formatting + +on: + pull_request: + branches: + - main + +jobs: + format: + runs-on: ubuntu-latest + + permissions: + contents: write + pull-requests: write + actions: write + + steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.12.1 + + - uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + + - uses: julia-actions/setup-julia@v2 + with: + version: 1.9 + + - name: Install JuliaFormatter and format + shell: bash + run: julia -e 'import Pkg; Pkg.add("JuliaFormatter"); using JuliaFormatter; format(".")' + + - name: Create Pull Request + id: pr + uses: peter-evans/create-pull-request@v6 + with: + commit-message: Format files using JuliaFormatter + title: ${{ format('[AUTO] Format {0} using JuliaFormatter', github.event.pull_request.number) }} + body: ${{ format('[JuliaFormatter.jl](https://github.com/domluna/JuliaFormatter.jl) would suggest these formatting changes against \#{0}.', github.event.pull_request.number) }} + labels: no changelog + branch: ${{ format('code-format/{0}', github.event.pull_request.number) }} + delete-branch: true + + - name: Fail if a PR was needed + if: ${{ steps.pr.outputs.pull-request-operation == 'created' || steps.pr.outputs.pull-request-operation == 'updated' }} + shell: bash + run: exit 1 diff --git a/test/dev/mocksocket.jl b/test/dev/mocksocket.jl deleted file mode 100644 index 823c58e..0000000 --- a/test/dev/mocksocket.jl +++ /dev/null @@ -1,135 +0,0 @@ -using Sockets: Sockets -import Base: read, close - -# commands -const CONNECT = 0x10 -const CONNACK = 0x20 -const PUBLISH = 0x30 -const PUBACK = 0x40 -const PUBREC = 0x50 -const PUBREL = 0x60 -const PUBCOMP = 0x70 -const SUBSCRIBE = 0x80 -const SUBACK = 0x90 -const UNSUBSCRIBE = 0xA0 -const UNSUBACK = 0xB0 -const PINGREQ = 0xC0 -const PINGRESP = 0xD0 -const DISCONNECT = 0xE0 -const QOS_1_BYTE = 0x02 -const QOS_2_BYTE = 0x04 - -mutable struct TestFileHandler <: IO - out_channel::Channel{UInt8} - in_channel::Channel{UInt8} - closenotify::Condition - new_input::Condition - closed::Bool -end - -function read(fh::TestFileHandler, t) - return take!(fh.in_channel) -end - -function read(fh::TestFileHandler, t::Type{UInt8}) - return take!(fh.in_channel) -end - -function read(fh::TestFileHandler, length::Integer) - data = Vector{UInt8}() - for i in 1:length - append!(data, take!(fh.in_channel)) - end - return data -end - -function close(fh::TestFileHandler) - fh.closed = true - return notify(fh.closenotify) -end - -function write(fh::TestFileHandler, data::UInt8) - return put!(fh.out_channel, data) -end - -function put(fh::TestFileHandler, data::Array{UInt8}) - for i in data - put!(fh.in_channel, i) - end -end - -function put(fh::TestFileHandler, data::SubArray) - for i in data - put!(fh.in_channel, i) - end -end - -function put(fh::TestFileHandler, data::UInt16) - buffer = PipeBuffer() - write(buffer, data) - return put(fh, reverse(read(buffer), 1)) #TODO use take! instead? -end - -function put(fh::TestFileHandler, data::UInt8) - return put!(fh.in_channel, data) -end - -function put_from_file(fh::TestFileHandler, filename) - return put(fh, read_all_to_arr(filename)) -end - -function get_mid_index(data::Array{UInt8}) - cmd = data[1] & 0xF0 - if cmd == CONNACK || - cmd == PINGRESP || - cmd == CONNECT || - cmd == DISCONNECT || - cmd == PINGREQ - return -1 - elseif cmd == PUBLISH - qos = data[1] & 0x06 - if qos == QOS_1_BYTE || qos == QOS_2_BYTE - buffer = PipeBuffer() - write(buffer, data[4]) - write(buffer, data[3]) - topic_len = read_len(buffer) - return 5 + topic_len - else - # QOS_0 has no message id - return -1 - end - else - # all other packets have m_id as their 3rd and 4th byte - return 3 - end -end - -function put_from_file(fh::TestFileHandler, filename, messageId::UInt16) - data = read_all_to_arr(filename) - mid_index = get_mid_index(data) - if mid_index > 0 - put(fh, view(data, 1:(mid_index - 1))) - put(fh, messageId) - put(fh, view(data, (mid_index + 2):length(data))) - else - put(fh, data) - end -end - -function read_all_to_arr(filename) - file = open(filename, "r") - data = Vector{UInt8}() - while !eof(file) - append!(data, read(file, UInt8)) - end - close(file) - return data -end - -function Sockets.connect(host::AbstractString, port::Integer) - th = TestFileHandler( - Channel{UInt8}(256), Channel{UInt8}(256), Condition(), Condition(), false - ) - put_from_file(th, "data/input/connack.dat") - return th -end diff --git a/test/dev/packet.jl b/test/dev/packet.jl deleted file mode 100644 index 13046b9..0000000 --- a/test/dev/packet.jl +++ /dev/null @@ -1,146 +0,0 @@ -const TOPIC_STRING = "foo" -const MESSAGE_STRING = "bar" - -function on_msg(topic, payload) - msg = p |> String - @info "Received message topic: [", topic, "] payload: [", msg, "]" - @test topic == TOPIC_STRING - @test msg == MESSAGE_STRING -end - -function is_out_correct(filename_expected::AbstractString, actual::Channel{UInt8}, mid::UInt16) - file_data = read_all_to_arr(filename_expected) - actual_data = Vector{UInt8}() - - for i in file_data - append!(actual_data, take!(actual)) - end - - mid_index = get_mid_index(file_data) - if mid_index > 0 - buffer = PipeBuffer() - write(buffer, mid) - converted_mid = take!(buffer) - file_data[mid_index] = converted_mid[2] - file_data[mid_index+1] = converted_mid[1] - end - - correct = true - i = 1 - while i <= length(file_data) - if file_data[i] != actual_data[i] - correct = false - break - end - i += 1 - end - return correct -end - -function is_out_correct(filename_expected::AbstractString, actual::Channel{UInt8}) - file = open(filename_expected, "r") - correct = true - while !eof(file) - if read(file, UInt8) != take!(actual) - correct = false - break - end - end - return correct -end - -@testset verbose=true "Running packet tests" begin - -client = Client(on_msg) -last_id::UInt16 = 0x0001 - -@testset "Testing connect" begin - connect(client, "test.mosquitto.org", 1883) - tfh::TestFileHandler = client.socket - @test is_out_correct("data/output/connect.dat", tfh.out_channel) - # CONNACK is automatically being sent in connect call -end - -@testset "Testing subscribe" begin - subscribe_async(client, (TOPIC_STRING, QOS_1), ("cba", QOS_0)) - put_from_file(tfh, "data/input/suback.dat", client.last_id) - @test is_out_correct("data/output/subreq.dat", tfh.out_channel, client.last_id) -end - -@testset "Testing unsubscribe" begin - unsubscribe_async(client, TOPIC_STRING, "cba") - put_from_file(tfh, "data/input/unsuback.dat", client.last_id) - @test is_out_correct("data/output/unsubreq.dat", tfh.out_channel, client.last_id) -end - -@testset "Testing receive publish QOS 0" begin - put_from_file(tfh, "data/input/qos0pub.dat") - @test is_out_correct("data/output/puback.dat", tfh.out_channel, last_id) -end - -@testset "Testing receive publish QOS 1" begin - put_from_file(tfh, "data/input/qos1pub.dat", last_id) - @test is_out_correct("data/output/puback.dat", tfh.out_channel, last_id) - #last_id += 1 -end - -@testset "Testing receive publish QOS 2" begin - put_from_file(tfh, "data/input/qos2pub.dat", last_id) - @test is_out_correct("data/output/pubrec.dat", tfh.out_channel, last_id) - put_from_file(tfh, "data/input/pubrel.dat", last_id) - @test is_out_correct("data/output/pubcomp.dat", tfh.out_channel, last_id) - #last_id += 1 -end - -@testset "Testing send publish QOS 0" begin - publish_async(client, "test1", "QOS_0", qos=QOS_0) - @test is_out_correct("data/output/qos0pub.dat", tfh.out_channel) -end - -@testset "Testing send publish QOS 1" begin - publish_async(client, "test2", "QOS_1", qos=QOS_1) - put_from_file(tfh, "data/input/puback.dat", client.last_id) - @test is_out_correct("data/output/qos1pub.dat", tfh.out_channel, client.last_id) -end - -@testset "Testing send publish QOS 2" begin - publish_async(client, "test3", "test", qos=QOS_2) - @test is_out_correct("data/output/qos2pub.dat", tfh.out_channel, client.last_id) - put_from_file(tfh, "data/input/pubrec.dat", client.last_id) - @test is_out_correct("data/output/pubrel.dat", tfh.out_channel, client.last_id) - put_from_file(tfh, "data/input/pubcomp.dat", client.last_id) -end - -@testset "Testing disconnect" begin - disconnect(client) - @test is_out_correct("data/output/disco.dat", tfh.out_channel) -end - - #This has to be in it's own connect flow to not interfere with other messages -@testset "Testing keep alive with response" begin - client = Client(on_msg) - - client.ping_timeout = 1 - connect(client, "test.mosquitto.org", 1883, client_id="TestID", keep_alive=1) - tfh = client.socket - @test is_out_correct("data/output/connect_keep_alive1s.dat", tfh.out_channel) # Consume output - @test is_out_correct("data/output/pingreq.dat", tfh.out_channel) - put_from_file(tfh, "data/input/pingresp.dat") -end - -@testset "Testing keep alive without response" begin - sleep(1.1) - @test is_out_correct("data/output/pingreq.dat", tfh.out_channel) - @test is_out_correct("data/output/disco.dat", tfh.out_channel) - - info("Testing unwanted pingresp") - client = Client(on_msg) - connect(client, "test.mosquitto.org", 1883, client_id="TestID", keep_alive=15) - tfh = client.socket - put_from_file(tfh, "data/input/pingresp.dat") - sleep(0.1) - @test tfh.closed -end -end - -end \ No newline at end of file