Commit Graph

94 Commits

Author SHA1 Message Date
Miek Gieben 045ac4ec6c
Fix RSAMD5 keytag calucation. (#1353)
Of course the wording was changed (for the better) in an errata:
https://www.rfc-editor.org/errata/eid193

We still followed the original RFC4034 text. Note I haven't given this
much thought, just changed the 2 into a 3 and ran the test.

Fixes: #1352

Signed-off-by: Miek Gieben <miek@miek.nl>
2022-04-01 14:01:47 +02:00
cesarkuroiwa a614451ab3
Use ed25519 from Go standard lib (#1243)
* Use "crypto/ed25519"

* Remove unused dependencies

* Version bump

Co-authored-by: Cesar Kuroiwa <cesar@registro.br>
2021-03-16 15:37:10 +01:00
Shubhendra Singh Chauhan 2f14d104f3
improve code quality (#1228)
* Combine multiple `append`s into a single call

* Fix Yoda conditions

* Fix check for empty string

* revert "combine multiple `append`s"
2021-02-25 17:01:55 +01:00
Miek Gieben 3b0ffe413f
tests: reduce timeouts and iterations (#1175)
This reduces the time it takes to run the test. Shorter timeouts on
clients to avoid awaiting for the detault timeouts.

It's also reduces the iterations in some test functions, this doesn't
seem to impact the tests indicating those numbers where random to begin
with.

Use shorter crypto keys, as we don't need to strength in tests.

Stop using Google Public DNS and other remotes in tests as well: it's
faster, keeps things local and avoids spilling info to Google.

This brings the test duration down from ~8s to ~2s on my machine, a 4x
reduction.

~~~
PASS
ok  	github.com/miekg/dns	2.046s

Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
PASS
ok  	github.com/miekg/dns	7.915s
~~~

Signed-off-by: Miek Gieben <miek@miek.nl>
2020-10-16 09:10:36 +02:00
Tom Thorogood 17c1bc6792
Eliminate lexer goroutines (#792)
* Eliminate zlexer goroutine

This replaces the zlexer goroutine and channels with a zlexer struct
that maintains state and provides a channel-like API.

* Eliminate klexer goroutine

This replaces the klexer goroutine and channels with a klexer struct
that maintains state and provides a channel-like API.

* Merge scan into zlexer and klexer

This does result in tokenText existing twice, but it's pretty simple
and small so it's not that bad.

* Avoid using text/scanner.Position to track position

* Track escape within zlexer.Next

* Avoid zl.commt check on space and tab in zlexer

* Track stri within zlexer.Next

* Track comi within zlexer.Next

There is one special case at the start of a comment that needs to be
handled, otherwise this is as simple as stri was.

* Use a single token buffer in zlexer

This is safe as there is never both a non-empty string buffer and a
non-empty comment buffer.

* Don't hardcode length of zl.tok in zlexer

* Eliminate lex.length field

This is always set to len(l.token) and is only queried in a few places.

It was added in 47cc5b052d without any
obvious need.

* Add whitespace to klexer.Next

* Track lex within klexer.Next

* Use a strings.Builder in klexer.Next

* Simplify : case in klexer.Next

* Add whitespace to zlexer.Next

* Change for loop style in zlexer.Next and klexer.Next

* Surface read errors in zlexer

* Surface read errors from klexer

* Remove debug line from parseKey

* Rename tokenText to readByte

* Make readByte return ok bool

Also change the for loop style to match the Next for loops.

* Make readByte errors sticky

klexer.Next calls readByte separately from within the loop. Without
readByte being sticky, an error that occurs during that readByte call
may be lost.

* Panic in testRR if the error is non-nil

* Add whitespace and unify field setting in zlexer.Next

* Remove eof fields from zlexer and klexer

With readByte having sticky errors, this no longer needed. zl.eof = true
was also in the wrong place and could mask an unbalanced brace error.

* Merge zl.tok blocks in zlexer.Next

* Split the tok buffer into separate string and comment buffers

The invariant of stri > 0 && comi > 0 never being true was broken when
x == '\n' && !zl.quote && zl.commt && zl.brace != 0 (the
"If not in a brace this ends the comment AND the RR" block).

Split the buffer back out into two separate buffers to avoid clobbering.

* Replace token slices with arrays in zlexer

* Add a NewRR benchmark

* Move token buffers into zlexer.Next

These don't need to be retained across Next calls and can be stack
allocated inside Next. This drastically reduces memory consumption as
they accounted for nearly half of all the memory used.

name      old alloc/op   new alloc/op   delta
NewRR-12    9.72kB ± 0%    4.98kB ± 0%  -48.72%  (p=0.000 n=10+10)

* Add a ReadRR benchmark

Unlike NewRR, this will use an io.Reader that does not implement any
methods aside from Read. In particular it does not implement
io.ByteReader.

* Avoid using a bufio.Reader for io.ByteReader readers

At the same time use a smaller buffer size of 1KiB rather than the
bufio.NewReader default of 4KiB.

name       old time/op    new time/op    delta
NewRR-12     11.0µs ± 3%     9.5µs ± 2%  -13.77%  (p=0.000 n=9+10)
ReadRR-12    11.2µs ±16%     9.8µs ± 1%  -13.03%  (p=0.000 n=10+10)

name       old alloc/op   new alloc/op   delta
NewRR-12     4.98kB ± 0%    0.81kB ± 0%  -83.79%  (p=0.000 n=10+10)
ReadRR-12    4.87kB ± 0%    1.82kB ± 0%  -62.73%  (p=0.000 n=10+10)

name       old allocs/op  new allocs/op  delta
NewRR-12       19.0 ± 0%      17.0 ± 0%  -10.53%  (p=0.000 n=10+10)
ReadRR-12      19.0 ± 0%      19.0 ± 0%     ~     (all equal)

ReadRR-12    11.2µs ±16%     9.8µs ± 1%  -13.03%  (p=0.000 n=10+10)

* Surface any remaining comment from zlexer.Next

* Improve comment handling in zlexer.Next

This both fixes a regression where comments could be lost under certain
circumstances and now emits comments that occur within braces.

* Remove outdated comment from zlexer.Next and klexer.Next

* Delay converting LF to space in braced comment

* Fixup TestParseZoneComments

* Remove tokenUpper field from lex

Not computing this for every token, and instead only
when needed is a substantial performance improvement.

name       old time/op    new time/op    delta
NewRR-12     9.56µs ± 0%    6.30µs ± 1%  -34.08%  (p=0.000 n=9+10)
ReadRR-12    9.93µs ± 1%    6.67µs ± 1%  -32.77%  (p=0.000 n=10+10)

name       old alloc/op   new alloc/op   delta
NewRR-12       824B ± 0%      808B ± 0%   -1.94%  (p=0.000 n=10+10)
ReadRR-12    1.83kB ± 0%    1.82kB ± 0%   -0.87%  (p=0.000 n=10+10)

name       old allocs/op  new allocs/op  delta
NewRR-12       17.0 ± 0%      17.0 ± 0%     ~     (all equal)
ReadRR-12      19.0 ± 0%      19.0 ± 0%     ~     (all equal)

* Update ParseZone documentation to match comment changes

The zlexer code was changed to return comments more often, so update the
ParseZone documentation to match.
2018-10-15 17:42:31 +10:30
Tom Thorogood 7f61c6631b
Fix dominikh/go-tools nits (#758)
* Remove unused functions and consts

* Address gosimple nits

* Address staticcheck nits

This excludes several that were intentional or weren't actual errors.

* Reduce size of lex struct

This reduces the size of the lex struct by 8 bytes from:
  lex.token string: 0-16 (size 16, align 8)
  lex.tokenUpper string: 16-32 (size 16, align 8)
  lex.length int: 32-40 (size 8, align 8)
  lex.err bool: 40-41 (size 1, align 1)
  lex.value uint8: 41-42 (size 1, align 1)
  padding: 42-48 (size 6, align 0)
  lex.line int: 48-56 (size 8, align 8)
  lex.column int: 56-64 (size 8, align 8)
  lex.torc uint16: 64-66 (size 2, align 2)
  padding: 66-72 (size 6, align 0)
  lex.comment string: 72-88 (size 16, align 8)
to:
  lex.token string: 0-16 (size 16, align 8)
  lex.tokenUpper string: 16-32 (size 16, align 8)
  lex.length int: 32-40 (size 8, align 8)
  lex.err bool: 40-41 (size 1, align 1)
  lex.value uint8: 41-42 (size 1, align 1)
  lex.torc uint16: 42-44 (size 2, align 2)
  padding: 44-48 (size 4, align 0)
  lex.line int: 48-56 (size 8, align 8)
  lex.column int: 56-64 (size 8, align 8)
  lex.comment string: 64-80 (size 16, align 8)

* Reduce size of response struct

This reduces the size of the response struct by 8 bytes from:
  response.msg []byte: 0-24 (size 24, align 8)
  response.hijacked bool: 24-25 (size 1, align 1)
  padding: 25-32 (size 7, align 0)
  response.tsigStatus error: 32-48 (size 16, align 8)
  response.tsigTimersOnly bool: 48-49 (size 1, align 1)
  padding: 49-56 (size 7, align 0)
  response.tsigRequestMAC string: 56-72 (size 16, align 8)
  response.tsigSecret map[string]string: 72-80 (size 8, align 8)
  response.udp *net.UDPConn: 80-88 (size 8, align 8)
  response.tcp net.Conn: 88-104 (size 16, align 8)
  response.udpSession *github.com/tmthrgd/dns.SessionUDP: 104-112 (size 8, align 8)
  response.writer github.com/tmthrgd/dns.Writer: 112-128 (size 16, align 8)
  response.wg *sync.WaitGroup: 128-136 (size 8, align 8)
to:
  response.msg []byte: 0-24 (size 24, align 8)
  response.hijacked bool: 24-25 (size 1, align 1)
  response.tsigTimersOnly bool: 25-26 (size 1, align 1)
  padding: 26-32 (size 6, align 0)
  response.tsigStatus error: 32-48 (size 16, align 8)
  response.tsigRequestMAC string: 48-64 (size 16, align 8)
  response.tsigSecret map[string]string: 64-72 (size 8, align 8)
  response.udp *net.UDPConn: 72-80 (size 8, align 8)
  response.tcp net.Conn: 80-96 (size 16, align 8)
  response.udpSession *github.com/tmthrgd/dns.SessionUDP: 96-104 (size 8, align 8)
  response.writer github.com/tmthrgd/dns.Writer: 104-120 (size 16, align 8)
  response.wg *sync.WaitGroup: 120-128 (size 8, align 8)
2018-09-27 04:02:05 +09:30
andrewtj da0e668c16 Fix unpacking RSA exponent and tighten exponent validation (#692)
* Add test from #688 demonstrating bug decoding RSA exponent

* Unpack RSA exponent in correct order

Fixes #688

* Don't unpack RSA keys with an exponent too large for the crypto package

* Update dnssec_test.go

Fix the one nit
2018-06-04 21:58:29 +01:00
Miek Gieben 57a0d1a2cf
458+dep (#591)
* Add support for Ed25519 DNSSEC signing from RFC 8080

Note: The test case from RFC 8080 has been modified
to correct the missing final brace, but is otherwise
present as-is.

* Explain why ed25519 is special cased in (*RRSIG).Sign

* Explain use of ed25519.GenerateKey in readPrivateKeyED25519

* Add dep

This is PR #458 with the dependency added into it.
2017-11-27 10:49:53 +00:00
Miek Gieben 388f6eea29
Tests updates (#556)
Use :0 for loopback testing. This is more portable between testing environments.
Add testRR that calls NewRR and throws error away - apply it everywhere where needed.

It seems only Go 1.9 can deal with :0 being used. Disable 1.8 in travis.
2017-11-08 10:01:19 +00:00
Miek Gieben 348c84f37e
Test: remove all Logf/Log (#547)
Move some of them to Errorf and friends, but most of them are just
gone: This make go test -v actually readable.

Remove a bunch of test that used ipv6 on localhost as this does not work
on Travis.
2017-11-03 15:50:01 +00:00
Miek Gieben f520760857 Lowercase all error msg from the tests 2015-11-26 14:12:38 +00:00
Filippo Valsorda e9635cc83b Fix (*HINFO).len() and HINFO canonicalization in rawSignatureData 2015-10-05 14:37:42 +01:00
Filippo Valsorda 034c247229 Refactor DNSSEC to use crypto.{PrivateKey,Signer}
This will allow RRSIG.Sign to use generic crypto.Signer implementations.

This is a interface breaking change, even if the required changes are most
likely just type asserions from crypto.PrivateKey to the underlying type or
crypto.Signer.
2015-08-19 17:51:02 +01:00
Aaron Lehmann 2cee5e58b0 Rename isValidRRSet to IsRRset, and move it to defaults.go 2015-06-28 17:04:05 -07:00
Aaron Lehmann f605c832f0 Remove isValidRRSet check from RRSIG Sign method
Change suggested by miekg, since the caller may already know it's
passing a proper RRset.

Update unit test to call isValidRRSet directly instead of expecting Sign
to return an error for sets the fail the check.
2015-06-28 16:57:40 -07:00
Aaron Lehmann dc56846101 Check that the RRs passed to Sign and Verify form a valid RFC2181 RRSet
Add a sanity check used by RRSig's Sign and Verify functions making sure
that the records they operate on form a valid RRSet (same name, type,
and class).

Add a unit test TestInvalidRRSet that calls RRSig's Sign and Verify
methods with invalid RRSets, and makes sure the correct error is
returned.
2015-06-04 11:13:49 -07:00
Michael Haro 2fb2a25e84 More test clean up
Remove trailing \n from t.Log and t.Error messages as it's unnecessary.

In some instances, combine multiple t.Error()s into one

To provide more consistency across the tests, rename e to err and use %v
as the format arg for errors.

Replace Logf and Errorf with Log and Error when it made sense.  For
example t.Errorf("%v", err) to t.Error(err)
2015-02-25 22:14:21 -08:00
Michael Haro f995f1aff3 Convert tests from being t.Log(..) then t.Fail() to just t.Error(...) as
t.Error(...) does both and makes it more clear which messages are errors
vs information log messages.
2015-02-23 17:43:07 -08:00
Matthew Farrellee 24ccdb3008 fix spelling of separate 2015-02-18 17:04:12 -05:00
Filippo Valsorda e9faa971b3 Refactor the DNSSEC private key code
Now PrivateKey is an interface exposing Sign() and String(). Common
implementations are wrappers for {rsa|dsa|ecdsa}.PrivateKey but
this allows for custom signers, and abstracts away the private-ops
code to a single place.
2015-01-23 13:04:29 -08:00
Filippo Valsorda 3fd8a8eef6 Fix: RSA key export and its test 2015-01-22 22:02:20 -08:00
Filippo Valsorda bcc05715b3 Make some ECDSA failures more informative/accurate 2014-12-05 18:39:06 +00:00
Miek Gieben 5fc7e36983 go test -short
Added a bunch a long running test function to the list of skipped
tests when giving -short to go test. Tests are bascially *all*
DNSSEC key generation tests and 1 serving test.

PASS
ok      github.com/miekg/dns    0.782s

Compared to 13+ s, so quite a bit faster.
2014-11-12 10:19:20 +00:00
Miek Gieben 0aa3021a83 Remove all copyright notices
Use the central COPYRIGHT file.
2014-09-11 20:57:37 +01:00
Miek Gieben cf89d7a324 Remove Copyrights from there 2014-09-11 20:53:14 +01:00
Miek Gieben 518ab47648 Move the tests into the existing _test.go files. 2014-09-11 20:49:20 +01:00
Filippo Valsorda 72f550b0ec Fix NewPrivateKey ECDSA parsing, and add test 2014-09-10 17:04:39 -07:00
Miek Gieben ad58c7b338 Disabled two failing ECDSA tests 2014-09-09 07:53:09 +01:00
Miek Gieben 6ecd49ff89 Fix ECDSA algorithms
Current code was completely wrong, so validation of ECDSA didn't work.
The new tests now works, the old one now doesn't
2014-09-09 07:51:15 +01:00
Miek Gieben 643720d10d Fix ECDSA algorithms
Current code was completely wrong, so validation of ECDSA didn't work.
The new tests now works, the old one now doesn't
2014-09-09 07:45:47 +01:00
Miek Gieben 818abf8202 Lowercase the Test to test 2014-09-05 08:47:23 +01:00
Miek Gieben 4b1fbc358e Cleanup the test a bit 2014-09-05 08:44:31 +01:00
Miek Gieben 919e4f9daf Add extra (failing) sign/verify test 2014-09-05 08:27:22 +01:00
Miek Gieben 53aa7cebbf Add test for SRV signing 2014-02-02 19:25:55 +00:00
Miek Gieben 9c75b3cdc2 Fi the tests too 2014-01-27 14:45:34 +00:00
Miek Gieben 70ee966106 Fix the unknown record sign test
Don't make up new error in msg.Pack when there are perfectly fine
errors to return.
2013-12-06 09:43:26 +00:00
Miek Gieben 6c9cc37ac3 Small tweaks to the pull request from Alex 2013-12-06 07:46:08 +00:00
Miek Gieben 110f6c9ea7 Test for TYPE65534 records, copy now copies slices correctly 2013-12-06 07:37:48 +00:00
Miek Gieben ee8ace0477 Merge branch 'master' of github.com:miekg/dns 2013-09-11 08:22:23 +01:00
Miek Gieben 9c1ee5d5ca Update IsDomainName
This new functions just compiles the domain to wire format, if that
works, the name is deemed OK. It is also much less strict than the
older code. Almost everything is allowed in the name, except two
dots back to back (there is an explicit test for that).
2013-09-10 18:09:22 +00:00
Miek Gieben a5ac1f46ee ...and fix the imports for the test files 2013-09-09 22:20:48 +01:00
Miek Gieben 81d35ce537 Move some functions to the correct file
DNSSEC tests go in dnssec_test.go
2013-09-09 22:19:24 +01:00
Miek Gieben f595bf7a06 Set license on test files 2013-05-12 16:09:52 +02:00
Miek Gieben d53d9eab81 gofmt 2013-05-05 20:30:44 +02:00
Miek Gieben 26994c382b gofmt 2013-01-28 14:34:18 +01:00
Miek Gieben 9977bf87c4 Drop unneeded packages 2012-12-14 12:42:39 +01:00
Miek Gieben 235e892dfc Rename the RR types drop the RR_ prefix
This is also done in the official Go library. It also make the
code shorter.
2012-12-09 19:23:25 +01:00
Miek Gieben dda096049e more tests 2012-04-18 12:13:44 +02:00
Miek Gieben 872dc4fc6b Make the private key reading more natural 2012-04-15 20:55:25 +02:00
Miek Gieben b58c604e17 Add symmetry to the reading of public/private keys
Add a NewPrivateKey that works on strings and calls ReadPrivateKey
that works on io.Readers.
2012-04-15 20:50:53 +02:00