spec/net_websocket_frames_spec.lua
author Kim Alvefur <zash@zash.se>
Thu, 02 Jan 2020 13:17:03 +0100
changeset 10587 624ad69dbaf7
parent 9664 7e75c348095b
child 10588 0c464bb7eb03
permissions -rw-r--r--
net.websocket.frames: Add test case for masked data ASCI is pretty neat in how lower case alphabet XOR space is upper case
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9664
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
describe("net.websocket.frames", function ()
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
	local nwf = require "net.websocket.frames";
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
	local test_frames = {
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
		simple_empty = {
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
			["opcode"] = 0;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
			["length"] = 0;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
			["data"] = "";
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
			["FIN"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
			["MASK"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
			["RSV1"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
			["RSV2"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
			["RSV3"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
		};
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
		simple_data = {
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
			["opcode"] = 0;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
			["length"] = 5;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
			["data"] = "hello";
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
			["FIN"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
			["MASK"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
			["RSV1"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
			["RSV2"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
			["RSV3"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
		};
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
		simple_fin = {
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
			["opcode"] = 0;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
			["length"] = 0;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
			["data"] = "";
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
			["FIN"] = true;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
			["MASK"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
			["RSV1"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    32
			["RSV2"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
			["RSV3"] = false;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
		};
10587
624ad69dbaf7 net.websocket.frames: Add test case for masked data
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
    35
		masked_data = {
624ad69dbaf7 net.websocket.frames: Add test case for masked data
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
    36
			["opcode"] = 0;
624ad69dbaf7 net.websocket.frames: Add test case for masked data
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
    37
			["length"] = 5;
624ad69dbaf7 net.websocket.frames: Add test case for masked data
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
    38
			["data"] = "hello";
624ad69dbaf7 net.websocket.frames: Add test case for masked data
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
    39
			["FIN"] = true;
624ad69dbaf7 net.websocket.frames: Add test case for masked data
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
    40
			["MASK"] = true;
624ad69dbaf7 net.websocket.frames: Add test case for masked data
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
    41
			["RSV1"] = false;
624ad69dbaf7 net.websocket.frames: Add test case for masked data
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
    42
			["RSV2"] = false;
624ad69dbaf7 net.websocket.frames: Add test case for masked data
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
    43
			["RSV3"] = false;
624ad69dbaf7 net.websocket.frames: Add test case for masked data
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
    44
			["key"] = { 0x20, 0x20, 0x20, 0x20, };
624ad69dbaf7 net.websocket.frames: Add test case for masked data
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
    45
		};
9664
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    46
	}
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    47
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    48
	describe("build", function ()
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    49
		local build = nwf.build;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    50
		it("works", function ()
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    51
			assert.equal("\0\0", build(test_frames.simple_empty));
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    52
			assert.equal("\0\5hello", build(test_frames.simple_data));
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    53
			assert.equal("\128\0", build(test_frames.simple_fin));
10587
624ad69dbaf7 net.websocket.frames: Add test case for masked data
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
    54
			assert.equal("\128\133    HELLO", build(test_frames.masked_data));
9664
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    55
		end);
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    56
	end);
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    57
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    58
	describe("parse", function ()
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    59
		local parse = nwf.parse;
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    60
		it("works", function ()
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    61
			assert.same(test_frames.simple_empty, parse("\0\0"));
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    62
			assert.same(test_frames.simple_data, parse("\0\5hello"));
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    63
			assert.same(test_frames.simple_fin, parse("\128\0"));
10587
624ad69dbaf7 net.websocket.frames: Add test case for masked data
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
    64
			assert.same(test_frames.masked_data, parse("\128\133    HELLO"));
9664
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    65
		end);
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    66
	end);
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    67
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    68
end);
7e75c348095b net.websocket.frames: Add some brief tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    69