Wednesday, July 31, 2013

Work notes; JTestR, JRuby and JMockit

Java; JMockit

Class MockUp

http://jmockit.googlecode.com/svn/trunk/www/javadoc/mockit/MockUp.html

Introduction to JMockit

public void testDoBusiness() {
Mockit.setUpMocks(MockCustomerDao.class)
List<Customer> customers = service.doBusiness();
http://bwinterberg.blogspot.com/2009/08/unittests-with-jmockit.html

Class Mockit

@Deprecated
public static void setUpMocks(Object... mockClassesOrInstances)
http://jmockit.googlecode.com/svn/trunk/www/javadoc/mockit/Mockit.html#setUpMocks%28java.lang.Object...%29

Instructed to use MockUp instead.

How to mock the default constructor of the Date class with JMockit?

long time = SystemTime.asMillis();
Calendar calendar = SystemTime.asCalendar();
Date date = SystemTime.asDate();
http://stackoverflow.com/questions/4563584/how-to-mock-the-default-constructor-of-the-date-class-with-jmockit

"it's good practice to use a SystemTime abstraction in your java classes. Replace your method calls (System#currentTimeMillis and Calendar#getInstance) and direct construction (new Date()) with static method calls"

Class Mockit; documentation

[[method summary]] http://jmockit.googlecode.com/svn/trunk/www/javadoc/mockit/Mockit.html

It appears that I have been finding lots of documentation that contains deprecated calls. Once I get that code working, I need to find out what the current way of doing business is.

JMockit Quick Tutorial (Cheat sheet)

[[tutorial table of contents]] https://github.com/ajermakovics/eclipse-jmockit-assist/wiki/JMockit-Quick-Tutorial-%28Cheat-sheet%29#wiki-Mocking_static_methods

JMockit and static methods

    @Test
    public void shouldMockStaticMethod() {
        new NonStrictExpectations() {
            final MockitEg mock = null;

            {
                MockitEg.shoeSize();
                result = new MockitEgDelegate();
            }
        };

        assertThat(MockitEg.shoeSize(),
                is(equalTo(MOCK_SHOE_SIZE)));
    }
http://binkley.blogspot.com/2011/07/jmockit-and-static-methods.html

There seems to be many ways of using JMockit. This example might hold some clues to understanding how to mock static methods. Other examples seem to indicate there is no difference between mocking static and instance methods. What happens, however, when there is a method with the same name for both static and instance scopes?

Mocking Static Method Calls

public class SweetLittleMock {
        // Same signature as static method in real class
        public static String doStuff() {
            return "SweetLittleMock";
    }
}
http://www.weblogism.com/item/254/mocking-static-method-calls

Another example of mocking static methods.

jmockit, openJDK and UnsatisfiedLinkError

Try adding <jdk6home>/lib/tools.jar to the classpath, before jmockit.jar. If that doesn't solve the problem, passing -javaagent:jmockit.jar as a JVM initialization parameter definitely should.
http://t5187.codeinpro.us/q/50813bbe4f1eba38a437e655

The problem I was having got resolved when I 1) specified JUnit 4 (instead of 3.x.x) in my pom.xml and 2) installed the Eclipse plugin for JMockit ("JMockit Eclipse Plug-in"; Help > Eclipse Marketplace > Find).

Getting started with the JMockit Testing Toolkit

If you are developing on JDK 1.5, then make sure that -javaagent:jmockit.jar (with the proper absolute or relative path to jmockit.jar) is passed as an initialization parameter to the JVM when running tests. This standard JVM initialization parameter causes it to load on start-up the "Java agent" that JMockit uses internally for bytecode instrumentation; this is required to work in all standard JVMs since version 1.5, in all OSs. You may have to use this parameter even on a newer JDK 1.6+, if its Attach API implementation is not supported by JMockit: such is the case with the IBM J9 JDK 1.6, the Mac OS X JDKs, and with JDKs for the Solaris OS.

If you use TestNG in a JDK 1.6+ environment, JMockit can be initialized in one of three possible ways (apart from use of "-javaagent" indicated above, which can also be used). See this page for details.
http://jmockit.googlecode.com/svn-history/r1166/trunk/www/installation.html

Again, installing the Eclipse plug-in did the trick for me.

mockito; simpler & better mocking

[[mockito logo]] http://code.google.com/p/mockito/

Looks nice and simple, but this being my first time in a long time of unit testing with Java, I don't want to feel like I'm limiting myself.

What's the best mock framework for Java? [closed]

http://stackoverflow.com/questions/22697/whats-the-best-mock-framework-for-java

RDoc

RDoc documentation

  Hyperlinks to the web starting http:, mailto:, ftp:, or www. are recognized. An HTTP url that references an external image file is converted into an inline <IMG..>. Hyperlinks starting 'link:' are assumed to refer to local files whose path is relative to the --op directory.

  Hyperlinks can also be of the form label[url], in which case the label is used in the displayed text, and url is used as the target.
http://rdoc.sourceforge.net/doc/

How to word wrap in Vim

:set textwidth=80
:set wrapmargin=2
http://stackoverflow.com/questions/3033423/vim-command-to-restructure-force-text-to-80-columns

I also created some useful functions in my .vimrc file:

function! TurnOnLineWrap(...)
  "http://stackoverflow.com/questions/3033423/vim-command-to-restructure-force-text-to-80-columns
  :set textwidth=80
  :set wrapmargin=2
endfunction

function! TurnOffLineWrap(...)
  "http://stackoverflow.com/questions/3033423/vim-command-to-restructure-force-text-to-80-columns
  :set textwidth=0
  :set wrapmargin=0
endfunction

Java; JTestR (testing Java with Ruby)

Mocks

JtestR bundles the Ruby mocking framework Mocha. This is the mocking framework used for mocking in both Test/Unit and RSpec tests. For several reasons the RSpec way of mocking doesn't fit well with mocking of Java classes. Mocha on the other hand supports this quite easily.
http://jtestr.codehaus.org/Mocks

'RSpec' example for JTestR

describe Account do
  context "transfering money" do
    it "deposits transfer amount to the other account" do
      source = Account.new(50, :USD)
      target = mock('target account')
      target.should_receive(:deposit).with(Money.new(5, :USD))
      source.transfer(5, :USD).to(target)
    end

    it "reduces its balance by the transfer amount" do
      source = Account.new(50, :USD)
      target = stub('target account')
      source.transfer(5, :USD).to(target)
      source.balance.should == Money.new(45, :USD)
    end
  end
end
https://github.com/olabini/jtestr/tree/master/src/ruby/rspec

I've tried to use pure RSpec; I must be missing something, because I can't seem to get it to work. There doesn't seem to be enough documentation handy for me to understand how to "turn on" RSpec for JTestR.

Configuration

rspec

The rspec configuration works exactly like the test_unit configuration value. It also has the same :all parameter, for specifying that all tests are RSpec tests.
http://jtestr.codehaus.org/Configuration

Supposedly, this is where I configure RSpec to be used for my tests. I get some strange error about the missing method "filenames" when I do.

Getting Started

http://jtestr.codehaus.org/Getting+Started

Versions of JTestR

http://docs.codehaus.org/pages/viewpage.action?pageId=42008590

RSpec documentation for mocks

https://www.relishapp.com/rspec/rspec-mocks/v/2-14/docs/message-expectations

How to return a dynamic value from a Mocha mock in Ruby

prc = Proc.new do |xml| 
  "mocked: #{xml}"
end
to_mock.stubs(:post_to_embassy).with(orderInfoXml1).returns(prc.call(orderInfoXml1))
to_mock.stubs(:post_to_embassy).with(orderInfoXml2).returns(prc.call(orderInfoXml2))
http://stackoverflow.com/questions/2742565/how-to-return-a-dynamic-value-from-a-mocha-mock-in-ruby

Mocking constructors in Ruby

require 'mocha'

mock_file_obj = mock("My Mock File") do
  stubs(:some_instance_method).returns("foo")
end

File.stubs(:new).with(is_a(String)).returns(mock_file_obj)
http://stackoverflow.com/questions/72220/mocking-constructors-in-ruby

Mocha documentation - Class: Mocha::Mock

object = mock()
object.expects(:expected_method)
object.expected_method
http://gofreerange.com/mocha/docs/Mocha/Mock.html

Using Java Classes in JRuby

include Java 
...
Dir["/some/path/\*.jar"].each { |jar| require jar }
https://blogs.oracle.com/coolstuff/entry/using_java_classes_in_jruby http://www.pressingquestion.com/720587/Using-Custom-Java-Class-File-In-Jruby

This was instrumental in helping me understand how to pull in Java files into my Ruby tests.

Different dependencies for different build profiles in maven

<profiles>
    <profile>
     <id>debug</id>
     …
     <dependencies>
      <dependency>…</dependency>
     </dependencies>
     …
    </profile>
    <profile>
     <id>release</id>
     …
     <dependencies>
      <dependency>…</dependency>
     </dependencies>
     …
    </profile>
</profiles>
http://stackoverflow.com/questions/166895/different-dependencies-for-different-build-profiles-in-maven

Configure JTestR with Maven

      <plugin>
        <groupId>org.jtestr</groupId>
        <artifactId>jtestr</artifactId>
        <version>0.3.2-SNAPSHOT</version>
        <configuration>
          <port>20333</port>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
https://github.com/stuarthalloway/jtestr/blob/master/jtestr/examples/multi_maven_project/pom.xml

Anyone using JRuby testing framework for Eclipse/Maven?

http://stackoverflow.com/questions/9179491/anyone-using-jruby-testing-framework-for-eclipse-maven

Cucumber-JVM might be an option I can look into if JTestR doesn't end up working.

JTestR + Eclipse

JRuby & JTestR in Eclipse
 
In your eclipse Package Manager sidebar, Right Click -#> Run As.. -#> Run Configurations. Go ahead and select ‘JUnit’ and hit the ‘New Launch Configuration’ button.
 
Fill in the configuration as follows:
 
* Run a single test
Project: your-project-name
Test class: org.jtestr.ant.JtestRSuite
Now just open the ‘Arguments’ tab and inside the ‘VM arguments:’ box
 
-Djtestr.junit.tests=rspec_tests
rspec_tests is the folder where my tests are stored, feel free to change this to something like /test/jtestr/funny_test or whatever structure you store your files in.
https://gist.github.com/peterlind/660434

Valuable little gem that helped me get my JTestR tests running in Eclipse.

Boost your Java Test with Ruby and JtestR

http://www.infoq.com/news/2008/01/boost-java-test

Yet another plug for JTestR. Again promising the benefits of RSpec. If I could only get it to work for me...

What is needed in order to run the JTestR samples? (or using JTestR at all)

mvn install:install-file -DgroupId=org.jtestr -DartifactId=jruby-complete -Dpackaging=jar -Dversion=r1c672b495cfd204421f4a7aed17f6135e730a3b2 -Dfile=[PATH_TO_DOWNLOADED.jar]
http://stackoverflow.com/questions/5578729/what-is-needed-in-order-to-run-the-jtestr-samples-or-using-jtestr-at-all

I needed to do this as well when I tried to pull in JTestR resources.

JRuby

JRuby documentation

http://jruby.org/documentation

Sweetens up JRuby Mocha with several helpers to simplify testing Java from Ruby

https://github.com/elight/jrsplenda

This is old code. If it could be resurrected, it might prove useful for testing my Java code.

Unit Testing J2EE from JRuby

http://evan.tiggerpalace.com/unit-testing-jruby-from-java-public.pdf

Again, old. And the speaker isn't there to fill in the context. It sounds like it was a great presentation. Might hold some further clues to successfully testing Java using Ruby.

Using JRuby with Maven

    <dependency>
      <groupId>org.jruby</groupId>
      <artifactId>jruby-core</artifactId>
      <version>1.7.0.RC1</version>
    </dependency>
https://github.com/jruby/jruby/wiki/Using-JRuby-with-Maven

JRuby and Java Code Examples

https://github.com/jruby/jruby/wiki/JRubyAndJavaCodeExamples

Ok; looks a little overwhelming at first glance.

Your First Day with JRuby

http://www.jfokus.se/jfokus10/preso/jf-10_FirstDayWithJRuby.pdf

PDF slideshow; without the speaker, I'm not sure how much worth this will be.

RSpec Expectations

http://rubydoc.info/gems/rspec-expectations/frames

Rjb - Ruby Java Bridge

instance = str.new_with_sig('Ljava.lang.String;', 'hiki is a wiki engine')
http://rjb.rubyforge.org/

Another way to call Ruby from Java code?

Module: Mocha::API

 - (Mock) mock(name, &block) 
 - (Mock) mock(expected_methods_vs_return_values = {}, &block) 
 - (Mock) mock(name, expected_methods_vs_return_values = {}, &block) 
http://gofreerange.com/mocha/docs/Mocha/API.html

Class: RSpec::Core::RakeTask

- (Object) pattern

Glob pattern to match files.

default: 'spec/*/_spec.rb'
http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#pattern-instance_method

Trying to get JRSplenda working again by upgrading some of the calls. I admit, I really didn't have enough background on the project yet to do much besides being dangerous.

RSpec

Behaviour Driven Development framework for Ruby (w/tag 1.2.0)

https://github.com/dchelimsky/rspec/tree/1.2.0

What's the require for "RSpec::Core::RakeTask"?

require 'rspec/core/rake_task'
https://www.relishapp.com/rspec/rspec-core/v/2-4/docs/command-line/rake-task

RSpec runner and formatters

https://github.com/rspec/rspec-core

Why can't I load spectask?

no such file to load -- spec/rake/spectask
https://www.ruby-forum.com/topic/312974

In my case, it is because the code I was revamping used RSpec and I had RSpec2 installed.

Ruby

Why does Ruby 1.9.2 remove “.” from LOAD_PATH, and what's the alternative?

require_relative 'file_to_require'

# or
require './filename'

# or 
export RUBYLIB="."

# or
require File.expand_path(File.join(File.dirname(__FILE__), 'filename'))
http://stackoverflow.com/questions/2900370/why-does-ruby-1-9-2-remove-from-load-path-and-whats-the-alternative

Bundler: how to use without rails?

require "rubygems"

# The part that activates bundler in your app:
require "bundler/setup" 

# require your gems as usual
require "some_gem"
http://stackoverflow.com/questions/6999547/bundler-how-to-use-without-rails

Unit testing in Ruby

http://www.developerfusion.com/article/84444/unit-testing-in-ruby/

There is no Java to speak of here, but this looked pretty good for Ruby development.

Maven

Running specific Maven Test with JVM Args

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <forkMode>pertest</forkMode>
          <argLine>${maven.test.jvmargs}</argLine>
        </configuration>
      </plugin>
http://blog.tfd.co.uk/2007/09/21/running-specific-maven-test-with-jvm-args/

Exec Maven Plugin - Usage

Note: The java goal doesn't spawn a new process. Any VM specific option that you want to pass to the executed class must be passed to the Maven VM using the MAVEN_OPTS environment variable. E.g.

MAVEN_OPTS=-Xmx1024m
http://mojo.codehaus.org/exec-maven-plugin/usage.html

Hoe versions

http://rubygems.org/gems/hoe/versions

Trying to get JRSplenda working required some changes. I thought by going back in time to a previous version of Hoe that I would be able to fix somethings. In the end, I think I simply commented out the references to hoe.

Switch from deprecated Hoe.new to Hoe.spec

https://github.com/collectiveidea/migration_test_helper/pull/5

How to use git to download a particular tag?

git clone will give you the whole repository.

After the clone, you can list the tags with git tag -l and then checkout a specific tag: git checkout tags/<tag_name>
http://stackoverflow.com/questions/791959/how-to-use-git-to-download-a-particular-tag

Install Gem from Github Branch?

$ rake build
http://stackoverflow.com/questions/2823492/install-gem-from-github-branch

It's suggested that "gem" be left off the "rake build" command.

Thursday, July 25, 2013

Work notes; more vim

Vim

Omit 'Pattern not found' error message in Vim script

:%s/x/y/ge
http://stackoverflow.com/questions/1043432/omit-pattern-not-found-error-message-in-vim-script

Advanced Vim macros

http://blog.sanctum.geek.nz/advanced-vim-macros/

Wonderful resource in helping me better understand registers, macros and how to use inside vim functions.

How to create escaped vim characters besides just copying?

<C-V> + somekey = escaped vim character code
http://unix.stackexchange.com/questions/46827/vim-executing-a-key-command-in-a-function

Variables in Vimscript: Registers as Variables

:let @a = "hello!"
http://learnvimscriptthehardway.stevelosh.com/chapters/19.html

How to use vim registers?

# from vim after selecting a block of text
"ny

# where n is the register letter you want to save the text in
http://stackoverflow.com/questions/1497958/how-to-use-vim-registers

Seven habits of effective text editing

:abbr Lunix Linux

vip:w !wc -w

:grep
:cn

[I:
http://www.moolenaar.net/habits.html

Vim Scripting - call function once for all selected lines

fun Foo() range
    ...
endfun
http://stackoverflow.com/questions/8597153/vim-scripting-call-function-once-for-all-selected-lines

Mapping keys in Vim - Tutorial (Part 1)

nunmap - Unmap a normal mode map
vunmap - Unmap a visual and select mode map
xunmap - Unmap a visual mode map
sunmap - Unmap a select mode map
iunmap - Unmap an insert and replace mode map
cunmap - Unmap a command-line mode map
ounmap - Unmap an operator pending mode map
http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_%28Part_1%29

Escaping single quotes

    :echo escape('as''df', '''') 
http://vim.1045645.n5.nabble.com/escape-and-td1200763.html

Clearing/setting the last search

:let @/ = ""
:let @/ = '^\c[a-z0-9]'
http://stackoverflow.com/questions/657447/vim-clear-last-search-highlighting

How do I emulate a keypress inside a Vim function?

" Press CTRL-Y:
normal <Ctrl+v><Ctrl+y>
http://stackoverflow.com/questions/9445273/how-do-i-emulate-a-keypress-inside-a-vim-function

Vim - yank into search register

" First, "Ayw to yank a word into register A
" Then, / ^R A to put the contents of register A into the search string.
http://stackoverflow.com/questions/2312844/vim-yank-into-search-register

Wednesday, July 24, 2013

Work notes; bash

Bash

How to view the rails console history?

less ~/.irb_history
http://stackoverflow.com/questions/11601303/how-to-view-the-rails-console-history

I opened one of my log files from finder and it was automatically opened in textedit. I proceeded to select content using vim key sequences and ended up over-writing a section. "No problem," I thought; "I'll just close the file and not save." To my dismay, textedit didn't even prompt me and saved the file just before closing it. And with no backups, I figured I had lost it.

But wait; I seem to recall that the Rails console has history because I can up-arrow to previous commands. Where is it? How relieved I was to discover '~/.irb_history'.

Exclude items in a list in a bash script

http://moinne.com/blog/ronald/bash/exclude-items-in-a-list-in-a-bash-script

Looks interesting, but I went with a different route.

Bash Shell Loop Over Set of Files

FILES="./ws-idm/src/main/config/server.jks
./ws-ucidm/src/main/config/server.jks
./ws-ucidm-service/src/main/config/server.jks"

for f in $FILES
do
 cp /Users/davidvezzani/java-app/tomcat/conf/server.002.jks $f
done
http://www.cyberciti.biz/faq/bash-loop-over-file/

I needed to update all instances of a Java keystore I was working on.

Monday, July 22, 2013

Work notes; ssl and certificates

SSL; Cipher suites

What are cipher suites?

http://en.wikipedia.org/wiki/Cipher_suite

Configuring Apache to accept a particular list of cipher suites

https://httpd.apache.org/docs/2.0/ssl/ssl_howto.html

Testing ssl connections using OpenSSL's s_client and s_server

http://wiki.wireshark.org/SSL

Bash script to test OpenSSL's supported cipher suites against a given web server

https://www.ssllabs.com/ssltest/index.html

OpenSSL's documentation for ciphers

http://www.openssl.org/docs/apps/ciphers.html#NAME

How to Disable SSL weak Ciphers in Tomcat Server

http://www.fromdev.com/2009/02/tomcat-best-practices-securing-ssl-by.html

How to control the SSL ciphers available to Tomcat

http://stackoverflow.com/questions/7417809/how-to-control-the-ssl-ciphers-available-to-tomcat

Java™ Cryptography Architecture Standard Algorithm Name Documentation; JSSE Cipher Suite Names

http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#jssenames

SSLHandshakeException: Received fatal alert: handshake_failure when setting ciphers on tomcat 7 server

http://stackoverflow.com/questions/15544116/sslhandshakeexception-received-fatal-alert-handshake-failure-when-setting-ciph?rq=1

-keyalg is only one part of the solution; the other peer involved in the ssl conversation must support a compatible hash algorith for OID in addition to supporting a common cipher suite

SSL; handshake

Give me a detailed breakdown of how the ssl-handshake works

http://pic.dhe.ibm.com/infocenter/tivihelp/v2r1/index.jsp?topic=%2Fcom.ibm.itame2.doc_5.1%2Fss7aumst18.htm

An overview of the SSL handshake

http://publib.boulder.ibm.com/infocenter/wmqv6/v6r0/index.jsp?topic=%2Fcom.ibm.mq.csqzas.doc%2Fsy10660_.htm http://pic.dhe.ibm.com/infocenter/tivihelp/v2r1/index.jsp?topic=%2Fcom.ibm.itame2.doc_5.1%2Fss7aumst18.htm

SSL; keytool

Documentation for keytool

http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/keytool.html

Keytool and the -keyalg option

http://stackoverflow.com/questions/15544116/sslhandshakeexception-received-fatal-alert-handshake-failure-when-setting-ciph?rq=1

Java keytool; common commands

https://www.sslshopper.com/article-most-common-java-keytool-keystore-commands.html

Keytool keeps giving SHA256 sigalg instead of requested alg

http://stackoverflow.com/questions/14163889/keytool-keeps-giving-sha256-sigalg-instead-of-requested-alg

keytool - Key and Certificate Management Tool; Supported Algorithms and Key Sizes

http://docs.oracle.com/javase/1.5.0/docs/tooldocs/solaris/keytool.html

How do I generate a 2048 bit CSR using Java Keytool?

http://www.entrust.net/knowledge-base/technote.cfm?tn=8425

keytool - Key and Certificate Management Tool; Option Defaults

http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/keytool.html

SSL; misc

Online tester for SSL-enabled servers

https://www.ssllabs.com/ssltest/index.html

Hash Algorithm OIDs

http://msdn.microsoft.com/en-us/library/ff635603.aspx

SSL; OpenSSL

Creating a self-signed test certificate

http://www.openssl.org/docs/HOWTO/certificates.txt

Public Key Encryption and Digital Signatures using OpenSSL

http://sandilands.info/sgordon/public-key-encryption-and-digital-signatures-using-openssl

these procedures do not involve browsers at all; it's a great example in raw form of how to use certificates for encrypting and signing data

Testing ssl connections using OpenSSL's s_client and s_server

http://wiki.wireshark.org/SSL

Bash script to test OpenSSL's supported cipher suites against a given web server

https://www.ssllabs.com/ssltest/index.html

OpenSSL's documentation for ciphers

http://www.openssl.org/docs/apps/ciphers.html#NAME

Tomcat

How to Disable SSL weak Ciphers in Tomcat Server

http://www.fromdev.com/2009/02/tomcat-best-practices-securing-ssl-by.html

How to control the SSL ciphers available to Tomcat

http://stackoverflow.com/questions/7417809/how-to-control-the-ssl-ciphers-available-to-tomcat

Tomcat configuration; creating a certificate for an SSL-enabled Tomcat server

http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html

HOWTO: Secure SSL in Tomcat and JBoss

http://www.techstacks.com/howto/secure-ssl-in-tomcat.html

The HTTP Connector; SSL Support - BIO and NIO

http://tomcat.apache.org/tomcat-7.0-doc/config/http.html

Vim

How do you do a case insensitive search using a pattern modifier using less?

http://stackoverflow.com/questions/16828/how-do-you-do-a-case-insensitive-search-using-a-pattern-modifier-using-less

How to do case insensitive search in Vim

http://stackoverflow.com/questions/2287440/how-to-do-case-insensitive-search-in-vim

Bash

Bash custom functions

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html

What's the best way to check that environment variables are set in Unix shellscript

http://stackoverflow.com/questions/307503/whats-the-best-way-to-check-that-environment-variables-are-set-in-unix-shellscr

Weblogic

Using Weblogic SSL

http://www.inf.fu-berlin.de/lehre/WS00/SWT/BEA/documentation/docs51/classdocs/API_secure.html

Introduction to WebLogic Security; Cipher Suites

http://docs.oracle.com/cd/E13222_01/wls/docs81/secintro/concepts.html

Understanding WebLogic Security; J2EE and WebLogic Security

http://docs.oracle.com/cd/E11035_01/wls100/secintro/concepts.html

Wednesday, July 3, 2013

Work notes; vim macros and mappings

Multiline capability for GVim

"Instead you can use \_., which means "match any single character including newline". It's a bit shorter than what you have. See :h /\_.."
/This\_.*text/
http://stackoverflow.com/questions/784176/multi-line-regex-support-in-vim

How to use vim registers?

Great reference on what registers are and how to use them. http://stackoverflow.com/questions/1497958/how-to-use-vim-registers

vim clear last search highlighting

    :let @/ = ""
http://stackoverflow.com/questions/657447/vim-clear-last-search-highlighting

Multiple search and replace in one line

There are a couple ways of doing multiple commands

one

:%s/aaa/bbb/e | %s/111/222/e

two (<CR> represents actual carriage returns)

:%s/aaa/bbb/e<CR>%s/111/222/e
http://stackoverflow.com/questions/4737099/multiple-search-and-replace-in-one-line

Vim: Visual mode maps

There is a lot more in the web references, but here are some of the examples.
:vnoremap g/ y/<C-R>"<CR>
:vnoremap qq <Esc>`>a'<Esc>`<i'<Esc>
View all current mappings
:vmap
Remove a mapping
:vunmap g/
http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_%28Part_1%29#Visual_mode_maps

Deselecting matching strings after search-and-replace in Vim

Stumbled upon command to turn highlighting on/off. Will remove highlighting from the current search. Highlighting will return on your next search.
:nohlsearch 
Will disable highlighting for your current vim session.
:set nohlsearch
Will clear the search pattern register (so that n etc. won't work).
:let @/="" 
http://stackoverflow.com/questions/10499866/deselecting-matching-strings-after-search-and-replace-in-vim

Write your own Vim function

function! JournalBasicConversion(...)
  :%s/^}}/ç/g
  :%s/\n{{\(\([\r\n]\+[^ç]\+.*\)\+\)\nç/\r<pre class=\"code\">\1\r<\/pre>/eg
  :%s/^==\+ \(.\+\)$/<h3>\1<\/h3>/eg
  :%s/^\[\([^\]]\+\)\]$/<a href=\"\1\" target=\"_new\">\1<\/a>/eg
  :%s/>\n\n/>\r<br \/>\r\r/eg
endfunction
http://vim.wikia.com/wiki/Write_your_own_Vim_function

Multiline capability for GVim

"Instead you can use \_., which means "match any single character including newline". It's a bit shorter than what you have. See :h /\_.."
/This\_.*text/
http://stackoverflow.com/questions/784176/multi-line-regex-support-in-vim

How to use vim registers?

Great reference on what registers are and how to use them. http://stackoverflow.com/questions/1497958/how-to-use-vim-registers

vim clear last search highlighting

    :let @/ = ""
http://stackoverflow.com/questions/657447/vim-clear-last-search-highlighting

Multiple search and replace in one line

There are a couple ways of doing multiple commands one
:%s/aaa/bbb/e | %s/111/222/e
two (<CR> represents actual carriage returns)
:%s/aaa/bbb/e<CR>%s/111/222/e
http://stackoverflow.com/questions/4737099/multiple-search-and-replace-in-one-line

Vim: Visual mode maps

There is a lot more in the web references, but here are some of the examples.
:vnoremap g/ y/<C-R>"<CR>
:vnoremap qq <Esc>`>a'<Esc>`<i'<Esc>
View all current mappings
:vmap
Remove a mapping
:vunmap g/
http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_%28Part_1%29#Visual_mode_maps

Deselecting matching strings after search-and-replace in Vim

Stumbled upon command to turn highlighting on/off. Will remove highlighting from the current search. Highlighting will return on your next search.
:nohlsearch 
Will disable highlighting for your current vim session.
:set nohlsearch
Will clear the search pattern register (so that n etc. won't work).
:let @/="" 
http://stackoverflow.com/questions/10499866/deselecting-matching-strings-after-search-and-replace-in-vim

When doing a search/replace on a visual selection, how can I keep the selection?

Great summary on how selections work in Vim.
  1. Run gv to reselect the visual area, and use : from there.
  2. Use the special visual range notation * as a shortcut. For example, you can run :*s/in/out/g to run a substitution on the lines of the previous visual area.
gv

:*s/in/out/g
http://www.reddit.com/r/vim/comments/1ghqng/when_doing_a_searchreplace_on_a_visual_selection/

Search and replace in a visual selection

"The substitute command (:s) applies to whole lines, however the \%V atom will restrict a pattern so that it matches only inside the visual selection. This works with characterwise and blockwise selection (and is not needed with linewise selection). "

http://vim.wikia.com/wiki/Search_and_replace_in_a_visual_selection

Scripting common tasks in Vim

Looks interesting...

"Here you can find a bunch of useful macros, also I recommend you to learn well the useful q macro recording command, it's very very powerful..."

http://stackoverflow.com/questions/255624/scripting-common-tasks-in-vim

VIM: how to execute content of the buffer?

This led me in the right direction to better understand macros, buffers and their relationship to one another.

http://stackoverflow.com/questions/5326430/vim-how-to-execute-content-of-the-buffer

CSS Gradient Background Maker

Wonderful resource to create gradients; many versions to support different browsers.

http://ie.microsoft.com/testdrive/graphics/cssgradientbackgroundmaker/

When doing a search/replace on a visual selection, how can I keep the selection?

Great summary on how selections work in Vim.
  1. Run gv to reselect the visual area, and use : from there.
  2. Use the special visual range notation * as a shortcut. For example, you can run :*s/in/out/g to run a substitution on the lines of the previous visual area.
gv

:*s/in/out/g
http://www.reddit.com/r/vim/comments/1ghqng/when_doing_a_searchreplace_on_a_visual_selection/

Search and replace in a visual selection

"The substitute command (:s) applies to whole lines, however the \%V atom will restrict a pattern so that it matches only inside the visual selection. This works with characterwise and blockwise selection (and is not needed with linewise selection). "

http://vim.wikia.com/wiki/Search_and_replace_in_a_visual_selection

Scripting common tasks in Vim

Looks interesting...

"Here you can find a bunch of useful macros, also I recommend you to learn well the useful q macro recording command, it's very very powerful..."

http://stackoverflow.com/questions/255624/scripting-common-tasks-in-vim

VIM: how to execute content of the buffer?

This led me in the right direction to better understand macros, buffers and their relationship to one another.

http://stackoverflow.com/questions/5326430/vim-how-to-execute-content-of-the-buffer

CSS Gradient Background Maker

Wonderful resource to create gradients; many versions to support different browsers.

http://ie.microsoft.com/testdrive/graphics/cssgradientbackgroundmaker/

Work notes; getting to know Oracle

What is the current (or default) schema in the database being accessed?

select user from dual;
https://forums.oracle.com/thread/495481

How do I create a schema?

Schemas are associated with database users in a one-to-one relationship. To create a schema, create a new user. This usually means you will need to have administrative access to the database.
CREATE USER sidney 
    IDENTIFIED BY out_standing1 
    DEFAULT TABLESPACE example 
    QUOTA 10M ON example 
    TEMPORARY TABLESPACE temp
    QUOTA 5M ON system 
    PROFILE app_user 
    PASSWORD EXPIRE;
http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_8003.htm#i2065278

How do I find out what schemas are available on the database I am accessesing?

Since schemas are linked to user accounts,
SELECT username FROM all_users ORDER BY username;
http://stackoverflow.com/questions/4833459/oracle-sql-query-for-listing-all-schemas-in-a-db

Creating table in other schema oracle using select.

CREATE TABLE schema1.table1 AS (SELECT * FROM SCHEMA.table@LinkedDB);
http://stackoverflow.com/questions/10730596/creating-table-in-other-schema-oracle-using-select

When issuing a command in maven that appears to go wrong, guard the arguments with double-quotes:

mvn install:install-file "-DgroupId=org.mozilla" "-DartifactId=jss" "-Dversion=4.2.5" "-Dpackaging=jar" "-Dfile=C:\Users\AArmijos\workspace\componentes-1.0.4\deps\jss-4.2.5.jar"
http://stackoverflow.com/questions/16348459/maven-3-0-5-command-error-the-goal-you-specified-requires-a-project-to-execute

Add the Oracle JDBC driver to your Maven environment.

mvn install:install-file "-Dfile={Path/to/your/ojdbc.jar}" "-DgroupId=com.oracle" "-DartifactId=ojdbc6" "-Dversion=11.2.0" "-Dpackaging=jar"
http://www.mkyong.com/maven/how-to-add-oracle-jdbc-driver-in-your-maven-local-repository/

Use the Oracle JDBC driver in your Maven project.

# pom.xml
   <!-- ORACLE JDBC driver, need install yourself -->
   <dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0</version>
   </dependency>
<br />

# hibernate.cfg.xml
    <hibernate-configuration>
     <session-factory>
      <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:MKYONG</property>
      <property name="hibernate.connection.username">mkyong</property>
      <property name="hibernate.connection.password">password</property>
      <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
      <property name="hibernate.default_schema">MKYONG</property>
      <property name="show_sql">true</property>
      <mapping resource="com/mkyong/user/DBUser.hbm.xml"></mapping>
    </session-factory>
   </hibernate-configuration>
<br />

# HibernateUtil.java
 static {
  try {
   factory = new AnnotationConfiguration().configure().
     setNamingStrategy(SrvTableNamePrefixNamingStrategy.INSTANCE).
     // addPackage("com.xyz") //add package if used.
     buildSessionFactory();
   
  } catch (Throwable ex) {
   // Make sure you log the exception, as it might be swallowed
   System.err.println("Failed to create sessionFactory object." + ex);
   throw new ExceptionInInitializerError(ex);
  }
 }

# Runner.java
 protected Session getSession(){
  Session session = HibernateUtil.getSessionFactory().openSession();
  return session;
 }


Convert a Maven project to an eclipse project.

mvn eclipse:eclipse
http://www.mkyong.com/hibernate/maven-3-hibernate-3-6-oracle-11g-example-xml-mapping/

Skip running the test phase when running Maven.

"-Dmaven.test.skip=true"
http://stackoverflow.com/questions/1607315/build-maven-project-without-running-unit-tests

What is the "printf" equivalent for Java?

String status = String.format("The rename status is (%d)", RENAME_SUCCEEDED);
http://alvinalexander.com/blog/post/java/use-string-format-java-string-output

Strip off '[' and ']' from a line.

:%s/^\[\([^\]]\+\)\]$/\1/g

Convert code blocks using '{{' and '}}' to a <pre class="code"> block

:%s/^}}/ç/g
:%s/\n{{\(\([\r\n]\+[^ç]\+.*\)\+\)\nç/\r<pre class=\"code\">\1\r<\/pre>/g

Create titles

:%s/^==\+ \(.\+\)$/<h4>\1<\/h4>/g

Create links

:%s/^\[\([^\]]\+\)\]$/<a href=\"\1\" target=\"_new\">\1<\/a>/g

Create breaks

:%s/>\n\n/>\r<br \/>\r\r/g

Escape < in code example blocks

:%s/\t</\t \&lt;/g
:%s/</\&lt;/g

Friday, May 31, 2013

Configuring Tomcat and SOAP client for one-way and two-way ssl



I will soon be testing the connectivity between two servers. Both involve self-signed certificates that are shared in one another's trust store. There are many details that factor in to a successful connection between two servers. If you're not using a CA certificate chain, you need to make sure that you have exported valid public certificates and then installed peer certificates in the server's trust chain. If signing is used, what ciphers are your servers configured to use? Are your certificates expired? Does the DN value match the domain of the server's involved (host-name verification). Again, there are many factors.

A pragmatic approach to testing the ssl connection between two servers is to execute a series of tests in building block fashion. First, just make sure that you and your peer servers have adjusted one another's firewalls to allow access. Do this first without mixing in certificates or ssl. If there are proxies involved (load balancing?), resolve any issues there too. I'm not sure if there are proxies that can simply pass the https request along to the next destination; it feels like there might be some security issues there. I have seen examples where the proxies handle the SSL and then package up the attributes (as environment variables?) and pass them along to secondary servers. Once the two peer servers can issue commands to one another (e.g., web service), you're ready for the next step.

One-way ssl can be thought of as a client verifying the server, but the server doesn't care didley-squat about who the client is.


  1. Client knocks on the server's door
  2. Server opens the door without asking who's there and hands them a copy of a key to their garage (their public key), waves goodbye, and closes the door
  3. Client opens the garage, drops off some boxes of clothes and grabs a soda from the outside fridge
  4. Client keeps Server's key on keychain for the next visit; no big deal if he loses it. He can always knock on the door again and get another copy from the Server
In order to carry out a 'one-way ssl' test, the server must extract their public key from their keystore and send it to the client. The client then imports the server's public key into his trust store (usually it is added to the keystore, as with Java Key Store files (JKS)). The server should not enforce client authentication (that comes later with '2-way ssl'); the client, however, should authenticate the server's certificate by verifying that his copy of the server's public key matches the one the server sends in response to the client's initial request. If that verification succeeds, the client has good reason to trust the server. Again, the server doesn't care who the client is; one may describe it as blind trust.The server  should care enough to match its certificate CN value with the domain name it is using. Most clients do a host-name verification as part of authenticating the server it wishes to connect to. If the client is not rejected (given that previous non-ssl tests were successful), and verifies the protocol used is https, then the 'one-way ssl' test should be deemed successful.

Two-way ssl involves two people who are more reserved. This server has a more cynical view on life and won't hand out keys to just any guy on the street.


  1. Client knocks on the server's door
  2. Server asks the Client for his name, driver's license and another form of photo id.
  3. He calls up the DMV to verify that there is such a person and that their license was issued from the DMV.
  4. He may check to see if the Client is on the list of people he is expecting to stop by if an appointment was made in the past
  5. If the Server doesn't trust the Client, he throws the Client's references out the mail slot in the front door; the door is certainly not opened.
  6. If the Server does trust the Client, he opens the door and gives him a key.
  7. Like with the other case, the Client then has access to designated rooms in the Server's house as the Server sees fit.
In order to carry out a 'two-way ssl' test, in addition to the steps taken in the 'one-way ssl' test, the client will also need to send his public certificate to the server. The server will then install the client's certificate in the server's trust store. The server should ensure that the client authentication option is turned on. In two steps, 1) the server authenticates the client first and then 2) the client authenticates the server. If the client doesn't trust the server, he can always walk away. Ever seen one of those browser warnings suggesting that the server you're connecting to may not be trustworthy? At that point, you may choose to make an exception and trust them anyway or you slam the door in the server's face.

In preparation for running these tests, I wanted to make sure what configuration changes need to be made for each test phase. I ran tests and pulled together the following matrix:


Green == "can access" and Red == "rejected". Some rejection takes place before the client is even out of the starting block. This could be due to the fact that you don't have any certificates and the server is asking for one. This often times shows itself as an ugly and cryptic error message displayed in your browser or an exception that is thrown by your client code. If the client has a public certificate to hand to the server, if upon evalation of the client's certificate the server finds it untrustworthy, the client can be rejected that way too. These tend to represent themselves as return codes 401 or 403.

Many of the colored boxes have reference numbers in them. I have collected some screen captures to provide some context for the test results in the matrix.

Server Configuration

Our server is using Apache CXF to manage our web service behavior, including authentication. Important components in this configuration are:

Apache Tomcat

  • server trust store includes client's public key (required for two-way ssl only)
  • tomcat/conf/server.xml
  • tomcat/libexec/conf/tomcat-users.xml
A server cannot establish an ssl-encrypted connection or handle https requests without a valid certificate in its keystore. If a CA authority chain is not used, a 'direct trust' must be established by including the client's public key in the server's trust store.

The Tomcat server must be listening on an ssl-enabled port. Whether to set clientAuth to 'true' or 'false' is determined by whether the server will use one-way ssl or two-way ssl.

If two-way ssl is configured, users will be authorized once they have been authenticated. A 'user' entry is made, associating the full DN value from a client certificate with a role. The role will then be used later on to determine if a user is authorized to access a resource.

Web Service Application

  • webapp/src/main/webapp/WEB-INF/web.xml (example)
The web.xml is used to configure a web application's behavior once it has been deployed to Tomcat. In our case, a security-constraint is defined to check the user's role (associated by Tomcat when the client is authenticated) with the resource being requested (in this case, all web service endpoints).

One-Way SSL (Server)

Our server can establish one-way ssl by:
  • web.xml: remove the security-constraint, login-config and security-role
  • server.xml: ensure that clientAuth="false"

Two-Way SSL (Server)

Our server can establish two-way ssl by:
  • web.xml: include the security-constraint, login-config and security-role
  • server.xml: ensure that clientAuth="false" (yes, still false; CXF is handling the authentication and authorization)
  • tomcat-users.xml: ensure the client's DN has been included in the list of authorized users and the correct role(s) has been applied. The DN must be identical to how it appears in the client's public certificate.

Client Configuration

Our test client also uses Apache CXF to interface with an Apache CXF-driven server, including authentication. Important components in this configuration are:

SOAP Client Application

  • client trust store includes servers's public key (for both one-way and two-way ssl)
  • ws-ucidm/src/main/java/xxx/xxx/idm/UCIDMServicesClient.java (snippet)
  • ws-ucidm/src/main/resources/ClientConfig.xml (example)
A client cannot communicate with a server that demands an ssl-encrypted connection or send https requests without obtaining a valid certificate from it's peer and including it in its keystore.  If a CA authority chain is not used, a 'direct trust' must be established by including the server's public key in the client's trust store.  Whether one-way or two-way ssl, a client must always have it's peer's certificate.

Apache CXF uses Spring xml configuration files to authenticate the server.  The client code pulls in this configuration file and automatically handles the ssl handshaking required to establish a secure connection.  I believe it also verifies the signature and handles the encryption/decryption of the SOAP body.  With the Spring xml configuration in place, the Java client need only include 4 lines of code to automatically handle encryption and signing for you.

One-Way SSL (Client)

The burden of determining one-way or two-way ssl lies primarily with the server.  In either case, the client code will need to include the server's public certificate in it's own trust store.  This means the client must have an unexpired certificate to go along with his trust store, although in the case of one-way ssl, the server won't even ask for the client's public certificate.

Two-Way SSL (Client)

The client must provide a public certificate to the server that is trusted by the server.  Now a particular certificate must be used, not just any one.

Surely there are lots of things that could go wrong in this test, factors that weren't even mentioned here. But I hope this overview provides enough to understand what's going on so that troubleshooting can be more easily accomplished.