You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HMACSHA1NonceGeneratorTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C) 2015, Google Inc.
  3. *
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Distribution License v1.0 which
  6. * accompanies this distribution, is reproduced below, and is
  7. * available at http://www.eclipse.org/org/documents/edl-v10.php
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials provided
  21. * with the distribution.
  22. *
  23. * - Neither the name of the Eclipse Foundation, Inc. nor the
  24. * names of its contributors may be used to endorse or promote
  25. * products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  29. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  30. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  31. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  35. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  40. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. package org.eclipse.jgit.transport;
  43. import static org.junit.Assert.assertEquals;
  44. import static org.junit.Assert.assertNotEquals;
  45. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  46. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  47. import org.eclipse.jgit.lib.Repository;
  48. import org.eclipse.jgit.transport.PushCertificate.NonceStatus;
  49. import org.junit.Before;
  50. import org.junit.Test;
  51. /** Test for HMAC SHA-1 certificate verifier. */
  52. public class HMACSHA1NonceGeneratorTest {
  53. private static final long TS = 1433954361;
  54. private HMACSHA1NonceGenerator gen;
  55. private Repository db;
  56. @Before
  57. public void setUp() {
  58. gen = new HMACSHA1NonceGenerator("sekret");
  59. db = new InMemoryRepository(new DfsRepositoryDescription("db"));
  60. }
  61. @Test
  62. public void missing() throws Exception {
  63. assertEquals(NonceStatus.MISSING, gen.verify("", "1234", db, false, 0));
  64. }
  65. @Test
  66. public void unsolicited() throws Exception {
  67. assertEquals(NonceStatus.UNSOLICITED, gen.verify("1234", "", db, false, 0));
  68. }
  69. @Test
  70. public void invalidFormat() throws Exception {
  71. String sent = gen.createNonce(db, TS);
  72. int idx = sent.indexOf('-');
  73. String sig = sent.substring(idx, sent.length() - idx);
  74. assertEquals(NonceStatus.BAD,
  75. gen.verify(Long.toString(TS), sent, db, true, 100));
  76. assertEquals(NonceStatus.BAD, gen.verify(sig, sent, db, true, 100));
  77. assertEquals(NonceStatus.BAD, gen.verify("xxx-" + sig, sent, db, true, 100));
  78. assertEquals(NonceStatus.BAD, gen.verify(sent, "xxx-" + sig, db, true, 100));
  79. }
  80. @Test
  81. public void slop() throws Exception {
  82. String sent = gen.createNonce(db, TS - 10);
  83. String received = gen.createNonce(db, TS);
  84. assertEquals(NonceStatus.BAD,
  85. gen.verify(received, sent, db, false, 0));
  86. assertEquals(NonceStatus.BAD,
  87. gen.verify(received, sent, db, false, 11));
  88. assertEquals(NonceStatus.SLOP,
  89. gen.verify(received, sent, db, true, 0));
  90. assertEquals(NonceStatus.SLOP,
  91. gen.verify(received, sent, db, true, 9));
  92. assertEquals(NonceStatus.OK,
  93. gen.verify(received, sent, db, true, 10));
  94. assertEquals(NonceStatus.OK,
  95. gen.verify(received, sent, db, true, 11));
  96. }
  97. @Test
  98. public void ok() throws Exception {
  99. String sent = gen.createNonce(db, TS);
  100. assertEquals(NonceStatus.OK, gen.verify(sent, sent, db, false, 0));
  101. }
  102. @Test
  103. public void signedByDifferentKey() throws Exception {
  104. HMACSHA1NonceGenerator other = new HMACSHA1NonceGenerator("other");
  105. String sent = gen.createNonce(db, TS);
  106. String received = other.createNonce(db, TS);
  107. assertNotEquals(received, sent);
  108. assertEquals(NonceStatus.BAD,
  109. gen.verify(received, sent, db, false, 0));
  110. }
  111. @Test
  112. public void signedByDifferentKeyWithSlop() throws Exception {
  113. HMACSHA1NonceGenerator other = new HMACSHA1NonceGenerator("other");
  114. String sent = gen.createNonce(db, TS - 10);
  115. String received = other.createNonce(db, TS);
  116. assertEquals(NonceStatus.BAD, gen.verify(received, sent, db, true, 100));
  117. }
  118. }