summaryrefslogtreecommitdiffstats
path: root/netwerk/test/gtest/TestDNSPacket.cpp
blob: 49530b80e00626929bd04817602c8fd1b2aeb5f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "gtest/gtest.h"

#include "mozilla/net/DNSPacket.h"
#include "mozilla/Preferences.h"

using namespace mozilla;
using namespace mozilla::net;

void AssertDnsPadding(uint32_t PaddingLength, unsigned int WithPadding,
                      unsigned int WithoutPadding, bool DisableEcn,
                      const nsCString& host) {
  DNSPacket encoder;
  nsCString buf;

  ASSERT_EQ(Preferences::SetUint("network.trr.padding.length", PaddingLength),
            NS_OK);

  ASSERT_EQ(Preferences::SetBool("network.trr.padding", true), NS_OK);
  ASSERT_EQ(encoder.EncodeRequest(buf, host, 1, DisableEcn), NS_OK);
  ASSERT_EQ(buf.Length(), WithPadding);

  ASSERT_EQ(Preferences::SetBool("network.trr.padding", false), NS_OK);
  ASSERT_EQ(encoder.EncodeRequest(buf, host, 1, DisableEcn), NS_OK);
  ASSERT_EQ(buf.Length(), WithoutPadding);
}

TEST(TestDNSPacket, PaddingLenEcn)
{
  AssertDnsPadding(16, 48, 41, true, "a.de"_ns);
  AssertDnsPadding(16, 48, 42, true, "ab.de"_ns);
  AssertDnsPadding(16, 48, 43, true, "abc.de"_ns);
  AssertDnsPadding(16, 48, 44, true, "abcd.de"_ns);
  AssertDnsPadding(16, 64, 45, true, "abcde.de"_ns);
  AssertDnsPadding(16, 64, 46, true, "abcdef.de"_ns);
  AssertDnsPadding(16, 64, 47, true, "abcdefg.de"_ns);
  AssertDnsPadding(16, 64, 48, true, "abcdefgh.de"_ns);
}

TEST(TestDNSPacket, PaddingLenDisableEcn)
{
  AssertDnsPadding(16, 48, 22, false, "a.de"_ns);
  AssertDnsPadding(16, 48, 23, false, "ab.de"_ns);
  AssertDnsPadding(16, 48, 24, false, "abc.de"_ns);
  AssertDnsPadding(16, 48, 25, false, "abcd.de"_ns);
  AssertDnsPadding(16, 48, 26, false, "abcde.de"_ns);
  AssertDnsPadding(16, 48, 27, false, "abcdef.de"_ns);
  AssertDnsPadding(16, 48, 32, false, "abcdefghijk.de"_ns);
  AssertDnsPadding(16, 48, 33, false, "abcdefghijkl.de"_ns);
  AssertDnsPadding(16, 64, 34, false, "abcdefghijklm.de"_ns);
  AssertDnsPadding(16, 64, 35, false, "abcdefghijklmn.de"_ns);
}

TEST(TestDNSPacket, PaddingLengths)
{
  AssertDnsPadding(0, 45, 41, true, "a.de"_ns);
  AssertDnsPadding(1, 45, 41, true, "a.de"_ns);
  AssertDnsPadding(2, 46, 41, true, "a.de"_ns);
  AssertDnsPadding(3, 45, 41, true, "a.de"_ns);
  AssertDnsPadding(4, 48, 41, true, "a.de"_ns);
  AssertDnsPadding(16, 48, 41, true, "a.de"_ns);
  AssertDnsPadding(32, 64, 41, true, "a.de"_ns);
  AssertDnsPadding(42, 84, 41, true, "a.de"_ns);
  AssertDnsPadding(52, 52, 41, true, "a.de"_ns);
  AssertDnsPadding(80, 80, 41, true, "a.de"_ns);
  AssertDnsPadding(128, 128, 41, true, "a.de"_ns);
  AssertDnsPadding(256, 256, 41, true, "a.de"_ns);
  AssertDnsPadding(1024, 1024, 41, true, "a.de"_ns);
  AssertDnsPadding(1025, 1024, 41, true, "a.de"_ns);
}