<commit_before>$:.unshift(File.dirname(__FILE__))

require 'spec_helper'
require 'mqtt'

describe EventMachine::MQTTS::Packet::Connect do
  it "should have the right type id" do
    packet = EventMachine::MQTTS::Packet::Connect.new
    packet.type_id.should == 0x04
  end

  describe "when serialising a packet" do
    it "should output the correct bytes for a packet with no flags" do
      packet = EventMachine::MQTTS::Packet::Connect.new(
        :client_id => 'mqtts-client-pub'
      )
      packet.to_s.should == "\026\004\004\001\000\017mqtts-client-pub"
    end

    it "should output the correct bytes for a packet with clean session turned off" do
      packet = EventMachine::MQTTS::Packet::Connect.new(
        :client_id => 'myclient',
        :clean_session => false
      )
      packet.to_s.should == "\016\004\000\001\000\017myclient"
    end

    it "should throw an exception when there is no client identifier" do
      lambda {
        EventMachine::MQTTS::Packet::Connect.new.to_s
      }.should raise_error(
        'Invalid client identifier when serialising packet'
      )
    end

    it "should output the correct bytes for a packet with a will request" do
      packet = EventMachine::MQTTS::Packet::Connect.new(
        :client_id => 'myclient',
        :request_will => true,
        :clean_session => true
      )
      packet.to_s.should == "\016\004\014\001\000\017myclient"
    end

    it "should output the correct bytes for with a custom keep alive" do
      packet = EventMachine::MQTTS::Packet::Connect.new(
        :client_id => 'myclient',
        :request_will => true,
        :clean_session => true,
        :keep_alive => 30
      )
      packet.to_s.should == "\016\004\014\001\000\036myclient"
    end
  end

  describe "when parsing a simple Connect packet" do
    before(:each) do
      @packet = EventMachine::MQTTS::Packet.parse(
        "\026\004\004\001\000\000mqtts-client-pub"
      )
    end

    it "should correctly create the right type of packet object" do
      @packet.class.should == EventMachine::MQTTS::Packet::Connect
    end

    it "should not have the request will flag set" do
      @packet.request_will.should be_false
    end

    it "shoul have the clean session flag set" do
      @packet.clean_session.should be_true
    end

    it "should set the Keep Alive timer of the packet correctly" do
      @packet.keep_alive.should == 0
    end

    it "should set the Client Identifier of the packet correctly" do
      @packet.client_id.should == 'mqtts-client-pub'
    end
  end

  describe "when parsing a Connect packet with the clean session flag set" do
    before(:each) do
      @packet = EventMachine::MQTTS::Packet.parse(
        "\016\004\004\001\000\017myclient"
      )
    end

    it "should set the clean session flag" do
      @packet.clean_session.should be_true
    end
  end

  describe "when parsing a Connect packet with the will request flag set" do
    before(:each) do
      @packet = EventMachine::MQTTS::Packet.parse(
        "\016\004\014\001\000\017myclient"
      )
    end

    it "should correctly create the right type of packet object" do
      @packet.class.should == EventMachine::MQTTS::Packet::Connect
    end
    it "should set the Client Identifier of the packet correctly" do
      @packet.client_id.should == 'myclient'
    end

    it "should set the clean session flag should be set" do
      @packet.clean_session.should be_true
    end

    it "should set the Will retain flag should be false" do
      @packet.request_will.should be_true
    end
  end

  context "that has an invalid type identifier" do
    it "should throw an exception" do
      lambda {
        EventMachine::MQTTS::Packet.parse( "\x02\xFF" )
      }.should raise_error(
        EventMachine::MQTTS::ProtocolException,
        "Invalid packet type identifier: 255"
      )
    end
  end
end

describe EventMachine::MQTTS::Packet::Connack do
  it "should have the right type id" do
    packet = EventMachine::MQTTS::Packet::Connack.new
    packet.type_id.should == 0x05
  end

  describe "when serialising a packet" do
    it "should output the correct bytes for a sucessful connection acknowledgement packet" do
      packet = EventMachine::MQTTS::Packet::Connack.new( :return_code => 0x00 )
      packet.to_s.should == "\x03\x05\x00"
    end
  end
<commit_msg>Added some extra tests for error conditions when handling packets<commit_after>$:.unshift(File.dirname(__FILE__))

require 'spec_helper'

describe EventMachine::MQTTS::Packet do

  describe "when creating a new packet" do
    it "should allow you to set the packet dup flag as a hash parameter" do
      packet = EventMachine::MQTTS::Packet.new( :duplicate => true )
      packet.duplicate.should be_true
    end

    it "should allow you to set the packet QOS level as a hash parameter" do
      packet = EventMachine::MQTTS::Packet.new( :qos => 2 )
      packet.qos.should == 2
    end

    it "should allow you to set the packet retain flag as a hash parameter" do
      packet = EventMachine::MQTTS::Packet.new( :retain => true )
      packet.retain.should be_true
    end
  end
  
  describe "getting the type id on a un-subclassed packet" do
    it "should throw an exception" do
      lambda {
        EventMachine::MQTTS::Packet.new.type_id
      }.should raise_error(
        RuntimeError,
        "Invalid packet type: EventMachine::MQTTS::Packet"
      )
    end
  end

  describe "Parsing a packet that does not match the packet length" do
    it "should throw an exception" do
      lambda {
        packet = EventMachine::MQTTS::Packet.parse("\x02\x1834567")
      }.should raise_error(
        EventMachine::MQTTS::ProtocolException,
        "Length of packet is not the same as the length header"
      )
    end  
  end  

end


describe EventMachine::MQTTS::Packet::Connect do
  it "should have the right type id" do
    packet = EventMachine::MQTTS::Packet::Connect.new
    packet.type_id.should == 0x04
  end

  describe "when serialising a packet" do
    it "should output the correct bytes for a packet with no flags" do
      packet = EventMachine::MQTTS::Packet::Connect.new(
        :client_id => 'mqtts-client-pub'
      )
      packet.to_s.should == "\026\004\004\001\000\017mqtts-client-pub"
    end

    it "should output the correct bytes for a packet with clean session turned off" do
      packet = EventMachine::MQTTS::Packet::Connect.new(
        :client_id => 'myclient',
        :clean_session => false
      )
      packet.to_s.should == "\016\004\000\001\000\017myclient"
    end

    it "should throw an exception when there is no client identifier" do
      lambda {
        EventMachine::MQTTS::Packet::Connect.new.to_s
      }.should raise_error(
        'Invalid client identifier when serialising packet'
      )
    end

    it "should output the correct bytes for a packet with a will request" do
      packet = EventMachine::MQTTS::Packet::Connect.new(
        :client_id => 'myclient',
        :request_will => true,
        :clean_session => true
      )
      packet.to_s.should == "\016\004\014\001\000\017myclient"
    end

    it "should output the correct bytes for with a custom keep alive" do
      packet = EventMachine::MQTTS::Packet::Connect.new(
        :client_id => 'myclient',
        :request_will => true,
        :clean_session => true,
        :keep_alive => 30
      )
      packet.to_s.should == "\016\004\014\001\000\036myclient"
    end
  end

  describe "when parsing a simple Connect packet" do
    before(:each) do
      @packet = EventMachine::MQTTS::Packet.parse(
        "\026\004\004\001\000\000mqtts-client-pub"
      )
    end

    it "should correctly create the right type of packet object" do
      @packet.class.should == EventMachine::MQTTS::Packet::Connect
    end

    it "should not have the request will flag set" do
      @packet.request_will.should be_false
    end

    it "shoul have the clean session flag set" do
      @packet.clean_session.should be_true
    end

    it "should set the Keep Alive timer of the packet correctly" do
      @packet.keep_alive.should == 0
    end

    it "should set the Client Identifier of the packet correctly" do
      @packet.client_id.should == 'mqtts-client-pub'
    end
  end

  describe "when parsing a Connect packet with the clean session flag set" do
    before(:each) do
      @packet = EventMachine::MQTTS::Packet.parse(
        "\016\004\004\001\000\017myclient"
      )
    end

    it "should set the clean session flag" do
      @packet.clean_session.should be_true
    end
  end

  describe "when parsing a Connect packet with the will request flag set" do
    before(:each) do
      @packet = EventMachine::MQTTS::Packet.parse(
        "\016\004\014\001\000\017myclient"
      )
    end

    it "should correctly create the right type of packet object" do
      @packet.class.should == EventMachine::MQTTS::Packet::Connect
    end
    it "should set the Client Identifier of the packet correctly" do
      @packet.client_id.should == 'myclient'
    end

    it "should set the clean session flag should be set" do
      @packet.clean_session.should be_true
    end

    it "should set the Will retain flag should be false" do
      @packet.request_will.should be_true
    end
  end

  context "that has an invalid type identifier" do
    it "should throw an exception" do
      lambda {
        EventMachine::MQTTS::Packet.parse( "\x02\xFF" )
      }.should raise_error(
        EventMachine::MQTTS::ProtocolException,
        "Invalid packet type identifier: 255"
      )
    end
  end

  describe "when parsing a Connect packet an unsupport protocol ID" do
    it "should throw an exception" do
      lambda {
        packet = EventMachine::MQTTS::Packet.parse(
          "\016\004\014\005\000\017myclient"
        )
      }.should raise_error(
        EventMachine::MQTTS::ProtocolException,
        "Unsupported protocol ID number: 5"
      )
    end
  end
end

describe EventMachine::MQTTS::Packet::Connack do
  it "should have the right type id" do
    packet = EventMachine::MQTTS::Packet::Connack.new
    packet.type_id.should == 0x05
  end

  describe "when serialising a packet" do
    it "should output the correct bytes for a sucessful connection acknowledgement packet" do
      packet = EventMachine::MQTTS::Packet::Connack.new( :return_code => 0x00 )
      packet.to_s.should == "\x03\x05\x00"
    end
  end

{'fraction_non_alphanumeric': 0.09329110279297437, 'fraction_numerical': 0.03148094826758806, 'mean_word_length': 2.745506829618979, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3369319', 'n_tokens_mistral': 3414, 'n_tokens_neox': 3084, 'n_words': 994}