<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14621] PyKerberos/trunk/test.py</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/14621">14621</a></dd>
<dt>Author</dt> <dd>wsanchez@apple.com</dd>
<dt>Date</dt> <dd>2015-03-26 15:14:07 -0700 (Thu, 26 Mar 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>cleanup</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#PyKerberostrunktestpy">PyKerberos/trunk/test.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="PyKerberostrunktestpy"></a>
<div class="modfile"><h4>Modified: PyKerberos/trunk/test.py (14620 => 14621)</h4>
<pre class="diff"><span>
<span class="info">--- PyKerberos/trunk/test.py        2015-03-26 21:54:26 UTC (rev 14620)
+++ PyKerberos/trunk/test.py        2015-03-26 22:14:07 UTC (rev 14621)
</span><span class="lines">@@ -16,40 +16,44 @@
</span><span class="cx"> #
</span><span class="cx"> ##
</span><span class="cx"> 
</span><ins>+&quot;&quot;&quot;
+Examples:
+
+    sudo ./test.py -s HTTP@example.com service
+
+    sudo ./test.py -u user01 -p user01 -s HTTP@example.com -r EXAMPLE.COM basic
+
+    sudo ./test.py -s HTTP@example.com -r EXAMPLE.COM gssapi
+
+    ./test.py -s HTTP@example.com -h calendar.example.com -p 8008 server
+
+For the gssapi and server tests you will need to kinit a principal on the
+server first.
+&quot;&quot;&quot;
+
+from __future__ import print_function
+
</ins><span class="cx"> import kerberos
</span><span class="cx"> import getopt
</span><span class="cx"> import sys
</span><del>-import httplib
</del><span class="cx"> import socket
</span><span class="cx"> import ssl
</span><span class="cx"> 
</span><del>-&quot;&quot;&quot;
-Examples:
</del><ins>+from httplib import HTTPSConnection, HTTPConnection
</ins><span class="cx"> 
</span><del>-sudo ./test.py -s HTTP@example.com service
</del><span class="cx"> 
</span><del>-sudo ./test.py -u user01 -p user01 -s HTTP@example.com -r EXAMPLE.COM basic
</del><span class="cx"> 
</span><del>-sudo ./test.py -s HTTP@example.com -r EXAMPLE.COM gssapi
-
-./test.py -s HTTP@example.com -h calendar.example.com -p 8008 server
-
-For the gssapi and server tests you will need to kinit a principal on the server first.
-
-&quot;&quot;&quot;
-
</del><span class="cx"> def main():
</span><del>-    
</del><span class="cx">     # Extract arguments
</span><span class="cx">     user = &quot;&quot;
</span><span class="cx">     pswd = &quot;&quot;
</span><span class="cx">     service = &quot;HTTP@EXAMPLE.COM&quot;
</span><span class="cx">     host = &quot;host.example.com&quot;
</span><del>-    realm =&quot;HOST.EXAMPLE.COM&quot;
</del><ins>+    realm = &quot;HOST.EXAMPLE.COM&quot;
</ins><span class="cx">     port = 8008
</span><span class="cx">     use_ssl = False
</span><span class="cx">     allowedActions = (&quot;service&quot;, &quot;basic&quot;, &quot;gssapi&quot;, &quot;server&quot;,)
</span><del>-    
</del><ins>+
</ins><span class="cx">     options, args = getopt.getopt(sys.argv[1:], &quot;u:p:s:h:i:r:x&quot;)
</span><span class="cx"> 
</span><span class="cx">     for option, value in options:
</span><span class="lines">@@ -67,56 +71,68 @@
</span><span class="cx">             realm = value
</span><span class="cx">         elif option == &quot;-x&quot;:
</span><span class="cx">             use_ssl = True
</span><del>-    
</del><ins>+
</ins><span class="cx">     actions = set()
</span><span class="cx">     for arg in args:
</span><span class="cx">         if arg in allowedActions:
</span><span class="cx">             actions.add(arg)
</span><span class="cx">         else:
</span><del>-            print &quot;Action not allowed: %s&quot; % (arg,)
</del><ins>+            print(&quot;Action not allowed: %s&quot; % (arg,))
</ins><span class="cx">             sys.exit(1)
</span><del>-    
</del><ins>+
</ins><span class="cx">     # Get service principal
</span><span class="cx">     if &quot;service&quot; in actions:
</span><del>-        print &quot;\n*** Running Service Principal test&quot;
</del><ins>+        print(&quot;\n*** Running Service Principal test&quot;)
</ins><span class="cx">         s, h = service.split(&quot;@&quot;)
</span><del>-        testServicePrincipal(s, h);
</del><ins>+        testServicePrincipal(s, h)
</ins><span class="cx"> 
</span><span class="cx">     # GSS Basic test
</span><span class="cx">     if &quot;basic&quot; in actions:
</span><span class="cx">         if (len(user) != 0) and (len(pswd) != 0):
</span><del>-            print &quot;\n*** Running basic test&quot;
</del><ins>+            print(&quot;\n*** Running basic test&quot;)
</ins><span class="cx">             testCheckpassword(user, pswd, service, realm)
</span><span class="cx">         else:
</span><del>-            print &quot;\n*** Skipping basic test: no user or password specified&quot;
</del><ins>+            print(&quot;\n*** Skipping basic test: no user or password specified&quot;)
</ins><span class="cx"> 
</span><span class="cx">     # Full GSSAPI test
</span><span class="cx">     if &quot;gssapi&quot; in actions:
</span><del>-        print &quot;\n*** Running GSSAPI test&quot;
</del><ins>+        print(&quot;\n*** Running GSSAPI test&quot;)
</ins><span class="cx">         testGSSAPI(service)
</span><span class="cx"> 
</span><span class="cx">     if &quot;server&quot; in actions:
</span><del>-        print &quot;\n*** Running HTTP test&quot;
</del><ins>+        print(&quot;\n*** Running HTTP test&quot;)
</ins><span class="cx">         testHTTP(host, port, use_ssl, service)
</span><span class="cx"> 
</span><del>-    print &quot;\n*** Done\n&quot;
</del><ins>+    print(&quot;\n*** Done\n&quot;)
</ins><span class="cx"> 
</span><ins>+
+
</ins><span class="cx"> def testServicePrincipal(service, hostname):
</span><span class="cx">     try:
</span><span class="cx">         result = kerberos.getServerPrincipalDetails(service, hostname)
</span><span class="cx">     except kerberos.KrbError, e:
</span><del>-        print &quot;Kerberos service principal for %s/%s failed: %s&quot; % (service, hostname, e[0])
</del><ins>+        print(
+            &quot;Kerberos service principal for %s/%s failed: %s&quot;
+            % (service, hostname, e[0])
+        )
</ins><span class="cx">     else:
</span><del>-        print &quot;Kerberos service principal for %s/%s succeeded: %s&quot; % (service, hostname, result)
</del><ins>+        print(
+            &quot;Kerberos service principal for %s/%s succeeded: %s&quot;
+            % (service, hostname, result)
+        )
</ins><span class="cx"> 
</span><ins>+
+
</ins><span class="cx"> def testCheckpassword(user, pswd, service, realm):
</span><span class="cx">     try:
</span><span class="cx">         kerberos.checkPassword(user, pswd, service, realm)
</span><span class="cx">     except kerberos.BasicAuthError, e:
</span><del>-        print &quot;Kerberos authentication for %s failed: %s&quot; % (user, e[0])
</del><ins>+        print(&quot;Kerberos authentication for %s failed: %s&quot; % (user, e[0]))
</ins><span class="cx">     else:
</span><del>-        print &quot;Kerberos authentication for %s succeeded&quot; % user
</del><ins>+        print(&quot;Kerberos authentication for %s succeeded&quot; % user)
</ins><span class="cx"> 
</span><ins>+
+
</ins><span class="cx"> def testGSSAPI(service):
</span><span class="cx">     def statusText(r):
</span><span class="cx">         if r == 1:
</span><span class="lines">@@ -126,81 +142,89 @@
</span><span class="cx">         else:
</span><span class="cx">             return &quot;Error&quot;
</span><span class="cx"> 
</span><del>-    rc, vc = kerberos.authGSSClientInit(service);
-    print &quot;Status for authGSSClientInit = %s&quot; % statusText(rc);
</del><ins>+    rc, vc = kerberos.authGSSClientInit(service)
+    print(&quot;Status for authGSSClientInit = %s&quot; % statusText(rc))
</ins><span class="cx">     if rc != 1:
</span><span class="cx">         return
</span><del>-    
-    rs, vs = kerberos.authGSSServerInit(service);
-    print &quot;Status for authGSSServerInit = %s&quot; % statusText(rs);
</del><ins>+
+    rs, vs = kerberos.authGSSServerInit(service)
+    print(&quot;Status for authGSSServerInit = %s&quot; % statusText(rs))
</ins><span class="cx">     if rs != 1:
</span><span class="cx">         return
</span><del>-    
-    rc = kerberos.authGSSClientStep(vc, &quot;&quot;);
-    print &quot;Status for authGSSClientStep = %s&quot; % statusText(rc);
</del><ins>+
+    rc = kerberos.authGSSClientStep(vc, &quot;&quot;)
+    print(&quot;Status for authGSSClientStep = %s&quot; % statusText(rc))
</ins><span class="cx">     if rc != 0:
</span><span class="cx">         return
</span><del>-    
-    rs = kerberos.authGSSServerStep(vs, kerberos.authGSSClientResponse(vc));
-    print &quot;Status for authGSSServerStep = %s&quot; % statusText(rs);
</del><ins>+
+    rs = kerberos.authGSSServerStep(vs, kerberos.authGSSClientResponse(vc))
+    print(&quot;Status for authGSSServerStep = %s&quot; % statusText(rs))
</ins><span class="cx">     if rs == -1:
</span><span class="cx">         return
</span><del>-    
-    rc = kerberos.authGSSClientStep(vc, kerberos.authGSSServerResponse(vs));
-    print &quot;Status for authGSSClientStep = %s&quot; % statusText(rc);
</del><ins>+
+    rc = kerberos.authGSSClientStep(vc, kerberos.authGSSServerResponse(vs))
+    print(&quot;Status for authGSSClientStep = %s&quot; % statusText(rc))
</ins><span class="cx">     if rc == -1:
</span><span class="cx">         return
</span><span class="cx"> 
</span><del>-    print &quot;Server user name: %s&quot; % kerberos.authGSSServerUserName(vs);
-    print &quot;Server target name: %s&quot; % kerberos.authGSSServerTargetName(vs);
-    print &quot;Client user name: %s&quot; % kerberos.authGSSClientUserName(vc);
-    
-    rc = kerberos.authGSSClientClean(vc);
-    print &quot;Status for authGSSClientClean = %s&quot; % statusText(rc);
-    
-    rs = kerberos.authGSSServerClean(vs);
-    print &quot;Status for authGSSServerClean = %s&quot; % statusText(rs);
</del><ins>+    print(&quot;Server user name: %s&quot; % kerberos.authGSSServerUserName(vs))
+    print(&quot;Server target name: %s&quot; % kerberos.authGSSServerTargetName(vs))
+    print(&quot;Client user name: %s&quot; % kerberos.authGSSClientUserName(vc))
</ins><span class="cx"> 
</span><ins>+    rc = kerberos.authGSSClientClean(vc)
+    print(&quot;Status for authGSSClientClean = %s&quot; % statusText(rc))
+
+    rs = kerberos.authGSSServerClean(vs)
+    print(&quot;Status for authGSSServerClean = %s&quot; % statusText(rs))
+
+
+
</ins><span class="cx"> def testHTTP(host, port, use_ssl, service):
</span><span class="cx"> 
</span><del>-    class HTTPSConnection_SSLv3(httplib.HTTPSConnection):
</del><ins>+    class HTTPSConnectionSSLv3(HTTPSConnection):
</ins><span class="cx">         &quot;This class allows communication via SSL.&quot;
</span><span class="cx"> 
</span><span class="cx">         def connect(self):
</span><span class="cx">             &quot;Connect to a host on a given (SSL) port.&quot;
</span><span class="cx"> 
</span><del>-            sock = socket.create_connection((self.host, self.port), self.timeout)
-            self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_SSLv3)
</del><ins>+            sock = socket.create_connection(
+                (self.host, self.port), self.timeout
+            )
+            self.sock = ssl.wrap_socket(
+                sock, self.key_file, self.cert_file,
+                ssl_version=ssl.PROTOCOL_SSLv3
+            )
</ins><span class="cx"> 
</span><ins>+
</ins><span class="cx">     def sendRequest(host, port, ssl, method, uri, headers):
</span><span class="cx">         response = None
</span><span class="cx">         if use_ssl:
</span><del>-            http = HTTPSConnection_SSLv3(host, port)
</del><ins>+            http = HTTPSConnectionSSLv3(host, port)
</ins><span class="cx">         else:
</span><del>-            http = httplib.HTTPConnection(host, port)
</del><ins>+            http = HTTPConnection(host, port)
</ins><span class="cx">         try:
</span><span class="cx">             http.request(method, uri, &quot;&quot;, headers)
</span><span class="cx">             response = http.getresponse()
</span><span class="cx">         finally:
</span><span class="cx">             http.close()
</span><del>-        
</del><ins>+
</ins><span class="cx">         return response
</span><span class="cx"> 
</span><span class="cx">     # Initial request without auth header
</span><span class="cx">     uri = &quot;/principals/&quot;
</span><span class="cx">     response = sendRequest(host, port, use_ssl, &quot;OPTIONS&quot;, uri, {})
</span><del>-    
</del><ins>+
</ins><span class="cx">     if response is None:
</span><del>-        print &quot;Initial HTTP request to server failed&quot;
</del><ins>+        print(&quot;Initial HTTP request to server failed&quot;)
</ins><span class="cx">         return
</span><del>-    
</del><ins>+
</ins><span class="cx">     if response.status != 401:
</span><del>-        print &quot;Initial HTTP request did not result in a 401 response&quot;
</del><ins>+        print(&quot;Initial HTTP request did not result in a 401 response&quot;)
</ins><span class="cx">         return
</span><del>-    
</del><ins>+
</ins><span class="cx">     hdrs = response.msg.getheaders(&quot;www-authenticate&quot;)
</span><span class="cx">     if (hdrs is None) or (len(hdrs) == 0):
</span><del>-        print &quot;No www-authenticate header in initial HTTP response.&quot;
</del><ins>+        print(&quot;No www-authenticate header in initial HTTP response.&quot;)
</ins><span class="cx">     for hdr in hdrs:
</span><span class="cx">         hdr = hdr.strip()
</span><span class="cx">         splits = hdr.split(' ', 1)
</span><span class="lines">@@ -209,38 +233,47 @@
</span><span class="cx">         else:
</span><span class="cx">             break
</span><span class="cx">     else:
</span><del>-        print &quot;No www-authenticate header with negotiate in initial HTTP response.&quot;
-        return        
</del><ins>+        print(
+            &quot;No www-authenticate header with negotiate in initial HTTP &quot;
+            &quot;response.&quot;
+        )
+        return
</ins><span class="cx"> 
</span><span class="cx">     try:
</span><del>-        rc, vc = kerberos.authGSSClientInit(service=service);
</del><ins>+        rc, vc = kerberos.authGSSClientInit(service=service)
</ins><span class="cx">     except kerberos.GSSError, e:
</span><del>-        print &quot;Could not initialize GSSAPI: %s/%s&quot; % (e[0][0], e[1][0])
</del><ins>+        print(&quot;Could not initialize GSSAPI: %s/%s&quot; % (e[0][0], e[1][0]))
</ins><span class="cx">         return
</span><span class="cx"> 
</span><span class="cx">     try:
</span><del>-        kerberos.authGSSClientStep(vc, &quot;&quot;);
</del><ins>+        kerberos.authGSSClientStep(vc, &quot;&quot;)
</ins><span class="cx">     except kerberos.GSSError, e:
</span><del>-        print &quot;Could not do GSSAPI step with continue: %s/%s&quot; % (e[0][0], e[1][0])
</del><ins>+        print(
+            &quot;Could not do GSSAPI step with continue: %s/%s&quot;
+            % (e[0][0], e[1][0])
+        )
</ins><span class="cx">         return
</span><span class="cx"> 
</span><span class="cx">     hdrs = {}
</span><del>-    hdrs[&quot;Authorization&quot;] = &quot;negotiate %s&quot; % kerberos.authGSSClientResponse(vc)    
</del><ins>+    hdrs[&quot;Authorization&quot;] = &quot;negotiate %s&quot; % kerberos.authGSSClientResponse(vc)
</ins><span class="cx"> 
</span><span class="cx">     # Second request with auth header
</span><span class="cx">     response = sendRequest(host, port, use_ssl, &quot;OPTIONS&quot;, uri, hdrs)
</span><del>-    
</del><ins>+
</ins><span class="cx">     if response is None:
</span><del>-        print &quot;Second HTTP request to server failed&quot;
</del><ins>+        print(&quot;Second HTTP request to server failed&quot;)
</ins><span class="cx">         return
</span><del>-    
-    if response.status/100 != 2:
-        print &quot;Second HTTP request did not result in a 2xx response: %d&quot; % (response.status,)
</del><ins>+
+    if response.status / 100 != 2:
+        print(
+            &quot;Second HTTP request did not result in a 2xx response: %d&quot;
+            % (response.status,)
+        )
</ins><span class="cx">         return
</span><del>-    
</del><ins>+
</ins><span class="cx">     hdrs = response.msg.getheaders(&quot;www-authenticate&quot;)
</span><span class="cx">     if (hdrs is None) or (len(hdrs) == 0):
</span><del>-        print &quot;No www-authenticate header in second HTTP response.&quot;
</del><ins>+        print(&quot;No www-authenticate header in second HTTP response.&quot;)
</ins><span class="cx">         return
</span><span class="cx">     for hdr in hdrs:
</span><span class="cx">         hdr = hdr.strip()
</span><span class="lines">@@ -250,23 +283,32 @@
</span><span class="cx">         else:
</span><span class="cx">             break
</span><span class="cx">     else:
</span><del>-        print &quot;No www-authenticate header with negotiate in second HTTP response.&quot;
-        return        
</del><ins>+        print(
+            &quot;No www-authenticate header with negotiate in second HTTP &quot;
+            &quot;response.&quot;
+        )
+        return
</ins><span class="cx"> 
</span><span class="cx">     try:
</span><span class="cx">         kerberos.authGSSClientStep(vc, splits[1])
</span><span class="cx">     except kerberos.GSSError, e:
</span><del>-        print &quot;Could not verify server www-authenticate header in second HTTP response: %s/%s&quot; % (e[0][0], e[1][0])
</del><ins>+        print(
+            &quot;Could not verify server www-authenticate header in second HTTP &quot;
+            &quot;response: %s/%s&quot;
+            % (e[0][0], e[1][0])
+        )
</ins><span class="cx">         return
</span><del>-    
</del><ins>+
</ins><span class="cx">     try:
</span><del>-        rc = kerberos.authGSSClientClean(vc);
</del><ins>+        kerberos.authGSSClientClean(vc)
</ins><span class="cx">     except kerberos.GSSError, e:
</span><del>-        print &quot;Could not clean-up GSSAPI: %s/%s&quot; % (e[0][0], e[1][0])
</del><ins>+        print(&quot;Could not clean-up GSSAPI: %s/%s&quot; % (e[0][0], e[1][0]))
</ins><span class="cx">         return
</span><span class="cx"> 
</span><del>-    print &quot;Authenticated successfully&quot;
</del><ins>+    print(&quot;Authenticated successfully&quot;)
</ins><span class="cx">     return
</span><span class="cx"> 
</span><del>-if __name__=='__main__':
</del><ins>+
+
+if __name__ == &quot;__main__&quot;:
</ins><span class="cx">     main()
</span></span></pre>
</div>
</div>

</body>
</html>