|
| 1 | +import std/[streams, tables, unittest] |
| 2 | + |
| 3 | +import dnsprotocol |
| 4 | + |
| 5 | +const |
| 6 | + strQHeader = "\x00\x01\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00" |
| 7 | + strRHeader = "\x00\x01\x81\x80\x00\x01\x00\x02\x00\x00\x00\x00" |
| 8 | + strQuestion = "\x08\x6e\x69\x6d\x2d\x6c\x61\x6e\x67\x03\x6f\x72\x67\x00\x00\x01\x00\x01" |
| 9 | + strRResourceRecord1 = "\xc0\x0c\x00\x01\x00\x01\x00\x00\x01\x2c\x00\x04\xac\x43\x84\xf2" |
| 10 | + strRResourceRecord2 = "\xc0\x0c\x00\x01\x00\x01\x00\x00\x01\x2c\x00\x04\x68\x15\x05\x2a" |
| 11 | + |
| 12 | +suite "Query A IN": |
| 13 | + let |
| 14 | + header = initHeader(1'u16, QR.Query, OpCode.Query, false, false, true, false, RCode.NoError, 1'u16, 0'u16, 0'u16, 0'u16) |
| 15 | + question = initQuestion("nim-lang.org", QType.A, QClass.IN) |
| 16 | + |
| 17 | + test "Header for binary msg": |
| 18 | + var ss = newStringStream() |
| 19 | + |
| 20 | + toBinMsg(header, ss) |
| 21 | + |
| 22 | + check(ss.data == strQHeader) |
| 23 | + |
| 24 | + test "Question for binary msg": |
| 25 | + var |
| 26 | + dictionary = initTable[string, uint16]() |
| 27 | + ss = newStringStream() |
| 28 | + |
| 29 | + toBinMsg(question, ss, dictionary) |
| 30 | + |
| 31 | + check(ss.data == strQuestion) |
| 32 | + |
| 33 | + test "Message for binary msg UDP": |
| 34 | + let bmsg = toBinMsg(initMessage(header, @[question]), false) |
| 35 | + |
| 36 | + check(bmsg == (strQHeader & strQuestion)) |
| 37 | + |
| 38 | + test "Message for binary msg TCP": |
| 39 | + let bmsg = toBinMsg(initMessage(header, @[question]), true) |
| 40 | + |
| 41 | + check(bmsg == ("\x00\x1E" & strQHeader & strQuestion)) |
| 42 | + |
| 43 | +suite "Query Response A IN": |
| 44 | + let |
| 45 | + header = initHeader(1'u16, QR.Response, OpCode.Query, false, false, true, true, RCode.NoError, 1'u16, 2'u16, 0'u16, 0'u16) |
| 46 | + question = initQuestion("nim-lang.org", QType.A, QClass.IN) |
| 47 | + rr1 = initResourceRecord("nim-lang.org", Type.A, Class.IN, 300'i32, 4'u16, RDataA(address: [0xac'u8, 0x43, 0x84, 0xf2])) |
| 48 | + rr2 = initResourceRecord("nim-lang.org", Type.A, Class.IN, 300'i32, 4'u16, RDataA(address: [0x68'u8, 0x15, 0x05, 0x2a])) |
| 49 | + |
| 50 | + var |
| 51 | + dictionary = initTable[string, uint16]() |
| 52 | + ss = newStringStream() |
| 53 | + |
| 54 | + test "Header for binary msg": |
| 55 | + toBinMsg(header, ss) |
| 56 | + |
| 57 | + check(ss.data == strRHeader) |
| 58 | + |
| 59 | + toBinMsg(question, ss, dictionary) |
| 60 | + |
| 61 | + test "Resource Record for binary msg": |
| 62 | + let startRR = getPosition(ss) |
| 63 | + |
| 64 | + toBinMsg(rr1, ss, dictionary) |
| 65 | + toBinMsg(rr2, ss, dictionary) |
| 66 | + |
| 67 | + check(ss.data[startRR..^1] == (strRResourceRecord1 & strRResourceRecord2)) |
| 68 | + |
| 69 | + test "Message for binary msg UDP": |
| 70 | + let bmsg = toBinMsg(initMessage(header, @[question], @[rr1, rr2]), false) |
| 71 | + |
| 72 | + check(bmsg == (strRHeader & strQuestion & strRResourceRecord1 & strRResourceRecord2)) |
| 73 | + |
| 74 | + test "Message for binary msg TCP": |
| 75 | + let bmsg = toBinMsg(initMessage(header, @[question], @[rr1, rr2]), true) |
| 76 | + |
| 77 | + check(bmsg == ("\x00\x3E" & strRHeader & strQuestion & strRResourceRecord1 & strRResourceRecord2)) |
0 commit comments